API testing: split out more test cases

EVP into test_evp_cipher, test_evp_digest, test_evp_pkey and test_evp.
OBJ into test_ossl_obj.
OpenSSL RAND into test_ossl_rand.
OpenSSL PKCS7 and PKCS12 tests into test_ossl_p7p12.
CertificateManager into test_certman.

Move some BIO tests from api.c into test_evp_bio.c.

Fix line lengths.
This commit is contained in:
Sean Parkinson
2026-01-12 20:03:14 +10:00
parent 6264c115cc
commit 43d831ff06
27 changed files with 11615 additions and 10833 deletions

View File

@@ -2705,6 +2705,13 @@ if(WOLFSSL_EXAMPLES)
tests/api/test_ossl_x509_str.c tests/api/test_ossl_x509_str.c
tests/api/test_ossl_x509_lu.c tests/api/test_ossl_x509_lu.c
tests/api/test_ossl_pem.c tests/api/test_ossl_pem.c
tests/api/test_ossl_rand.c
tests/api/test_ossl_obj.c
tests/api/test_ossl_p7p12.c
tests/api/test_evp_digest.c
tests/api/test_evp_cipher.c
tests/api/test_evp_pkey.c
tests/api/test_certman.c
tests/api/test_tls13.c tests/api/test_tls13.c
tests/srp.c tests/srp.c
tests/suites.c tests/suites.c

10731
tests/api.c

File diff suppressed because it is too large Load Diff

View File

@@ -52,6 +52,11 @@
#define FOURK_BUF 4096 #define FOURK_BUF 4096
#endif #endif
#if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
!defined(NO_CERTS) && \
(!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
#define HAVE_CERT_CHAIN_VALIDATION
#endif
#ifndef NO_RSA #ifndef NO_RSA
#define GEN_BUF 294 #define GEN_BUF 294

View File

@@ -93,6 +93,16 @@ tests_unit_test_SOURCES += tests/api/test_ossl_x509_str.c
tests_unit_test_SOURCES += tests/api/test_ossl_x509_lu.c tests_unit_test_SOURCES += tests/api/test_ossl_x509_lu.c
# SSL PEM # SSL PEM
tests_unit_test_SOURCES += tests/api/test_ossl_pem.c tests_unit_test_SOURCES += tests/api/test_ossl_pem.c
# SSL Random
tests_unit_test_SOURCES += tests/api/test_ossl_rand.c
tests_unit_test_SOURCES += tests/api/test_ossl_obj.c
tests_unit_test_SOURCES += tests/api/test_ossl_p7p12.c
# EVP APIs
tests_unit_test_SOURCES += tests/api/test_evp_digest.c
tests_unit_test_SOURCES += tests/api/test_evp_cipher.c
tests_unit_test_SOURCES += tests/api/test_evp_pkey.c
# CertificateManager
tests_unit_test_SOURCES += tests/api/test_certman.c
# TLS 1.3 specific # TLS 1.3 specific
tests_unit_test_SOURCES += tests/api/test_tls13.c tests_unit_test_SOURCES += tests/api/test_tls13.c
endif endif
@@ -174,5 +184,12 @@ EXTRA_DIST += tests/api/test_ossl_x509_info.h
EXTRA_DIST += tests/api/test_ossl_x509_str.h EXTRA_DIST += tests/api/test_ossl_x509_str.h
EXTRA_DIST += tests/api/test_ossl_x509_lu.h EXTRA_DIST += tests/api/test_ossl_x509_lu.h
EXTRA_DIST += tests/api/test_ossl_pem.h EXTRA_DIST += tests/api/test_ossl_pem.h
EXTRA_DIST += tests/api/test_ossl_rand.h
EXTRA_DIST += tests/api/test_ossl_obj.h
EXTRA_DIST += tests/api/test_ossl_p7p12.h
EXTRA_DIST += tests/api/test_evp_digest.h
EXTRA_DIST += tests/api/test_evp_cipher.h
EXTRA_DIST += tests/api/test_evp_pkey.h
EXTRA_DIST += tests/api/test_certman.h
EXTRA_DIST += tests/api/test_tls13.h EXTRA_DIST += tests/api/test_tls13.h

2370
tests/api/test_certman.c Normal file

File diff suppressed because it is too large Load Diff

61
tests/api/test_certman.h Normal file
View File

@@ -0,0 +1,61 @@
/* test_certman.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_CERTMAN_H
#define WOLFCRYPT_TEST_CERTMAN_H
#include <tests/api/api_decl.h>
int test_wolfSSL_CertManagerAPI(void);
int test_wolfSSL_CertManagerLoadCABuffer(void);
int test_wolfSSL_CertManagerLoadCABuffer_ex(void);
int test_wolfSSL_CertManagerLoadCABufferType(void);
int test_wolfSSL_CertManagerGetCerts(void);
int test_wolfSSL_CertManagerSetVerify(void);
int test_wolfSSL_CertManagerNameConstraint(void);
int test_wolfSSL_CertManagerNameConstraint2(void);
int test_wolfSSL_CertManagerNameConstraint3(void);
int test_wolfSSL_CertManagerNameConstraint4(void);
int test_wolfSSL_CertManagerNameConstraint5(void);
int test_wolfSSL_CertManagerCRL(void);
int test_wolfSSL_CRL_duplicate_extensions(void);
int test_wolfSSL_CertManagerCheckOCSPResponse(void);
int test_various_pathlen_chains(void);
#define TEST_CERTMAN_DECLS \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerAPI), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerLoadCABuffer), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerLoadCABuffer_ex), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerLoadCABufferType), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerGetCerts), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerSetVerify), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerNameConstraint), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerNameConstraint2), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerNameConstraint3), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerNameConstraint4), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerNameConstraint5), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerCRL), \
TEST_DECL_GROUP("certman", test_wolfSSL_CRL_duplicate_extensions), \
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerCheckOCSPResponse), \
TEST_DECL_GROUP("certman", test_various_pathlen_chains)
#endif /* WOLFCRYPT_TEST_CERTMAN_H */

View File

@@ -26,75 +26,546 @@
#include <wolfssl/openssl/evp.h> #include <wolfssl/openssl/evp.h>
#include <tests/api/test_evp.h> #include <tests/api/test_evp.h>
/* Test for NULL_CIPHER_TYPE in wolfSSL_EVP_CipherUpdate() */ /* Test functions for base64 encode/decode */
int test_wolfSSL_EVP_CipherUpdate_Null(void) int test_wolfSSL_EVP_ENCODE_CTX_new(void)
{ {
EXPECT_DECLS; EXPECT_DECLS;
#ifdef OPENSSL_EXTRA #if defined(OPENSSL_EXTRA) && \
WOLFSSL_EVP_CIPHER_CTX* ctx; ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
const char* testData = "Test NULL cipher data"; EVP_ENCODE_CTX* ctx = NULL;
unsigned char output[100];
int outputLen = 0;
int testDataLen = (int)XSTRLEN(testData);
/* Create and initialize the cipher context */ ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
ctx = wolfSSL_EVP_CIPHER_CTX_new(); ExpectIntEQ(ctx->remaining,0);
ExpectNotNull(ctx); ExpectIntEQ(ctx->data[0],0);
ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
/* Initialize with NULL cipher */ EVP_ENCODE_CTX_free(ctx);
ExpectIntEQ(wolfSSL_EVP_CipherInit_ex(ctx, wolfSSL_EVP_enc_null(), #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
NULL, NULL, NULL, 1), WOLFSSL_SUCCESS); return EXPECT_RESULT();
}
/* Test encryption (which should just copy the data) */ int test_wolfSSL_EVP_ENCODE_CTX_free(void)
ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, output, &outputLen, {
(const unsigned char*)testData, EXPECT_DECLS;
testDataLen), WOLFSSL_SUCCESS); #if defined(OPENSSL_EXTRA) && \
( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
/* Verify output length matches input length */ EVP_ENCODE_CTX* ctx = NULL;
ExpectIntEQ(outputLen, testDataLen);
/* Verify output data matches input data (no encryption occurred) */
ExpectIntEQ(XMEMCMP(output, testData, testDataLen), 0);
/* Clean up */
wolfSSL_EVP_CIPHER_CTX_free(ctx);
#endif /* OPENSSL_EXTRA */
ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
EVP_ENCODE_CTX_free(ctx);
#endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
return EXPECT_RESULT(); return EXPECT_RESULT();
} }
/* Test for wolfSSL_EVP_CIPHER_type_string() */ int test_wolfSSL_EVP_EncodeInit(void)
int test_wolfSSL_EVP_CIPHER_type_string(void)
{ {
EXPECT_DECLS; EXPECT_DECLS;
#ifdef OPENSSL_EXTRA #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
const char* cipherStr; EVP_ENCODE_CTX* ctx = NULL;
/* Test with valid cipher types */ ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
#ifdef HAVE_AES_CBC ExpectIntEQ(ctx->remaining, 0);
#ifdef WOLFSSL_AES_128 ExpectIntEQ(ctx->data[0], 0);
cipherStr = wolfSSL_EVP_CIPHER_type_string(WC_AES_128_CBC_TYPE); ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
ExpectNotNull(cipherStr);
ExpectStrEQ(cipherStr, "AES-128-CBC");
#endif
#endif
#ifndef NO_DES3 if (ctx != NULL) {
cipherStr = wolfSSL_EVP_CIPHER_type_string(WC_DES_CBC_TYPE); /* make ctx dirty */
ExpectNotNull(cipherStr); ctx->remaining = 10;
ExpectStrEQ(cipherStr, "DES-CBC"); XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
#endif }
/* Test with NULL cipher type */ EVP_EncodeInit(ctx);
cipherStr = wolfSSL_EVP_CIPHER_type_string(WC_NULL_CIPHER_TYPE);
ExpectNotNull(cipherStr);
ExpectStrEQ(cipherStr, "NULL");
/* Test with invalid cipher type */ ExpectIntEQ(ctx->remaining, 0);
cipherStr = wolfSSL_EVP_CIPHER_type_string(0xFFFF); ExpectIntEQ(ctx->data[0], 0);
ExpectNull(cipherStr); ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
#endif /* OPENSSL_EXTRA */
EVP_ENCODE_CTX_free(ctx);
#endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
return EXPECT_RESULT(); return EXPECT_RESULT();
} }
int test_wolfSSL_EVP_EncodeUpdate(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
int outl;
int total;
const unsigned char plain0[] = {"Th"};
const unsigned char plain1[] = {"This is a base64 encodeing test."};
const unsigned char plain2[] = {"This is additional data."};
const unsigned char encBlock0[] = {"VGg="};
const unsigned char enc0[] = {"VGg=\n"};
/* expected encoded result for the first output 64 chars plus trailing LF*/
const unsigned char enc1[] = {
"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"
};
const unsigned char enc2[] = {
"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"
"YWwgZGF0YS4=\n"
};
unsigned char encOutBuff[300];
EVP_ENCODE_CTX* ctx = NULL;
ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
EVP_EncodeInit(ctx);
/* illegal parameter test */
ExpectIntEQ(
EVP_EncodeUpdate(
NULL, /* pass NULL as ctx */
encOutBuff,
&outl,
plain1,
sizeof(plain1)-1),
0 /* expected result code 0: fail */
);
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
NULL, /* pass NULL as out buff */
&outl,
plain1,
sizeof(plain1)-1),
0 /* expected result code 0: fail */
);
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
encOutBuff,
NULL, /* pass NULL as outl */
plain1,
sizeof(plain1)-1),
0 /* expected result code 0: fail */
);
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
encOutBuff,
&outl,
NULL, /* pass NULL as in */
sizeof(plain1)-1),
0 /* expected result code 0: fail */
);
ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
/* meaningless parameter test */
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
encOutBuff,
&outl,
plain1,
0), /* pass zero input */
1 /* expected result code 1: success */
);
/* very small data encoding test */
EVP_EncodeInit(ctx);
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
encOutBuff,
&outl,
plain0,
sizeof(plain0)-1),
1 /* expected result code 1: success */
);
ExpectIntEQ(outl,0);
if (EXPECT_SUCCESS()) {
EVP_EncodeFinal(
ctx,
encOutBuff + outl,
&outl);
}
ExpectIntEQ( outl, sizeof(enc0)-1);
ExpectIntEQ(
XSTRNCMP(
(const char*)encOutBuff,
(const char*)enc0,sizeof(enc0) ),
0);
XMEMSET( encOutBuff,0, sizeof(encOutBuff));
ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
sizeof(encBlock0)-1);
ExpectStrEQ(encOutBuff, encBlock0);
/* pass small size( < 48bytes ) input, then make sure they are not
* encoded and just stored in ctx
*/
EVP_EncodeInit(ctx);
total = 0;
outl = 0;
XMEMSET( encOutBuff,0, sizeof(encOutBuff));
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
encOutBuff, /* buffer for output */
&outl, /* size of output */
plain1, /* input */
sizeof(plain1)-1), /* size of input */
1); /* expected result code 1:success */
total += outl;
ExpectIntEQ(outl, 0); /* no output expected */
ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
ExpectTrue(
XSTRNCMP((const char*)(ctx->data),
(const char*)plain1,
ctx->remaining) ==0 );
ExpectTrue(encOutBuff[0] == 0);
/* call wolfSSL_EVP_EncodeUpdate again to make it encode
* the stored data and the new input together
*/
ExpectIntEQ(
EVP_EncodeUpdate(
ctx,
encOutBuff + outl, /* buffer for output */
&outl, /* size of output */
plain2, /* additional input */
sizeof(plain2) -1), /* size of additional input */
1); /* expected result code 1:success */
total += outl;
ExpectIntNE(outl, 0); /* some output is expected this time*/
ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
ExpectIntEQ(
XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
/* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
EVP_EncodeFinal(
ctx,
encOutBuff + outl,
&outl);
total += outl;
ExpectIntNE(total,0);
ExpectIntNE(outl,0);
ExpectIntEQ(XSTRNCMP(
(const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
/* test with illeagal parameters */
outl = 1;
EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
ExpectIntEQ(outl, 0);
outl = 1;
EVP_EncodeFinal(ctx, NULL, &outl);
ExpectIntEQ(outl, 0);
EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
EVP_EncodeFinal(NULL, NULL, NULL);
EVP_ENCODE_CTX_free(ctx);
#endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_EncodeFinal(void)
{
int res = TEST_SKIPPED;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
/* tests for wolfSSL_EVP_EncodeFinal are included in
* test_wolfSSL_EVP_EncodeUpdate
*/
res = TEST_SUCCESS;
#endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
return res;
}
int test_wolfSSL_EVP_DecodeInit(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
EVP_ENCODE_CTX* ctx = NULL;
ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
ExpectIntEQ( ctx->remaining,0);
ExpectIntEQ( ctx->data[0],0);
ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
if (ctx != NULL) {
/* make ctx dirty */
ctx->remaining = 10;
XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
}
EVP_DecodeInit(ctx);
ExpectIntEQ( ctx->remaining,0);
ExpectIntEQ( ctx->data[0],0);
ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
EVP_ENCODE_CTX_free(ctx);
#endif /* OPENSSL && WOLFSSL_BASE_DECODE */
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_DecodeUpdate(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
int outl;
unsigned char decOutBuff[300];
EVP_ENCODE_CTX* ctx = NULL;
static const unsigned char enc1[] =
{"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
/* const unsigned char plain1[] =
{"This is a base64 decoding test."} */
ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
EVP_DecodeInit(ctx);
/* illegal parameter tests */
/* pass NULL as ctx */
ExpectIntEQ(
EVP_DecodeUpdate(
NULL, /* pass NULL as ctx */
decOutBuff,
&outl,
enc1,
sizeof(enc1)-1),
-1 /* expected result code -1: fail */
);
ExpectIntEQ( outl, 0);
/* pass NULL as output */
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
NULL, /* pass NULL as out buff */
&outl,
enc1,
sizeof(enc1)-1),
-1 /* expected result code -1: fail */
);
ExpectIntEQ( outl, 0);
/* pass NULL as outl */
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
NULL, /* pass NULL as outl */
enc1,
sizeof(enc1)-1),
-1 /* expected result code -1: fail */
);
/* pass NULL as input */
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
&outl,
NULL, /* pass NULL as in */
sizeof(enc1)-1),
-1 /* expected result code -1: fail */
);
ExpectIntEQ( outl, 0);
ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
/* pass zero length input */
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
&outl,
enc1,
0), /* pass zero as input len */
1 /* expected result code 1: success */
);
/* decode correct base64 string */
{
static const unsigned char enc2[] =
{"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
static const unsigned char plain2[] =
{"This is a base64 decoding test."};
EVP_EncodeInit(ctx);
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
&outl,
enc2,
sizeof(enc2)-1),
0 /* expected result code 0: success */
);
ExpectIntEQ(outl,sizeof(plain2) -1);
ExpectIntEQ(
EVP_DecodeFinal(
ctx,
decOutBuff + outl,
&outl),
1 /* expected result code 1: success */
);
ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
sizeof(plain2) -1 ),0);
ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
sizeof(plain2)-1);
ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
sizeof(plain2) -1 ),0);
}
/* decode correct base64 string which does not have '\n' in its last*/
{
static const unsigned char enc3[] =
{"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
static const unsigned char plain3[] =
{"This is a base64 decoding test."}; /* 31 chars */
EVP_EncodeInit(ctx);
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
&outl,
enc3,
sizeof(enc3)-1),
0 /* expected result code 0: success */
);
ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
sizeof(plain3) -1 ),0);
ExpectIntEQ(
EVP_DecodeFinal(
ctx,
decOutBuff + outl,
&outl),
1 /* expected result code 1: success */
);
ExpectIntEQ(outl,0 );
ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
sizeof(plain3)-1);
ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
sizeof(plain3) -1 ),0);
}
/* decode string which has a padding char ('=') in the illegal position*/
{
static const unsigned char enc4[] =
{"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
EVP_EncodeInit(ctx);
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
&outl,
enc4,
sizeof(enc4)-1),
-1 /* expected result code -1: error */
);
ExpectIntEQ(outl,0);
ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
}
/* small data decode test */
{
static const unsigned char enc00[] = {"VG"};
static const unsigned char enc01[] = {"g=\n"};
static const unsigned char plain4[] = {"Th"};
EVP_EncodeInit(ctx);
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff,
&outl,
enc00,
sizeof(enc00)-1),
1 /* expected result code 1: success */
);
ExpectIntEQ(outl,0);
ExpectIntEQ(
EVP_DecodeUpdate(
ctx,
decOutBuff + outl,
&outl,
enc01,
sizeof(enc01)-1),
0 /* expected result code 0: success */
);
ExpectIntEQ(outl,sizeof(plain4)-1);
/* test with illegal parameters */
ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
if (EXPECT_SUCCESS()) {
EVP_DecodeFinal(
ctx,
decOutBuff + outl,
&outl);
}
ExpectIntEQ( outl, 0);
ExpectIntEQ(
XSTRNCMP(
(const char*)decOutBuff,
(const char*)plain4,sizeof(plain4)-1 ),
0);
}
EVP_ENCODE_CTX_free(ctx);
#endif /* OPENSSL && WOLFSSL_BASE_DECODE */
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_DecodeFinal(void)
{
int res = TEST_SKIPPED;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
/* tests for wolfSSL_EVP_DecodeFinal are included in
* test_wolfSSL_EVP_DecodeUpdate
*/
res = TEST_SUCCESS;
#endif /* OPENSSL && WOLFSSL_BASE_DECODE */
return res;
}

