blob: e29a1c797b343615a54b08223b7526c3028a3177 [file] [log] [blame]
Remi Tricot-Le Breton7feb3612021-10-01 15:36:54 +02001/*
2 * JSON Web Token (JWT) processing
3 *
4 * Copyright 2021 HAProxy Technologies
5 * Remi Tricot-Le Breton <rlebreton@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <import/ebmbtree.h>
14#include <import/ebsttree.h>
15
16#include <haproxy/api.h>
17#include <haproxy/tools.h>
18#include <haproxy/openssl-compat.h>
19#include <haproxy/base64.h>
20#include <haproxy/jwt.h>
21
22
23#ifdef USE_OPENSSL
Remi Tricot-Le Breton864089e2021-10-01 15:36:56 +020024/* Tree into which the public certificates used to validate JWTs will be stored. */
25static struct eb_root jwt_cert_tree = EB_ROOT_UNIQUE;
26
Remi Tricot-Le Breton7feb3612021-10-01 15:36:54 +020027/*
28 * The possible algorithm strings that can be found in a JWS's JOSE header are
29 * defined in section 3.1 of RFC7518.
30 */
31enum jwt_alg jwt_parse_alg(const char *alg_str, unsigned int alg_len)
32{
33 enum jwt_alg alg = JWT_ALG_DEFAULT;
34
35 /* Algorithms are all 5 characters long apart from "none". */
36 if (alg_len < sizeof("HS256")-1) {
37 if (strncmp("none", alg_str, alg_len) == 0)
38 alg = JWS_ALG_NONE;
39 return alg;
40 }
41
42 if (alg == JWT_ALG_DEFAULT) {
43 switch(*alg_str++) {
44 case 'H':
45 if (strncmp(alg_str, "S256", alg_len-1) == 0)
46 alg = JWS_ALG_HS256;
47 else if (strncmp(alg_str, "S384", alg_len-1) == 0)
48 alg = JWS_ALG_HS384;
49 else if (strncmp(alg_str, "S512", alg_len-1) == 0)
50 alg = JWS_ALG_HS512;
51 break;
52 case 'R':
53 if (strncmp(alg_str, "S256", alg_len-1) == 0)
54 alg = JWS_ALG_RS256;
55 else if (strncmp(alg_str, "S384", alg_len-1) == 0)
56 alg = JWS_ALG_RS384;
57 else if (strncmp(alg_str, "S512", alg_len-1) == 0)
58 alg = JWS_ALG_RS512;
59 break;
60 case 'E':
61 if (strncmp(alg_str, "S256", alg_len-1) == 0)
62 alg = JWS_ALG_ES256;
63 else if (strncmp(alg_str, "S384", alg_len-1) == 0)
64 alg = JWS_ALG_ES384;
65 else if (strncmp(alg_str, "S512", alg_len-1) == 0)
66 alg = JWS_ALG_ES512;
67 break;
68 case 'P':
69 if (strncmp(alg_str, "S256", alg_len-1) == 0)
70 alg = JWS_ALG_PS256;
71 else if (strncmp(alg_str, "S384", alg_len-1) == 0)
72 alg = JWS_ALG_PS384;
73 else if (strncmp(alg_str, "S512", alg_len-1) == 0)
74 alg = JWS_ALG_PS512;
75 break;
76 default:
77 break;
78 }
79 }
80
81 return alg;
82}
Remi Tricot-Le Bretone0d3c002021-10-01 15:36:55 +020083
Remi Tricot-Le Bretone0d3c002021-10-01 15:36:55 +020084/*
85 * Split a JWT into its separate dot-separated parts.
86 * Since only JWS following the Compact Serialization format are managed for
87 * now, we don't need to manage more than three subparts in the tokens.
88 * See section 3.1 of RFC7515 for more information about JWS Compact
89 * Serialization.
90 * Returns 0 in case of success.
91 */
92int jwt_tokenize(const struct buffer *jwt, struct jwt_item *items, unsigned int *item_num)
93{
94 char *ptr = jwt->area;
95 char *jwt_end = jwt->area + jwt->data;
96 unsigned int index = 0;
97 unsigned int length = 0;
98
99 if (index < *item_num) {
100 items[index].start = ptr;
101 items[index].length = 0;
102 }
103
104 while (index < *item_num && ptr < jwt_end) {
105 if (*ptr++ == '.') {
106 items[index++].length = length;
107
108 if (index == *item_num)
109 return -1;
110 items[index].start = ptr;
111 items[index].length = 0;
112 length = 0;
113 } else
114 ++length;
115 }
116
117 if (index < *item_num)
118 items[index].length = length;
119
120 *item_num = (index+1);
121
122 return (ptr != jwt_end);
123}
124
Remi Tricot-Le Breton864089e2021-10-01 15:36:56 +0200125/*
126 * Parse a public certificate and insert it into the jwt_cert_tree.
127 * Returns 0 in case of success.
128 */
129int jwt_tree_load_cert(char *path, int pathlen, char **err)
130{
131 int retval = -1;
132 struct jwt_cert_tree_entry *entry = NULL;
133 EVP_PKEY *pkey = NULL;
134 BIO *bio = NULL;
135
136 bio = BIO_new(BIO_s_file());
137 if (!bio) {
138 memprintf(err, "%sunable to allocate memory (BIO).\n", err && *err ? *err : "");
139 goto end;
140 }
141
142 if (BIO_read_filename(bio, path) == 1) {
143
144 pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
145
146 if (!pkey) {
147 memprintf(err, "%sfile not found (%s)\n", err && *err ? *err : "", path);
148 goto end;
149 }
150
151 entry = calloc(1, sizeof(*entry) + pathlen + 1);
152 if (!entry) {
153 memprintf(err, "%sunable to allocate memory (jwt_cert_tree_entry).\n", err && *err ? *err : "");
154 goto end;
155 }
156
157 memcpy(entry->path, path, pathlen + 1);
158 entry->pkey = pkey;
159
160 ebst_insert(&jwt_cert_tree, &entry->node);
161 retval = 0;
162 }
163
164end:
165 BIO_free(bio);
166 return retval;
167}
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200168
169/*
170 * Calculate the HMAC signature of a specific JWT and check that it matches the
171 * one included in the token.
172 * Returns 1 in case of success.
173 */
174static enum jwt_vrfy_status
175jwt_jwsverify_hmac(const struct jwt_ctx *ctx, const struct buffer *decoded_signature)
176{
177 const EVP_MD *evp = NULL;
178 unsigned char *signature = NULL;
179 unsigned int signature_length = 0;
180 struct buffer *trash = NULL;
181 unsigned char *hmac_res = NULL;
182 enum jwt_vrfy_status retval = JWT_VRFY_KO;
183
184 trash = alloc_trash_chunk();
185 if (!trash)
186 return JWT_VRFY_OUT_OF_MEMORY;
187
188 signature = (unsigned char*)trash->area;
189 signature_length = trash->size;
190
191 switch(ctx->alg) {
192 case JWS_ALG_HS256:
193 evp = EVP_sha256();
194 break;
195 case JWS_ALG_HS384:
196 evp = EVP_sha384();
197 break;
198 case JWS_ALG_HS512:
199 evp = EVP_sha512();
200 break;
201 default: break;
202 }
203
204 hmac_res = HMAC(evp, ctx->key, ctx->key_length, (const unsigned char*)ctx->jose.start,
205 ctx->jose.length + ctx->claims.length + 1, signature, &signature_length);
206
207 if (hmac_res && signature_length == decoded_signature->data &&
Willy Tarreauce16db42021-10-15 11:52:41 +0200208 (CRYPTO_memcmp(decoded_signature->area, signature, signature_length) == 0))
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200209 retval = JWT_VRFY_OK;
210
211 free_trash_chunk(trash);
212
213 return retval;
214}
215
216/*
217 * Check that the signature included in a JWT signed via RSA or ECDSA is valid
218 * and can be verified thanks to a given public certificate.
219 * Returns 1 in case of success.
220 */
221static enum jwt_vrfy_status
222jwt_jwsverify_rsa_ecdsa(const struct jwt_ctx *ctx, const struct buffer *decoded_signature)
223{
224 const EVP_MD *evp = NULL;
225 EVP_MD_CTX *evp_md_ctx;
226 enum jwt_vrfy_status retval = JWT_VRFY_KO;
227 struct buffer *trash = NULL;
228 struct ebmb_node *eb;
229 struct jwt_cert_tree_entry *entry = NULL;
230
231 trash = alloc_trash_chunk();
232 if (!trash)
233 return JWT_VRFY_OUT_OF_MEMORY;
234
235 switch(ctx->alg) {
236 case JWS_ALG_RS256:
237 case JWS_ALG_ES256:
238 evp = EVP_sha256();
239 break;
240 case JWS_ALG_RS384:
241 case JWS_ALG_ES384:
242 evp = EVP_sha384();
243 break;
244 case JWS_ALG_RS512:
245 case JWS_ALG_ES512:
246 evp = EVP_sha512();
247 break;
248 default: break;
249 }
250
251 evp_md_ctx = EVP_MD_CTX_new();
252 if (!evp_md_ctx) {
253 free_trash_chunk(trash);
254 return JWT_VRFY_OUT_OF_MEMORY;
255 }
256
257 eb = ebst_lookup(&jwt_cert_tree, ctx->key);
258
259 if (!eb) {
260 retval = JWT_VRFY_UNKNOWN_CERT;
261 goto end;
262 }
263
264 entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node);
265
266 if (!entry->pkey) {
267 retval = JWT_VRFY_UNKNOWN_CERT;
268 goto end;
269 }
270
271 if (EVP_DigestVerifyInit(evp_md_ctx, NULL, evp, NULL,entry-> pkey) == 1 &&
272 EVP_DigestVerifyUpdate(evp_md_ctx, (const unsigned char*)ctx->jose.start,
273 ctx->jose.length + ctx->claims.length + 1) == 1 &&
274 EVP_DigestVerifyFinal(evp_md_ctx, (const unsigned char*)decoded_signature->area, decoded_signature->data) == 1) {
275 retval = JWT_VRFY_OK;
276 }
277
278end:
279 EVP_MD_CTX_free(evp_md_ctx);
280 free_trash_chunk(trash);
281 return retval;
282}
283
284/*
285 * Check that the <token> that was signed via algorithm <alg> using the <key>
286 * (either an HMAC secret or the path to a public certificate) has a valid
287 * signature.
288 * Returns 1 in case of success.
289 */
290enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer *alg,
291 const struct buffer *key)
292{
293 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
294 unsigned int item_num = JWT_ELT_MAX;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200295 struct buffer *decoded_sig = NULL;
296 struct jwt_ctx ctx = {};
297 enum jwt_vrfy_status retval = JWT_VRFY_KO;
Willy Tarreau468c0002021-10-15 11:41:16 +0200298 int ret;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200299
300 ctx.alg = jwt_parse_alg(alg->area, alg->data);
301
302 if (ctx.alg == JWT_ALG_DEFAULT)
303 return JWT_VRFY_UNKNOWN_ALG;
304
305 if (jwt_tokenize(token, items, &item_num))
306 return JWT_VRFY_INVALID_TOKEN;
307
308 if (item_num != JWT_ELT_MAX)
309 if (ctx.alg != JWS_ALG_NONE || item_num != JWT_ELT_SIG)
310 return JWT_VRFY_INVALID_TOKEN;
311
312 ctx.jose = items[JWT_ELT_JOSE];
313 ctx.claims = items[JWT_ELT_CLAIMS];
314 ctx.signature = items[JWT_ELT_SIG];
315
316 /* "alg" is "none", the signature must be empty for the JWS to be valid. */
317 if (ctx.alg == JWS_ALG_NONE) {
318 return (ctx.signature.length == 0) ? JWT_VRFY_OK : JWT_VRFY_KO;
319 }
320
321 if (ctx.signature.length == 0)
322 return JWT_VRFY_INVALID_TOKEN;
323
324 decoded_sig = alloc_trash_chunk();
325 if (!decoded_sig)
326 return JWT_VRFY_OUT_OF_MEMORY;
327
Willy Tarreau468c0002021-10-15 11:41:16 +0200328 ret = base64urldec(ctx.signature.start, ctx.signature.length,
329 decoded_sig->area, decoded_sig->size);
330 if (ret == -1) {
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200331 retval = JWT_VRFY_INVALID_TOKEN;
332 goto end;
333 }
334
Willy Tarreau468c0002021-10-15 11:41:16 +0200335 decoded_sig->data = ret;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200336 ctx.key = key->area;
337 ctx.key_length = key->data;
338
339 /* We have all three sections, signature calculation can begin. */
340
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200341 switch(ctx.alg) {
342
343 case JWS_ALG_HS256:
344 case JWS_ALG_HS384:
345 case JWS_ALG_HS512:
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200346 /* HMAC + SHA-XXX */
347 retval = jwt_jwsverify_hmac(&ctx, decoded_sig);
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200348 break;
349 case JWS_ALG_RS256:
350 case JWS_ALG_RS384:
351 case JWS_ALG_RS512:
352 case JWS_ALG_ES256:
353 case JWS_ALG_ES384:
354 case JWS_ALG_ES512:
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200355 /* RSASSA-PKCS1-v1_5 + SHA-XXX */
356 /* ECDSA using P-XXX and SHA-XXX */
357 retval = jwt_jwsverify_rsa_ecdsa(&ctx, decoded_sig);
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200358 break;
359 case JWS_ALG_PS256:
360 case JWS_ALG_PS384:
361 case JWS_ALG_PS512:
362 default:
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200363 /* RSASSA-PSS using SHA-XXX and MGF1 with SHA-XXX */
364
365 /* Not managed yet */
366 retval = JWT_VRFY_UNMANAGED_ALG;
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200367 break;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200368 }
369
370end:
371 free_trash_chunk(decoded_sig);
372
373 return retval;
374}
375
Remi Tricot-Le Breton0b24d2f2021-10-18 15:14:47 +0200376static void jwt_deinit(void)
377{
378 struct ebmb_node *node = NULL;
379 struct jwt_cert_tree_entry *entry = NULL;
380
381 node = ebmb_first(&jwt_cert_tree);
382 while (node) {
383 entry = ebmb_entry(node, struct jwt_cert_tree_entry, node);
384 ha_free(&entry);
385 node = ebmb_first(&jwt_cert_tree);
386 }
387}
388REGISTER_POST_DEINIT(jwt_deinit);
389
390
Remi Tricot-Le Breton7feb3612021-10-01 15:36:54 +0200391#endif /* USE_OPENSSL */