Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "net/base/x509_util.h" |
| 6 #include "net/base/x509_util_nss.h" | 6 #include "net/base/x509_util_nss.h" |
| 7 | 7 |
| 8 #include <cert.h> | 8 #include <cert.h> |
| 9 #include <cryptohi.h> | 9 #include <cryptohi.h> |
| 10 #include <pk11pub.h> | 10 #include <pk11pub.h> |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 | 71 |
| 72 // Creates a Certificate object that may be passed to the SignCertificate | 72 // Creates a Certificate object that may be passed to the SignCertificate |
| 73 // method to generate an X509 certificate. | 73 // method to generate an X509 certificate. |
| 74 // Returns NULL if an error is encountered in the certificate creation | 74 // Returns NULL if an error is encountered in the certificate creation |
| 75 // process. | 75 // process. |
| 76 // Caller responsible for freeing returned certificate object. | 76 // Caller responsible for freeing returned certificate object. |
| 77 CERTCertificate* CreateCertificate( | 77 CERTCertificate* CreateCertificate( |
| 78 SECKEYPublicKey* public_key, | 78 SECKEYPublicKey* public_key, |
| 79 const std::string& subject, | 79 const std::string& subject, |
| 80 uint32 serial_number, | 80 uint32 serial_number, |
| 81 base::TimeDelta valid_duration) { | 81 base::Time not_valid_before, |
| 82 base::Time not_valid_after) { | |
| 82 // Create info about public key. | 83 // Create info about public key. |
| 83 CERTSubjectPublicKeyInfo* spki = | 84 CERTSubjectPublicKeyInfo* spki = |
| 84 SECKEY_CreateSubjectPublicKeyInfo(public_key); | 85 SECKEY_CreateSubjectPublicKeyInfo(public_key); |
| 85 if (!spki) | 86 if (!spki) |
| 86 return NULL; | 87 return NULL; |
| 87 | 88 |
| 88 // Create the certificate request. | 89 // Create the certificate request. |
| 89 CERTName* subject_name = | 90 CERTName* subject_name = |
| 90 CERT_AsciiToName(const_cast<char*>(subject.c_str())); | 91 CERT_AsciiToName(const_cast<char*>(subject.c_str())); |
| 91 CERTCertificateRequest* cert_request = | 92 CERTCertificateRequest* cert_request = |
| 92 CERT_CreateCertificateRequest(subject_name, spki, NULL); | 93 CERT_CreateCertificateRequest(subject_name, spki, NULL); |
| 93 SECKEY_DestroySubjectPublicKeyInfo(spki); | 94 SECKEY_DestroySubjectPublicKeyInfo(spki); |
| 94 | 95 |
| 95 if (!cert_request) { | 96 if (!cert_request) { |
| 96 PRErrorCode prerr = PR_GetError(); | 97 PRErrorCode prerr = PR_GetError(); |
| 97 LOG(ERROR) << "Failed to create certificate request: " << prerr; | 98 LOG(ERROR) << "Failed to create certificate request: " << prerr; |
| 98 CERT_DestroyName(subject_name); | 99 CERT_DestroyName(subject_name); |
| 99 return NULL; | 100 return NULL; |
| 100 } | 101 } |
| 101 | 102 |
| 102 PRTime now = PR_Now(); | 103 CERTValidity* validity = CERT_CreateValidity( |
| 103 PRTime not_after = now + valid_duration.InMicroseconds(); | 104 crypto::BaseTimeToPRTime(not_valid_before), |
| 104 | 105 crypto::BaseTimeToPRTime(not_valid_after)); |
| 105 // Note that the time is now in micro-second unit. | |
| 106 CERTValidity* validity = CERT_CreateValidity(now, not_after); | |
| 107 CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name, | 106 CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name, |
| 108 validity, cert_request); | 107 validity, cert_request); |
| 109 if (!cert) { | 108 if (!cert) { |
| 110 PRErrorCode prerr = PR_GetError(); | 109 PRErrorCode prerr = PR_GetError(); |
| 111 LOG(ERROR) << "Failed to create certificate: " << prerr; | 110 LOG(ERROR) << "Failed to create certificate: " << prerr; |
| 112 } | 111 } |
| 113 | 112 |
| 114 // Cleanup for resources used to generate the cert. | 113 // Cleanup for resources used to generate the cert. |
| 115 CERT_DestroyName(subject_name); | 114 CERT_DestroyName(subject_name); |
| 116 CERT_DestroyValidity(validity); | 115 CERT_DestroyValidity(validity); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 cert->derCert = *result; | 168 cert->derCert = *result; |
| 170 | 169 |
| 171 return true; | 170 return true; |
| 172 } | 171 } |
| 173 | 172 |
| 174 bool CreateOriginBoundCertInternal( | 173 bool CreateOriginBoundCertInternal( |
| 175 SECKEYPublicKey* public_key, | 174 SECKEYPublicKey* public_key, |
| 176 SECKEYPrivateKey* private_key, | 175 SECKEYPrivateKey* private_key, |
| 177 const std::string& origin, | 176 const std::string& origin, |
| 178 uint32 serial_number, | 177 uint32 serial_number, |
| 179 base::TimeDelta valid_duration, | 178 base::Time not_valid_before, |
| 179 base::Time not_valid_after, | |
| 180 std::string* der_cert) { | 180 std::string* der_cert) { |
| 181 | 181 |
| 182 CERTCertificate* cert = CreateCertificate(public_key, | 182 CERTCertificate* cert = CreateCertificate(public_key, |
| 183 "CN=anonymous.invalid", | 183 "CN=anonymous.invalid", |
| 184 serial_number, | 184 serial_number, |
| 185 valid_duration); | 185 not_valid_before, |
| 186 not_valid_after); | |
| 186 | 187 |
| 187 if (!cert) | 188 if (!cert) |
| 188 return false; | 189 return false; |
| 189 | 190 |
| 190 // Create opaque handle used to add extensions later. | 191 // Create opaque handle used to add extensions later. |
| 191 void* cert_handle; | 192 void* cert_handle; |
| 192 if ((cert_handle = CERT_StartCertExtensions(cert)) == NULL) { | 193 if ((cert_handle = CERT_StartCertExtensions(cert)) == NULL) { |
| 193 LOG(ERROR) << "Unable to get opaque handle for adding extensions"; | 194 LOG(ERROR) << "Unable to get opaque handle for adding extensions"; |
| 194 CERT_DestroyCertificate(cert); | 195 CERT_DestroyCertificate(cert); |
| 195 return false; | 196 return false; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 | 248 |
| 248 namespace net { | 249 namespace net { |
| 249 | 250 |
| 250 namespace x509_util { | 251 namespace x509_util { |
| 251 | 252 |
| 252 CERTCertificate* CreateSelfSignedCert( | 253 CERTCertificate* CreateSelfSignedCert( |
| 253 SECKEYPublicKey* public_key, | 254 SECKEYPublicKey* public_key, |
| 254 SECKEYPrivateKey* private_key, | 255 SECKEYPrivateKey* private_key, |
| 255 const std::string& subject, | 256 const std::string& subject, |
| 256 uint32 serial_number, | 257 uint32 serial_number, |
| 257 base::TimeDelta valid_duration) { | 258 base::TimeDelta valid_duration) { |
|
wtc
2011/12/20 19:46:55
Is it more convenient for the CreateSelfSignedCert
mattm
2011/12/20 20:38:55
Yeah, I was initially looking at updating this all
| |
| 259 base::Time not_valid_before = base::Time::Now(); | |
| 260 base::Time not_valid_after = not_valid_before + valid_duration; | |
| 258 CERTCertificate* cert = CreateCertificate(public_key, | 261 CERTCertificate* cert = CreateCertificate(public_key, |
| 259 subject, | 262 subject, |
| 260 serial_number, | 263 serial_number, |
| 261 valid_duration); | 264 not_valid_before, |
| 265 not_valid_after); | |
| 262 if (!cert) | 266 if (!cert) |
| 263 return NULL; | 267 return NULL; |
| 264 | 268 |
| 265 if (!SignCertificate(cert, private_key)) { | 269 if (!SignCertificate(cert, private_key)) { |
| 266 CERT_DestroyCertificate(cert); | 270 CERT_DestroyCertificate(cert); |
| 267 return NULL; | 271 return NULL; |
| 268 } | 272 } |
| 269 | 273 |
| 270 return cert; | 274 return cert; |
| 271 } | 275 } |
| 272 | 276 |
| 273 bool CreateOriginBoundCertRSA( | 277 bool CreateOriginBoundCertRSA( |
| 274 crypto::RSAPrivateKey* key, | 278 crypto::RSAPrivateKey* key, |
| 275 const std::string& origin, | 279 const std::string& origin, |
| 276 uint32 serial_number, | 280 uint32 serial_number, |
| 277 base::TimeDelta valid_duration, | 281 base::Time not_valid_before, |
| 282 base::Time not_valid_after, | |
| 278 std::string* der_cert) { | 283 std::string* der_cert) { |
| 279 DCHECK(key); | 284 DCHECK(key); |
| 280 | 285 |
| 281 SECKEYPublicKey* public_key; | 286 SECKEYPublicKey* public_key; |
| 282 SECKEYPrivateKey* private_key; | 287 SECKEYPrivateKey* private_key; |
| 283 #if defined(USE_NSS) | 288 #if defined(USE_NSS) |
| 284 public_key = key->public_key(); | 289 public_key = key->public_key(); |
| 285 private_key = key->key(); | 290 private_key = key->key(); |
| 286 #else | 291 #else |
| 287 crypto::ScopedSECKEYPublicKey scoped_public_key; | 292 crypto::ScopedSECKEYPublicKey scoped_public_key; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 return NULL; | 327 return NULL; |
| 323 } | 328 } |
| 324 scoped_public_key.reset(public_key); | 329 scoped_public_key.reset(public_key); |
| 325 } | 330 } |
| 326 #endif | 331 #endif |
| 327 | 332 |
| 328 return CreateOriginBoundCertInternal(public_key, | 333 return CreateOriginBoundCertInternal(public_key, |
| 329 private_key, | 334 private_key, |
| 330 origin, | 335 origin, |
| 331 serial_number, | 336 serial_number, |
| 332 valid_duration, | 337 not_valid_before, |
| 338 not_valid_after, | |
| 333 der_cert); | 339 der_cert); |
| 334 } | 340 } |
| 335 | 341 |
| 336 bool CreateOriginBoundCertEC( | 342 bool CreateOriginBoundCertEC( |
| 337 crypto::ECPrivateKey* key, | 343 crypto::ECPrivateKey* key, |
| 338 const std::string& origin, | 344 const std::string& origin, |
| 339 uint32 serial_number, | 345 uint32 serial_number, |
| 340 base::TimeDelta valid_duration, | 346 base::Time not_valid_before, |
| 347 base::Time not_valid_after, | |
| 341 std::string* der_cert) { | 348 std::string* der_cert) { |
| 342 DCHECK(key); | 349 DCHECK(key); |
| 343 return CreateOriginBoundCertInternal(key->public_key(), | 350 return CreateOriginBoundCertInternal(key->public_key(), |
| 344 key->key(), | 351 key->key(), |
| 345 origin, | 352 origin, |
| 346 serial_number, | 353 serial_number, |
| 347 valid_duration, | 354 not_valid_before, |
| 355 not_valid_after, | |
| 348 der_cert); | 356 der_cert); |
| 349 } | 357 } |
| 350 | 358 |
| 351 } // namespace x509_util | 359 } // namespace x509_util |
| 352 | 360 |
| 353 } // namespace net | 361 } // namespace net |
| OLD | NEW |