View File

@@ -22,7 +22,25 @@
#ifndef WOLFSSL_TEST_EVP_H #ifndef WOLFSSL_TEST_EVP_H
#define WOLFSSL_TEST_EVP_H #define WOLFSSL_TEST_EVP_H
int test_wolfSSL_EVP_CipherUpdate_Null(void); #include <tests/api/api_decl.h>
int test_wolfSSL_EVP_CIPHER_type_string(void);
int test_wolfSSL_EVP_ENCODE_CTX_new(void);
int test_wolfSSL_EVP_ENCODE_CTX_free(void);
int test_wolfSSL_EVP_EncodeInit(void);
int test_wolfSSL_EVP_EncodeUpdate(void);
int test_wolfSSL_EVP_EncodeFinal(void);
int test_wolfSSL_EVP_DecodeInit(void);
int test_wolfSSL_EVP_DecodeUpdate(void);
int test_wolfSSL_EVP_DecodeFinal(void);
#define TEST_EVP_ENC_DECLS \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_ENCODE_CTX_new), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_ENCODE_CTX_free), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_EncodeInit), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_EncodeUpdate), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_EncodeFinal), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_DecodeInit), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_DecodeUpdate), \
TEST_DECL_GROUP("evp_enc", test_wolfSSL_EVP_DecodeFinal)
#endif /* WOLFSSL_TEST_EVP_H */ #endif /* WOLFSSL_TEST_EVP_H */

