blob: 5e3f3878c12f6df4bc7432a7738c328098b1c216 [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/**
2 * \file asn1.h
3 *
4 * \brief Generic ASN.1 parsing
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * 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 Bakkerefc30292011-11-10 14:43:23 +000021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_ASN1_H
23#define MBEDTLS_ASN1_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Glenn Strauss82ba2742022-11-04 04:01:23 -040027#include "mbedtls/platform_util.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <stddef.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_BIGNUM_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000033#endif
34
Paul Bakker9af723c2014-05-01 13:03:14 +020035/**
Paul Bakkerefc30292011-11-10 14:43:23 +000036 * \addtogroup asn1_module
Paul Bakker9af723c2014-05-01 13:03:14 +020037 * \{
Paul Bakkerefc30292011-11-10 14:43:23 +000038 */
Paul Bakker9af723c2014-05-01 13:03:14 +020039
Paul Bakkerefc30292011-11-10 14:43:23 +000040/**
41 * \name ASN1 Error codes
Nayna Jainc9deb182020-11-16 19:03:12 +000042 * These error codes are combined with other error codes for
Paul Bakker9af723c2014-05-01 13:03:14 +020043 * higher error granularity.
Nayna Jainc9deb182020-11-16 19:03:12 +000044 * e.g. X.509 and PKCS #7 error codes
Paul Bakkerefc30292011-11-10 14:43:23 +000045 * ASN1 is a standard to specify data structures.
46 * \{
47 */
Gilles Peskined2971572021-07-26 18:48:10 +020048/** 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 Bakker05888152012-02-16 10:26:57 +000062
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +010063/** \} name ASN1 Error codes */
Paul Bakkerefc30292011-11-10 14:43:23 +000064
65/**
66 * \name DER constants
Andres Amaya Garcia9fb02052017-08-25 17:24:44 +010067 * These constants comply with the DER encoded ASN.1 type tags.
Paul Bakkerefc30292011-11-10 14:43:23 +000068 * 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 Peskine96ddb0a2020-02-26 19:50:01 +010073 * Such sequences are typically read into \c ::mbedtls_x509_buf.
Paul Bakkerefc30292011-11-10 14:43:23 +000074 * \{
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#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 Sopiha20180ca2019-10-29 15:58:10 +020082#define MBEDTLS_ASN1_ENUMERATED 0x0A
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#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 Garcia7512bf72017-08-25 17:12:11 +010096
Hanno Beckercba71222019-09-11 14:15:10 +010097/* Slightly smaller way to check if tag is a string tag
98 * compared to canonical implementation. */
99#define MBEDTLS_ASN1_IS_STRING_TAG( tag ) \
100 ( ( 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 ) | \
107 ( 1u << MBEDTLS_ASN1_BIT_STRING ) ) ) != 0 ) )
108
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100109/*
110 * Bit masks for each of the components of an ASN.1 tag as specified in
Gilles Peskine1ed45ea2018-03-08 18:16:45 +0100111 * ITU X.690 (08/2015), section 8.1 "General rules for encoding",
112 * paragraph 8.1.2.2:
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100113 *
114 * Bit 8 7 6 5 1
115 * +-------+-----+------------+
116 * | Class | P/C | Tag number |
117 * +-------+-----+------------+
118 */
Andres Amaya Garcia7786abc2017-11-07 20:21:56 +0000119#define MBEDTLS_ASN1_TAG_CLASS_MASK 0xC0
120#define MBEDTLS_ASN1_TAG_PC_MASK 0x20
121#define MBEDTLS_ASN1_TAG_VALUE_MASK 0x1F
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100122
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100123/** \} name DER constants */
Paul Bakkerefc30292011-11-10 14:43:23 +0000124
125/** Returns the size of the binary string, without the trailing \\0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
Paul Bakkerefc30292011-11-10 14:43:23 +0000127
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100128/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 * Compares an mbedtls_asn1_buf structure to a reference OID.
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100130 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100132 * 'unsigned char *oid' here!
Paul Bakkere5eae762013-08-26 12:05:14 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#define MBEDTLS_OID_CMP(oid_str, oid_buf) \
135 ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \
Manuel Pégourié-Gonnard6e064372015-03-19 16:54:56 +0000136 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200137
Hanno Becker63e38fe2019-09-11 14:16:40 +0100138#define MBEDTLS_OID_CMP_RAW(oid_str, oid_buf, oid_buf_len) \
139 ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf_len) ) || \
140 memcmp( (oid_str), (oid_buf), (oid_buf_len) ) != 0 )
141
Paul Bakkerefc30292011-11-10 14:43:23 +0000142#ifdef __cplusplus
143extern "C" {
144#endif
145
146/**
147 * \name Functions to parse ASN.1 data structures
148 * \{
149 */
150
151/**
152 * Type-length-value structure that allows for ASN1 using DER.
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154typedef struct mbedtls_asn1_buf
Paul Bakkerefc30292011-11-10 14:43:23 +0000155{
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200156 int tag; /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
157 size_t len; /**< ASN1 length, in octets. */
158 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
Paul Bakkerefc30292011-11-10 14:43:23 +0000159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160mbedtls_asn1_buf;
Paul Bakkerefc30292011-11-10 14:43:23 +0000161
162/**
163 * Container for ASN1 bit strings.
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165typedef struct mbedtls_asn1_bitstring
Paul Bakkerefc30292011-11-10 14:43:23 +0000166{
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200167 size_t len; /**< ASN1 length, in octets. */
168 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
169 unsigned char *p; /**< Raw ASN1 data for the bit string */
Paul Bakkerefc30292011-11-10 14:43:23 +0000170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171mbedtls_asn1_bitstring;
Paul Bakkerefc30292011-11-10 14:43:23 +0000172
173/**
174 * Container for a sequence of ASN.1 items
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176typedef struct mbedtls_asn1_sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000177{
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200178 mbedtls_asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
179
180 /** The next entry in the sequence.
181 *
Gilles Peskine44ffc792021-08-31 22:59:35 +0200182 * The details of memory management for sequences are not documented and
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200183 * may change in future versions. Set this field to \p NULL when
Gilles Peskine44ffc792021-08-31 22:59:35 +0200184 * initializing a structure, and do not modify it except via Mbed TLS
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200185 * library functions.
186 */
187 struct mbedtls_asn1_sequence *next;
Paul Bakkerefc30292011-11-10 14:43:23 +0000188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189mbedtls_asn1_sequence;
Paul Bakkerefc30292011-11-10 14:43:23 +0000190
191/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200192 * Container for a sequence or list of 'named' ASN.1 data items
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194typedef struct mbedtls_asn1_named_data
Paul Bakkere5eae762013-08-26 12:05:14 +0200195{
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200196 mbedtls_asn1_buf oid; /**< The object identifier. */
197 mbedtls_asn1_buf val; /**< The named value. */
198
199 /** The next entry in the sequence.
200 *
Gilles Peskine44ffc792021-08-31 22:59:35 +0200201 * The details of memory management for named data sequences are not
202 * documented and may change in future versions. Set this field to \p NULL
203 * when initializing a structure, and do not modify it except via Mbed TLS
Gilles Peskineb89d9c02021-08-04 20:55:33 +0200204 * library functions.
205 */
206 struct mbedtls_asn1_named_data *next;
207
208 /** Merge next item into the current one?
209 *
210 * This field exists for the sake of Mbed TLS's X.509 certificate parsing
211 * code and may change in future versions of the library.
212 */
213 unsigned char MBEDTLS_PRIVATE(next_merged);
Paul Bakkere5eae762013-08-26 12:05:14 +0200214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215mbedtls_asn1_named_data;
Paul Bakkere5eae762013-08-26 12:05:14 +0200216
217/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200218 * \brief Get the length of an ASN.1 element.
219 * Updates the pointer to immediately behind the length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000220 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100221 * \param p On entry, \c *p points to the first byte of the length,
222 * i.e. immediately after the tag.
223 * On successful completion, \c *p points to the first byte
224 * after the length, i.e. the first byte of the content.
225 * On error, the value of \c *p is undefined.
226 * \param end End of data.
Gilles Peskinedc2db482019-09-20 21:06:01 +0200227 * \param len On successful completion, \c *len contains the length
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100228 * read from the ASN.1 input.
Paul Bakkerefc30292011-11-10 14:43:23 +0000229 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100230 * \return 0 if successful.
231 * \return #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
232 * would end beyond \p end.
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400233 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparsable.
Paul Bakkerefc30292011-11-10 14:43:23 +0000234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_asn1_get_len( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100236 const unsigned char *end,
237 size_t *len );
Paul Bakkerefc30292011-11-10 14:43:23 +0000238
239/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100240 * \brief Get the tag and length of the element.
241 * Check for the requested tag.
Paul Bakkercdda0972013-09-09 12:51:29 +0200242 * Updates the pointer to immediately behind the tag and length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000243 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100244 * \param p On entry, \c *p points to the start of the ASN.1 element.
245 * On successful completion, \c *p points to the first byte
246 * after the length, i.e. the first byte of the content.
247 * On error, the value of \c *p is undefined.
248 * \param end End of data.
Gilles Peskinedc2db482019-09-20 21:06:01 +0200249 * \param len On successful completion, \c *len contains the length
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100250 * read from the ASN.1 input.
251 * \param tag The expected tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000252 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100253 * \return 0 if successful.
254 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the data does not start
255 * with the requested tag.
256 * \return #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
257 * would end beyond \p end.
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800258 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparsable.
Paul Bakkerefc30292011-11-10 14:43:23 +0000259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_asn1_get_tag( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100261 const unsigned char *end,
262 size_t *len, int tag );
Paul Bakkerefc30292011-11-10 14:43:23 +0000263
264/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200265 * \brief Retrieve a boolean ASN.1 tag and its value.
266 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000267 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100268 * \param p On entry, \c *p points to the start of the ASN.1 element.
269 * On successful completion, \c *p points to the first byte
270 * beyond the ASN.1 element.
271 * On error, the value of \c *p is undefined.
272 * \param end End of data.
273 * \param val On success, the parsed value (\c 0 or \c 1).
Paul Bakkerefc30292011-11-10 14:43:23 +0000274 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100275 * \return 0 if successful.
276 * \return An ASN.1 error code if the input does not start with
277 * a valid ASN.1 BOOLEAN.
Paul Bakkerefc30292011-11-10 14:43:23 +0000278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_asn1_get_bool( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100280 const unsigned char *end,
281 int *val );
Paul Bakkerefc30292011-11-10 14:43:23 +0000282
283/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200284 * \brief Retrieve an integer ASN.1 tag and its value.
285 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000286 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100287 * \param p On entry, \c *p points to the start of the ASN.1 element.
288 * On successful completion, \c *p points to the first byte
289 * beyond the ASN.1 element.
290 * On error, the value of \c *p is undefined.
291 * \param end End of data.
292 * \param val On success, the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000293 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100294 * \return 0 if successful.
295 * \return An ASN.1 error code if the input does not start with
296 * a valid ASN.1 INTEGER.
297 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
298 * not fit in an \c int.
Paul Bakkerefc30292011-11-10 14:43:23 +0000299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_asn1_get_int( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100301 const unsigned char *end,
302 int *val );
Paul Bakkerefc30292011-11-10 14:43:23 +0000303
304/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200305 * \brief Retrieve an enumerated ASN.1 tag and its value.
306 * Updates the pointer to immediately behind the full tag.
307 *
308 * \param p On entry, \c *p points to the start of the ASN.1 element.
309 * On successful completion, \c *p points to the first byte
310 * beyond the ASN.1 element.
311 * On error, the value of \c *p is undefined.
312 * \param end End of data.
313 * \param val On success, the parsed value.
314 *
315 * \return 0 if successful.
316 * \return An ASN.1 error code if the input does not start with
317 * a valid ASN.1 ENUMERATED.
318 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
319 * not fit in an \c int.
320 */
321int mbedtls_asn1_get_enum( unsigned char **p,
322 const unsigned char *end,
323 int *val );
324
325/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200326 * \brief Retrieve a bitstring ASN.1 tag and its value.
327 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000328 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100329 * \param p On entry, \c *p points to the start of the ASN.1 element.
330 * On successful completion, \c *p is equal to \p end.
331 * On error, the value of \c *p is undefined.
332 * \param end End of data.
333 * \param bs On success, ::mbedtls_asn1_bitstring information about
334 * the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000335 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100336 * \return 0 if successful.
337 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
338 * extra data after a valid BIT STRING.
339 * \return An ASN.1 error code if the input does not start with
340 * a valid ASN.1 BIT STRING.
Paul Bakkerefc30292011-11-10 14:43:23 +0000341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100343 mbedtls_asn1_bitstring *bs );
Paul Bakkerefc30292011-11-10 14:43:23 +0000344
345/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200346 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
347 * value.
348 * Updates the pointer to the beginning of the bit/octet string.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200349 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100350 * \param p On entry, \c *p points to the start of the ASN.1 element.
351 * On successful completion, \c *p points to the first byte
352 * of the content of the BIT STRING.
353 * On error, the value of \c *p is undefined.
354 * \param end End of data.
355 * \param len On success, \c *len is the length of the content in bytes.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200356 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100357 * \return 0 if successful.
358 * \return #MBEDTLS_ERR_ASN1_INVALID_DATA if the input starts with
359 * a valid BIT STRING with a nonzero number of unused bits.
360 * \return An ASN.1 error code if the input does not start with
361 * a valid ASN.1 BIT STRING.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200362 */
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100363int mbedtls_asn1_get_bitstring_null( unsigned char **p,
364 const unsigned char *end,
365 size_t *len );
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200366
367/**
Gilles Peskinedc2db482019-09-20 21:06:01 +0200368 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>".
369 * Updates the pointer to immediately behind the full sequence tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000370 *
Hanno Becker12ae27d2019-09-11 14:20:09 +0100371 * This function allocates memory for the sequence elements. You can free
372 * the allocated memory with mbedtls_asn1_sequence_free().
373 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100374 * \note On error, this function may return a partial list in \p cur.
375 * You must set `cur->next = NULL` before calling this function!
376 * Otherwise it is impossible to distinguish a previously non-null
377 * pointer from a pointer to an object allocated by this function.
Paul Bakkerefc30292011-11-10 14:43:23 +0000378 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100379 * \note If the sequence is empty, this function does not modify
380 * \c *cur. If the sequence is valid and non-empty, this
381 * function sets `cur->buf.tag` to \p tag. This allows
382 * callers to distinguish between an empty sequence and
383 * a one-element sequence.
384 *
385 * \param p On entry, \c *p points to the start of the ASN.1 element.
386 * On successful completion, \c *p is equal to \p end.
387 * On error, the value of \c *p is undefined.
388 * \param end End of data.
389 * \param cur A ::mbedtls_asn1_sequence which this function fills.
390 * When this function returns, \c *cur is the head of a linked
391 * list. Each node in this list is allocated with
392 * mbedtls_calloc() apart from \p cur itself, and should
393 * therefore be freed with mbedtls_free().
394 * The list describes the content of the sequence.
395 * The head of the list (i.e. \c *cur itself) describes the
396 * first element, `*cur->next` describes the second element, etc.
397 * For each element, `buf.tag == tag`, `buf.len` is the length
398 * of the content of the content of the element, and `buf.p`
399 * points to the first byte of the content (i.e. immediately
400 * past the length of the element).
401 * Note that list elements may be allocated even on error.
402 * \param tag Each element of the sequence must have this tag.
403 *
404 * \return 0 if successful.
405 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
406 * extra data after a valid SEQUENCE OF \p tag.
Gilles Peskineb5c74a52019-10-30 17:07:02 +0100407 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts with
408 * an ASN.1 SEQUENCE in which an element has a tag that
409 * is different from \p tag.
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100410 * \return #MBEDTLS_ERR_ASN1_ALLOC_FAILED if a memory allocation failed.
411 * \return An ASN.1 error code if the input does not start with
Gilles Peskineb5c74a52019-10-30 17:07:02 +0100412 * a valid ASN.1 SEQUENCE.
Paul Bakkerefc30292011-11-10 14:43:23 +0000413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_asn1_get_sequence_of( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100415 const unsigned char *end,
416 mbedtls_asn1_sequence *cur,
417 int tag );
Hanno Becker12ae27d2019-09-11 14:20:09 +0100418/**
419 * \brief Free a heap-allocated linked list presentation of
420 * an ASN.1 sequence, including the first element.
421 *
422 * There are two common ways to manage the memory used for the representation
423 * of a parsed ASN.1 sequence:
424 * - Allocate a head node `mbedtls_asn1_sequence *head` with mbedtls_calloc().
425 * Pass this node 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`.
428 * - Allocate a head node `mbedtls_asn1_sequence *head` in any manner,
429 * for example on the stack. Make sure that `head->next == NULL`.
430 * Pass `head` as the `cur` argument to mbedtls_asn1_get_sequence_of().
431 * When you have finished processing the sequence,
432 * call mbedtls_asn1_sequence_free() on `head->cur`,
433 * then free `head` itself in the appropriate manner.
434 *
435 * \param seq The address of the first sequence component. This may
436 * be \c NULL, in which case this functions returns
437 * immediately.
438 */
439void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq );
Paul Bakkerefc30292011-11-10 14:43:23 +0000440
Hanno Becker199b7092019-09-11 14:21:26 +0100441/**
442 * \brief Traverse an ASN.1 SEQUENCE container and
443 * call a callback for each entry.
444 *
445 * This function checks that the input is a SEQUENCE of elements that
446 * each have a "must" tag, and calls a callback function on the elements
447 * that have a "may" tag.
448 *
449 * For example, to validate that the input is a SEQUENCE of `tag1` and call
450 * `cb` on each element, use
451 * ```
452 * mbedtls_asn1_traverse_sequence_of(&p, end, 0xff, tag1, 0, 0, cb, ctx);
453 * ```
454 *
455 * To validate that the input is a SEQUENCE of ANY and call `cb` on
456 * each element, use
457 * ```
458 * mbedtls_asn1_traverse_sequence_of(&p, end, 0, 0, 0, 0, cb, ctx);
459 * ```
460 *
461 * To validate that the input is a SEQUENCE of CHOICE {NULL, OCTET STRING}
462 * and call `cb` on each element that is an OCTET STRING, use
463 * ```
464 * mbedtls_asn1_traverse_sequence_of(&p, end, 0xfe, 0x04, 0xff, 0x04, cb, ctx);
465 * ```
466 *
467 * The callback is called on the elements with a "may" tag from left to
468 * right. If the input is not a valid SEQUENCE of elements with a "must" tag,
469 * the callback is called on the elements up to the leftmost point where
470 * the input is invalid.
471 *
472 * \warning This function is still experimental and may change
473 * at any time.
474 *
475 * \param p The address of the pointer to the beginning of
476 * the ASN.1 SEQUENCE header. This is updated to
477 * point to the end of the ASN.1 SEQUENCE container
478 * on a successful invocation.
479 * \param end The end of the ASN.1 SEQUENCE container.
480 * \param tag_must_mask A mask to be applied to the ASN.1 tags found within
481 * the SEQUENCE before comparing to \p tag_must_value.
482 * \param tag_must_val The required value of each ASN.1 tag found in the
483 * SEQUENCE, after masking with \p tag_must_mask.
484 * Mismatching tags lead to an error.
485 * For example, a value of \c 0 for both \p tag_must_mask
486 * and \p tag_must_val means that every tag is allowed,
487 * while a value of \c 0xFF for \p tag_must_mask means
488 * that \p tag_must_val is the only allowed tag.
489 * \param tag_may_mask A mask to be applied to the ASN.1 tags found within
490 * the SEQUENCE before comparing to \p tag_may_value.
491 * \param tag_may_val The desired value of each ASN.1 tag found in the
492 * SEQUENCE, after masking with \p tag_may_mask.
493 * Mismatching tags will be silently ignored.
494 * For example, a value of \c 0 for \p tag_may_mask and
495 * \p tag_may_val means that any tag will be considered,
496 * while a value of \c 0xFF for \p tag_may_mask means
497 * that all tags with value different from \p tag_may_val
498 * will be ignored.
499 * \param cb The callback to trigger for each component
500 * in the ASN.1 SEQUENCE that matches \p tag_may_val.
501 * The callback function is called with the following
502 * parameters:
503 * - \p ctx.
504 * - The tag of the current element.
505 * - A pointer to the start of the current element's
506 * content inside the input.
507 * - The length of the content of the current element.
508 * If the callback returns a non-zero value,
509 * the function stops immediately,
510 * forwarding the callback's return value.
511 * \param ctx The context to be passed to the callback \p cb.
512 *
513 * \return \c 0 if successful the entire ASN.1 SEQUENCE
514 * was traversed without parsing or callback errors.
515 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input
516 * contains extra data after a valid SEQUENCE
517 * of elements with an accepted tag.
518 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts
519 * with an ASN.1 SEQUENCE in which an element has a tag
520 * that is not accepted.
521 * \return An ASN.1 error code if the input does not start with
522 * a valid ASN.1 SEQUENCE.
523 * \return A non-zero error code forwarded from the callback
524 * \p cb in case the latter returns a non-zero value.
525 */
526int mbedtls_asn1_traverse_sequence_of(
527 unsigned char **p,
528 const unsigned char *end,
Hanno Becker34aada22020-02-03 10:39:55 +0000529 unsigned char tag_must_mask, unsigned char tag_must_val,
530 unsigned char tag_may_mask, unsigned char tag_may_val,
Hanno Becker199b7092019-09-11 14:21:26 +0100531 int (*cb)( void *ctx, int tag,
532 unsigned char* start, size_t len ),
533 void *ctx );
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_BIGNUM_C)
Paul Bakkerefc30292011-11-10 14:43:23 +0000536/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100537 * \brief Retrieve an integer ASN.1 tag and its value.
Paul Bakkercdda0972013-09-09 12:51:29 +0200538 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000539 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100540 * \param p On entry, \c *p points to the start of the ASN.1 element.
541 * On successful completion, \c *p points to the first byte
542 * beyond the ASN.1 element.
543 * On error, the value of \c *p is undefined.
544 * \param end End of data.
545 * \param X On success, the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000546 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100547 * \return 0 if successful.
548 * \return An ASN.1 error code if the input does not start with
549 * a valid ASN.1 INTEGER.
550 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
551 * not fit in an \c int.
552 * \return An MPI error code if the parsed value is too large.
Paul Bakkerefc30292011-11-10 14:43:23 +0000553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554int mbedtls_asn1_get_mpi( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100555 const unsigned char *end,
556 mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkerefc30292011-11-10 14:43:23 +0000558
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200559/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200560 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
561 * Updates the pointer to immediately behind the full
562 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200563 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100564 * \param p On entry, \c *p points to the start of the ASN.1 element.
565 * On successful completion, \c *p points to the first byte
566 * beyond the AlgorithmIdentifier element.
567 * On error, the value of \c *p is undefined.
568 * \param end End of data.
569 * \param alg The buffer to receive the OID.
570 * \param params The buffer to receive the parameters.
571 * This is zeroized if there are no parameters.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200572 *
573 * \return 0 if successful or a specific ASN.1 or MPI error code.
574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_asn1_get_alg( unsigned char **p,
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200576 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200578
579/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200580 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
581 * params.
582 * Updates the pointer to immediately behind the full
583 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200584 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100585 * \param p On entry, \c *p points to the start of the ASN.1 element.
586 * On successful completion, \c *p points to the first byte
587 * beyond the AlgorithmIdentifier element.
588 * On error, the value of \c *p is undefined.
589 * \param end End of data.
590 * \param alg The buffer to receive the OID.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200591 *
592 * \return 0 if successful or a specific ASN.1 or MPI error code.
593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_asn1_get_alg_null( unsigned char **p,
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200595 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_asn1_buf *alg );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200597
Paul Bakkere5eae762013-08-26 12:05:14 +0200598/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200599 * \brief Find a specific named_data entry in a sequence or list based on
600 * the OID.
Paul Bakkere5eae762013-08-26 12:05:14 +0200601 *
602 * \param list The list to seek through
603 * \param oid The OID to look for
604 * \param len Size of the OID
605 *
606 * \return NULL if not found, or a pointer to the existing entry.
607 */
Mateusz Starzyk59961cf2021-01-26 13:57:43 +0100608const mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( const mbedtls_asn1_named_data *list,
Paul Bakkere5eae762013-08-26 12:05:14 +0200609 const char *oid, size_t len );
610
Glenn Strauss82ba2742022-11-04 04:01:23 -0400611#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Paul Bakkere5eae762013-08-26 12:05:14 +0200612/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 * \brief Free a mbedtls_asn1_named_data entry
Paul Bakkere5eae762013-08-26 12:05:14 +0200614 *
Glenn Strauss82ba2742022-11-04 04:01:23 -0400615 * \deprecated This function is deprecated and will be removed in a
616 * future version of the library.
617 * Please use mbedtls_asn1_free_named_data_list()
618 * or mbedtls_asn1_free_named_data_list_shallow().
619 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100620 * \param entry The named data entry to free.
621 * This function calls mbedtls_free() on
622 * `entry->oid.p` and `entry->val.p`.
Paul Bakkere5eae762013-08-26 12:05:14 +0200623 */
Glenn Strauss82ba2742022-11-04 04:01:23 -0400624void MBEDTLS_DEPRECATED mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
625#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakkere5eae762013-08-26 12:05:14 +0200626
Paul Bakkerc547cc92013-09-09 12:01:23 +0200627/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100628 * \brief Free all entries in a mbedtls_asn1_named_data list.
Paul Bakkerc547cc92013-09-09 12:01:23 +0200629 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100630 * \param head Pointer to the head of the list of named data entries to free.
Glenn Strauss82ba2742022-11-04 04:01:23 -0400631 * This function calls mbedtls_free() on
632 * `entry->oid.p` and `entry->val.p` and then on `entry`
633 * for each list entry, and sets \c *head to \c NULL.
Paul Bakkerc547cc92013-09-09 12:01:23 +0200634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
Paul Bakkerc547cc92013-09-09 12:01:23 +0200636
Glenn Straussa4b40412022-06-26 19:32:09 -0400637/**
638 * \brief Free all shallow entries in a mbedtls_asn1_named_data list,
639 * but do not free internal pointer targets.
640 *
641 * \param name Head of the list of named data entries to free.
642 * This function calls mbedtls_free() on each list element.
643 */
644void mbedtls_asn1_free_named_data_list_shallow( mbedtls_asn1_named_data *name );
645
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100646/** \} name Functions to parse ASN.1 data structures */
Andrzej Kureka0defed2021-12-30 12:33:31 +0100647/** \} addtogroup asn1_module */
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100648
Paul Bakkerefc30292011-11-10 14:43:23 +0000649#ifdef __cplusplus
650}
651#endif
652
653#endif /* asn1.h */