| OLD | NEW |
| 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" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 ASSERT_TRUE(certs[1].get()); | 45 ASSERT_TRUE(certs[1].get()); |
| 46 EXPECT_EQ("older cert", certs[1]->subject().common_name); | 46 EXPECT_EQ("older cert", certs[1]->subject().common_name); |
| 47 ASSERT_TRUE(certs[2].get()); | 47 ASSERT_TRUE(certs[2].get()); |
| 48 EXPECT_EQ("not yet valid", certs[2]->subject().common_name); | 48 EXPECT_EQ("not yet valid", certs[2]->subject().common_name); |
| 49 ASSERT_TRUE(certs[3].get()); | 49 ASSERT_TRUE(certs[3].get()); |
| 50 EXPECT_EQ("expired", certs[3]->subject().common_name); | 50 EXPECT_EQ("expired", certs[3]->subject().common_name); |
| 51 ASSERT_FALSE(certs[4].get()); | 51 ASSERT_FALSE(certs[4].get()); |
| 52 ASSERT_FALSE(certs[5].get()); | 52 ASSERT_FALSE(certs[5].get()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // This test creates a self-signed cert from a private key and then verify the | 55 // This test creates a self-signed cert and a private key and then verifies the |
| 56 // content of the certificate. | 56 // content of the certificate. |
| 57 TEST(X509UtilTest, CreateSelfSigned) { | 57 TEST(X509UtilTest, CreateKeyAndSelfSigned) { |
| 58 scoped_ptr<crypto::RSAPrivateKey> private_key( | 58 scoped_ptr<crypto::RSAPrivateKey> private_key; |
| 59 crypto::RSAPrivateKey::Create(1024)); | |
| 60 | |
| 61 ASSERT_TRUE(private_key.get()); | |
| 62 | 59 |
| 63 std::string der_cert; | 60 std::string der_cert; |
| 64 ASSERT_TRUE(x509_util::CreateSelfSignedCert( | 61 ASSERT_TRUE(x509_util::CreateKeyAndSelfSignedCert( |
| 65 private_key.get(), | |
| 66 "CN=subject", | 62 "CN=subject", |
| 67 1, | 63 1, |
| 68 base::Time::Now(), | 64 base::Time::Now(), |
| 69 base::Time::Now() + base::TimeDelta::FromDays(1), | 65 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 66 &private_key, |
| 70 &der_cert)); | 67 &der_cert)); |
| 71 | 68 |
| 69 ASSERT_TRUE(private_key.get()); |
| 70 |
| 72 scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes( | 71 scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes( |
| 73 der_cert.data(), der_cert.size())); | 72 der_cert.data(), der_cert.size())); |
| 74 ASSERT_TRUE(cert.get()); | 73 ASSERT_TRUE(cert.get()); |
| 75 | 74 |
| 76 EXPECT_EQ("subject", cert->subject().GetDisplayName()); | 75 EXPECT_EQ("subject", cert->subject().GetDisplayName()); |
| 77 EXPECT_FALSE(cert->HasExpired()); | 76 EXPECT_FALSE(cert->HasExpired()); |
| 77 } |
| 78 | 78 |
| 79 cert = NULL; | 79 // This test creates a self-signed cert from a private key and then verifies the |
| 80 | 80 // content of the certificate. |
| 81 TEST(X509UtilTest, CreateSelfSigned) { |
| 81 const uint8 private_key_info[] = { | 82 const uint8 private_key_info[] = { |
| 82 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, | 83 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, |
| 83 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, | 84 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 84 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, | 85 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, |
| 85 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, | 86 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, |
| 86 0x00, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, | 87 0x00, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, |
| 87 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61, | 88 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61, |
| 88 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08, | 89 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08, |
| 89 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64, | 90 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64, |
| 90 0x85, 0x7b, 0x0c, 0x04, 0x13, 0x3f, 0x8d, 0xf4, | 91 0x85, 0x7b, 0x0c, 0x04, 0x13, 0x3f, 0x8d, 0xf4, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 0xc6, 0xa4, 0x92, 0xd1, 0xce, 0x6c, 0x72, 0xfb, | 159 0xc6, 0xa4, 0x92, 0xd1, 0xce, 0x6c, 0x72, 0xfb, |
| 159 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca, | 160 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca, |
| 160 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, | 161 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, |
| 161 0xb1, 0xc5, 0x15, 0xf3 | 162 0xb1, 0xc5, 0x15, 0xf3 |
| 162 }; | 163 }; |
| 163 | 164 |
| 164 std::vector<uint8> input; | 165 std::vector<uint8> input; |
| 165 input.resize(sizeof(private_key_info)); | 166 input.resize(sizeof(private_key_info)); |
| 166 memcpy(&input.front(), private_key_info, sizeof(private_key_info)); | 167 memcpy(&input.front(), private_key_info, sizeof(private_key_info)); |
| 167 | 168 |
| 168 private_key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); | 169 scoped_ptr<crypto::RSAPrivateKey> private_key( |
| 170 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); |
| 169 ASSERT_TRUE(private_key.get()); | 171 ASSERT_TRUE(private_key.get()); |
| 170 | 172 |
| 173 std::string der_cert; |
| 171 ASSERT_TRUE(x509_util::CreateSelfSignedCert( | 174 ASSERT_TRUE(x509_util::CreateSelfSignedCert( |
| 172 private_key.get(), | 175 private_key.get(), |
| 176 x509_util::DIGEST_SHA1, |
| 173 "CN=subject", | 177 "CN=subject", |
| 174 1, | 178 1, |
| 175 base::Time::Now(), | 179 base::Time::Now(), |
| 176 base::Time::Now() + base::TimeDelta::FromDays(1), | 180 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 177 &der_cert)); | 181 &der_cert)); |
| 178 | 182 |
| 179 cert = X509Certificate::CreateFromBytes(der_cert.data(), der_cert.size()); | 183 scoped_refptr<X509Certificate> cert = |
| 184 X509Certificate::CreateFromBytes(der_cert.data(), der_cert.size()); |
| 180 ASSERT_TRUE(cert.get()); | 185 ASSERT_TRUE(cert.get()); |
| 181 | 186 |
| 182 EXPECT_EQ("subject", cert->subject().GetDisplayName()); | 187 EXPECT_EQ("subject", cert->subject().GetDisplayName()); |
| 183 EXPECT_FALSE(cert->HasExpired()); | 188 EXPECT_FALSE(cert->HasExpired()); |
| 184 } | 189 } |
| 185 | 190 |
| 186 } // namespace x509_util | 191 } // namespace x509_util |
| 187 | 192 |
| 188 } // namespace net | 193 } // namespace net |
| OLD | NEW |