blob: 126e2dfb459a20b29d23f6930d886c908173c08a [file] [log] [blame]
Gilles Peskine57948362021-02-13 00:14:28 +01001/** \file asn1_helpers.c
2 *
3 * \brief Helper functions for tests that manipulate ASN.1 data.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include <test/helpers.h>
24#include <test/macros.h>
25
Gilles Peskine8e94efe2021-02-13 00:25:53 +010026#if defined(MBEDTLS_ASN1_PARSE_C)
27
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020028# include <mbedtls/asn1.h>
Gilles Peskine8e94efe2021-02-13 00:25:53 +010029
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020030int mbedtls_test_asn1_skip_integer(unsigned char **p,
31 const unsigned char *end,
32 size_t min_bits,
33 size_t max_bits,
34 int must_be_odd)
Gilles Peskine8e94efe2021-02-13 00:25:53 +010035{
36 size_t len;
37 size_t actual_bits;
38 unsigned char msb;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039 TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER), 0);
Gilles Peskine8e94efe2021-02-13 00:25:53 +010040
41 /* Check if the retrieved length doesn't extend the actual buffer's size.
42 * It is assumed here, that end >= p, which validates casting to size_t. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043 TEST_ASSERT(len <= (size_t)(end - *p));
Gilles Peskine8e94efe2021-02-13 00:25:53 +010044
45 /* Tolerate a slight departure from DER encoding:
46 * - 0 may be represented by an empty string or a 1-byte string.
47 * - The sign bit may be used as a value bit. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048 if ((len == 1 && (*p)[0] == 0) ||
49 (len > 1 && (*p)[0] == 0 && ((*p)[1] & 0x80) != 0)) {
50 ++(*p);
Gilles Peskine8e94efe2021-02-13 00:25:53 +010051 --len;
52 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053 if (min_bits == 0 && len == 0)
54 return 1;
55 msb = (*p)[0];
56 TEST_ASSERT(msb != 0);
57 actual_bits = 8 * (len - 1);
58 while (msb != 0) {
Gilles Peskine8e94efe2021-02-13 00:25:53 +010059 msb >>= 1;
60 ++actual_bits;
61 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062 TEST_ASSERT(actual_bits >= min_bits);
63 TEST_ASSERT(actual_bits <= max_bits);
64 if (must_be_odd)
65 TEST_ASSERT(((*p)[len - 1] & 1) != 0);
Gilles Peskine8e94efe2021-02-13 00:25:53 +010066 *p += len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067 return 1;
Gilles Peskine8e94efe2021-02-13 00:25:53 +010068exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069 return 0;
Gilles Peskine8e94efe2021-02-13 00:25:53 +010070}
71
72#endif /* MBEDTLS_ASN1_PARSE_C */