| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #include "crypto/rsa_private_key.h" | 12 #include "crypto/rsa_private_key.h" |
| 13 #include "net/base/asn1_util.h" | 13 #include "net/base/asn1_util.h" |
| 14 #include "net/base/cert_status_flags.h" | 14 #include "net/base/cert_status_flags.h" |
| 15 #include "net/base/cert_test_util.h" | 15 #include "net/base/cert_test_util.h" |
| 16 #include "net/base/cert_verify_result.h" | 16 #include "net/base/cert_verify_result.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/test_certificate_data.h" | 18 #include "net/base/test_certificate_data.h" |
| 19 #include "net/base/test_root_certs.h" | 19 #include "net/base/test_root_certs.h" |
| 20 #include "net/base/x509_certificate.h" | 20 #include "net/base/x509_certificate.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 #if defined(USE_NSS) | |
| 24 #include <cert.h> | |
| 25 #include <secoid.h> | |
| 26 #endif | |
| 27 | |
| 28 // Unit tests aren't allowed to access external resources. Unfortunately, to | 23 // Unit tests aren't allowed to access external resources. Unfortunately, to |
| 29 // properly verify the EV-ness of a cert, we need to check for its revocation | 24 // properly verify the EV-ness of a cert, we need to check for its revocation |
| 30 // through online servers. If you're manually running unit tests, feel free to | 25 // through online servers. If you're manually running unit tests, feel free to |
| 31 // turn this on to test EV certs. But leave it turned off for the automated | 26 // turn this on to test EV certs. But leave it turned off for the automated |
| 32 // testing. | 27 // testing. |
| 33 #define ALLOW_EXTERNAL_ACCESS 0 | 28 #define ALLOW_EXTERNAL_ACCESS 0 |
| 34 | 29 |
| 35 #if ALLOW_EXTERNAL_ACCESS && defined(OS_WIN) | 30 #if ALLOW_EXTERNAL_ACCESS && defined(OS_WIN) |
| 36 #define TEST_EV 1 // Test CERT_STATUS_IS_EV | 31 #define TEST_EV 1 // Test CERT_STATUS_IS_EV |
| 37 #endif | 32 #endif |
| (...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 scoped_refptr<X509Certificate> cert = | 1125 scoped_refptr<X509Certificate> cert = |
| 1131 X509Certificate::CreateSelfSigned( | 1126 X509Certificate::CreateSelfSigned( |
| 1132 private_key.get(), "CN=subject", 0, base::TimeDelta::FromDays(1)); | 1127 private_key.get(), "CN=subject", 0, base::TimeDelta::FromDays(1)); |
| 1133 | 1128 |
| 1134 std::string der_cert; | 1129 std::string der_cert; |
| 1135 EXPECT_TRUE(cert->GetDEREncoded(&der_cert)); | 1130 EXPECT_TRUE(cert->GetDEREncoded(&der_cert)); |
| 1136 EXPECT_FALSE(der_cert.empty()); | 1131 EXPECT_FALSE(der_cert.empty()); |
| 1137 } | 1132 } |
| 1138 #endif | 1133 #endif |
| 1139 | 1134 |
| 1140 #if defined(USE_NSS) | |
| 1141 // This test creates an origin-bound cert from a private key and | |
| 1142 // then verifies the content of the certificate. | |
| 1143 TEST(X509CertificateTest, CreateOriginBound) { | |
| 1144 // Origin Bound Cert OID. | |
| 1145 static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6"; | |
| 1146 | |
| 1147 // Create a sample ASCII weborigin. | |
| 1148 std::string origin = "http://weborigin.com:443"; | |
| 1149 | |
| 1150 // Create object neccissary for extension lookup call. | |
| 1151 SECItem extension_object = { | |
| 1152 siAsciiString, | |
| 1153 (unsigned char*)origin.data(), | |
| 1154 origin.size() | |
| 1155 }; | |
| 1156 | |
| 1157 scoped_ptr<crypto::RSAPrivateKey> private_key( | |
| 1158 crypto::RSAPrivateKey::Create(1024)); | |
| 1159 scoped_refptr<X509Certificate> cert = | |
| 1160 X509Certificate::CreateOriginBound(private_key.get(), | |
| 1161 origin, 1, | |
| 1162 base::TimeDelta::FromDays(1)); | |
| 1163 | |
| 1164 EXPECT_EQ("anonymous.invalid", cert->subject().GetDisplayName()); | |
| 1165 EXPECT_FALSE(cert->HasExpired()); | |
| 1166 | |
| 1167 // IA5Encode and arena allocate SECItem. | |
| 1168 PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | |
| 1169 SECItem* expected = SEC_ASN1EncodeItem(arena, | |
| 1170 NULL, | |
| 1171 &extension_object, | |
| 1172 SEC_ASN1_GET(SEC_IA5StringTemplate)); | |
| 1173 | |
| 1174 ASSERT_NE(static_cast<SECItem*>(NULL), expected); | |
| 1175 | |
| 1176 // Create OID SECItem. | |
| 1177 SECItem ob_cert_oid = { siDEROID, NULL, 0 }; | |
| 1178 SECStatus ok = SEC_StringToOID(arena, &ob_cert_oid, | |
| 1179 oid_string, NULL); | |
| 1180 | |
| 1181 ASSERT_EQ(SECSuccess, ok); | |
| 1182 | |
| 1183 SECOidTag ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid); | |
| 1184 | |
| 1185 ASSERT_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag); | |
| 1186 | |
| 1187 // Lookup Origin Bound Cert extension in generated cert. | |
| 1188 SECItem actual = { siBuffer, NULL, 0 }; | |
| 1189 ok = CERT_FindCertExtension(cert->os_cert_handle(), | |
| 1190 ob_cert_oid_tag, | |
| 1191 &actual); | |
| 1192 ASSERT_EQ(SECSuccess, ok); | |
| 1193 | |
| 1194 // Compare expected and actual extension values. | |
| 1195 PRBool result = SECITEM_ItemsAreEqual(expected, &actual); | |
| 1196 ASSERT_TRUE(result); | |
| 1197 | |
| 1198 // Do Cleanup. | |
| 1199 SECITEM_FreeItem(&actual, PR_FALSE); | |
| 1200 PORT_FreeArena(arena, PR_FALSE); | |
| 1201 } | |
| 1202 #else // defined(USE_NSS) | |
| 1203 // On other platforms, X509Certificate::CreateOriginBound() is not implemented | |
| 1204 // and should return NULL. This unit test ensures that a stub implementation | |
| 1205 // is present. | |
| 1206 TEST(X509CertificateTest, CreateOriginBoundNotImplemented) { | |
| 1207 std::string origin = "http://weborigin.com:443"; | |
| 1208 scoped_ptr<crypto::RSAPrivateKey> private_key( | |
| 1209 crypto::RSAPrivateKey::Create(1024)); | |
| 1210 scoped_refptr<X509Certificate> cert = | |
| 1211 X509Certificate::CreateOriginBound(private_key.get(), | |
| 1212 origin, 2, | |
| 1213 base::TimeDelta::FromDays(1)); | |
| 1214 EXPECT_FALSE(cert); | |
| 1215 } | |
| 1216 #endif // defined(USE_NSS) | |
| 1217 | |
| 1218 class X509CertificateParseTest | 1135 class X509CertificateParseTest |
| 1219 : public testing::TestWithParam<CertificateFormatTestData> { | 1136 : public testing::TestWithParam<CertificateFormatTestData> { |
| 1220 public: | 1137 public: |
| 1221 virtual ~X509CertificateParseTest() {} | 1138 virtual ~X509CertificateParseTest() {} |
| 1222 virtual void SetUp() { | 1139 virtual void SetUp() { |
| 1223 test_data_ = GetParam(); | 1140 test_data_ = GetParam(); |
| 1224 } | 1141 } |
| 1225 virtual void TearDown() {} | 1142 virtual void TearDown() {} |
| 1226 | 1143 |
| 1227 protected: | 1144 protected: |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1456 } | 1373 } |
| 1457 | 1374 |
| 1458 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname( | 1375 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname( |
| 1459 test_data.hostname, common_name, dns_names, ip_addressses)); | 1376 test_data.hostname, common_name, dns_names, ip_addressses)); |
| 1460 } | 1377 } |
| 1461 | 1378 |
| 1462 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest, | 1379 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest, |
| 1463 testing::ValuesIn(kNameVerifyTestData)); | 1380 testing::ValuesIn(kNameVerifyTestData)); |
| 1464 | 1381 |
| 1465 } // namespace net | 1382 } // namespace net |
| OLD | NEW |