ASN1 tests: Match "Empty INTEGER" with the actual library behavior
mbedtls_asn1_get_int() and mbedtls_asn1_get_mpi() behave differently
on an empty INTEGER (0200). Don't change the library behavior for now
because this might break interoperability in some applications. Write
a test function that matches the library behavior.
diff --git a/tests/suites/test_suite_asn1parse.function b/tests/suites/test_suite_asn1parse.function
index 0497631..f794db7 100644
--- a/tests/suites/test_suite_asn1parse.function
+++ b/tests/suites/test_suite_asn1parse.function
@@ -251,6 +251,41 @@
/* END_CASE */
/* BEGIN_CASE */
+void empty_integer( const data_t *input )
+{
+ unsigned char *p;
+#if defined(MBEDTLS_BIGNUM_C)
+ mbedtls_mpi actual_mpi;
+#endif
+ int val;
+
+#if defined(MBEDTLS_BIGNUM_C)
+ mbedtls_mpi_init( & actual_mpi );
+#endif
+
+ /* An INTEGER with no content is not valid. */
+ p = input->x;
+ TEST_EQUAL( mbedtls_asn1_get_int( &p, input->x + input->len, &val ),
+ MBEDTLS_ERR_ASN1_INVALID_LENGTH );
+
+#if defined(MBEDTLS_BIGNUM_C)
+ /* INTEGERs are sometimes abused as bitstrings, so the library accepts
+ * an INTEGER with empty content and gives it the value 0. */
+ p = input->x;
+ TEST_EQUAL( mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi ),
+ 0 );
+ TEST_EQUAL( mbedtls_mpi_cmp_int( &actual_mpi, 0 ), 0 );
+#endif
+
+exit:
+#if defined(MBEDTLS_BIGNUM_C)
+ mbedtls_mpi_free( &actual_mpi );
+#endif
+ /*empty cleanup in some configurations*/ ;
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
void get_integer( const data_t *input,
const char *expected_hex, int expected_result )
{