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.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 <nss.h> | 10 #include <nss.h> |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 for (int i = 0; i < num_certs; ++i) { | 266 for (int i = 0; i < num_certs; ++i) { |
267 X509Certificate::OSCertHandle handle = | 267 X509Certificate::OSCertHandle handle = |
268 X509Certificate::CreateOSCertHandleFromBytes( | 268 X509Certificate::CreateOSCertHandleFromBytes( |
269 reinterpret_cast<char*>(certs[i]->data), certs[i]->len); | 269 reinterpret_cast<char*>(certs[i]->data), certs[i]->len); |
270 if (handle) | 270 if (handle) |
271 results->push_back(handle); | 271 results->push_back(handle); |
272 } | 272 } |
273 | 273 |
274 return SECSuccess; | 274 return SECSuccess; |
275 } | 275 } |
| 276 |
| 277 typedef scoped_ptr_malloc< |
| 278 CERTName, |
| 279 crypto::NSSDestroyer<CERTName, CERT_DestroyName> > ScopedCERTName; |
| 280 |
| 281 // Create a new CERTName object from its encoded representation. |
| 282 // |arena| is the allocation pool to use. |
| 283 // |data| points to a DER-encoded X.509 DistinguishedName. |
| 284 // Return a new CERTName pointer on success, or NULL. |
| 285 CERTName* CreateCertNameFromEncoded(PLArenaPool* arena, |
| 286 const base::StringPiece& data) { |
| 287 if (!arena) |
| 288 return NULL; |
| 289 |
| 290 ScopedCERTName name(PORT_ArenaZNew(arena, CERTName)); |
| 291 if (!name.get()) |
| 292 return NULL; |
| 293 |
| 294 SECItem item; |
| 295 item.len = static_cast<unsigned int>(data.length()); |
| 296 item.data = reinterpret_cast<unsigned char*>( |
| 297 const_cast<char*>(data.data())); |
| 298 |
| 299 SECStatus rv = SEC_ASN1DecodeItem( |
| 300 arena, name.get(), SEC_ASN1_GET(CERT_NameTemplate), &item); |
| 301 if (rv != SECSuccess) |
| 302 return NULL; |
| 303 |
| 304 return name.release(); |
| 305 } |
| 306 |
276 #endif // defined(USE_NSS) || defined(OS_IOS) | 307 #endif // defined(USE_NSS) || defined(OS_IOS) |
277 | 308 |
278 } // namespace | 309 } // namespace |
279 | 310 |
280 namespace x509_util { | 311 namespace x509_util { |
281 | 312 |
282 CERTCertificate* CreateSelfSignedCert( | 313 CERTCertificate* CreateSelfSignedCert( |
283 SECKEYPublicKey* public_key, | 314 SECKEYPublicKey* public_key, |
284 SECKEYPrivateKey* private_key, | 315 SECKEYPrivateKey* private_key, |
285 const std::string& subject, | 316 const std::string& subject, |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 break; | 551 break; |
521 case ecKey: | 552 case ecKey: |
522 *type = X509Certificate::kPublicKeyTypeECDSA; | 553 *type = X509Certificate::kPublicKeyTypeECDSA; |
523 break; | 554 break; |
524 default: | 555 default: |
525 *type = X509Certificate::kPublicKeyTypeUnknown; | 556 *type = X509Certificate::kPublicKeyTypeUnknown; |
526 *size_bits = 0; | 557 *size_bits = 0; |
527 break; | 558 break; |
528 } | 559 } |
529 } | 560 } |
| 561 |
| 562 bool GetIssuersFromEncodedList( |
| 563 const std::vector<std::string>& encoded_issuers, |
| 564 PLArenaPool* arena, |
| 565 std::vector<CERTName*>* out) { |
| 566 std::vector<CERTName*> result; |
| 567 for (size_t n = 0; n < encoded_issuers.size(); ++n) { |
| 568 CERTName* name = CreateCertNameFromEncoded(arena, encoded_issuers[n]); |
| 569 if (name != NULL) |
| 570 result.push_back(name); |
| 571 } |
| 572 |
| 573 if (result.size() == encoded_issuers.size()) { |
| 574 out->swap(result); |
| 575 return true; |
| 576 } |
| 577 |
| 578 for (size_t n = 0; n < result.size(); ++n) |
| 579 CERT_DestroyName(result[n]); |
| 580 return false; |
| 581 } |
| 582 |
| 583 |
| 584 bool IsCertificateIssuedBy(const std::vector<CERTCertificate*>& cert_chain, |
| 585 const std::vector<CERTName*>& valid_issuers) { |
| 586 for (size_t n = 0; n < cert_chain.size(); ++n) { |
| 587 CERTName* cert_issuer = &cert_chain[n]->issuer; |
| 588 for (size_t i = 0; i < valid_issuers.size(); ++i) { |
| 589 if (CERT_CompareName(valid_issuers[i], cert_issuer) == SECEqual) |
| 590 return true; |
| 591 } |
| 592 } |
| 593 return false; |
| 594 } |
| 595 |
530 #endif // defined(USE_NSS) || defined(OS_IOS) | 596 #endif // defined(USE_NSS) || defined(OS_IOS) |
531 | 597 |
532 } // namespace x509_util | 598 } // namespace x509_util |
533 | 599 |
534 } // namespace net | 600 } // namespace net |
OLD | NEW |