Make PEM_read_{,bio_}PrivateKey use secmem
authorBenjamin Kaduk <bkaduk@akamai.com>
Fri, 19 Feb 2016 23:36:52 +0000 (17:36 -0600)
committerRichard Levitte <levitte@openssl.org>
Mon, 8 May 2017 19:20:31 +0000 (21:20 +0200)
We now have a version of PEM_read_bytes that can use temporary
buffers allocated from the secure heap; use them to handle this
sensitive information.

Note that for PEM_read_PrivateKey, the i/o still goes through
stdio since the input is a FILE pointer.  Standard I/O performs
additional buffering, which cannot be changed to use the OpenSSL
secure heap for temporary storage.  As such, it is recommended
to use BIO_new_file() and PEM_read_bio_PrivateKey() instead.

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/1700)

crypto/pem/pem_pkey.c

index 63086227075ede42a8600b20552817c41ce484e1..93565011c02096acd7c8d1a243bd01291ab04afe 100644 (file)
@@ -32,7 +32,8 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
     int slen;
     EVP_PKEY *ret = NULL;
 
-    if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
+    if (!PEM_bytes_read_bio_secmem(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp,
+                                  cb, u))
         return NULL;
     p = data;
 
@@ -85,8 +86,8 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
     if (ret == NULL)
         PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
  err:
-    OPENSSL_free(nm);
-    OPENSSL_clear_free(data, len);
+    OPENSSL_secure_free(nm);
+    OPENSSL_secure_free(data);
     return (ret);
 }