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_ios.h" | 5 #include "net/cert/x509_util_ios.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <CommonCrypto/CommonDigest.h> | 8 #include <CommonCrypto/CommonDigest.h> |
9 #include <nss.h> | 9 #include <nss.h> |
10 #include <prtypes.h> | 10 #include <prtypes.h> |
11 | 11 |
12 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
13 #include "crypto/nss_util.h" | 13 #include "crypto/nss_util.h" |
14 #include "net/cert/x509_certificate.h" | 14 #include "net/cert/x509_certificate.h" |
15 #include "net/cert/x509_util_nss.h" | 15 #include "net/cert/x509_util_nss.h" |
16 | 16 |
17 using base::ScopedCFTypeRef; | 17 using base::ScopedCFTypeRef; |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 namespace x509_util_ios { | 20 namespace x509_util_ios { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Creates an NSS certificate handle from |data|, which is |length| bytes in | 24 // Creates an NSS certificate handle from |data|, which is |length| bytes in |
25 // size. | 25 // size. |
26 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, | 26 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, int length) { |
27 int length) { | |
28 if (length < 0) | 27 if (length < 0) |
29 return NULL; | 28 return NULL; |
30 | 29 |
31 crypto::EnsureNSSInit(); | 30 crypto::EnsureNSSInit(); |
32 | 31 |
33 if (!NSS_IsInitialized()) | 32 if (!NSS_IsInitialized()) |
34 return NULL; | 33 return NULL; |
35 | 34 |
36 SECItem der_cert; | 35 SECItem der_cert; |
37 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); | 36 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); |
38 der_cert.len = length; | 37 der_cert.len = length; |
39 der_cert.type = siDERCertBuffer; | 38 der_cert.type = siDERCertBuffer; |
40 | 39 |
41 // Parse into a certificate structure. | 40 // Parse into a certificate structure. |
42 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, | 41 return CERT_NewTempCertificate( |
43 PR_FALSE, PR_TRUE); | 42 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE); |
44 } | 43 } |
45 | 44 |
46 } // namespace | 45 } // namespace |
47 | 46 |
48 CERTCertificate* CreateNSSCertHandleFromOSHandle( | 47 CERTCertificate* CreateNSSCertHandleFromOSHandle( |
49 SecCertificateRef cert_handle) { | 48 SecCertificateRef cert_handle) { |
50 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert_handle)); | 49 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert_handle)); |
51 return CreateNSSCertHandleFromBytes( | 50 return CreateNSSCertHandleFromBytes( |
52 reinterpret_cast<const char*>(CFDataGetBytePtr(cert_data)), | 51 reinterpret_cast<const char*>(CFDataGetBytePtr(cert_data)), |
53 CFDataGetLength(cert_data)); | 52 CFDataGetLength(cert_data)); |
(...skipping 17 matching lines...) Expand all Loading... |
71 for (size_t i = 0; i < intermediates.size(); ++i) { | 70 for (size_t i = 0; i < intermediates.size(); ++i) { |
72 SecCertificateRef intermediate = | 71 SecCertificateRef intermediate = |
73 CreateOSCertHandleFromNSSHandle(intermediates[i]); | 72 CreateOSCertHandleFromNSSHandle(intermediates[i]); |
74 if (!intermediate) | 73 if (!intermediate) |
75 break; | 74 break; |
76 os_intermediates.push_back(intermediate); | 75 os_intermediates.push_back(intermediate); |
77 } | 76 } |
78 | 77 |
79 X509Certificate* cert = NULL; | 78 X509Certificate* cert = NULL; |
80 if (intermediates.size() == os_intermediates.size()) { | 79 if (intermediates.size() == os_intermediates.size()) { |
81 cert = X509Certificate::CreateFromHandle(os_server_cert, | 80 cert = X509Certificate::CreateFromHandle(os_server_cert, os_intermediates); |
82 os_intermediates); | |
83 } | 81 } |
84 | 82 |
85 for (size_t i = 0; i < os_intermediates.size(); ++i) | 83 for (size_t i = 0; i < os_intermediates.size(); ++i) |
86 CFRelease(os_intermediates[i]); | 84 CFRelease(os_intermediates[i]); |
87 return cert; | 85 return cert; |
88 } | 86 } |
89 | 87 |
90 SHA1HashValue CalculateFingerprintNSS(CERTCertificate* cert) { | 88 SHA1HashValue CalculateFingerprintNSS(CERTCertificate* cert) { |
91 DCHECK(cert->derCert.data); | 89 DCHECK(cert->derCert.data); |
92 DCHECK_NE(0U, cert->derCert.len); | 90 DCHECK_NE(0U, cert->derCert.len); |
(...skipping 16 matching lines...) Expand all Loading... |
109 } | 107 } |
110 | 108 |
111 CERTCertificate* NSSCertificate::cert_handle() const { | 109 CERTCertificate* NSSCertificate::cert_handle() const { |
112 return nss_cert_handle_; | 110 return nss_cert_handle_; |
113 } | 111 } |
114 | 112 |
115 // NSSCertChain implementation | 113 // NSSCertChain implementation |
116 | 114 |
117 NSSCertChain::NSSCertChain(X509Certificate* certificate) { | 115 NSSCertChain::NSSCertChain(X509Certificate* certificate) { |
118 DCHECK(certificate); | 116 DCHECK(certificate); |
119 certs_.push_back(CreateNSSCertHandleFromOSHandle( | 117 certs_.push_back( |
120 certificate->os_cert_handle())); | 118 CreateNSSCertHandleFromOSHandle(certificate->os_cert_handle())); |
121 const X509Certificate::OSCertHandles& cert_intermediates = | 119 const X509Certificate::OSCertHandles& cert_intermediates = |
122 certificate->GetIntermediateCertificates(); | 120 certificate->GetIntermediateCertificates(); |
123 for (size_t i = 0; i < cert_intermediates.size(); ++i) | 121 for (size_t i = 0; i < cert_intermediates.size(); ++i) |
124 certs_.push_back(CreateNSSCertHandleFromOSHandle(cert_intermediates[i])); | 122 certs_.push_back(CreateNSSCertHandleFromOSHandle(cert_intermediates[i])); |
125 } | 123 } |
126 | 124 |
127 NSSCertChain::~NSSCertChain() { | 125 NSSCertChain::~NSSCertChain() { |
128 for (size_t i = 0; i < certs_.size(); ++i) | 126 for (size_t i = 0; i < certs_.size(); ++i) |
129 CERT_DestroyCertificate(certs_[i]); | 127 CERT_DestroyCertificate(certs_[i]); |
130 } | 128 } |
131 | 129 |
132 CERTCertificate* NSSCertChain::cert_handle() const { | 130 CERTCertificate* NSSCertChain::cert_handle() const { |
133 return certs_.empty() ? NULL : certs_.front(); | 131 return certs_.empty() ? NULL : certs_.front(); |
134 } | 132 } |
135 | 133 |
136 const std::vector<CERTCertificate*>& NSSCertChain::cert_chain() const { | 134 const std::vector<CERTCertificate*>& NSSCertChain::cert_chain() const { |
137 return certs_; | 135 return certs_; |
138 } | 136 } |
139 | 137 |
140 } // namespace x509_util_ios | 138 } // namespace x509_util_ios |
141 } // namespace net | 139 } // namespace net |
OLD | NEW |