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

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: Added unittests and fixed compilation issues. 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
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 <cert.h>
8 #include <CommonCrypto/CommonDigest.h>
9 #include <cryptohi.h>
10 #include <keyhi.h>
11 #include <nss.h>
12 #include <pk11pub.h>
13 #include <prerror.h>
14 #include <prtime.h>
15 #include <prtypes.h>
16 #include <secder.h>
17 #include <secerr.h>
18 #include <Security/Security.h>
19 #include <sslerr.h>
20
21 #include <vector>
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_certificate_nss_util.h"
36 #include "net/base/x509_util_ios.h"
37
38 using base::mac::ScopedCFTypeRef;
39
40 namespace net {
41 namespace {
42 // Test that a given |cert_handle| is actually a valid X.509 certificate, and
43 // return true if it is.
44 //
45 // SecCertificateCreateFromData() does not return any errors if called with
46 // invalid data, as long as data is present. The actual decoding of the
47 // certificate does not happen until an API that requires a CSSM handle is
48 // called. Use SecCertificateCopySubjectSummary to check that the certificate
49 // parsed as a valid X.509 certificate.
50 bool IsValidOSCertHandle(SecCertificateRef cert_handle) {
51 ScopedCFTypeRef<CFStringRef> sanity_check(
52 SecCertificateCopySubjectSummary(cert_handle));
53 return sanity_check != NULL;
54 }
55 } // namespace
56
57 void X509Certificate::Initialize() {
58 x509_util::NSSCertificate nss_cert(cert_handle_);
59 CERTCertificate* cert_handle = nss_cert.cert_handle();
60
61 x509_certificate_nss_util::ParsePrincipal(&cert_handle->subject, &subject_);
62 x509_certificate_nss_util::ParsePrincipal(&cert_handle->issuer, &issuer_);
63
64 x509_certificate_nss_util::ParseDate(&cert_handle->validity.notBefore,
65 &valid_start_);
66 x509_certificate_nss_util::ParseDate(&cert_handle->validity.notAfter,
67 &valid_expiry_);
68
69 fingerprint_ = CalculateFingerprint(cert_handle_);
70 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_);
71
72 serial_number_ = x509_certificate_nss_util::ParseSerialNumber(cert_handle);
73 }
74
75 // static
76 X509Certificate* X509Certificate::CreateSelfSigned(
77 crypto::RSAPrivateKey* key,
78 const std::string& subject,
79 uint32 serial_number,
80 base::TimeDelta valid_duration) {
81 DCHECK(key);
82 DCHECK(!subject.empty());
83 NOTIMPLEMENTED();
84 return NULL;
85 }
86
87 void X509Certificate::GetSubjectAltName(
88 std::vector<std::string>* dns_names,
89 std::vector<std::string>* ip_addrs) const {
90 x509_util::NSSCertificate nss_cert(cert_handle_);
91 x509_certificate_nss_util::GetSubjectAltName(
92 nss_cert.cert_handle(), dns_names, ip_addrs);
93 }
94
95 // static
96 bool X509Certificate::GetDEREncoded(OSCertHandle cert_handle,
97 std::string* encoded) {
98 ScopedCFTypeRef<CFDataRef> der_data(SecCertificateCopyData(cert_handle));
99 if (!der_data)
100 return false;
101 encoded->assign(reinterpret_cast<const char*>(CFDataGetBytePtr(der_data)),
102 CFDataGetLength(der_data));
103 return true;
104 }
105
106 // static
107 bool X509Certificate::IsSameOSCert(X509Certificate::OSCertHandle a,
108 X509Certificate::OSCertHandle b) {
109 DCHECK(a && b);
110 if (a == b)
111 return true;
112 if (CFEqual(a, b))
113 return true;
114 ScopedCFTypeRef<CFDataRef> a_data(SecCertificateCopyData(a));
115 ScopedCFTypeRef<CFDataRef> b_data(SecCertificateCopyData(b));
116 return a_data && b_data &&
117 CFDataGetLength(a_data) == CFDataGetLength(b_data) &&
118 memcmp(CFDataGetBytePtr(a_data), CFDataGetBytePtr(b_data),
119 CFDataGetLength(a_data)) == 0;
120 }
121
122 // static
123 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes(
124 const char* data, int length) {
125 ScopedCFTypeRef<CFDataRef> cert_data(CFDataCreateWithBytesNoCopy(
126 kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(data), length,
127 kCFAllocatorNull));
128 if (!cert_data)
129 return NULL;
130 OSCertHandle cert_handle = SecCertificateCreateWithData(NULL, cert_data);
131 if (!cert_handle)
132 return NULL;
133 if (!IsValidOSCertHandle(cert_handle)) {
134 CFRelease(cert_handle);
135 return NULL;
136 }
137 return cert_handle;
138 }
139
140 // static
141 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes(
142 const char* data,
143 int length,
144 Format format) {
145 return x509_certificate_nss_util::CreateOSCertHandlesFromBytes(
146 data, length, format);
147 }
148
149 // static
150 X509Certificate::OSCertHandle X509Certificate::DupOSCertHandle(
151 OSCertHandle handle) {
152 if (!handle)
153 return NULL;
154 return reinterpret_cast<OSCertHandle>(const_cast<void*>(CFRetain(handle)));
155 }
156
157 // static
158 void X509Certificate::FreeOSCertHandle(OSCertHandle cert_handle) {
159 CFRelease(cert_handle);
160 }
161
162 // static
163 SHA1HashValue X509Certificate::CalculateFingerprint(
164 OSCertHandle cert) {
165 SHA1HashValue sha1;
166 memset(sha1.data, 0, sizeof(sha1.data));
167
168 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert));
169 if (!cert_data)
170 return sha1;
171 DCHECK(CFDataGetBytePtr(cert_data));
172 DCHECK_NE(CFDataGetLength(cert_data), 0);
173 CC_SHA1(CFDataGetBytePtr(cert_data), CFDataGetLength(cert_data), sha1.data);
174
175 return sha1;
176 }
177
178 // static
179 SHA1HashValue X509Certificate::CalculateCAFingerprint(
180 const OSCertHandles& intermediates) {
181 SHA1HashValue sha1;
182 memset(sha1.data, 0, sizeof(sha1.data));
183
184 // The CC_SHA(3cc) man page says all CC_SHA1_xxx routines return 1, so
185 // we don't check their return values.
186 CC_SHA1_CTX sha1_ctx;
187 CC_SHA1_Init(&sha1_ctx);
188 for (size_t i = 0; i < intermediates.size(); ++i) {
189 ScopedCFTypeRef<CFDataRef>
190 cert_data(SecCertificateCopyData(intermediates[i]));
191 if (!cert_data)
192 return sha1;
193 CC_SHA1_Update(&sha1_ctx,
194 CFDataGetBytePtr(cert_data),
195 CFDataGetLength(cert_data));
196 }
197 CC_SHA1_Final(sha1.data, &sha1_ctx);
198 return sha1;
199 }
200
201 // static
202 X509Certificate::OSCertHandle
203 X509Certificate::ReadOSCertHandleFromPickle(PickleIterator* pickle_iter) {
204 return x509_certificate_nss_util::ReadOSCertHandleFromPickle(pickle_iter);
205 }
206
207 // static
208 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle,
209 Pickle* pickle) {
210 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert_handle));
211 if (!cert_data)
212 return false;
213
214 return pickle->WriteData(
215 reinterpret_cast<const char*>(CFDataGetBytePtr(cert_data)),
216 CFDataGetLength(cert_data));
217 }
218
219 // static
220 void X509Certificate::GetPublicKeyInfo(OSCertHandle cert_handle,
221 size_t* size_bits,
222 PublicKeyType* type) {
223 x509_util::NSSCertificate nss_cert(cert_handle);
224 x509_certificate_nss_util::GetPublicKeyInfo(
225 nss_cert.cert_handle(), size_bits, type);
226 }
227
228 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ev_root_ca_metadata.cc ('k') | net/base/x509_certificate_nss.cc » ('j') | net/net.gyp » ('J')

Powered by Google App Engine
This is Rietveld 408576698