/
jws.clj
176 lines (155 loc) · 6.97 KB
/
jws.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;; Copyright (c) 2014-2018 Andrey Antukh <niwi@niwi.nz>
;;
;; Licensed under the Apache License, Version 2.0 (the "License")
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;; Links to rfcs:
;; - http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32
;; - http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-40
;; - https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
(ns buddy.sign.jws
"Json Web Signature implementation."
(:require [buddy.core.codecs :as codecs]
[buddy.core.codecs.base64 :as b64]
[buddy.core.mac :as mac]
[buddy.core.dsa :as dsa]
[buddy.sign.util :as util]
[buddy.util.ecdsa :refer [transcode-to-der transcode-to-concat]]
[clojure.string :as str]
[cheshire.core :as json]))
(def +signers-map+
"Supported algorithms."
{:hs256 {:signer #(mac/hash %1 {:alg :hmac+sha256 :key %2})
:verifier #(mac/verify %1 %2 {:alg :hmac+sha256 :key %3})}
:hs384 {:signer #(mac/hash %1 {:alg :hmac+sha384 :key %2})
:verifier #(mac/verify %1 %2 {:alg :hmac+sha384 :key %3})}
:hs512 {:signer #(mac/hash %1 {:alg :hmac+sha512 :key %2})
:verifier #(mac/verify %1 %2 {:alg :hmac+sha512 :key %3})}
;; NOT RECOMMENDED
:rs256 {:signer #(dsa/sign %1 {:alg :rsassa-pkcs15+sha256 :key %2})
:verifier #(dsa/verify %1 %2 {:alg :rsassa-pkcs15+sha256 :key %3})}
:rs384 {:signer #(dsa/sign %1 {:alg :rsassa-pkcs15+sha384 :key %2})
:verifier #(dsa/verify %1 %2 {:alg :rsassa-pkcs15+sha384 :key %3})}
:rs512 {:signer #(dsa/sign %1 {:alg :rsassa-pkcs15+sha512 :key %2})
:verifier #(dsa/verify %1 %2 {:alg :rsassa-pkcs15+sha512 :key %3})}
:ps256 {:signer #(dsa/sign %1 {:alg :rsassa-pss+sha256 :key %2})
:verifier #(dsa/verify %1 %2 {:alg :rsassa-pss+sha256 :key %3})}
:ps384 {:signer #(dsa/sign %1 {:alg :rsassa-pss+sha384 :key %2})
:verifier #(dsa/verify %1 %2 {:alg :rsassa-pss+sha384 :key %3})}
:ps512 {:signer #(dsa/sign %1 {:alg :rsassa-pss+sha512 :key %2})
:verifier #(dsa/verify %1 %2 {:alg :rsassa-pss+sha512 :key %3})}
;; ECDSA with signature conversions
:es256 {:signer #(-> (dsa/sign %1 {:alg :ecdsa+sha256 :key %2})
(transcode-to-concat 64))
:verifier #(dsa/verify %1 (transcode-to-der %2) {:alg :ecdsa+sha256 :key %3})}
:es384 {:signer #(-> (dsa/sign %1 {:alg :ecdsa+sha384 :key %2})
(transcode-to-concat 96))
:verifier #(dsa/verify %1 (transcode-to-der %2) {:alg :ecdsa+sha384 :key %3})}
:es512 {:signer #(-> (dsa/sign %1 {:alg :ecdsa+sha512 :key %2})
(transcode-to-concat 132))
:verifier #(dsa/verify %1 (transcode-to-der %2) {:alg :ecdsa+sha512 :key %3})}
:eddsa {:signer #(dsa/sign %1 {:alg :eddsa :key %2})
:verifier #(dsa/verify %1 %2 {:alg :eddsa :key %3})}})
;; --- Implementation
(defn- encode-header
[header]
(-> header
(update :alg #(if (= % :eddsa) "EdDSA" (str/upper-case (name %))))
(json/generate-string)
(b64/encode true)
(codecs/bytes->str)))
(defn- parse-header
[^String data]
(try
(let [header (-> (b64/decode data)
(codecs/bytes->str)
(json/parse-string true))]
(when-not (map? header)
(throw (ex-info "Message seems corrupt or manipulated."
{:type :validation :cause :header})))
(update header :alg #(keyword (str/lower-case %))))
(catch java.lang.NullPointerException e
(throw (ex-info "Message seems corrupt or manipulated."
{:type :validation :cause :header})))
(catch com.fasterxml.jackson.core.JsonParseException e
(throw (ex-info "Message seems corrupt or manipulated."
{:type :validation :cause :header})))))
(defn- encode-payload
[input]
(-> (b64/encode input true)
(codecs/bytes->str)))
(defn- decode-payload
[payload]
(b64/decode payload))
(defn- calculate-signature
"Given the bunch of bytes, a private key and algorithm,
return a calculated signature as byte array."
[{:keys [key alg header payload]}]
(let [signer (get-in +signers-map+ [alg :signer])
authdata (str/join "." [header payload])]
(-> (signer authdata key)
(b64/encode true)
(codecs/bytes->str))))
(defn- verify-signature
"Given a bunch of bytes, a previously generated
signature, the private key and algorithm, return
signature matches or not."
[{:keys [alg signature key header payload]}]
(let [verifier (get-in +signers-map+ [alg :verifier])
authdata (str/join "." [header payload])
signature (b64/decode signature)]
(verifier authdata signature key)))
(defn- split-jws-message
[message]
(str/split message #"\." 3))
;; --- Public Api
(defn decode-header
"Given a message, decode the header.
WARNING: This does not perform any signature validation."
[input]
(let [[header] (split-jws-message input)]
(parse-header header)))
(defn sign
"Sign arbitrary length string/byte array using
json web token/signature."
[payload pkey & [{:keys [alg header] :or {alg :hs256} :as opts}]]
{:pre [payload]}
(let [header (-> (merge {:alg alg} header)
(encode-header))
payload (encode-payload payload)
signature (calculate-signature {:key pkey
:alg alg
:header header
:payload payload})]
(str/join "." [header payload signature])))
(defn unsign
"Given a signed message, verify it and return
the decoded payload."
([input pkey] (unsign input pkey nil))
([input pkey {:keys [alg] :or {alg :hs256}}]
(let [[header payload signature] (split-jws-message input)
header-data (parse-header header)]
(when-not
(try
(verify-signature {:key (util/resolve-key pkey header-data)
:signature signature
:alg alg
:header header
:payload payload})
(catch java.security.SignatureException se
(throw (ex-info "Message seems corrupt or manipulated."
{:type :validation :cause :signature}
se))))
(throw (ex-info "Message seems corrupt or manipulated."
{:type :validation :cause :signature})))
(decode-payload payload))))
(util/defalias encode sign)
(util/defalias decode unsign)