Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: net/base/x509_certificate_unittest.cc

Issue 7384002: Added CreateOriginBound method to x509_certificate.h. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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>
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"
(...skipping 994 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 private_key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); 1012 private_key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
1010 ASSERT_TRUE(private_key.get()); 1013 ASSERT_TRUE(private_key.get());
1011 1014
1012 cert = X509Certificate::CreateSelfSigned( 1015 cert = X509Certificate::CreateSelfSigned(
1013 private_key.get(), "CN=subject", 1, base::TimeDelta::FromDays(1)); 1016 private_key.get(), "CN=subject", 1, base::TimeDelta::FromDays(1));
1014 1017
1015 EXPECT_EQ("subject", cert->subject().GetDisplayName()); 1018 EXPECT_EQ("subject", cert->subject().GetDisplayName());
1016 EXPECT_FALSE(cert->HasExpired()); 1019 EXPECT_FALSE(cert->HasExpired());
1017 } 1020 }
1018 1021
1022 // This test creates an origin-bound cert from a private key and
1023 // then verifies the content of the certificate.
1024 TEST(X509CertificateTest, CreateOriginBound) {
1025 SECItem ob_cert_oid = {siDEROID, NULL, 0};
wtc 2011/08/04 00:37:53 Add a space after '{' and before '}'.
mdietz 2011/08/18 00:02:45 Done.
1026 SECItem* expected;
1027 SECItem actual = {siBuffer, NULL, 0};
1028 SECOidTag ob_cert_oid_tag;
1029 SECStatus ok;
1030 PRBool res;
wtc 2011/08/04 00:37:53 res => result or equal The Style Guide recomm
mdietz 2011/08/18 00:02:45 Done.
1031
1032 // Origin Bound Cert OID
1033 std::string oid_string = "1.3.6.1.4.1.11129.2.1.6";
wtc 2011/08/04 00:37:53 Use a C string because this is passed to NSS: st
mdietz 2011/08/18 00:02:45 Done.
1034
1035 // Sample ASCII weborigin
1036 std::string origin = "http://weborigin.com:443";
1037
1038 // Create object neccissary for extension lookup call
1039 SECItem tmp = {siAsciiString,
wtc 2011/08/04 00:37:53 Use a more descriptive name than "tmp".
mdietz 2011/08/18 00:02:45 Done.
1040 (unsigned char*)origin.c_str(),
1041 origin.size()+1};
1042
1043 scoped_ptr<crypto::RSAPrivateKey> private_key(
1044 crypto::RSAPrivateKey::Create(1024));
1045 scoped_refptr<X509Certificate> cert =
1046 X509Certificate::CreateOriginBound(private_key.get(),
1047 "CN=subject",
1048 origin, 1,
1049 base::TimeDelta::FromDays(1));
1050
1051 EXPECT_EQ("subject", cert->subject().GetDisplayName());
1052 EXPECT_FALSE(cert->HasExpired());
1053
1054 // IA5Encode and arena allocate SECItem
1055 expected = SEC_ASN1EncodeItem(cert->os_cert_handle()->arena, NULL, &tmp,
1056 SEC_ASN1_GET(SEC_IA5StringTemplate));
1057
1058 ASSERT_NE(expected, static_cast<SECItem*>(NULL));
wtc 2011/08/04 00:37:53 In these ASSERT_xx and EXPECT_xx macros, the expec
mdietz 2011/08/18 00:02:45 Done.
1059
1060 // Create OID SECItem
1061 ok = SEC_StringToOID(cert->os_cert_handle()->arena, &ob_cert_oid,
1062 oid_string.c_str(), NULL);
wtc 2011/08/04 00:37:53 You should create an arena: PLArenaPool* arena =
mdietz 2011/08/18 00:02:45 Done.
1063 ASSERT_EQ(ok, SECSuccess);
1064
1065 ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid);
1066
1067 ASSERT_NE(ob_cert_oid_tag, SEC_OID_UNKNOWN);
1068
1069 // Lookup Origin Bound Cert extension in generated cert
1070 ok = CERT_FindCertExtension(cert->os_cert_handle(),
1071 ob_cert_oid_tag,
1072 &actual);
wtc 2011/08/04 00:37:53 Align these function arguments.
mdietz 2011/08/18 00:02:45 Done.
1073 ASSERT_EQ(ok, SECSuccess);
1074
1075 // Compare expected and actual extension values
1076 res = SECITEM_ItemsAreEqual(expected, &actual);
1077 ASSERT_EQ(res, PR_TRUE);
wtc 2011/08/04 00:37:53 You can use ASSERT_TRUE(res).
mdietz 2011/08/18 00:02:45 Done.
1078
1079 const uint8 private_key_info[] = {
wtc 2011/08/04 00:37:53 Add 'static'.
mdietz 2011/08/18 00:02:45 Done.
1080 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30,
1081 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
1082 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
1083 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01,
1084 0x00, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b,
1085 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61,
1086 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08,
1087 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64,
1088 0x85, 0x7b, 0x0c, 0x04, 0x13, 0x3f, 0x8d, 0xf4,
1089 0xbc, 0x38, 0x0d, 0x49, 0xfe, 0x6b, 0xc4, 0x5a,
1090 0xb0, 0x40, 0x53, 0x3a, 0xd7, 0x66, 0x09, 0x0f,
1091 0x9e, 0x36, 0x74, 0x30, 0xda, 0x8a, 0x31, 0x4f,
1092 0x1f, 0x14, 0x50, 0xd7, 0xc7, 0x20, 0x94, 0x17,
1093 0xde, 0x4e, 0xb9, 0x57, 0x5e, 0x7e, 0x0a, 0xe5,
1094 0xb2, 0x65, 0x7a, 0x89, 0x4e, 0xb6, 0x47, 0xff,
1095 0x1c, 0xbd, 0xb7, 0x38, 0x13, 0xaf, 0x47, 0x85,
1096 0x84, 0x32, 0x33, 0xf3, 0x17, 0x49, 0xbf, 0xe9,
1097 0x96, 0xd0, 0xd6, 0x14, 0x6f, 0x13, 0x8d, 0xc5,
1098 0xfc, 0x2c, 0x72, 0xba, 0xac, 0xea, 0x7e, 0x18,
1099 0x53, 0x56, 0xa6, 0x83, 0xa2, 0xce, 0x93, 0x93,
1100 0xe7, 0x1f, 0x0f, 0xe6, 0x0f, 0x02, 0x03, 0x01,
1101 0x00, 0x01, 0x02, 0x81, 0x80, 0x03, 0x61, 0x89,
1102 0x37, 0xcb, 0xf2, 0x98, 0xa0, 0xce, 0xb4, 0xcb,
1103 0x16, 0x13, 0xf0, 0xe6, 0xaf, 0x5c, 0xc5, 0xa7,
1104 0x69, 0x71, 0xca, 0xba, 0x8d, 0xe0, 0x4d, 0xdd,
1105 0xed, 0xb8, 0x48, 0x8b, 0x16, 0x93, 0x36, 0x95,
1106 0xc2, 0x91, 0x40, 0x65, 0x17, 0xbd, 0x7f, 0xd6,
1107 0xad, 0x9e, 0x30, 0x28, 0x46, 0xe4, 0x3e, 0xcc,
1108 0x43, 0x78, 0xf9, 0xfe, 0x1f, 0x33, 0x23, 0x1e,
1109 0x31, 0x12, 0x9d, 0x3c, 0xa7, 0x08, 0x82, 0x7b,
1110 0x7d, 0x25, 0x4e, 0x5e, 0x19, 0xa8, 0x9b, 0xed,
1111 0x86, 0xb2, 0xcb, 0x3c, 0xfe, 0x4e, 0xa1, 0xfa,
1112 0x62, 0x87, 0x3a, 0x17, 0xf7, 0x60, 0xec, 0x38,
1113 0x29, 0xe8, 0x4f, 0x34, 0x9f, 0x76, 0x9d, 0xee,
1114 0xa3, 0xf6, 0x85, 0x6b, 0x84, 0x43, 0xc9, 0x1e,
1115 0x01, 0xff, 0xfd, 0xd0, 0x29, 0x4c, 0xfa, 0x8e,
1116 0x57, 0x0c, 0xc0, 0x71, 0xa5, 0xbb, 0x88, 0x46,
1117 0x29, 0x5c, 0xc0, 0x4f, 0x01, 0x02, 0x41, 0x00,
1118 0xf5, 0x83, 0xa4, 0x64, 0x4a, 0xf2, 0xdd, 0x8c,
1119 0x2c, 0xed, 0xa8, 0xd5, 0x60, 0x5a, 0xe4, 0xc7,
1120 0xcc, 0x61, 0xcd, 0x38, 0x42, 0x20, 0xd3, 0x82,
1121 0x18, 0xf2, 0x35, 0x00, 0x72, 0x2d, 0xf7, 0x89,
1122 0x80, 0x67, 0xb5, 0x93, 0x05, 0x5f, 0xdd, 0x42,
1123 0xba, 0x16, 0x1a, 0xea, 0x15, 0xc6, 0xf0, 0xb8,
1124 0x8c, 0xbc, 0xbf, 0x54, 0x9e, 0xf1, 0xc1, 0xb2,
1125 0xb3, 0x8b, 0xb6, 0x26, 0x02, 0x30, 0xc4, 0x81,
1126 0x02, 0x41, 0x00, 0xc0, 0x60, 0x62, 0x80, 0xe1,
1127 0x22, 0x78, 0xf6, 0x9d, 0x83, 0x18, 0xeb, 0x72,
1128 0x45, 0xd7, 0xc8, 0x01, 0x7f, 0xa9, 0xca, 0x8f,
1129 0x7d, 0xd6, 0xb8, 0x31, 0x2b, 0x84, 0x7f, 0x62,
1130 0xd9, 0xa9, 0x22, 0x17, 0x7d, 0x06, 0x35, 0x6c,
1131 0xf3, 0xc1, 0x94, 0x17, 0x85, 0x5a, 0xaf, 0x9c,
1132 0x5c, 0x09, 0x3c, 0xcf, 0x2f, 0x44, 0x9d, 0xb6,
1133 0x52, 0x68, 0x5f, 0xf9, 0x59, 0xc8, 0x84, 0x2b,
1134 0x39, 0x22, 0x8f, 0x02, 0x41, 0x00, 0xb2, 0x04,
1135 0xe2, 0x0e, 0x56, 0xca, 0x03, 0x1a, 0xc0, 0xf9,
1136 0x12, 0x92, 0xa5, 0x6b, 0x42, 0xb8, 0x1c, 0xda,
1137 0x4d, 0x93, 0x9d, 0x5f, 0x6f, 0xfd, 0xc5, 0x58,
1138 0xda, 0x55, 0x98, 0x74, 0xfc, 0x28, 0x17, 0x93,
1139 0x1b, 0x75, 0x9f, 0x50, 0x03, 0x7f, 0x7e, 0xae,
1140 0xc8, 0x95, 0x33, 0x75, 0x2c, 0xd6, 0xa4, 0x35,
1141 0xb8, 0x06, 0x03, 0xba, 0x08, 0x59, 0x2b, 0x17,
1142 0x02, 0xdc, 0x4c, 0x7a, 0x50, 0x01, 0x02, 0x41,
1143 0x00, 0x9d, 0xdb, 0x39, 0x59, 0x09, 0xe4, 0x30,
1144 0xa0, 0x24, 0xf5, 0xdb, 0x2f, 0xf0, 0x2f, 0xf1,
1145 0x75, 0x74, 0x0d, 0x5e, 0xb5, 0x11, 0x73, 0xb0,
1146 0x0a, 0xaa, 0x86, 0x4c, 0x0d, 0xff, 0x7e, 0x1d,
1147 0xb4, 0x14, 0xd4, 0x09, 0x91, 0x33, 0x5a, 0xfd,
1148 0xa0, 0x58, 0x80, 0x9b, 0xbe, 0x78, 0x2e, 0x69,
1149 0x82, 0x15, 0x7c, 0x72, 0xf0, 0x7b, 0x18, 0x39,
1150 0xff, 0x6e, 0xeb, 0xc6, 0x86, 0xf5, 0xb4, 0xc7,
1151 0x6f, 0x02, 0x41, 0x00, 0x8d, 0x1a, 0x37, 0x0f,
1152 0x76, 0xc4, 0x82, 0xfa, 0x5c, 0xc3, 0x79, 0x35,
1153 0x3e, 0x70, 0x8a, 0xbf, 0x27, 0x49, 0xb0, 0x99,
1154 0x63, 0xcb, 0x77, 0x5f, 0xa8, 0x82, 0x65, 0xf6,
1155 0x03, 0x52, 0x51, 0xf1, 0xae, 0x2e, 0x05, 0xb3,
1156 0xc6, 0xa4, 0x92, 0xd1, 0xce, 0x6c, 0x72, 0xfb,
1157 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca,
1158 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3,
1159 0xb1, 0xc5, 0x15, 0xf3
1160 };
1161
1162 std::vector<uint8> input;
1163 input.resize(sizeof(private_key_info));
1164 memcpy(&input.front(), private_key_info, sizeof(private_key_info));
1165
1166 private_key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
wtc 2011/08/04 00:37:53 I don't know why we're repeating the test. The on
mdietz 2011/08/18 00:02:45 Agreed, removed.
1167 ASSERT_TRUE(private_key.get());
1168
1169 // Reset cert and actual values
1170 cert = NULL;
1171 actual.type = siBuffer;
1172 actual.data = NULL;
1173 actual.len = 0;
wtc 2011/08/04 00:37:53 Call SECITEM_FreeItem(&actual, PR_FALSE); befor
mdietz 2011/08/18 00:02:45 No longer an issue since I've removed this code.
1174
1175 cert = X509Certificate::CreateOriginBound(private_key.get(),
1176 "CN=subject",
1177 origin, 1,
1178 base::TimeDelta::FromDays(1));
1179
1180 EXPECT_EQ("subject", cert->subject().GetDisplayName());
1181 EXPECT_FALSE(cert->HasExpired());
1182
1183 // Lookup Origin Bound Cert extension in generated cert
1184 ok = CERT_FindCertExtension(cert->os_cert_handle(),
1185 ob_cert_oid_tag,
1186 &actual);
1187 ASSERT_EQ(ok, SECSuccess);
1188
1189 // Compare expected and actual extension values
1190 res = SECITEM_ItemsAreEqual(expected, &actual);
1191 ASSERT_EQ(res, PR_TRUE);
1192 }
1193
1019 TEST(X509CertificateTest, GetDEREncoded) { 1194 TEST(X509CertificateTest, GetDEREncoded) {
1020 scoped_ptr<crypto::RSAPrivateKey> private_key( 1195 scoped_ptr<crypto::RSAPrivateKey> private_key(
1021 crypto::RSAPrivateKey::Create(1024)); 1196 crypto::RSAPrivateKey::Create(1024));
1022 scoped_refptr<X509Certificate> cert = 1197 scoped_refptr<X509Certificate> cert =
1023 X509Certificate::CreateSelfSigned( 1198 X509Certificate::CreateSelfSigned(
1024 private_key.get(), "CN=subject", 0, base::TimeDelta::FromDays(1)); 1199 private_key.get(), "CN=subject", 0, base::TimeDelta::FromDays(1));
1025 1200
1026 std::string der_cert; 1201 std::string der_cert;
1027 EXPECT_TRUE(cert->GetDEREncoded(&der_cert)); 1202 EXPECT_TRUE(cert->GetDEREncoded(&der_cert));
1028 EXPECT_FALSE(der_cert.empty()); 1203 EXPECT_FALSE(der_cert.empty());
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1270 } 1445 }
1271 1446
1272 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname( 1447 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname(
1273 test_data.hostname, common_name, dns_names, ip_addressses)); 1448 test_data.hostname, common_name, dns_names, ip_addressses));
1274 } 1449 }
1275 1450
1276 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest, 1451 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest,
1277 testing::ValuesIn(kNameVerifyTestData)); 1452 testing::ValuesIn(kNameVerifyTestData));
1278 1453
1279 } // namespace net 1454 } // namespace net
OLDNEW
« net/base/x509_certificate_nss.cc ('K') | « net/base/x509_certificate_nss.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698