blob: 8c45375428f552e837866e74c4fbb30b63474ffd [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) {
Remi Tricot-Le Breton72663502021-11-03 12:23:54 +010037 if (alg_len == sizeof("none")-1 && strcmp("none", alg_str) == 0)
Remi Tricot-Le Breton7feb3612021-10-01 15:36:54 +020038 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;
Tim Duesterhusc87d3c22021-10-18 18:40:28 +0200178 unsigned char signature[EVP_MAX_MD_SIZE];
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200179 unsigned int signature_length = 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200180 unsigned char *hmac_res = NULL;
181 enum jwt_vrfy_status retval = JWT_VRFY_KO;
182
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200183 switch(ctx->alg) {
184 case JWS_ALG_HS256:
185 evp = EVP_sha256();
186 break;
187 case JWS_ALG_HS384:
188 evp = EVP_sha384();
189 break;
190 case JWS_ALG_HS512:
191 evp = EVP_sha512();
192 break;
193 default: break;
194 }
195
196 hmac_res = HMAC(evp, ctx->key, ctx->key_length, (const unsigned char*)ctx->jose.start,
197 ctx->jose.length + ctx->claims.length + 1, signature, &signature_length);
198
199 if (hmac_res && signature_length == decoded_signature->data &&
Willy Tarreauce16db42021-10-15 11:52:41 +0200200 (CRYPTO_memcmp(decoded_signature->area, signature, signature_length) == 0))
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200201 retval = JWT_VRFY_OK;
202
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200203 return retval;
204}
205
206/*
207 * Check that the signature included in a JWT signed via RSA or ECDSA is valid
208 * and can be verified thanks to a given public certificate.
209 * Returns 1 in case of success.
210 */
211static enum jwt_vrfy_status
212jwt_jwsverify_rsa_ecdsa(const struct jwt_ctx *ctx, const struct buffer *decoded_signature)
213{
214 const EVP_MD *evp = NULL;
215 EVP_MD_CTX *evp_md_ctx;
216 enum jwt_vrfy_status retval = JWT_VRFY_KO;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200217 struct ebmb_node *eb;
218 struct jwt_cert_tree_entry *entry = NULL;
219
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200220 switch(ctx->alg) {
221 case JWS_ALG_RS256:
222 case JWS_ALG_ES256:
223 evp = EVP_sha256();
224 break;
225 case JWS_ALG_RS384:
226 case JWS_ALG_ES384:
227 evp = EVP_sha384();
228 break;
229 case JWS_ALG_RS512:
230 case JWS_ALG_ES512:
231 evp = EVP_sha512();
232 break;
233 default: break;
234 }
235
236 evp_md_ctx = EVP_MD_CTX_new();
Tim Duesterhuse0c1d742021-10-18 18:40:29 +0200237 if (!evp_md_ctx)
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200238 return JWT_VRFY_OUT_OF_MEMORY;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200239
240 eb = ebst_lookup(&jwt_cert_tree, ctx->key);
241
242 if (!eb) {
243 retval = JWT_VRFY_UNKNOWN_CERT;
244 goto end;
245 }
246
247 entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node);
248
249 if (!entry->pkey) {
250 retval = JWT_VRFY_UNKNOWN_CERT;
251 goto end;
252 }
253
254 if (EVP_DigestVerifyInit(evp_md_ctx, NULL, evp, NULL,entry-> pkey) == 1 &&
255 EVP_DigestVerifyUpdate(evp_md_ctx, (const unsigned char*)ctx->jose.start,
256 ctx->jose.length + ctx->claims.length + 1) == 1 &&
257 EVP_DigestVerifyFinal(evp_md_ctx, (const unsigned char*)decoded_signature->area, decoded_signature->data) == 1) {
258 retval = JWT_VRFY_OK;
259 }
260
261end:
262 EVP_MD_CTX_free(evp_md_ctx);
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200263 return retval;
264}
265
266/*
267 * Check that the <token> that was signed via algorithm <alg> using the <key>
268 * (either an HMAC secret or the path to a public certificate) has a valid
269 * signature.
270 * Returns 1 in case of success.
271 */
272enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer *alg,
273 const struct buffer *key)
274{
275 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
276 unsigned int item_num = JWT_ELT_MAX;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200277 struct buffer *decoded_sig = NULL;
278 struct jwt_ctx ctx = {};
279 enum jwt_vrfy_status retval = JWT_VRFY_KO;
Willy Tarreau468c0002021-10-15 11:41:16 +0200280 int ret;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200281
282 ctx.alg = jwt_parse_alg(alg->area, alg->data);
283
284 if (ctx.alg == JWT_ALG_DEFAULT)
285 return JWT_VRFY_UNKNOWN_ALG;
286
287 if (jwt_tokenize(token, items, &item_num))
288 return JWT_VRFY_INVALID_TOKEN;
289
290 if (item_num != JWT_ELT_MAX)
291 if (ctx.alg != JWS_ALG_NONE || item_num != JWT_ELT_SIG)
292 return JWT_VRFY_INVALID_TOKEN;
293
294 ctx.jose = items[JWT_ELT_JOSE];
295 ctx.claims = items[JWT_ELT_CLAIMS];
296 ctx.signature = items[JWT_ELT_SIG];
297
298 /* "alg" is "none", the signature must be empty for the JWS to be valid. */
299 if (ctx.alg == JWS_ALG_NONE) {
300 return (ctx.signature.length == 0) ? JWT_VRFY_OK : JWT_VRFY_KO;
301 }
302
303 if (ctx.signature.length == 0)
304 return JWT_VRFY_INVALID_TOKEN;
305
306 decoded_sig = alloc_trash_chunk();
307 if (!decoded_sig)
308 return JWT_VRFY_OUT_OF_MEMORY;
309
Willy Tarreau468c0002021-10-15 11:41:16 +0200310 ret = base64urldec(ctx.signature.start, ctx.signature.length,
311 decoded_sig->area, decoded_sig->size);
312 if (ret == -1) {
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200313 retval = JWT_VRFY_INVALID_TOKEN;
314 goto end;
315 }
316
Willy Tarreau468c0002021-10-15 11:41:16 +0200317 decoded_sig->data = ret;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200318 ctx.key = key->area;
319 ctx.key_length = key->data;
320
321 /* We have all three sections, signature calculation can begin. */
322
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200323 switch(ctx.alg) {
324
325 case JWS_ALG_HS256:
326 case JWS_ALG_HS384:
327 case JWS_ALG_HS512:
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200328 /* HMAC + SHA-XXX */
329 retval = jwt_jwsverify_hmac(&ctx, decoded_sig);
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200330 break;
331 case JWS_ALG_RS256:
332 case JWS_ALG_RS384:
333 case JWS_ALG_RS512:
334 case JWS_ALG_ES256:
335 case JWS_ALG_ES384:
336 case JWS_ALG_ES512:
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200337 /* RSASSA-PKCS1-v1_5 + SHA-XXX */
338 /* ECDSA using P-XXX and SHA-XXX */
339 retval = jwt_jwsverify_rsa_ecdsa(&ctx, decoded_sig);
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200340 break;
341 case JWS_ALG_PS256:
342 case JWS_ALG_PS384:
343 case JWS_ALG_PS512:
344 default:
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200345 /* RSASSA-PSS using SHA-XXX and MGF1 with SHA-XXX */
346
347 /* Not managed yet */
348 retval = JWT_VRFY_UNMANAGED_ALG;
Remi Tricot-Le Breton8abed172021-10-18 15:14:48 +0200349 break;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +0200350 }
351
352end:
353 free_trash_chunk(decoded_sig);
354
355 return retval;
356}
357
Remi Tricot-Le Breton0b24d2f2021-10-18 15:14:47 +0200358static void jwt_deinit(void)
359{
360 struct ebmb_node *node = NULL;
361 struct jwt_cert_tree_entry *entry = NULL;
362
363 node = ebmb_first(&jwt_cert_tree);
364 while (node) {
365 entry = ebmb_entry(node, struct jwt_cert_tree_entry, node);
366 ha_free(&entry);
367 node = ebmb_first(&jwt_cert_tree);
368 }
369}
370REGISTER_POST_DEINIT(jwt_deinit);
371
372
Remi Tricot-Le Breton7feb3612021-10-01 15:36:54 +0200373#endif /* USE_OPENSSL */