| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file asn1.h | 
|  | 3 | * | 
|  | 4 | * \brief Generic ASN.1 parsing | 
| Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ | 
|  | 6 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 21 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #ifndef MBEDTLS_ASN1_H | 
|  | 23 | #define MBEDTLS_ASN1_H | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 24 | #include "mbedtls/private_access.h" | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 25 |  | 
| Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 27 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 28 | #include <stddef.h> | 
|  | 29 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_BIGNUM_C) | 
| Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 31 | #include "mbedtls/bignum.h" | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 32 | #endif | 
|  | 33 |  | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 34 | /** | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 35 | * \addtogroup asn1_module | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 36 | * \{ | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 37 | */ | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 38 |  | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 39 | /** | 
|  | 40 | * \name ASN1 Error codes | 
|  | 41 | * These error codes are OR'ed to X509 error codes for | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 42 | * higher error granularity. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 43 | * ASN1 is a standard to specify data structures. | 
|  | 44 | * \{ | 
|  | 45 | */ | 
| Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 46 | /** Out of data when parsing an ASN1 data structure. */ | 
|  | 47 | #define MBEDTLS_ERR_ASN1_OUT_OF_DATA                      -0x0060 | 
|  | 48 | /** ASN1 tag was of an unexpected value. */ | 
|  | 49 | #define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG                   -0x0062 | 
|  | 50 | /** Error when trying to determine the length or invalid length. */ | 
|  | 51 | #define MBEDTLS_ERR_ASN1_INVALID_LENGTH                   -0x0064 | 
|  | 52 | /** Actual length differs from expected length. */ | 
|  | 53 | #define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH                  -0x0066 | 
|  | 54 | /** Data is invalid. */ | 
|  | 55 | #define MBEDTLS_ERR_ASN1_INVALID_DATA                     -0x0068 | 
|  | 56 | /** Memory allocation failed */ | 
|  | 57 | #define MBEDTLS_ERR_ASN1_ALLOC_FAILED                     -0x006A | 
|  | 58 | /** Buffer too small when writing ASN.1 data structure. */ | 
|  | 59 | #define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL                    -0x006C | 
| Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 60 |  | 
| Andrzej Kurek | 38d4fdd | 2021-12-28 16:22:52 +0100 | [diff] [blame] | 61 | /** \} name ASN1 Error codes */ | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 62 |  | 
|  | 63 | /** | 
|  | 64 | * \name DER constants | 
| Andres Amaya Garcia | 9fb0205 | 2017-08-25 17:24:44 +0100 | [diff] [blame] | 65 | * These constants comply with the DER encoded ASN.1 type tags. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 66 | * DER encoding uses hexadecimal representation. | 
|  | 67 | * An example DER sequence is:\n | 
|  | 68 | * - 0x02 -- tag indicating INTEGER | 
|  | 69 | * - 0x01 -- length in octets | 
|  | 70 | * - 0x05 -- value | 
| Gilles Peskine | 96ddb0a | 2020-02-26 19:50:01 +0100 | [diff] [blame] | 71 | * Such sequences are typically read into \c ::mbedtls_x509_buf. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 72 | * \{ | 
|  | 73 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | #define MBEDTLS_ASN1_BOOLEAN                 0x01 | 
|  | 75 | #define MBEDTLS_ASN1_INTEGER                 0x02 | 
|  | 76 | #define MBEDTLS_ASN1_BIT_STRING              0x03 | 
|  | 77 | #define MBEDTLS_ASN1_OCTET_STRING            0x04 | 
|  | 78 | #define MBEDTLS_ASN1_NULL                    0x05 | 
|  | 79 | #define MBEDTLS_ASN1_OID                     0x06 | 
| Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 80 | #define MBEDTLS_ASN1_ENUMERATED              0x0A | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | #define MBEDTLS_ASN1_UTF8_STRING             0x0C | 
|  | 82 | #define MBEDTLS_ASN1_SEQUENCE                0x10 | 
|  | 83 | #define MBEDTLS_ASN1_SET                     0x11 | 
|  | 84 | #define MBEDTLS_ASN1_PRINTABLE_STRING        0x13 | 
|  | 85 | #define MBEDTLS_ASN1_T61_STRING              0x14 | 
|  | 86 | #define MBEDTLS_ASN1_IA5_STRING              0x16 | 
|  | 87 | #define MBEDTLS_ASN1_UTC_TIME                0x17 | 
|  | 88 | #define MBEDTLS_ASN1_GENERALIZED_TIME        0x18 | 
|  | 89 | #define MBEDTLS_ASN1_UNIVERSAL_STRING        0x1C | 
|  | 90 | #define MBEDTLS_ASN1_BMP_STRING              0x1E | 
|  | 91 | #define MBEDTLS_ASN1_PRIMITIVE               0x00 | 
|  | 92 | #define MBEDTLS_ASN1_CONSTRUCTED             0x20 | 
|  | 93 | #define MBEDTLS_ASN1_CONTEXT_SPECIFIC        0x80 | 
| Andres Amaya Garcia | 7512bf7 | 2017-08-25 17:12:11 +0100 | [diff] [blame] | 94 |  | 
| Hanno Becker | cba7122 | 2019-09-11 14:15:10 +0100 | [diff] [blame] | 95 | /* Slightly smaller way to check if tag is a string tag | 
|  | 96 | * compared to canonical implementation. */ | 
|  | 97 | #define MBEDTLS_ASN1_IS_STRING_TAG( tag )                                     \ | 
|  | 98 | ( ( tag ) < 32u && (                                                      \ | 
|  | 99 | ( ( 1u << ( tag ) ) & ( ( 1u << MBEDTLS_ASN1_BMP_STRING )       |     \ | 
|  | 100 | ( 1u << MBEDTLS_ASN1_UTF8_STRING )      |     \ | 
|  | 101 | ( 1u << MBEDTLS_ASN1_T61_STRING )       |     \ | 
|  | 102 | ( 1u << MBEDTLS_ASN1_IA5_STRING )       |     \ | 
|  | 103 | ( 1u << MBEDTLS_ASN1_UNIVERSAL_STRING ) |     \ | 
|  | 104 | ( 1u << MBEDTLS_ASN1_PRINTABLE_STRING ) |     \ | 
|  | 105 | ( 1u << MBEDTLS_ASN1_BIT_STRING ) ) ) != 0 ) ) | 
|  | 106 |  | 
| Andres Amaya Garcia | 7512bf7 | 2017-08-25 17:12:11 +0100 | [diff] [blame] | 107 | /* | 
|  | 108 | * Bit masks for each of the components of an ASN.1 tag as specified in | 
| Gilles Peskine | 1ed45ea | 2018-03-08 18:16:45 +0100 | [diff] [blame] | 109 | * ITU X.690 (08/2015), section 8.1 "General rules for encoding", | 
|  | 110 | * paragraph 8.1.2.2: | 
| Andres Amaya Garcia | 7512bf7 | 2017-08-25 17:12:11 +0100 | [diff] [blame] | 111 | * | 
|  | 112 | * Bit  8     7   6   5          1 | 
|  | 113 | *     +-------+-----+------------+ | 
|  | 114 | *     | Class | P/C | Tag number | | 
|  | 115 | *     +-------+-----+------------+ | 
|  | 116 | */ | 
| Andres Amaya Garcia | 7786abc | 2017-11-07 20:21:56 +0000 | [diff] [blame] | 117 | #define MBEDTLS_ASN1_TAG_CLASS_MASK          0xC0 | 
|  | 118 | #define MBEDTLS_ASN1_TAG_PC_MASK             0x20 | 
|  | 119 | #define MBEDTLS_ASN1_TAG_VALUE_MASK          0x1F | 
| Andres Amaya Garcia | 7512bf7 | 2017-08-25 17:12:11 +0100 | [diff] [blame] | 120 |  | 
| Andrzej Kurek | 38d4fdd | 2021-12-28 16:22:52 +0100 | [diff] [blame] | 121 | /** \} name DER constants */ | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 122 |  | 
|  | 123 | /** Returns the size of the binary string, without the trailing \\0 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | #define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1) | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 125 |  | 
| Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 126 | /** | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | * Compares an mbedtls_asn1_buf structure to a reference OID. | 
| Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 128 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a | 
| Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 130 | * 'unsigned char *oid' here! | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 131 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | #define MBEDTLS_OID_CMP(oid_str, oid_buf)                                   \ | 
|  | 133 | ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) ||                \ | 
| Manuel Pégourié-Gonnard | 6e06437 | 2015-03-19 16:54:56 +0000 | [diff] [blame] | 134 | memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 ) | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 135 |  | 
| Hanno Becker | 63e38fe | 2019-09-11 14:16:40 +0100 | [diff] [blame] | 136 | #define MBEDTLS_OID_CMP_RAW(oid_str, oid_buf, oid_buf_len)              \ | 
|  | 137 | ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf_len) ) ||             \ | 
|  | 138 | memcmp( (oid_str), (oid_buf), (oid_buf_len) ) != 0 ) | 
|  | 139 |  | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 140 | #ifdef __cplusplus | 
|  | 141 | extern "C" { | 
|  | 142 | #endif | 
|  | 143 |  | 
|  | 144 | /** | 
|  | 145 | * \name Functions to parse ASN.1 data structures | 
|  | 146 | * \{ | 
|  | 147 | */ | 
|  | 148 |  | 
|  | 149 | /** | 
|  | 150 | * Type-length-value structure that allows for ASN1 using DER. | 
|  | 151 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | typedef struct mbedtls_asn1_buf | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 153 | { | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 154 | int tag;                /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */ | 
|  | 155 | size_t len;             /**< ASN1 length, in octets. */ | 
|  | 156 | unsigned char *p;       /**< ASN1 data, e.g. in ASCII. */ | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 157 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_asn1_buf; | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 159 |  | 
|  | 160 | /** | 
|  | 161 | * Container for ASN1 bit strings. | 
|  | 162 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | typedef struct mbedtls_asn1_bitstring | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 164 | { | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 165 | size_t len;                 /**< ASN1 length, in octets. */ | 
|  | 166 | unsigned char unused_bits;  /**< Number of unused bits at the end of the string */ | 
|  | 167 | unsigned char *p;           /**< Raw ASN1 data for the bit string */ | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 168 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | mbedtls_asn1_bitstring; | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 170 |  | 
|  | 171 | /** | 
|  | 172 | * Container for a sequence of ASN.1 items | 
|  | 173 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | typedef struct mbedtls_asn1_sequence | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 175 | { | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 176 | mbedtls_asn1_buf buf;                   /**< Buffer containing the given ASN.1 item. */ | 
|  | 177 |  | 
|  | 178 | /** The next entry in the sequence. | 
|  | 179 | * | 
| Gilles Peskine | 44ffc79 | 2021-08-31 22:59:35 +0200 | [diff] [blame] | 180 | * The details of memory management for sequences are not documented and | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 181 | * may change in future versions. Set this field to \p NULL when | 
| Gilles Peskine | 44ffc79 | 2021-08-31 22:59:35 +0200 | [diff] [blame] | 182 | * initializing a structure, and do not modify it except via Mbed TLS | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 183 | * library functions. | 
|  | 184 | */ | 
|  | 185 | struct mbedtls_asn1_sequence *next; | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 186 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | mbedtls_asn1_sequence; | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | /** | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 190 | * Container for a sequence or list of 'named' ASN.1 data items | 
|  | 191 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | typedef struct mbedtls_asn1_named_data | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 193 | { | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 194 | mbedtls_asn1_buf oid;                   /**< The object identifier. */ | 
|  | 195 | mbedtls_asn1_buf val;                   /**< The named value. */ | 
|  | 196 |  | 
|  | 197 | /** The next entry in the sequence. | 
|  | 198 | * | 
| Gilles Peskine | 44ffc79 | 2021-08-31 22:59:35 +0200 | [diff] [blame] | 199 | * The details of memory management for named data sequences are not | 
|  | 200 | * documented and may change in future versions. Set this field to \p NULL | 
|  | 201 | * when initializing a structure, and do not modify it except via Mbed TLS | 
| Gilles Peskine | b89d9c0 | 2021-08-04 20:55:33 +0200 | [diff] [blame] | 202 | * library functions. | 
|  | 203 | */ | 
|  | 204 | struct mbedtls_asn1_named_data *next; | 
|  | 205 |  | 
|  | 206 | /** Merge next item into the current one? | 
|  | 207 | * | 
|  | 208 | * This field exists for the sake of Mbed TLS's X.509 certificate parsing | 
|  | 209 | * code and may change in future versions of the library. | 
|  | 210 | */ | 
|  | 211 | unsigned char MBEDTLS_PRIVATE(next_merged); | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 212 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | mbedtls_asn1_named_data; | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 214 |  | 
|  | 215 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 216 | * \brief       Get the length of an ASN.1 element. | 
|  | 217 | *              Updates the pointer to immediately behind the length. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 218 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 219 | * \param p     On entry, \c *p points to the first byte of the length, | 
|  | 220 | *              i.e. immediately after the tag. | 
|  | 221 | *              On successful completion, \c *p points to the first byte | 
|  | 222 | *              after the length, i.e. the first byte of the content. | 
|  | 223 | *              On error, the value of \c *p is undefined. | 
|  | 224 | * \param end   End of data. | 
| Gilles Peskine | dc2db48 | 2019-09-20 21:06:01 +0200 | [diff] [blame] | 225 | * \param len   On successful completion, \c *len contains the length | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 226 | *              read from the ASN.1 input. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 227 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 228 | * \return      0 if successful. | 
|  | 229 | * \return      #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element | 
|  | 230 | *              would end beyond \p end. | 
|  | 231 | * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparseable. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 232 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | int mbedtls_asn1_get_len( unsigned char **p, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 234 | const unsigned char *end, | 
|  | 235 | size_t *len ); | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 236 |  | 
|  | 237 | /** | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 238 | * \brief       Get the tag and length of the element. | 
|  | 239 | *              Check for the requested tag. | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 240 | *              Updates the pointer to immediately behind the tag and length. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 241 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 242 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 243 | *              On successful completion, \c *p points to the first byte | 
|  | 244 | *              after the length, i.e. the first byte of the content. | 
|  | 245 | *              On error, the value of \c *p is undefined. | 
|  | 246 | * \param end   End of data. | 
| Gilles Peskine | dc2db48 | 2019-09-20 21:06:01 +0200 | [diff] [blame] | 247 | * \param len   On successful completion, \c *len contains the length | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 248 | *              read from the ASN.1 input. | 
|  | 249 | * \param tag   The expected tag. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 250 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 251 | * \return      0 if successful. | 
|  | 252 | * \return      #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the data does not start | 
|  | 253 | *              with the requested tag. | 
|  | 254 | * \return      #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element | 
|  | 255 | *              would end beyond \p end. | 
|  | 256 | * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparseable. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 257 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | int mbedtls_asn1_get_tag( unsigned char **p, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 259 | const unsigned char *end, | 
|  | 260 | size_t *len, int tag ); | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 261 |  | 
|  | 262 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 263 | * \brief       Retrieve a boolean ASN.1 tag and its value. | 
|  | 264 | *              Updates the pointer to immediately behind the full tag. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 265 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 266 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 267 | *              On successful completion, \c *p points to the first byte | 
|  | 268 | *              beyond the ASN.1 element. | 
|  | 269 | *              On error, the value of \c *p is undefined. | 
|  | 270 | * \param end   End of data. | 
|  | 271 | * \param val   On success, the parsed value (\c 0 or \c 1). | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 272 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 273 | * \return      0 if successful. | 
|  | 274 | * \return      An ASN.1 error code if the input does not start with | 
|  | 275 | *              a valid ASN.1 BOOLEAN. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 276 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | int mbedtls_asn1_get_bool( unsigned char **p, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 278 | const unsigned char *end, | 
|  | 279 | int *val ); | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 280 |  | 
|  | 281 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 282 | * \brief       Retrieve an integer ASN.1 tag and its value. | 
|  | 283 | *              Updates the pointer to immediately behind the full tag. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 284 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 285 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 286 | *              On successful completion, \c *p points to the first byte | 
|  | 287 | *              beyond the ASN.1 element. | 
|  | 288 | *              On error, the value of \c *p is undefined. | 
|  | 289 | * \param end   End of data. | 
|  | 290 | * \param val   On success, the parsed value. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 291 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 292 | * \return      0 if successful. | 
|  | 293 | * \return      An ASN.1 error code if the input does not start with | 
|  | 294 | *              a valid ASN.1 INTEGER. | 
|  | 295 | * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does | 
|  | 296 | *              not fit in an \c int. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 297 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | int mbedtls_asn1_get_int( unsigned char **p, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 299 | const unsigned char *end, | 
|  | 300 | int *val ); | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 301 |  | 
|  | 302 | /** | 
| Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 303 | * \brief       Retrieve an enumerated ASN.1 tag and its value. | 
|  | 304 | *              Updates the pointer to immediately behind the full tag. | 
|  | 305 | * | 
|  | 306 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 307 | *              On successful completion, \c *p points to the first byte | 
|  | 308 | *              beyond the ASN.1 element. | 
|  | 309 | *              On error, the value of \c *p is undefined. | 
|  | 310 | * \param end   End of data. | 
|  | 311 | * \param val   On success, the parsed value. | 
|  | 312 | * | 
|  | 313 | * \return      0 if successful. | 
|  | 314 | * \return      An ASN.1 error code if the input does not start with | 
|  | 315 | *              a valid ASN.1 ENUMERATED. | 
|  | 316 | * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does | 
|  | 317 | *              not fit in an \c int. | 
|  | 318 | */ | 
|  | 319 | int mbedtls_asn1_get_enum( unsigned char **p, | 
|  | 320 | const unsigned char *end, | 
|  | 321 | int *val ); | 
|  | 322 |  | 
|  | 323 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 324 | * \brief       Retrieve a bitstring ASN.1 tag and its value. | 
|  | 325 | *              Updates the pointer to immediately behind the full tag. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 326 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 327 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 328 | *              On successful completion, \c *p is equal to \p end. | 
|  | 329 | *              On error, the value of \c *p is undefined. | 
|  | 330 | * \param end   End of data. | 
|  | 331 | * \param bs    On success, ::mbedtls_asn1_bitstring information about | 
|  | 332 | *              the parsed value. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 333 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 334 | * \return      0 if successful. | 
|  | 335 | * \return      #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains | 
|  | 336 | *              extra data after a valid BIT STRING. | 
|  | 337 | * \return      An ASN.1 error code if the input does not start with | 
|  | 338 | *              a valid ASN.1 BIT STRING. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 339 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 341 | mbedtls_asn1_bitstring *bs ); | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 342 |  | 
|  | 343 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 344 | * \brief       Retrieve a bitstring ASN.1 tag without unused bits and its | 
|  | 345 | *              value. | 
|  | 346 | *              Updates the pointer to the beginning of the bit/octet string. | 
| Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 347 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 348 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 349 | *              On successful completion, \c *p points to the first byte | 
|  | 350 | *              of the content of the BIT STRING. | 
|  | 351 | *              On error, the value of \c *p is undefined. | 
|  | 352 | * \param end   End of data. | 
|  | 353 | * \param len   On success, \c *len is the length of the content in bytes. | 
| Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 354 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 355 | * \return      0 if successful. | 
|  | 356 | * \return      #MBEDTLS_ERR_ASN1_INVALID_DATA if the input starts with | 
|  | 357 | *              a valid BIT STRING with a nonzero number of unused bits. | 
|  | 358 | * \return      An ASN.1 error code if the input does not start with | 
|  | 359 | *              a valid ASN.1 BIT STRING. | 
| Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 360 | */ | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 361 | int mbedtls_asn1_get_bitstring_null( unsigned char **p, | 
|  | 362 | const unsigned char *end, | 
|  | 363 | size_t *len ); | 
| Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 364 |  | 
|  | 365 | /** | 
| Gilles Peskine | dc2db48 | 2019-09-20 21:06:01 +0200 | [diff] [blame] | 366 | * \brief       Parses and splits an ASN.1 "SEQUENCE OF <tag>". | 
|  | 367 | *              Updates the pointer to immediately behind the full sequence tag. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 368 | * | 
| Hanno Becker | 12ae27d | 2019-09-11 14:20:09 +0100 | [diff] [blame] | 369 | * This function allocates memory for the sequence elements. You can free | 
|  | 370 | * the allocated memory with mbedtls_asn1_sequence_free(). | 
|  | 371 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 372 | * \note        On error, this function may return a partial list in \p cur. | 
|  | 373 | *              You must set `cur->next = NULL` before calling this function! | 
|  | 374 | *              Otherwise it is impossible to distinguish a previously non-null | 
|  | 375 | *              pointer from a pointer to an object allocated by this function. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 376 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 377 | * \note        If the sequence is empty, this function does not modify | 
|  | 378 | *              \c *cur. If the sequence is valid and non-empty, this | 
|  | 379 | *              function sets `cur->buf.tag` to \p tag. This allows | 
|  | 380 | *              callers to distinguish between an empty sequence and | 
|  | 381 | *              a one-element sequence. | 
|  | 382 | * | 
|  | 383 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 384 | *              On successful completion, \c *p is equal to \p end. | 
|  | 385 | *              On error, the value of \c *p is undefined. | 
|  | 386 | * \param end   End of data. | 
|  | 387 | * \param cur   A ::mbedtls_asn1_sequence which this function fills. | 
|  | 388 | *              When this function returns, \c *cur is the head of a linked | 
|  | 389 | *              list. Each node in this list is allocated with | 
|  | 390 | *              mbedtls_calloc() apart from \p cur itself, and should | 
|  | 391 | *              therefore be freed with mbedtls_free(). | 
|  | 392 | *              The list describes the content of the sequence. | 
|  | 393 | *              The head of the list (i.e. \c *cur itself) describes the | 
|  | 394 | *              first element, `*cur->next` describes the second element, etc. | 
|  | 395 | *              For each element, `buf.tag == tag`, `buf.len` is the length | 
|  | 396 | *              of the content of the content of the element, and `buf.p` | 
|  | 397 | *              points to the first byte of the content (i.e. immediately | 
|  | 398 | *              past the length of the element). | 
|  | 399 | *              Note that list elements may be allocated even on error. | 
|  | 400 | * \param tag   Each element of the sequence must have this tag. | 
|  | 401 | * | 
|  | 402 | * \return      0 if successful. | 
|  | 403 | * \return      #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains | 
|  | 404 | *              extra data after a valid SEQUENCE OF \p tag. | 
| Gilles Peskine | b5c74a5 | 2019-10-30 17:07:02 +0100 | [diff] [blame] | 405 | * \return      #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts with | 
|  | 406 | *              an ASN.1 SEQUENCE in which an element has a tag that | 
|  | 407 | *              is different from \p tag. | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 408 | * \return      #MBEDTLS_ERR_ASN1_ALLOC_FAILED if a memory allocation failed. | 
|  | 409 | * \return      An ASN.1 error code if the input does not start with | 
| Gilles Peskine | b5c74a5 | 2019-10-30 17:07:02 +0100 | [diff] [blame] | 410 | *              a valid ASN.1 SEQUENCE. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 411 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | int mbedtls_asn1_get_sequence_of( unsigned char **p, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 413 | const unsigned char *end, | 
|  | 414 | mbedtls_asn1_sequence *cur, | 
|  | 415 | int tag ); | 
| Hanno Becker | 12ae27d | 2019-09-11 14:20:09 +0100 | [diff] [blame] | 416 | /** | 
|  | 417 | * \brief          Free a heap-allocated linked list presentation of | 
|  | 418 | *                 an ASN.1 sequence, including the first element. | 
|  | 419 | * | 
|  | 420 | * There are two common ways to manage the memory used for the representation | 
|  | 421 | * of a parsed ASN.1 sequence: | 
|  | 422 | * - Allocate a head node `mbedtls_asn1_sequence *head` with mbedtls_calloc(). | 
|  | 423 | *   Pass this node as the `cur` argument to mbedtls_asn1_get_sequence_of(). | 
|  | 424 | *   When you have finished processing the sequence, | 
|  | 425 | *   call mbedtls_asn1_sequence_free() on `head`. | 
|  | 426 | * - Allocate a head node `mbedtls_asn1_sequence *head` in any manner, | 
|  | 427 | *   for example on the stack. Make sure that `head->next == NULL`. | 
|  | 428 | *   Pass `head` as the `cur` argument to mbedtls_asn1_get_sequence_of(). | 
|  | 429 | *   When you have finished processing the sequence, | 
|  | 430 | *   call mbedtls_asn1_sequence_free() on `head->cur`, | 
|  | 431 | *   then free `head` itself in the appropriate manner. | 
|  | 432 | * | 
|  | 433 | * \param seq      The address of the first sequence component. This may | 
|  | 434 | *                 be \c NULL, in which case this functions returns | 
|  | 435 | *                 immediately. | 
|  | 436 | */ | 
|  | 437 | void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq ); | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 438 |  | 
| Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 439 | /** | 
|  | 440 | * \brief                Traverse an ASN.1 SEQUENCE container and | 
|  | 441 | *                       call a callback for each entry. | 
|  | 442 | * | 
|  | 443 | * This function checks that the input is a SEQUENCE of elements that | 
|  | 444 | * each have a "must" tag, and calls a callback function on the elements | 
|  | 445 | * that have a "may" tag. | 
|  | 446 | * | 
|  | 447 | * For example, to validate that the input is a SEQUENCE of `tag1` and call | 
|  | 448 | * `cb` on each element, use | 
|  | 449 | * ``` | 
|  | 450 | * mbedtls_asn1_traverse_sequence_of(&p, end, 0xff, tag1, 0, 0, cb, ctx); | 
|  | 451 | * ``` | 
|  | 452 | * | 
|  | 453 | * To validate that the input is a SEQUENCE of ANY and call `cb` on | 
|  | 454 | * each element, use | 
|  | 455 | * ``` | 
|  | 456 | * mbedtls_asn1_traverse_sequence_of(&p, end, 0, 0, 0, 0, cb, ctx); | 
|  | 457 | * ``` | 
|  | 458 | * | 
|  | 459 | * To validate that the input is a SEQUENCE of CHOICE {NULL, OCTET STRING} | 
|  | 460 | * and call `cb` on each element that is an OCTET STRING, use | 
|  | 461 | * ``` | 
|  | 462 | * mbedtls_asn1_traverse_sequence_of(&p, end, 0xfe, 0x04, 0xff, 0x04, cb, ctx); | 
|  | 463 | * ``` | 
|  | 464 | * | 
|  | 465 | * The callback is called on the elements with a "may" tag from left to | 
|  | 466 | * right. If the input is not a valid SEQUENCE of elements with a "must" tag, | 
|  | 467 | * the callback is called on the elements up to the leftmost point where | 
|  | 468 | * the input is invalid. | 
|  | 469 | * | 
|  | 470 | * \warning              This function is still experimental and may change | 
|  | 471 | *                       at any time. | 
|  | 472 | * | 
|  | 473 | * \param p              The address of the pointer to the beginning of | 
|  | 474 | *                       the ASN.1 SEQUENCE header. This is updated to | 
|  | 475 | *                       point to the end of the ASN.1 SEQUENCE container | 
|  | 476 | *                       on a successful invocation. | 
|  | 477 | * \param end            The end of the ASN.1 SEQUENCE container. | 
|  | 478 | * \param tag_must_mask  A mask to be applied to the ASN.1 tags found within | 
|  | 479 | *                       the SEQUENCE before comparing to \p tag_must_value. | 
|  | 480 | * \param tag_must_val   The required value of each ASN.1 tag found in the | 
|  | 481 | *                       SEQUENCE, after masking with \p tag_must_mask. | 
|  | 482 | *                       Mismatching tags lead to an error. | 
|  | 483 | *                       For example, a value of \c 0 for both \p tag_must_mask | 
|  | 484 | *                       and \p tag_must_val means that every tag is allowed, | 
|  | 485 | *                       while a value of \c 0xFF for \p tag_must_mask means | 
|  | 486 | *                       that \p tag_must_val is the only allowed tag. | 
|  | 487 | * \param tag_may_mask   A mask to be applied to the ASN.1 tags found within | 
|  | 488 | *                       the SEQUENCE before comparing to \p tag_may_value. | 
|  | 489 | * \param tag_may_val    The desired value of each ASN.1 tag found in the | 
|  | 490 | *                       SEQUENCE, after masking with \p tag_may_mask. | 
|  | 491 | *                       Mismatching tags will be silently ignored. | 
|  | 492 | *                       For example, a value of \c 0 for \p tag_may_mask and | 
|  | 493 | *                       \p tag_may_val means that any tag will be considered, | 
|  | 494 | *                       while a value of \c 0xFF for \p tag_may_mask means | 
|  | 495 | *                       that all tags with value different from \p tag_may_val | 
|  | 496 | *                       will be ignored. | 
|  | 497 | * \param cb             The callback to trigger for each component | 
|  | 498 | *                       in the ASN.1 SEQUENCE that matches \p tag_may_val. | 
|  | 499 | *                       The callback function is called with the following | 
|  | 500 | *                       parameters: | 
|  | 501 | *                       - \p ctx. | 
|  | 502 | *                       - The tag of the current element. | 
|  | 503 | *                       - A pointer to the start of the current element's | 
|  | 504 | *                         content inside the input. | 
|  | 505 | *                       - The length of the content of the current element. | 
|  | 506 | *                       If the callback returns a non-zero value, | 
|  | 507 | *                       the function stops immediately, | 
|  | 508 | *                       forwarding the callback's return value. | 
|  | 509 | * \param ctx            The context to be passed to the callback \p cb. | 
|  | 510 | * | 
|  | 511 | * \return               \c 0 if successful the entire ASN.1 SEQUENCE | 
|  | 512 | *                       was traversed without parsing or callback errors. | 
|  | 513 | * \return               #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input | 
|  | 514 | *                       contains extra data after a valid SEQUENCE | 
|  | 515 | *                       of elements with an accepted tag. | 
|  | 516 | * \return               #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts | 
|  | 517 | *                       with an ASN.1 SEQUENCE in which an element has a tag | 
|  | 518 | *                       that is not accepted. | 
|  | 519 | * \return               An ASN.1 error code if the input does not start with | 
|  | 520 | *                       a valid ASN.1 SEQUENCE. | 
|  | 521 | * \return               A non-zero error code forwarded from the callback | 
|  | 522 | *                       \p cb in case the latter returns a non-zero value. | 
|  | 523 | */ | 
|  | 524 | int mbedtls_asn1_traverse_sequence_of( | 
|  | 525 | unsigned char **p, | 
|  | 526 | const unsigned char *end, | 
| Hanno Becker | 34aada2 | 2020-02-03 10:39:55 +0000 | [diff] [blame] | 527 | unsigned char tag_must_mask, unsigned char tag_must_val, | 
|  | 528 | unsigned char tag_may_mask, unsigned char tag_may_val, | 
| Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 529 | int (*cb)( void *ctx, int tag, | 
|  | 530 | unsigned char* start, size_t len ), | 
|  | 531 | void *ctx ); | 
|  | 532 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 533 | #if defined(MBEDTLS_BIGNUM_C) | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 534 | /** | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 535 | * \brief       Retrieve an integer ASN.1 tag and its value. | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 536 | *              Updates the pointer to immediately behind the full tag. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 537 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 538 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 539 | *              On successful completion, \c *p points to the first byte | 
|  | 540 | *              beyond the ASN.1 element. | 
|  | 541 | *              On error, the value of \c *p is undefined. | 
|  | 542 | * \param end   End of data. | 
|  | 543 | * \param X     On success, the parsed value. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 544 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 545 | * \return      0 if successful. | 
|  | 546 | * \return      An ASN.1 error code if the input does not start with | 
|  | 547 | *              a valid ASN.1 INTEGER. | 
|  | 548 | * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does | 
|  | 549 | *              not fit in an \c int. | 
|  | 550 | * \return      An MPI error code if the parsed value is too large. | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 551 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | int mbedtls_asn1_get_mpi( unsigned char **p, | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 553 | const unsigned char *end, | 
|  | 554 | mbedtls_mpi *X ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | #endif /* MBEDTLS_BIGNUM_C */ | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 556 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 557 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 558 | * \brief       Retrieve an AlgorithmIdentifier ASN.1 sequence. | 
|  | 559 | *              Updates the pointer to immediately behind the full | 
|  | 560 | *              AlgorithmIdentifier. | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 561 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 562 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 563 | *              On successful completion, \c *p points to the first byte | 
|  | 564 | *              beyond the AlgorithmIdentifier element. | 
|  | 565 | *              On error, the value of \c *p is undefined. | 
|  | 566 | * \param end   End of data. | 
|  | 567 | * \param alg   The buffer to receive the OID. | 
|  | 568 | * \param params The buffer to receive the parameters. | 
|  | 569 | *              This is zeroized if there are no parameters. | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 570 | * | 
|  | 571 | * \return      0 if successful or a specific ASN.1 or MPI error code. | 
|  | 572 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | int mbedtls_asn1_get_alg( unsigned char **p, | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 574 | const unsigned char *end, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params ); | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 576 |  | 
|  | 577 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 578 | * \brief       Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no | 
|  | 579 | *              params. | 
|  | 580 | *              Updates the pointer to immediately behind the full | 
|  | 581 | *              AlgorithmIdentifier. | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 582 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 583 | * \param p     On entry, \c *p points to the start of the ASN.1 element. | 
|  | 584 | *              On successful completion, \c *p points to the first byte | 
|  | 585 | *              beyond the AlgorithmIdentifier element. | 
|  | 586 | *              On error, the value of \c *p is undefined. | 
|  | 587 | * \param end   End of data. | 
|  | 588 | * \param alg   The buffer to receive the OID. | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 589 | * | 
|  | 590 | * \return      0 if successful or a specific ASN.1 or MPI error code. | 
|  | 591 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | int mbedtls_asn1_get_alg_null( unsigned char **p, | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 593 | const unsigned char *end, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | mbedtls_asn1_buf *alg ); | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 595 |  | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 596 | /** | 
| Paul Bakker | cdda097 | 2013-09-09 12:51:29 +0200 | [diff] [blame] | 597 | * \brief       Find a specific named_data entry in a sequence or list based on | 
|  | 598 | *              the OID. | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 599 | * | 
|  | 600 | * \param list  The list to seek through | 
|  | 601 | * \param oid   The OID to look for | 
|  | 602 | * \param len   Size of the OID | 
|  | 603 | * | 
|  | 604 | * \return      NULL if not found, or a pointer to the existing entry. | 
|  | 605 | */ | 
| Mateusz Starzyk | 59961cf | 2021-01-26 13:57:43 +0100 | [diff] [blame] | 606 | const mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( const mbedtls_asn1_named_data *list, | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 607 | const char *oid, size_t len ); | 
|  | 608 |  | 
|  | 609 | /** | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | * \brief       Free a mbedtls_asn1_named_data entry | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 611 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 612 | * \param entry The named data entry to free. | 
|  | 613 | *              This function calls mbedtls_free() on | 
|  | 614 | *              `entry->oid.p` and `entry->val.p`. | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 615 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry ); | 
| Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 617 |  | 
| Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 618 | /** | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 619 | * \brief       Free all entries in a mbedtls_asn1_named_data list. | 
| Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 620 | * | 
| Gilles Peskine | bcbe1df | 2019-03-01 17:57:43 +0100 | [diff] [blame] | 621 | * \param head  Pointer to the head of the list of named data entries to free. | 
|  | 622 | *              This function calls mbedtls_asn1_free_named_data() and | 
|  | 623 | *              mbedtls_free() on each list element and | 
|  | 624 | *              sets \c *head to \c NULL. | 
| Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 625 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head ); | 
| Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 627 |  | 
| Andrzej Kurek | 38d4fdd | 2021-12-28 16:22:52 +0100 | [diff] [blame] | 628 | /** \} name Functions to parse ASN.1 data structures */ | 
| Andrzej Kurek | a0defed | 2021-12-30 12:33:31 +0100 | [diff] [blame] | 629 | /** \} addtogroup asn1_module */ | 
| Andrzej Kurek | 38d4fdd | 2021-12-28 16:22:52 +0100 | [diff] [blame] | 630 |  | 
| Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 631 | #ifdef __cplusplus | 
|  | 632 | } | 
|  | 633 | #endif | 
|  | 634 |  | 
|  | 635 | #endif /* asn1.h */ |