Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
crypto: use less memory for storing keys
Browse files Browse the repository at this point in the history
Use `BIO_new_mem_buf` where possible to reduce memory usage and
initialization costs.
  • Loading branch information
indutny committed Sep 3, 2014
1 parent 4bd396a commit 68c14d6
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/node_crypto.cc
Expand Up @@ -3283,13 +3283,10 @@ SignBase::Error Sign::SignFinal(const char* key_pem,
EVP_PKEY* pkey = NULL;
bool fatal = true;

bp = BIO_new(BIO_s_mem());
bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
if (bp == NULL)
goto exit;

if (!BIO_write(bp, key_pem, key_pem_len))
goto exit;

pkey = PEM_read_bio_PrivateKey(bp,
NULL,
CryptoPemCallback,
Expand Down Expand Up @@ -3475,13 +3472,10 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem,
bool fatal = true;
int r = 0;

bp = BIO_new(BIO_s_mem());
bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
if (bp == NULL)
goto exit;

if (!BIO_write(bp, key_pem, key_pem_len))
goto exit;

// Check if this is a PKCS#8 or RSA public key before trying as X.509.
// Split this out into a separate function once we have more than one
// consumer of public keys.
Expand Down Expand Up @@ -3595,13 +3589,10 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
X509* x509 = NULL;
bool fatal = true;

bp = BIO_new(BIO_s_mem());
bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
if (bp == NULL)
goto exit;

if (!BIO_write(bp, key_pem, key_pem_len))
goto exit;

// Check if this is a PKCS#8 or RSA public key before trying as X.509 and
// private key.
if (operation == kEncrypt &&
Expand Down

0 comments on commit 68c14d6

Please sign in to comment.