-
Notifications
You must be signed in to change notification settings - Fork 6
/
jws.cljc
176 lines (156 loc) · 7.04 KB
/
jws.cljc
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
(ns axel-f.buddy.jws
(:require [axel-f.buddy.codecs.base64 :as b64]
[axel-f.buddy.codecs.json :as json]
[axel-f.buddy.codecs :as codecs]
[axel-f.buddy.mac :as mac]
[axel-f.buddy.dsa :as dsa]
#?(:clj [axel-f.buddy.util.ecdsa :refer [transcode-to-der transcode-to-concat]])
[clojure.string :as string]))
(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})}
;; ECDSA with signature conversions
:es256 {:signer #?(:clj #(-> (dsa/sign %1 {:alg :ecdsa+sha256 :key %2}) (transcode-to-concat 64))
:cljs (fn [& _args] "signing+not+yet+implemented"))
:verifier #?(:clj #(dsa/verify %1 (transcode-to-der %2) {:alg :ecdsa+sha256 :key %3})
:cljs (fn [& _args] true))}
:es384 {:signer #?(:clj #(-> (dsa/sign %1 {:alg :ecdsa+sha384 :key %2}) (transcode-to-concat 96))
:cljs (fn [& _args] "signing+not+yet+implemented"))
:verifier #?(:clj #(dsa/verify %1 (transcode-to-der %2) {:alg :ecdsa+sha384 :key %3})
:cljs (fn [& _args] true))}
:es512 {:signer #?(:clj #(-> (dsa/sign %1 {:alg :ecdsa+sha512 :key %2}) (transcode-to-concat 132))
:cljs (fn [& _args] "signing+not+yet+implemented"))
:verifier #?(:clj #(dsa/verify %1 (transcode-to-der %2) {:alg :ecdsa+sha512 :key %3})
:cljs (fn [& _args] true))}
: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})}})
;; --- Implementation
(defn- encode-header
[header]
(-> header
(update :alg #(if (= % :eddsa) "EdDSA" (string/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 (string/lower-case %))))
(catch #?(:clj com.fasterxml.jackson.core.JsonParseException
:cljs js/Error) _error
(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 (string/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 (string/join "." [header payload])
signature (b64/decode signature)]
(verifier authdata signature key)))
(defn- split-jws-message
[message]
(string/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}}]
{: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})]
(string/join "." [header payload signature])))
(defn verify
[input pkey & {:keys [alg] :or {alg :hs256}}]
(let [[header payload signature] (split-jws-message input)
header-data (try
(parse-header header)
(catch #?(:clj Throwable
:cljs js/Error) _
{}))]
(cond
(empty? header-data)
{"error" {"type" 1
"message" "Message seems corrupt or manipulated. Headers can not be extracted."}}
(not= alg (:alg header-data))
{"error" {"type" 0
"message" "Algorithm mismatch"}}
(not (verify-signature {:key pkey
:signature signature
:alg alg
:header header
:payload payload}))
{"error" {"type" 1
"message" "Message seems corrupt or manipulated."}}
;; TODO check exp
:else
{"payload" (decode-payload payload)})))
(defn extract
"Given a signed message, verify it and return
the decoded payload."
[input pkey & {:keys [alg] :or {alg :hs256}}]
(let [[header payload signature] (split-jws-message input)]
(when-not
(try
(verify-signature {:key pkey
:signature signature
:alg alg
:header header
:payload payload})
(catch #?(:clj java.security.SignatureException
:cljs js/Error) 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)))