used, or no chain was provided in the PEM.
jwt.allow_none_alg
- Allow JWT signed token with the "none" algorithm in the 'jwt_verify'
- converter. They will always be considered valid when this option is set. By
- default unsigned tokens are rejected since they are considered unsafe (see
- RFC8725 section 3.1).
+ Allow JWT signed token with the "none" algorithm in the 'jwt_verify',
+ 'jwt_verify_secret' or 'jwt_verify_cert' converters. They will always be
+ considered valid when this option is set. By default unsigned tokens are
+ rejected since they are considered unsafe (see RFC8725 section 3.1).
jwt.decrypt_alg_list <list>
Set the list of algorithms allowed in the jwt_decrypt_XXX converters. JWT
-- keyword -------------------------------------+- input type + output type -
jwt_verify(alg,key) string integer
jwt_verify_cert(alg,cert) string integer
+jwt_verify_secret(alg,cert) string integer
language(value[,default]) string string
length string integer
lower string string
ensure that the header and payload contents are fully valid JSONs once
decoded for instance, and no checks are performed regarding their respective
contents.
+ This converter cannot be used to validate tokens signed with HMAC algorithm,
+ it takes a certificate or a public key as second parameter.
- <alg> can be either a string or a variable name (See also "set-var") that
holds the name of the algorithm used to verify. Unlike the "jwt_verify"
- converter, this converter only expects a certificate as second parameter so
- it should not be used for tokens using HMAC algorithms.
+ converter, this converter only expects a certificate or a public key as
+ second parameter so it should not be used for tokens using HMAC algorithms.
Algorithms mentioned in section 3.1 of RFC7518 are managed (apart from HMAC
ones):
+--------------+---------------------------------------------------------+
- <key> can be either a string or a variable name (See also "set-var") that
- holds a certificate path.
+ holds a certificate or public key path.
Certificates must be standard PEM certificates (starting with BEGIN
CERTIFICATE). Their path can be passed directly to the converter or
using the stats socket. See also "set ssl cert" and "new ssl cert" in the
management guide.
+ All the public keys that might be used to verify JWTs must be known during
+ init in order to be added into a dedicated cache so that no disk access is
+ required during runtime.
+
Returns 1 in case of verification success, 0 in case of verification failure
and a strictly negative value for any other error. Because of all those
non-null error return values, the result of this converter should never be
http-request deny unless { var(txn.jwt_alg) -m str "RS256" }
http-request deny unless { var(txn.bearer),jwt_verify_cert(txn.jwt_alg,"/path/to/cert.pem") 1 }
+jwt_verify_secret(<alg>,<secret>)
+
+ Performs a signature verification for the JSON Web Token (JWT) given in input
+ by using the <alg> algorithm and the <cert> parameter.
+ For now, only JWS tokens using the Compact Serialization format can be
+ processed (three dot-separated base64-url encoded strings).
+ This converter only verifies the signature of the token and does not perform
+ a full JWT validation as specified in section 7.2 of RFC7519. We do not
+ ensure that the header and payload contents are fully valid JSONs once
+ decoded for instance, and no checks are performed regarding their respective
+ contents.
+ This converter can only be used to validate tokens signed with HMAC
+ algorithm.
+
+ - <alg> can be either a string or a variable name (See also "set-var") that
+ holds the name of the algorithm used to verify.
+
+ Among the algorithms mentioned in section 3.1 of RFC7518, only the HMAC
+ ones are supported in this converter and the "none" special case.
+
+ +--------------+---------------------------------------------------------+
+ | "alg" Param | Digital Signature or MAC Algorithm |
+ | Value | |
+ +--------------+---------------------------------------------------------+
+ | HS256 | HMAC using SHA-256 |
+ | HS384 | HMAC using SHA-384 |
+ | HS512 | HMAC using SHA-512 |
+ | none | No digital signature or MAC performed (see note below) |
+ +--------------+---------------------------------------------------------+
+
+ - <secret> can be either a string or a variable name (See also "set-var")
+ that holds the secret used to verify the token.
+
+ Returns 1 in case of verification success, 0 in case of verification failure
+ and a strictly negative value for any other error. Because of all those
+ non-null error return values, the result of this converter should never be
+ converted to a boolean. See below for a full list of the possible return
+ values.
+
+ The possible return values are the following :
+
+ +----+----------------------------------------------------------------------+
+ | ID | message |
+ +----+----------------------------------------------------------------------+
+ | 1 | "Verification success" |
+ | 0 | "Verification failure" |
+ | -1 | "Unknown algorithm (not mentioned in RFC7518)" |
+ | -2 | "Unmanaged algorithm" |
+ | -3 | "Invalid token" |
+ | -4 | "Out of memory" |
+ | -6 | "Internal error" |
+ +----+----------------------------------------------------------------------+
+
+ Please note that this converter is only available when HAProxy has been
+ compiled with USE_OPENSSL.
+
+ Example:
+ # Get a JWT from the authorization header, extract the "alg" field of its
+ # JOSE header and use a public certificate to verify a signature
+ http-request set-var(txn.bearer) http_auth_bearer
+ http-request set-var(txn.jwt_alg) var(txn.bearer),jwt_header_query('$.alg')
+ http-request deny unless { var(txn.jwt_alg) -m str "HS256" }
+ http-request set-var(txn.secret) str(YQBiY2RlZg==),b64dec
+ http-request return status 200 hdr x-res "%[req.hdr(x-tok),jwt_verify_secret(txn.jwt_alg,txn.secret)]"
+
language(<value>[,<default>])
Returns the value with the highest q-factor from a list as extracted from the
"accept-language" header using "req.fhdr". Values with no q-factor have a
JWS_ALG_PS512,
};
+typedef enum {
+ JWT_VRFY_MODE_DFLT = 0,
+ JWT_VRFY_MODE_SECRET,
+ JWT_VRFY_MODE_CERT
+} jwt_verify_mode;
+
struct jwt_item {
char *start;
size_t length;
struct jwt_item signature;
char *key;
unsigned int key_length;
- int is_x509; /* 1 if 'key' field is a certificate, 0 otherwise */
+ jwt_verify_mode mode; /* default, secret-only, cert-only */
};
enum jwt_elt {
#ifdef USE_OPENSSL
enum jwt_alg jwt_parse_alg(const char *alg_str, unsigned int alg_len);
int jwt_tokenize(const struct buffer *jwt, struct jwt_item *items, unsigned int item_num);
-int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *file, int line, char **err);
+int jwt_tree_load_cert(char *path, int pathlen, const char *file, int line, char **err);
enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer *alg,
- const struct buffer *key, int is_x509);
+ const struct buffer *key, jwt_verify_mode mode);
int jwt_tree_create_entry_from_store(struct ckch_store *store, char **err);
int jwt_tree_delete_entry(char *path);
http-response set-header x-jwt-verify-HS256 %[var(txn.bearer),jwt_verify(txn.jwt_alg,"hmac key hs256")] if { var(txn.jwt_alg) -m str "HS256" }
http-response set-header x-jwt-verify-HS384 %[var(txn.bearer),jwt_verify(txn.jwt_alg,"hmac key hs384")] if { var(txn.jwt_alg) -m str "HS384" }
http-response set-header x-jwt-verify-HS512 %[var(txn.bearer),jwt_verify(txn.jwt_alg,"hmac key hs512")] if { var(txn.jwt_alg) -m str "HS512" }
+
+ http-response set-header x-jwt-verify-secret-HS256 %[var(txn.bearer),jwt_verify_secret(txn.jwt_alg,"hmac key hs256")] if { var(txn.jwt_alg) -m str "HS256" }
+ http-response set-header x-jwt-verify-secret-HS384 %[var(txn.bearer),jwt_verify_secret(txn.jwt_alg,"hmac key hs384")] if { var(txn.jwt_alg) -m str "HS384" }
+ http-response set-header x-jwt-verify-secret-HS512 %[var(txn.bearer),jwt_verify_secret(txn.jwt_alg,"hmac key hs512")] if { var(txn.jwt_alg) -m str "HS512" }
server s1 ${s1_addr}:${s1_port}
backend rsXXX_be
expect resp.status == 200
expect resp.http.x-jwt-alg == "HS256"
expect resp.http.x-jwt-verify-HS256 == "1"
+ expect resp.http.x-jwt-verify-secret-HS256 == "1"
# the two parts of that token are {"alg":"HS256","typ":"JWT"} and
# {"sub":"1234567890","name":"John Doe","iat":1516239022}
expect resp.http.x-jwt-header ~ "\"alg\":\"HS256\""
expect resp.status == 200
expect resp.http.x-jwt-alg == "HS384"
expect resp.http.x-jwt-verify-HS384 == "1"
+ expect resp.http.x-jwt-verify-secret-HS384 == "1"
} -run
client c3 -connect ${h1_mainfe_sock} {
expect resp.status == 200
expect resp.http.x-jwt-alg == "HS512"
expect resp.http.x-jwt-verify-HS512 == "1"
+ expect resp.http.x-jwt-verify-secret-HS512 == "1"
} -run
# The following token is invalid (it has three extra characters at the end of the signature)
expect resp.status == 200
expect resp.http.x-jwt-alg == "HS512"
expect resp.http.x-jwt-verify-HS512 == "-3"
+ expect resp.http.x-jwt-verify-secret-HS512 == "-3"
} -run
* Parse a public certificate and insert it into the jwt_cert_tree.
* Returns 0 in case of success.
*/
-int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *file, int line, char **err)
+int jwt_tree_load_cert(char *path, int pathlen, const char *file, int line, char **err)
{
int retval = -1;
EVP_PKEY *pubkey = NULL;
}
}
- if (!tryload_cert)
- goto end;
-
/* Look for an actual certificate or crt-store with the given name.
* If the path corresponds to an actual certificate that was not loaded
* yet we will create the corresponding ckch_store. */
unsigned char *hmac_res = NULL;
enum jwt_vrfy_status retval = JWT_VRFY_KO;
- if (ctx->is_x509) {
- return JWT_VRFY_UNMANAGED_ALG;
- }
-
switch(ctx->alg) {
case JWS_ALG_HS256:
evp = EVP_sha256();
* Returns 1 in case of success.
*/
enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer *alg,
- const struct buffer *key, int is_x509_path)
+ const struct buffer *key, jwt_verify_mode mode)
{
struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
unsigned int item_num = JWT_ELT_MAX;
decoded_sig->data = ret;
ctx.key = key->area;
ctx.key_length = key->data;
- ctx.is_x509 = is_x509_path;
+ ctx.mode = mode;
/* We have all three sections, signature calculation can begin. */
case JWS_ALG_HS384:
case JWS_ALG_HS512:
/* HMAC + SHA-XXX */
+ if (mode == JWT_VRFY_MODE_CERT) {
+ retval = JWT_VRFY_UNMANAGED_ALG;
+ goto end;
+ }
retval = jwt_jwsverify_hmac(&ctx, decoded_sig);
break;
case JWS_ALG_RS256:
case JWS_ALG_PS256:
case JWS_ALG_PS384:
case JWS_ALG_PS512:
+
+ if (mode == JWT_VRFY_MODE_SECRET) {
+ retval = JWT_VRFY_UNMANAGED_ALG;
+ goto end;
+ }
+
/* RSASSA-PKCS1-v1_5 + SHA-XXX */
/* ECDSA using P-XXX and SHA-XXX */
/* RSASSA-PSS using SHA-XXX and MGF1 with SHA-XXX */
{
enum jwt_alg alg = JWT_ALG_DEFAULT;
int retval = 0;
+ int expect_cert = 0;
+ int expect_secret = 0;
vars_check_arg(&args[0], NULL);
vars_check_arg(&args[1], NULL);
+ if (strcmp(conv->kw, "jwt_verify_cert") == 0)
+ expect_cert = 1;
+ else if (strcmp(conv->kw, "jwt_verify_secret") == 0)
+ expect_secret = 1;
+
if (args[0].type == ARGT_STR) {
alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
if (args[1].type == ARGT_STR) {
switch (alg) {
+ case JWS_ALG_NONE:
+ /* The "none" algorithm does not use any key, the
+ * second parameter is simply ignored. Whether the
+ * algorithm is allowed or not is checked at runtime
+ * through the global "jwt.allow_none_alg" option.
+ */
+ retval = 1;
+ break;
case JWS_ALG_HS256:
case JWS_ALG_HS384:
case JWS_ALG_HS512:
/* don't try to load a file with HMAC algorithms */
+ if (expect_cert) {
+ memprintf(err, "Can't use HMAC-based tokens with %s converter", conv->kw);
+ return 0;
+ }
retval = 1;
break;
default:
+ if (expect_secret) {
+ if (args[0].type != ARGT_VAR) {
+ memprintf(err, "Can't use non-HMAC-based tokens with %s converter", conv->kw);
+ return 0;
+ }
+ /* The algorithm is stored in a variable, it
+ * might be "none" or an HMAC one at runtime and
+ * the second parameter is a secret, so there is
+ * nothing to load.
+ */
+ retval = 1;
+ break;
+ }
retval = (jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data,
- 0, file, line, err) == 0);
+ file, line, err) == 0);
/* The second arg might be an HMAC secret but
* the 'alg' is stored in a var */
if (!retval && args[0].type == ARGT_VAR)
return retval;
}
-static int sample_conv_jwt_verify_cert_check(struct arg *args, struct sample_conv *conv,
- const char *file, int line, char **err)
-{
- enum jwt_alg alg = JWT_ALG_DEFAULT;
- int retval = 0;
-
- vars_check_arg(&args[0], NULL);
- vars_check_arg(&args[1], NULL);
-
- if (args[0].type == ARGT_STR) {
- alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
-
- if (alg == JWT_ALG_DEFAULT) {
- memprintf(err, "unknown JWT algorithm: %s", args[0].data.str.area);
- return 0;
- }
- }
-
- if (args[1].type == ARGT_STR) {
- switch (alg) {
- case JWS_ALG_HS256:
- case JWS_ALG_HS384:
- case JWS_ALG_HS512:
- /* We can't have a certificate as second parameter for
- * HMAC-signed JWT tokens */
- memprintf(err, "HMAC-signed tokens can't be processed by this converter");
- break;
- default:
- retval = (jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data,
- 1, file, line, err) == 0);
- break;
- }
- } else if (args[1].type == ARGT_VAR) {
- /* We will try to resolve the var during runtime because the
- * processing might work if it actually points to an already
- * existing ckch_store.
- */
- retval = 1;
- }
-
- return retval;
-}
-
/* Check that a JWT's signature is correct */
-static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private)
+static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, jwt_verify_mode mode)
{
struct sample alg_smp, key_smp;
enum jwt_vrfy_status ret;
goto end;
key->area[key->data] = 0;
- ret = jwt_verify(input, alg, key, 0);
+ ret = jwt_verify(input, alg, key, mode);
smp->data.type = SMP_T_SINT;
smp->data.u.sint = ret;
return retval;
}
-static int sample_conv_jwt_verify_cert(const struct arg *args, struct sample *smp, void *private)
+static int sample_conv_jwt_verify_legacy(const struct arg *args, struct sample *smp, void *private)
{
- struct sample alg_smp, key_smp;
- enum jwt_vrfy_status ret;
- struct buffer *input = NULL;
- struct buffer *alg = NULL;
- struct buffer *key = NULL;
- int retval = 0;
-
- /* The two following calls to 'sample_conv_var2smp_str' will both make
- * use of the preallocated trash buffer (via get_trash_chunk call in
- * smp_dup) which would end up erasing the contents of the 'smp' input
- * buffer.
- */
- input = alloc_trash_chunk_sz(smp->data.u.str.data);
- if (!input)
- return 0;
- alg = alloc_trash_chunk();
- if (!alg)
- goto end;
- key = alloc_trash_chunk();
- if (!key)
- goto end;
-
- if (!chunk_cpy(input, &smp->data.u.str))
- goto end;
-
- smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt);
- if (!sample_conv_var2smp_str(&args[0], &alg_smp))
- goto end;
- if (chunk_printf(alg, "%.*s", (int)b_data(&alg_smp.data.u.str), b_orig(&alg_smp.data.u.str)) <= 0)
- goto end;
-
- smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt);
- if (!sample_conv_var2smp_str(&args[1], &key_smp))
- goto end;
- if (chunk_printf(key, "%.*s", (int)b_data(&key_smp.data.u.str), b_orig(&key_smp.data.u.str)) <= 0)
- goto end;
-
- ret = jwt_verify(input, alg, key, 1);
-
- smp->data.type = SMP_T_SINT;
- smp->data.u.sint = ret;
+ return sample_conv_jwt_verify(args, smp, JWT_VRFY_MODE_DFLT);
+}
- retval = 1;
+static int sample_conv_jwt_verify_secret(const struct arg *args, struct sample *smp, void *private)
+{
+ return sample_conv_jwt_verify(args, smp, JWT_VRFY_MODE_SECRET);
+}
-end:
- free_trash_chunk(input);
- free_trash_chunk(alg);
- free_trash_chunk(key);
- return retval;
+static int sample_conv_jwt_verify_cert(const struct arg *args, struct sample *smp, void *private)
+{
+ return sample_conv_jwt_verify(args, smp, JWT_VRFY_MODE_CERT);
}
/* JSON Web Token converters */
{ "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
{ "jwt_payload_query", sample_conv_jwt_payload_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
- { "jwt_verify", sample_conv_jwt_verify, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT },
- { "jwt_verify_cert", sample_conv_jwt_verify_cert, ARG2(2,STR,STR), sample_conv_jwt_verify_cert_check, SMP_T_BIN, SMP_T_SINT },
+ { "jwt_verify", sample_conv_jwt_verify_legacy, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT },
+ { "jwt_verify_secret", sample_conv_jwt_verify_secret, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT },
+ { "jwt_verify_cert", sample_conv_jwt_verify_cert, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT },
#endif
{ "when", sample_conv_when, ARG3(1,STR,STR,STR), check_when_cond, SMP_T_ANY, SMP_T_ANY },
{ NULL, NULL, 0, 0, 0 },