Replace hardcoded parameters
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
// License Agreement. For a copy of this agreement, please contact
|
||||
// widevine-licensing@google.com.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//
|
||||
// Description:
|
||||
// RSA utility functions for serializing and deserializing RSA keys,
|
||||
@@ -14,12 +13,14 @@
|
||||
#include "common/rsa_util.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
#include "glog/logging.h"
|
||||
#include "openssl/pem.h"
|
||||
#include "openssl/x509.h"
|
||||
#include "common/private_key_util.h"
|
||||
|
||||
namespace {
|
||||
int BigNumGreaterThanPow2(const BIGNUM* b, int n) {
|
||||
@@ -34,92 +35,28 @@ int BigNumGreaterThanPow2(const BIGNUM* b, int n) {
|
||||
namespace widevine {
|
||||
namespace rsa_util {
|
||||
|
||||
static bool SerializeRsaKey(const RSA* key, std::string* serialized_key,
|
||||
bool serialize_private_key) {
|
||||
if (key == nullptr) {
|
||||
LOG(ERROR) << (serialize_private_key ? "Private" : "Public")
|
||||
<< " RSA key is nullptr.";
|
||||
return false;
|
||||
}
|
||||
if (serialized_key == nullptr) {
|
||||
LOG(ERROR) << "Pointer to hold serialized RSA"
|
||||
<< (serialize_private_key ? "Private" : "Public")
|
||||
<< "Key is nullptr.";
|
||||
return false;
|
||||
}
|
||||
BIO* bio = BIO_new(BIO_s_mem());
|
||||
if (bio == nullptr) {
|
||||
LOG(ERROR) << "BIO_new returned nullptr";
|
||||
return false;
|
||||
}
|
||||
bool success = false;
|
||||
if ((serialize_private_key
|
||||
? i2d_RSAPrivateKey_bio(bio, const_cast<RSA*>(key))
|
||||
: i2d_RSAPublicKey_bio(bio, const_cast<RSA*>(key))) != 0) {
|
||||
int serialized_size = BIO_pending(bio);
|
||||
serialized_key->assign(serialized_size, 0);
|
||||
if (BIO_read(bio, &(*serialized_key)[0], serialized_size) ==
|
||||
serialized_size) {
|
||||
success = true;
|
||||
} else {
|
||||
LOG(ERROR) << "BIO_read failure";
|
||||
}
|
||||
} else {
|
||||
LOG(ERROR) << (serialize_private_key ? "Private" : "Public")
|
||||
<< " key serialization failure";
|
||||
}
|
||||
BIO_free(bio);
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool DeserializeRsaKey(const std::string& serialized_key, RSA** key,
|
||||
bool deserialize_private_key) {
|
||||
if (serialized_key.empty()) {
|
||||
LOG(ERROR) << "Serialized RSA"
|
||||
<< (deserialize_private_key ? "Private" : "Public")
|
||||
<< "Key is empty.";
|
||||
return false;
|
||||
}
|
||||
if (key == nullptr) {
|
||||
LOG(ERROR) << "Pointer to hold new RSA "
|
||||
<< (deserialize_private_key ? "private" : "public")
|
||||
<< " key is nullptr.";
|
||||
return false;
|
||||
}
|
||||
BIO* bio = BIO_new_mem_buf(const_cast<char*>(serialized_key.data()),
|
||||
serialized_key.size());
|
||||
if (bio == nullptr) {
|
||||
LOG(ERROR) << "BIO_new_mem_buf returned nullptr";
|
||||
return false;
|
||||
}
|
||||
*key = deserialize_private_key ? d2i_RSAPrivateKey_bio(bio, nullptr)
|
||||
: d2i_RSAPublicKey_bio(bio, nullptr);
|
||||
BIO_free(bio);
|
||||
if (*key == nullptr) {
|
||||
LOG(ERROR) << (deserialize_private_key ? "Private" : "Public")
|
||||
<< " RSA key deserialization failure";
|
||||
}
|
||||
return *key != nullptr;
|
||||
}
|
||||
|
||||
bool SerializeRsaPrivateKey(const RSA* private_key,
|
||||
std::string* serialized_private_key) {
|
||||
return SerializeRsaKey(private_key, serialized_private_key, true);
|
||||
return private_key_util::SerializeKey<RSA>(private_key, i2d_RSAPrivateKey_bio,
|
||||
serialized_private_key);
|
||||
}
|
||||
|
||||
bool DeserializeRsaPrivateKey(const std::string& serialized_private_key,
|
||||
RSA** private_key) {
|
||||
return DeserializeRsaKey(serialized_private_key, private_key, true);
|
||||
return private_key_util::DeserializeKey<RSA>(
|
||||
serialized_private_key, d2i_RSAPrivateKey_bio, private_key);
|
||||
}
|
||||
|
||||
bool SerializeRsaPublicKey(const RSA* public_key,
|
||||
std::string* serialized_public_key) {
|
||||
return SerializeRsaKey(public_key, serialized_public_key, false);
|
||||
return private_key_util::SerializeKey<RSA>(public_key, i2d_RSAPublicKey_bio,
|
||||
serialized_public_key);
|
||||
}
|
||||
|
||||
bool DeserializeRsaPublicKey(const std::string& serialized_public_key,
|
||||
RSA** public_key) {
|
||||
return DeserializeRsaKey(serialized_public_key, public_key, false);
|
||||
return private_key_util::DeserializeKey<RSA>(
|
||||
serialized_public_key, d2i_RSAPublicKey_bio, public_key);
|
||||
}
|
||||
|
||||
bool SerializePrivateKeyInfo(const RSA* private_key,
|
||||
@@ -327,9 +264,9 @@ int get_password(char* buf, int size, int rwflag, void* u) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool DeserializeEncryptedPrivateKeyInfo(const std::string& serialized_private_key,
|
||||
const std::string& passphrase,
|
||||
RSA** private_key) {
|
||||
bool DeserializeEncryptedPrivateKeyInfo(
|
||||
const std::string& serialized_private_key, const std::string& passphrase,
|
||||
RSA** private_key) {
|
||||
if (serialized_private_key.empty()) {
|
||||
LOG(ERROR) << "Serialized RSAEncryptedPrivateKeyInfo is empty.";
|
||||
return false;
|
||||
@@ -349,8 +286,8 @@ bool DeserializeEncryptedPrivateKeyInfo(const std::string& serialized_private_ke
|
||||
return false;
|
||||
}
|
||||
bool success = false;
|
||||
EVP_PKEY* evp = d2i_PKCS8PrivateKey_bio(bio, nullptr, get_password,
|
||||
const_cast<std::string*>(&passphrase));
|
||||
EVP_PKEY* evp = d2i_PKCS8PrivateKey_bio(
|
||||
bio, nullptr, get_password, const_cast<std::string*>(&passphrase));
|
||||
if (evp == nullptr) {
|
||||
LOG(ERROR) << "d2i_PKCS8PrivateKey_bio returned nullptr.";
|
||||
goto cleanup;
|
||||
|
||||
Reference in New Issue
Block a user