Added test for PEM_read_bio_PrivateKey using BIO loaded using BIO_new_mem_buf.

This commit is contained in:
David Garske
2018-06-06 10:04:39 -07:00
parent e1890a4b0e
commit 9cbd2b00d4

View File

@@ -321,7 +321,7 @@
#include "wolfssl/internal.h" /* for testing SSL_get_peer_cert_chain */ #include "wolfssl/internal.h" /* for testing SSL_get_peer_cert_chain */
#endif #endif
/* enable testing buffer load functions */ /* force enable test buffers */
#ifndef USE_CERT_BUFFERS_2048 #ifndef USE_CERT_BUFFERS_2048
#define USE_CERT_BUFFERS_2048 #define USE_CERT_BUFFERS_2048
#endif #endif
@@ -15688,18 +15688,51 @@ static void test_wolfSSL_PEM_PrivateKey(void)
{ {
#if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \ #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
!defined(NO_FILESYSTEM) && !defined(NO_RSA) && \ !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
(defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)) && \
defined(USE_CERT_BUFFERS_2048) defined(USE_CERT_BUFFERS_2048)
const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
EVP_PKEY* pkey = NULL; EVP_PKEY* pkey = NULL;
EVP_PKEY* pkey2 = NULL; const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
/* test creating new EVP_PKEY with bad arg */
AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
#if !defined(NO_FILESYSTEM)
{
BIO* bio; BIO* bio;
XFILE file;
const char* fname = "./certs/server-key.pem";
size_t sz;
byte* buf;
file = XFOPEN(fname, "rb");
AssertTrue((file != XBADFILE));
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
XFCLOSE(file);
/* Test using BIO new mem and loading PEM private key */
AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
BIO_free(bio);
EVP_PKEY_free(pkey);
}
#endif
#if (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
{
BIO* bio;
EVP_PKEY* pkey2 = NULL;
unsigned char extra[10]; unsigned char extra[10];
int i; int i;
printf(testingFmt, "wolfSSL_PEM_PrivateKey()"); printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
XMEMSET(extra, 0, sizeof(extra)); XMEMSET(extra, 0, sizeof(extra));
AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem())); AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE); AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
@@ -15712,9 +15745,6 @@ static void test_wolfSSL_PEM_PrivateKey(void)
AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL), AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
WOLFSSL_SUCCESS); WOLFSSL_SUCCESS);
/* test creating new EVP_PKEY with bad arg */
AssertNull((pkey2 = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
/* test creating new EVP_PKEY with good args */ /* test creating new EVP_PKEY with good args */
AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL))); AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0); AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
@@ -15737,6 +15767,8 @@ static void test_wolfSSL_PEM_PrivateKey(void)
BIO_free(bio); BIO_free(bio);
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
EVP_PKEY_free(pkey2); EVP_PKEY_free(pkey2);
}
#endif
/* key is DES encrypted */ /* key is DES encrypted */
#if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS)
@@ -15810,7 +15842,9 @@ static void test_wolfSSL_PEM_PrivateKey(void)
#endif #endif
printf(resultFmt, passed); printf(resultFmt, passed);
#endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
(void)server_key;
#endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
} }