| Index: chrome/browser/extensions/api/enterprise_platform_keys/token_method_nss.cc
|
| diff --git a/chrome/browser/extensions/api/enterprise_platform_keys/token_method_nss.cc b/chrome/browser/extensions/api/enterprise_platform_keys/token_method_nss.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3d38826b605bb1366a5e474e364cfa439b46b82b
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/enterprise_platform_keys/token_method_nss.cc
|
| @@ -0,0 +1,279 @@
|
| +// Copyright 2014 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 "chrome/browser/extensions/api/enterprise_platform_keys/token_method_nss.h"
|
| +
|
| +#include <cryptohi.h>
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/callback.h"
|
| +#include "base/logging.h"
|
| +#include "chrome/browser/extensions/api/enterprise_platform_keys/enterprise_platform_keys_api.h"
|
| +#include "chrome/browser/net/nss_context.h"
|
| +#include "crypto/rsa_private_key.h"
|
| +#include "net/base/crypto_module.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/cert/cert_database.h"
|
| +#include "net/cert/nss_cert_database.h"
|
| +#include "net/cert/x509_certificate.h"
|
| +
|
| +namespace {
|
| +const char kErrorInternal[] = "Internal Error";
|
| +const char kErrorKeyNotFound[] = "Key not found.";
|
| +const char kErrorInvalidX509Cert[] =
|
| + "Certificate is not a valid X.509 certificate.";
|
| +const char kErrorCertificateNotFound[] = "Certificate could not be found.";
|
| +}
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace enterprise_platform_keys {
|
| +
|
| +namespace nss {
|
| +
|
| +namespace {
|
| +
|
| +namespace api_epk = api::enterprise_platform_keys;
|
| +namespace api_epki = api::enterprise_platform_keys_internal;
|
| +
|
| +typedef base::Callback<bool(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + net::NSSCertDatabase* cert_db)> GetCertDBCallback;
|
| +
|
| +void DidGetCertDB(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + const GetCertDBCallback& callback,
|
| + net::NSSCertDatabase* cert_db) {
|
| + if (!cert_db) {
|
| + LOG(ERROR) << "Couldn't get NSSCertDatabase.";
|
| + func->SetError(kErrorInternal);
|
| + func->SendResponse(false);
|
| + return;
|
| + }
|
| +
|
| + crypto::ScopedPK11Slot slot = cert_db->GetPrivateSlot();
|
| + if (!slot) {
|
| + LOG(ERROR) << "No private slot";
|
| + func->SetError(kErrorInternal);
|
| + func->SendResponse(false);
|
| + return;
|
| + }
|
| + if (!callback.Run(func, slot.Pass(), cert_db))
|
| + func->SendResponse(false);
|
| +}
|
| +
|
| +void GetCertDatabase(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + const std::string& token_id,
|
| + const GetCertDBCallback& callback) {
|
| + GetNSSCertDatabaseForProfile(func->GetProfile(),
|
| + base::Bind(&DidGetCertDB, func, callback));
|
| +}
|
| +
|
| +bool GenerateWithDB(scoped_ptr<api_epki::GenerateKey::Params> params,
|
| + scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + net::NSSCertDatabase* cert_db) {
|
| + scoped_ptr<crypto::RSAPrivateKey> rsa_key(
|
| + crypto::RSAPrivateKey::CreateSensitive(slot.get(), 512));
|
| + if (!rsa_key) {
|
| + LOG(ERROR) << "Couldn't create key.";
|
| + func->SetError(kErrorInternal);
|
| + return false;
|
| + }
|
| +
|
| + std::vector<uint8> public_key_der;
|
| + if (!rsa_key->ExportPublicKey(&public_key_der)) {
|
| + // TODO(pneubeck): Remove rsa_key from storage.
|
| + LOG(ERROR) << "Couldn't export public key.";
|
| + func->SetError(kErrorInternal);
|
| + return false;
|
| + }
|
| + std::string public_key_der_str(public_key_der.begin(), public_key_der.end());
|
| +
|
| + func->SetResults(api_epki::GenerateKey::Results::Create(public_key_der_str));
|
| + func->SendResponse(true);
|
| + return true;
|
| +}
|
| +
|
| +bool SignWithDB(scoped_ptr<api_epki::Sign::Params> params,
|
| + scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + net::NSSCertDatabase* cert_db) {
|
| + const std::string& public_key = params->public_key;
|
| + const uint8* public_key_uint8 =
|
| + reinterpret_cast<const uint8*>(public_key.data());
|
| + std::vector<uint8> public_key_vector(public_key_uint8,
|
| + public_key_uint8 + public_key.size());
|
| +
|
| + // TODO(pneubeck): This searches all slots. Change to look only at |slot_|.
|
| + scoped_ptr<crypto::RSAPrivateKey> rsa_key(
|
| + crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key_vector));
|
| + if (!rsa_key) {
|
| + func->SetError(kErrorKeyNotFound);
|
| + return false;
|
| + }
|
| +
|
| + const std::string& data = params->data;
|
| + SECItem sign_result = {siBuffer, NULL, 0};
|
| + if (SEC_SignData(&sign_result,
|
| + reinterpret_cast<const unsigned char*>(data.data()),
|
| + data.size(),
|
| + rsa_key->key(),
|
| + SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION) != SECSuccess) {
|
| + LOG(ERROR) << "Couldn't sign.";
|
| + func->SetError(kErrorInternal);
|
| + return false;
|
| + }
|
| +
|
| + std::string signature(reinterpret_cast<const char*>(sign_result.data),
|
| + sign_result.len);
|
| + func->SetResults(api_epki::Sign::Results::Create(signature));
|
| + func->SendResponse(true);
|
| + return true;
|
| +}
|
| +
|
| +void DidGetCerts(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + scoped_ptr<net::CertificateList> certs) {
|
| + // std::vector<std::string> client_certs;
|
| + scoped_ptr<base::ListValue> client_certs(new base::ListValue());
|
| + for (net::CertificateList::const_iterator it = certs->begin();
|
| + it != certs->end();
|
| + ++it) {
|
| + net::X509Certificate::OSCertHandle cert_handle = (*it)->os_cert_handle();
|
| + crypto::ScopedPK11Slot cert_slot(PK11_KeyForCertExists(cert_handle,
|
| + NULL, // keyPtr
|
| + NULL)); // wincx
|
| +
|
| + // Keep only user certificate, i.e. certs for which the private key is
|
| + // present, and certs where the private key is stored in the queried slot.
|
| + if (cert_slot != slot)
|
| + continue;
|
| +
|
| + std::string der_encoding;
|
| + net::X509Certificate::GetDEREncoded(cert_handle, &der_encoding);
|
| + // client_certs.push_back(der_encoding);
|
| + client_certs->Append(base::BinaryValue::CreateWithCopiedBuffer(
|
| + der_encoding.data(), der_encoding.size()));
|
| + }
|
| + scoped_ptr<base::ListValue> results(new base::ListValue());
|
| + results->Append(client_certs.release());
|
| + func->SetResults(results.Pass());
|
| + // results_ = api_eci::GetClientCertificates::Results::Create(client_certs);
|
| + func->SendResponse(true);
|
| +}
|
| +
|
| +bool GetCertsWithDB(scoped_ptr<api_epk::GetCertificates::Params> params,
|
| + scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + net::NSSCertDatabase* cert_db) {
|
| + PK11SlotInfo* slot_ptr = slot.get();
|
| + cert_db->ListCertsInSlot(base::Bind(&DidGetCerts, func, base::Passed(&slot)),
|
| + slot_ptr);
|
| + return true;
|
| +}
|
| +
|
| +bool ImportWithDB(scoped_ptr<api_epk::ImportCertificate::Params> params,
|
| + scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + net::NSSCertDatabase* cert_db) {
|
| + const std::string& certificate = params->certificate;
|
| + scoped_refptr<net::X509Certificate> cert_x509 =
|
| + net::X509Certificate::CreateFromBytes(certificate.data(),
|
| + certificate.size());
|
| + if (!cert_x509) {
|
| + func->SetError(kErrorInvalidX509Cert);
|
| + return false;
|
| + }
|
| +
|
| + net::CertDatabase* db = net::CertDatabase::GetInstance();
|
| +
|
| + const net::Error cert_status = db->CheckUserCert(cert_x509);
|
| + if (cert_status == net::ERR_NO_PRIVATE_KEY_FOR_CERT) {
|
| + func->SetError(kErrorKeyNotFound);
|
| + return false;
|
| + } else if (cert_status != net::OK) {
|
| + func->SetError(net::ErrorToString(cert_status));
|
| + return false;
|
| + }
|
| +
|
| + const net::Error import_status = db->AddUserCert(cert_x509.get());
|
| + if (import_status != net::OK) {
|
| + LOG(ERROR) << "Could not import certificate.";
|
| + func->SetError(net::ErrorToString(import_status));
|
| + return false;
|
| + }
|
| +
|
| + func->SendResponse(true);
|
| + return true;
|
| +}
|
| +
|
| +bool RemoveWithDB(scoped_ptr<api_epk::RemoveCertificate::Params> params,
|
| + scoped_refptr<TokenMethodExtensionFunction> func,
|
| + crypto::ScopedPK11Slot slot,
|
| + net::NSSCertDatabase* cert_db) {
|
| + const std::string& certificate = params->certificate;
|
| + scoped_refptr<net::X509Certificate> cert_x509 =
|
| + net::X509Certificate::CreateFromBytes(certificate.data(),
|
| + certificate.size());
|
| + if (!cert_x509) {
|
| + func->SetError(kErrorInvalidX509Cert);
|
| + return false;
|
| + }
|
| +
|
| + bool certificate_found = cert_x509->os_cert_handle()->isperm;
|
| + bool success = cert_db->DeleteCertAndKey(cert_x509);
|
| +
|
| + // CertificateNotFound error has precedence over an internal error.
|
| + if (!certificate_found) {
|
| + func->SetError(kErrorCertificateNotFound);
|
| + return false;
|
| + }
|
| + if (!success) {
|
| + func->SetError(kErrorInternal);
|
| + return false;
|
| + }
|
| +
|
| + func->SendResponse(true);
|
| + return true;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +void Generate(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + scoped_ptr<api_epki::GenerateKey::Params> params) {
|
| + GetCertDatabase(func,
|
| + params->token_id,
|
| + base::Bind(GenerateWithDB, base::Passed(¶ms)));
|
| +}
|
| +
|
| +void Sign(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + scoped_ptr<api_epki::Sign::Params> params) {
|
| + GetCertDatabase(
|
| + func, params->token_id, base::Bind(SignWithDB, base::Passed(¶ms)));
|
| +}
|
| +
|
| +void GetCerts(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + scoped_ptr<api_epk::GetCertificates::Params> params) {
|
| + GetCertDatabase(func,
|
| + params->token_id,
|
| + base::Bind(GetCertsWithDB, base::Passed(¶ms)));
|
| +}
|
| +
|
| +void Import(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + scoped_ptr<api_epk::ImportCertificate::Params> params) {
|
| + GetCertDatabase(
|
| + func, params->token_id, base::Bind(ImportWithDB, base::Passed(¶ms)));
|
| +}
|
| +
|
| +void Remove(scoped_refptr<TokenMethodExtensionFunction> func,
|
| + scoped_ptr<api_epk::RemoveCertificate::Params> params) {
|
| + GetCertDatabase(
|
| + func, params->token_id, base::Bind(RemoveWithDB, base::Passed(¶ms)));
|
| +}
|
| +
|
| +} // namespace nss
|
| +
|
| +} // namespace enterprise_platform_keys
|
| +
|
| +} // namespace extensions
|
|
|