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_certificate.h" | 5 #include "net/base/x509_certificate.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <cryptohi.h> | 8 #include <cryptohi.h> |
9 #include <keyhi.h> | 9 #include <keyhi.h> |
10 #include <nss.h> | 10 #include <nss.h> |
11 #include <pk11pub.h> | 11 #include <pk11pub.h> |
12 #include <prerror.h> | 12 #include <prerror.h> |
13 #include <prtime.h> | 13 #include <prtime.h> |
14 #include <secder.h> | 14 #include <secder.h> |
15 #include <secerr.h> | 15 #include <secerr.h> |
16 #include <sechash.h> | 16 #include <sechash.h> |
17 #include <sslerr.h> | 17 #include <sslerr.h> |
18 | 18 |
19 #include "base/crypto/rsa_private_key.h" | |
20 #include "base/logging.h" | 19 #include "base/logging.h" |
21 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
22 #include "base/pickle.h" | 21 #include "base/pickle.h" |
23 #include "base/time.h" | 22 #include "base/time.h" |
24 #include "base/nss_util.h" | 23 #include "crypto/nss_util.h" |
| 24 #include "crypto/rsa_private_key.h" |
25 #include "net/base/cert_status_flags.h" | 25 #include "net/base/cert_status_flags.h" |
26 #include "net/base/cert_verify_result.h" | 26 #include "net/base/cert_verify_result.h" |
27 #include "net/base/ev_root_ca_metadata.h" | 27 #include "net/base/ev_root_ca_metadata.h" |
28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
29 | 29 |
30 namespace net { | 30 namespace net { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 class ScopedCERTCertificatePolicies { | 34 class ScopedCERTCertificatePolicies { |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 single_values[i]->assign(value); | 271 single_values[i]->assign(value); |
272 PORT_Free(value); | 272 PORT_Free(value); |
273 } | 273 } |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
277 void ParseDate(SECItem* der_date, base::Time* result) { | 277 void ParseDate(SECItem* der_date, base::Time* result) { |
278 PRTime prtime; | 278 PRTime prtime; |
279 SECStatus rv = DER_DecodeTimeChoice(&prtime, der_date); | 279 SECStatus rv = DER_DecodeTimeChoice(&prtime, der_date); |
280 DCHECK(rv == SECSuccess); | 280 DCHECK(rv == SECSuccess); |
281 *result = base::PRTimeToBaseTime(prtime); | 281 *result = crypto::PRTimeToBaseTime(prtime); |
282 } | 282 } |
283 | 283 |
284 void GetCertSubjectAltNamesOfType(X509Certificate::OSCertHandle cert_handle, | 284 void GetCertSubjectAltNamesOfType(X509Certificate::OSCertHandle cert_handle, |
285 CERTGeneralNameType name_type, | 285 CERTGeneralNameType name_type, |
286 std::vector<std::string>* result) { | 286 std::vector<std::string>* result) { |
287 // For future extension: We only support general names of types | 287 // For future extension: We only support general names of types |
288 // RFC822Name, DNSName or URI. | 288 // RFC822Name, DNSName or URI. |
289 DCHECK(name_type == certRFC822Name || | 289 DCHECK(name_type == certRFC822Name || |
290 name_type == certDNSName || | 290 name_type == certDNSName || |
291 name_type == certURI); | 291 name_type == certURI); |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 const char* data; | 655 const char* data; |
656 int length; | 656 int length; |
657 if (!pickle.ReadData(pickle_iter, &data, &length)) | 657 if (!pickle.ReadData(pickle_iter, &data, &length)) |
658 return NULL; | 658 return NULL; |
659 | 659 |
660 return CreateFromBytes(data, length); | 660 return CreateFromBytes(data, length); |
661 } | 661 } |
662 | 662 |
663 // static | 663 // static |
664 X509Certificate* X509Certificate::CreateSelfSigned( | 664 X509Certificate* X509Certificate::CreateSelfSigned( |
665 base::RSAPrivateKey* key, | 665 crypto::RSAPrivateKey* key, |
666 const std::string& subject, | 666 const std::string& subject, |
667 uint32 serial_number, | 667 uint32 serial_number, |
668 base::TimeDelta valid_duration) { | 668 base::TimeDelta valid_duration) { |
669 DCHECK(key); | 669 DCHECK(key); |
670 | 670 |
671 // Create info about public key. | 671 // Create info about public key. |
672 CERTSubjectPublicKeyInfo* spki = | 672 CERTSubjectPublicKeyInfo* spki = |
673 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); | 673 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); |
674 if (!spki) | 674 if (!spki) |
675 return NULL; | 675 return NULL; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 return a->derCert.len == b->derCert.len && | 919 return a->derCert.len == b->derCert.len && |
920 memcmp(a->derCert.data, b->derCert.data, a->derCert.len) == 0; | 920 memcmp(a->derCert.data, b->derCert.data, a->derCert.len) == 0; |
921 } | 921 } |
922 | 922 |
923 // static | 923 // static |
924 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes( | 924 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes( |
925 const char* data, int length) { | 925 const char* data, int length) { |
926 if (length < 0) | 926 if (length < 0) |
927 return NULL; | 927 return NULL; |
928 | 928 |
929 base::EnsureNSSInit(); | 929 crypto::EnsureNSSInit(); |
930 | 930 |
931 if (!NSS_IsInitialized()) | 931 if (!NSS_IsInitialized()) |
932 return NULL; | 932 return NULL; |
933 | 933 |
934 SECItem der_cert; | 934 SECItem der_cert; |
935 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); | 935 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); |
936 der_cert.len = length; | 936 der_cert.len = length; |
937 der_cert.type = siDERCertBuffer; | 937 der_cert.type = siDERCertBuffer; |
938 | 938 |
939 // Parse into a certificate structure. | 939 // Parse into a certificate structure. |
940 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, | 940 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, |
941 PR_FALSE, PR_TRUE); | 941 PR_FALSE, PR_TRUE); |
942 } | 942 } |
943 | 943 |
944 // static | 944 // static |
945 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes( | 945 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes( |
946 const char* data, int length, Format format) { | 946 const char* data, int length, Format format) { |
947 OSCertHandles results; | 947 OSCertHandles results; |
948 if (length < 0) | 948 if (length < 0) |
949 return results; | 949 return results; |
950 | 950 |
951 base::EnsureNSSInit(); | 951 crypto::EnsureNSSInit(); |
952 | 952 |
953 if (!NSS_IsInitialized()) | 953 if (!NSS_IsInitialized()) |
954 return results; | 954 return results; |
955 | 955 |
956 switch (format) { | 956 switch (format) { |
957 case FORMAT_SINGLE_CERTIFICATE: { | 957 case FORMAT_SINGLE_CERTIFICATE: { |
958 OSCertHandle handle = CreateOSCertHandleFromBytes(data, length); | 958 OSCertHandle handle = CreateOSCertHandleFromBytes(data, length); |
959 if (handle) | 959 if (handle) |
960 results.push_back(handle); | 960 results.push_back(handle); |
961 break; | 961 break; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 DCHECK(0 != cert->derCert.len); | 999 DCHECK(0 != cert->derCert.len); |
1000 | 1000 |
1001 SECStatus rv = HASH_HashBuf(HASH_AlgSHA1, sha1.data, | 1001 SECStatus rv = HASH_HashBuf(HASH_AlgSHA1, sha1.data, |
1002 cert->derCert.data, cert->derCert.len); | 1002 cert->derCert.data, cert->derCert.len); |
1003 DCHECK(rv == SECSuccess); | 1003 DCHECK(rv == SECSuccess); |
1004 | 1004 |
1005 return sha1; | 1005 return sha1; |
1006 } | 1006 } |
1007 | 1007 |
1008 } // namespace net | 1008 } // namespace net |
OLD | NEW |