2704
tests/api/test_evp_cipher.c Normal file

File diff suppressed because it is too large Load Diff

108
tests/api/test_evp_cipher.h Normal file
View File

@@ -0,0 +1,108 @@
/* test_evp_cipher.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_EVP_CIPHER_H
#define WOLFCRYPT_TEST_EVP_CIPHER_H
#include <tests/api/api_decl.h>
int test_wolfSSL_EVP_CIPHER_CTX(void);
int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void);
int test_wolfSSL_EVP_CIPHER_CTX_key_length(void);
int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void);
int test_wolfSSL_EVP_get_cipherbynid(void);
int test_wolfSSL_EVP_CIPHER_block_size(void);
int test_wolfSSL_EVP_CIPHER_iv_length(void);
int test_wolfSSL_EVP_CipherUpdate_Null(void);
int test_wolfSSL_EVP_CIPHER_type_string(void);
int test_wolfSSL_EVP_BytesToKey(void);
int test_wolfSSL_EVP_Cipher_extra(void);
int test_wolfSSL_EVP_X_STATE(void);
int test_wolfSSL_EVP_X_STATE_LEN(void);
int test_wolfSSL_EVP_aes_256_gcm(void);
int test_wolfSSL_EVP_aes_192_gcm(void);
int test_wolfSSL_EVP_aes_128_gcm(void);
int test_evp_cipher_aes_gcm(void);
int test_wolfssl_EVP_aes_gcm(void);
int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void);
int test_wolfssl_EVP_aes_gcm_zeroLen(void);
int test_wolfSSL_EVP_aes_256_ccm(void);
int test_wolfSSL_EVP_aes_192_ccm(void);
int test_wolfSSL_EVP_aes_128_ccm(void);
int test_wolfssl_EVP_aes_ccm(void);
int test_wolfssl_EVP_aes_ccm_zeroLen(void);
int test_wolfssl_EVP_chacha20(void);
int test_wolfssl_EVP_chacha20_poly1305(void);
int test_wolfssl_EVP_aria_gcm(void);
int test_wolfssl_EVP_sm4_ecb(void);
int test_wolfssl_EVP_sm4_cbc(void);
int test_wolfssl_EVP_sm4_ctr(void);
int test_wolfssl_EVP_sm4_gcm_zeroLen(void);
int test_wolfssl_EVP_sm4_gcm(void);
int test_wolfssl_EVP_sm4_ccm_zeroLen(void);
int test_wolfssl_EVP_sm4_ccm(void);
int test_wolfSSL_EVP_rc4(void);
int test_wolfSSL_EVP_enc_null(void);
int test_wolfSSL_EVP_rc2_cbc(void);
int test_wolfSSL_EVP_mdc2(void);
#define TEST_EVP_CIPHER_DECLS \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_CTX), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_CTX_iv_length), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_CTX_key_length), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_CTX_set_iv), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_get_cipherbynid), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_block_size), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_iv_length), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CipherUpdate_Null), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_type_string), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_BytesToKey), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_Cipher_extra), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_X_STATE), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_X_STATE_LEN), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_aes_256_gcm), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_aes_192_gcm), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_aes_128_gcm), \
TEST_DECL_GROUP("evp_cipher", test_evp_cipher_aes_gcm), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_aes_gcm), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_aes_gcm_AAD_2_parts), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_aes_gcm_zeroLen), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_aes_256_ccm), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_aes_192_ccm), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_aes_128_ccm), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_aes_ccm), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_aes_ccm_zeroLen), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_chacha20), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_chacha20_poly1305), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_aria_gcm), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_ecb), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_cbc), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_ctr), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_gcm_zeroLen), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_gcm), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_ccm_zeroLen), \
TEST_DECL_GROUP("evp_cipher", test_wolfssl_EVP_sm4_ccm), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_rc4), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_enc_null), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_rc2_cbc), \
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_mdc2)
#endif /* WOLFCRYPT_TEST_EVP_CIPHER_H */

589
tests/api/test_evp_digest.c Normal file
View File

