Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: net/base/x509_certificate_ios.cc

Issue 10928107: Support x509 certificate on iOS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix windows compilation Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/ev_root_ca_metadata.cc ('k') | net/base/x509_certificate_nss.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/base/x509_certificate.h"
6
7 #include <CommonCrypto/CommonDigest.h>
8 #include <Security/Security.h>
9 #include <vector>
10
11 #include <cryptohi.h>
12 #include <cert.h>
13 #include <keyhi.h>
14 #include <nss.h>
15 #include <pk11pub.h>
16 #include <prerror.h>
17 #include <prtime.h>
18 #include <prtypes.h>
19 #include <secder.h>
20 #include <secerr.h>
21 #include <sslerr.h>
22
23 #include "base/logging.h"
24 #include "base/mac/scoped_cftyperef.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/pickle.h"
27 #include "base/time.h"
28 #include "crypto/nss_util.h"
29 #include "crypto/scoped_nss_types.h"
30 #include "net/base/asn1_util.h"
31 #include "net/base/cert_status_flags.h"
32 #include "net/base/cert_verify_result.h"
33 #include "net/base/ev_root_ca_metadata.h"
34 #include "net/base/net_errors.h"
35 #include "net/base/x509_util_ios.h"
36 #include "net/base/x509_util_nss.h"
37
38 using base::mac::ScopedCFTypeRef;
39
40 namespace net {
41 namespace {
42 // Returns true if a given |cert_handle| is actually a valid X.509 certificate
43 // handle.
44 //
45 // SecCertificateCreateFromData() does not always force the immediate parsing of
46 // the certificate, and as such, may return a SecCertificateRef for an
47 // invalid/unparsable certificate. Force parsing to occur to ensure that the
48 // SecCertificateRef is correct. On later versions where
49 // SecCertificateCreateFromData() immediately parses, rather than lazily, this
50 // call is cheap, as the subject is cached.
51 bool IsValidOSCertHandle(SecCertificateRef cert_handle) {
52 ScopedCFTypeRef<CFStringRef> sanity_check(
53 SecCertificateCopySubjectSummary(cert_handle));
54 return sanity_check != NULL;
55 }
56 } // namespace
57
58 void X509Certificate::Initialize() {
59 x509_util_ios::NSSCertificate nss_cert(cert_handle_);
60 CERTCertificate* cert_handle = nss_cert.cert_handle();
61 if (cert_handle) {
62 x509_util::ParsePrincipal(&cert_handle->subject, &subject_);
63 x509_util::ParsePrincipal(&cert_handle->issuer, &issuer_);
64 x509_util::ParseDate(&cert_handle->validity.notBefore, &valid_start_);
65 x509_util::ParseDate(&cert_handle->validity.notAfter, &valid_expiry_);
66 serial_number_ = x509_util::ParseSerialNumber(cert_handle);
67 }
68 fingerprint_ = CalculateFingerprint(cert_handle_);
69 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_);
70 }
71
72 // static
73 X509Certificate* X509Certificate::CreateSelfSigned(
74 crypto::RSAPrivateKey* key,
75 const std::string& subject,
76 uint32 serial_number,
77 base::TimeDelta valid_duration) {
78 DCHECK(key);
79 DCHECK(!subject.empty());
80 NOTIMPLEMENTED();
81 return NULL;
82 }
83
84 void X509Certificate::GetSubjectAltName(
85 std::vector<std::string>* dns_names,
86 std::vector<std::string>* ip_addrs) const {
87 x509_util_ios::NSSCertificate nss_cert(cert_handle_);
88 CERTCertificate* cert_handle = nss_cert.cert_handle();
89 if (!cert_handle) {
90 if (dns_names)
91 dns_names->clear();
92 if (ip_addrs)
93 ip_addrs->clear();
94 return;
95 }
96 x509_util::GetSubjectAltName(cert_handle, dns_names, ip_addrs);
97 }
98
99 // static
100 bool X509Certificate::GetDEREncoded(OSCertHandle cert_handle,
101 std::string* encoded) {
102 ScopedCFTypeRef<CFDataRef> der_data(SecCertificateCopyData(cert_handle));
103 if (!der_data)
104 return false;
105 encoded->assign(reinterpret_cast<const char*>(CFDataGetBytePtr(der_data)),
106 CFDataGetLength(der_data));
107 return true;
108 }
109
110 // static
111 bool X509Certificate::IsSameOSCert(X509Certificate::OSCertHandle a,
112 X509Certificate::OSCertHandle b) {
113 DCHECK(a && b);
114 if (a == b)
115 return true;
116 if (CFEqual(a, b))
117 return true;
118 ScopedCFTypeRef<CFDataRef> a_data(SecCertificateCopyData(a));
119 ScopedCFTypeRef<CFDataRef> b_data(SecCertificateCopyData(b));
120 return a_data && b_data &&
121 CFDataGetLength(a_data) == CFDataGetLength(b_data) &&
122 memcmp(CFDataGetBytePtr(a_data), CFDataGetBytePtr(b_data),
123 CFDataGetLength(a_data)) == 0;
124 }
125
126 // static
127 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes(
128 const char* data, int length) {
129 ScopedCFTypeRef<CFDataRef> cert_data(CFDataCreateWithBytesNoCopy(
130 kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(data), length,
131 kCFAllocatorNull));
132 if (!cert_data)
133 return NULL;
134 OSCertHandle cert_handle = SecCertificateCreateWithData(NULL, cert_data);
135 if (!cert_handle)
136 return NULL;
137 if (!IsValidOSCertHandle(cert_handle)) {
138 CFRelease(cert_handle);
139 return NULL;
140 }
141 return cert_handle;
142 }
143
144 // static
145 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes(
146 const char* data,
147 int length,
148 Format format) {
149 return x509_util::CreateOSCertHandlesFromBytes(data, length, format);
150 }
151
152 // static
153 X509Certificate::OSCertHandle X509Certificate::DupOSCertHandle(
154 OSCertHandle handle) {
155 if (!handle)
156 return NULL;
157 return reinterpret_cast<OSCertHandle>(const_cast<void*>(CFRetain(handle)));
158 }
159
160 // static
161 void X509Certificate::FreeOSCertHandle(OSCertHandle cert_handle) {
162 CFRelease(cert_handle);
163 }
164
165 // static
166 SHA1HashValue X509Certificate::CalculateFingerprint(
167 OSCertHandle cert) {
168 SHA1HashValue sha1;
169 memset(sha1.data, 0, sizeof(sha1.data));
170
171 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert));
172 if (!cert_data)
173 return sha1;
174 DCHECK(CFDataGetBytePtr(cert_data));
175 DCHECK_NE(0, CFDataGetLength(cert_data));
176 CC_SHA1(CFDataGetBytePtr(cert_data), CFDataGetLength(cert_data), sha1.data);
177
178 return sha1;
179 }
180
181 // static
182 SHA1HashValue X509Certificate::CalculateCAFingerprint(
183 const OSCertHandles& intermediates) {
184 SHA1HashValue sha1;
185 memset(sha1.data, 0, sizeof(sha1.data));
186
187 // The CC_SHA(3cc) man page says all CC_SHA1_xxx routines return 1, so
188 // we don't check their return values.
189 CC_SHA1_CTX sha1_ctx;
190 CC_SHA1_Init(&sha1_ctx);
191 for (size_t i = 0; i < intermediates.size(); ++i) {
192 ScopedCFTypeRef<CFDataRef>
193 cert_data(SecCertificateCopyData(intermediates[i]));
194 if (!cert_data)
195 return sha1;
196 CC_SHA1_Update(&sha1_ctx,
197 CFDataGetBytePtr(cert_data),
198 CFDataGetLength(cert_data));
199 }
200 CC_SHA1_Final(sha1.data, &sha1_ctx);
201 return sha1;
202 }
203
204 // static
205 X509Certificate::OSCertHandle
206 X509Certificate::ReadOSCertHandleFromPickle(PickleIterator* pickle_iter) {
207 return x509_util::ReadOSCertHandleFromPickle(pickle_iter);
208 }
209
210 // static
211 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle,
212 Pickle* pickle) {
213 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert_handle));
214 if (!cert_data)
215 return false;
216
217 return pickle->WriteData(
218 reinterpret_cast<const char*>(CFDataGetBytePtr(cert_data)),
219 CFDataGetLength(cert_data));
220 }
221
222 // static
223 void X509Certificate::GetPublicKeyInfo(OSCertHandle cert_handle,
224 size_t* size_bits,
225 PublicKeyType* type) {
226 x509_util_ios::NSSCertificate nss_cert(cert_handle);
227 x509_util::GetPublicKeyInfo(nss_cert.cert_handle(), size_bits, type);
228 }
229
230 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ev_root_ca_metadata.cc ('k') | net/base/x509_certificate_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698