| 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 "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" | |
| 14 #include "crypto/rsa_private_key.h" | 13 #include "crypto/rsa_private_key.h" |
| 15 #include "crypto/scoped_nss_types.h" | |
| 16 #include "crypto/signature_verifier.h" | |
| 17 #include "net/base/x509_certificate.h" | 14 #include "net/base/x509_certificate.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 16 |
| 20 namespace net { | |
| 21 | |
| 22 namespace { | 17 namespace { |
| 23 | 18 |
| 24 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, size_t length) { | 19 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, size_t length) { |
| 25 SECItem der_cert; | 20 SECItem der_cert; |
| 26 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); | 21 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); |
| 27 der_cert.len = length; | 22 der_cert.len = length; |
| 28 der_cert.type = siDERCertBuffer; | 23 der_cert.type = siDERCertBuffer; |
| 29 | 24 |
| 30 // Parse into a certificate structure. | 25 // Parse into a certificate structure. |
| 31 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, | 26 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, |
| 32 PR_FALSE, PR_TRUE); | 27 PR_FALSE, PR_TRUE); |
| 33 } | 28 } |
| 34 | 29 |
| 35 void VerifyCertificateSignature(const std::string& der_cert, | 30 } // namespace |
| 36 const std::vector<uint8>& der_spki) { | |
| 37 crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE)); | |
| 38 | 31 |
| 39 CERTSignedData sd; | 32 namespace net { |
| 40 memset(&sd, 0, sizeof(sd)); | |
| 41 | 33 |
| 42 SECItem der_cert_item = { | 34 // This test creates an origin-bound cert from a private key and |
| 43 siDERCertBuffer, | 35 // then verifies the content of the certificate. |
| 44 reinterpret_cast<unsigned char*>(const_cast<char*>(der_cert.data())), | 36 TEST(X509UtilNSSTest, CreateOriginBoundCert) { |
| 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) { | |
| 80 // Origin Bound Cert OID. | 37 // Origin Bound Cert OID. |
| 81 static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6"; | 38 static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6"; |
| 82 | 39 |
| 40 // Create a sample ASCII weborigin. |
| 41 std::string origin = "http://weborigin.com:443"; |
| 42 |
| 83 // Create object neccessary for extension lookup call. | 43 // Create object neccessary for extension lookup call. |
| 84 SECItem extension_object = { | 44 SECItem extension_object = { |
| 85 siAsciiString, | 45 siAsciiString, |
| 86 (unsigned char*)origin.data(), | 46 (unsigned char*)origin.data(), |
| 87 origin.size() | 47 origin.size() |
| 88 }; | 48 }; |
| 89 | 49 |
| 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 |
| 90 // IA5Encode and arena allocate SECItem. | 64 // IA5Encode and arena allocate SECItem. |
| 91 PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 65 PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
| 92 SECItem* expected = SEC_ASN1EncodeItem(arena, | 66 SECItem* expected = SEC_ASN1EncodeItem(arena, |
| 93 NULL, | 67 NULL, |
| 94 &extension_object, | 68 &extension_object, |
| 95 SEC_ASN1_GET(SEC_IA5StringTemplate)); | 69 SEC_ASN1_GET(SEC_IA5StringTemplate)); |
| 96 | 70 |
| 97 ASSERT_NE(static_cast<SECItem*>(NULL), expected); | 71 ASSERT_NE(static_cast<SECItem*>(NULL), expected); |
| 98 | 72 |
| 99 // Create OID SECItem. | 73 // Create OID SECItem. |
| 100 SECItem ob_cert_oid = { siDEROID, NULL, 0 }; | 74 SECItem ob_cert_oid = { siDEROID, NULL, 0 }; |
| 101 SECStatus ok = SEC_StringToOID(arena, &ob_cert_oid, | 75 SECStatus ok = SEC_StringToOID(arena, &ob_cert_oid, |
| 102 oid_string, 0); | 76 oid_string, 0); |
| 103 | 77 |
| 104 ASSERT_EQ(SECSuccess, ok); | 78 ASSERT_EQ(SECSuccess, ok); |
| 105 | 79 |
| 106 SECOidTag ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid); | 80 SECOidTag ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid); |
| 107 | 81 |
| 108 ASSERT_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag); | 82 ASSERT_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag); |
| 109 | 83 |
| 110 // This test is run on Mac and Win where X509Certificate::os_cert_handle isn't | 84 // This test is run on Mac and Win where X509Certificate::os_cert_handle isn't |
| 111 // an NSS type, so we have to manually create a NSS certificate object so we | 85 // an NSS type, so we have to manually create a NSS certificate object so we |
| 112 // can use CERT_FindCertExtension. We also check the subject and validity | 86 // can use CERT_FindCertExtension. |
| 113 // times using NSS since X509Certificate will fail with EC certs on OSX 10.5 | |
| 114 // (http://crbug.com/101231). | |
| 115 CERTCertificate* nss_cert = CreateNSSCertHandleFromBytes( | 87 CERTCertificate* nss_cert = CreateNSSCertHandleFromBytes( |
| 116 der_cert.data(), der_cert.size()); | 88 der_cert.data(), der_cert.size()); |
| 117 | |
| 118 EXPECT_STREQ("anonymous.invalid", CERT_GetCommonName(&nss_cert->subject)); | |
| 119 EXPECT_EQ(SECSuccess, CERT_CertTimesValid(nss_cert)); | |
| 120 | |
| 121 // Lookup Origin Bound Cert extension in generated cert. | 89 // Lookup Origin Bound Cert extension in generated cert. |
| 122 SECItem actual = { siBuffer, NULL, 0 }; | 90 SECItem actual = { siBuffer, NULL, 0 }; |
| 123 ok = CERT_FindCertExtension(nss_cert, | 91 ok = CERT_FindCertExtension(nss_cert, |
| 124 ob_cert_oid_tag, | 92 ob_cert_oid_tag, |
| 125 &actual); | 93 &actual); |
| 126 CERT_DestroyCertificate(nss_cert); | 94 CERT_DestroyCertificate(nss_cert); |
| 127 ASSERT_EQ(SECSuccess, ok); | 95 ASSERT_EQ(SECSuccess, ok); |
| 128 | 96 |
| 129 // Compare expected and actual extension values. | 97 // Compare expected and actual extension values. |
| 130 PRBool result = SECITEM_ItemsAreEqual(expected, &actual); | 98 PRBool result = SECITEM_ItemsAreEqual(expected, &actual); |
| 131 ASSERT_TRUE(result); | 99 ASSERT_TRUE(result); |
| 132 | 100 |
| 133 // Do Cleanup. | 101 // Do Cleanup. |
| 134 SECITEM_FreeItem(&actual, PR_FALSE); | 102 SECITEM_FreeItem(&actual, PR_FALSE); |
| 135 PORT_FreeArena(arena, PR_FALSE); | 103 PORT_FreeArena(arena, PR_FALSE); |
| 136 } | 104 } |
| 137 | 105 |
| 138 } // namespace | |
| 139 | |
| 140 // This test creates an origin-bound cert from a RSA private key and | |
| 141 // then verifies the content of the certificate. | |
| 142 TEST(X509UtilNSSTest, CreateOriginBoundCertRSA) { | |
| 143 // Create a sample ASCII weborigin. | |
| 144 std::string origin = "http://weborigin.com:443"; | |
| 145 | |
| 146 scoped_ptr<crypto::RSAPrivateKey> private_key( | |
| 147 crypto::RSAPrivateKey::Create(1024)); | |
| 148 std::string der_cert; | |
| 149 ASSERT_TRUE(x509_util::CreateOriginBoundCertRSA(private_key.get(), | |
| 150 origin, 1, | |
| 151 base::TimeDelta::FromDays(1), | |
| 152 &der_cert)); | |
| 153 | |
| 154 VerifyOriginBoundCert(origin, der_cert); | |
| 155 | |
| 156 std::vector<uint8> spki; | |
| 157 ASSERT_TRUE(private_key->ExportPublicKey(&spki)); | |
| 158 VerifyCertificateSignature(der_cert, spki); | |
| 159 } | |
| 160 | |
| 161 // This test creates an origin-bound cert from an EC private key and | |
| 162 // then verifies the content of the certificate. | |
| 163 TEST(X509UtilNSSTest, CreateOriginBoundCertEC) { | |
| 164 // Create a sample ASCII weborigin. | |
| 165 std::string origin = "http://weborigin.com:443"; | |
| 166 | |
| 167 scoped_ptr<crypto::ECPrivateKey> private_key( | |
| 168 crypto::ECPrivateKey::Create()); | |
| 169 std::string der_cert; | |
| 170 ASSERT_TRUE(x509_util::CreateOriginBoundCertEC(private_key.get(), | |
| 171 origin, 1, | |
| 172 base::TimeDelta::FromDays(1), | |
| 173 &der_cert)); | |
| 174 | |
| 175 VerifyOriginBoundCert(origin, der_cert); | |
| 176 | |
| 177 #if !defined(OS_WIN) && !defined(OS_MACOSX) | |
| 178 // signature_verifier_win and signature_verifier_mac can't handle EC certs. | |
| 179 std::vector<uint8> spki; | |
| 180 ASSERT_TRUE(private_key->ExportPublicKey(&spki)); | |
| 181 VerifyCertificateSignature(der_cert, spki); | |
| 182 #endif | |
| 183 } | |
| 184 | |
| 185 } // namespace net | 106 } // namespace net |
| OLD | NEW |