Commit ffe5a137 authored by rsleevi's avatar rsleevi Committed by Commit bot

Make //crypto factories return std::unique_ptr<>s

Rather than make callers use base::WrapUnique or .reset(),
have //crypto functions that create new instances return them
in std::unique_ptr<>s

Also fixup NULL vs nullptr where it matters most, and remove
superflous .get() tests from the unique_ptr<>s

BUG=none
R=davidben@chromium.org

Review-Url: https://codereview.chromium.org/2095523002
Cr-Commit-Position: refs/heads/master@{#402368}
parent 4e58d3d7
...@@ -44,11 +44,11 @@ class BrowsingDataChannelIDHelperTest ...@@ -44,11 +44,11 @@ class BrowsingDataChannelIDHelperTest
channel_id_store->SetChannelID( channel_id_store->SetChannelID(
base::WrapUnique(new net::ChannelIDStore::ChannelID( base::WrapUnique(new net::ChannelIDStore::ChannelID(
"https://www.google.com:443", base::Time(), "https://www.google.com:443", base::Time(),
base::WrapUnique(crypto::ECPrivateKey::Create())))); crypto::ECPrivateKey::Create())));
channel_id_store->SetChannelID( channel_id_store->SetChannelID(
base::WrapUnique(new net::ChannelIDStore::ChannelID( base::WrapUnique(new net::ChannelIDStore::ChannelID(
"https://www.youtube.com:443", base::Time(), "https://www.youtube.com:443", base::Time(),
base::WrapUnique(crypto::ECPrivateKey::Create())))); crypto::ECPrivateKey::Create())));
} }
void FetchCallback( void FetchCallback(
...@@ -142,7 +142,7 @@ TEST_F(BrowsingDataChannelIDHelperTest, CannedEmpty) { ...@@ -142,7 +142,7 @@ TEST_F(BrowsingDataChannelIDHelperTest, CannedEmpty) {
ASSERT_TRUE(helper->empty()); ASSERT_TRUE(helper->empty());
helper->AddChannelID(net::ChannelIDStore::ChannelID( helper->AddChannelID(net::ChannelIDStore::ChannelID(
origin, base::Time(), base::WrapUnique(crypto::ECPrivateKey::Create()))); origin, base::Time(), crypto::ECPrivateKey::Create()));
ASSERT_FALSE(helper->empty()); ASSERT_FALSE(helper->empty());
helper->Reset(); helper->Reset();
ASSERT_TRUE(helper->empty()); ASSERT_TRUE(helper->empty());
......
...@@ -493,8 +493,7 @@ class RemoveChannelIDTester : public net::SSLConfigService::Observer { ...@@ -493,8 +493,7 @@ class RemoveChannelIDTester : public net::SSLConfigService::Observer {
base::Time creation_time) { base::Time creation_time) {
GetChannelIDStore()->SetChannelID( GetChannelIDStore()->SetChannelID(
base::WrapUnique(new net::ChannelIDStore::ChannelID( base::WrapUnique(new net::ChannelIDStore::ChannelID(
server_identifier, creation_time, server_identifier, creation_time, crypto::ECPrivateKey::Create())));
base::WrapUnique(crypto::ECPrivateKey::Create()))));
} }
// Add a server bound cert for |server|, with the current time as the // Add a server bound cert for |server|, with the current time as the
......
...@@ -110,17 +110,15 @@ class ProfileAuthDataTest : public testing::Test { ...@@ -110,17 +110,15 @@ class ProfileAuthDataTest : public testing::Test {
}; };
void ProfileAuthDataTest::SetUp() { void ProfileAuthDataTest::SetUp() {
channel_id_key1_.reset(crypto::ECPrivateKey::Create()); channel_id_key1_ = crypto::ECPrivateKey::Create();
channel_id_key2_.reset(crypto::ECPrivateKey::Create()); channel_id_key2_ = crypto::ECPrivateKey::Create();
PopulateBrowserContext(&login_browser_context_, kProxyAuthPassword1, PopulateBrowserContext(&login_browser_context_, kProxyAuthPassword1,
kCookieValue1, kCookieValue1, channel_id_key1_->Copy());
base::WrapUnique(channel_id_key1_->Copy()));
} }
void ProfileAuthDataTest::PopulateUserBrowserContext() { void ProfileAuthDataTest::PopulateUserBrowserContext() {
PopulateBrowserContext(&user_browser_context_, kProxyAuthPassword2, PopulateBrowserContext(&user_browser_context_, kProxyAuthPassword2,
kCookieValue2, kCookieValue2, channel_id_key2_->Copy());
base::WrapUnique(channel_id_key2_->Copy()));
} }
void ProfileAuthDataTest::Transfer( void ProfileAuthDataTest::Transfer(
......
...@@ -31,7 +31,7 @@ CryptohomeTokenEncryptor::CryptohomeTokenEncryptor( ...@@ -31,7 +31,7 @@ CryptohomeTokenEncryptor::CryptohomeTokenEncryptor(
DCHECK(!system_salt.empty()); DCHECK(!system_salt.empty());
// TODO(davidroche): should this use the system salt for both the password // TODO(davidroche): should this use the system salt for both the password
// and the salt value, or should this use a separate salt value? // and the salt value, or should this use a separate salt value?
system_salt_key_.reset(PassphraseToKey(system_salt_, system_salt_)); system_salt_key_ = PassphraseToKey(system_salt_, system_salt_);
} }
CryptohomeTokenEncryptor::~CryptohomeTokenEncryptor() { CryptohomeTokenEncryptor::~CryptohomeTokenEncryptor() {
...@@ -67,7 +67,7 @@ std::string CryptohomeTokenEncryptor::DecryptWithSystemSalt( ...@@ -67,7 +67,7 @@ std::string CryptohomeTokenEncryptor::DecryptWithSystemSalt(
encrypted_token_hex); encrypted_token_hex);
} }
crypto::SymmetricKey* CryptohomeTokenEncryptor::PassphraseToKey( std::unique_ptr<crypto::SymmetricKey> CryptohomeTokenEncryptor::PassphraseToKey(
const std::string& passphrase, const std::string& passphrase,
const std::string& salt) { const std::string& salt) {
return crypto::SymmetricKey::DeriveKeyFromPassword( return crypto::SymmetricKey::DeriveKeyFromPassword(
......
...@@ -47,8 +47,9 @@ class CryptohomeTokenEncryptor : public TokenEncryptor { ...@@ -47,8 +47,9 @@ class CryptohomeTokenEncryptor : public TokenEncryptor {
private: private:
// Converts |passphrase| to a SymmetricKey using the given |salt|. // Converts |passphrase| to a SymmetricKey using the given |salt|.
crypto::SymmetricKey* PassphraseToKey(const std::string& passphrase, std::unique_ptr<crypto::SymmetricKey> PassphraseToKey(
const std::string& salt); const std::string& passphrase,
const std::string& salt);
// Encrypts (AES) the token given |key| and |salt|. // Encrypts (AES) the token given |key| and |salt|.
std::string EncryptTokenWithKey(crypto::SymmetricKey* key, std::string EncryptTokenWithKey(crypto::SymmetricKey* key,
......
...@@ -98,14 +98,13 @@ class LocalExtensionCacheTest : public testing::Test { ...@@ -98,14 +98,13 @@ class LocalExtensionCacheTest : public testing::Test {
base::FilePath* filename) { base::FilePath* filename) {
std::string data(size, 0); std::string data(size, 0);
crypto::SecureHash* hash = std::unique_ptr<crypto::SecureHash> hash =
crypto::SecureHash::Create(crypto::SecureHash::SHA256); crypto::SecureHash::Create(crypto::SecureHash::SHA256);
hash->Update(data.c_str(), size); hash->Update(data.c_str(), size);
uint8_t output[crypto::kSHA256Length]; uint8_t output[crypto::kSHA256Length];
hash->Finish(output, sizeof(output)); hash->Finish(output, sizeof(output));
const std::string hex_hash = const std::string hex_hash =
base::ToLowerASCII(base::HexEncode(output, sizeof(output))); base::ToLowerASCII(base::HexEncode(output, sizeof(output)));
delete hash;
const base::FilePath file = const base::FilePath file =
GetExtensionFileName(dir, id, version, hex_hash); GetExtensionFileName(dir, id, version, hex_hash);
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h" #include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
...@@ -84,11 +83,9 @@ TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { ...@@ -84,11 +83,9 @@ TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) {
crypto::ECPrivateKey::Create()); crypto::ECPrivateKey::Create());
std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create());
store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
"google.com", base::Time::FromInternalValue(1), "google.com", base::Time::FromInternalValue(1), goog_key->Copy()));
base::WrapUnique(goog_key->Copy())));
store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
"foo.com", base::Time::FromInternalValue(3), "foo.com", base::Time::FromInternalValue(3), foo_key->Copy()));
base::WrapUnique(foo_key->Copy())));
std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>> std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>>
channel_ids; channel_ids;
...@@ -143,10 +140,10 @@ TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { ...@@ -143,10 +140,10 @@ TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) {
TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) {
store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
"google.com", base::Time::FromInternalValue(1), "google.com", base::Time::FromInternalValue(1),
base::WrapUnique(crypto::ECPrivateKey::Create()))); crypto::ECPrivateKey::Create()));
store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
"nonpersistent.com", base::Time::FromInternalValue(3), "nonpersistent.com", base::Time::FromInternalValue(3),
base::WrapUnique(crypto::ECPrivateKey::Create()))); crypto::ECPrivateKey::Create()));
std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>> std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>>
channel_ids; channel_ids;
...@@ -176,10 +173,10 @@ TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { ...@@ -176,10 +173,10 @@ TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) {
// being committed to disk. // being committed to disk.
store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
"nonpersistent.com", base::Time::FromInternalValue(5), "nonpersistent.com", base::Time::FromInternalValue(5),
base::WrapUnique(crypto::ECPrivateKey::Create()))); crypto::ECPrivateKey::Create()));
store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
"persistent.com", base::Time::FromInternalValue(7), "persistent.com", base::Time::FromInternalValue(7),
base::WrapUnique(crypto::ECPrivateKey::Create()))); crypto::ECPrivateKey::Create()));
// Now close the store, and the nonpersistent.com channel IDs should be // Now close the store, and the nonpersistent.com channel IDs should be
// deleted according to policy. // deleted according to policy.
......
...@@ -111,7 +111,7 @@ CrxFile::ValidateError CrxFile::ValidateSignature( ...@@ -111,7 +111,7 @@ CrxFile::ValidateError CrxFile::ValidateSignature(
base::ScopedFILE file(base::OpenFile(crx_path, "rb")); base::ScopedFILE file(base::OpenFile(crx_path, "rb"));
std::unique_ptr<crypto::SecureHash> hash; std::unique_ptr<crypto::SecureHash> hash;
if (!expected_hash.empty()) if (!expected_hash.empty())
hash.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); hash = crypto::SecureHash::Create(crypto::SecureHash::SHA256);
if (!file.get()) if (!file.get())
return ValidateError::CRX_FILE_NOT_READABLE; return ValidateError::CRX_FILE_NOT_READABLE;
......
...@@ -77,12 +77,10 @@ crypto::SymmetricKey* GetEncryptionKey() { ...@@ -77,12 +77,10 @@ crypto::SymmetricKey* GetEncryptionKey() {
// Create an encryption key from our password and salt. The key is // Create an encryption key from our password and salt. The key is
// intentionally leaked. // intentionally leaked.
cached_encryption_key = cached_encryption_key = crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, crypto::SymmetricKey::AES, password, salt,
password, kEncryptionIterations, kDerivedKeySizeInBits)
salt, .release();
kEncryptionIterations,
kDerivedKeySizeInBits);
ANNOTATE_LEAKING_OBJECT_PTR(cached_encryption_key); ANNOTATE_LEAKING_OBJECT_PTR(cached_encryption_key);
DCHECK(cached_encryption_key); DCHECK(cached_encryption_key);
return cached_encryption_key; return cached_encryption_key;
......
...@@ -208,7 +208,7 @@ std::string BaseFile::DebugString() const { ...@@ -208,7 +208,7 @@ std::string BaseFile::DebugString() const {
DownloadInterruptReason BaseFile::CalculatePartialHash( DownloadInterruptReason BaseFile::CalculatePartialHash(
const std::string& hash_to_expect) { const std::string& hash_to_expect) {
secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); secure_hash_ = crypto::SecureHash::Create(crypto::SecureHash::SHA256);
if (bytes_so_far_ == 0) if (bytes_so_far_ == 0)
return DOWNLOAD_INTERRUPT_REASON_NONE; return DOWNLOAD_INTERRUPT_REASON_NONE;
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include "base/format_macros.h" #include "base/format_macros.h"
#include "base/guid.h" #include "base/guid.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
...@@ -1175,9 +1174,9 @@ void DownloadItemImpl::Start( ...@@ -1175,9 +1174,9 @@ void DownloadItemImpl::Start(
int64_t offset = new_create_info.save_info->offset; int64_t offset = new_create_info.save_info->offset;
std::unique_ptr<crypto::SecureHash> hash_state = std::unique_ptr<crypto::SecureHash> hash_state =
base::WrapUnique(new_create_info.save_info->hash_state new_create_info.save_info->hash_state
? new_create_info.save_info->hash_state->Clone() ? new_create_info.save_info->hash_state->Clone()
: nullptr); : nullptr;
// Interrupted downloads also need a target path. // Interrupted downloads also need a target path.
if (target_path_.empty()) { if (target_path_.empty()) {
......
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <memory>
#include "base/logging.h" #include "base/logging.h"
#include "crypto/auto_cbb.h" #include "crypto/auto_cbb.h"
#include "crypto/openssl_util.h" #include "crypto/openssl_util.h"
...@@ -43,13 +41,13 @@ bool ExportKeyWithBio(const void* key, ...@@ -43,13 +41,13 @@ bool ExportKeyWithBio(const void* key,
return false; return false;
ScopedBIO bio(BIO_new(BIO_s_mem())); ScopedBIO bio(BIO_new(BIO_s_mem()));
if (!bio.get()) if (!bio)
return false; return false;
if (!export_fn(bio.get(), key)) if (!export_fn(bio.get(), key))
return false; return false;
char* data = NULL; char* data = nullptr;
long len = BIO_get_mem_data(bio.get(), &data); long len = BIO_get_mem_data(bio.get(), &data);
if (!data || len < 0) if (!data || len < 0)
return false; return false;
...@@ -65,28 +63,21 @@ ECPrivateKey::~ECPrivateKey() { ...@@ -65,28 +63,21 @@ ECPrivateKey::~ECPrivateKey() {
EVP_PKEY_free(key_); EVP_PKEY_free(key_);
} }
ECPrivateKey* ECPrivateKey::Copy() const {
std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey);
if (key_)
copy->key_ = EVP_PKEY_up_ref(key_);
return copy.release();
}
// static // static
ECPrivateKey* ECPrivateKey::Create() { std::unique_ptr<ECPrivateKey> ECPrivateKey::Create() {
OpenSSLErrStackTracer err_tracer(FROM_HERE); OpenSSLErrStackTracer err_tracer(FROM_HERE);
ScopedEC_KEY ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); ScopedEC_KEY ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
if (!ec_key.get() || !EC_KEY_generate_key(ec_key.get())) if (!ec_key || !EC_KEY_generate_key(ec_key.get()))
return NULL; return nullptr;
std::unique_ptr<ECPrivateKey> result(new ECPrivateKey()); std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
result->key_ = EVP_PKEY_new(); result->key_ = EVP_PKEY_new();
if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_, ec_key.get())) if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_, ec_key.get()))
return NULL; return nullptr;
CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_)); CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_));
return result.release(); return result;
} }
// static // static
...@@ -100,13 +91,13 @@ std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo( ...@@ -100,13 +91,13 @@ std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo(
if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC) if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
return nullptr; return nullptr;
std::unique_ptr<ECPrivateKey> result(new ECPrivateKey); std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
result->key_ = pkey.release(); result->key_ = pkey.release();
return result; return result;
} }
// static // static
ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
const std::string& password, const std::string& password,
const std::vector<uint8_t>& encrypted_private_key_info, const std::vector<uint8_t>& encrypted_private_key_info,
const std::vector<uint8_t>& subject_public_key_info) { const std::vector<uint8_t>& subject_public_key_info) {
...@@ -114,16 +105,16 @@ ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( ...@@ -114,16 +105,16 @@ ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
// useful for the NSS implementation (which uses the public key's SHA1 // useful for the NSS implementation (which uses the public key's SHA1
// as a lookup key when storing the private one in its store). // as a lookup key when storing the private one in its store).
if (encrypted_private_key_info.empty()) if (encrypted_private_key_info.empty())
return NULL; return nullptr;
OpenSSLErrStackTracer err_tracer(FROM_HERE); OpenSSLErrStackTracer err_tracer(FROM_HERE);
const uint8_t* data = &encrypted_private_key_info[0]; const uint8_t* data = &encrypted_private_key_info[0];
const uint8_t* ptr = data; const uint8_t* ptr = data;
ScopedX509_SIG p8_encrypted( ScopedX509_SIG p8_encrypted(
d2i_X509_SIG(NULL, &ptr, encrypted_private_key_info.size())); d2i_X509_SIG(nullptr, &ptr, encrypted_private_key_info.size()));
if (!p8_encrypted || ptr != data + encrypted_private_key_info.size()) if (!p8_encrypted || ptr != data + encrypted_private_key_info.size())
return NULL; return nullptr;
ScopedPKCS8_PRIV_KEY_INFO p8_decrypted; ScopedPKCS8_PRIV_KEY_INFO p8_decrypted;
if (password.empty()) { if (password.empty()) {
...@@ -142,15 +133,22 @@ ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( ...@@ -142,15 +133,22 @@ ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
} }
if (!p8_decrypted) if (!p8_decrypted)
return NULL; return nullptr;
// Create a new EVP_PKEY for it. // Create a new EVP_PKEY for it.
std::unique_ptr<ECPrivateKey> result(new ECPrivateKey); std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
result->key_ = EVP_PKCS82PKEY(p8_decrypted.get()); result->key_ = EVP_PKCS82PKEY(p8_decrypted.get());
if (!result->key_ || EVP_PKEY_id(result->key_) != EVP_PKEY_EC) if (!result->key_ || EVP_PKEY_id(result->key_) != EVP_PKEY_EC)
return NULL; return nullptr;
return result.release(); return result;
}
std::unique_ptr<ECPrivateKey> ECPrivateKey::Copy() const {
std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey());
if (key_)
copy->key_ = EVP_PKEY_up_ref(key_);
return copy;
} }
bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const { bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
...@@ -174,7 +172,7 @@ bool ECPrivateKey::ExportEncryptedPrivateKey( ...@@ -174,7 +172,7 @@ bool ECPrivateKey::ExportEncryptedPrivateKey(
OpenSSLErrStackTracer err_tracer(FROM_HERE); OpenSSLErrStackTracer err_tracer(FROM_HERE);
// Convert into a PKCS#8 object. // Convert into a PKCS#8 object.
ScopedPKCS8_PRIV_KEY_INFO pkcs8(EVP_PKEY2PKCS8(key_)); ScopedPKCS8_PRIV_KEY_INFO pkcs8(EVP_PKEY2PKCS8(key_));
if (!pkcs8.get()) if (!pkcs8)
return false; return false;
// Encrypt the object. // Encrypt the object.
...@@ -190,7 +188,7 @@ bool ECPrivateKey::ExportEncryptedPrivateKey( ...@@ -190,7 +188,7 @@ bool ECPrivateKey::ExportEncryptedPrivateKey(
0, 0,
iterations, iterations,
pkcs8.get())); pkcs8.get()));
if (!encrypted.get()) if (!encrypted)
return false; return false;
// Write it into |*output| // Write it into |*output|
...@@ -236,6 +234,6 @@ bool ECPrivateKey::ExportRawPublicKey(std::string* output) const { ...@@ -236,6 +234,6 @@ bool ECPrivateKey::ExportRawPublicKey(std::string* output) const {
return true; return true;
} }
ECPrivateKey::ECPrivateKey() : key_(NULL) {} ECPrivateKey::ECPrivateKey() : key_(nullptr) {}
} // namespace crypto } // namespace crypto
...@@ -30,10 +30,10 @@ class CRYPTO_EXPORT ECPrivateKey { ...@@ -30,10 +30,10 @@ class CRYPTO_EXPORT ECPrivateKey {
public: public:
~ECPrivateKey(); ~ECPrivateKey();
// Creates a new random instance. Can return NULL if initialization fails. // Creates a new random instance. Can return nullptr if initialization fails.
// The created key will use the NIST P-256 curve. // The created key will use the NIST P-256 curve.
// TODO(mattm): Add a curve parameter. // TODO(mattm): Add a curve parameter.
static ECPrivateKey* Create(); static std::unique_ptr<ECPrivateKey> Create();
// Create a new instance by importing an existing private key. The format is // 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 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return
...@@ -44,17 +44,17 @@ class CRYPTO_EXPORT ECPrivateKey { ...@@ -44,17 +44,17 @@ class CRYPTO_EXPORT ECPrivateKey {
// Creates a new instance by importing an existing key pair. // Creates a new instance by importing an existing key pair.
// The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo
// block and an X.509 SubjectPublicKeyInfo block. // block and an X.509 SubjectPublicKeyInfo block.
// Returns NULL if initialization fails. // Returns nullptr if initialization fails.
// //
// This function is deprecated. Use CreateFromPrivateKeyInfo for new code. // This function is deprecated. Use CreateFromPrivateKeyInfo for new code.
// See https://crbug.com/603319. // See https://crbug.com/603319.
static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo( static std::unique_ptr<ECPrivateKey> CreateFromEncryptedPrivateKeyInfo(
const std::string& password, const std::string& password,
const std::vector<uint8_t>& encrypted_private_key_info, const std::vector<uint8_t>& encrypted_private_key_info,
const std::vector<uint8_t>& subject_public_key_info); const std::vector<uint8_t>& subject_public_key_info);
// Returns a copy of the object. // Returns a copy of the object.
ECPrivateKey* Copy() const; std::unique_ptr<ECPrivateKey> Copy() const;
EVP_PKEY* key() { return key_; } EVP_PKEY* key() { return key_; }
......
...@@ -45,7 +45,7 @@ TEST(ECPrivateKeyUnitTest, InitRandomTest) { ...@@ -45,7 +45,7 @@ TEST(ECPrivateKeyUnitTest, InitRandomTest) {
static const char kPassword2[] = "test"; static const char kPassword2[] = "test";
std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create()); std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create());
ASSERT_TRUE(keypair.get()); ASSERT_TRUE(keypair);
// Re-import as a PrivateKeyInfo. // Re-import as a PrivateKeyInfo.
std::vector<uint8_t> privkey; std::vector<uint8_t> privkey;
...@@ -61,16 +61,16 @@ TEST(ECPrivateKeyUnitTest, InitRandomTest) { ...@@ -61,16 +61,16 @@ TEST(ECPrivateKeyUnitTest, InitRandomTest) {
EXPECT_TRUE( EXPECT_TRUE(
keypair->ExportEncryptedPrivateKey(kPassword1, 1, &encrypted_privkey));