@@ -0,0 +1,589 @@
/* test_evp_digest.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <tests/unit.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/openssl/evp.h>
#include <tests/api/api.h>
#include <tests/api/test_evp_digest.h>
int test_wolfSSL_EVP_shake128(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
defined(WOLFSSL_SHAKE128)
const EVP_MD* md = NULL;
ExpectNotNull(md = EVP_shake128());
ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_shake256(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
defined(WOLFSSL_SHAKE256)
const EVP_MD* md = NULL;
ExpectNotNull(md = EVP_shake256());
ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
#endif
return EXPECT_RESULT();
}
/*
* Testing EVP digest API with SM3
*/
int test_wolfSSL_EVP_sm3(void)
{
int res = TEST_SKIPPED;
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
EXPECT_DECLS;
const EVP_MD* md = NULL;
EVP_MD_CTX* mdCtx = NULL;
byte data[WC_SM3_BLOCK_SIZE * 4];
byte hash[WC_SM3_DIGEST_SIZE];
byte calcHash[WC_SM3_DIGEST_SIZE];
byte expHash[WC_SM3_DIGEST_SIZE] = {
0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
};
word32 chunk;
word32 i;
unsigned int sz;
int ret;
XMEMSET(data, 0, sizeof(data));
md = EVP_sm3();
ExpectTrue(md != NULL);
ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
mdCtx = EVP_MD_CTX_new();
ExpectTrue(mdCtx != NULL);
/* Invalid Parameters */
ExpectIntEQ(EVP_DigestInit(NULL, md), WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
/* Valid Parameters */
ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
/* Valid Parameters */
ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
WOLFSSL_SUCCESS);
/* Ensure too many bytes for lengths. */
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
WOLFSSL_SUCCESS);
/* Invalid Parameters */
ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
/* Valid Parameters */
ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
/* Chunk tests. */
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
WOLFSSL_SUCCESS);
}
if (i < (word32)sizeof(data)) {
ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
(word32)sizeof(data) - i), WOLFSSL_SUCCESS);
}
ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
}
/* Not testing when the low 32-bit length overflows. */
ret = EVP_MD_CTX_cleanup(mdCtx);
ExpectIntEQ(ret, WOLFSSL_SUCCESS);
wolfSSL_EVP_MD_CTX_free(mdCtx);
res = EXPECT_RESULT();
#endif
return res;
} /* END test_EVP_sm3 */
int test_EVP_blake2(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
const EVP_MD* md = NULL;
(void)md;
#if defined(HAVE_BLAKE2)
ExpectNotNull(md = EVP_blake2b512());
ExpectIntEQ(XSTRNCMP(md, "BLAKE2b512", XSTRLEN("BLAKE2b512")), 0);
#endif
#if defined(HAVE_BLAKE2S)
ExpectNotNull(md = EVP_blake2s256());
ExpectIntEQ(XSTRNCMP(md, "BLAKE2s256", XSTRLEN("BLAKE2s256")), 0);
#endif
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_md4(void)
{
EXPECT_DECLS;
#if !defined(NO_MD4) && defined(OPENSSL_ALL)
ExpectNotNull(wolfSSL_EVP_md4());
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_ripemd160(void)
{
EXPECT_DECLS;
#if !defined(NO_WOLFSSL_STUB) && defined(OPENSSL_ALL)
ExpectNull(wolfSSL_EVP_ripemd160());
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_get_digestbynid(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_ALL
#ifndef NO_MD5
ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
#endif
#ifndef NO_SHA
ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
#endif
#ifndef NO_SHA256
ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
#endif
ExpectNull(wolfSSL_EVP_get_digestbynid(0));
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_Digest(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
const char* in = "abc";
int inLen = (int)XSTRLEN(in);
byte out[WC_SHA256_DIGEST_SIZE];
unsigned int outLen;
const char* expOut =
"\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
"\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
"\x15\xAD";
ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
"SHA256", NULL), 1);
ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
#endif /* OPEN_EXTRA && ! NO_SHA256 */
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_Digest_all(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_EXTRA
const char* digests[] = {
#ifndef NO_MD5
"MD5",
#endif
#ifndef NO_SHA
"SHA",
#endif
#ifdef WOLFSSL_SHA224
"SHA224",
#endif
#ifndef NO_SHA256
"SHA256",
#endif
#ifdef WOLFSSL_SHA384
"SHA384",
#endif
#ifdef WOLFSSL_SHA512
"SHA512",
#endif
#if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
"SHA512-224",
#endif
#if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
"SHA512-256",
#endif
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224
"SHA3-224",
#endif
#ifndef WOLFSSL_NOSHA3_256
"SHA3-256",
#endif
"SHA3-384",
#ifndef WOLFSSL_NOSHA3_512
"SHA3-512",
#endif
#endif /* WOLFSSL_SHA3 */
NULL
};
const char** d;
const unsigned char in[] = "abc";
int inLen = XSTR_SIZEOF(in);
byte out[WC_MAX_DIGEST_SIZE];
unsigned int outLen;
for (d = digests; *d != NULL; d++) {
ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
ExpectIntGT(outLen, 0);
ExpectIntEQ(EVP_MD_size(*d), outLen);
}
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_DigestFinal_ex(void)
{
EXPECT_DECLS;
#if !defined(NO_SHA256) && defined(OPENSSL_ALL)
WOLFSSL_EVP_MD_CTX mdCtx;
unsigned int s = 0;
unsigned char md[WC_SHA256_DIGEST_SIZE];
unsigned char md2[WC_SHA256_DIGEST_SIZE];
/* Bad Case */
#if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
(HAVE_FIPS_VERSION > 2))
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#else
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
#endif
/* Good Case */
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_DigestFinalXOF(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) && defined(OPENSSL_ALL)
WOLFSSL_EVP_MD_CTX mdCtx;
unsigned char shake[256];
unsigned char zeros[10];
unsigned char data[] = "Test data";
unsigned int sz;
XMEMSET(zeros, 0, sizeof(zeros));
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(EVP_DigestInit(&mdCtx, EVP_shake256()), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_MD_flags(EVP_shake256()), EVP_MD_FLAG_XOF);
ExpectIntEQ(EVP_MD_flags(EVP_sha3_256()), 0);
ExpectIntEQ(EVP_DigestUpdate(&mdCtx, data, 1), WOLFSSL_SUCCESS);
XMEMSET(shake, 0, sizeof(shake));
ExpectIntEQ(EVP_DigestFinalXOF(&mdCtx, shake, 10), WOLFSSL_SUCCESS);
/* make sure was only size of 10 */
ExpectIntEQ(XMEMCMP(&shake[11], zeros, 10), 0);
ExpectIntEQ(EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(EVP_DigestInit(&mdCtx, EVP_shake256()), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(&mdCtx, data, 1), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestFinal(&mdCtx, shake, &sz), WOLFSSL_SUCCESS);
ExpectIntEQ(sz, 32);
ExpectIntEQ(EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
#if defined(WOLFSSL_SHAKE128)
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(EVP_DigestInit(&mdCtx, EVP_shake128()), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestUpdate(&mdCtx, data, 1), WOLFSSL_SUCCESS);
ExpectIntEQ(EVP_DigestFinal(&mdCtx, shake, &sz), WOLFSSL_SUCCESS);
ExpectIntEQ(sz, 16);
ExpectIntEQ(EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
#endif
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EVP_MD_nid(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_ALL
#ifndef NO_MD5
ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
#endif
#ifndef NO_SHA
ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
#endif
#ifndef NO_SHA256
ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
#endif
ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
#endif
return EXPECT_RESULT();
}
#if defined(OPENSSL_EXTRA)
static void list_md_fn(const EVP_MD* m, const char* from,
const char* to, void* arg)
{
const char* mn;
BIO *bio;
(void) from;
(void) to;
(void) arg;
(void) mn;
(void) bio;
if (!m) {
/* alias */
AssertNull(m);
AssertNotNull(to);
}
else {
AssertNotNull(m);
AssertNull(to);
}
AssertNotNull(from);
#if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
mn = EVP_get_digestbyname(from);
/* print to stderr */
AssertNotNull(arg);
bio = BIO_new(BIO_s_file());
BIO_set_fp(bio, arg, BIO_NOCLOSE);
BIO_printf(bio, "Use %s message digest algorithm\n", mn);
BIO_free(bio);
#endif
}
#endif
int test_EVP_MD_do_all(void)
{
int res = TEST_SKIPPED;
#if defined(OPENSSL_EXTRA)
EVP_MD_do_all(NULL, stderr);
EVP_MD_do_all(list_md_fn, stderr);
res = TEST_SUCCESS;
#endif
return res;
}
int test_wolfSSL_EVP_MD_size(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_EXTRA
WOLFSSL_EVP_MD_CTX mdCtx;
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3-224"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#ifndef WOLFSSL_NOSHA3_256
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3-256"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3-384"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#ifndef WOLFSSL_NOSHA3_512
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3-512"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#endif /* WOLFSSL_SHA3 */
#ifndef NO_SHA256
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA256_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA256_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#ifndef NO_MD5
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_MD5_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_MD5_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#ifdef WOLFSSL_SHA224
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA224_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA224_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#ifdef WOLFSSL_SHA384
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA384_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA384_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#ifdef WOLFSSL_SHA512
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA512_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA512_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
#ifndef NO_SHA
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
WC_SHA_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif
/* error case */
wolfSSL_EVP_MD_CTX_init(&mdCtx);
ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), 0);
ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), 0);
ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), 0);
/* Cleanup is valid on uninit'ed struct */
ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
#endif /* OPENSSL_EXTRA */
return EXPECT_RESULT();
}

View File

@@ -0,0 +1,58 @@
/* test_evp_digest.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_EVP_DIGEST_H
#define WOLFCRYPT_TEST_EVP_DIGEST_H
#include <tests/api/api_decl.h>
int test_wolfSSL_EVP_shake128(void);
int test_wolfSSL_EVP_shake256(void);
int test_wolfSSL_EVP_sm3(void);
int test_EVP_blake2(void);
int test_wolfSSL_EVP_md4(void);
int test_wolfSSL_EVP_ripemd160(void);
int test_wolfSSL_EVP_get_digestbynid(void);
int test_wolfSSL_EVP_Digest(void);
int test_wolfSSL_EVP_Digest_all(void);
int test_wolfSSL_EVP_DigestFinal_ex(void);
int test_wolfSSL_EVP_DigestFinalXOF(void);
int test_wolfSSL_EVP_MD_nid(void);
int test_EVP_MD_do_all(void);
int test_wolfSSL_EVP_MD_size(void);
#define TEST_EVP_DIGEST_DECLS \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_shake128), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_shake256), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_sm3), \
TEST_DECL_GROUP("evp_digest", test_EVP_blake2), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_md4), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_ripemd160), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_get_digestbynid), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_Digest), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_Digest_all), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_DigestFinal_ex), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_DigestFinalXOF), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_MD_nid), \
TEST_DECL_GROUP("evp_digest", test_EVP_MD_do_all), \
TEST_DECL_GROUP("evp_digest", test_wolfSSL_EVP_MD_size)
#endif /* WOLFCRYPT_TEST_EVP_DIGEST_H */

2359
tests/api/test_evp_pkey.c Normal file

File diff suppressed because it is too large Load Diff

102
tests/api/test_evp_pkey.h Normal file
View File

@@ -0,0 +1,102 @@
/* test_evp_pkey.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_EVP_PKEY_H
#define WOLFCRYPT_TEST_EVP_PKEY_H
#include <tests/api/api_decl.h>
int test_wolfSSL_EVP_PKEY_CTX_new_id(void);
int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void);
int test_wolfSSL_QT_EVP_PKEY_CTX_free(void);
int test_wolfSSL_EVP_PKEY_up_ref(void);
int test_wolfSSL_EVP_PKEY_base_id(void);
int test_wolfSSL_EVP_PKEY_id(void);
int test_wolfSSL_EVP_MD_pkey_type(void);
int test_wolfSSL_EVP_MD_hmac_signing(void);
int test_wolfSSL_EVP_PKEY_new_mac_key(void);
int test_wolfSSL_EVP_PKEY_hkdf(void);
int test_wolfSSL_EVP_PBE_scrypt(void);
int test_EVP_PKEY_cmp(void);
int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void);
int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void);
int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void);
int test_wolfSSL_EVP_PKEY_set1_get1_DH (void);
int test_wolfSSL_EVP_PKEY_assign(void);
int test_wolfSSL_EVP_PKEY_assign_DH(void);
int test_EVP_PKEY_rsa(void);
int test_EVP_PKEY_ec(void);
int test_wolfSSL_EVP_PKEY_missing_parameters(void);
int test_wolfSSL_EVP_PKEY_copy_parameters(void);
int test_wolfSSL_EVP_PKEY_paramgen(void);
int test_wolfSSL_EVP_PKEY_param_check(void);
int test_wolfSSL_EVP_PKEY_keygen_init(void);
int test_wolfSSL_EVP_PKEY_keygen(void);
int test_wolfSSL_EVP_SignInit_ex(void);
int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void);
int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void);
int test_wolfSSL_EVP_PKEY_sign_verify_ec(void);
int test_wolfSSL_EVP_MD_rsa_signing(void);
int test_wc_RsaPSS_DigitalSignVerify(void);
int test_wolfSSL_EVP_MD_ecc_signing(void);
int test_wolfSSL_EVP_PKEY_encrypt(void);
int test_wolfSSL_EVP_PKEY_derive(void);
int test_wolfSSL_EVP_PKEY_print_public(void);
#define TEST_EVP_PKEY_DECLS \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_CTX_new_id), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),\
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_QT_EVP_PKEY_CTX_free), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_up_ref), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_base_id), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_id), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_pkey_type), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_hmac_signing), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_new_mac_key), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_hkdf), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PBE_scrypt), \
TEST_DECL_GROUP("evp_pkey", test_EVP_PKEY_cmp), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_set1_get1_DSA), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_get0_EC_KEY), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_set1_get1_DH), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_assign), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_assign_DH), \
TEST_DECL_GROUP("evp_pkey", test_EVP_PKEY_rsa), \
TEST_DECL_GROUP("evp_pkey", test_EVP_PKEY_ec), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_missing_parameters), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_copy_parameters), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_paramgen), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_param_check), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_keygen_init), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_keygen), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_SignInit_ex), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_sign_verify_rsa), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_sign_verify_dsa), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_sign_verify_ec), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_rsa_signing), \
TEST_DECL_GROUP("evp_pkey", test_wc_RsaPSS_DigitalSignVerify), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_ecc_signing), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_encrypt), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_derive), \
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_print_public)
#endif /* WOLFCRYPT_TEST_EVP_PKEY_H */

View File

@@ -1195,7 +1195,8 @@ int test_wolfSSL_ASN1_STRING_to_UTF8(void)
ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx)); ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e)); ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15); ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0); ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len),
0);
a = NULL; a = NULL;
/* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */ /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
@@ -1269,9 +1270,12 @@ int test_wolfSSL_ASN1_STRING_canon(void)
ExpectNotNull(canon = ASN1_STRING_new()); ExpectNotNull(canon = ASN1_STRING_new());
/* Invalid parameter testing. */ /* Invalid parameter testing. */
ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL),
ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1); ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0); ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
@@ -1622,9 +1626,12 @@ int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1); ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
/* Invalid parameters testing. */ /* Invalid parameters testing. */
ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL),
ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1); ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20); ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);

View File

