Skip to contentSkip to conversation

X.509: Fix determination of self-signedness

PATCH In review David Howells 2 participants 3 messages Dec 18, 2015

Subsystems (2)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 X.509: Fix determination of self-signedness

Fix determination of whether an X.509 certificate is self-signed or not.

It is currently assumed that a cert is self-signed if has no authorityKeyIdentifier or the authorityKeyIdentifier matches the subjectKeyIdentifier. However, it is possible to encounter a certificate that has neither AKID not SKID but is not self-signed.

This symptoms of this show up as an attempt to load a certificate failing with -ERANGE or -EBADMSG, produced from the RSA module when the result of calculating "m = s^e mod n" is checked.

To fix this, don't check to see if a certificate is self-signed if the Issuer and Subject names differ.

cc David Woodhouse <[email protected]>

---

 crypto/asymmetric_keys/x509_public_key.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
crypto/asymmetric_keys/x509_public_key.c +8 -3
index 2a44b3752471..6236e7996f19 100644
@@ -313,9 +313,14 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
 	cert->pub->id_type = PKEY_ID_X509;
 
 	/* Check the signature on the key if it appears to be self-signed */
-	if ((!cert->akid_skid && !cert->akid_id) ||
-	    asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
-	    asymmetric_key_id_same(cert->id, cert->akid_id)) {
+	if ((!cert->akid_skid && !cert->akid_id)) {
+		if (cert->raw_issuer_size == cert->raw_subject_size &&
+		    memcmp(cert->raw_issuer, cert->raw_subject,
+			   cert->raw_subject_size) == 0)
+			goto self_signed;
+	} else if (asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
+		   asymmetric_key_id_same(cert->id, cert->akid_id)) {
+self_signed:
 		ret = x509_check_signature(cert->pub, cert); /* self-signed */
 		if (ret < 0)
 			goto error_free_cert;
#1 lore raw
Quoted 11 lines · David Howells

Fix determination of whether an X.509 certificate is self-signed or not.

It is currently assumed that a cert is self-signed if has no authorityKeyIdentifier or the authorityKeyIdentifier matches the subjectKeyIdentifier. However, it is possible to encounter a certificate that has neither AKID not SKID but is not self-signed.

This symptoms of this show up as an attempt to load a certificate failing with -ERANGE or -EBADMSG, produced from the RSA module when the result of calculating "m = s^e mod n" is checked.

Quoted 5 lines

To fix this, don't check to see if a certificate is self-signed if the Issuer and Subject names differ.

cc David Woodhouse <[email protected]>

Should this also be Cc'd to stable?

josh

Quoted 28 lines

---

 crypto/asymmetric_keys/x509_public_key.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
crypto/asymmetric_keys/x509_public_key.c +8 -3
index 2a44b3752471..6236e7996f19 100644
@@ -313,9 +313,14 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
        cert->pub->id_type = PKEY_ID_X509;
 
        /* Check the signature on the key if it appears to be self-signed */
-       if ((!cert->akid_skid && !cert->akid_id) ||
-           asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
-           asymmetric_key_id_same(cert->id, cert->akid_id)) {
+       if ((!cert->akid_skid && !cert->akid_id)) {
+               if (cert->raw_issuer_size == cert->raw_subject_size &&
+                   memcmp(cert->raw_issuer, cert->raw_subject,
+                          cert->raw_subject_size) == 0)
+                       goto self_signed;
+       } else if (asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
+                  asymmetric_key_id_same(cert->id, cert->akid_id)) {
+self_signed:
                ret = x509_check_signature(cert->pub, cert); /* self-signed */
                if (ret < 0)
                        goto error_free_cert;
author maintainer
#2 lore raw
Quoted 1 line · Josh Boyer

Should this also be Cc'd to stable?

Argh. Probably.

David