OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/cert/x509_util_ios_and_mac.h" |
| 6 |
| 7 #include "net/cert/x509_certificate.h" |
| 8 #include "net/test/cert_test_util.h" |
| 9 #include "net/test/test_data_directory.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 #if defined(OS_IOS) |
| 13 #include "net/cert/x509_util_ios.h" |
| 14 #else |
| 15 #include "net/cert/x509_util_mac.h" |
| 16 #endif |
| 17 |
| 18 namespace net { |
| 19 |
| 20 namespace x509_util { |
| 21 |
| 22 namespace { |
| 23 |
| 24 std::string BytesForSecCert(SecCertificateRef sec_cert) { |
| 25 std::string result; |
| 26 base::ScopedCFTypeRef<CFDataRef> der_data(SecCertificateCopyData(sec_cert)); |
| 27 if (!der_data) { |
| 28 ADD_FAILURE(); |
| 29 return result; |
| 30 } |
| 31 result.assign(reinterpret_cast<const char*>(CFDataGetBytePtr(der_data)), |
| 32 CFDataGetLength(der_data)); |
| 33 return result; |
| 34 } |
| 35 |
| 36 std::string BytesForSecCert(const void* sec_cert) { |
| 37 return BytesForSecCert( |
| 38 reinterpret_cast<SecCertificateRef>(const_cast<void*>(sec_cert))); |
| 39 } |
| 40 |
| 41 std::string BytesForX509CertHandle(X509Certificate::OSCertHandle handle) { |
| 42 std::string result; |
| 43 if (!X509Certificate::GetDEREncoded(handle, &result)) |
| 44 ADD_FAILURE(); |
| 45 return result; |
| 46 } |
| 47 |
| 48 std::string BytesForX509Cert(X509Certificate* cert) { |
| 49 return BytesForX509CertHandle(cert->os_cert_handle()); |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 54 TEST(X509UtilTest, CreateSecCertificateArrayForX509Certificate) { |
| 55 scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| 56 GetTestCertsDirectory(), "multi-root-chain1.pem", |
| 57 X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 58 ASSERT_TRUE(cert); |
| 59 EXPECT_EQ(3U, cert->GetIntermediateCertificates().size()); |
| 60 |
| 61 base::ScopedCFTypeRef<CFMutableArrayRef> sec_certs( |
| 62 CreateSecCertificateArrayForX509Certificate(cert.get())); |
| 63 ASSERT_TRUE(sec_certs); |
| 64 ASSERT_EQ(4, CFArrayGetCount(sec_certs.get())); |
| 65 for (int i = 0; i < 4; ++i) |
| 66 ASSERT_TRUE(CFArrayGetValueAtIndex(sec_certs.get(), i)); |
| 67 |
| 68 EXPECT_EQ(BytesForX509Cert(cert.get()), |
| 69 BytesForSecCert(CFArrayGetValueAtIndex(sec_certs.get(), 0))); |
| 70 EXPECT_EQ(BytesForX509CertHandle(cert->GetIntermediateCertificates()[0]), |
| 71 BytesForSecCert(CFArrayGetValueAtIndex(sec_certs.get(), 1))); |
| 72 EXPECT_EQ(BytesForX509CertHandle(cert->GetIntermediateCertificates()[1]), |
| 73 BytesForSecCert(CFArrayGetValueAtIndex(sec_certs.get(), 2))); |
| 74 EXPECT_EQ(BytesForX509CertHandle(cert->GetIntermediateCertificates()[2]), |
| 75 BytesForSecCert(CFArrayGetValueAtIndex(sec_certs.get(), 3))); |
| 76 } |
| 77 |
| 78 TEST(X509UtilTest, |
| 79 CreateSecCertificateFromBytesAndCreateX509CertificateFromSecCertificate) { |
| 80 CertificateList certs = CreateCertificateListFromFile( |
| 81 GetTestCertsDirectory(), "multi-root-chain1.pem", |
| 82 X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 83 ASSERT_EQ(4u, certs.size()); |
| 84 |
| 85 std::string bytes_cert0 = BytesForX509CertHandle(certs[0]->os_cert_handle()); |
| 86 std::string bytes_cert1 = BytesForX509CertHandle(certs[1]->os_cert_handle()); |
| 87 std::string bytes_cert2 = BytesForX509CertHandle(certs[2]->os_cert_handle()); |
| 88 std::string bytes_cert3 = BytesForX509CertHandle(certs[3]->os_cert_handle()); |
| 89 |
| 90 base::ScopedCFTypeRef<SecCertificateRef> sec_cert0( |
| 91 CreateSecCertificateFromBytes( |
| 92 reinterpret_cast<const uint8_t*>(bytes_cert0.data()), |
| 93 bytes_cert0.length())); |
| 94 ASSERT_TRUE(sec_cert0); |
| 95 EXPECT_EQ(bytes_cert0, BytesForSecCert(sec_cert0)); |
| 96 |
| 97 base::ScopedCFTypeRef<SecCertificateRef> sec_cert1( |
| 98 CreateSecCertificateFromBytes( |
| 99 reinterpret_cast<const uint8_t*>(bytes_cert1.data()), |
| 100 bytes_cert1.length())); |
| 101 ASSERT_TRUE(sec_cert1); |
| 102 EXPECT_EQ(bytes_cert1, BytesForSecCert(sec_cert1)); |
| 103 |
| 104 base::ScopedCFTypeRef<SecCertificateRef> sec_cert2( |
| 105 CreateSecCertificateFromX509Certificate(certs[2].get())); |
| 106 ASSERT_TRUE(sec_cert2); |
| 107 EXPECT_EQ(bytes_cert2, BytesForSecCert(sec_cert2)); |
| 108 |
| 109 base::ScopedCFTypeRef<SecCertificateRef> sec_cert3( |
| 110 CreateSecCertificateFromX509Certificate(certs[3].get())); |
| 111 ASSERT_TRUE(sec_cert3); |
| 112 EXPECT_EQ(bytes_cert3, BytesForSecCert(sec_cert3)); |
| 113 |
| 114 scoped_refptr<X509Certificate> x509_cert_no_intermediates = |
| 115 CreateX509CertificateFromSecCertificate(sec_cert0.get(), {}); |
| 116 ASSERT_TRUE(x509_cert_no_intermediates); |
| 117 EXPECT_EQ(0U, |
| 118 x509_cert_no_intermediates->GetIntermediateCertificates().size()); |
| 119 EXPECT_EQ(bytes_cert0, BytesForX509CertHandle( |
| 120 x509_cert_no_intermediates->os_cert_handle())); |
| 121 |
| 122 scoped_refptr<X509Certificate> x509_cert_one_intermediate = |
| 123 CreateX509CertificateFromSecCertificate(sec_cert0.get(), |
| 124 {sec_cert1.get()}); |
| 125 ASSERT_TRUE(x509_cert_one_intermediate); |
| 126 EXPECT_EQ(bytes_cert0, BytesForX509CertHandle( |
| 127 x509_cert_one_intermediate->os_cert_handle())); |
| 128 ASSERT_EQ(1U, |
| 129 x509_cert_one_intermediate->GetIntermediateCertificates().size()); |
| 130 EXPECT_EQ(bytes_cert1, |
| 131 BytesForX509CertHandle( |
| 132 x509_cert_one_intermediate->GetIntermediateCertificates()[0])); |
| 133 |
| 134 scoped_refptr<X509Certificate> x509_cert_two_intermediates = |
| 135 CreateX509CertificateFromSecCertificate( |
| 136 sec_cert0.get(), {sec_cert1.get(), sec_cert2.get()}); |
| 137 ASSERT_TRUE(x509_cert_two_intermediates); |
| 138 EXPECT_EQ(bytes_cert0, BytesForX509CertHandle( |
| 139 x509_cert_two_intermediates->os_cert_handle())); |
| 140 ASSERT_EQ(2U, |
| 141 x509_cert_two_intermediates->GetIntermediateCertificates().size()); |
| 142 EXPECT_EQ(bytes_cert1, |
| 143 BytesForX509CertHandle( |
| 144 x509_cert_two_intermediates->GetIntermediateCertificates()[0])); |
| 145 EXPECT_EQ(bytes_cert2, |
| 146 BytesForX509CertHandle( |
| 147 x509_cert_two_intermediates->GetIntermediateCertificates()[1])); |
| 148 } |
| 149 |
| 150 } // namespace x509_util |
| 151 |
| 152 } // namespace net |
OLD | NEW |