backend dflt_be
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 set-var(txn.cert) req.hdr(x-jwt-cert) if { req.hdr(x-jwt-cert) -m found }
http-request set-var(txn.jwt_verify) var(txn.bearer),jwt_verify(txn.jwt_alg,"unknown_cert.pem")
http-response set-header x-jwt-token %[var(txn.bearer)]
http-response set-header x-jwt-alg %[var(txn.jwt_alg)]
http-response set-header x-jwt-verify %[var(txn.jwt_verify)]
+ http-response set-header x-jwt-cert-verify %[var(txn.bearer),jwt_verify_cert(txn.jwt_alg,txn.cert)] if { var(txn.cert) -m found }
server s1 ${s1_addr}:${s1_port}
expect resp.status == 200
expect resp.http.x-int == "1"
} -run
+
+# The JWT entry of a certificate created through "new ssl cert" must survive
+# the "commit ssl cert" call even when the certificate does not use JWT. It is
+# kept as a keyless entry so that jwt_verify_cert() reports
+# JWT_VRFY_UNAVAIL_CERT (-7) instead of JWT_VRFY_UNKNOWN_CERT (-5).
+shell {
+ echo "new ssl cert ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+ printf "set ssl cert ${tmpdir}/keyless.pem <<\n$(cat ${testdir}/cert.rsa.pem)\n\n" | socat "${tmpdir}/h1/stats" -
+ echo "commit ssl cert ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+}
+
+client c31 -connect ${h1_mainfe_sock} {
+ txreq -url "/errors" \
+ -hdr "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.AA" \
+ -hdr "x-jwt-cert: ${tmpdir}/keyless.pem"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-jwt-cert-verify == "-7"
+} -run
+
+# Repeated "add/del ssl jwt" on a certificate must not leak references on its
+# JWT entry. Once the certificate is deleted, the entry must be removed and
+# jwt_verify_cert() must return JWT_VRFY_UNKNOWN_CERT (-5).
+shell {
+ echo "add ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+ echo "del ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+ echo "add ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+ echo "del ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+
+ echo "del ssl cert ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" -
+}
+
+client c32 -connect ${h1_mainfe_sock} {
+ txreq -url "/errors" \
+ -hdr "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.AA" \
+ -hdr "x-jwt-cert: ${tmpdir}/keyless.pem"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-jwt-cert-verify == "-5"
+} -run
return (ptr != jwt_end);
}
+#define UPDATE_PKEY(dst, pkey) ({ \
+ EVP_PKEY_free(dst); \
+ dst = pkey; \
+ if (pkey) \
+ EVP_PKEY_up_ref(pkey); \
+})
+
/*
* Allocate a new entry for <path> and insert it into the JWT certificate tree.
* Both <pubkey> and <privkey> are optional and may be NULL, which is used to
* mark a certificate as known to the JWT tree while making it unusable by the
* jwt_verify/jwt_decrypt converters. The keys are not stolen: a reference is
* taken on each of them, so that the caller keeps the ownership of its own
- * reference. If an entry already exists for this path (for instance because it
- * was kept around by jwt_tree_clear_entry() after a "del ssl jwt" command),
- * its keys are replaced in place instead of creating a duplicate entry, which
- * also makes it possible to re-enable the "jwt" option on a certificate.
+ * reference. If an entry already exists for this path, its keys are replaced in
+ * place instead of creating a duplicate entry.
* Returns 0 on success, -1 on allocation failure (and fills <err> if provided).
*/
static int jwt_tree_create_entry(char *path, int pathlen, EVP_PKEY *pubkey, EVP_PKEY *privkey, char **err)
entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node);
}
- EVP_PKEY_free(entry->pubkey);
- entry->pubkey = NULL;
- if (pubkey) {
- entry->pubkey = pubkey;
- EVP_PKEY_up_ref(pubkey);
- }
- EVP_PKEY_free(entry->privkey);
- entry->privkey = NULL;
- if (privkey) {
- entry->privkey = privkey;
- EVP_PKEY_up_ref(privkey);
- }
+ ++entry->refcount;
+
+ UPDATE_PKEY(entry->pubkey, pubkey);
+ UPDATE_PKEY(entry->privkey, privkey);
+
HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock);
return 0;
}
/*
- * Release the keys held by the JWT tree entry matching <path> but keep the
- * entry itself in the tree. This is used when the "jwt" option is disabled on
- * a certificate ("del ssl jwt"): keeping an empty entry makes it possible for
- * jwt_verify to return JWT_VRFY_UNAVAIL_CERT instead of
- * JWT_VRFY_UNKNOWN_CERT. The keys can later be restored by
- * jwt_tree_create_entry().
- * Returns 0 (the entry may legitimately not exist).
+ * Update the JWT tree entry matching <store>'s path and take a new owner
+ * reference on it if <take_ref> is set, so that it can survive the release of
+ * the previous store. The entry is expected to already exist.
+ * This function is only meant to be called either with the "add/del ssl jwt"
+ * cli command or during the ckch_store_replace process where we update the
+ * corresponding JWT tree entry before deleting the previous ckch_store and
+ * inserting the new one in the ckch_store tree.
*/
-int jwt_tree_clear_entry(char *path)
+void jwt_tree_update_entry_from_store(struct ckch_store *store, int take_ref)
{
+ EVP_PKEY *pubkey = NULL;
+ EVP_PKEY *privkey = NULL;
struct jwt_cert_tree_entry *entry = NULL;
struct ebmb_node *eb = NULL;
- HA_RWLOCK_WRLOCK(JWT_LOCK, &jwt_tree_lock);
- eb = ebst_lookup(&jwt_cert_tree, path);
- if (eb != NULL) {
- entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node);
- EVP_PKEY_free(entry->pubkey);
- EVP_PKEY_free(entry->privkey);
- entry->pubkey = NULL;
- entry->privkey = NULL;
+ if (!store)
+ return;
+
+ if (store->conf.jwt) {
+ /* X509_get_pubkey() returns an owned reference which is released
+ * below. Note that the entry creation takes its own reference on
+ * the key. */
+ pubkey = X509_get_pubkey(store->data->cert);
+ privkey = store->data->key;
}
- HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock);
- return 0;
+ HA_RWLOCK_WRLOCK(JWT_LOCK, &jwt_tree_lock);
+ eb = ebst_lookup(&jwt_cert_tree, store->path);
+ if (unlikely(!eb))
+ /* Should never happen */
+ goto end;
+
+ entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node);
+
+ if (take_ref)
+ ++entry->refcount;
+
+ UPDATE_PKEY(entry->pubkey, pubkey);
+ UPDATE_PKEY(entry->privkey, privkey);
+
+end:
+ EVP_PKEY_free(pubkey);
+ HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock);
}
/*
eb = ebst_lookup(&jwt_cert_tree, path);
if (eb != NULL) {
entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node);
- EVP_PKEY_free(entry->pubkey);
- EVP_PKEY_free(entry->privkey);
- ebmb_delete(eb);
- free(entry);
+
+ --entry->refcount;
+
+ if (entry->refcount <= 0) {
+ EVP_PKEY_free(entry->pubkey);
+ EVP_PKEY_free(entry->privkey);
+ ebmb_delete(eb);
+ free(entry);
+ }
}
HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock);
{
struct ckch_store *ckchs;
char *err = NULL;
- int oldvalue;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return cli_err(appctx, "Can't allocate memory!\n");
if ((ckchs = ckchs_lookup(args[3])) == NULL)
goto error;
- oldvalue = ckchs->conf.jwt;
ckchs->conf.jwt = value;
- if (value) {
- if (jwt_tree_create_entry_from_store(ckchs, &err)) {
- ckchs->conf.jwt = oldvalue;
- HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
- return cli_dynerr(appctx, memprintf(&err, "%sAdding certificate '%s' to JWT tree failed", err ? err : "\n", args[3]));
- }
- } else {
- jwt_tree_clear_entry(args[3]);
- }
+ jwt_tree_update_entry_from_store(ckchs, 0);
HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
return 0;
__ckch_inst_free_locked(ckchi);
}
- ckch_store_free(old_ckchs);
+ /*
+ * We update the JWT entry before calling ckch_store_free so that we
+ * don't end up with a short window during which the entry does not
+ * exist.
+ */
+ jwt_tree_update_entry_from_store(new_ckchs, 1);
- /* The old entry should have been removed in chck_store_free, create a
- * new entry altogether. */
- jwt_tree_create_entry_from_store(new_ckchs, NULL);
+ ckch_store_free(old_ckchs);
ebst_insert(&ckchs_tree, &new_ckchs->node);
}