2019-03-01 18:02:53 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#if defined(MBEDTLS_ASN1_WRITE_C)
|
|
|
|
#include "mbedtls/asn1write.h"
|
|
|
|
#endif
|
|
|
|
|
2020-01-21 21:26:36 +01:00
|
|
|
/* Used internally to report an error that indicates a bug in a parsing function. */
|
2019-03-01 18:02:53 +01:00
|
|
|
#define ERR_PARSE_INCONSISTENCY INT_MAX
|
|
|
|
|
2020-01-21 21:26:36 +01:00
|
|
|
/* Use this magic value in some tests to indicate that the expected result
|
|
|
|
* should not be checked. */
|
|
|
|
#define UNPREDICTABLE_RESULT 0x5552
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int nested_parse(unsigned char **const p,
|
|
|
|
const unsigned char *const end)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
size_t len2 = 0;
|
|
|
|
unsigned char *const start = *p;
|
|
|
|
unsigned char *content_start;
|
|
|
|
unsigned char tag;
|
|
|
|
|
|
|
|
/* First get the length, skipping over the tag. */
|
|
|
|
content_start = start + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_len(&content_start, end, &len);
|
|
|
|
TEST_ASSERT(content_start <= end);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
|
|
|
|
/* Since we have a valid element start (tag and length), retrieve and
|
|
|
|
* check the tag. */
|
|
|
|
tag = start[0];
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len2, tag ^ 1),
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
2019-03-01 18:02:53 +01:00
|
|
|
*p = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len2, tag), 0);
|
|
|
|
TEST_EQUAL(len, len2);
|
|
|
|
TEST_ASSERT(*p == content_start);
|
2019-03-01 18:02:53 +01:00
|
|
|
*p = content_start;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (tag & 0x1f) {
|
2019-03-01 18:02:53 +01:00
|
|
|
case MBEDTLS_ASN1_BOOLEAN:
|
|
|
|
{
|
|
|
|
int val = -257;
|
|
|
|
*p = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_bool(p, end, &val);
|
|
|
|
if (ret == 0) {
|
|
|
|
TEST_ASSERT(val == 0 || val == 1);
|
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MBEDTLS_ASN1_INTEGER:
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
mbedtls_mpi mpi;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&mpi);
|
2019-03-01 18:02:53 +01:00
|
|
|
*p = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_mpi(p, end, &mpi);
|
|
|
|
mbedtls_mpi_free(&mpi);
|
2019-10-10 19:15:18 +02:00
|
|
|
#else
|
|
|
|
*p = start + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_len(p, end, &len);
|
2019-10-10 19:15:18 +02:00
|
|
|
*p += len;
|
2019-03-01 18:02:53 +01:00
|
|
|
#endif
|
|
|
|
/* If we're sure that the number fits in an int, also
|
|
|
|
* call mbedtls_asn1_get_int(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0 && len < sizeof(int)) {
|
2019-03-01 18:02:53 +01:00
|
|
|
int val = -257;
|
|
|
|
unsigned char *q = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_int(&q, end, &val);
|
|
|
|
TEST_ASSERT(*p == q);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MBEDTLS_ASN1_BIT_STRING:
|
|
|
|
{
|
|
|
|
mbedtls_asn1_bitstring bs;
|
|
|
|
*p = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_bitstring(p, end, &bs);
|
2019-03-01 18:02:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MBEDTLS_ASN1_SEQUENCE:
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
while (*p <= end && *p < content_start + len && ret == 0) {
|
|
|
|
ret = nested_parse(p, content_start + len);
|
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MBEDTLS_ASN1_OCTET_STRING:
|
|
|
|
case MBEDTLS_ASN1_NULL:
|
|
|
|
case MBEDTLS_ASN1_OID:
|
|
|
|
case MBEDTLS_ASN1_UTF8_STRING:
|
|
|
|
case MBEDTLS_ASN1_SET:
|
|
|
|
case MBEDTLS_ASN1_PRINTABLE_STRING:
|
|
|
|
case MBEDTLS_ASN1_T61_STRING:
|
|
|
|
case MBEDTLS_ASN1_IA5_STRING:
|
|
|
|
case MBEDTLS_ASN1_UTC_TIME:
|
|
|
|
case MBEDTLS_ASN1_GENERALIZED_TIME:
|
|
|
|
case MBEDTLS_ASN1_UNIVERSAL_STRING:
|
|
|
|
case MBEDTLS_ASN1_BMP_STRING:
|
|
|
|
default:
|
|
|
|
/* No further testing implemented for this tag. */
|
|
|
|
*p += len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(*p <= end);
|
|
|
|
return ret;
|
2019-03-01 18:02:53 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ERR_PARSE_INCONSISTENCY;
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int get_len_step(const data_t *input, size_t buffer_size,
|
|
|
|
size_t actual_length)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
unsigned char *p = NULL;
|
2020-01-21 16:12:07 +01:00
|
|
|
unsigned char *end;
|
2019-03-01 18:02:53 +01:00
|
|
|
size_t parsed_length;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_set_step(buffer_size);
|
2019-03-01 18:02:53 +01:00
|
|
|
/* Allocate a new buffer of exactly the length to parse each time.
|
|
|
|
* This gives memory sanitizers a chance to catch buffer overreads. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buffer_size == 0) {
|
|
|
|
ASSERT_ALLOC(buf, 1);
|
2020-01-21 16:12:07 +01:00
|
|
|
end = buf + 1;
|
|
|
|
p = end;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
ASSERT_ALLOC_WEAK(buf, buffer_size);
|
|
|
|
if (buffer_size > input->len) {
|
|
|
|
memcpy(buf, input->x, input->len);
|
|
|
|
memset(buf + input->len, 'A', buffer_size - input->len);
|
|
|
|
} else {
|
|
|
|
memcpy(buf, input->x, buffer_size);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
p = buf;
|
2020-01-21 16:12:07 +01:00
|
|
|
end = buf + buffer_size;
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_len(&p, end, &parsed_length);
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buffer_size >= input->len + actual_length) {
|
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
TEST_ASSERT(p == buf + input->len);
|
|
|
|
TEST_EQUAL(parsed_length, actual_length);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(buf);
|
|
|
|
return 1;
|
2019-03-01 18:02:53 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(buf);
|
|
|
|
return 0;
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2019-09-11 15:21:26 +02:00
|
|
|
const unsigned char *input_start;
|
|
|
|
const char *description;
|
|
|
|
} traverse_state_t;
|
|
|
|
|
|
|
|
/* Value returned by traverse_callback if description runs out. */
|
|
|
|
#define RET_TRAVERSE_STOP 1
|
|
|
|
/* Value returned by traverse_callback if description has an invalid format
|
|
|
|
* (see traverse_sequence_of). */
|
|
|
|
#define RET_TRAVERSE_ERROR 2
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int traverse_callback(void *ctx, int tag,
|
|
|
|
unsigned char *content, size_t len)
|
2019-09-11 15:21:26 +02:00
|
|
|
{
|
|
|
|
traverse_state_t *state = ctx;
|
|
|
|
size_t offset;
|
|
|
|
const char *rest = state->description;
|
|
|
|
unsigned long n;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(content > state->input_start);
|
2019-09-11 15:21:26 +02:00
|
|
|
offset = content - state->input_start;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_set_step(offset);
|
2019-09-11 15:21:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*rest == 0) {
|
|
|
|
return RET_TRAVERSE_STOP;
|
|
|
|
}
|
|
|
|
n = strtoul(rest, (char **) &rest, 0);
|
|
|
|
TEST_EQUAL(n, offset);
|
|
|
|
TEST_EQUAL(*rest, ',');
|
2019-09-11 15:21:26 +02:00
|
|
|
++rest;
|
2023-01-11 14:50:10 +01:00
|
|
|
n = strtoul(rest, (char **) &rest, 0);
|
|
|
|
TEST_EQUAL(n, (unsigned) tag);
|
|
|
|
TEST_EQUAL(*rest, ',');
|
2019-09-11 15:21:26 +02:00
|
|
|
++rest;
|
2023-01-11 14:50:10 +01:00
|
|
|
n = strtoul(rest, (char **) &rest, 0);
|
|
|
|
TEST_EQUAL(n, len);
|
|
|
|
if (*rest == ',') {
|
2019-09-11 15:21:26 +02:00
|
|
|
++rest;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-09-11 15:21:26 +02:00
|
|
|
|
|
|
|
state->description = rest;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-09-11 15:21:26 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return RET_TRAVERSE_ERROR;
|
2019-09-11 15:21:26 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 18:02:53 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_ASN1_PARSE_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void parse_prefixes(const data_t *input,
|
|
|
|
int full_result,
|
|
|
|
int overfull_result)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
2020-01-21 21:26:36 +01:00
|
|
|
/* full_result: expected result from parsing the given string. */
|
|
|
|
/* overfull_result: expected_result from parsing the given string plus
|
|
|
|
* some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
|
|
|
|
* any result: use this for invalid inputs that may or may not become
|
|
|
|
* valid depending on what the trailing garbage is. */
|
|
|
|
|
2019-03-01 18:02:53 +01:00
|
|
|
unsigned char *buf = NULL;
|
|
|
|
unsigned char *p = NULL;
|
|
|
|
size_t buffer_size;
|
|
|
|
int ret;
|
|
|
|
|
2020-01-21 18:56:27 +01:00
|
|
|
/* Test every prefix of the input, except the empty string.
|
|
|
|
* The first byte of the string is the tag. Without a tag byte,
|
|
|
|
* we wouldn't know what to parse the input as.
|
2020-01-21 21:26:36 +01:00
|
|
|
* Also test the input followed by an extra byte.
|
2020-01-21 18:56:27 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (buffer_size = 1; buffer_size <= input->len + 1; buffer_size++) {
|
|
|
|
mbedtls_test_set_step(buffer_size);
|
2019-03-01 18:02:53 +01:00
|
|
|
/* Allocate a new buffer of exactly the length to parse each time.
|
|
|
|
* This gives memory sanitizers a chance to catch buffer overreads. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(buf, buffer_size);
|
|
|
|
memcpy(buf, input->x, buffer_size);
|
2019-03-01 18:02:53 +01:00
|
|
|
p = buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = nested_parse(&p, buf + buffer_size);
|
2020-01-21 21:26:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == ERR_PARSE_INCONSISTENCY) {
|
2019-03-01 18:02:53 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buffer_size < input->len) {
|
|
|
|
TEST_EQUAL(ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
} else if (buffer_size == input->len) {
|
|
|
|
TEST_EQUAL(ret, full_result);
|
|
|
|
} else { /* ( buffer_size > input->len ) */
|
|
|
|
if (overfull_result != UNPREDICTABLE_RESULT) {
|
|
|
|
TEST_EQUAL(ret, overfull_result);
|
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
TEST_ASSERT(p == buf + input->len);
|
2020-01-21 21:26:36 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(buf);
|
2019-03-01 18:02:53 +01:00
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(buf);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_len(const data_t *input, int actual_length_arg)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
size_t actual_length = actual_length_arg;
|
|
|
|
size_t buffer_size;
|
|
|
|
|
2020-01-21 18:56:27 +01:00
|
|
|
/* Test prefixes of a buffer containing the given length string
|
|
|
|
* followed by `actual_length` bytes of payload. To save a bit of
|
|
|
|
* time, we skip some "boring" prefixes: we don't test prefixes where
|
|
|
|
* the payload is truncated more than one byte away from either end,
|
|
|
|
* and we only test the empty string on a 1-byte input.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (buffer_size = 1; buffer_size <= input->len + 1; buffer_size++) {
|
|
|
|
if (!get_len_step(input, buffer_size, actual_length)) {
|
2019-03-01 18:02:53 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!get_len_step(input, input->len + actual_length - 1, actual_length)) {
|
2019-03-01 18:02:53 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (!get_len_step(input, input->len + actual_length, actual_length)) {
|
2019-03-01 18:02:53 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_boolean(const data_t *input,
|
|
|
|
int expected_value, int expected_result)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
unsigned char *p = input->x;
|
|
|
|
int val;
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_bool(&p, input->x + input->len, &val);
|
|
|
|
TEST_EQUAL(ret, expected_result);
|
|
|
|
if (expected_result == 0) {
|
|
|
|
TEST_EQUAL(val, expected_value);
|
|
|
|
TEST_ASSERT(p == input->x + input->len);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-10-10 19:18:21 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void empty_integer(const data_t *input)
|
2019-10-10 19:18:21 +02:00
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
mbedtls_mpi actual_mpi;
|
|
|
|
#endif
|
|
|
|
int val;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&actual_mpi);
|
2019-10-10 19:18:21 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* An INTEGER with no content is not valid. */
|
|
|
|
p = input->x;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_int(&p, input->x + input->len, &val),
|
|
|
|
MBEDTLS_ERR_ASN1_INVALID_LENGTH);
|
2019-10-10 19:18:21 +02:00
|
|
|
|
|
|
|
#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;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_mpi(&p, input->x + input->len, &actual_mpi),
|
|
|
|
0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&actual_mpi, 0), 0);
|
2019-10-10 19:18:21 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&actual_mpi);
|
2019-10-10 19:18:21 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
/*empty cleanup in some configurations*/;
|
2019-10-10 19:18:21 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-03-01 18:02:53 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_integer(const data_t *input,
|
|
|
|
const char *expected_hex, int expected_result)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
mbedtls_mpi expected_mpi;
|
|
|
|
mbedtls_mpi actual_mpi;
|
2019-10-10 19:21:12 +02:00
|
|
|
mbedtls_mpi complement;
|
2019-10-10 19:15:18 +02:00
|
|
|
int expected_result_for_mpi = expected_result;
|
2019-03-01 18:02:53 +01:00
|
|
|
#endif
|
|
|
|
long expected_value;
|
|
|
|
int expected_result_for_int = expected_result;
|
|
|
|
int val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&expected_mpi);
|
|
|
|
mbedtls_mpi_init(&actual_mpi);
|
|
|
|
mbedtls_mpi_init(&complement);
|
2019-03-01 18:02:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
errno = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
expected_value = strtol(expected_hex, NULL, 16);
|
|
|
|
if (expected_result == 0 &&
|
|
|
|
(errno == ERANGE
|
2019-03-01 18:02:53 +01:00
|
|
|
#if LONG_MAX > INT_MAX
|
2023-01-11 14:50:10 +01:00
|
|
|
|| expected_value > INT_MAX || expected_value < INT_MIN
|
2019-03-01 18:02:53 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
)) {
|
2019-10-10 19:21:12 +02:00
|
|
|
/* The library returns the dubious error code INVALID_LENGTH
|
|
|
|
* for integers that are out of range. */
|
|
|
|
expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_result == 0 && expected_value < 0) {
|
2019-10-10 19:21:12 +02:00
|
|
|
/* The library does not support negative INTEGERs and
|
|
|
|
* returns the dubious error code INVALID_LENGTH.
|
|
|
|
* Test that we preserve the historical behavior. If we
|
|
|
|
* decide to change the behavior, we'll also change this test. */
|
2019-03-01 18:02:53 +01:00
|
|
|
expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = input->x;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_int(&p, input->x + input->len, &val);
|
|
|
|
TEST_EQUAL(ret, expected_result_for_int);
|
|
|
|
if (ret == 0) {
|
|
|
|
TEST_EQUAL(val, expected_value);
|
|
|
|
TEST_ASSERT(p == input->x + input->len);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_test_read_mpi(&expected_mpi, expected_hex);
|
|
|
|
TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
|
|
|
if (ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA) {
|
2019-03-01 18:02:53 +01:00
|
|
|
/* The data overflows the maximum MPI size. */
|
|
|
|
expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
p = input->x;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_mpi(&p, input->x + input->len, &actual_mpi);
|
|
|
|
TEST_EQUAL(ret, expected_result_for_mpi);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (expected_value >= 0) {
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&actual_mpi,
|
|
|
|
&expected_mpi) == 0);
|
|
|
|
} else {
|
2019-10-10 19:21:12 +02:00
|
|
|
/* The library ignores the sign bit in ASN.1 INTEGERs
|
|
|
|
* (which makes sense insofar as INTEGERs are sometimes
|
|
|
|
* abused as bit strings), so the result of parsing them
|
|
|
|
* is a positive integer such that expected_mpi +
|
|
|
|
* actual_mpi = 2^n where n is the length of the content
|
|
|
|
* of the INTEGER. (Leading ff octets don't matter for the
|
|
|
|
* expected value, but they matter for the actual value.)
|
|
|
|
* Test that we don't change from this behavior. If we
|
|
|
|
* decide to fix the library to change the behavior on
|
|
|
|
* negative INTEGERs, we'll fix this test code. */
|
|
|
|
unsigned char *q = input->x + 1;
|
|
|
|
size_t len;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_asn1_get_len(&q, input->x + input->len,
|
|
|
|
&len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&complement, 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&complement, len * 8) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_add_mpi(&complement, &complement,
|
|
|
|
&expected_mpi) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&complement,
|
|
|
|
&actual_mpi) == 0);
|
2019-10-10 19:21:12 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(p == input->x + input->len);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&expected_mpi);
|
|
|
|
mbedtls_mpi_free(&actual_mpi);
|
|
|
|
mbedtls_mpi_free(&complement);
|
2019-03-01 18:02:53 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
/*empty cleanup in some configurations*/;
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-10-31 14:55:16 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_enum(const data_t *input,
|
|
|
|
const char *expected_hex, int expected_result)
|
2019-10-31 14:55:16 +01:00
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
long expected_value;
|
|
|
|
int expected_result_for_enum = expected_result;
|
|
|
|
int val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
errno = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
expected_value = strtol(expected_hex, NULL, 16);
|
|
|
|
if (expected_result == 0 &&
|
|
|
|
(errno == ERANGE
|
2019-10-31 14:55:16 +01:00
|
|
|
#if LONG_MAX > INT_MAX
|
2023-01-11 14:50:10 +01:00
|
|
|
|| expected_value > INT_MAX || expected_value < INT_MIN
|
2019-10-31 14:55:16 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
)) {
|
2019-10-31 14:55:16 +01:00
|
|
|
/* The library returns the dubious error code INVALID_LENGTH
|
|
|
|
* for integers that are out of range. */
|
|
|
|
expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_result == 0 && expected_value < 0) {
|
2019-10-31 14:55:16 +01:00
|
|
|
/* The library does not support negative INTEGERs and
|
|
|
|
* returns the dubious error code INVALID_LENGTH.
|
|
|
|
* Test that we preserve the historical behavior. If we
|
|
|
|
* decide to change the behavior, we'll also change this test. */
|
|
|
|
expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = input->x;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_enum(&p, input->x + input->len, &val);
|
|
|
|
TEST_EQUAL(ret, expected_result_for_enum);
|
|
|
|
if (ret == 0) {
|
|
|
|
TEST_EQUAL(val, expected_value);
|
|
|
|
TEST_ASSERT(p == input->x + input->len);
|
2019-10-31 14:55:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-03-01 18:02:53 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_mpi_too_large()
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
unsigned char *p;
|
|
|
|
mbedtls_mpi actual_mpi;
|
|
|
|
size_t too_many_octets =
|
|
|
|
MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1;
|
|
|
|
size_t size = too_many_octets + 6;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&actual_mpi);
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(buf, size);
|
2019-03-01 18:02:53 +01:00
|
|
|
buf[0] = 0x02; /* tag: INTEGER */
|
|
|
|
buf[1] = 0x84; /* 4-octet length */
|
2023-01-11 14:50:10 +01:00
|
|
|
buf[2] = (too_many_octets >> 24) & 0xff;
|
|
|
|
buf[3] = (too_many_octets >> 16) & 0xff;
|
|
|
|
buf[4] = (too_many_octets >> 8) & 0xff;
|
2019-03-01 18:02:53 +01:00
|
|
|
buf[5] = too_many_octets & 0xff;
|
|
|
|
buf[6] = 0x01; /* most significant octet */
|
|
|
|
|
|
|
|
p = buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_mpi(&p, buf + size, &actual_mpi),
|
|
|
|
MBEDTLS_ERR_MPI_ALLOC_FAILED);
|
2019-03-01 18:02:53 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&actual_mpi);
|
|
|
|
mbedtls_free(buf);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_bitstring(const data_t *input,
|
|
|
|
int expected_length, int expected_unused_bits,
|
|
|
|
int expected_result, int expected_result_null)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL };
|
|
|
|
unsigned char *p = input->x;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_bitstring(&p, input->x + input->len, &bs),
|
|
|
|
expected_result);
|
|
|
|
if (expected_result == 0) {
|
|
|
|
TEST_EQUAL(bs.len, (size_t) expected_length);
|
|
|
|
TEST_EQUAL(bs.unused_bits, expected_unused_bits);
|
|
|
|
TEST_ASSERT(bs.p != NULL);
|
|
|
|
TEST_EQUAL(bs.p - input->x + bs.len, input->len);
|
|
|
|
TEST_ASSERT(p == input->x + input->len);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p = input->x;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_bitstring_null(&p, input->x + input->len,
|
|
|
|
&bs.len),
|
|
|
|
expected_result_null);
|
|
|
|
if (expected_result_null == 0) {
|
|
|
|
TEST_EQUAL(bs.len, (size_t) expected_length);
|
|
|
|
if (expected_result == 0) {
|
|
|
|
TEST_ASSERT(p == input->x + input->len - bs.len);
|
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_sequence_of(const data_t *input, int tag,
|
|
|
|
const char *description,
|
|
|
|
int expected_result)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
2019-09-11 15:21:26 +02:00
|
|
|
/* The description string is a comma-separated list of integers.
|
|
|
|
* For each element in the SEQUENCE in input, description contains
|
|
|
|
* two integers: the offset of the element (offset from the start
|
|
|
|
* of input to the tag of the element) and the length of the
|
|
|
|
* element's contents.
|
|
|
|
* "offset1,length1,..." */
|
|
|
|
|
2019-03-01 18:02:53 +01:00
|
|
|
mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
|
2019-09-11 15:20:09 +02:00
|
|
|
mbedtls_asn1_sequence *cur;
|
2019-03-01 18:02:53 +01:00
|
|
|
unsigned char *p = input->x;
|
|
|
|
const char *rest = description;
|
|
|
|
unsigned long n;
|
2021-02-03 13:07:01 +01:00
|
|
|
unsigned int step = 0;
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_sequence_of(&p, input->x + input->len,
|
|
|
|
&head, tag),
|
|
|
|
expected_result);
|
|
|
|
if (expected_result == 0) {
|
|
|
|
TEST_ASSERT(p == input->x + input->len);
|
|
|
|
|
|
|
|
if (!*rest) {
|
|
|
|
TEST_EQUAL(head.buf.tag, 0);
|
|
|
|
TEST_ASSERT(head.buf.p == NULL);
|
|
|
|
TEST_EQUAL(head.buf.len, 0);
|
|
|
|
TEST_ASSERT(head.next == NULL);
|
|
|
|
} else {
|
2019-03-01 18:02:53 +01:00
|
|
|
cur = &head;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (*rest) {
|
|
|
|
mbedtls_test_set_step(step);
|
|
|
|
TEST_ASSERT(cur != NULL);
|
|
|
|
TEST_EQUAL(cur->buf.tag, tag);
|
|
|
|
n = strtoul(rest, (char **) &rest, 0);
|
|
|
|
TEST_EQUAL(n, (size_t) (cur->buf.p - input->x));
|
2019-03-01 18:02:53 +01:00
|
|
|
++rest;
|
2023-01-11 14:50:10 +01:00
|
|
|
n = strtoul(rest, (char **) &rest, 0);
|
|
|
|
TEST_EQUAL(n, cur->buf.len);
|
|
|
|
if (*rest) {
|
2019-03-01 18:02:53 +01:00
|
|
|
++rest;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
cur = cur->next;
|
2021-02-03 13:07:01 +01:00
|
|
|
++step;
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(cur == NULL);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_sequence_free(head.next);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-09-11 15:21:26 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void traverse_sequence_of(const data_t *input,
|
|
|
|
int tag_must_mask, int tag_must_val,
|
|
|
|
int tag_may_mask, int tag_may_val,
|
|
|
|
const char *description,
|
|
|
|
int expected_result)
|
2019-09-11 15:21:26 +02:00
|
|
|
{
|
|
|
|
/* The description string is a comma-separated list of integers.
|
|
|
|
* For each element in the SEQUENCE in input, description contains
|
|
|
|
* three integers: the offset of the element's content (offset from
|
|
|
|
* the start of input to the content of the element), the element's tag,
|
|
|
|
* and the length of the element's contents.
|
|
|
|
* "offset1,tag1,length1,..." */
|
|
|
|
|
|
|
|
unsigned char *p = input->x;
|
2023-01-11 14:50:10 +01:00
|
|
|
traverse_state_t traverse_state = { input->x, description };
|
2019-09-11 15:21:26 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_traverse_sequence_of(&p, input->x + input->len,
|
|
|
|
(uint8_t) tag_must_mask, (uint8_t) tag_must_val,
|
|
|
|
(uint8_t) tag_may_mask, (uint8_t) tag_may_val,
|
|
|
|
traverse_callback, &traverse_state);
|
|
|
|
if (ret == RET_TRAVERSE_ERROR) {
|
2019-09-11 15:21:26 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TEST_EQUAL(ret, expected_result);
|
|
|
|
TEST_EQUAL(*traverse_state.description, 0);
|
2019-09-11 15:21:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-03-01 18:02:53 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void get_alg(const data_t *input,
|
|
|
|
int oid_offset, int oid_length,
|
|
|
|
int params_tag, int params_offset, int params_length,
|
|
|
|
int total_length,
|
|
|
|
int expected_result)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
mbedtls_asn1_buf oid = { -1, 0, NULL };
|
|
|
|
mbedtls_asn1_buf params = { -1, 0, NULL };
|
|
|
|
unsigned char *p = input->x;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_alg(&p, input->x + input->len,
|
|
|
|
&oid, ¶ms),
|
|
|
|
expected_result);
|
|
|
|
if (expected_result == 0) {
|
|
|
|
TEST_EQUAL(oid.tag, MBEDTLS_ASN1_OID);
|
|
|
|
TEST_EQUAL(oid.p - input->x, oid_offset);
|
|
|
|
TEST_EQUAL(oid.len, (size_t) oid_length);
|
|
|
|
TEST_EQUAL(params.tag, params_tag);
|
|
|
|
if (params_offset != 0) {
|
|
|
|
TEST_EQUAL(params.p - input->x, params_offset);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(params.p == NULL);
|
|
|
|
}
|
|
|
|
TEST_EQUAL(params.len, (size_t) params_length);
|
|
|
|
TEST_EQUAL(p - input->x, total_length);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_alg_null(&p, input->x + input->len, &oid);
|
|
|
|
if (expected_result == 0 && params_offset == 0) {
|
|
|
|
TEST_EQUAL(oid.tag, MBEDTLS_ASN1_OID);
|
|
|
|
TEST_EQUAL(oid.p - input->x, oid_offset);
|
|
|
|
TEST_EQUAL(oid.len, (size_t) oid_length);
|
|
|
|
TEST_EQUAL(p - input->x, total_length);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(ret != 0);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void find_named_data(data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3,
|
|
|
|
data_t *needle, int from, int position)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_named_data nd[] = {
|
|
|
|
{ { 0x06, oid0->len, oid0->x }, { 0, 0, NULL }, NULL, 0 },
|
|
|
|
{ { 0x06, oid1->len, oid1->x }, { 0, 0, NULL }, NULL, 0 },
|
|
|
|
{ { 0x06, oid2->len, oid2->x }, { 0, 0, NULL }, NULL, 0 },
|
|
|
|
{ { 0x06, oid3->len, oid3->x }, { 0, 0, NULL }, NULL, 0 },
|
2019-03-01 18:02:53 +01:00
|
|
|
};
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_named_data *pointers[ARRAY_LENGTH(nd) + 1];
|
2019-03-01 18:02:53 +01:00
|
|
|
size_t i;
|
2021-01-26 13:57:43 +01:00
|
|
|
const mbedtls_asn1_named_data *found;
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(nd); i++) {
|
2019-03-01 18:02:53 +01:00
|
|
|
pointers[i] = &nd[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
pointers[ARRAY_LENGTH(nd)] = NULL;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(nd); i++) {
|
2019-03-01 18:02:53 +01:00
|
|
|
nd[i].next = pointers[i+1];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
found = mbedtls_asn1_find_named_data((const mbedtls_asn1_named_data *) pointers[from],
|
|
|
|
(const char *) needle->x,
|
|
|
|
needle->len);
|
|
|
|
TEST_ASSERT(found == pointers[position]);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-11-08 02:09:38 +01:00
|
|
|
/* BEGIN_CASE depends_on:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING */
|
2023-01-11 14:50:10 +01:00
|
|
|
void free_named_data_null()
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data(NULL);
|
2019-03-01 18:02:53 +01:00
|
|
|
goto exit; /* Silence unused label warning */
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-11-08 02:08:54 +01:00
|
|
|
/* BEGIN_CASE depends_on:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING */
|
2023-01-11 14:50:10 +01:00
|
|
|
void free_named_data(int with_oid, int with_val, int with_next)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
mbedtls_asn1_named_data next =
|
2023-01-11 14:50:10 +01:00
|
|
|
{ { 0x06, 0, NULL }, { 0, 0xcafe, NULL }, NULL, 0 };
|
2019-03-01 18:02:53 +01:00
|
|
|
mbedtls_asn1_named_data head =
|
2023-01-11 14:50:10 +01:00
|
|
|
{ { 0x06, 0, NULL }, { 0, 0, NULL }, NULL, 0 };
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (with_oid) {
|
|
|
|
ASSERT_ALLOC(head.oid.p, 1);
|
|
|
|
}
|
|
|
|
if (with_val) {
|
|
|
|
ASSERT_ALLOC(head.val.p, 1);
|
|
|
|
}
|
|
|
|
if (with_next) {
|
2019-03-01 18:02:53 +01:00
|
|
|
head.next = &next;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-01 18:02:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data(&head);
|
|
|
|
TEST_ASSERT(head.oid.p == NULL);
|
|
|
|
TEST_ASSERT(head.val.p == NULL);
|
|
|
|
TEST_ASSERT(head.next == NULL);
|
|
|
|
TEST_ASSERT(next.val.len == 0xcafe);
|
2019-03-01 18:02:53 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(head.oid.p);
|
|
|
|
mbedtls_free(head.val.p);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void free_named_data_list(int length)
|
2019-03-01 18:02:53 +01:00
|
|
|
{
|
|
|
|
mbedtls_asn1_named_data *head = NULL;
|
|
|
|
int i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < length; i++) {
|
2019-03-01 18:02:53 +01:00
|
|
|
mbedtls_asn1_named_data *new = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(new, 1);
|
2019-09-20 21:06:27 +02:00
|
|
|
new->next = head;
|
2019-03-01 18:02:53 +01:00
|
|
|
head = new;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data_list(&head);
|
|
|
|
TEST_ASSERT(head == NULL);
|
2019-03-01 18:02:53 +01:00
|
|
|
/* Most of the point of the test is that it doesn't leak memory.
|
|
|
|
* So this test is only really useful under a memory leak detection
|
|
|
|
* framework. */
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data_list(&head);
|
2019-03-01 18:02:53 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|