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_certificate.h" | 5 #include "net/cert/x509_certificate.h" |
6 | 6 |
7 #include <blapi.h> // Implement CalculateChainFingerprint() with NSS. | 7 #include <blapi.h> // Implement CalculateChainFingerprint() with NSS. |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "crypto/capi_util.h" | 15 #include "crypto/capi_util.h" |
16 #include "crypto/rsa_private_key.h" | |
17 #include "crypto/scoped_capi_types.h" | 16 #include "crypto/scoped_capi_types.h" |
18 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
19 | 18 |
20 #pragma comment(lib, "crypt32.lib") | 19 #pragma comment(lib, "crypt32.lib") |
21 | 20 |
22 using base::Time; | 21 using base::Time; |
23 | 22 |
24 namespace net { | 23 namespace net { |
25 | 24 |
26 namespace { | 25 namespace { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_); | 157 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_); |
159 | 158 |
160 const CRYPT_INTEGER_BLOB* serial = &cert_handle_->pCertInfo->SerialNumber; | 159 const CRYPT_INTEGER_BLOB* serial = &cert_handle_->pCertInfo->SerialNumber; |
161 scoped_ptr<uint8[]> serial_bytes(new uint8[serial->cbData]); | 160 scoped_ptr<uint8[]> serial_bytes(new uint8[serial->cbData]); |
162 for (unsigned i = 0; i < serial->cbData; i++) | 161 for (unsigned i = 0; i < serial->cbData; i++) |
163 serial_bytes[i] = serial->pbData[serial->cbData - i - 1]; | 162 serial_bytes[i] = serial->pbData[serial->cbData - i - 1]; |
164 serial_number_ = std::string( | 163 serial_number_ = std::string( |
165 reinterpret_cast<char*>(serial_bytes.get()), serial->cbData); | 164 reinterpret_cast<char*>(serial_bytes.get()), serial->cbData); |
166 } | 165 } |
167 | 166 |
168 // static | |
169 X509Certificate* X509Certificate::CreateSelfSigned( | |
170 crypto::RSAPrivateKey* key, | |
171 const std::string& subject, | |
172 uint32 serial_number, | |
173 base::TimeDelta valid_duration) { | |
174 // Get the ASN.1 encoding of the certificate subject. | |
175 std::wstring w_subject = ASCIIToWide(subject); | |
176 DWORD encoded_subject_length = 0; | |
177 if (!CertStrToName( | |
178 X509_ASN_ENCODING, | |
179 w_subject.c_str(), | |
180 CERT_X500_NAME_STR, NULL, NULL, &encoded_subject_length, NULL)) { | |
181 return NULL; | |
182 } | |
183 | |
184 scoped_ptr<BYTE[]> encoded_subject(new BYTE[encoded_subject_length]); | |
185 if (!CertStrToName( | |
186 X509_ASN_ENCODING, | |
187 w_subject.c_str(), | |
188 CERT_X500_NAME_STR, NULL, | |
189 encoded_subject.get(), | |
190 &encoded_subject_length, NULL)) { | |
191 return NULL; | |
192 } | |
193 | |
194 CERT_NAME_BLOB subject_name; | |
195 memset(&subject_name, 0, sizeof(subject_name)); | |
196 subject_name.cbData = encoded_subject_length; | |
197 subject_name.pbData = encoded_subject.get(); | |
198 | |
199 CRYPT_ALGORITHM_IDENTIFIER sign_algo; | |
200 memset(&sign_algo, 0, sizeof(sign_algo)); | |
201 sign_algo.pszObjId = szOID_RSA_SHA1RSA; | |
202 | |
203 base::Time not_before = base::Time::Now(); | |
204 base::Time not_after = not_before + valid_duration; | |
205 base::Time::Exploded exploded; | |
206 | |
207 // Create the system time structs representing our exploded times. | |
208 not_before.UTCExplode(&exploded); | |
209 SYSTEMTIME start_time; | |
210 ExplodedTimeToSystemTime(exploded, &start_time); | |
211 not_after.UTCExplode(&exploded); | |
212 SYSTEMTIME end_time; | |
213 ExplodedTimeToSystemTime(exploded, &end_time); | |
214 | |
215 PCCERT_CONTEXT cert_handle = | |
216 CertCreateSelfSignCertificate(key->provider(), &subject_name, | |
217 CERT_CREATE_SELFSIGN_NO_KEY_INFO, NULL, | |
218 &sign_algo, &start_time, &end_time, NULL); | |
219 DCHECK(cert_handle) << "Failed to create self-signed certificate: " | |
220 << GetLastError(); | |
221 if (!cert_handle) | |
222 return NULL; | |
223 | |
224 X509Certificate* cert = CreateFromHandle(cert_handle, OSCertHandles()); | |
225 FreeOSCertHandle(cert_handle); | |
226 return cert; | |
227 } | |
228 | |
229 void X509Certificate::GetSubjectAltName( | 167 void X509Certificate::GetSubjectAltName( |
230 std::vector<std::string>* dns_names, | 168 std::vector<std::string>* dns_names, |
231 std::vector<std::string>* ip_addrs) const { | 169 std::vector<std::string>* ip_addrs) const { |
232 if (dns_names) | 170 if (dns_names) |
233 dns_names->clear(); | 171 dns_names->clear(); |
234 if (ip_addrs) | 172 if (ip_addrs) |
235 ip_addrs->clear(); | 173 ip_addrs->clear(); |
236 | 174 |
237 if (!cert_handle_) | 175 if (!cert_handle_) |
238 return; | 176 return; |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 if (IsCertNameBlobInIssuerList(&(*it)->pCertInfo->Issuer, | 434 if (IsCertNameBlobInIssuerList(&(*it)->pCertInfo->Issuer, |
497 valid_issuers)) { | 435 valid_issuers)) { |
498 return true; | 436 return true; |
499 } | 437 } |
500 } | 438 } |
501 | 439 |
502 return false; | 440 return false; |
503 } | 441 } |
504 | 442 |
505 } // namespace net | 443 } // namespace net |
OLD | NEW |