Skip to content

Commit 1337585

Browse files
committed
feat: Map rsaesOaep SPKI to RSA in parse_key_type
TPM 1.2 Endorsement Key certificates use rsaesOaep (OID 1.2.840.113549.1.1.7) as their SubjectPublicKeyInfo algorithm identifier. The underlying key is a standard RSA key. Without this mapping, X509_get_pubkey() fails with PUBLIC_KEY_DECODE_ERROR and X509_verify_cert() cannot validate these certificates. Add NID_rsaesOaep alongside the existing NID_rsa special case in parse_key_type() to return rsa_asn1_meth for both. Add a test for public key extraction using a real TPM 1.2 EK certificate with rsaesOaep SPKI.
1 parent dd90637 commit 1337585

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

crypto/evp_extra/evp_asn1.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
// specific key type implementations like PQDSA.
7979
// The OID is then searched against ASN.1 methods for a method with that OID.
8080
// As the |OID| is read from |cbs| the buffer is advanced.
81-
// For the case of |NID_rsa| the method |rsa_asn1_meth| is returned.
81+
// For the case of |NID_rsa| or |NID_rsaesOaep| the method |rsa_asn1_meth| is returned.
8282
// For the case of |EVP_PKEY_PQDSA| the method |pqdsa_asn1.meth| is returned.
8383
// For the case of |EVP_PKEY_KEM| the method |kem_asn1.meth| is returned.
8484
static const EVP_PKEY_ASN1_METHOD *parse_key_type(CBS *cbs, CBS *out_oid) {
@@ -99,20 +99,25 @@ static const EVP_PKEY_ASN1_METHOD *parse_key_type(CBS *cbs, CBS *out_oid) {
9999
}
100100
}
101101

102-
// Special logic to handle the rarer |NID_rsa|.
102+
// Special logic to handle the rarer |NID_rsa| and |NID_rsaesOaep|.
103+
// NID_rsa:
103104
// https://www.itu.int/ITU-T/formal-language/itu-t/x/x509/2008/AlgorithmObjectIdentifiers.html
104-
if (OBJ_cbs2nid(&oid) == NID_rsa) {
105+
// NID_rsaesOaep: underlying key is the same as |NID_rsa|. Used by
106+
// TPM 1.2 Endorsement Key certificates per TCG Credential Profiles
107+
// V1.2, section 3.2.7.
108+
int nid = OBJ_cbs2nid(&oid);
109+
if (nid == NID_rsa || nid == NID_rsaesOaep) {
105110
return &rsa_asn1_meth;
106111
}
107112

108113
// The pkey_id for the pqdsa_asn1_meth is EVP_PKEY_PQDSA, as this holds all
109114
// asn1 functions for pqdsa types. However, the incoming CBS has the OID for
110115
// the specific algorithm. So we must search explicitly for the algorithm.
111-
const EVP_PKEY_ASN1_METHOD *pqdsa_method = PQDSA_find_asn1_by_nid(OBJ_cbs2nid(&oid));
116+
const EVP_PKEY_ASN1_METHOD *pqdsa_method = PQDSA_find_asn1_by_nid(nid);
112117
if (pqdsa_method != NULL) {
113118
return pqdsa_method;
114119
}
115-
return KEM_find_asn1_by_nid(OBJ_cbs2nid(&oid));
120+
return KEM_find_asn1_by_nid(nid);
116121
}
117122

118123
EVP_PKEY *EVP_parse_public_key(CBS *cbs) {

crypto/x509/x509_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6284,6 +6284,47 @@ TEST(X509Test, ITUT_X509_nid_rsa) {
62846284
EXPECT_TRUE(rsa);
62856285
}
62866286

6287+
// kRsaesOaepCertPEM is a TPM 1.2 EK certificate with |NID_rsaesOaep| SPKI.
6288+
static const char kRsaesOaepCertPEM[] = R"(
6289+
-----BEGIN CERTIFICATE-----
6290+
MIIEXjCCA0agAwIBAgIUBchBXcXPAWxNMJEsLXEXHv/eVZswDQYJKoZIhvcNAQEF
6291+
BQAwVTELMAkGA1UEBhMCQ0gxHjAcBgNVBAoTFVNUTWljcm9lbGVjdHJvbmljcyBO
6292+
VjEmMCQGA1UEAxMdU1RNIFRQTSBFSyBJbnRlcm1lZGlhdGUgQ0EgMDIwHhcNMjEw
6293+
OTA0MDAwMDAwWhcNMzEwOTA0MDAwMDAwWjAAMIIBNzAiBgkqhkiG9w0BAQcwFaIT
6294+
MBEGCSqGSIb3DQEBCQQEVENQQQOCAQ8AMIIBCgKCAQEAlZyGH0K5061jb9glk9FJ
6295+
eghjeAqca0hRKExVLOLkNeZ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ct09jAC7N3wbqUn/4lQk
6311+
2Tq79zfsu5Px2BUWnWiEA9F9ThA0XVSll1CWii199BZECIY6VXkjYBHUT1ptb2Bt
6312+
53HR/+FcCwtEWG37GrrOHwJqqDYbW3ACPSZxFuMbUg1V/56pk/ZBY43ue7CGSVuC
6313+
HPAeERH+07hX5/7YXaH6MCnl
6314+
-----END CERTIFICATE-----
6315+
)";
6316+
6317+
TEST(X509Test, RsaesOaepSPKI) {
6318+
bssl::UniquePtr<X509> cert(CertFromPEM(kRsaesOaepCertPEM));
6319+
ASSERT_TRUE(cert);
6320+
6321+
bssl::UniquePtr<EVP_PKEY> evp_pkey(X509_get_pubkey(cert.get()));
6322+
EXPECT_TRUE(evp_pkey);
6323+
6324+
bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(evp_pkey.get()));
6325+
EXPECT_TRUE(rsa);
6326+
}
6327+
62876328
// kLargeSerialPEM is a certificate with a large serial number.
62886329
static const char kLargeSerialPEM[] = R"(
62896330
-----BEGIN CERTIFICATE-----

0 commit comments

Comments
 (0)