Gilles Peskine | 5794836 | 2021-02-13 00:14:28 +0100 | [diff] [blame] | 1 | /** \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 Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 26 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 27 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 28 | # include <mbedtls/asn1.h> |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 29 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 30 | int 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 Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 35 | { |
| 36 | size_t len; |
| 37 | size_t actual_bits; |
| 38 | unsigned char msb; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 39 | TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER), 0); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 40 | |
| 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 Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 43 | TEST_ASSERT(len <= (size_t)(end - *p)); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 44 | |
| 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 Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | if ((len == 1 && (*p)[0] == 0) || |
| 49 | (len > 1 && (*p)[0] == 0 && ((*p)[1] & 0x80) != 0)) { |
| 50 | ++(*p); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 51 | --len; |
| 52 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 53 | 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 Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 59 | msb >>= 1; |
| 60 | ++actual_bits; |
| 61 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 62 | 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 Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 66 | *p += len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 67 | return 1; |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 68 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 69 | return 0; |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | #endif /* MBEDTLS_ASN1_PARSE_C */ |