| OLD | NEW |
| (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_util.h" | |
| 6 #include "net/base/x509_util_nss.h" | |
| 7 | |
| 8 #include <cert.h> | |
| 9 #include <cryptohi.h> | |
| 10 #include <nss.h> | |
| 11 #include <pk11pub.h> | |
| 12 #include <prerror.h> | |
| 13 #include <secder.h> | |
| 14 #include <secmod.h> | |
| 15 #include <secport.h> | |
| 16 | |
| 17 #include "base/debug/leak_annotations.h" | |
| 18 #include "base/logging.h" | |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/memory/singleton.h" | |
| 21 #include "base/pickle.h" | |
| 22 #include "crypto/ec_private_key.h" | |
| 23 #include "crypto/nss_util.h" | |
| 24 #include "crypto/nss_util_internal.h" | |
| 25 #include "crypto/scoped_nss_types.h" | |
| 26 #include "crypto/third_party/nss/chromium-nss.h" | |
| 27 #include "net/base/x509_certificate.h" | |
| 28 | |
| 29 namespace net { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 class DomainBoundCertOIDWrapper { | |
| 34 public: | |
| 35 static DomainBoundCertOIDWrapper* GetInstance() { | |
| 36 // Instantiated as a leaky singleton to allow the singleton to be | |
| 37 // constructed on a worker thead that is not joined when a process | |
| 38 // shuts down. | |
| 39 return Singleton<DomainBoundCertOIDWrapper, | |
| 40 LeakySingletonTraits<DomainBoundCertOIDWrapper> >::get(); | |
| 41 } | |
| 42 | |
| 43 SECOidTag domain_bound_cert_oid_tag() const { | |
| 44 return domain_bound_cert_oid_tag_; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 friend struct DefaultSingletonTraits<DomainBoundCertOIDWrapper>; | |
| 49 | |
| 50 DomainBoundCertOIDWrapper(); | |
| 51 | |
| 52 SECOidTag domain_bound_cert_oid_tag_; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(DomainBoundCertOIDWrapper); | |
| 55 }; | |
| 56 | |
| 57 DomainBoundCertOIDWrapper::DomainBoundCertOIDWrapper() | |
| 58 : domain_bound_cert_oid_tag_(SEC_OID_UNKNOWN) { | |
| 59 // 1.3.6.1.4.1.11129.2.1.6 | |
| 60 // (iso.org.dod.internet.private.enterprises.google.googleSecurity. | |
| 61 // certificateExtensions.originBoundCertificate) | |
| 62 static const uint8 kObCertOID[] = { | |
| 63 0x2b, 0x06, 0x01, 0x04, 0x01, 0xd6, 0x79, 0x02, 0x01, 0x06 | |
| 64 }; | |
| 65 SECOidData oid_data; | |
| 66 memset(&oid_data, 0, sizeof(oid_data)); | |
| 67 oid_data.oid.data = const_cast<uint8*>(kObCertOID); | |
| 68 oid_data.oid.len = sizeof(kObCertOID); | |
| 69 oid_data.offset = SEC_OID_UNKNOWN; | |
| 70 oid_data.desc = "Origin Bound Certificate"; | |
| 71 oid_data.mechanism = CKM_INVALID_MECHANISM; | |
| 72 oid_data.supportedExtension = SUPPORTED_CERT_EXTENSION; | |
| 73 domain_bound_cert_oid_tag_ = SECOID_AddEntry(&oid_data); | |
| 74 if (domain_bound_cert_oid_tag_ == SEC_OID_UNKNOWN) | |
| 75 LOG(ERROR) << "OB_CERT OID tag creation failed"; | |
| 76 } | |
| 77 | |
| 78 // Creates a Certificate object that may be passed to the SignCertificate | |
| 79 // method to generate an X509 certificate. | |
| 80 // Returns NULL if an error is encountered in the certificate creation | |
| 81 // process. | |
| 82 // Caller responsible for freeing returned certificate object. | |
| 83 CERTCertificate* CreateCertificate( | |
| 84 SECKEYPublicKey* public_key, | |
| 85 const std::string& subject, | |
| 86 uint32 serial_number, | |
| 87 base::Time not_valid_before, | |
| 88 base::Time not_valid_after) { | |
| 89 // Create info about public key. | |
| 90 CERTSubjectPublicKeyInfo* spki = | |
| 91 SECKEY_CreateSubjectPublicKeyInfo(public_key); | |
| 92 if (!spki) | |
| 93 return NULL; | |
| 94 | |
| 95 // Create the certificate request. | |
| 96 CERTName* subject_name = | |
| 97 CERT_AsciiToName(const_cast<char*>(subject.c_str())); | |
| 98 CERTCertificateRequest* cert_request = | |
| 99 CERT_CreateCertificateRequest(subject_name, spki, NULL); | |
| 100 SECKEY_DestroySubjectPublicKeyInfo(spki); | |
| 101 | |
| 102 if (!cert_request) { | |
| 103 PRErrorCode prerr = PR_GetError(); | |
| 104 LOG(ERROR) << "Failed to create certificate request: " << prerr; | |
| 105 CERT_DestroyName(subject_name); | |
| 106 return NULL; | |
| 107 } | |
| 108 | |
| 109 CERTValidity* validity = CERT_CreateValidity( | |
| 110 crypto::BaseTimeToPRTime(not_valid_before), | |
| 111 crypto::BaseTimeToPRTime(not_valid_after)); | |
| 112 if (!validity) { | |
| 113 PRErrorCode prerr = PR_GetError(); | |
| 114 LOG(ERROR) << "Failed to create certificate validity object: " << prerr; | |
| 115 CERT_DestroyName(subject_name); | |
| 116 CERT_DestroyCertificateRequest(cert_request); | |
| 117 return NULL; | |
| 118 } | |
| 119 CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name, | |
| 120 validity, cert_request); | |
| 121 if (!cert) { | |
| 122 PRErrorCode prerr = PR_GetError(); | |
| 123 LOG(ERROR) << "Failed to create certificate: " << prerr; | |
| 124 } | |
| 125 | |
| 126 // Cleanup for resources used to generate the cert. | |
| 127 CERT_DestroyName(subject_name); | |
| 128 CERT_DestroyValidity(validity); | |
| 129 CERT_DestroyCertificateRequest(cert_request); | |
| 130 | |
| 131 return cert; | |
| 132 } | |
| 133 | |
| 134 // Signs a certificate object, with |key| generating a new X509Certificate | |
| 135 // and destroying the passed certificate object (even when NULL is returned). | |
| 136 // The logic of this method references SignCert() in NSS utility certutil: | |
| 137 // http://mxr.mozilla.org/security/ident?i=SignCert. | |
| 138 // Returns true on success or false if an error is encountered in the | |
| 139 // certificate signing process. | |
| 140 bool SignCertificate( | |
| 141 CERTCertificate* cert, | |
| 142 SECKEYPrivateKey* key) { | |
| 143 // |arena| is used to encode the cert. | |
| 144 PLArenaPool* arena = cert->arena; | |
| 145 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->keyType, | |
| 146 SEC_OID_SHA1); | |
| 147 if (algo_id == SEC_OID_UNKNOWN) | |
| 148 return false; | |
| 149 | |
| 150 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0); | |
| 151 if (rv != SECSuccess) | |
| 152 return false; | |
| 153 | |
| 154 // Generate a cert of version 3. | |
| 155 *(cert->version.data) = 2; | |
| 156 cert->version.len = 1; | |
| 157 | |
| 158 SECItem der = { siBuffer, NULL, 0 }; | |
| 159 | |
| 160 // Use ASN1 DER to encode the cert. | |
| 161 void* encode_result = SEC_ASN1EncodeItem( | |
| 162 NULL, &der, cert, SEC_ASN1_GET(CERT_CertificateTemplate)); | |
| 163 if (!encode_result) | |
| 164 return false; | |
| 165 | |
| 166 // Allocate space to contain the signed cert. | |
| 167 SECItem result = { siBuffer, NULL, 0 }; | |
| 168 | |
| 169 // Sign the ASN1 encoded cert and save it to |result|. | |
| 170 rv = DerSignData(arena, &result, &der, key, algo_id); | |
| 171 PORT_Free(der.data); | |
| 172 if (rv != SECSuccess) { | |
| 173 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); | |
| 174 return false; | |
| 175 } | |
| 176 | |
| 177 // Save the signed result to the cert. | |
| 178 cert->derCert = result; | |
| 179 | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 bool CreateDomainBoundCertInternal( | |
| 184 SECKEYPublicKey* public_key, | |
| 185 SECKEYPrivateKey* private_key, | |
| 186 const std::string& domain, | |
| 187 uint32 serial_number, | |
| 188 base::Time not_valid_before, | |
| 189 base::Time not_valid_after, | |
| 190 std::string* der_cert) { | |
| 191 CERTCertificate* cert = CreateCertificate(public_key, | |
| 192 "CN=anonymous.invalid", | |
| 193 serial_number, | |
| 194 not_valid_before, | |
| 195 not_valid_after); | |
| 196 | |
| 197 if (!cert) | |
| 198 return false; | |
| 199 | |
| 200 // Create opaque handle used to add extensions later. | |
| 201 void* cert_handle; | |
| 202 if ((cert_handle = CERT_StartCertExtensions(cert)) == NULL) { | |
| 203 LOG(ERROR) << "Unable to get opaque handle for adding extensions"; | |
| 204 CERT_DestroyCertificate(cert); | |
| 205 return false; | |
| 206 } | |
| 207 | |
| 208 // Create SECItem for IA5String encoding. | |
| 209 SECItem domain_string_item = { | |
| 210 siAsciiString, | |
| 211 (unsigned char*)domain.data(), | |
| 212 static_cast<unsigned>(domain.size()) | |
| 213 }; | |
| 214 | |
| 215 // IA5Encode and arena allocate SECItem | |
| 216 SECItem* asn1_domain_string = SEC_ASN1EncodeItem( | |
| 217 cert->arena, NULL, &domain_string_item, | |
| 218 SEC_ASN1_GET(SEC_IA5StringTemplate)); | |
| 219 if (asn1_domain_string == NULL) { | |
| 220 LOG(ERROR) << "Unable to get ASN1 encoding for domain in domain_bound_cert" | |
| 221 " extension"; | |
| 222 CERT_DestroyCertificate(cert); | |
| 223 return false; | |
| 224 } | |
| 225 | |
| 226 // Add the extension to the opaque handle | |
| 227 if (CERT_AddExtension( | |
| 228 cert_handle, | |
| 229 DomainBoundCertOIDWrapper::GetInstance()->domain_bound_cert_oid_tag(), | |
| 230 asn1_domain_string, PR_TRUE, PR_TRUE) != SECSuccess){ | |
| 231 LOG(ERROR) << "Unable to add domain bound cert extension to opaque handle"; | |
| 232 CERT_DestroyCertificate(cert); | |
| 233 return false; | |
| 234 } | |
| 235 | |
| 236 // Copy extension into x509 cert | |
| 237 if (CERT_FinishExtensions(cert_handle) != SECSuccess){ | |
| 238 LOG(ERROR) << "Unable to copy extension to X509 cert"; | |
| 239 CERT_DestroyCertificate(cert); | |
| 240 return false; | |
| 241 } | |
| 242 | |
| 243 if (!SignCertificate(cert, private_key)) { | |
| 244 CERT_DestroyCertificate(cert); | |
| 245 return false; | |
| 246 } | |
| 247 | |
| 248 DCHECK(cert->derCert.len); | |
| 249 // XXX copied from X509Certificate::GetDEREncoded | |
| 250 der_cert->clear(); | |
| 251 der_cert->append(reinterpret_cast<char*>(cert->derCert.data), | |
| 252 cert->derCert.len); | |
| 253 CERT_DestroyCertificate(cert); | |
| 254 return true; | |
| 255 } | |
| 256 | |
| 257 #if defined(USE_NSS) || defined(OS_IOS) | |
| 258 // Callback for CERT_DecodeCertPackage(), used in | |
| 259 // CreateOSCertHandlesFromBytes(). | |
| 260 SECStatus PR_CALLBACK CollectCertsCallback(void* arg, | |
| 261 SECItem** certs, | |
| 262 int num_certs) { | |
| 263 X509Certificate::OSCertHandles* results = | |
| 264 reinterpret_cast<X509Certificate::OSCertHandles*>(arg); | |
| 265 | |
| 266 for (int i = 0; i < num_certs; ++i) { | |
| 267 X509Certificate::OSCertHandle handle = | |
| 268 X509Certificate::CreateOSCertHandleFromBytes( | |
| 269 reinterpret_cast<char*>(certs[i]->data), certs[i]->len); | |
| 270 if (handle) | |
| 271 results->push_back(handle); | |
| 272 } | |
| 273 | |
| 274 return SECSuccess; | |
| 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 | |
| 307 #endif // defined(USE_NSS) || defined(OS_IOS) | |
| 308 | |
| 309 } // namespace | |
| 310 | |
| 311 namespace x509_util { | |
| 312 | |
| 313 CERTCertificate* CreateSelfSignedCert( | |
| 314 SECKEYPublicKey* public_key, | |
| 315 SECKEYPrivateKey* private_key, | |
| 316 const std::string& subject, | |
| 317 uint32 serial_number, | |
| 318 base::Time not_valid_before, | |
| 319 base::Time not_valid_after) { | |
| 320 CERTCertificate* cert = CreateCertificate(public_key, | |
| 321 subject, | |
| 322 serial_number, | |
| 323 not_valid_before, | |
| 324 not_valid_after); | |
| 325 if (!cert) | |
| 326 return NULL; | |
| 327 | |
| 328 if (!SignCertificate(cert, private_key)) { | |
| 329 CERT_DestroyCertificate(cert); | |
| 330 return NULL; | |
| 331 } | |
| 332 | |
| 333 return cert; | |
| 334 } | |
| 335 | |
| 336 bool IsSupportedValidityRange(base::Time not_valid_before, | |
| 337 base::Time not_valid_after) { | |
| 338 CERTValidity* validity = CERT_CreateValidity( | |
| 339 crypto::BaseTimeToPRTime(not_valid_before), | |
| 340 crypto::BaseTimeToPRTime(not_valid_after)); | |
| 341 | |
| 342 if (!validity) | |
| 343 return false; | |
| 344 | |
| 345 CERT_DestroyValidity(validity); | |
| 346 return true; | |
| 347 } | |
| 348 | |
| 349 bool CreateDomainBoundCertEC( | |
| 350 crypto::ECPrivateKey* key, | |
| 351 const std::string& domain, | |
| 352 uint32 serial_number, | |
| 353 base::Time not_valid_before, | |
| 354 base::Time not_valid_after, | |
| 355 std::string* der_cert) { | |
| 356 DCHECK(key); | |
| 357 return CreateDomainBoundCertInternal(key->public_key(), | |
| 358 key->key(), | |
| 359 domain, | |
| 360 serial_number, | |
| 361 not_valid_before, | |
| 362 not_valid_after, | |
| 363 der_cert); | |
| 364 } | |
| 365 | |
| 366 #if defined(USE_NSS) || defined(OS_IOS) | |
| 367 void ParsePrincipal(CERTName* name, CertPrincipal* principal) { | |
| 368 typedef char* (*CERTGetNameFunc)(CERTName* name); | |
| 369 | |
| 370 // TODO(jcampan): add business_category and serial_number. | |
| 371 // TODO(wtc): NSS has the CERT_GetOrgName, CERT_GetOrgUnitName, and | |
| 372 // CERT_GetDomainComponentName functions, but they return only the most | |
| 373 // general (the first) RDN. NSS doesn't have a function for the street | |
| 374 // address. | |
| 375 static const SECOidTag kOIDs[] = { | |
| 376 SEC_OID_AVA_STREET_ADDRESS, | |
| 377 SEC_OID_AVA_ORGANIZATION_NAME, | |
| 378 SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME, | |
| 379 SEC_OID_AVA_DC }; | |
| 380 | |
| 381 std::vector<std::string>* values[] = { | |
| 382 &principal->street_addresses, | |
| 383 &principal->organization_names, | |
| 384 &principal->organization_unit_names, | |
| 385 &principal->domain_components }; | |
| 386 DCHECK_EQ(arraysize(kOIDs), arraysize(values)); | |
| 387 | |
| 388 CERTRDN** rdns = name->rdns; | |
| 389 for (size_t rdn = 0; rdns[rdn]; ++rdn) { | |
| 390 CERTAVA** avas = rdns[rdn]->avas; | |
| 391 for (size_t pair = 0; avas[pair] != 0; ++pair) { | |
| 392 SECOidTag tag = CERT_GetAVATag(avas[pair]); | |
| 393 for (size_t oid = 0; oid < arraysize(kOIDs); ++oid) { | |
| 394 if (kOIDs[oid] == tag) { | |
| 395 SECItem* decode_item = CERT_DecodeAVAValue(&avas[pair]->value); | |
| 396 if (!decode_item) | |
| 397 break; | |
| 398 // TODO(wtc): Pass decode_item to CERT_RFC1485_EscapeAndQuote. | |
| 399 std::string value(reinterpret_cast<char*>(decode_item->data), | |
| 400 decode_item->len); | |
| 401 values[oid]->push_back(value); | |
| 402 SECITEM_FreeItem(decode_item, PR_TRUE); | |
| 403 break; | |
| 404 } | |
| 405 } | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 // Get CN, L, S, and C. | |
| 410 CERTGetNameFunc get_name_funcs[4] = { | |
| 411 CERT_GetCommonName, CERT_GetLocalityName, | |
| 412 CERT_GetStateName, CERT_GetCountryName }; | |
| 413 std::string* single_values[4] = { | |
| 414 &principal->common_name, &principal->locality_name, | |
| 415 &principal->state_or_province_name, &principal->country_name }; | |
| 416 for (size_t i = 0; i < arraysize(get_name_funcs); ++i) { | |
| 417 char* value = get_name_funcs[i](name); | |
| 418 if (value) { | |
| 419 single_values[i]->assign(value); | |
| 420 PORT_Free(value); | |
| 421 } | |
| 422 } | |
| 423 } | |
| 424 | |
| 425 void ParseDate(const SECItem* der_date, base::Time* result) { | |
| 426 PRTime prtime; | |
| 427 SECStatus rv = DER_DecodeTimeChoice(&prtime, der_date); | |
| 428 DCHECK_EQ(SECSuccess, rv); | |
| 429 *result = crypto::PRTimeToBaseTime(prtime); | |
| 430 } | |
| 431 | |
| 432 std::string ParseSerialNumber(const CERTCertificate* certificate) { | |
| 433 return std::string(reinterpret_cast<char*>(certificate->serialNumber.data), | |
| 434 certificate->serialNumber.len); | |
| 435 } | |
| 436 | |
| 437 void GetSubjectAltName(CERTCertificate* cert_handle, | |
| 438 std::vector<std::string>* dns_names, | |
| 439 std::vector<std::string>* ip_addrs) { | |
| 440 if (dns_names) | |
| 441 dns_names->clear(); | |
| 442 if (ip_addrs) | |
| 443 ip_addrs->clear(); | |
| 444 | |
| 445 SECItem alt_name; | |
| 446 SECStatus rv = CERT_FindCertExtension(cert_handle, | |
| 447 SEC_OID_X509_SUBJECT_ALT_NAME, | |
| 448 &alt_name); | |
| 449 if (rv != SECSuccess) | |
| 450 return; | |
| 451 | |
| 452 PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | |
| 453 DCHECK(arena != NULL); | |
| 454 | |
| 455 CERTGeneralName* alt_name_list; | |
| 456 alt_name_list = CERT_DecodeAltNameExtension(arena, &alt_name); | |
| 457 SECITEM_FreeItem(&alt_name, PR_FALSE); | |
| 458 | |
| 459 CERTGeneralName* name = alt_name_list; | |
| 460 while (name) { | |
| 461 // DNSName and IPAddress are encoded as IA5String and OCTET STRINGs | |
| 462 // respectively, both of which can be byte copied from | |
| 463 // SECItemType::data into the appropriate output vector. | |
| 464 if (dns_names && name->type == certDNSName) { | |
| 465 dns_names->push_back(std::string( | |
| 466 reinterpret_cast<char*>(name->name.other.data), | |
| 467 name->name.other.len)); | |
| 468 } else if (ip_addrs && name->type == certIPAddress) { | |
| 469 ip_addrs->push_back(std::string( | |
| 470 reinterpret_cast<char*>(name->name.other.data), | |
| 471 name->name.other.len)); | |
| 472 } | |
| 473 name = CERT_GetNextGeneralName(name); | |
| 474 if (name == alt_name_list) | |
| 475 break; | |
| 476 } | |
| 477 PORT_FreeArena(arena, PR_FALSE); | |
| 478 } | |
| 479 | |
| 480 X509Certificate::OSCertHandles CreateOSCertHandlesFromBytes( | |
| 481 const char* data, | |
| 482 int length, | |
| 483 X509Certificate::Format format) { | |
| 484 X509Certificate::OSCertHandles results; | |
| 485 if (length < 0) | |
| 486 return results; | |
| 487 | |
| 488 crypto::EnsureNSSInit(); | |
| 489 | |
| 490 if (!NSS_IsInitialized()) | |
| 491 return results; | |
| 492 | |
| 493 switch (format) { | |
| 494 case X509Certificate::FORMAT_SINGLE_CERTIFICATE: { | |
| 495 X509Certificate::OSCertHandle handle = | |
| 496 X509Certificate::CreateOSCertHandleFromBytes(data, length); | |
| 497 if (handle) | |
| 498 results.push_back(handle); | |
| 499 break; | |
| 500 } | |
| 501 case X509Certificate::FORMAT_PKCS7: { | |
| 502 // Make a copy since CERT_DecodeCertPackage may modify it | |
| 503 std::vector<char> data_copy(data, data + length); | |
| 504 | |
| 505 SECStatus result = CERT_DecodeCertPackage(&data_copy[0], | |
| 506 length, CollectCertsCallback, &results); | |
| 507 if (result != SECSuccess) | |
| 508 results.clear(); | |
| 509 break; | |
| 510 } | |
| 511 default: | |
| 512 NOTREACHED() << "Certificate format " << format << " unimplemented"; | |
| 513 break; | |
| 514 } | |
| 515 | |
| 516 return results; | |
| 517 } | |
| 518 | |
| 519 X509Certificate::OSCertHandle ReadOSCertHandleFromPickle( | |
| 520 PickleIterator* pickle_iter) { | |
| 521 const char* data; | |
| 522 int length; | |
| 523 if (!pickle_iter->ReadData(&data, &length)) | |
| 524 return NULL; | |
| 525 | |
| 526 return X509Certificate::CreateOSCertHandleFromBytes(data, length); | |
| 527 } | |
| 528 | |
| 529 void GetPublicKeyInfo(CERTCertificate* handle, | |
| 530 size_t* size_bits, | |
| 531 X509Certificate::PublicKeyType* type) { | |
| 532 // Since we might fail, set the output parameters to default values first. | |
| 533 *type = X509Certificate::kPublicKeyTypeUnknown; | |
| 534 *size_bits = 0; | |
| 535 | |
| 536 crypto::ScopedSECKEYPublicKey key(CERT_ExtractPublicKey(handle)); | |
| 537 if (!key.get()) | |
| 538 return; | |
| 539 | |
| 540 *size_bits = SECKEY_PublicKeyStrengthInBits(key.get()); | |
| 541 | |
| 542 switch (key->keyType) { | |
| 543 case rsaKey: | |
| 544 *type = X509Certificate::kPublicKeyTypeRSA; | |
| 545 break; | |
| 546 case dsaKey: | |
| 547 *type = X509Certificate::kPublicKeyTypeDSA; | |
| 548 break; | |
| 549 case dhKey: | |
| 550 *type = X509Certificate::kPublicKeyTypeDH; | |
| 551 break; | |
| 552 case ecKey: | |
| 553 *type = X509Certificate::kPublicKeyTypeECDSA; | |
| 554 break; | |
| 555 default: | |
| 556 *type = X509Certificate::kPublicKeyTypeUnknown; | |
| 557 *size_bits = 0; | |
| 558 break; | |
| 559 } | |
| 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 | |
| 596 #endif // defined(USE_NSS) || defined(OS_IOS) | |
| 597 | |
| 598 } // namespace x509_util | |
| 599 | |
| 600 } // namespace net | |
| OLD | NEW |