@@ -58,7 +58,8 @@ int test_wolfSSL_BIO_gets(void)
/* try with bad args */ /* try with bad args */
ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg))); ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
#ifdef OPENSSL_ALL #ifdef OPENSSL_ALL
ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#endif #endif
/* try with real msg */ /* try with real msg */
@@ -594,7 +595,8 @@ int test_wolfSSL_BIO_tls(void)
int test_wolfSSL_BIO_datagram(void) int test_wolfSSL_BIO_datagram(void)
{ {
EXPECT_DECLS; EXPECT_DECLS;
#if !defined(NO_BIO) && defined(WOLFSSL_DTLS) && defined(WOLFSSL_HAVE_BIO_ADDR) && defined(OPENSSL_EXTRA) #if !defined(NO_BIO) && defined(WOLFSSL_DTLS) && \
defined(WOLFSSL_HAVE_BIO_ADDR) && defined(OPENSSL_EXTRA)
int ret; int ret;
SOCKET_T fd1 = SOCKET_INVALID, fd2 = SOCKET_INVALID; SOCKET_T fd1 = SOCKET_INVALID, fd2 = SOCKET_INVALID;
WOLFSSL_BIO *bio1 = NULL, *bio2 = NULL; WOLFSSL_BIO *bio1 = NULL, *bio2 = NULL;
@@ -636,7 +638,8 @@ int test_wolfSSL_BIO_datagram(void)
sin1.sin_port = 0; sin1.sin_port = 0;
slen = (socklen_t)sizeof(sin1); slen = (socklen_t)sizeof(sin1);
ExpectIntEQ(bind(fd1, (const struct sockaddr *)&sin1, slen), 0); ExpectIntEQ(bind(fd1, (const struct sockaddr *)&sin1, slen), 0);
ExpectIntEQ(setsockopt(fd1, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)), 0); ExpectIntEQ(setsockopt(fd1, SOL_SOCKET, SO_RCVTIMEO,
(const char *)&timeout, sizeof(timeout)), 0);
ExpectIntEQ(getsockname(fd1, (struct sockaddr *)&sin1, &slen), 0); ExpectIntEQ(getsockname(fd1, (struct sockaddr *)&sin1, &slen), 0);
} }
@@ -646,7 +649,8 @@ int test_wolfSSL_BIO_datagram(void)
sin2.sin_port = 0; sin2.sin_port = 0;
slen = (socklen_t)sizeof(sin2); slen = (socklen_t)sizeof(sin2);
ExpectIntEQ(bind(fd2, (const struct sockaddr *)&sin2, slen), 0); ExpectIntEQ(bind(fd2, (const struct sockaddr *)&sin2, slen), 0);
ExpectIntEQ(setsockopt(fd2, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)), 0); ExpectIntEQ(setsockopt(fd2, SOL_SOCKET, SO_RCVTIMEO,
(const char *)&timeout, sizeof(timeout)), 0);
ExpectIntEQ(getsockname(fd2, (struct sockaddr *)&sin2, &slen), 0); ExpectIntEQ(getsockname(fd2, (struct sockaddr *)&sin2, &slen), 0);
} }
@@ -661,15 +665,19 @@ int test_wolfSSL_BIO_datagram(void)
} }
if (EXPECT_SUCCESS()) { if (EXPECT_SUCCESS()) {
/* for OpenSSL compatibility, direct copying of sockaddrs into BIO_ADDRs must work right. */ /* for OpenSSL compatibility, direct copying of sockaddrs into BIO_ADDRs
* must work right. */
XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2)); XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2));
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_PEER, 0, bio_addr2), WOLFSSL_SUCCESS); ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_PEER, 0,
bio_addr2), WOLFSSL_SUCCESS);
wolfSSL_BIO_ADDR_clear(bio_addr2); wolfSSL_BIO_ADDR_clear(bio_addr2);
} }
test_msg_recvd[0] = 0; test_msg_recvd[0] = 0;
ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)), (int)sizeof(test_msg)); ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)),
ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg)); (int)sizeof(test_msg));
ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)),
(int)sizeof(test_msg));
ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0); ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
#ifdef WOLFSSL_BIO_HAVE_FLOW_STATS #ifdef WOLFSSL_BIO_HAVE_FLOW_STATS
@@ -682,58 +690,76 @@ int test_wolfSSL_BIO_datagram(void)
*/ */
test_msg_recvd[0] = 0; test_msg_recvd[0] = 0;
ExpectIntEQ(wolfSSL_BIO_write(bio2, test_msg, sizeof(test_msg)), (int)sizeof(test_msg)); ExpectIntEQ(wolfSSL_BIO_write(bio2, test_msg, sizeof(test_msg)),
ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg)); (int)sizeof(test_msg));
ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)),
(int)sizeof(test_msg));
ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0); ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)), WOLFSSL_BIO_ERROR); ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)),
WOLFSSL_BIO_ERROR);
ExpectIntNE(BIO_should_retry(bio1), 0); ExpectIntNE(BIO_should_retry(bio1), 0);
ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)), WOLFSSL_BIO_ERROR); ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)),
WOLFSSL_BIO_ERROR);
ExpectIntNE(BIO_should_retry(bio2), 0); ExpectIntNE(BIO_should_retry(bio2), 0);
/* now "connect" the sockets. */ /* now "connect" the sockets. */
ExpectIntEQ(connect(fd1, (const struct sockaddr *)&sin2, (socklen_t)sizeof(sin2)), 0); ExpectIntEQ(connect(fd1, (const struct sockaddr *)&sin2,
ExpectIntEQ(connect(fd2, (const struct sockaddr *)&sin1, (socklen_t)sizeof(sin1)), 0); (socklen_t)sizeof(sin2)), 0);
ExpectIntEQ(connect(fd2, (const struct sockaddr *)&sin1,
(socklen_t)sizeof(sin1)), 0);
if (EXPECT_SUCCESS()) { if (EXPECT_SUCCESS()) {
XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2)); XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2));
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_CONNECTED, 0, bio_addr2), WOLFSSL_SUCCESS); ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_CONNECTED, 0,
bio_addr2), WOLFSSL_SUCCESS);
wolfSSL_BIO_ADDR_clear(bio_addr2); wolfSSL_BIO_ADDR_clear(bio_addr2);
} }
if (EXPECT_SUCCESS()) { if (EXPECT_SUCCESS()) {
XMEMCPY(&bio_addr1->sa_in, &sin1, sizeof(sin1)); XMEMCPY(&bio_addr1->sa_in, &sin1, sizeof(sin1));
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio2, BIO_CTRL_DGRAM_SET_CONNECTED, 0, bio_addr1), WOLFSSL_SUCCESS); ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio2, BIO_CTRL_DGRAM_SET_CONNECTED, 0,
bio_addr1), WOLFSSL_SUCCESS);
wolfSSL_BIO_ADDR_clear(bio_addr1); wolfSSL_BIO_ADDR_clear(bio_addr1);
} }
test_msg_recvd[0] = 0; test_msg_recvd[0] = 0;
ExpectIntEQ(wolfSSL_BIO_write(bio2, test_msg, sizeof(test_msg)), (int)sizeof(test_msg)); ExpectIntEQ(wolfSSL_BIO_write(bio2, test_msg, sizeof(test_msg)),
ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg)); (int)sizeof(test_msg));
ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)),
(int)sizeof(test_msg));
ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0); ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
test_msg_recvd[0] = 0; test_msg_recvd[0] = 0;
ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)), (int)sizeof(test_msg)); ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)),
ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg)); (int)sizeof(test_msg));
ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)),
(int)sizeof(test_msg));
ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0); ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
#ifdef __linux__ #ifdef __linux__
/* now "disconnect" the sockets and attempt transmits expected to fail. */ /* now "disconnect" the sockets and attempt transmits expected to fail. */
sin1.sin_family = AF_UNSPEC; sin1.sin_family = AF_UNSPEC;
ExpectIntEQ(connect(fd1, (const struct sockaddr *)&sin1, (socklen_t)sizeof(sin1)), 0); ExpectIntEQ(connect(fd1, (const struct sockaddr *)&sin1,
ExpectIntEQ(connect(fd2, (const struct sockaddr *)&sin1, (socklen_t)sizeof(sin1)), 0); (socklen_t)sizeof(sin1)), 0);
ExpectIntEQ(connect(fd2, (const struct sockaddr *)&sin1,
(socklen_t)sizeof(sin1)), 0);
sin1.sin_family = AF_INET; sin1.sin_family = AF_INET;
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_CONNECTED, 0, NULL), WOLFSSL_SUCCESS); ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_CONNECTED, 0,
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio2, BIO_CTRL_DGRAM_SET_CONNECTED, 0, NULL), WOLFSSL_SUCCESS); NULL), WOLFSSL_SUCCESS);
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio2, BIO_CTRL_DGRAM_SET_CONNECTED, 0,
NULL), WOLFSSL_SUCCESS);
if (EXPECT_SUCCESS()) { if (EXPECT_SUCCESS()) {
sin2.sin_addr.s_addr = htonl(0xc0a8c0a8); /* 192.168.192.168 -- invalid for loopback interface. */ /* 192.168.192.168 -- invalid for loopback interface. */
sin2.sin_addr.s_addr = htonl(0xc0a8c0a8);
XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2)); XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2));
ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_PEER, 0, bio_addr2), WOLFSSL_SUCCESS); ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_PEER, 0,
bio_addr2), WOLFSSL_SUCCESS);
wolfSSL_BIO_ADDR_clear(bio_addr2); wolfSSL_BIO_ADDR_clear(bio_addr2);
} }
@@ -796,7 +822,8 @@ static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
(void)args; (void)args;
AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0); AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP,
wolfSSLPort), 0);
clientBio = BIO_new_connect(connectAddr); clientBio = BIO_new_connect(connectAddr);
AssertNotNull(clientBio); AssertNotNull(clientBio);
AssertIntEQ(BIO_do_connect(clientBio), 1); AssertIntEQ(BIO_do_connect(clientBio), 1);
@@ -804,7 +831,8 @@ static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
AssertNotNull(ctx); AssertNotNull(ctx);
sslClient = SSL_new(ctx); sslClient = SSL_new(ctx);
AssertNotNull(sslClient); AssertNotNull(sslClient);
AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS); AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
WOLFSSL_SUCCESS);
SSL_set_bio(sslClient, clientBio, clientBio); SSL_set_bio(sslClient, clientBio, clientBio);
AssertIntEQ(SSL_connect(sslClient), 1); AssertIntEQ(SSL_connect(sslClient), 1);
@@ -1156,5 +1184,287 @@ int test_wolfSSL_BIO_get_len(void)
return EXPECT_RESULT(); return EXPECT_RESULT();
} }
#if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
long argl, long ret)
{
(void)bio;
(void)cmd;
(void)argp;
(void)argi;
(void)argl;
return ret;
}
#endif
int test_wolfSSL_BIO(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
const unsigned char* p = NULL;
byte buff[20];
BIO* bio1 = NULL;
BIO* bio2 = NULL;
BIO* bio3 = NULL;
char* bufPt = NULL;
int i;
for (i = 0; i < 20; i++) {
buff[i] = i;
}
/* test BIO_free with NULL */
ExpectIntEQ(BIO_free(NULL), WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
/* Creating and testing type BIO_s_bio */
ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
/* read/write before set up */
ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
ExpectNotNull(XMEMCPY(bufPt, buff, 10));
ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
/* write buffer full */
ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
/* write the other direction with pair */
ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
ExpectNotNull(XMEMCPY(bufPt, buff, 8));
ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
/* try read */
ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
/* try read using ctrl function */
ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
for (i = 0; i < 20; i++) {
ExpectIntEQ((int)bufPt[i], i);
}
ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
for (i = 0; i < 8; i++) {
ExpectIntEQ((int)bufPt[i], i);
}
ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
/* new pair */
ExpectIntEQ(BIO_make_bio_pair(bio1, bio3),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
BIO_free(bio2); /* free bio2 and automatically remove from pair */
bio2 = NULL;
ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
/* test wrap around... */
ExpectIntEQ(BIO_reset(bio1), 1);
ExpectIntEQ(BIO_reset(bio3), 1);
/* fill write buffer, read only small amount then write again */
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
ExpectNotNull(XMEMCPY(bufPt, buff, 20));
ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
for (i = 0; i < 4; i++) {
ExpectIntEQ(bufPt[i], i);
}
/* try writing over read index */
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
ExpectNotNull(XMEMSET(bufPt, 0, 4));
ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
/* read and write 0 bytes */
ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
/* should read only to end of write buffer then need to read again */
ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
for (i = 0; i < 16; i++) {
ExpectIntEQ(bufPt[i], buff[4 + i]);
}
ExpectIntEQ(BIO_nread(bio3, NULL, 0), WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
for (i = 0; i < 4; i++) {
ExpectIntEQ(bufPt[i], 0);
}
/* read index should not have advanced with nread0 */
ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
for (i = 0; i < 4; i++) {
ExpectIntEQ(bufPt[i], 0);
}
/* write and fill up buffer checking reset of index state */
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
ExpectNotNull(XMEMCPY(bufPt, buff, 20));
/* test reset on data in bio1 write buffer */
ExpectIntEQ(BIO_reset(bio1), 1);
ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
ExpectNotNull(p);
ExpectNotNull(XMEMCPY(bufPt, buff, 20));
ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
for (i = 0; i < 6; i++) {
ExpectIntEQ(bufPt[i], i);
}
/* test case of writing twice with offset read index */
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
BIO_free(bio1);
bio1 = NULL;
BIO_free(bio3);
bio3 = NULL;
#if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
{
BIO* bioA = NULL;
BIO* bioB = NULL;
ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
BIO_free(bioA);
bioA = NULL;
BIO_free(bioB);
bioB = NULL;
}
#endif /* OPENSSL_ALL || WOLFSSL_ASIO */
/* BIOs with file pointers */
#if !defined(NO_FILESYSTEM)
{
XFILE f1 = XBADFILE;
XFILE f2 = XBADFILE;
BIO* f_bio1 = NULL;
BIO* f_bio2 = NULL;
unsigned char cert[300];
char testFile[] = "tests/bio_write_test.txt";
char msg[] = "bio_write_test.txt contains the first 300 bytes of "
"certs/server-cert.pem\n"
"created by tests/unit.test\n\n";
ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
/* Failure due to wrong BIO type */
ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
ExpectTrue((f1 = XFOPEN(svrCertFile, "rb+")) != XBADFILE);
ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_reset(f_bio2), 1);
ExpectIntEQ(BIO_tell(NULL),-1);
ExpectIntEQ(BIO_tell(f_bio2),0);
ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
ExpectIntEQ(BIO_tell(f_bio2),4);
BIO_free(f_bio1);
f_bio1 = NULL;
BIO_free(f_bio2);
f_bio2 = NULL;
ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rb+"));
ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
BIO_free(f_bio1);
f_bio1 = NULL;
}
#endif /* !defined(NO_FILESYSTEM) */
/* BIO info callback */
{
const char* testArg = "test";
BIO* cb_bio = NULL;
ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
BIO_set_callback(cb_bio, bioCallback);
ExpectNotNull(BIO_get_callback(cb_bio));
BIO_set_callback(cb_bio, NULL);
ExpectNull(BIO_get_callback(cb_bio));
BIO_set_callback_arg(cb_bio, (char*)testArg);
ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
ExpectNull(BIO_get_callback_arg(NULL));
BIO_free(cb_bio);
cb_bio = NULL;
}
/* BIO_vfree */
ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
BIO_vfree(NULL);
BIO_vfree(bio1);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_BIO_BIO_ring_read(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_ALL) && !defined(NO_BIO)
BIO* bio1 = NULL;
BIO* bio2 = NULL;
byte data[50];
byte tmp[50];
XMEMSET(data, 42, sizeof(data));
ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
SSL_SUCCESS);
ExpectIntEQ(BIO_write(bio1, data, 40), 40);
ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
ExpectBufEQ(tmp, data, 20);
ExpectIntEQ(BIO_write(bio1, data, 20), 20);
ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
ExpectBufEQ(tmp, data, 40);
BIO_free(bio1);
BIO_free(bio2);
#endif
return EXPECT_RESULT();
}
#endif /* !NO_BIO */ #endif /* !NO_BIO */

View File

@@ -40,6 +40,8 @@ int test_wolfSSL_BIO_f_md(void);
int test_wolfSSL_BIO_up_ref(void); int test_wolfSSL_BIO_up_ref(void);
int test_wolfSSL_BIO_reset(void); int test_wolfSSL_BIO_reset(void);
int test_wolfSSL_BIO_get_len(void); int test_wolfSSL_BIO_get_len(void);
int test_wolfSSL_BIO(void);
int test_wolfSSL_BIO_BIO_ring_read(void);
#define TEST_OSSL_BIO_DECLS \ #define TEST_OSSL_BIO_DECLS \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_gets), \ TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_gets), \
@@ -52,7 +54,9 @@ int test_wolfSSL_BIO_get_len(void);
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_f_md), \ TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_f_md), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_up_ref), \ TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_up_ref), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_reset), \ TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_reset), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_get_len) TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_get_len), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_BIO_ring_read)
#define TEST_OSSL_BIO_TLS_DECLS \ #define TEST_OSSL_BIO_TLS_DECLS \
TEST_DECL_GROUP("ossl_bio_tls", test_wolfSSL_BIO_connect), \ TEST_DECL_GROUP("ossl_bio_tls", test_wolfSSL_BIO_connect), \

View File

@@ -217,14 +217,16 @@ int test_wolfSSL_BN_init(void)
ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS); ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
/* a^b mod c = */ /* a^b mod c = */
ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS); ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
/* check result 3^2 mod 5 */ /* check result 3^2 mod 5 */
ExpectIntEQ(BN_get_word(&dv), 4); ExpectIntEQ(BN_get_word(&dv), 4);
/* a*b mod c = */ /* a*b mod c = */
ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS); ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
/* check result 3*2 mod 5 */ /* check result 3*2 mod 5 */
@@ -1027,7 +1029,8 @@ int test_wolfSSL_BN_prime(void)
EXPECT_DECLS; EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \ #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
!defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH) !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
#if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA)) #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
!defined(NO_DSA))
BIGNUM* a = NULL; BIGNUM* a = NULL;
BIGNUM* add = NULL; BIGNUM* add = NULL;
BIGNUM* rem = NULL; BIGNUM* rem = NULL;

View File

@@ -161,8 +161,10 @@ int test_wolfSSL_MD5_Transform(void)
ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0); ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0); ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
ExpectIntEQ(wc_Md5Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Md5Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1),
ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Md5Transform(&md5.native, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Init MD5 CTX */ /* Init MD5 CTX */
ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1); ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
@@ -359,8 +361,10 @@ int test_wolfSSL_SHA_Transform(void)
ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0); ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0); ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
ExpectIntEQ(wc_ShaTransform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_ShaTransform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1),
ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_ShaTransform(&sha.native, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Init SHA CTX */ /* Init SHA CTX */
ExpectIntEQ(SHA_Init(&sha.compat), 1); ExpectIntEQ(SHA_Init(&sha.compat), 1);
@@ -500,8 +504,10 @@ int test_wolfSSL_SHA256_Transform(void)
ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0); ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0); ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
ExpectIntEQ(wc_Sha256Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha256Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1),
ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Init SHA256 CTX */ /* Init SHA256 CTX */
ExpectIntEQ(SHA256_Init(&sha256.compat), 1); ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
@@ -574,8 +580,10 @@ int test_wolfSSL_SHA512_Transform(void)
ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0); ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0); ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
ExpectIntEQ(wc_Sha512Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha512Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1),
ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Init SHA512 CTX */ /* Init SHA512 CTX */
ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1); ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
@@ -584,8 +592,8 @@ int test_wolfSSL_SHA512_Transform(void)
sLen = (word32)XSTRLEN((char*)input1); sLen = (word32)XSTRLEN((char*)input1);
XMEMCPY(local, input1, sLen); XMEMCPY(local, input1, sLen);
ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1); ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
ExpectIntEQ(XMEMCMP(sha512.native.digest, output1, ExpectIntEQ(XMEMCMP(sha512.native.digest, output1, WC_SHA512_DIGEST_SIZE),
WC_SHA512_DIGEST_SIZE), 0); 0);
ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */ ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
/* Init SHA512 CTX */ /* Init SHA512 CTX */
@@ -594,8 +602,8 @@ int test_wolfSSL_SHA512_Transform(void)
XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE); XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
XMEMCPY(local, input2, sLen); XMEMCPY(local, input2, sLen);
ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1); ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
ExpectIntEQ(XMEMCMP(sha512.native.digest, output2, ExpectIntEQ(XMEMCMP(sha512.native.digest, output2, WC_SHA512_DIGEST_SIZE),
WC_SHA512_DIGEST_SIZE), 0); 0);
ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */ ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
(void)input1; (void)input1;
@@ -643,10 +651,12 @@ int test_wolfSSL_SHA512_224_Transform(void)
ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0); ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0); ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0); ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1), ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Init SHA512 CTX */ /* Init SHA512 CTX */
ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1); ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
@@ -716,10 +726,12 @@ int test_wolfSSL_SHA512_256_Transform(void)
ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0); ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0); ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0); ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1), ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Init SHA512 CTX */ /* Init SHA512 CTX */
ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1); ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);

View File

@@ -1345,12 +1345,15 @@ int test_wolfSSL_EC_KEY_print_fp(void)
EC_KEY* key = NULL; EC_KEY* key = NULL;
/* Bad file pointer. */ /* Bad file pointer. */
ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
/* NULL key. */ /* NULL key. */
ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1))); ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
/* Negative indent. */ /* Negative indent. */
ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS); ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS); ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);

465
tests/api/test_ossl_obj.c Normal file
View File

