pki: Limit MTC CA IDs to 32 bytes draft-ietf-plants-merkle-tree-certs-07, Section 5.1, defines an MTC CA ID as a trust anchor ID, which draft-ietf-tls-trust-anchor-ids-06 limits to 32 bytes in its binary (RELATIVE-OID contents) representation. Enforce this limit in ParseMtcCaInfo and EncodeMtcCaCertificate, matching crypto/x509 (https://boringssl-review.googlesource.com/c/boringssl/+/103948). Bug: 575507260 Change-Id: Ib0870c0e8f59e57a2c8a57c6ff4943f36a6a6964 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106169 Auto-Submit: Lily Chen <chlily@google.com> Reviewed-by: Matt Mueller <mattm@google.com> Commit-Queue: Matt Mueller <mattm@google.com> Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/pki/encode_certificate.cc b/pki/encode_certificate.cc index 28c45c0..b0f6c87 100644 --- a/pki/encode_certificate.cc +++ b/pki/encode_certificate.cc
@@ -14,6 +14,8 @@ #include "encode_certificate.h" +#include <stddef.h> + #include <openssl/bytestring.h> #include <openssl/nid.h> #include <openssl/pool.h> @@ -28,6 +30,9 @@ // draft-ietf-plants-merkle-tree-certs-07, Section 5.5: mtcMinSerial. constexpr uint64_t kMtcMinSerial = uint64_t{1} << 48; +// draft-ietf-tls-trust-anchor-ids-06 limits trust anchor IDs to 32 bytes. +constexpr size_t kMaxTrustAnchorIdBytes = 32; + // `EncodeMtcCaCertificate` produces a certificate that is not issued by anyone, // so its serial number is arbitrary, but RFC 5280, Section 4.1.2.2, requires it // to be positive. @@ -93,7 +98,8 @@ CBS_len(&key_cbs) != 0) { return nullptr; } - if (!CBS_is_valid_asn1_relative_oid(&ca_id_cbs)) { + if (!CBS_is_valid_asn1_relative_oid(&ca_id_cbs) || + CBS_len(&ca_id_cbs) > kMaxTrustAnchorIdBytes) { return nullptr; }
diff --git a/pki/encode_certificate.h b/pki/encode_certificate.h index 6b67372..cf4a1c5 100644 --- a/pki/encode_certificate.h +++ b/pki/encode_certificate.h
@@ -28,11 +28,11 @@ // Returns an MTC CA certificate encoded according to // draft-ietf-plants-merkle-tree-certs-07, Section 5.5, or nullptr on error. // The certificate is allocated from `pool` (which may be null). `ca_id` -// contains the DER encoding of the relative OID of the CA's ID, and -// `ca_cosign_sigalg` and `ca_cosign_key` (a DER-encoded SubjectPublicKeyInfo) -// describe the CA cosigner algorithm and key. Its allowed serial number range -// is set to `min_serial`..`max_serial`, inclusive, which is empty if -// `min_serial` > `max_serial`. The certificate is unsigned (RFC 9925). +// contains the DER encoding of the relative OID of the CA's ID (at most 32 +// bytes), and `ca_cosign_sigalg` and `ca_cosign_key` (a DER-encoded +// SubjectPublicKeyInfo) describe the CA cosigner algorithm and key. Its allowed +// serial number range is set to `min_serial`..`max_serial`, inclusive, which is +// empty if `min_serial` > `max_serial`. The certificate is unsigned (RFC 9925). OPENSSL_EXPORT UniquePtr<CRYPTO_BUFFER> EncodeMtcCaCertificate( Span<const uint8_t> ca_id, SignatureAlgorithm ca_cosign_sigalg, Span<const uint8_t> ca_cosign_key, uint64_t min_serial, uint64_t max_serial,
diff --git a/pki/encode_certificate_unittest.cc b/pki/encode_certificate_unittest.cc index d8c3a98..1871d51 100644 --- a/pki/encode_certificate_unittest.cc +++ b/pki/encode_certificate_unittest.cc
@@ -115,6 +115,12 @@ EXPECT_TRUE(encode(kCaId, spki)); EXPECT_FALSE(encode(kInvalidCaId, spki)); EXPECT_FALSE(encode({}, spki)); + // Trust anchor IDs are at most 32 bytes. + const std::vector<uint8_t> max_ca_id(32, 0x01); + const std::vector<uint8_t> too_long_ca_id(33, 0x01); + EXPECT_TRUE(EncodeAndParseMtcCaCertificate( + max_ca_id, SignatureAlgorithm::kMldsa44, spki, kMinSerial, kMaxSerial)); + EXPECT_FALSE(encode(too_long_ca_id, spki)); EXPECT_FALSE(encode(kCaId, kInvalidSpki)); EXPECT_FALSE(encode(kCaId, {})); EXPECT_FALSE(encode(kCaId, spki_trailing_data));
diff --git a/pki/parse_certificate.cc b/pki/parse_certificate.cc index 3268f0e..f40871d 100644 --- a/pki/parse_certificate.cc +++ b/pki/parse_certificate.cc
@@ -14,6 +14,8 @@ #include "parse_certificate.h" +#include <stddef.h> + #include <optional> #include <utility> @@ -1023,6 +1025,9 @@ // mtcMaxSerial INTEGER ::= 18446744073709551615 -- 2^64-1 constexpr uint64_t kMtcMinSerial = uint64_t{1} << 48; +// draft-ietf-tls-trust-anchor-ids-06 limits trust anchor IDs to 32 bytes. +constexpr size_t kMaxTrustAnchorIdBytes = 32; + // From draft-ietf-plants-merkle-tree-certs-07, Section 5.1: // // id-rdna-trustAnchorID OBJECT IDENTIFIER ::= { @@ -1052,9 +1057,10 @@ } // Parses `subject_tlv`, checking that it consists of a single -// RelativeDistinguishedName with a single valid RELATIVE-OID attribute of type -// id-rdna-trustAnchorID, per draft-ietf-plants-merkle-tree-certs-07, section -// 5.1. On success, sets `*value` to that attribute's value. +// RelativeDistinguishedName with a single valid RELATIVE-OID attribute (at most +// 32 bytes) of type id-rdna-trustAnchorID, per +// draft-ietf-plants-merkle-tree-certs-07, section 5.1. On success, sets +// `*value` to that attribute's value. [[nodiscard]] bool ParseMtcCaSubject(der::Input subject_tlv, der::Input *value) { RDNSequence rdns; @@ -1066,7 +1072,8 @@ } CBS ca_id; CBS_init(&ca_id, rdns[0][0].value.data(), rdns[0][0].value.size()); - if (!CBS_is_valid_asn1_relative_oid(&ca_id)) { + if (!CBS_is_valid_asn1_relative_oid(&ca_id) || + CBS_len(&ca_id) > kMaxTrustAnchorIdBytes) { return false; } *value = rdns[0][0].value;
diff --git a/pki/parse_certificate.h b/pki/parse_certificate.h index 587ce38..2f3b9b9 100644 --- a/pki/parse_certificate.h +++ b/pki/parse_certificate.h
@@ -661,7 +661,7 @@ // maxSerial INTEGER (mtcMinSerial..mtcMaxSerial) } struct OPENSSL_EXPORT ParsedMtcCaInfo { // The CA ID, i.e. the contents of the RELATIVE-OID in the subject's single - // id-rdna-trustAnchorID attribute. + // id-rdna-trustAnchorID attribute (at most 32 bytes). der::Input ca_id; // The DER-encoded AlgorithmIdentifier for the CA cosigner.
diff --git a/pki/parsed_certificate_unittest.cc b/pki/parsed_certificate_unittest.cc index 8190f39..653cc03 100644 --- a/pki/parsed_certificate_unittest.cc +++ b/pki/parsed_certificate_unittest.cc
@@ -14,6 +14,8 @@ #include "parsed_certificate.h" +#include <vector> + #include <gtest/gtest.h> #include <openssl/nid.h> @@ -637,13 +639,11 @@ EXPECT_EQ(mtc_ca.max_serial, (uint64_t{1} << 48) + 4096); } -// Unlike crypto/x509, libpki does not limit the length of the CA ID. TEST(ParsedCertificateTest, MtcCaExtensionLongCAId) { - std::shared_ptr<const ParsedCertificate> cert = - ReadCryptoX509MTCTestCert("ca_id_too_long/ca_cert.pem"); - ASSERT_TRUE(cert); - ASSERT_TRUE(cert->mtc_ca_info()); - EXPECT_EQ(cert->mtc_ca_info()->ca_id.size(), 36u); + CertErrors errors; + EXPECT_FALSE( + ReadCryptoX509MTCTestCert("ca_id_too_long/ca_cert.pem", &errors)); + EXPECT_EQ(errors.ToDebugString(), "ERROR: Failed parsing MTC CA info\n"); } TEST(ParsedCertificateTest, MtcCaExtensionTrailingData) { @@ -691,6 +691,31 @@ 0x0d, 1, // RELATIVE-OID 0x01, // 1 }; + // Name with a 32-byte RELATIVE-OID (the maximum allowed length). + static constexpr uint8_t kSubjectMaxCaId[] = { + 0x30, 48, // SEQUENCE (48 bytes) + 0x31, 46, // SET (46 bytes) + 0x30, 44, // SEQUENCE (44 bytes) + 0x06, 8, OBJ_ENC_rdna_trustAnchorID, // OBJECT IDENTIFIER (8 bytes) + 0x0d, 32, // RELATIVE-OID (32 bytes) + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + }; + // Name with a 33-byte RELATIVE-OID (exceeding the 32-byte limit). + static constexpr uint8_t kSubjectCaIdTooLong[] = { + 0x30, 49, // SEQUENCE (49 bytes) + 0x31, 47, // SET (47 bytes) + 0x30, 45, // SEQUENCE (45 bytes) + 0x06, 8, OBJ_ENC_rdna_trustAnchorID, // OBJECT IDENTIFIER (8 bytes) + 0x0d, 33, // RELATIVE-OID (33 bytes) + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, + }; // Same as above, but with an invalid RELATIVE-OID. static constexpr uint8_t kSubjectInvalidRelativeOid[] = { 0x30, 18, // SEQUENCE (Name) @@ -783,6 +808,15 @@ EXPECT_EQ(out.max_serial, (uint64_t{1} << 48) + 1); } +TEST_F(ParseMtcCaExtensionTest, MaxLengthCaId) { + ParsedMtcCaInfo out; + ASSERT_TRUE(ParseMtcCaInfo( + MakeExtension(der::Input(kExtensionValue), /*critical=*/true), + der::Input(kSubjectMaxCaId), &out)); + const std::vector<uint8_t> expected_ca_id(32, 0x01); + EXPECT_EQ(out.ca_id, der::Input(expected_ca_id)); +} + TEST_F(ParseMtcCaExtensionTest, NotCritical) { ParsedMtcCaInfo out; EXPECT_FALSE(ParseMtcCaInfo( @@ -794,6 +828,7 @@ const ParsedExtension extension = MakeExtension(der::Input(kExtensionValue), /*critical=*/true); for (der::Input subject : { + der::Input(kSubjectCaIdTooLong), der::Input(kSubjectInvalidRelativeOid), der::Input(kSubjectUTF8String), der::Input(kSubjectTwoRDNs),