diff --git a/src/ssl_asn1.c b/src/ssl_asn1.c index 5c5d90685..3075665dc 100644 --- a/src/ssl_asn1.c +++ b/src/ssl_asn1.c @@ -65,6 +65,9 @@ static int wolfssl_asn1_item_new(void** item, int type) case WOLFSSL_ASN1_BIT_STRING_ASN1: *(WOLFSSL_ASN1_BIT_STRING**)item = wolfSSL_ASN1_BIT_STRING_new(); break; + case WOLFSSL_ASN1_INTEGER_ASN1: + *(WOLFSSL_ASN1_INTEGER**)item = wolfSSL_ASN1_INTEGER_new(); + break; default: WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_new"); *(void**)item = NULL; @@ -128,6 +131,9 @@ static void wolfssl_asn1_item_free(void** item, int type) case WOLFSSL_ASN1_BIT_STRING_ASN1: wolfSSL_ASN1_BIT_STRING_free(*(WOLFSSL_ASN1_BIT_STRING**)item); break; + case WOLFSSL_ASN1_INTEGER_ASN1: + wolfSSL_ASN1_INTEGER_free(*(WOLFSSL_ASN1_INTEGER**)item); + break; default: WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_free"); } @@ -225,6 +231,17 @@ static int wolfssl_i2d_asn1_item(void** item, int type, byte* buf) len = wolfSSL_i2d_ASN1_BIT_STRING( *(const WOLFSSL_ASN1_BIT_STRING**)item, buf); break; + case WOLFSSL_ASN1_INTEGER_ASN1: + { + byte *tmp_buf = buf; + len = wolfSSL_i2d_ASN1_INTEGER( + *(const WOLFSSL_ASN1_INTEGER**)item, &tmp_buf); + if ((buf == NULL) && (tmp_buf != NULL)) { + XFREE(tmp_buf, NULL, DYNAMIC_TYPE_ASN1); + tmp_buf = NULL; + } + } + break; default: WOLFSSL_MSG("Type not support in processMembers"); len = 0; @@ -543,7 +560,7 @@ WOLFSSL_ASN1_INTEGER* wolfSSL_ASN1_INTEGER_new(void) void wolfSSL_ASN1_INTEGER_free(WOLFSSL_ASN1_INTEGER* in) { if ((in != NULL) && (in->isDynamic)) { - /* Dispose of any data allocated in BIT_STRING. */ + /* Dispose of any data allocated in INTEGER. */ XFREE(in->data, NULL, DYNAMIC_TYPE_OPENSSL); } /* Dispose of the ASN.1 INTEGER object. */ @@ -787,7 +804,7 @@ static int wolfssl_asn1_int_twos_compl(byte* data, int length, byte* neg) * @return -1 when a is NULL or no data, out is NULL, dynamic memory allocation * fails or encoding length fails. */ -int wolfSSL_i2d_ASN1_INTEGER(WOLFSSL_ASN1_INTEGER* a, unsigned char** out) +int wolfSSL_i2d_ASN1_INTEGER(const WOLFSSL_ASN1_INTEGER* a, unsigned char** out) { int ret = 0; byte* buf = NULL; diff --git a/tests/api.c b/tests/api.c index fde5cda3f..a68bd1db8 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33539,7 +33539,8 @@ static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void) const EC_GROUP *group; const EC_POINT *point; int nid; - TEST_ASN1 test_asn1; + TEST_ASN1 *test_asn1 = NULL; + const unsigned char badObjDer[] = { 0x06, 0x00 }; const unsigned char goodObjDer[] = { 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 @@ -33631,12 +33632,17 @@ static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void) XFREE(der, NULL, DYNAMIC_TYPE_ASN1); DPP_BOOTSTRAPPING_KEY_free(bootstrap); + /* Test integer */ + AssertNotNull(test_asn1 = TEST_ASN1_new()); + der = NULL; + ASN1_INTEGER_set(test_asn1->integer, 100); + AssertIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5); + XFREE(der, NULL, DYNAMIC_TYPE_ASN1); + TEST_ASN1_free(test_asn1); + /* Test error cases. */ - AssertNull(TEST_ASN1_new()); AssertNull(wolfSSL_ASN1_item_new(NULL)); TEST_ASN1_free(NULL); - XMEMSET(&test_asn1, 0, sizeof(TEST_ASN1)); - AssertIntEQ(i2d_TEST_ASN1(&test_asn1, &der), 0); res = TEST_RES_CHECK(1); #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */ diff --git a/wolfssl/openssl/asn1.h b/wolfssl/openssl/asn1.h index edfa66291..12ad36980 100644 --- a/wolfssl/openssl/asn1.h +++ b/wolfssl/openssl/asn1.h @@ -140,6 +140,7 @@ typedef struct { typedef enum { WOLFSSL_X509_ALGOR_ASN1 = 0, WOLFSSL_ASN1_BIT_STRING_ASN1, + WOLFSSL_ASN1_INTEGER_ASN1, } WOLFSSL_ASN1_TYPES; #define ASN1_SEQUENCE(type) \ diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 4797848a4..648cdbcee 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -2057,7 +2057,7 @@ WOLFSSL_API WOLFSSL_ASN1_INTEGER* wolfSSL_d2i_ASN1_INTEGER( WOLFSSL_ASN1_INTEGER** a, const unsigned char** in, long inSz); -WOLFSSL_API int wolfSSL_i2d_ASN1_INTEGER(WOLFSSL_ASN1_INTEGER* a, +WOLFSSL_API int wolfSSL_i2d_ASN1_INTEGER(const WOLFSSL_ASN1_INTEGER* a, unsigned char** out); WOLFSSL_API int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime);