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

Side by Side Diff: net/cert/x509_util_unittest.cc

Issue 27832002: Sign self-signed certs with SHA256. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/cert/x509_util.h" 5 #include "net/cert/x509_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "crypto/hmac.h"
12 #include "crypto/rsa_private_key.h" 13 #include "crypto/rsa_private_key.h"
13 #include "net/cert/x509_certificate.h" 14 #include "net/cert/x509_certificate.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace net { 17 namespace net {
17 18
18 namespace x509_util { 19 namespace x509_util {
19 20
20 TEST(X509UtilTest, SortClientCertificates) { 21 TEST(X509UtilTest, SortClientCertificates) {
21 CertificateList certs; 22 CertificateList certs;
(...skipping 23 matching lines...) Expand all
45 ASSERT_TRUE(certs[1].get()); 46 ASSERT_TRUE(certs[1].get());
46 EXPECT_EQ("older cert", certs[1]->subject().common_name); 47 EXPECT_EQ("older cert", certs[1]->subject().common_name);
47 ASSERT_TRUE(certs[2].get()); 48 ASSERT_TRUE(certs[2].get());
48 EXPECT_EQ("not yet valid", certs[2]->subject().common_name); 49 EXPECT_EQ("not yet valid", certs[2]->subject().common_name);
49 ASSERT_TRUE(certs[3].get()); 50 ASSERT_TRUE(certs[3].get());
50 EXPECT_EQ("expired", certs[3]->subject().common_name); 51 EXPECT_EQ("expired", certs[3]->subject().common_name);
51 ASSERT_FALSE(certs[4].get()); 52 ASSERT_FALSE(certs[4].get());
52 ASSERT_FALSE(certs[5].get()); 53 ASSERT_FALSE(certs[5].get());
53 } 54 }
54 55
55 // This test creates a self-signed cert from a private key and then verify the 56 // This test creates a self-signed cert and a private key and then verifies the
56 // content of the certificate. 57 // content of the certificate.
57 TEST(X509UtilTest, CreateSelfSigned) { 58 TEST(X509UtilTest, CreateKeyAndSelfSigned) {
58 scoped_ptr<crypto::RSAPrivateKey> private_key( 59 crypto::RSAPrivateKey* raw_key;
59 crypto::RSAPrivateKey::Create(1024));
60
61 ASSERT_TRUE(private_key.get());
62 60
63 std::string der_cert; 61 std::string der_cert;
64 ASSERT_TRUE(x509_util::CreateSelfSignedCert( 62 ASSERT_TRUE(x509_util::CreateKeyAndSelfSignedCert(
65 private_key.get(),
66 "CN=subject", 63 "CN=subject",
67 1, 64 1,
68 base::Time::Now(), 65 base::Time::Now(),
69 base::Time::Now() + base::TimeDelta::FromDays(1), 66 base::Time::Now() + base::TimeDelta::FromDays(1),
67 &raw_key,
70 &der_cert)); 68 &der_cert));
71 69
70 scoped_ptr<crypto::RSAPrivateKey> key(raw_key);
71 ASSERT_TRUE(key.get());
72
72 scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes( 73 scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes(
73 der_cert.data(), der_cert.size())); 74 der_cert.data(), der_cert.size()));
74 ASSERT_TRUE(cert.get()); 75 ASSERT_TRUE(cert.get());
75 76
76 EXPECT_EQ("subject", cert->subject().GetDisplayName()); 77 EXPECT_EQ("subject", cert->subject().GetDisplayName());
77 EXPECT_FALSE(cert->HasExpired()); 78 EXPECT_FALSE(cert->HasExpired());
79 }
78 80
79 cert = NULL; 81 // This test creates a self-signed cert from a private key and then verifies the
80 82 // content of the certificate.
83 TEST(X509UtilTest, CreateSelfSignedInternal) {
81 const uint8 private_key_info[] = { 84 const uint8 private_key_info[] = {
82 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, 85 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30,
83 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 86 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
84 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 87 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
85 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 88 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01,
86 0x00, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, 89 0x00, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b,
87 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61, 90 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61,
88 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08, 91 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08,
89 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64, 92 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64,
90 0x85, 0x7b, 0x0c, 0x04, 0x13, 0x3f, 0x8d, 0xf4, 93 0x85, 0x7b, 0x0c, 0x04, 0x13, 0x3f, 0x8d, 0xf4,
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 0xc6, 0xa4, 0x92, 0xd1, 0xce, 0x6c, 0x72, 0xfb, 161 0xc6, 0xa4, 0x92, 0xd1, 0xce, 0x6c, 0x72, 0xfb,
159 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca, 162 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca,
160 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, 163 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3,
161 0xb1, 0xc5, 0x15, 0xf3 164 0xb1, 0xc5, 0x15, 0xf3
162 }; 165 };
163 166
164 std::vector<uint8> input; 167 std::vector<uint8> input;
165 input.resize(sizeof(private_key_info)); 168 input.resize(sizeof(private_key_info));
166 memcpy(&input.front(), private_key_info, sizeof(private_key_info)); 169 memcpy(&input.front(), private_key_info, sizeof(private_key_info));
167 170
168 private_key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); 171 scoped_ptr<crypto::RSAPrivateKey> private_key(
172 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
169 ASSERT_TRUE(private_key.get()); 173 ASSERT_TRUE(private_key.get());
170 174
171 ASSERT_TRUE(x509_util::CreateSelfSignedCert( 175 std::string der_cert;
176 ASSERT_TRUE(x509_util::CreateSelfSignedCertInternal(
172 private_key.get(), 177 private_key.get(),
178 crypto::HMAC::SHA1,
173 "CN=subject", 179 "CN=subject",
174 1, 180 1,
175 base::Time::Now(), 181 base::Time::Now(),
176 base::Time::Now() + base::TimeDelta::FromDays(1), 182 base::Time::Now() + base::TimeDelta::FromDays(1),
177 &der_cert)); 183 &der_cert));
178 184
179 cert = X509Certificate::CreateFromBytes(der_cert.data(), der_cert.size()); 185 scoped_refptr<X509Certificate> cert =
186 X509Certificate::CreateFromBytes(der_cert.data(), der_cert.size());
180 ASSERT_TRUE(cert.get()); 187 ASSERT_TRUE(cert.get());
181 188
182 EXPECT_EQ("subject", cert->subject().GetDisplayName()); 189 EXPECT_EQ("subject", cert->subject().GetDisplayName());
183 EXPECT_FALSE(cert->HasExpired()); 190 EXPECT_FALSE(cert->HasExpired());
184 } 191 }
185 192
186 } // namespace x509_util 193 } // namespace x509_util
187 194
188 } // namespace net 195 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698