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

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

Issue 8537025: Allow signing EC certs and creating EC origin-bound certs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: oops Created 9 years 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
« no previous file with comments | « net/base/x509_util_nss.cc ('k') | net/base/x509_util_openssl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "net/base/x509_util.h" 5 #include "net/base/x509_util.h"
6 #include "net/base/x509_util_nss.h" 6 #include "net/base/x509_util_nss.h"
7 7
8 #include <cert.h> 8 #include <cert.h>
9 #include <secoid.h> 9 #include <secoid.h>
10 10
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "crypto/ec_private_key.h"
13 #include "crypto/rsa_private_key.h" 14 #include "crypto/rsa_private_key.h"
15 #include "crypto/scoped_nss_types.h"
16 #include "crypto/signature_verifier.h"
14 #include "net/base/x509_certificate.h" 17 #include "net/base/x509_certificate.h"
15 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
16 19
20 namespace net {
21
17 namespace { 22 namespace {
18 23
19 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, size_t length) { 24 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, size_t length) {
20 SECItem der_cert; 25 SECItem der_cert;
21 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); 26 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data));
22 der_cert.len = length; 27 der_cert.len = length;
23 der_cert.type = siDERCertBuffer; 28 der_cert.type = siDERCertBuffer;
24 29
25 // Parse into a certificate structure. 30 // Parse into a certificate structure.
26 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, 31 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL,
27 PR_FALSE, PR_TRUE); 32 PR_FALSE, PR_TRUE);
28 } 33 }
29 34
30 } // namespace 35 void VerifyCertificateSignature(const std::string& der_cert,
36 const std::vector<uint8>& der_spki) {
37 crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
31 38
32 namespace net { 39 CERTSignedData sd;
40 memset(&sd, 0, sizeof(sd));
33 41
34 // This test creates an origin-bound cert from a private key and 42 SECItem der_cert_item = {
35 // then verifies the content of the certificate. 43 siDERCertBuffer,
36 TEST(X509UtilNSSTest, CreateOriginBoundCert) { 44 reinterpret_cast<unsigned char*>(const_cast<char*>(der_cert.data())),
45 der_cert.size()
46 };
47 SECStatus rv = SEC_ASN1DecodeItem(arena.get(), &sd,
48 SEC_ASN1_GET(CERT_SignedDataTemplate),
49 &der_cert_item);
50 ASSERT_EQ(SECSuccess, rv);
51
52 // The CERTSignedData.signatureAlgorithm is decoded, but SignatureVerifier
53 // wants the DER encoded form, so re-encode it again.
54 SECItem* signature_algorithm = SEC_ASN1EncodeItem(
55 arena.get(),
56 NULL,
57 &sd.signatureAlgorithm,
58 SEC_ASN1_GET(SECOID_AlgorithmIDTemplate));
59 ASSERT_TRUE(signature_algorithm);
60
61 crypto::SignatureVerifier verifier;
62 bool ok = verifier.VerifyInit(
63 signature_algorithm->data,
64 signature_algorithm->len,
65 sd.signature.data,
66 sd.signature.len / 8, // Signature is a BIT STRING, convert to bytes.
67 &der_spki[0],
68 der_spki.size());
69
70 ASSERT_TRUE(ok);
71 verifier.VerifyUpdate(sd.data.data,
72 sd.data.len);
73
74 ok = verifier.VerifyFinal();
75 EXPECT_TRUE(ok);
76 }
77
78 void VerifyOriginBoundCert(const std::string& origin,
79 const std::string& der_cert) {
37 // Origin Bound Cert OID. 80 // Origin Bound Cert OID.
38 static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6"; 81 static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6";
39 82
40 // Create a sample ASCII weborigin.
41 std::string origin = "http://weborigin.com:443";
42
43 // Create object neccessary for extension lookup call. 83 // Create object neccessary for extension lookup call.
44 SECItem extension_object = { 84 SECItem extension_object = {
45 siAsciiString, 85 siAsciiString,
46 (unsigned char*)origin.data(), 86 (unsigned char*)origin.data(),
47 origin.size() 87 origin.size()
48 }; 88 };
49 89
50 scoped_ptr<crypto::RSAPrivateKey> private_key(
51 crypto::RSAPrivateKey::Create(1024));
52 std::string der_cert;
53 ASSERT_TRUE(x509_util::CreateOriginBoundCert(private_key.get(),
54 origin, 1,
55 base::TimeDelta::FromDays(1),
56 &der_cert));
57
58 scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromBytes(
59 der_cert.data(), der_cert.size());
60
61 EXPECT_EQ("anonymous.invalid", cert->subject().GetDisplayName());
62 EXPECT_FALSE(cert->HasExpired());
63
64 // IA5Encode and arena allocate SECItem. 90 // IA5Encode and arena allocate SECItem.
65 PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); 91 PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
66 SECItem* expected = SEC_ASN1EncodeItem(arena, 92 SECItem* expected = SEC_ASN1EncodeItem(arena,
67 NULL, 93 NULL,
68 &extension_object, 94 &extension_object,
69 SEC_ASN1_GET(SEC_IA5StringTemplate)); 95 SEC_ASN1_GET(SEC_IA5StringTemplate));
70 96
71 ASSERT_NE(static_cast<SECItem*>(NULL), expected); 97 ASSERT_NE(static_cast<SECItem*>(NULL), expected);
72 98
73 // Create OID SECItem. 99 // Create OID SECItem.
74 SECItem ob_cert_oid = { siDEROID, NULL, 0 }; 100 SECItem ob_cert_oid = { siDEROID, NULL, 0 };
75 SECStatus ok = SEC_StringToOID(arena, &ob_cert_oid, 101 SECStatus ok = SEC_StringToOID(arena, &ob_cert_oid,
76 oid_string, 0); 102 oid_string, 0);
77 103
78 ASSERT_EQ(SECSuccess, ok); 104 ASSERT_EQ(SECSuccess, ok);
79 105
80 SECOidTag ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid); 106 SECOidTag ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid);
81 107
82 ASSERT_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag); 108 ASSERT_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag);
83 109
84 // This test is run on Mac and Win where X509Certificate::os_cert_handle isn't 110 // This test is run on Mac and Win where X509Certificate::os_cert_handle isn't
85 // an NSS type, so we have to manually create a NSS certificate object so we 111 // an NSS type, so we have to manually create a NSS certificate object so we
86 // can use CERT_FindCertExtension. 112 // can use CERT_FindCertExtension. We also check the subject and validity
113 // times using NSS since X509Certificate will fail with EC certs on OSX 10.5
114 // (http://crbug.com/101231).
87 CERTCertificate* nss_cert = CreateNSSCertHandleFromBytes( 115 CERTCertificate* nss_cert = CreateNSSCertHandleFromBytes(
88 der_cert.data(), der_cert.size()); 116 der_cert.data(), der_cert.size());
117
118 char* common_name = CERT_GetCommonName(&nss_cert->subject);
119 ASSERT_TRUE(common_name);
120 EXPECT_STREQ("anonymous.invalid", common_name);
121 PORT_Free(common_name);
122 EXPECT_EQ(SECSuccess, CERT_CertTimesValid(nss_cert));
123
89 // Lookup Origin Bound Cert extension in generated cert. 124 // Lookup Origin Bound Cert extension in generated cert.
90 SECItem actual = { siBuffer, NULL, 0 }; 125 SECItem actual = { siBuffer, NULL, 0 };
91 ok = CERT_FindCertExtension(nss_cert, 126 ok = CERT_FindCertExtension(nss_cert,
92 ob_cert_oid_tag, 127 ob_cert_oid_tag,
93 &actual); 128 &actual);
94 CERT_DestroyCertificate(nss_cert); 129 CERT_DestroyCertificate(nss_cert);
95 ASSERT_EQ(SECSuccess, ok); 130 ASSERT_EQ(SECSuccess, ok);
96 131
97 // Compare expected and actual extension values. 132 // Compare expected and actual extension values.
98 PRBool result = SECITEM_ItemsAreEqual(expected, &actual); 133 PRBool result = SECITEM_ItemsAreEqual(expected, &actual);
99 ASSERT_TRUE(result); 134 ASSERT_TRUE(result);
100 135
101 // Do Cleanup. 136 // Do Cleanup.
102 SECITEM_FreeItem(&actual, PR_FALSE); 137 SECITEM_FreeItem(&actual, PR_FALSE);
103 PORT_FreeArena(arena, PR_FALSE); 138 PORT_FreeArena(arena, PR_FALSE);
104 } 139 }
105 140
141 } // namespace
142
143 // This test creates an origin-bound cert from a RSA private key and
144 // then verifies the content of the certificate.
145 TEST(X509UtilNSSTest, CreateOriginBoundCertRSA) {
146 // Create a sample ASCII weborigin.
147 std::string origin = "http://weborigin.com:443";
148
149 scoped_ptr<crypto::RSAPrivateKey> private_key(
150 crypto::RSAPrivateKey::Create(1024));
151 std::string der_cert;
152 ASSERT_TRUE(x509_util::CreateOriginBoundCertRSA(private_key.get(),
153 origin, 1,
154 base::TimeDelta::FromDays(1),
155 &der_cert));
156
157 VerifyOriginBoundCert(origin, der_cert);
158
159 std::vector<uint8> spki;
160 ASSERT_TRUE(private_key->ExportPublicKey(&spki));
161 VerifyCertificateSignature(der_cert, spki);
162 }
163
164 // This test creates an origin-bound cert from an EC private key and
165 // then verifies the content of the certificate.
166 TEST(X509UtilNSSTest, CreateOriginBoundCertEC) {
167 // Create a sample ASCII weborigin.
168 std::string origin = "http://weborigin.com:443";
169
170 scoped_ptr<crypto::ECPrivateKey> private_key(
171 crypto::ECPrivateKey::Create());
172 std::string der_cert;
173 ASSERT_TRUE(x509_util::CreateOriginBoundCertEC(private_key.get(),
174 origin, 1,
175 base::TimeDelta::FromDays(1),
176 &der_cert));
177
178 VerifyOriginBoundCert(origin, der_cert);
179
180 #if !defined(OS_WIN) && !defined(OS_MACOSX)
181 // signature_verifier_win and signature_verifier_mac can't handle EC certs.
182 std::vector<uint8> spki;
183 ASSERT_TRUE(private_key->ExportPublicKey(&spki));
184 VerifyCertificateSignature(der_cert, spki);
185 #endif
186 }
187
106 } // namespace net 188 } // namespace net
OLDNEW
« no previous file with comments | « net/base/x509_util_nss.cc ('k') | net/base/x509_util_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698