Commit 47410d3a authored by David Howells's avatar David Howells

X.509: Extract signature digest and make self-signed cert checks earlier

Extract the signature digest for an X.509 certificate earlier, at the end
of x509_cert_parse() rather than leaving it to the callers thereof.

Further, immediately after that, check the signature on self-signed
certificates, also rather in the callers of x509_cert_parse().

This we need to determine whether or not the X.509 cert requires crypto
that we don't support before we do the above two steps.

We note in the x509_certificate struct the following bits of information:

 (1) Whether the signature is self-signed (even if we can't check the
     signature due to missing crypto).

 (2) Whether the key held in the certificate needs unsupported crypto to be
     used.  We may get a PKCS#7 message with X.509 certs that we can't make
     use of - we just ignore them and give ENOPKG at the end it we couldn't
     verify anything if at least one of these unusable certs are in the
     chain of trust.

 (3) Whether the signature held in the certificate needs unsupported crypto
     to be checked.  We can still use the key held in this certificate,
     even if we can't check the signature on it - if it is held in the
     system trusted keyring, for instance.  We just can't add it to a ring
     of trusted keys or follow it further up the chain of trust.

Making these checks earlier allows x509_check_signature() to be removed and
replaced with direct calls to public_key_verify_signature().
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
parent dfd0cabf
...@@ -190,9 +190,8 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, ...@@ -190,9 +190,8 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
x509->subject, x509->subject,
x509->raw_serial_size, x509->raw_serial); x509->raw_serial_size, x509->raw_serial);
x509->seen = true; x509->seen = true;
ret = x509_get_sig_params(x509); if (x509->unsupported_key)
if (ret < 0) goto unsupported_crypto_in_x509;
goto maybe_missing_crypto_in_x509;
pr_debug("- issuer %s\n", x509->issuer); pr_debug("- issuer %s\n", x509->issuer);
sig = x509->sig; sig = x509->sig;
...@@ -203,22 +202,14 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, ...@@ -203,22 +202,14 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
pr_debug("- authkeyid.skid %*phN\n", pr_debug("- authkeyid.skid %*phN\n",
sig->auth_ids[1]->len, sig->auth_ids[1]->data); sig->auth_ids[1]->len, sig->auth_ids[1]->data);
if ((!x509->sig->auth_ids[0] && !x509->sig->auth_ids[1]) || if (x509->self_signed) {
strcmp(x509->subject, x509->issuer) == 0) {
/* If there's no authority certificate specified, then /* If there's no authority certificate specified, then
* the certificate must be self-signed and is the root * the certificate must be self-signed and is the root
* of the chain. Likewise if the cert is its own * of the chain. Likewise if the cert is its own
* authority. * authority.
*/ */
pr_debug("- no auth?\n"); if (x509->unsupported_sig)
if (x509->raw_subject_size != x509->raw_issuer_size || goto unsupported_crypto_in_x509;
memcmp(x509->raw_subject, x509->raw_issuer,
x509->raw_issuer_size) != 0)
return 0;
ret = x509_check_signature(x509->pub, x509);
if (ret < 0)
goto maybe_missing_crypto_in_x509;
x509->signer = x509; x509->signer = x509;
pr_debug("- self-signed\n"); pr_debug("- self-signed\n");
return 0; return 0;
...@@ -270,7 +261,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, ...@@ -270,7 +261,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
sinfo->index); sinfo->index);
return 0; return 0;
} }
ret = x509_check_signature(p->pub, x509); ret = public_key_verify_signature(p->pub, p->sig);
if (ret < 0) if (ret < 0)
return ret; return ret;
x509->signer = p; x509->signer = p;
...@@ -282,16 +273,14 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, ...@@ -282,16 +273,14 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
might_sleep(); might_sleep();
} }
maybe_missing_crypto_in_x509: unsupported_crypto_in_x509:
/* Just prune the certificate chain at this point if we lack some /* Just prune the certificate chain at this point if we lack some
* crypto module to go further. Note, however, we don't want to set * crypto module to go further. Note, however, we don't want to set
* sinfo->missing_crypto as the signed info block may still be * sinfo->unsupported_crypto as the signed info block may still be
* validatable against an X.509 cert lower in the chain that we have a * validatable against an X.509 cert lower in the chain that we have a
* trusted copy of. * trusted copy of.
*/ */
if (ret == -ENOPKG) return 0;
return 0;
return ret;
} }
/* /*
...@@ -378,9 +367,8 @@ int pkcs7_verify(struct pkcs7_message *pkcs7, ...@@ -378,9 +367,8 @@ int pkcs7_verify(struct pkcs7_message *pkcs7,
enum key_being_used_for usage) enum key_being_used_for usage)
{ {
struct pkcs7_signed_info *sinfo; struct pkcs7_signed_info *sinfo;
struct x509_certificate *x509;
int enopkg = -ENOPKG; int enopkg = -ENOPKG;
int ret, n; int ret;
kenter(""); kenter("");
...@@ -422,12 +410,6 @@ int pkcs7_verify(struct pkcs7_message *pkcs7, ...@@ -422,12 +410,6 @@ int pkcs7_verify(struct pkcs7_message *pkcs7,
return -EINVAL; return -EINVAL;
} }
for (n = 0, x509 = pkcs7->certs; x509; x509 = x509->next, n++) {
ret = x509_get_sig_params(x509);
if (ret < 0)
return ret;
}
for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) { for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
ret = pkcs7_verify_one(pkcs7, sinfo); ret = pkcs7_verify_one(pkcs7, sinfo);
if (ret < 0) { if (ret < 0) {
......
...@@ -108,6 +108,11 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) ...@@ -108,6 +108,11 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
if (ret < 0) if (ret < 0)
goto error_decode; goto error_decode;
/* Grab the signature bits */
ret = x509_get_sig_params(cert);
if (ret < 0)
goto error_decode;
/* Generate cert issuer + serial number key ID */ /* Generate cert issuer + serial number key ID */
kid = asymmetric_key_generate_id(cert->raw_serial, kid = asymmetric_key_generate_id(cert->raw_serial,
cert->raw_serial_size, cert->raw_serial_size,
...@@ -119,6 +124,11 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) ...@@ -119,6 +124,11 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
} }
cert->id = kid; cert->id = kid;
/* Detect self-signed certificates */
ret = x509_check_for_self_signed(cert);
if (ret < 0)
goto error_decode;
kfree(ctx); kfree(ctx);
return cert; return cert;
......
...@@ -39,7 +39,9 @@ struct x509_certificate { ...@@ -39,7 +39,9 @@ struct x509_certificate {
unsigned index; unsigned index;
bool seen; /* Infinite recursion prevention */ bool seen; /* Infinite recursion prevention */
bool verified; bool verified;
bool unsupported_crypto; /* T if can't be verified due to missing crypto */ bool self_signed; /* T if self-signed (check unsupported_sig too) */
bool unsupported_key; /* T if key uses unsupported crypto */
bool unsupported_sig; /* T if signature uses unsupported crypto */
}; };
/* /*
...@@ -55,5 +57,4 @@ extern int x509_decode_time(time64_t *_t, size_t hdrlen, ...@@ -55,5 +57,4 @@ extern int x509_decode_time(time64_t *_t, size_t hdrlen,
* x509_public_key.c * x509_public_key.c
*/ */
extern int x509_get_sig_params(struct x509_certificate *cert); extern int x509_get_sig_params(struct x509_certificate *cert);
extern int x509_check_signature(const struct public_key *pub, extern int x509_check_for_self_signed(struct x509_certificate *cert);
struct x509_certificate *cert);
...@@ -165,10 +165,20 @@ int x509_get_sig_params(struct x509_certificate *cert) ...@@ -165,10 +165,20 @@ int x509_get_sig_params(struct x509_certificate *cert)
pr_devel("==>%s()\n", __func__); pr_devel("==>%s()\n", __func__);
if (cert->unsupported_crypto) if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
return -ENOPKG; !pkey_algo[cert->pub->pkey_algo])
if (sig->rsa.s) cert->unsupported_key = true;
if (sig->pkey_algo >= PKEY_ALGO__LAST ||
!pkey_algo[sig->pkey_algo])
cert->unsupported_sig = true;
/* We check the hash if we can - even if we can't then verify it */
if (sig->pkey_hash_algo >= PKEY_HASH__LAST ||
!hash_algo_name[sig->pkey_hash_algo]) {
cert->unsupported_sig = true;
return 0; return 0;
}
sig->rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size); sig->rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
if (!sig->rsa.s) if (!sig->rsa.s)
...@@ -181,8 +191,8 @@ int x509_get_sig_params(struct x509_certificate *cert) ...@@ -181,8 +191,8 @@ int x509_get_sig_params(struct x509_certificate *cert)
tfm = crypto_alloc_shash(hash_algo_name[sig->pkey_hash_algo], 0, 0); tfm = crypto_alloc_shash(hash_algo_name[sig->pkey_hash_algo], 0, 0);
if (IS_ERR(tfm)) { if (IS_ERR(tfm)) {
if (PTR_ERR(tfm) == -ENOENT) { if (PTR_ERR(tfm) == -ENOENT) {
cert->unsupported_crypto = true; cert->unsupported_sig = true;
return -ENOPKG; return 0;
} }
return PTR_ERR(tfm); return PTR_ERR(tfm);
} }
...@@ -217,26 +227,60 @@ int x509_get_sig_params(struct x509_certificate *cert) ...@@ -217,26 +227,60 @@ int x509_get_sig_params(struct x509_certificate *cert)
EXPORT_SYMBOL_GPL(x509_get_sig_params); EXPORT_SYMBOL_GPL(x509_get_sig_params);
/* /*
* Check the signature on a certificate using the provided public key * Check for self-signedness in an X.509 cert and if found, check the signature
* immediately if we can.
*/ */
int x509_check_signature(const struct public_key *pub, int x509_check_for_self_signed(struct x509_certificate *cert)
struct x509_certificate *cert)
{ {
int ret; int ret = 0;
pr_devel("==>%s()\n", __func__); pr_devel("==>%s()\n", __func__);
ret = x509_get_sig_params(cert); if (cert->raw_subject_size != cert->raw_issuer_size ||
if (ret < 0) memcmp(cert->raw_subject, cert->raw_issuer,
return ret; cert->raw_issuer_size) != 0)
goto not_self_signed;
if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
/* If the AKID is present it may have one or two parts. If
* both are supplied, both must match.
*/
bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
if (!a && !b)
goto not_self_signed;
ret = -EKEYREJECTED;
if (((a && !b) || (b && !a)) &&
cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
goto out;
}
ret = -EKEYREJECTED;
if (cert->pub->pkey_algo != cert->sig->pkey_algo)
goto out;
ret = public_key_verify_signature(pub, cert->sig); ret = public_key_verify_signature(cert->pub, cert->sig);
if (ret == -ENOPKG) if (ret < 0) {
cert->unsupported_crypto = true; if (ret == -ENOPKG) {
pr_debug("Cert Verification: %d\n", ret); cert->unsupported_sig = true;
ret = 0;
}
goto out;
}
pr_devel("Cert Self-signature verified");
cert->self_signed = true;
out:
pr_devel("<==%s() = %d\n", __func__, ret);
return ret; return ret;
not_self_signed:
pr_devel("<==%s() = 0 [not]\n", __func__);
return 0;
} }
EXPORT_SYMBOL_GPL(x509_check_signature);
/* /*
* Check the new certificate against the ones in the trust keyring. If one of * Check the new certificate against the ones in the trust keyring. If one of
...@@ -256,20 +300,25 @@ static int x509_validate_trust(struct x509_certificate *cert, ...@@ -256,20 +300,25 @@ static int x509_validate_trust(struct x509_certificate *cert,
if (!trust_keyring) if (!trust_keyring)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid)) if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
return -EPERM; return -EPERM;
if (cert->unsupported_sig)
return -ENOPKG;
key = x509_request_asymmetric_key(trust_keyring, key = x509_request_asymmetric_key(trust_keyring,
sig->auth_ids[0], sig->auth_ids[1], sig->auth_ids[0], sig->auth_ids[1],
false); false);
if (!IS_ERR(key)) { if (IS_ERR(key))
if (!use_builtin_keys return PTR_ERR(key);
|| test_bit(KEY_FLAG_BUILTIN, &key->flags))
ret = x509_check_signature(key->payload.data[asym_crypto], if (!use_builtin_keys ||
cert); test_bit(KEY_FLAG_BUILTIN, &key->flags)) {
key_put(key); ret = public_key_verify_signature(
key->payload.data[asym_crypto], cert->sig);
if (ret == -ENOPKG)
cert->unsupported_sig = true;
} }
key_put(key);
return ret; return ret;
} }
...@@ -292,36 +341,42 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) ...@@ -292,36 +341,42 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
pr_devel("Cert Issuer: %s\n", cert->issuer); pr_devel("Cert Issuer: %s\n", cert->issuer);
pr_devel("Cert Subject: %s\n", cert->subject); pr_devel("Cert Subject: %s\n", cert->subject);
if (cert->pub->pkey_algo >= PKEY_ALGO__LAST || if (cert->unsupported_key) {
cert->sig->pkey_algo >= PKEY_ALGO__LAST ||
cert->sig->pkey_hash_algo >= PKEY_HASH__LAST ||
!pkey_algo[cert->pub->pkey_algo] ||
!pkey_algo[cert->sig->pkey_algo] ||
!hash_algo_name[cert->sig->pkey_hash_algo]) {
ret = -ENOPKG; ret = -ENOPKG;
goto error_free_cert; goto error_free_cert;
} }
pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]); pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to); pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
pr_devel("Cert Signature: %s + %s\n",
pkey_algo_name[cert->sig->pkey_algo],
hash_algo_name[cert->sig->pkey_hash_algo]);
cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
cert->pub->id_type = PKEY_ID_X509; cert->pub->id_type = PKEY_ID_X509;
/* Check the signature on the key if it appears to be self-signed */ /* Check the signature on the key if it appears to be self-signed */
if ((!cert->sig->auth_ids[0] && !cert->sig->auth_ids[1]) || if (cert->unsupported_sig) {
asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]) || public_key_free(NULL, cert->sig);
asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0])) { cert->sig = NULL;
ret = x509_check_signature(cert->pub, cert); /* self-signed */
if (ret < 0)
goto error_free_cert;
} else { } else {
ret = x509_validate_trust(cert, get_system_trusted_keyring()); pr_devel("Cert Signature: %s + %s\n",
if (ret == -EKEYREJECTED) pkey_algo_name[cert->sig->pkey_algo],
goto error_free_cert; hash_algo_name[cert->sig->pkey_hash_algo]);
if (cert->self_signed) {
/* Self-signed */
if (cert->unsupported_sig) {
ret = -ENOPKG;
goto error_free_cert;
}
/* There's no point retaining the signature */
public_key_free(NULL, cert->sig);
cert->sig = NULL;
} else {
ret = x509_validate_trust(cert,
get_system_trusted_keyring());
if (ret == -EKEYREJECTED)
goto error_free_cert;
}
} }
/* Propose a description */ /* Propose a description */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment