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

Unified Diff: net/cert/x509_util_nss.cc

Issue 1882433002: Removing NSS files and USE_OPENSSL flag (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/cert/x509_util_nss.h ('k') | net/cert/x509_util_nss_certs.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/cert/x509_util_nss.cc
diff --git a/net/cert/x509_util_nss.cc b/net/cert/x509_util_nss.cc
index d059eb30cca5065b9bd7cbb2f93292f368436ee9..134c085e2484ea8c200febc212316334aba8117e 100644
--- a/net/cert/x509_util_nss.cc
+++ b/net/cert/x509_util_nss.cc
@@ -1,9 +1,7 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "net/cert/x509_util_nss.h"
-
#include <cert.h> // Must be included before certdb.h
#include <certdb.h>
#include <cryptohi.h>
@@ -19,6 +17,7 @@
#include "base/debug/leak_annotations.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
+#include "base/numerics/safe_conversions.h"
#include "base/pickle.h"
#include "base/strings/stringprintf.h"
#include "crypto/ec_private_key.h"
@@ -29,164 +28,389 @@
#include "crypto/third_party/nss/chromium-nss.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
+#include "net/cert/x509_util_nss.h"
namespace net {
namespace {
-// Creates a Certificate object that may be passed to the SignCertificate
-// method to generate an X509 certificate.
-// Returns NULL if an error is encountered in the certificate creation
-// process.
-// Caller responsible for freeing returned certificate object.
-CERTCertificate* CreateCertificate(SECKEYPublicKey* public_key,
- const std::string& subject,
- uint32_t serial_number,
- base::Time not_valid_before,
- base::Time not_valid_after) {
- // Create info about public key.
- CERTSubjectPublicKeyInfo* spki =
- SECKEY_CreateSubjectPublicKeyInfo(public_key);
- if (!spki)
+// Microsoft User Principal Name: 1.3.6.1.4.1.311.20.2.3
+const uint8_t kUpnOid[] = {0x2b, 0x6, 0x1, 0x4, 0x1,
+ 0x82, 0x37, 0x14, 0x2, 0x3};
+
+// Callback for CERT_DecodeCertPackage(), used in
+// CreateOSCertHandlesFromBytes().
+SECStatus PR_CALLBACK CollectCertsCallback(void* arg,
+ SECItem** certs,
+ int num_certs) {
+ X509Certificate::OSCertHandles* results =
+ reinterpret_cast<X509Certificate::OSCertHandles*>(arg);
+
+ for (int i = 0; i < num_certs; ++i) {
+ X509Certificate::OSCertHandle handle =
+ X509Certificate::CreateOSCertHandleFromBytes(
+ reinterpret_cast<char*>(certs[i]->data), certs[i]->len);
+ if (handle)
+ results->push_back(handle);
+ }
+
+ return SECSuccess;
+}
+
+typedef std::unique_ptr<CERTName,
+ crypto::NSSDestroyer<CERTName, CERT_DestroyName>>
+ ScopedCERTName;
+
+// Create a new CERTName object from its encoded representation.
+// |arena| is the allocation pool to use.
+// |data| points to a DER-encoded X.509 DistinguishedName.
+// Return a new CERTName pointer on success, or NULL.
+CERTName* CreateCertNameFromEncoded(PLArenaPool* arena,
+ const base::StringPiece& data) {
+ if (!arena)
return NULL;
- // Create the certificate request.
- CERTName* subject_name =
- CERT_AsciiToName(const_cast<char*>(subject.c_str()));
- CERTCertificateRequest* cert_request =
- CERT_CreateCertificateRequest(subject_name, spki, NULL);
- SECKEY_DestroySubjectPublicKeyInfo(spki);
-
- if (!cert_request) {
- PRErrorCode prerr = PR_GetError();
- LOG(ERROR) << "Failed to create certificate request: " << prerr;
- CERT_DestroyName(subject_name);
+ ScopedCERTName name(PORT_ArenaZNew(arena, CERTName));
+ if (!name.get())
return NULL;
- }
- CERTValidity* validity = CERT_CreateValidity(
- crypto::BaseTimeToPRTime(not_valid_before),
- crypto::BaseTimeToPRTime(not_valid_after));
- if (!validity) {
- PRErrorCode prerr = PR_GetError();
- LOG(ERROR) << "Failed to create certificate validity object: " << prerr;
- CERT_DestroyName(subject_name);
- CERT_DestroyCertificateRequest(cert_request);
+ SECItem item;
+ item.len = static_cast<unsigned int>(data.length());
+ item.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data.data()));
+
+ SECStatus rv = SEC_ASN1DecodeItem(arena, name.get(),
+ SEC_ASN1_GET(CERT_NameTemplate), &item);
+ if (rv != SECSuccess)
return NULL;
+
+ return name.release();
+}
+
+} // namespace
+
+namespace x509_util {
+
+void ParsePrincipal(CERTName* name, CertPrincipal* principal) {
+// Starting in NSS 3.15, CERTGetNameFunc takes a const CERTName* argument.
+#if NSS_VMINOR >= 15
+ typedef char* (*CERTGetNameFunc)(const CERTName* name);
+#else
+ typedef char* (*CERTGetNameFunc)(CERTName * name);
+#endif
+
+ // TODO(jcampan): add business_category and serial_number.
+ // TODO(wtc): NSS has the CERT_GetOrgName, CERT_GetOrgUnitName, and
+ // CERT_GetDomainComponentName functions, but they return only the most
+ // general (the first) RDN. NSS doesn't have a function for the street
+ // address.
+ static const SECOidTag kOIDs[] = {
+ SEC_OID_AVA_STREET_ADDRESS, SEC_OID_AVA_ORGANIZATION_NAME,
+ SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME, SEC_OID_AVA_DC};
+
+ std::vector<std::string>* values[] = {
+ &principal->street_addresses, &principal->organization_names,
+ &principal->organization_unit_names, &principal->domain_components};
+ DCHECK_EQ(arraysize(kOIDs), arraysize(values));
+
+ CERTRDN** rdns = name->rdns;
+ for (size_t rdn = 0; rdns[rdn]; ++rdn) {
+ CERTAVA** avas = rdns[rdn]->avas;
+ for (size_t pair = 0; avas[pair] != 0; ++pair) {
+ SECOidTag tag = CERT_GetAVATag(avas[pair]);
+ for (size_t oid = 0; oid < arraysize(kOIDs); ++oid) {
+ if (kOIDs[oid] == tag) {
+ SECItem* decode_item = CERT_DecodeAVAValue(&avas[pair]->value);
+ if (!decode_item)
+ break;
+ // TODO(wtc): Pass decode_item to CERT_RFC1485_EscapeAndQuote.
+ std::string value(reinterpret_cast<char*>(decode_item->data),
+ decode_item->len);
+ values[oid]->push_back(value);
+ SECITEM_FreeItem(decode_item, PR_TRUE);
+ break;
+ }
+ }
+ }
}
- CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name,
- validity, cert_request);
- if (!cert) {
- PRErrorCode prerr = PR_GetError();
- LOG(ERROR) << "Failed to create certificate: " << prerr;
+
+ // Get CN, L, S, and C.
+ CERTGetNameFunc get_name_funcs[4] = {CERT_GetCommonName, CERT_GetLocalityName,
+ CERT_GetStateName, CERT_GetCountryName};
+ std::string* single_values[4] = {
+ &principal->common_name, &principal->locality_name,
+ &principal->state_or_province_name, &principal->country_name};
+ for (size_t i = 0; i < arraysize(get_name_funcs); ++i) {
+ char* value = get_name_funcs[i](name);
+ if (value) {
+ single_values[i]->assign(value);
+ PORT_Free(value);
+ }
}
+}
- // Cleanup for resources used to generate the cert.
- CERT_DestroyName(subject_name);
- CERT_DestroyValidity(validity);
- CERT_DestroyCertificateRequest(cert_request);
+void ParseDate(const SECItem* der_date, base::Time* result) {
+ PRTime prtime;
+ SECStatus rv = DER_DecodeTimeChoice(&prtime, der_date);
+ DCHECK_EQ(SECSuccess, rv);
+ *result = crypto::PRTimeToBaseTime(prtime);
+}
- return cert;
+std::string ParseSerialNumber(const CERTCertificate* certificate) {
+ return std::string(reinterpret_cast<char*>(certificate->serialNumber.data),
+ certificate->serialNumber.len);
}
-SECOidTag ToSECOid(x509_util::DigestAlgorithm alg) {
- switch (alg) {
- case x509_util::DIGEST_SHA1:
- return SEC_OID_SHA1;
- case x509_util::DIGEST_SHA256:
- return SEC_OID_SHA256;
+void GetSubjectAltName(CERTCertificate* cert_handle,
+ std::vector<std::string>* dns_names,
+ std::vector<std::string>* ip_addrs) {
+ if (dns_names)
+ dns_names->clear();
+ if (ip_addrs)
+ ip_addrs->clear();
+
+ SECItem alt_name;
+ SECStatus rv = CERT_FindCertExtension(
+ cert_handle, SEC_OID_X509_SUBJECT_ALT_NAME, &alt_name);
+ if (rv != SECSuccess)
+ return;
+
+ PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
+ DCHECK(arena != NULL);
+
+ CERTGeneralName* alt_name_list;
+ alt_name_list = CERT_DecodeAltNameExtension(arena, &alt_name);
+ SECITEM_FreeItem(&alt_name, PR_FALSE);
+
+ CERTGeneralName* name = alt_name_list;
+ while (name) {
+ // DNSName and IPAddress are encoded as IA5String and OCTET STRINGs
+ // respectively, both of which can be byte copied from
+ // SECItemType::data into the appropriate output vector.
+ if (dns_names && name->type == certDNSName) {
+ dns_names->push_back(
+ std::string(reinterpret_cast<char*>(name->name.other.data),
+ name->name.other.len));
+ } else if (ip_addrs && name->type == certIPAddress) {
+ ip_addrs->push_back(
+ std::string(reinterpret_cast<char*>(name->name.other.data),
+ name->name.other.len));
+ }
+ name = CERT_GetNextGeneralName(name);
+ if (name == alt_name_list)
+ break;
}
- return SEC_OID_UNKNOWN;
+ PORT_FreeArena(arena, PR_FALSE);
}
-// Signs a certificate object, with |key| generating a new X509Certificate
-// and destroying the passed certificate object (even when NULL is returned).
-// The logic of this method references SignCert() in NSS utility certutil:
-// http://mxr.mozilla.org/security/ident?i=SignCert.
-// Returns true on success or false if an error is encountered in the
-// certificate signing process.
-bool SignCertificate(
- CERTCertificate* cert,
- SECKEYPrivateKey* key,
- SECOidTag hash_algorithm) {
- // |arena| is used to encode the cert.
- PLArenaPool* arena = cert->arena;
- SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->keyType,
- hash_algorithm);
- if (algo_id == SEC_OID_UNKNOWN)
- return false;
-
- SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0);
+void GetRFC822SubjectAltNames(CERTCertificate* cert_handle,
+ std::vector<std::string>* names) {
+ crypto::ScopedSECItem alt_name(SECITEM_AllocItem(NULL, NULL, 0));
+ DCHECK(alt_name.get());
+
+ names->clear();
+ SECStatus rv = CERT_FindCertExtension(
+ cert_handle, SEC_OID_X509_SUBJECT_ALT_NAME, alt_name.get());
if (rv != SECSuccess)
- return false;
+ return;
- // Generate a cert of version 3.
- *(cert->version.data) = 2;
- cert->version.len = 1;
+ crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
+ DCHECK(arena.get());
- SECItem der = { siBuffer, NULL, 0 };
+ CERTGeneralName* alt_name_list;
+ alt_name_list = CERT_DecodeAltNameExtension(arena.get(), alt_name.get());
- // Use ASN1 DER to encode the cert.
- void* encode_result = SEC_ASN1EncodeItem(
- NULL, &der, cert, SEC_ASN1_GET(CERT_CertificateTemplate));
- if (!encode_result)
- return false;
+ CERTGeneralName* name = alt_name_list;
+ while (name) {
+ if (name->type == certRFC822Name) {
+ names->push_back(
+ std::string(reinterpret_cast<char*>(name->name.other.data),
+ name->name.other.len));
+ }
+ name = CERT_GetNextGeneralName(name);
+ if (name == alt_name_list)
+ break;
+ }
+}
- // Allocate space to contain the signed cert.
- SECItem result = { siBuffer, NULL, 0 };
+void GetUPNSubjectAltNames(CERTCertificate* cert_handle,
+ std::vector<std::string>* names) {
+ crypto::ScopedSECItem alt_name(SECITEM_AllocItem(NULL, NULL, 0));
+ DCHECK(alt_name.get());
- // Sign the ASN1 encoded cert and save it to |result|.
- rv = DerSignData(arena, &result, &der, key, algo_id);
- PORT_Free(der.data);
- if (rv != SECSuccess) {
- DLOG(ERROR) << "DerSignData: " << PORT_GetError();
- return false;
+ names->clear();
+ SECStatus rv = CERT_FindCertExtension(
+ cert_handle, SEC_OID_X509_SUBJECT_ALT_NAME, alt_name.get());
+ if (rv != SECSuccess)
+ return;
+
+ crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
+ DCHECK(arena.get());
+
+ CERTGeneralName* alt_name_list;
+ alt_name_list = CERT_DecodeAltNameExtension(arena.get(), alt_name.get());
+
+ CERTGeneralName* name = alt_name_list;
+ while (name) {
+ if (name->type == certOtherName) {
+ OtherName* on = &name->name.OthName;
+ if (on->oid.len == sizeof(kUpnOid) &&
+ memcmp(on->oid.data, kUpnOid, sizeof(kUpnOid)) == 0) {
+ SECItem decoded;
+ if (SEC_QuickDERDecodeItem(arena.get(), &decoded,
+ SEC_ASN1_GET(SEC_UTF8StringTemplate),
+ &name->name.OthName.name) == SECSuccess) {
+ names->push_back(
+ std::string(reinterpret_cast<char*>(decoded.data), decoded.len));
+ }
+ }
+ }
+ name = CERT_GetNextGeneralName(name);
+ if (name == alt_name_list)
+ break;
}
+}
- // Save the signed result to the cert.
- cert->derCert = result;
+X509Certificate::OSCertHandles CreateOSCertHandlesFromBytes(
+ const char* data,
+ size_t length,
+ X509Certificate::Format format) {
+ X509Certificate::OSCertHandles results;
- return true;
+ crypto::EnsureNSSInit();
+
+ if (!NSS_IsInitialized())
+ return results;
+
+ switch (format) {
+ case X509Certificate::FORMAT_SINGLE_CERTIFICATE: {
+ X509Certificate::OSCertHandle handle =
+ X509Certificate::CreateOSCertHandleFromBytes(data, length);
+ if (handle)
+ results.push_back(handle);
+ break;
+ }
+ case X509Certificate::FORMAT_PKCS7: {
+ // Make a copy since CERT_DecodeCertPackage may modify it
+ std::vector<char> data_copy(data, data + length);
+
+ SECStatus result = CERT_DecodeCertPackage(
+ data_copy.data(), base::checked_cast<int>(data_copy.size()),
+ CollectCertsCallback, &results);
+ if (result != SECSuccess)
+ results.clear();
+ break;
+ }
+ default:
+ NOTREACHED() << "Certificate format " << format << " unimplemented";
+ break;
+ }
+
+ return results;
}
-} // namespace
+X509Certificate::OSCertHandle ReadOSCertHandleFromPickle(
+ base::PickleIterator* pickle_iter) {
+ const char* data;
+ int length;
+ if (!pickle_iter->ReadData(&data, &length))
+ return NULL;
-namespace x509_util {
+ return X509Certificate::CreateOSCertHandleFromBytes(data, length);
+}
+
+void GetPublicKeyInfo(CERTCertificate* handle,
+ size_t* size_bits,
+ X509Certificate::PublicKeyType* type) {
+ // Since we might fail, set the output parameters to default values first.
+ *type = X509Certificate::kPublicKeyTypeUnknown;
+ *size_bits = 0;
+
+ crypto::ScopedSECKEYPublicKey key(CERT_ExtractPublicKey(handle));
+ if (!key.get())
+ return;
+
+ *size_bits = SECKEY_PublicKeyStrengthInBits(key.get());
-bool CreateSelfSignedCert(crypto::RSAPrivateKey* key,
- DigestAlgorithm alg,
- const std::string& subject,
- uint32_t serial_number,
- base::Time not_valid_before,
- base::Time not_valid_after,
- std::string* der_cert) {
- DCHECK(key);
- DCHECK(!strncmp(subject.c_str(), "CN=", 3U));
- CERTCertificate* cert = CreateCertificate(key->public_key(),
- subject,
- serial_number,
- not_valid_before,
- not_valid_after);
- if (!cert)
- return false;
-
- if (!SignCertificate(cert, key->key(), ToSECOid(alg))) {
- CERT_DestroyCertificate(cert);
- return false;
+ switch (key->keyType) {
+ case rsaKey:
+ *type = X509Certificate::kPublicKeyTypeRSA;
+ break;
+ case dsaKey:
+ *type = X509Certificate::kPublicKeyTypeDSA;
+ break;
+ case dhKey:
+ *type = X509Certificate::kPublicKeyTypeDH;
+ break;
+ case ecKey:
+ *type = X509Certificate::kPublicKeyTypeECDSA;
+ break;
+ default:
+ *type = X509Certificate::kPublicKeyTypeUnknown;
+ *size_bits = 0;
+ break;
+ }
+}
+
+bool GetIssuersFromEncodedList(const std::vector<std::string>& encoded_issuers,
+ PLArenaPool* arena,
+ std::vector<CERTName*>* out) {
+ std::vector<CERTName*> result;
+ for (size_t n = 0; n < encoded_issuers.size(); ++n) {
+ CERTName* name = CreateCertNameFromEncoded(arena, encoded_issuers[n]);
+ if (name != NULL)
+ result.push_back(name);
+ }
+
+ if (result.size() == encoded_issuers.size()) {
+ out->swap(result);
+ return true;
}
- der_cert->assign(reinterpret_cast<char*>(cert->derCert.data),
- cert->derCert.len);
- CERT_DestroyCertificate(cert);
- return true;
+ for (size_t n = 0; n < result.size(); ++n)
+ CERT_DestroyName(result[n]);
+ return false;
}
-bool GetTLSServerEndPointChannelBinding(const X509Certificate& certificate,
- std::string* token) {
- NOTIMPLEMENTED();
+bool IsCertificateIssuedBy(const std::vector<CERTCertificate*>& cert_chain,
+ const std::vector<CERTName*>& valid_issuers) {
+ for (size_t n = 0; n < cert_chain.size(); ++n) {
+ CERTName* cert_issuer = &cert_chain[n]->issuer;
+ for (size_t i = 0; i < valid_issuers.size(); ++i) {
+ if (CERT_CompareName(valid_issuers[i], cert_issuer) == SECEqual)
+ return true;
+ }
+ }
return false;
}
-} // namespace x509_util
+std::string GetUniqueNicknameForSlot(const std::string& nickname,
+ const SECItem* subject,
+ PK11SlotInfo* slot) {
+ int index = 2;
+ std::string new_name = nickname;
+ std::string temp_nickname = new_name;
+ std::string token_name;
+
+ if (!slot)
+ return new_name;
+
+ if (!PK11_IsInternalKeySlot(slot)) {
+ token_name.assign(PK11_GetTokenName(slot));
+ token_name.append(":");
+
+ temp_nickname = token_name + new_name;
+ }
+
+ while (SEC_CertNicknameConflict(temp_nickname.c_str(),
+ const_cast<SECItem*>(subject),
+ CERT_GetDefaultCertDB())) {
+ base::SStringPrintf(&new_name, "%s #%d", nickname.c_str(), index++);
+ temp_nickname = token_name + new_name;
+ }
+
+ return new_name;
+}
+
+} // namespace x509_util
-} // namespace net
+} // namespace net
« no previous file with comments | « net/cert/x509_util_nss.h ('k') | net/cert/x509_util_nss_certs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698