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/base/x509_util_ios.h" | 5 #include "net/base/x509_util_ios.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <CommonCrypto/CommonDigest.h> | |
8 #include <nss.h> | 9 #include <nss.h> |
9 #include <prtypes.h> | 10 #include <prtypes.h> |
10 | 11 |
11 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
12 #include "crypto/nss_util.h" | 13 #include "crypto/nss_util.h" |
13 #include "net/base/x509_certificate.h" | 14 #include "net/base/x509_certificate.h" |
14 | 15 |
15 using base::mac::ScopedCFTypeRef; | 16 using base::mac::ScopedCFTypeRef; |
16 | 17 |
17 namespace net { | 18 namespace net { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 CFDataGetLength(cert_data)); | 52 CFDataGetLength(cert_data)); |
52 } | 53 } |
53 | 54 |
54 SecCertificateRef CreateOSCertHandleFromNSSHandle( | 55 SecCertificateRef CreateOSCertHandleFromNSSHandle( |
55 CERTCertificate* nss_cert_handle) { | 56 CERTCertificate* nss_cert_handle) { |
56 return X509Certificate::CreateOSCertHandleFromBytes( | 57 return X509Certificate::CreateOSCertHandleFromBytes( |
57 reinterpret_cast<const char*>(nss_cert_handle->derCert.data), | 58 reinterpret_cast<const char*>(nss_cert_handle->derCert.data), |
58 nss_cert_handle->derCert.len); | 59 nss_cert_handle->derCert.len); |
59 } | 60 } |
60 | 61 |
62 X509Certificate* CreateCertFromNSSHandles( | |
63 CERTCertificate* cert_handle, | |
64 const std::vector<CERTCertificate*>& intermediates) { | |
65 ScopedCFTypeRef<SecCertificateRef> os_server_cert( | |
66 CreateOSCertHandleFromNSSHandle(cert_handle)); | |
67 if (!os_server_cert) | |
68 return NULL; | |
69 std::vector<SecCertificateRef> os_intermediates; | |
70 for (size_t i = 0; i < intermediates.size(); ++i) { | |
71 SecCertificateRef intermediate = | |
72 CreateOSCertHandleFromNSSHandle(intermediates[i]); | |
73 if (!intermediate) | |
74 break; | |
75 os_intermediates.push_back(intermediate); | |
76 } | |
77 | |
78 X509Certificate* cert = NULL; | |
79 if (intermediates.size() == os_intermediates.size()) { | |
80 cert = X509Certificate::CreateFromHandle(os_server_cert, | |
81 os_intermediates); | |
82 } | |
83 | |
84 for (size_t i = 0; i < os_intermediates.size(); ++i) | |
85 CFRelease(os_intermediates[i]); | |
86 return cert; | |
87 } | |
88 | |
89 SHA1HashValue CalculateFingerprintNSS(CERTCertificate* cert) { | |
90 DCHECK(NULL != cert->derCert.data); | |
Ryan Sleevi
2012/09/25 17:13:33
DCHECK_NE(NULL, cert->derCert.data)
| |
91 DCHECK_NE(0U, cert->derCert.len); | |
92 SHA1HashValue sha1; | |
93 memset(sha1.data, 0, sizeof(sha1.data)); | |
94 CC_SHA1(cert->derCert.data, cert->derCert.len, sha1.data); | |
95 return sha1; | |
96 } | |
97 | |
98 // NSSCertificate implementation. | |
99 | |
61 NSSCertificate::NSSCertificate(SecCertificateRef cert_handle) { | 100 NSSCertificate::NSSCertificate(SecCertificateRef cert_handle) { |
62 nss_cert_handle_ = CreateNSSCertHandleFromOSHandle(cert_handle); | 101 nss_cert_handle_ = CreateNSSCertHandleFromOSHandle(cert_handle); |
63 DLOG_IF(INFO, cert_handle && !nss_cert_handle_) | 102 DLOG_IF(INFO, cert_handle && !nss_cert_handle_) |
64 << "Could not convert SecCertificateRef to CERTCertificate*"; | 103 << "Could not convert SecCertificateRef to CERTCertificate*"; |
65 } | 104 } |
66 | 105 |
67 NSSCertificate::~NSSCertificate() { | 106 NSSCertificate::~NSSCertificate() { |
68 CERT_DestroyCertificate(nss_cert_handle_); | 107 CERT_DestroyCertificate(nss_cert_handle_); |
69 } | 108 } |
70 | 109 |
71 CERTCertificate* NSSCertificate::cert_handle() { | 110 CERTCertificate* NSSCertificate::cert_handle() const { |
72 return nss_cert_handle_; | 111 return nss_cert_handle_; |
73 } | 112 } |
74 | 113 |
114 // NSSCertChain implementation | |
115 | |
116 NSSCertChain::NSSCertChain(X509Certificate* certificate) { | |
117 DCHECK(certificate); | |
118 certs_.push_back(CreateNSSCertHandleFromOSHandle( | |
119 certificate->os_cert_handle())); | |
120 const X509Certificate::OSCertHandles& cert_intermediates = | |
121 certificate->GetIntermediateCertificates(); | |
122 for (size_t i = 0; i < cert_intermediates.size(); ++i) { | |
123 certs_.push_back(CreateNSSCertHandleFromOSHandle(cert_intermediates[i])); | |
124 } | |
125 } | |
126 | |
127 NSSCertChain::~NSSCertChain() { | |
128 for (size_t i = 0; i < certs_.size(); ++i) { | |
129 CERT_DestroyCertificate(certs_[i]); | |
130 } | |
Ryan Sleevi
2012/09/25 17:13:33
nit: you can lose the braces here (and on lines 12
| |
131 } | |
132 | |
133 CERTCertificate* NSSCertChain::cert_handle() const { | |
134 return certs_.empty() ? NULL : certs_.front(); | |
135 } | |
136 | |
75 } // namespace x509_util_ios | 137 } // namespace x509_util_ios |
76 } // namespace net | 138 } // namespace net |
77 | 139 |
OLD | NEW |