forked from wolfSSL/wolfssl
Added test for PEM_read_bio_PrivateKey
using BIO loaded using BIO_new_mem_buf
.
This commit is contained in:
126
tests/api.c
126
tests/api.c
@@ -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
|
||||||
@@ -15686,57 +15686,89 @@ static void test_wolfSSL_private_keys(void)
|
|||||||
|
|
||||||
static void test_wolfSSL_PEM_PrivateKey(void)
|
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;
|
||||||
BIO* bio;
|
|
||||||
unsigned char extra[10];
|
|
||||||
int i;
|
|
||||||
|
|
||||||
printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
|
|
||||||
|
|
||||||
XMEMSET(extra, 0, sizeof(extra));
|
|
||||||
AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
|
|
||||||
AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
|
|
||||||
|
|
||||||
AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
|
|
||||||
&server_key, (long)sizeof_server_key_der_2048));
|
|
||||||
AssertNull(pkey);
|
|
||||||
|
|
||||||
AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
|
|
||||||
&server_key, (long)sizeof_server_key_der_2048));
|
|
||||||
AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
|
|
||||||
WOLFSSL_SUCCESS);
|
|
||||||
|
|
||||||
/* test creating new EVP_PKEY with bad arg */
|
/* test creating new EVP_PKEY with bad arg */
|
||||||
AssertNull((pkey2 = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
|
AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
|
||||||
|
|
||||||
/* test creating new EVP_PKEY with good args */
|
#if !defined(NO_FILESYSTEM)
|
||||||
AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
|
{
|
||||||
AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
|
BIO* bio;
|
||||||
|
XFILE file;
|
||||||
|
const char* fname = "./certs/server-key.pem";
|
||||||
|
size_t sz;
|
||||||
|
byte* buf;
|
||||||
|
|
||||||
/* test of reuse of EVP_PKEY */
|
file = XFOPEN(fname, "rb");
|
||||||
AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
|
AssertTrue((file != XBADFILE));
|
||||||
AssertIntEQ(BIO_pending(bio), 0);
|
XFSEEK(file, 0, XSEEK_END);
|
||||||
AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
|
sz = XFTELL(file);
|
||||||
SSL_SUCCESS);
|
XREWIND(file);
|
||||||
AssertIntEQ(BIO_write(bio, extra, 10), 10); /*add 10 extra bytes after PEM*/
|
AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
|
||||||
AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
|
AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
|
||||||
AssertNotNull(pkey);
|
XFCLOSE(file);
|
||||||
AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
|
|
||||||
AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
|
/* Test using BIO new mem and loading PEM private key */
|
||||||
AssertIntEQ(BIO_read(bio, extra, 10), 10);
|
AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
|
||||||
for (i = 0; i < 10; i++) {
|
AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
|
||||||
AssertIntEQ(extra[i], 0);
|
XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
|
||||||
|
BIO_free(bio);
|
||||||
|
EVP_PKEY_free(pkey);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
BIO_free(bio);
|
#if (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
|
||||||
EVP_PKEY_free(pkey);
|
{
|
||||||
EVP_PKEY_free(pkey2);
|
BIO* bio;
|
||||||
|
EVP_PKEY* pkey2 = NULL;
|
||||||
|
unsigned char extra[10];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
|
||||||
|
|
||||||
|
XMEMSET(extra, 0, sizeof(extra));
|
||||||
|
|
||||||
|
AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
|
||||||
|
AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
|
||||||
|
|
||||||
|
AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
|
||||||
|
&server_key, (long)sizeof_server_key_der_2048));
|
||||||
|
AssertNull(pkey);
|
||||||
|
|
||||||
|
AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
|
||||||
|
&server_key, (long)sizeof_server_key_der_2048));
|
||||||
|
AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
|
||||||
|
WOLFSSL_SUCCESS);
|
||||||
|
|
||||||
|
/* test creating new EVP_PKEY with good args */
|
||||||
|
AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
|
||||||
|
AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
|
||||||
|
|
||||||
|
/* test of reuse of EVP_PKEY */
|
||||||
|
AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
|
||||||
|
AssertIntEQ(BIO_pending(bio), 0);
|
||||||
|
AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
|
||||||
|
SSL_SUCCESS);
|
||||||
|
AssertIntEQ(BIO_write(bio, extra, 10), 10); /*add 10 extra bytes after PEM*/
|
||||||
|
AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
|
||||||
|
AssertNotNull(pkey);
|
||||||
|
AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
|
||||||
|
AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
|
||||||
|
AssertIntEQ(BIO_read(bio, extra, 10), 10);
|
||||||
|
for (i = 0; i < 10; i++) {
|
||||||
|
AssertIntEQ(extra[i], 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
BIO_free(bio);
|
||||||
|
EVP_PKEY_free(pkey);
|
||||||
|
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 */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user