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