summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn Willden <swillden@google.com>2014-06-17 11:45:07 -0600
committerShawn Willden <swillden@google.com>2014-06-17 11:45:07 -0600
commit8d0531e9748aa5e4860d3b52c6b0c88cea52f8bd (patch)
treed156583a7f9a3b0f93d198b69edc2e16e3c700dd
parentf7386706a1d92eefdc02f359aa46cfe33f2d158a (diff)
downloadsecurity-8d0531e9748aa5e4860d3b52c6b0c88cea52f8bd.tar.gz
Make usage of new/delete and malloc/free consistent.
All buffers returned to the caller should be allocated with malloc, since the caller assumes it's calling a C API. Internally, stuff allocated with new should be freed with delete, and so on. Change-Id: Ie08d910b9f6ebee38dc39127310e695453d1256f
-rw-r--r--softkeymaster/keymaster_openssl.cpp20
1 files changed, 14 insertions, 6 deletions
diff --git a/softkeymaster/keymaster_openssl.cpp b/softkeymaster/keymaster_openssl.cpp
index f81e844b..3bf4cecd 100644
--- a/softkeymaster/keymaster_openssl.cpp
+++ b/softkeymaster/keymaster_openssl.cpp
@@ -78,6 +78,12 @@ struct RSA_Delete {
};
typedef UniquePtr<RSA, RSA_Delete> Unique_RSA;
+struct Malloc_Free {
+ void operator()(void* p) const {
+ free(p);
+ }
+};
+
typedef UniquePtr<keymaster_device_t> Unique_keymaster_device_t;
/**
@@ -123,7 +129,9 @@ static int wrap_key(EVP_PKEY* pkey, int type, uint8_t** keyBlob, size_t* keyBlob
*keyBlobLength = get_softkey_header_size() + sizeof(type) + sizeof(publicLen) + privateLen +
sizeof(privateLen) + publicLen;
- UniquePtr<unsigned char> derData(new unsigned char[*keyBlobLength]);
+ // derData will be returned to the caller, so allocate it with malloc.
+ UniquePtr<unsigned char, Malloc_Free> derData(
+ static_cast<unsigned char*>(malloc(*keyBlobLength)));
if (derData.get() == NULL) {
ALOGE("could not allocate memory for key blob");
return -1;
@@ -452,7 +460,7 @@ __attribute__((visibility("default"))) int openssl_get_keypair_public(
return -1;
}
- UniquePtr<uint8_t> key(static_cast<uint8_t*>(malloc(len)));
+ UniquePtr<uint8_t, Malloc_Free> key(static_cast<uint8_t*>(malloc(len)));
if (key.get() == NULL) {
ALOGE("Could not allocate memory for public key data");
return -1;
@@ -485,7 +493,7 @@ static int sign_dsa(EVP_PKEY* pkey, keymaster_dsa_sign_params_t* sign_params, co
}
unsigned int dsaSize = DSA_size(dsa.get());
- UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dsaSize)));
+ UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dsaSize)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_dsa");
return -1;
@@ -517,7 +525,7 @@ static int sign_ec(EVP_PKEY* pkey, keymaster_ec_sign_params_t* sign_params, cons
}
unsigned int ecdsaSize = ECDSA_size(eckey.get());
- UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(ecdsaSize)));
+ UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(ecdsaSize)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_ec");
return -1;
@@ -551,7 +559,7 @@ static int sign_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params, co
return -1;
}
- UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
+ UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_rsa");
return -1;
@@ -673,7 +681,7 @@ static int verify_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params,
return -1;
}
- UniquePtr<uint8_t> dataPtr(reinterpret_cast<uint8_t*>(malloc(signedDataLength)));
+ UniquePtr<uint8_t[]> dataPtr(new uint8_t[signedDataLength]);
if (dataPtr.get() == NULL) {
logOpenSSLError("openssl_verify_data");
return -1;