Remi Tricot-Le Breton | 7feb361 | 2021-10-01 15:36:54 +0200 | [diff] [blame] | 1 | /* |
| 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 Breton | 864089e | 2021-10-01 15:36:56 +0200 | [diff] [blame] | 24 | /* Tree into which the public certificates used to validate JWTs will be stored. */ |
| 25 | static struct eb_root jwt_cert_tree = EB_ROOT_UNIQUE; |
| 26 | |
Remi Tricot-Le Breton | 7feb361 | 2021-10-01 15:36:54 +0200 | [diff] [blame] | 27 | /* |
| 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 | */ |
| 31 | enum 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 Breton | 7266350 | 2021-11-03 12:23:54 +0100 | [diff] [blame] | 37 | if (alg_len == sizeof("none")-1 && strcmp("none", alg_str) == 0) |
Remi Tricot-Le Breton | 7feb361 | 2021-10-01 15:36:54 +0200 | [diff] [blame] | 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 Breton | e0d3c00 | 2021-10-01 15:36:55 +0200 | [diff] [blame] | 83 | |
Remi Tricot-Le Breton | e0d3c00 | 2021-10-01 15:36:55 +0200 | [diff] [blame] | 84 | /* |
| 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 | */ |
| 92 | int 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 Breton | 864089e | 2021-10-01 15:36:56 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Parse a public certificate and insert it into the jwt_cert_tree. |
| 127 | * Returns 0 in case of success. |
| 128 | */ |
| 129 | int 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 | |
| 164 | end: |
| 165 | BIO_free(bio); |
| 166 | return retval; |
| 167 | } |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 168 | |
| 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 | */ |
| 174 | static enum jwt_vrfy_status |
| 175 | jwt_jwsverify_hmac(const struct jwt_ctx *ctx, const struct buffer *decoded_signature) |
| 176 | { |
| 177 | const EVP_MD *evp = NULL; |
Tim Duesterhus | c87d3c2 | 2021-10-18 18:40:28 +0200 | [diff] [blame] | 178 | unsigned char signature[EVP_MAX_MD_SIZE]; |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 179 | unsigned int signature_length = 0; |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 180 | unsigned char *hmac_res = NULL; |
| 181 | enum jwt_vrfy_status retval = JWT_VRFY_KO; |
| 182 | |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 183 | 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 Tarreau | ce16db4 | 2021-10-15 11:52:41 +0200 | [diff] [blame] | 200 | (CRYPTO_memcmp(decoded_signature->area, signature, signature_length) == 0)) |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 201 | retval = JWT_VRFY_OK; |
| 202 | |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 203 | 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 | */ |
| 211 | static enum jwt_vrfy_status |
| 212 | jwt_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 Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 217 | struct ebmb_node *eb; |
| 218 | struct jwt_cert_tree_entry *entry = NULL; |
| 219 | |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 220 | 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 Duesterhus | e0c1d74 | 2021-10-18 18:40:29 +0200 | [diff] [blame] | 237 | if (!evp_md_ctx) |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 238 | return JWT_VRFY_OUT_OF_MEMORY; |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 239 | |
| 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 | |
| 261 | end: |
| 262 | EVP_MD_CTX_free(evp_md_ctx); |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 263 | 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 | */ |
| 272 | enum 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 Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 277 | struct buffer *decoded_sig = NULL; |
| 278 | struct jwt_ctx ctx = {}; |
| 279 | enum jwt_vrfy_status retval = JWT_VRFY_KO; |
Willy Tarreau | 468c000 | 2021-10-15 11:41:16 +0200 | [diff] [blame] | 280 | int ret; |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 281 | |
| 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 Tarreau | 468c000 | 2021-10-15 11:41:16 +0200 | [diff] [blame] | 310 | ret = base64urldec(ctx.signature.start, ctx.signature.length, |
| 311 | decoded_sig->area, decoded_sig->size); |
| 312 | if (ret == -1) { |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 313 | retval = JWT_VRFY_INVALID_TOKEN; |
| 314 | goto end; |
| 315 | } |
| 316 | |
Willy Tarreau | 468c000 | 2021-10-15 11:41:16 +0200 | [diff] [blame] | 317 | decoded_sig->data = ret; |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 318 | 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 Breton | 8abed17 | 2021-10-18 15:14:48 +0200 | [diff] [blame] | 323 | switch(ctx.alg) { |
| 324 | |
| 325 | case JWS_ALG_HS256: |
| 326 | case JWS_ALG_HS384: |
| 327 | case JWS_ALG_HS512: |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 328 | /* HMAC + SHA-XXX */ |
| 329 | retval = jwt_jwsverify_hmac(&ctx, decoded_sig); |
Remi Tricot-Le Breton | 8abed17 | 2021-10-18 15:14:48 +0200 | [diff] [blame] | 330 | 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 Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 337 | /* 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 Breton | 8abed17 | 2021-10-18 15:14:48 +0200 | [diff] [blame] | 340 | break; |
| 341 | case JWS_ALG_PS256: |
| 342 | case JWS_ALG_PS384: |
| 343 | case JWS_ALG_PS512: |
| 344 | default: |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 345 | /* RSASSA-PSS using SHA-XXX and MGF1 with SHA-XXX */ |
| 346 | |
| 347 | /* Not managed yet */ |
| 348 | retval = JWT_VRFY_UNMANAGED_ALG; |
Remi Tricot-Le Breton | 8abed17 | 2021-10-18 15:14:48 +0200 | [diff] [blame] | 349 | break; |
Remi Tricot-Le Breton | 130e142 | 2021-10-01 15:36:58 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | end: |
| 353 | free_trash_chunk(decoded_sig); |
| 354 | |
| 355 | return retval; |
| 356 | } |
| 357 | |
Remi Tricot-Le Breton | 0b24d2f | 2021-10-18 15:14:47 +0200 | [diff] [blame] | 358 | static 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 | } |
| 370 | REGISTER_POST_DEINIT(jwt_deinit); |
| 371 | |
| 372 | |
Remi Tricot-Le Breton | 7feb361 | 2021-10-01 15:36:54 +0200 | [diff] [blame] | 373 | #endif /* USE_OPENSSL */ |