@@ -0,0 +1,465 @@
/* test_ossl_obj.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <tests/unit.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/openssl/objects.h>
#include <wolfssl/openssl/pkcs12.h>
#include <tests/api/api.h>
#include <tests/api/test_ossl_obj.h>
#if defined(OPENSSL_EXTRA)
static void obj_name_t(const OBJ_NAME* nm, void* arg)
{
(void)arg;
(void)nm;
AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
#if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
/* print to stderr */
AssertNotNull(arg);
BIO *bio = BIO_new(BIO_s_file());
BIO_set_fp(bio, arg, BIO_NOCLOSE);
BIO_printf(bio, "%s\n", nm);
BIO_free(bio);
#endif
}
#endif
int test_OBJ_NAME_do_all(void)
{
int res = TEST_SKIPPED;
#if defined(OPENSSL_EXTRA)
OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
OBJ_NAME_do_all(-1, obj_name_t, stderr);
res = TEST_SUCCESS;
#endif
return res;
}
int test_wolfSSL_OBJ(void)
{
/* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
* mode
*/
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
!defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
ASN1_OBJECT *obj = NULL;
ASN1_OBJECT *obj2 = NULL;
char buf[50];
XFILE fp = XBADFILE;
X509 *x509 = NULL;
X509_NAME *x509Name = NULL;
X509_NAME_ENTRY *x509NameEntry = NULL;
ASN1_OBJECT *asn1Name = NULL;
int numNames = 0;
BIO *bio = NULL;
int nid;
int i, j;
const char *f[] = {
#ifndef NO_RSA
"./certs/ca-cert.der",
#endif
#ifdef HAVE_ECC
"./certs/ca-ecc-cert.der",
"./certs/ca-ecc384-cert.der",
#endif
NULL};
ASN1_OBJECT *field_name_obj = NULL;
int lastpos = -1;
int tmp = -1;
ASN1_STRING *asn1 = NULL;
unsigned char *buf_dyn = NULL;
ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1),
WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
ASN1_OBJECT_free(obj);
obj = NULL;
ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
#ifdef WOLFSSL_CERT_EXT
ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
#endif
ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
ExpectNotNull(obj2 = OBJ_dup(obj));
ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
ASN1_OBJECT_free(obj);
obj = NULL;
ASN1_OBJECT_free(obj2);
obj2 = NULL;
for (i = 0; f[i] != NULL; i++)
{
ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
if (fp != XBADFILE) {
XFCLOSE(fp);
fp = XBADFILE;
}
ExpectNotNull(x509Name = X509_get_issuer_name(x509));
ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
/* Get the Common Name by using OBJ_txt2obj */
ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
ExpectIntEQ(X509_NAME_get_index_by_OBJ(NULL, NULL, 99),
WOLFSSL_FATAL_ERROR);
ExpectIntEQ(X509_NAME_get_index_by_OBJ(x509Name, NULL, 99),
WOLFSSL_FATAL_ERROR);
ExpectIntEQ(X509_NAME_get_index_by_OBJ(NULL, field_name_obj, 99),
WOLFSSL_FATAL_ERROR);
ExpectIntEQ(X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, 99),
WOLFSSL_FATAL_ERROR);
ExpectIntEQ(X509_NAME_get_index_by_OBJ(x509Name, NULL, 0),
WOLFSSL_FATAL_ERROR);
do
{
lastpos = tmp;
tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
} while (tmp > -1);
ExpectIntNE(lastpos, -1);
ASN1_OBJECT_free(field_name_obj);
field_name_obj = NULL;
ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
/*
* All Common Names should be www.wolfssl.com
* This makes testing easier as we can test for the expected value.
*/
ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
OPENSSL_free(buf_dyn);
buf_dyn = NULL;
bio = BIO_new(BIO_s_mem());
ExpectTrue(bio != NULL);
for (j = 0; j < numNames; j++)
{
ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
}
BIO_free(bio);
bio = NULL;
X509_free(x509);
x509 = NULL;
}
#ifdef HAVE_PKCS12
{
PKCS12 *p12 = NULL;
int boolRet;
EVP_PKEY *pkey = NULL;
const char *p12_f[] = {
/* bundle uses AES-CBC 256 and PKCS7 key uses DES3 */
#if !defined(NO_DES3) && defined(WOLFSSL_AES_256) && !defined(NO_RSA)
"./certs/test-servercert.p12",
#endif
NULL
};
for (i = 0; p12_f[i] != NULL; i++)
{
ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
if (fp != XBADFILE) {
XFCLOSE(fp);
fp = XBADFILE;
}
ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
&pkey, &x509, NULL)) > 0);
wc_PKCS12_free(p12);
p12 = NULL;
EVP_PKEY_free(pkey);
x509Name = X509_get_issuer_name(x509);
ExpectNotNull(x509Name);
ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
for (j = 0; j < numNames; j++)
{
ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
ExpectNotNull(asn1Name =
X509_NAME_ENTRY_get_object(x509NameEntry));
ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
}
BIO_free(bio);
bio = NULL;
X509_free(x509);
x509 = NULL;
}
}
#endif /* HAVE_PKCS12 */
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_OBJ_cmp(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
ASN1_OBJECT *obj = NULL;
ASN1_OBJECT *obj2 = NULL;
ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
ExpectIntEQ(OBJ_cmp(NULL, NULL), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
ExpectIntEQ(OBJ_cmp(obj, NULL), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
ExpectIntEQ(OBJ_cmp(NULL, obj2), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
ExpectIntEQ(OBJ_cmp(obj, obj2), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
ExpectIntEQ(OBJ_cmp(obj, obj), 0);
ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
ASN1_OBJECT_free(obj);
ASN1_OBJECT_free(obj2);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_OBJ_txt2nid(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
defined(WOLFSSL_APACHE_HTTPD)
int i;
static const struct {
const char* sn;
const char* ln;
const char* oid;
int nid;
} testVals[] = {
#ifdef WOLFSSL_APACHE_HTTPD
{ "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
{ "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
NID_id_on_dnsSRV },
{ "msUPN", "Microsoft User Principal Name",
"1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
#endif
{ NULL, NULL, NULL, NID_undef }
};
/* Invalid cases */
ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
/* Valid cases */
for (i = 0; testVals[i].sn != NULL; i++) {
ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
}
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_OBJ_txt2obj(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
int i;
char buf[50];
ASN1_OBJECT* obj = NULL;
static const struct {
const char* oidStr;
const char* sn;
const char* ln;
} objs_list[] = {
#if defined(WOLFSSL_APACHE_HTTPD)
{ "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
{ "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
#endif
{ "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
{ NULL, NULL, NULL }
};
static const struct {
const char* numeric;
const char* name;
} objs_named[] = {
/* In dictionary but not in normal list. */
{ "1.3.6.1.5.5.7.3.8", "Time Stamping" },
/* Made up OID. */
{ "1.3.5.7", "1.3.5.7" },
{ NULL, NULL }
};
ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
ASN1_OBJECT_free(obj);
obj = NULL;
ExpectNull(obj = OBJ_txt2obj(NULL, 0));
ASN1_OBJECT_free(obj);
obj = NULL;
for (i = 0; objs_list[i].oidStr != NULL; i++) {
/* Test numerical value of oid (oidStr) */
ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
/* Convert object back to text to confirm oid is correct */
wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
ASN1_OBJECT_free(obj);
obj = NULL;
XMEMSET(buf, 0, sizeof(buf));
/* Test short name (sn) */
ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
/* Convert object back to text to confirm oid is correct */
wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
ASN1_OBJECT_free(obj);
obj = NULL;
XMEMSET(buf, 0, sizeof(buf));
/* Test long name (ln) - should fail when no_name = 1 */
ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
/* Convert object back to text to confirm oid is correct */
wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
ASN1_OBJECT_free(obj);
obj = NULL;
XMEMSET(buf, 0, sizeof(buf));
}
for (i = 0; objs_named[i].numeric != NULL; i++) {
ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
ASN1_OBJECT_free(obj);
obj = NULL;
}
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_OBJ_ln(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_ALL
const int nid_set[] = {
NID_commonName,
NID_serialNumber,
NID_countryName,
NID_localityName,
NID_stateOrProvinceName,
NID_organizationName,
NID_organizationalUnitName,
NID_domainComponent,
NID_businessCategory,
NID_jurisdictionCountryName,
NID_jurisdictionStateOrProvinceName,
NID_emailAddress
};
const char* ln_set[] = {
"commonName",
"serialNumber",
"countryName",
"localityName",
"stateOrProvinceName",
"organizationName",
"organizationalUnitName",
"domainComponent",
"businessCategory",
"jurisdictionCountryName",
"jurisdictionStateOrProvinceName",
"emailAddress",
};
size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
#ifdef HAVE_ECC
#if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
{
EC_builtin_curve r[27];
size_t nCurves = sizeof(r) / sizeof(r[0]);
nCurves = EC_get_builtin_curves(r, nCurves);
for (i = 0; i < nCurves; i++) {
/* skip ECC_CURVE_INVALID */
if (r[i].nid != ECC_CURVE_INVALID) {
ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
}
}
}
#endif
#endif
for (i = 0; i < maxIdx; i++) {
ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
}
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_OBJ_sn(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_ALL
int i = 0, maxIdx = 7;
const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
NID_stateOrProvinceName,NID_organizationName,
NID_organizationalUnitName,NID_emailAddress};
const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
for (i = 0; i < maxIdx; i++) {
ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_open_set[i]), nid_set[i]);
ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
}
#endif
return EXPECT_RESULT();
}

45
tests/api/test_ossl_obj.h Normal file
View File

@@ -0,0 +1,45 @@
/* test_ossl_obj.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_OSSL_OBJ_H
#define WOLFCRYPT_TEST_OSSL_OBJ_H
#include <tests/api/api_decl.h>
int test_OBJ_NAME_do_all(void);
int test_wolfSSL_OBJ(void);
int test_wolfSSL_OBJ_cmp(void);
int test_wolfSSL_OBJ_txt2nid(void);
int test_wolfSSL_OBJ_txt2obj(void);
int test_wolfSSL_OBJ_ln(void);
int test_wolfSSL_OBJ_sn(void);
#define TEST_OSSL_OBJ_DECLS \
TEST_DECL_GROUP("ossl_obj", test_OBJ_NAME_do_all), \
TEST_DECL_GROUP("ossl_obj", test_wolfSSL_OBJ), \
TEST_DECL_GROUP("ossl_obj", test_wolfSSL_OBJ_cmp), \
TEST_DECL_GROUP("ossl_obj", test_wolfSSL_OBJ_txt2nid), \
TEST_DECL_GROUP("ossl_obj", test_wolfSSL_OBJ_txt2obj), \
TEST_DECL_GROUP("ossl_obj", test_wolfSSL_OBJ_ln), \
TEST_DECL_GROUP("ossl_obj", test_wolfSSL_OBJ_sn)
#endif /* WOLFCRYPT_TEST_OSSL_OBJ_H */

1321
tests/api/test_ossl_p7p12.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,54 @@
/* test_ossl_p7p12.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_OSSL_P7P12_H
#define WOLFCRYPT_TEST_OSSL_P7P12_H
#include <tests/api/api_decl.h>
int test_wolfssl_PKCS7(void);
int test_wolfSSL_PKCS7_certs(void);
int test_wolfSSL_PKCS7_sign(void);
int test_wolfSSL_PKCS7_SIGNED_new(void);
int test_wolfSSL_PEM_write_bio_PKCS7(void);
int test_wolfSSL_PEM_write_bio_encryptedKey(void);
int test_wolfSSL_SMIME_read_PKCS7(void);
int test_wolfSSL_SMIME_write_PKCS7(void);
int test_wolfSSL_PKCS12(void);
#define TEST_OSSL_PKCS7_DECLS \
TEST_DECL_GROUP("ossl_p7", test_wolfssl_PKCS7), \
TEST_DECL_GROUP("ossl_p7", test_wolfSSL_PKCS7_certs), \
TEST_DECL_GROUP("ossl_p7", test_wolfSSL_PKCS7_sign), \
TEST_DECL_GROUP("ossl_p7", test_wolfSSL_PKCS7_SIGNED_new), \
TEST_DECL_GROUP("ossl_p7", test_wolfSSL_PEM_write_bio_PKCS7), \
TEST_DECL_GROUP("ossl_p7", test_wolfSSL_PEM_write_bio_encryptedKey), \
TEST_DECL_GROUP("ossl_p7", test_wolfSSL_RAND_poll)
#define TEST_OSSL_SMIME_DECLS \
TEST_DECL_GROUP("ossl_smime", test_wolfSSL_SMIME_read_PKCS7), \
TEST_DECL_GROUP("ossl_smime", test_wolfSSL_SMIME_write_PKCS7)
#define TEST_OSSL_PKCS12_DECLS \
TEST_DECL_GROUP("ossl_p12", test_wolfSSL_PKCS12)
#endif /* WOLFCRYPT_TEST_OSSL_P7P12_H */

340
tests/api/test_ossl_rand.c Normal file
View File

@@ -0,0 +1,340 @@
/* test_ossl_rand.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <tests/unit.h>
#if defined(__linux__) || defined(__FreeBSD__)
#include <unistd.h>
#include <sys/wait.h>
#endif
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/ssl.h>
#ifdef OPENSSL_EXTRA
#include <wolfssl/openssl/rand.h>
#endif
#include <tests/api/api.h>
#include <tests/api/test_ossl_rand.h>
#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
static int stub_rand_seed(const void *buf, int num)
{
(void)buf;
(void)num;
return 123;
}
static int stub_rand_bytes(unsigned char *buf, int num)
{
(void)buf;
(void)num;
return 456;
}
static byte* was_stub_rand_cleanup_called(void)
{
static byte was_called = 0;
return &was_called;
}
static void stub_rand_cleanup(void)
{
byte* was_called = was_stub_rand_cleanup_called();
*was_called = 1;
return;
}
static byte* was_stub_rand_add_called(void)
{
static byte was_called = 0;
return &was_called;
}
static int stub_rand_add(const void *buf, int num, double entropy)
{
byte* was_called = was_stub_rand_add_called();
(void)buf;
(void)num;
(void)entropy;
*was_called = 1;
return 0;
}
static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
{
(void)buf;
(void)num;
return 9876;
}
static int stub_rand_status(void)
{
return 5432;
}
#endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
int test_wolfSSL_RAND_set_rand_method(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
unsigned char* buf = NULL;
int num = 0;
double entropy = 0;
int ret;
byte* was_cleanup_called = was_stub_rand_cleanup_called();
byte* was_add_called = was_stub_rand_add_called();
ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
DYNAMIC_TYPE_TMP_BUFFER));
ExpectIntNE(wolfSSL_RAND_status(), 5432);
ExpectIntEQ(*was_cleanup_called, 0);
RAND_cleanup();
ExpectIntEQ(*was_cleanup_called, 0);
rand_methods.seed = &stub_rand_seed;
rand_methods.bytes = &stub_rand_bytes;
rand_methods.cleanup = &stub_rand_cleanup;
rand_methods.add = &stub_rand_add;
rand_methods.pseudorand = &stub_rand_pseudo_bytes;
rand_methods.status = &stub_rand_status;
ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
ExpectIntEQ(RAND_seed(buf, num), 123);
ExpectIntEQ(RAND_bytes(buf, num), 456);
ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
ExpectIntEQ(RAND_status(), 5432);
ExpectIntEQ(*was_add_called, 0);
/* The function pointer for RAND_add returns int, but RAND_add itself
* returns void. */
RAND_add(buf, num, entropy);
ExpectIntEQ(*was_add_called, 1);
was_add_called = 0;
ExpectIntEQ(*was_cleanup_called, 0);
RAND_cleanup();
ExpectIntEQ(*was_cleanup_called, 1);
*was_cleanup_called = 0;
ret = RAND_set_rand_method(NULL);
ExpectIntEQ(ret, WOLFSSL_SUCCESS);
ExpectIntNE(RAND_status(), 5432);
ExpectIntEQ(*was_cleanup_called, 0);
RAND_cleanup();
ExpectIntEQ(*was_cleanup_called, 0);
RAND_set_rand_method(NULL);
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
return EXPECT_RESULT();
}
int test_wolfSSL_RAND_bytes(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA)
const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
int max_bufsize;
byte *my_buf = NULL;
#if defined(OPENSSL_EXTRA) && defined(HAVE_GETPID) && !defined(__MINGW64__) && \
!defined(__MINGW32__)
byte seed[16] = {0};
byte randbuf[8] = {0};
int pipefds[2] = {0};
pid_t pid = 0;
#endif
/* sanity check */
ExpectIntEQ(RAND_bytes(NULL, 16), 0);
ExpectIntEQ(RAND_bytes(NULL, 0), 0);
max_bufsize = size4;
ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), HEAP_HINT,
DYNAMIC_TYPE_TMP_BUFFER));
ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
XFREE(my_buf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
#if defined(OPENSSL_EXTRA) && defined(HAVE_GETPID) && !defined(__MINGW64__) && \
!defined(__MINGW32__)
XMEMSET(seed, 0, sizeof(seed));
RAND_cleanup();
/* No global methods set. */
ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
ExpectIntEQ(pipe(pipefds), 0);
pid = fork();
ExpectIntGE(pid, 0);
if (pid == 0) {
ssize_t n_written = 0;
/* Child process. */
close(pipefds[0]);
RAND_bytes(randbuf, sizeof(randbuf));
n_written = write(pipefds[1], randbuf, sizeof(randbuf));
close(pipefds[1]);
exit(n_written == sizeof(randbuf) ? 0 : 1);
}
else {
/* Parent process. */
byte childrand[8] = {0};
int waitstatus = 0;
close(pipefds[1]);
ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1);
ExpectIntEQ(read(pipefds[0], childrand, sizeof(childrand)),
sizeof(childrand));
#ifdef WOLFSSL_NO_GETPID
ExpectBufEQ(randbuf, childrand, sizeof(randbuf));
#else
ExpectBufNE(randbuf, childrand, sizeof(randbuf));
#endif
close(pipefds[0]);
waitpid(pid, &waitstatus, 0);
}
RAND_cleanup();
#endif
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_RAND(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA)
byte seed[16];
XMEMSET(seed, 0, sizeof(seed));
/* No global methods set. */
ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
ExpectIntEQ(RAND_poll(), 1);
RAND_cleanup();
ExpectIntEQ(RAND_egd(NULL), -1);
#ifndef NO_FILESYSTEM
{
char fname[100];
ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
ExpectIntEQ(RAND_write_file(NULL), 0);
}
#endif
#endif
return EXPECT_RESULT();
}
#if defined(WC_RNG_SEED_CB) && defined(OPENSSL_EXTRA)
static int wc_DummyGenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
word32 i;
for (i = 0; i < sz; i++ )
output[i] = (byte)i;
(void)os;
return 0;
}
#endif /* WC_RNG_SEED_CB */
int test_wolfSSL_RAND_poll(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA)
byte seed[16];
byte rand1[16];
#ifdef WC_RNG_SEED_CB
byte rand2[16];
#endif
XMEMSET(seed, 0, sizeof(seed));
ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
ExpectIntEQ(RAND_poll(), 1);
ExpectIntEQ(RAND_bytes(rand1, 16), 1);
RAND_cleanup();
#ifdef WC_RNG_SEED_CB
/* Test with custom seed and poll */
wc_SetSeed_Cb(wc_DummyGenerateSeed);
ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
ExpectIntEQ(RAND_bytes(rand1, 16), 1);
RAND_cleanup();
/* test that the same value is generated twice with dummy seed function */
ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
ExpectIntEQ(RAND_bytes(rand2, 16), 1);
ExpectIntEQ(XMEMCMP(rand1, rand2, 16), 0);
RAND_cleanup();
/* test that doing a poll is reseeding RNG */
ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
ExpectIntEQ(RAND_poll(), 1);
ExpectIntEQ(RAND_bytes(rand2, 16), 1);
ExpectIntNE(XMEMCMP(rand1, rand2, 16), 0);
/* reset the seed function used */
wc_SetSeed_Cb(WC_GENERATE_SEED_DEFAULT);
#endif
RAND_cleanup();
ExpectIntEQ(RAND_egd(NULL), -1);
#endif
return EXPECT_RESULT();
}

View File

@@ -0,0 +1,39 @@
/* test_ossl_rand.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_OSSL_RAND_H
#define WOLFCRYPT_TEST_OSSL_RAND_H
#include <tests/api/api_decl.h>
int test_wolfSSL_RAND_set_rand_method(void);
int test_wolfSSL_RAND_bytes(void);
int test_wolfSSL_RAND(void);
int test_wolfSSL_RAND_poll(void);
#define TEST_OSSL_RAND_DECLS \
TEST_DECL_GROUP("ossl_rand", test_wolfSSL_RAND_set_rand_method), \
TEST_DECL_GROUP("ossl_rand", test_wolfSSL_RAND_bytes), \
TEST_DECL_GROUP("ossl_rand", test_wolfSSL_RAND), \
TEST_DECL_GROUP("ossl_rand", test_wolfSSL_RAND_poll)
#endif /* WOLFCRYPT_TEST_OSSL_RAND_H */

View File

@@ -1470,8 +1470,10 @@ int test_wolfSSL_RSA_To_Der(void)
rsa = NULL; rsa = NULL;
ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz)); ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT),
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz); ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
outDer = out; outDer = out;
@@ -1491,14 +1493,17 @@ int test_wolfSSL_RSA_To_Der(void)
RSA_free(rsa); RSA_free(rsa);
ExpectNotNull(rsa = RSA_new()); ExpectNotNull(rsa = RSA_new());
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT),
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
RSA_free(rsa); RSA_free(rsa);
der = pubDer; der = pubDer;
rsa = NULL; rsa = NULL;
ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz)); ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
RSA_free(rsa); RSA_free(rsa);
#endif #endif
#endif #endif