| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "parsed_certificate.h" |
| 16 |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <openssl/pool.h> |
| 19 | #include "cert_errors.h" |
| 20 | #include "input.h" |
| 21 | #include "parse_certificate.h" |
| 22 | #include "test_helpers.h" |
| 23 |
| 24 | // TODO(eroman): Add tests for parsing of policy mappings. |
| 25 |
| 26 | BSSL_NAMESPACE_BEGIN |
| 27 |
| 28 | namespace { |
| 29 |
| 30 | std::string GetFilePath(const std::string &file_name) { |
| 31 | return std::string("testdata/parse_certificate_unittest/") + file_name; |
| 32 | } |
| 33 |
| 34 | // Reads and parses a certificate from the PEM file |file_name|. |
| 35 | // |
| 36 | // Returns nullptr if the certificate parsing failed, and verifies that any |
| 37 | // errors match the ERRORS block in the .pem file. |
| 38 | std::shared_ptr<const ParsedCertificate> ParseCertificateFromFile( |
| 39 | const std::string &file_name, const ParseCertificateOptions &options) { |
| 40 | std::string data; |
| 41 | std::string expected_errors; |
| 42 |
| 43 | // Read the certificate data and error expectations from a single PEM file. |
| 44 | const PemBlockMapping mappings[] = { |
| 45 | {"CERTIFICATE", &data}, |
| 46 | {"ERRORS", &expected_errors, true /*optional*/}, |
| 47 | }; |
| 48 | std::string test_file_path = GetFilePath(file_name); |
| 49 | EXPECT_TRUE(ReadTestDataFromPemFile(test_file_path, mappings)); |
| 50 |
| 51 | CertErrors errors; |
| 52 | std::shared_ptr<const ParsedCertificate> cert = ParsedCertificate::Create( |
| 53 | bssl::UniquePtr<CRYPTO_BUFFER>( |
| 54 | CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(data.data()), |
| 55 | data.size(), nullptr)), |
| 56 | options, &errors); |
| 57 |
| 58 | // The errors are baselined for |!allow_invalid_serial_numbers|. So if |
| 59 | // requesting a non-default option skip the error checks. |
| 60 | // TODO(eroman): This is ugly. |
| 61 | if (!options.allow_invalid_serial_numbers) { |
| 62 | VerifyCertErrors(expected_errors, errors, test_file_path); |
| 63 | } |
| 64 |
| 65 | // Every parse failure being tested should emit error information. |
| 66 | if (!cert) { |
| 67 | EXPECT_FALSE(errors.ToDebugString().empty()); |
| 68 | } |
| 69 |
| 70 | return cert; |
| 71 | } |
| 72 |
| 73 | der::Input DavidBenOid() { |
| 74 | // This OID corresponds with |
| 75 | // 1.2.840.113554.4.1.72585.0 (https://davidben.net/oid) |
| 76 | static const uint8_t kOid[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, |
| 77 | 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00}; |
| 78 | return der::Input(kOid); |
| 79 | } |
| 80 |
| 81 | // Parses an Extension whose critical field is true (255). |
| 82 | TEST(ParsedCertificateTest, ExtensionCritical) { |
| 83 | std::shared_ptr<const ParsedCertificate> cert = |
| 84 | ParseCertificateFromFile("extension_critical.pem", {}); |
| 85 | ASSERT_TRUE(cert); |
| 86 |
| 87 | const uint8_t kExpectedValue[] = {0x30, 0x00}; |
| 88 |
| 89 | ParsedExtension extension; |
| 90 | ASSERT_TRUE(cert->GetExtension(DavidBenOid(), &extension)); |
| 91 |
| 92 | EXPECT_TRUE(extension.critical); |
| 93 | EXPECT_EQ(DavidBenOid(), extension.oid); |
| 94 | EXPECT_EQ(der::Input(kExpectedValue), extension.value); |
| 95 | } |
| 96 |
| 97 | // Parses an Extension whose critical field is false (omitted). |
| 98 | TEST(ParsedCertificateTest, ExtensionNotCritical) { |
| 99 | std::shared_ptr<const ParsedCertificate> cert = |
| 100 | ParseCertificateFromFile("extension_not_critical.pem", {}); |
| 101 | ASSERT_TRUE(cert); |
| 102 |
| 103 | const uint8_t kExpectedValue[] = {0x30, 0x00}; |
| 104 |
| 105 | ParsedExtension extension; |
| 106 | ASSERT_TRUE(cert->GetExtension(DavidBenOid(), &extension)); |
| 107 |
| 108 | EXPECT_FALSE(extension.critical); |
| 109 | EXPECT_EQ(DavidBenOid(), extension.oid); |
| 110 | EXPECT_EQ(der::Input(kExpectedValue), extension.value); |
| 111 | } |
| 112 |
| 113 | // Parses an Extension whose critical field is 0. This is in one sense FALSE, |
| 114 | // however because critical has DEFAULT of false this is in fact invalid |
| 115 | // DER-encoding. |
| 116 | TEST(ParsedCertificateTest, ExtensionCritical0) { |
| 117 | ASSERT_FALSE(ParseCertificateFromFile("extension_critical_0.pem", {})); |
| 118 | } |
| 119 |
| 120 | // Parses an Extension whose critical field is 3. Under DER-encoding BOOLEAN |
| 121 | // values must an octet of either all zero bits, or all 1 bits, so this is not |
| 122 | // valid. |
| 123 | TEST(ParsedCertificateTest, ExtensionCritical3) { |
| 124 | ASSERT_FALSE(ParseCertificateFromFile("extension_critical_3.pem", {})); |
| 125 | } |
| 126 |
| 127 | // Parses an Extensions that is an empty sequence. |
| 128 | TEST(ParsedCertificateTest, ExtensionsEmptySequence) { |
| 129 | ASSERT_FALSE(ParseCertificateFromFile("extensions_empty_sequence.pem", {})); |
| 130 | } |
| 131 |
| 132 | // Parses an Extensions that is not a sequence. |
| 133 | TEST(ParsedCertificateTest, ExtensionsNotSequence) { |
| 134 | ASSERT_FALSE(ParseCertificateFromFile("extensions_not_sequence.pem", {})); |
| 135 | } |
| 136 |
| 137 | // Parses an Extensions that has data after the sequence. |
| 138 | TEST(ParsedCertificateTest, ExtensionsDataAfterSequence) { |
| 139 | ASSERT_FALSE( |
| 140 | ParseCertificateFromFile("extensions_data_after_sequence.pem", {})); |
| 141 | } |
| 142 |
| 143 | // Parses an Extensions that contains duplicated key usages. |
| 144 | TEST(ParsedCertificateTest, ExtensionsDuplicateKeyUsage) { |
| 145 | ASSERT_FALSE( |
| 146 | ParseCertificateFromFile("extensions_duplicate_key_usage.pem", {})); |
| 147 | } |
| 148 |
| 149 | // Parses a certificate with a bad key usage extension (BIT STRING with zero |
| 150 | // elements). |
| 151 | TEST(ParsedCertificateTest, BadKeyUsage) { |
| 152 | ASSERT_FALSE(ParseCertificateFromFile("bad_key_usage.pem", {})); |
| 153 | } |
| 154 |
| 155 | // Parses a certificate that has a PolicyQualifierInfo that is missing the |
| 156 | // qualifier field. |
| 157 | TEST(ParsedCertificateTest, BadPolicyQualifiers) { |
| 158 | ASSERT_FALSE(ParseCertificateFromFile("bad_policy_qualifiers.pem", {})); |
| 159 | } |
| 160 |
| 161 | // Parses a certificate that uses an unknown signature algorithm OID (00). |
| 162 | TEST(ParsedCertificateTest, BadSignatureAlgorithmOid) { |
| 163 | std::shared_ptr<const ParsedCertificate> cert = |
| 164 | ParseCertificateFromFile("bad_signature_algorithm_oid.pem", {}); |
| 165 | ASSERT_TRUE(cert); |
| 166 | ASSERT_FALSE(cert->signature_algorithm()); |
| 167 | } |
| 168 |
| 169 | // The validity encodes time as UTCTime but following the BER rules rather than |
| 170 | // DER rules (i.e. YYMMDDHHMMZ instead of YYMMDDHHMMSSZ). |
| 171 | TEST(ParsedCertificateTest, BadValidity) { |
| 172 | ASSERT_FALSE(ParseCertificateFromFile("bad_validity.pem", {})); |
| 173 | } |
| 174 |
| 175 | // The signature algorithm contains an unexpected parameters field. |
| 176 | TEST(ParsedCertificateTest, FailedSignatureAlgorithm) { |
| 177 | std::shared_ptr<const ParsedCertificate> cert = |
| 178 | ParseCertificateFromFile("failed_signature_algorithm.pem", {}); |
| 179 | ASSERT_TRUE(cert); |
| 180 | ASSERT_FALSE(cert->signature_algorithm()); |
| 181 | } |
| 182 |
| 183 | // '&' is not acceptable in a printable string. |
| 184 | TEST(ParsedCertificateTest, IssuerBadPrintableString) { |
| 185 | ASSERT_FALSE(ParseCertificateFromFile("issuer_bad_printable_string.pem", {})); |
| 186 | } |
| 187 |
| 188 | TEST(ParsedCertificateTest, NameConstraintsBadIp) { |
| 189 | ASSERT_FALSE(ParseCertificateFromFile("name_constraints_bad_ip.pem", {})); |
| 190 | } |
| 191 |
| 192 | TEST(ParsedCertificateTest, PolicyQualifiersEmptySequence) { |
| 193 | ASSERT_FALSE( |
| 194 | ParseCertificateFromFile("policy_qualifiers_empty_sequence.pem", {})); |
| 195 | } |
| 196 |
| 197 | TEST(ParsedCertificateTest, SubjectBlankSubjectAltNameNotCritical) { |
| 198 | ASSERT_FALSE(ParseCertificateFromFile( |
| 199 | "subject_blank_subjectaltname_not_critical.pem", {})); |
| 200 | } |
| 201 |
| 202 | // Non-ASCII character in locality name in subject. |
| 203 | TEST(ParsedCertificateTest, SubjectNotAscii) { |
| 204 | ASSERT_FALSE(ParseCertificateFromFile("subject_not_ascii.pem", {})); |
| 205 | } |
| 206 |
| 207 | // '&' is not acceptable in a printable string. |
| 208 | TEST(ParsedCertificateTest, SubjectNotPrintableString) { |
| 209 | ASSERT_FALSE( |
| 210 | ParseCertificateFromFile("subject_not_printable_string.pem", {})); |
| 211 | } |
| 212 |
| 213 | TEST(ParsedCertificateTest, SubjectAltNameBadIp) { |
| 214 | ASSERT_FALSE(ParseCertificateFromFile("subjectaltname_bad_ip.pem", {})); |
| 215 | } |
| 216 |
| 217 | TEST(ParsedCertificateTest, SubjectAltNameDnsNotAscii) { |
| 218 | ASSERT_FALSE( |
| 219 | ParseCertificateFromFile("subjectaltname_dns_not_ascii.pem", {})); |
| 220 | } |
| 221 |
| 222 | TEST(ParsedCertificateTest, SubjectAltNameGeneralNamesEmptySequence) { |
| 223 | ASSERT_FALSE(ParseCertificateFromFile( |
| 224 | "subjectaltname_general_names_empty_sequence.pem", {})); |
| 225 | } |
| 226 |
| 227 | TEST(ParsedCertificateTest, SubjectAltNameTrailingData) { |
| 228 | ASSERT_FALSE( |
| 229 | ParseCertificateFromFile("subjectaltname_trailing_data.pem", {})); |
| 230 | } |
| 231 |
| 232 | TEST(ParsedCertificateTest, V1ExplicitVersion) { |
| 233 | ASSERT_FALSE(ParseCertificateFromFile("v1_explicit_version.pem", {})); |
| 234 | } |
| 235 |
| 236 | // Parses an Extensions that contains an extended key usages. |
| 237 | TEST(ParsedCertificateTest, ExtendedKeyUsage) { |
| 238 | std::shared_ptr<const ParsedCertificate> cert = |
| 239 | ParseCertificateFromFile("extended_key_usage.pem", {}); |
| 240 | ASSERT_TRUE(cert); |
| 241 |
| 242 | ASSERT_EQ(4u, cert->extensions().size()); |
| 243 |
| 244 | ParsedExtension extension; |
| 245 | ASSERT_TRUE(cert->GetExtension(der::Input(kExtKeyUsageOid), &extension)); |
| 246 |
| 247 | EXPECT_FALSE(extension.critical); |
| 248 | EXPECT_EQ(45u, extension.value.size()); |
| 249 |
| 250 | EXPECT_TRUE(cert->has_extended_key_usage()); |
| 251 | EXPECT_EQ(4u, cert->extended_key_usage().size()); |
| 252 | } |
| 253 |
| 254 | // Parses an Extensions that contains a key usage. |
| 255 | TEST(ParsedCertificateTest, KeyUsage) { |
| 256 | std::shared_ptr<const ParsedCertificate> cert = |
| 257 | ParseCertificateFromFile("key_usage.pem", {}); |
| 258 | ASSERT_TRUE(cert); |
| 259 |
| 260 | ASSERT_TRUE(cert->has_key_usage()); |
| 261 |
| 262 | EXPECT_EQ(5u, cert->key_usage().unused_bits()); |
| 263 | const uint8_t kExpectedBytes[] = {0xA0}; |
| 264 | EXPECT_EQ(der::Input(kExpectedBytes), cert->key_usage().bytes()); |
| 265 |
| 266 | EXPECT_TRUE(cert->key_usage().AssertsBit(0)); |
| 267 | EXPECT_FALSE(cert->key_usage().AssertsBit(1)); |
| 268 | EXPECT_TRUE(cert->key_usage().AssertsBit(2)); |
| 269 | } |
| 270 |
| 271 | // Parses an Extensions that contains a policies extension. |
| 272 | TEST(ParsedCertificateTest, Policies) { |
| 273 | std::shared_ptr<const ParsedCertificate> cert = |
| 274 | ParseCertificateFromFile("policies.pem", {}); |
| 275 | ASSERT_TRUE(cert); |
| 276 |
| 277 | ASSERT_EQ(4u, cert->extensions().size()); |
| 278 |
| 279 | ParsedExtension extension; |
| 280 | ASSERT_TRUE( |
| 281 | cert->GetExtension(der::Input(kCertificatePoliciesOid), &extension)); |
| 282 |
| 283 | EXPECT_FALSE(extension.critical); |
| 284 | EXPECT_EQ(95u, extension.value.size()); |
| 285 |
| 286 | EXPECT_TRUE(cert->has_policy_oids()); |
| 287 | EXPECT_EQ(2u, cert->policy_oids().size()); |
| 288 | } |
| 289 |
| 290 | // Parses an Extensions that contains a subjectaltname extension. |
| 291 | TEST(ParsedCertificateTest, SubjectAltName) { |
| 292 | std::shared_ptr<const ParsedCertificate> cert = |
| 293 | ParseCertificateFromFile("subject_alt_name.pem", {}); |
| 294 | ASSERT_TRUE(cert); |
| 295 |
| 296 | ASSERT_TRUE(cert->has_subject_alt_names()); |
| 297 | } |
| 298 |
| 299 | // Parses an Extensions that contains multiple extensions, sourced from a |
| 300 | // real-world certificate. |
| 301 | TEST(ParsedCertificateTest, ExtensionsReal) { |
| 302 | std::shared_ptr<const ParsedCertificate> cert = |
| 303 | ParseCertificateFromFile("extensions_real.pem", {}); |
| 304 | ASSERT_TRUE(cert); |
| 305 |
| 306 | ASSERT_EQ(7u, cert->extensions().size()); |
| 307 |
| 308 | EXPECT_TRUE(cert->has_key_usage()); |
| 309 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 310 | EXPECT_TRUE(cert->has_authority_info_access()); |
| 311 | EXPECT_TRUE(cert->has_policy_oids()); |
| 312 |
| 313 | ASSERT_TRUE(cert->authority_key_identifier()); |
| 314 | ASSERT_TRUE(cert->authority_key_identifier()->key_identifier); |
| 315 | EXPECT_FALSE(cert->authority_key_identifier()->authority_cert_issuer); |
| 316 | EXPECT_FALSE(cert->authority_key_identifier()->authority_cert_serial_number); |
| 317 | const uint8_t expected_authority_key_identifier[] = { |
| 318 | 0xc0, 0x7a, 0x98, 0x68, 0x8d, 0x89, 0xfb, 0xab, 0x05, 0x64, |
| 319 | 0x0c, 0x11, 0x7d, 0xaa, 0x7d, 0x65, 0xb8, 0xca, 0xcc, 0x4e, |
| 320 | }; |
| 321 | EXPECT_EQ(der::Input(expected_authority_key_identifier), |
| 322 | cert->authority_key_identifier()->key_identifier); |
| 323 |
| 324 | ASSERT_TRUE(cert->subject_key_identifier()); |
| 325 | const uint8_t expected_subject_key_identifier[] = { |
| 326 | 0x4a, 0xdd, 0x06, 0x16, 0x1b, 0xbc, 0xf6, 0x68, 0xb5, 0x76, |
| 327 | 0xf5, 0x81, 0xb6, 0xbb, 0x62, 0x1a, 0xba, 0x5a, 0x81, 0x2f}; |
| 328 | EXPECT_EQ(der::Input(expected_subject_key_identifier), |
| 329 | cert->subject_key_identifier()); |
| 330 |
| 331 | ParsedExtension extension; |
| 332 | ASSERT_TRUE( |
| 333 | cert->GetExtension(der::Input(kCertificatePoliciesOid), &extension)); |
| 334 |
| 335 | EXPECT_FALSE(extension.critical); |
| 336 | EXPECT_EQ(16u, extension.value.size()); |
| 337 |
| 338 | // TODO(eroman): Verify the other extensions' values. |
| 339 | } |
| 340 |
| 341 | // Parses a BasicConstraints with no CA or pathlen. |
| 342 | TEST(ParsedCertificateTest, BasicConstraintsNotCa) { |
| 343 | std::shared_ptr<const ParsedCertificate> cert = |
| 344 | ParseCertificateFromFile("basic_constraints_not_ca.pem", {}); |
| 345 | ASSERT_TRUE(cert); |
| 346 |
| 347 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 348 | EXPECT_FALSE(cert->basic_constraints().is_ca); |
| 349 | EXPECT_FALSE(cert->basic_constraints().has_path_len); |
| 350 | } |
| 351 |
| 352 | // Parses a BasicConstraints with CA but no pathlen. |
| 353 | TEST(ParsedCertificateTest, BasicConstraintsCaNoPath) { |
| 354 | std::shared_ptr<const ParsedCertificate> cert = |
| 355 | ParseCertificateFromFile("basic_constraints_ca_no_path.pem", {}); |
| 356 | ASSERT_TRUE(cert); |
| 357 |
| 358 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 359 | EXPECT_TRUE(cert->basic_constraints().is_ca); |
| 360 | EXPECT_FALSE(cert->basic_constraints().has_path_len); |
| 361 | } |
| 362 |
| 363 | // Parses a BasicConstraints with CA and pathlen of 9. |
| 364 | TEST(ParsedCertificateTest, BasicConstraintsCaPath9) { |
| 365 | std::shared_ptr<const ParsedCertificate> cert = |
| 366 | ParseCertificateFromFile("basic_constraints_ca_path_9.pem", {}); |
| 367 | ASSERT_TRUE(cert); |
| 368 |
| 369 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 370 | EXPECT_TRUE(cert->basic_constraints().is_ca); |
| 371 | EXPECT_TRUE(cert->basic_constraints().has_path_len); |
| 372 | EXPECT_EQ(9u, cert->basic_constraints().path_len); |
| 373 | } |
| 374 |
| 375 | // Parses a BasicConstraints with CA and pathlen of 255 (largest allowed size). |
| 376 | TEST(ParsedCertificateTest, BasicConstraintsPathlen255) { |
| 377 | std::shared_ptr<const ParsedCertificate> cert = |
| 378 | ParseCertificateFromFile("basic_constraints_pathlen_255.pem", {}); |
| 379 | ASSERT_TRUE(cert); |
| 380 |
| 381 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 382 | EXPECT_TRUE(cert->basic_constraints().is_ca); |
| 383 | EXPECT_TRUE(cert->basic_constraints().has_path_len); |
| 384 | EXPECT_EQ(255, cert->basic_constraints().path_len); |
| 385 | } |
| 386 |
| 387 | // Parses a BasicConstraints with CA and pathlen of 256 (too large). |
| 388 | TEST(ParsedCertificateTest, BasicConstraintsPathlen256) { |
| 389 | ASSERT_FALSE( |
| 390 | ParseCertificateFromFile("basic_constraints_pathlen_256.pem", {})); |
| 391 | } |
| 392 |
| 393 | // Parses a BasicConstraints with CA and a negative pathlen. |
| 394 | TEST(ParsedCertificateTest, BasicConstraintsNegativePath) { |
| 395 | ASSERT_FALSE( |
| 396 | ParseCertificateFromFile("basic_constraints_negative_path.pem", {})); |
| 397 | } |
| 398 |
| 399 | // Parses a BasicConstraints with CA and pathlen that is very large (and |
| 400 | // couldn't fit in a 64-bit integer). |
| 401 | TEST(ParsedCertificateTest, BasicConstraintsPathTooLarge) { |
| 402 | ASSERT_FALSE( |
| 403 | ParseCertificateFromFile("basic_constraints_path_too_large.pem", {})); |
| 404 | } |
| 405 |
| 406 | // Parses a BasicConstraints with CA explicitly set to false. This violates |
| 407 | // DER-encoding rules, however is commonly used, so it is accepted. |
| 408 | TEST(ParsedCertificateTest, BasicConstraintsCaFalse) { |
| 409 | std::shared_ptr<const ParsedCertificate> cert = |
| 410 | ParseCertificateFromFile("basic_constraints_ca_false.pem", {}); |
| 411 | ASSERT_TRUE(cert); |
| 412 |
| 413 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 414 | EXPECT_FALSE(cert->basic_constraints().is_ca); |
| 415 | EXPECT_FALSE(cert->basic_constraints().has_path_len); |
| 416 | } |
| 417 |
| 418 | // Parses a BasicConstraints with CA set to true and an unexpected NULL at |
| 419 | // the end. |
| 420 | TEST(ParsedCertificateTest, BasicConstraintsUnconsumedData) { |
| 421 | ASSERT_FALSE( |
| 422 | ParseCertificateFromFile("basic_constraints_unconsumed_data.pem", {})); |
| 423 | } |
| 424 |
| 425 | // Parses a BasicConstraints with CA omitted (false), but with a pathlen of 1. |
| 426 | // This is valid DER for the ASN.1, however is not valid when interpreting the |
| 427 | // BasicConstraints at a higher level. |
| 428 | TEST(ParsedCertificateTest, BasicConstraintsPathLenButNotCa) { |
| 429 | std::shared_ptr<const ParsedCertificate> cert = |
| 430 | ParseCertificateFromFile("basic_constraints_pathlen_not_ca.pem", {}); |
| 431 | ASSERT_TRUE(cert); |
| 432 |
| 433 | EXPECT_TRUE(cert->has_basic_constraints()); |
| 434 | EXPECT_FALSE(cert->basic_constraints().is_ca); |
| 435 | EXPECT_TRUE(cert->basic_constraints().has_path_len); |
| 436 | EXPECT_EQ(1u, cert->basic_constraints().path_len); |
| 437 | } |
| 438 |
| 439 | // Tests parsing a certificate that contains a policyConstraints |
| 440 | // extension having requireExplicitPolicy:3. |
| 441 | TEST(ParsedCertificateTest, PolicyConstraintsRequire) { |
| 442 | std::shared_ptr<const ParsedCertificate> cert = |
| 443 | ParseCertificateFromFile("policy_constraints_require.pem", {}); |
| 444 | ASSERT_TRUE(cert); |
| 445 |
| 446 | EXPECT_TRUE(cert->has_policy_constraints()); |
| 447 | EXPECT_TRUE(cert->policy_constraints().require_explicit_policy.has_value()); |
| 448 | EXPECT_EQ(3, cert->policy_constraints().require_explicit_policy.value()); |
| 449 | EXPECT_FALSE(cert->policy_constraints().inhibit_policy_mapping.has_value()); |
| 450 | } |
| 451 |
| 452 | // Tests parsing a certificate that contains a policyConstraints |
| 453 | // extension having inhibitPolicyMapping:1. |
| 454 | TEST(ParsedCertificateTest, PolicyConstraintsInhibit) { |
| 455 | std::shared_ptr<const ParsedCertificate> cert = |
| 456 | ParseCertificateFromFile("policy_constraints_inhibit.pem", {}); |
| 457 | ASSERT_TRUE(cert); |
| 458 |
| 459 | EXPECT_TRUE(cert->has_policy_constraints()); |
| 460 | EXPECT_FALSE(cert->policy_constraints().require_explicit_policy.has_value()); |
| 461 | EXPECT_TRUE(cert->policy_constraints().inhibit_policy_mapping.has_value()); |
| 462 | EXPECT_EQ(1, cert->policy_constraints().inhibit_policy_mapping.value()); |
| 463 | } |
| 464 |
| 465 | // Tests parsing a certificate that contains a policyConstraints |
| 466 | // extension having requireExplicitPolicy:5,inhibitPolicyMapping:2. |
| 467 | TEST(ParsedCertificateTest, PolicyConstraintsInhibitRequire) { |
| 468 | std::shared_ptr<const ParsedCertificate> cert = |
| 469 | ParseCertificateFromFile("policy_constraints_inhibit_require.pem", {}); |
| 470 | ASSERT_TRUE(cert); |
| 471 |
| 472 | EXPECT_TRUE(cert->has_policy_constraints()); |
| 473 | EXPECT_TRUE(cert->policy_constraints().require_explicit_policy.has_value()); |
| 474 | EXPECT_EQ(5, cert->policy_constraints().require_explicit_policy.value()); |
| 475 | EXPECT_TRUE(cert->policy_constraints().inhibit_policy_mapping.has_value()); |
| 476 | EXPECT_EQ(2, cert->policy_constraints().inhibit_policy_mapping.value()); |
| 477 | } |
| 478 |
| 479 | // Tests parsing a certificate that has a policyConstraints |
| 480 | // extension with an empty sequence. |
| 481 | TEST(ParsedCertificateTest, PolicyConstraintsEmpty) { |
| 482 | std::shared_ptr<const ParsedCertificate> cert = |
| 483 | ParseCertificateFromFile("policy_constraints_empty.pem", {}); |
| 484 | ASSERT_FALSE(cert); |
| 485 | } |
| 486 |
| 487 | // Tests a certificate with a serial number with a leading 0 padding byte in |
| 488 | // the encoding since it is not negative. |
| 489 | TEST(ParsedCertificateTest, SerialNumberZeroPadded) { |
| 490 | std::shared_ptr<const ParsedCertificate> cert = |
| 491 | ParseCertificateFromFile("serial_zero_padded.pem", {}); |
| 492 | ASSERT_TRUE(cert); |
| 493 |
| 494 | static const uint8_t expected_serial[3] = {0x00, 0x80, 0x01}; |
| 495 | EXPECT_EQ(der::Input(expected_serial), cert->tbs().serial_number); |
| 496 | } |
| 497 |
| 498 | // Tests a serial number where the MSB is >= 0x80, causing the encoded |
| 499 | // length to be 21 bytes long. This is an error, as RFC 5280 specifies a |
| 500 | // maximum of 20 bytes. |
| 501 | TEST(ParsedCertificateTest, SerialNumberZeroPadded21BytesLong) { |
| 502 | std::shared_ptr<const ParsedCertificate> cert = |
| 503 | ParseCertificateFromFile("serial_zero_padded_21_bytes.pem", {}); |
| 504 | ASSERT_FALSE(cert); |
| 505 |
| 506 | // Try again with allow_invalid_serial_numbers=true. Parsing should succeed. |
| 507 | ParseCertificateOptions options; |
| 508 | options.allow_invalid_serial_numbers = true; |
| 509 | cert = ParseCertificateFromFile("serial_zero_padded_21_bytes.pem", options); |
| 510 | ASSERT_TRUE(cert); |
| 511 |
| 512 | static const uint8_t expected_serial[21] = { |
| 513 | 0x00, 0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, |
| 514 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13}; |
| 515 | EXPECT_EQ(der::Input(expected_serial), cert->tbs().serial_number); |
| 516 | } |
| 517 |
| 518 | // Tests a serial number which is negative. CAs are not supposed to include |
| 519 | // negative serial numbers, however RFC 5280 expects consumers to deal with it |
| 520 | // anyway. |
| 521 | TEST(ParsedCertificateTest, SerialNumberNegative) { |
| 522 | std::shared_ptr<const ParsedCertificate> cert = |
| 523 | ParseCertificateFromFile("serial_negative.pem", {}); |
| 524 | ASSERT_TRUE(cert); |
| 525 |
| 526 | static const uint8_t expected_serial[2] = {0x80, 0x01}; |
| 527 | EXPECT_EQ(der::Input(expected_serial), cert->tbs().serial_number); |
| 528 | } |
| 529 |
| 530 | // Tests a serial number which is very long. RFC 5280 specifies a maximum of 20 |
| 531 | // bytes. |
| 532 | TEST(ParsedCertificateTest, SerialNumber37BytesLong) { |
| 533 | std::shared_ptr<const ParsedCertificate> cert = |
| 534 | ParseCertificateFromFile("serial_37_bytes.pem", {}); |
| 535 | ASSERT_FALSE(cert); |
| 536 |
| 537 | // Try again with allow_invalid_serial_numbers=true. Parsing should succeed. |
| 538 | ParseCertificateOptions options; |
| 539 | options.allow_invalid_serial_numbers = true; |
| 540 | cert = ParseCertificateFromFile("serial_37_bytes.pem", options); |
| 541 | ASSERT_TRUE(cert); |
| 542 |
| 543 | static const uint8_t expected_serial[37] = { |
| 544 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, |
| 545 | 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, |
| 546 | 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, |
| 547 | 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25}; |
| 548 | EXPECT_EQ(der::Input(expected_serial), cert->tbs().serial_number); |
| 549 | } |
| 550 |
| 551 | // Tests a serial number which is zero. RFC 5280 says they should be positive, |
| 552 | // however also recommends supporting non-positive ones, so parsing here |
| 553 | // is expected to succeed. |
| 554 | TEST(ParsedCertificateTest, SerialNumberZero) { |
| 555 | std::shared_ptr<const ParsedCertificate> cert = |
| 556 | ParseCertificateFromFile("serial_zero.pem", {}); |
| 557 | ASSERT_TRUE(cert); |
| 558 |
| 559 | static const uint8_t expected_serial[] = {0x00}; |
| 560 | EXPECT_EQ(der::Input(expected_serial), cert->tbs().serial_number); |
| 561 | } |
| 562 |
| 563 | // Tests a serial number which not a number (NULL). |
| 564 | TEST(ParsedCertificateTest, SerialNotNumber) { |
| 565 | std::shared_ptr<const ParsedCertificate> cert = |
| 566 | ParseCertificateFromFile("serial_not_number.pem", {}); |
| 567 | ASSERT_FALSE(cert); |
| 568 | } |
| 569 |
| 570 | // Tests a serial number which uses a non-minimal INTEGER encoding |
| 571 | TEST(ParsedCertificateTest, SerialNotMinimal) { |
| 572 | std::shared_ptr<const ParsedCertificate> cert = |
| 573 | ParseCertificateFromFile("serial_not_minimal.pem", {}); |
| 574 | ASSERT_FALSE(cert); |
| 575 | } |
| 576 |
| 577 | // Tests parsing a certificate that has an inhibitAnyPolicy extension. |
| 578 | TEST(ParsedCertificateTest, InhibitAnyPolicy) { |
| 579 | std::shared_ptr<const ParsedCertificate> cert = |
| 580 | ParseCertificateFromFile("inhibit_any_policy.pem", {}); |
| 581 | ASSERT_TRUE(cert); |
| 582 |
| 583 | ParsedExtension extension; |
| 584 | ASSERT_TRUE(cert->GetExtension(der::Input(kInhibitAnyPolicyOid), &extension)); |
| 585 |
| 586 | std::optional<uint8_t> skip_count = ParseInhibitAnyPolicy(extension.value); |
| 587 | ASSERT_TRUE(skip_count.has_value()); |
| 588 | EXPECT_EQ(3, skip_count.value()); |
| 589 | } |
| 590 |
| 591 | // Tests a subjectKeyIdentifier that is not an OCTET_STRING. |
| 592 | TEST(ParsedCertificateTest, SubjectKeyIdentifierNotOctetString) { |
| 593 | std::shared_ptr<const ParsedCertificate> cert = ParseCertificateFromFile( |
| 594 | "subject_key_identifier_not_octet_string.pem", {}); |
| 595 | ASSERT_FALSE(cert); |
| 596 | } |
| 597 |
| 598 | // Tests an authorityKeyIdentifier that is not a SEQUENCE. |
| 599 | TEST(ParsedCertificateTest, AuthourityKeyIdentifierNotSequence) { |
| 600 | std::shared_ptr<const ParsedCertificate> cert = |
| 601 | ParseCertificateFromFile("authority_key_identifier_not_sequence.pem", {}); |
| 602 | ASSERT_FALSE(cert); |
| 603 | } |
| 604 |
| 605 | } // namespace |
| 606 |
| 607 | BSSL_NAMESPACE_END |
| 608 |