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