Skip to content

Commit

Permalink
Make crypto::RSAPrivateKey's factory functions return unique_ptrs
Browse files Browse the repository at this point in the history
BUG=none
TBR=yurys@chromium.org

Review-Url: https://codereview.chromium.org/2089883002
Cr-Commit-Position: refs/heads/master@{#401201}
  • Loading branch information
sleevi authored and Commit bot committed Jun 22, 2016
1 parent 8553918 commit d1afa1e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 42 deletions.
10 changes: 5 additions & 5 deletions chrome/browser/devtools/device/usb/android_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,27 @@ void BnDiv(uint32_t* a, uint32_t* b, uint32_t** pq, uint32_t** pr) {

} // namespace

crypto::RSAPrivateKey* AndroidRSAPrivateKey(Profile* profile) {
std::unique_ptr<crypto::RSAPrivateKey> AndroidRSAPrivateKey(Profile* profile) {
std::string encoded_key =
profile->GetPrefs()->GetString(prefs::kDevToolsAdbKey);
std::string decoded_key;
std::unique_ptr<crypto::RSAPrivateKey> key;
if (!encoded_key.empty() && base::Base64Decode(encoded_key, &decoded_key)) {
std::vector<uint8_t> key_info(decoded_key.begin(), decoded_key.end());
key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
key = crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info);
}
if (!key) {
key.reset(crypto::RSAPrivateKey::Create(2048));
key = crypto::RSAPrivateKey::Create(2048);
std::vector<uint8_t> key_info;
if (!key || !key->ExportPrivateKey(&key_info))
return NULL;
return nullptr;

std::string key_string(key_info.begin(), key_info.end());
base::Base64Encode(key_string, &encoded_key);
profile->GetPrefs()->SetString(prefs::kDevToolsAdbKey,
encoded_key);
}
return key.release();
return key;
}

std::string AndroidRSAPublicKey(crypto::RSAPrivateKey* key) {
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/devtools/device/usb/android_rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_RSA_H_
#define CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_RSA_H_

#include <memory>
#include <string>

namespace crypto {
Expand All @@ -13,7 +14,7 @@ class RSAPrivateKey;

class Profile;

crypto::RSAPrivateKey* AndroidRSAPrivateKey(Profile* profile);
std::unique_ptr<crypto::RSAPrivateKey> AndroidRSAPrivateKey(Profile* profile);

std::string AndroidRSAPublicKey(crypto::RSAPrivateKey* key);

Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/devtools/device/usb/usb_device_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void UsbDeviceProvider::CountDevices(
}

UsbDeviceProvider::UsbDeviceProvider(Profile* profile){
rsa_key_.reset(AndroidRSAPrivateKey(profile));
rsa_key_ = AndroidRSAPrivateKey(profile);
}

void UsbDeviceProvider::QueryDevices(const SerialsCallback& callback) {
Expand Down
14 changes: 7 additions & 7 deletions chrome/browser/extensions/extension_creator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ bool ExtensionCreator::ValidateManifest(const base::FilePath& extension_dir,
return !!extension.get();
}

crypto::RSAPrivateKey* ExtensionCreator::ReadInputKey(const base::FilePath&
private_key_path) {
std::unique_ptr<crypto::RSAPrivateKey> ExtensionCreator::ReadInputKey(
const base::FilePath& private_key_path) {
if (!base::PathExists(private_key_path)) {
error_message_ =
l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_NO_EXISTS);
Expand All @@ -146,8 +146,8 @@ crypto::RSAPrivateKey* ExtensionCreator::ReadInputKey(const base::FilePath&
std::vector<uint8_t>(private_key_bytes.begin(), private_key_bytes.end()));
}

crypto::RSAPrivateKey* ExtensionCreator::GenerateKey(const base::FilePath&
output_private_key_path) {
std::unique_ptr<crypto::RSAPrivateKey> ExtensionCreator::GenerateKey(
const base::FilePath& output_private_key_path) {
std::unique_ptr<crypto::RSAPrivateKey> key_pair(
crypto::RSAPrivateKey::Create(kRSAKeySize));
if (!key_pair) {
Expand Down Expand Up @@ -189,7 +189,7 @@ crypto::RSAPrivateKey* ExtensionCreator::GenerateKey(const base::FilePath&
}
}

return key_pair.release();
return key_pair;
}

bool ExtensionCreator::CreateZip(const base::FilePath& extension_dir,
Expand Down Expand Up @@ -301,9 +301,9 @@ bool ExtensionCreator::Run(const base::FilePath& extension_dir,
// Initialize Key Pair
std::unique_ptr<crypto::RSAPrivateKey> key_pair;
if (!private_key_path.value().empty())
key_pair.reset(ReadInputKey(private_key_path));
key_pair = ReadInputKey(private_key_path);
else
key_pair.reset(GenerateKey(output_private_key_path));
key_pair = GenerateKey(output_private_key_path);
if (!key_pair)
return false;

Expand Down
7 changes: 5 additions & 2 deletions chrome/browser/extensions/extension_creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -72,11 +73,13 @@ class ExtensionCreator {
int run_flags);

// Reads private key from |private_key_path|.
crypto::RSAPrivateKey* ReadInputKey(const base::FilePath& private_key_path);
std::unique_ptr<crypto::RSAPrivateKey> ReadInputKey(
const base::FilePath& private_key_path);

// Generates a key pair and writes the private key to |private_key_path|
// if provided.
crypto::RSAPrivateKey* GenerateKey(const base::FilePath& private_key_path);
std::unique_ptr<crypto::RSAPrivateKey> GenerateKey(
const base::FilePath& private_key_path);

// Creates temporary zip file for the extension.
bool CreateZip(const base::FilePath& extension_dir, const base::FilePath& temp_path,
Expand Down
36 changes: 17 additions & 19 deletions crypto/rsa_private_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@
namespace crypto {

// static
RSAPrivateKey* RSAPrivateKey::Create(uint16_t num_bits) {
std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Create(uint16_t num_bits) {
OpenSSLErrStackTracer err_tracer(FROM_HERE);

ScopedRSA rsa_key(RSA_new());
ScopedBIGNUM bn(BN_new());
if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
return NULL;
return nullptr;

if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL))
return NULL;
if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), nullptr))
return nullptr;

std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
result->key_ = EVP_PKEY_new();
if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get()))
return NULL;
return nullptr;

return result.release();
return result;
}

// static
RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromPrivateKeyInfo(
const std::vector<uint8_t>& input) {
OpenSSLErrStackTracer err_tracer(FROM_HERE);

Expand All @@ -53,37 +53,35 @@ RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(

std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
result->key_ = pkey.release();
return result.release();
return result;
}

// static
RSAPrivateKey* RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
DCHECK(key);
if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA)
return NULL;
RSAPrivateKey* copy = new RSAPrivateKey();
return nullptr;
std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
copy->key_ = EVP_PKEY_up_ref(key);
return copy;
}

RSAPrivateKey::RSAPrivateKey()
: key_(NULL) {
}
RSAPrivateKey::RSAPrivateKey() : key_(nullptr) {}

RSAPrivateKey::~RSAPrivateKey() {
if (key_)
EVP_PKEY_free(key_);
}

RSAPrivateKey* RSAPrivateKey::Copy() const {
std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey());
std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Copy() const {
std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
ScopedRSA rsa(EVP_PKEY_get1_RSA(key_));
if (!rsa)
return NULL;
return nullptr;
copy->key_ = EVP_PKEY_new();
if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get()))
return NULL;
return copy.release();
return nullptr;
return copy;
}

bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
Expand Down
10 changes: 5 additions & 5 deletions crypto/rsa_private_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stddef.h>
#include <stdint.h>

#include <list>
#include <memory>
#include <vector>

#include "base/macros.h"
Expand All @@ -28,23 +28,23 @@ class CRYPTO_EXPORT RSAPrivateKey {
~RSAPrivateKey();

// Create a new random instance. Can return NULL if initialization fails.
static RSAPrivateKey* Create(uint16_t num_bits);
static std::unique_ptr<RSAPrivateKey> Create(uint16_t num_bits);

// Create a new instance by importing an existing private key. The format is
// an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if
// initialization fails.
static RSAPrivateKey* CreateFromPrivateKeyInfo(
static std::unique_ptr<RSAPrivateKey> CreateFromPrivateKeyInfo(
const std::vector<uint8_t>& input);

// Create a new instance from an existing EVP_PKEY, taking a
// reference to it. |key| must be an RSA key. Returns NULL on
// failure.
static RSAPrivateKey* CreateFromKey(EVP_PKEY* key);
static std::unique_ptr<RSAPrivateKey> CreateFromKey(EVP_PKEY* key);

EVP_PKEY* key() { return key_; }

// Creates a copy of the object.
RSAPrivateKey* Copy() const;
std::unique_ptr<RSAPrivateKey> Copy() const;

// Exports the private key to a PKCS #8 PrivateKeyInfo block.
bool ExportPrivateKey(std::vector<uint8_t>* output) const;
Expand Down
4 changes: 2 additions & 2 deletions net/quic/crypto/proof_source_chromium.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ bool ProofSourceChromium::Initialize(const base::FilePath& cert_path,

const uint8_t* p = reinterpret_cast<const uint8_t*>(key_data.data());
std::vector<uint8_t> input(p, p + key_data.size());
private_key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
if (private_key_.get() == nullptr) {
private_key_ = crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input);
if (!private_key_) {
DLOG(FATAL) << "Unable to create private key.";
return false;
}
Expand Down

0 comments on commit d1afa1e

Please sign in to comment.