blob: f45fc175116232f944f1306ad38f87de87a8407a [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"
Paul Bakkerefc30292011-11-10 14:43:23 +000027
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <stddef.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_BIGNUM_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000032#endif
33
Paul Bakker9af723c2014-05-01 13:03:14 +020034/**
Paul Bakkerefc30292011-11-10 14:43:23 +000035 * \addtogroup asn1_module
Paul Bakker9af723c2014-05-01 13:03:14 +020036 * \{
Paul Bakkerefc30292011-11-10 14:43:23 +000037 */
Paul Bakker9af723c2014-05-01 13:03:14 +020038
Paul Bakkerefc30292011-11-10 14:43:23 +000039/**
40 * \name ASN1 Error codes
41 * These error codes are OR'ed to X509 error codes for
Paul Bakker9af723c2014-05-01 13:03:14 +020042 * higher error granularity.
Paul Bakkerefc30292011-11-10 14:43:23 +000043 * ASN1 is a standard to specify data structures.
44 * \{
45 */
Gilles Peskined2971572021-07-26 18:48:10 +020046/** 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 Bakker05888152012-02-16 10:26:57 +000060
Paul Bakkerefc30292011-11-10 14:43:23 +000061/* \} name */
62
63/**
64 * \name DER constants
Andres Amaya Garcia9fb02052017-08-25 17:24:44 +010065 * These constants comply with the DER encoded ASN.1 type tags.
Paul Bakkerefc30292011-11-10 14:43:23 +000066 * 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 Peskine96ddb0a2020-02-26 19:50:01 +010071 * Such sequences are typically read into \c ::mbedtls_x509_buf.
Paul Bakkerefc30292011-11-10 14:43:23 +000072 * \{
73 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#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 Sopiha20180ca2019-10-29 15:58:10 +020080#define MBEDTLS_ASN1_ENUMERATED 0x0A
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#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 Garcia7512bf72017-08-25 17:12:11 +010094
Hanno Beckercba71222019-09-11 14:15:10 +010095/* 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 Garcia7512bf72017-08-25 17:12:11 +0100107/*
108 * Bit masks for each of the components of an ASN.1 tag as specified in
Gilles Peskine1ed45ea2018-03-08 18:16:45 +0100109 * ITU X.690 (08/2015), section 8.1 "General rules for encoding",
110 * paragraph 8.1.2.2:
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100111 *
112 * Bit 8 7 6 5 1
113 * +-------+-----+------------+
114 * | Class | P/C | Tag number |
115 * +-------+-----+------------+
116 */
Andres Amaya Garcia7786abc2017-11-07 20:21:56 +0000117#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 Garcia7512bf72017-08-25 17:12:11 +0100120
Paul Bakkerefc30292011-11-10 14:43:23 +0000121/* \} name */
122/* \} addtogroup asn1_module */
123
124/** Returns the size of the binary string, without the trailing \\0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
Paul Bakkerefc30292011-11-10 14:43:23 +0000126
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100127/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * Compares an mbedtls_asn1_buf structure to a reference OID.
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100129 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100131 * 'unsigned char *oid' here!
Paul Bakkere5eae762013-08-26 12:05:14 +0200132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#define MBEDTLS_OID_CMP(oid_str, oid_buf) \
134 ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \
Manuel Pégourié-Gonnard6e064372015-03-19 16:54:56 +0000135 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200136
Hanno Becker63e38fe2019-09-11 14:16:40 +0100137#define MBEDTLS_OID_CMP_RAW(oid_str, oid_buf, oid_buf_len) \
138 ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf_len) ) || \
139 memcmp( (oid_str), (oid_buf), (oid_buf_len) ) != 0 )
140
Paul Bakkerefc30292011-11-10 14:43:23 +0000141#ifdef __cplusplus
142extern "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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153typedef struct mbedtls_asn1_buf
Paul Bakkerefc30292011-11-10 14:43:23 +0000154{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200155 int MBEDTLS_PRIVATE(tag); /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
156 size_t MBEDTLS_PRIVATE(len); /**< ASN1 length, in octets. */
157 unsigned char *MBEDTLS_PRIVATE(p); /**< ASN1 data, e.g. in ASCII. */
Paul Bakkerefc30292011-11-10 14:43:23 +0000158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159mbedtls_asn1_buf;
Paul Bakkerefc30292011-11-10 14:43:23 +0000160
161/**
162 * Container for ASN1 bit strings.
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164typedef struct mbedtls_asn1_bitstring
Paul Bakkerefc30292011-11-10 14:43:23 +0000165{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200166 size_t MBEDTLS_PRIVATE(len); /**< ASN1 length, in octets. */
167 unsigned char MBEDTLS_PRIVATE(unused_bits); /**< Number of unused bits at the end of the string */
168 unsigned char *MBEDTLS_PRIVATE(p); /**< Raw ASN1 data for the bit string */
Paul Bakkerefc30292011-11-10 14:43:23 +0000169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170mbedtls_asn1_bitstring;
Paul Bakkerefc30292011-11-10 14:43:23 +0000171
172/**
173 * Container for a sequence of ASN.1 items
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175typedef struct mbedtls_asn1_sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000176{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200177 mbedtls_asn1_buf MBEDTLS_PRIVATE(buf); /**< Buffer containing the given ASN.1 item. */
178 struct mbedtls_asn1_sequence *MBEDTLS_PRIVATE(next); /**< The next entry in the sequence. */
Paul Bakkerefc30292011-11-10 14:43:23 +0000179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180mbedtls_asn1_sequence;
Paul Bakkerefc30292011-11-10 14:43:23 +0000181
182/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200183 * Container for a sequence or list of 'named' ASN.1 data items
184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185typedef struct mbedtls_asn1_named_data
Paul Bakkere5eae762013-08-26 12:05:14 +0200186{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200187 mbedtls_asn1_buf MBEDTLS_PRIVATE(oid); /**< The object identifier. */
188 mbedtls_asn1_buf MBEDTLS_PRIVATE(val); /**< The named value. */
189 struct mbedtls_asn1_named_data *MBEDTLS_PRIVATE(next); /**< The next entry in the sequence. */
190 unsigned char MBEDTLS_PRIVATE(next_merged); /**< Merge next item into the current one? */
Paul Bakkere5eae762013-08-26 12:05:14 +0200191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192mbedtls_asn1_named_data;
Paul Bakkere5eae762013-08-26 12:05:14 +0200193
194/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200195 * \brief Get the length of an ASN.1 element.
196 * Updates the pointer to immediately behind the length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000197 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100198 * \param p On entry, \c *p points to the first byte of the length,
199 * i.e. immediately after the tag.
200 * On successful completion, \c *p points to the first byte
201 * after the length, i.e. the first byte of the content.
202 * On error, the value of \c *p is undefined.
203 * \param end End of data.
Gilles Peskinedc2db482019-09-20 21:06:01 +0200204 * \param len On successful completion, \c *len contains the length
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100205 * read from the ASN.1 input.
Paul Bakkerefc30292011-11-10 14:43:23 +0000206 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100207 * \return 0 if successful.
208 * \return #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
209 * would end beyond \p end.
210 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparseable.
Paul Bakkerefc30292011-11-10 14:43:23 +0000211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_asn1_get_len( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100213 const unsigned char *end,
214 size_t *len );
Paul Bakkerefc30292011-11-10 14:43:23 +0000215
216/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100217 * \brief Get the tag and length of the element.
218 * Check for the requested tag.
Paul Bakkercdda0972013-09-09 12:51:29 +0200219 * Updates the pointer to immediately behind the tag and 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 start of the ASN.1 element.
222 * On successful completion, \c *p points to the first byte
223 * after the length, i.e. the first byte of the content.
224 * On error, the value of \c *p is undefined.
225 * \param end End of data.
Gilles Peskinedc2db482019-09-20 21:06:01 +0200226 * \param len On successful completion, \c *len contains the length
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100227 * read from the ASN.1 input.
228 * \param tag The expected tag.
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_UNEXPECTED_TAG if the data does not start
232 * with the requested tag.
233 * \return #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
234 * would end beyond \p end.
235 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparseable.
Paul Bakkerefc30292011-11-10 14:43:23 +0000236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237int mbedtls_asn1_get_tag( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100238 const unsigned char *end,
239 size_t *len, int tag );
Paul Bakkerefc30292011-11-10 14:43:23 +0000240
241/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200242 * \brief Retrieve a boolean ASN.1 tag and its value.
243 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000244 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100245 * \param p On entry, \c *p points to the start of the ASN.1 element.
246 * On successful completion, \c *p points to the first byte
247 * beyond the ASN.1 element.
248 * On error, the value of \c *p is undefined.
249 * \param end End of data.
250 * \param val On success, the parsed value (\c 0 or \c 1).
Paul Bakkerefc30292011-11-10 14:43:23 +0000251 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100252 * \return 0 if successful.
253 * \return An ASN.1 error code if the input does not start with
254 * a valid ASN.1 BOOLEAN.
Paul Bakkerefc30292011-11-10 14:43:23 +0000255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_asn1_get_bool( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100257 const unsigned char *end,
258 int *val );
Paul Bakkerefc30292011-11-10 14:43:23 +0000259
260/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200261 * \brief Retrieve an integer ASN.1 tag and its value.
262 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000263 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100264 * \param p On entry, \c *p points to the start of the ASN.1 element.
265 * On successful completion, \c *p points to the first byte
266 * beyond the ASN.1 element.
267 * On error, the value of \c *p is undefined.
268 * \param end End of data.
269 * \param val On success, the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000270 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100271 * \return 0 if successful.
272 * \return An ASN.1 error code if the input does not start with
273 * a valid ASN.1 INTEGER.
274 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
275 * not fit in an \c int.
Paul Bakkerefc30292011-11-10 14:43:23 +0000276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277int mbedtls_asn1_get_int( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100278 const unsigned char *end,
279 int *val );
Paul Bakkerefc30292011-11-10 14:43:23 +0000280
281/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200282 * \brief Retrieve an enumerated ASN.1 tag and its value.
283 * Updates the pointer to immediately behind the full tag.
284 *
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.
291 *
292 * \return 0 if successful.
293 * \return An ASN.1 error code if the input does not start with
294 * a valid ASN.1 ENUMERATED.
295 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
296 * not fit in an \c int.
297 */
298int mbedtls_asn1_get_enum( unsigned char **p,
299 const unsigned char *end,
300 int *val );
301
302/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200303 * \brief Retrieve a bitstring ASN.1 tag and its value.
304 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000305 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100306 * \param p On entry, \c *p points to the start of the ASN.1 element.
307 * On successful completion, \c *p is equal to \p end.
308 * On error, the value of \c *p is undefined.
309 * \param end End of data.
310 * \param bs On success, ::mbedtls_asn1_bitstring information about
311 * the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000312 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100313 * \return 0 if successful.
314 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
315 * extra data after a valid BIT STRING.
316 * \return An ASN.1 error code if the input does not start with
317 * a valid ASN.1 BIT STRING.
Paul Bakkerefc30292011-11-10 14:43:23 +0000318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100320 mbedtls_asn1_bitstring *bs );
Paul Bakkerefc30292011-11-10 14:43:23 +0000321
322/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200323 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
324 * value.
325 * Updates the pointer to the beginning of the bit/octet string.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200326 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100327 * \param p On entry, \c *p points to the start of the ASN.1 element.
328 * On successful completion, \c *p points to the first byte
329 * of the content of the BIT STRING.
330 * On error, the value of \c *p is undefined.
331 * \param end End of data.
332 * \param len On success, \c *len is the length of the content in bytes.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200333 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100334 * \return 0 if successful.
335 * \return #MBEDTLS_ERR_ASN1_INVALID_DATA if the input starts with
336 * a valid BIT STRING with a nonzero number of unused bits.
337 * \return An ASN.1 error code if the input does not start with
338 * a valid ASN.1 BIT STRING.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200339 */
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100340int mbedtls_asn1_get_bitstring_null( unsigned char **p,
341 const unsigned char *end,
342 size_t *len );
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200343
344/**
Gilles Peskinedc2db482019-09-20 21:06:01 +0200345 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>".
346 * Updates the pointer to immediately behind the full sequence tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000347 *
Hanno Becker12ae27d2019-09-11 14:20:09 +0100348 * This function allocates memory for the sequence elements. You can free
349 * the allocated memory with mbedtls_asn1_sequence_free().
350 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100351 * \note On error, this function may return a partial list in \p cur.
352 * You must set `cur->next = NULL` before calling this function!
353 * Otherwise it is impossible to distinguish a previously non-null
354 * pointer from a pointer to an object allocated by this function.
Paul Bakkerefc30292011-11-10 14:43:23 +0000355 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100356 * \note If the sequence is empty, this function does not modify
357 * \c *cur. If the sequence is valid and non-empty, this
358 * function sets `cur->buf.tag` to \p tag. This allows
359 * callers to distinguish between an empty sequence and
360 * a one-element sequence.
361 *
362 * \param p On entry, \c *p points to the start of the ASN.1 element.
363 * On successful completion, \c *p is equal to \p end.
364 * On error, the value of \c *p is undefined.
365 * \param end End of data.
366 * \param cur A ::mbedtls_asn1_sequence which this function fills.
367 * When this function returns, \c *cur is the head of a linked
368 * list. Each node in this list is allocated with
369 * mbedtls_calloc() apart from \p cur itself, and should
370 * therefore be freed with mbedtls_free().
371 * The list describes the content of the sequence.
372 * The head of the list (i.e. \c *cur itself) describes the
373 * first element, `*cur->next` describes the second element, etc.
374 * For each element, `buf.tag == tag`, `buf.len` is the length
375 * of the content of the content of the element, and `buf.p`
376 * points to the first byte of the content (i.e. immediately
377 * past the length of the element).
378 * Note that list elements may be allocated even on error.
379 * \param tag Each element of the sequence must have this tag.
380 *
381 * \return 0 if successful.
382 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
383 * extra data after a valid SEQUENCE OF \p tag.
Gilles Peskineb5c74a52019-10-30 17:07:02 +0100384 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts with
385 * an ASN.1 SEQUENCE in which an element has a tag that
386 * is different from \p tag.
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100387 * \return #MBEDTLS_ERR_ASN1_ALLOC_FAILED if a memory allocation failed.
388 * \return An ASN.1 error code if the input does not start with
Gilles Peskineb5c74a52019-10-30 17:07:02 +0100389 * a valid ASN.1 SEQUENCE.
Paul Bakkerefc30292011-11-10 14:43:23 +0000390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_asn1_get_sequence_of( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100392 const unsigned char *end,
393 mbedtls_asn1_sequence *cur,
394 int tag );
Hanno Becker12ae27d2019-09-11 14:20:09 +0100395/**
396 * \brief Free a heap-allocated linked list presentation of
397 * an ASN.1 sequence, including the first element.
398 *
399 * There are two common ways to manage the memory used for the representation
400 * of a parsed ASN.1 sequence:
401 * - Allocate a head node `mbedtls_asn1_sequence *head` with mbedtls_calloc().
402 * Pass this node as the `cur` argument to mbedtls_asn1_get_sequence_of().
403 * When you have finished processing the sequence,
404 * call mbedtls_asn1_sequence_free() on `head`.
405 * - Allocate a head node `mbedtls_asn1_sequence *head` in any manner,
406 * for example on the stack. Make sure that `head->next == NULL`.
407 * Pass `head` as the `cur` argument to mbedtls_asn1_get_sequence_of().
408 * When you have finished processing the sequence,
409 * call mbedtls_asn1_sequence_free() on `head->cur`,
410 * then free `head` itself in the appropriate manner.
411 *
412 * \param seq The address of the first sequence component. This may
413 * be \c NULL, in which case this functions returns
414 * immediately.
415 */
416void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq );
Paul Bakkerefc30292011-11-10 14:43:23 +0000417
Hanno Becker199b7092019-09-11 14:21:26 +0100418/**
419 * \brief Traverse an ASN.1 SEQUENCE container and
420 * call a callback for each entry.
421 *
422 * This function checks that the input is a SEQUENCE of elements that
423 * each have a "must" tag, and calls a callback function on the elements
424 * that have a "may" tag.
425 *
426 * For example, to validate that the input is a SEQUENCE of `tag1` and call
427 * `cb` on each element, use
428 * ```
429 * mbedtls_asn1_traverse_sequence_of(&p, end, 0xff, tag1, 0, 0, cb, ctx);
430 * ```
431 *
432 * To validate that the input is a SEQUENCE of ANY and call `cb` on
433 * each element, use
434 * ```
435 * mbedtls_asn1_traverse_sequence_of(&p, end, 0, 0, 0, 0, cb, ctx);
436 * ```
437 *
438 * To validate that the input is a SEQUENCE of CHOICE {NULL, OCTET STRING}
439 * and call `cb` on each element that is an OCTET STRING, use
440 * ```
441 * mbedtls_asn1_traverse_sequence_of(&p, end, 0xfe, 0x04, 0xff, 0x04, cb, ctx);
442 * ```
443 *
444 * The callback is called on the elements with a "may" tag from left to
445 * right. If the input is not a valid SEQUENCE of elements with a "must" tag,
446 * the callback is called on the elements up to the leftmost point where
447 * the input is invalid.
448 *
449 * \warning This function is still experimental and may change
450 * at any time.
451 *
452 * \param p The address of the pointer to the beginning of
453 * the ASN.1 SEQUENCE header. This is updated to
454 * point to the end of the ASN.1 SEQUENCE container
455 * on a successful invocation.
456 * \param end The end of the ASN.1 SEQUENCE container.
457 * \param tag_must_mask A mask to be applied to the ASN.1 tags found within
458 * the SEQUENCE before comparing to \p tag_must_value.
459 * \param tag_must_val The required value of each ASN.1 tag found in the
460 * SEQUENCE, after masking with \p tag_must_mask.
461 * Mismatching tags lead to an error.
462 * For example, a value of \c 0 for both \p tag_must_mask
463 * and \p tag_must_val means that every tag is allowed,
464 * while a value of \c 0xFF for \p tag_must_mask means
465 * that \p tag_must_val is the only allowed tag.
466 * \param tag_may_mask A mask to be applied to the ASN.1 tags found within
467 * the SEQUENCE before comparing to \p tag_may_value.
468 * \param tag_may_val The desired value of each ASN.1 tag found in the
469 * SEQUENCE, after masking with \p tag_may_mask.
470 * Mismatching tags will be silently ignored.
471 * For example, a value of \c 0 for \p tag_may_mask and
472 * \p tag_may_val means that any tag will be considered,
473 * while a value of \c 0xFF for \p tag_may_mask means
474 * that all tags with value different from \p tag_may_val
475 * will be ignored.
476 * \param cb The callback to trigger for each component
477 * in the ASN.1 SEQUENCE that matches \p tag_may_val.
478 * The callback function is called with the following
479 * parameters:
480 * - \p ctx.
481 * - The tag of the current element.
482 * - A pointer to the start of the current element's
483 * content inside the input.
484 * - The length of the content of the current element.
485 * If the callback returns a non-zero value,
486 * the function stops immediately,
487 * forwarding the callback's return value.
488 * \param ctx The context to be passed to the callback \p cb.
489 *
490 * \return \c 0 if successful the entire ASN.1 SEQUENCE
491 * was traversed without parsing or callback errors.
492 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input
493 * contains extra data after a valid SEQUENCE
494 * of elements with an accepted tag.
495 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts
496 * with an ASN.1 SEQUENCE in which an element has a tag
497 * that is not accepted.
498 * \return An ASN.1 error code if the input does not start with
499 * a valid ASN.1 SEQUENCE.
500 * \return A non-zero error code forwarded from the callback
501 * \p cb in case the latter returns a non-zero value.
502 */
503int mbedtls_asn1_traverse_sequence_of(
504 unsigned char **p,
505 const unsigned char *end,
Hanno Becker34aada22020-02-03 10:39:55 +0000506 unsigned char tag_must_mask, unsigned char tag_must_val,
507 unsigned char tag_may_mask, unsigned char tag_may_val,
Hanno Becker199b7092019-09-11 14:21:26 +0100508 int (*cb)( void *ctx, int tag,
509 unsigned char* start, size_t len ),
510 void *ctx );
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_BIGNUM_C)
Paul Bakkerefc30292011-11-10 14:43:23 +0000513/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100514 * \brief Retrieve an integer ASN.1 tag and its value.
Paul Bakkercdda0972013-09-09 12:51:29 +0200515 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000516 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100517 * \param p On entry, \c *p points to the start of the ASN.1 element.
518 * On successful completion, \c *p points to the first byte
519 * beyond the ASN.1 element.
520 * On error, the value of \c *p is undefined.
521 * \param end End of data.
522 * \param X On success, the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000523 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100524 * \return 0 if successful.
525 * \return An ASN.1 error code if the input does not start with
526 * a valid ASN.1 INTEGER.
527 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
528 * not fit in an \c int.
529 * \return An MPI error code if the parsed value is too large.
Paul Bakkerefc30292011-11-10 14:43:23 +0000530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531int mbedtls_asn1_get_mpi( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100532 const unsigned char *end,
533 mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkerefc30292011-11-10 14:43:23 +0000535
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200536/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200537 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
538 * Updates the pointer to immediately behind the full
539 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200540 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100541 * \param p On entry, \c *p points to the start of the ASN.1 element.
542 * On successful completion, \c *p points to the first byte
543 * beyond the AlgorithmIdentifier element.
544 * On error, the value of \c *p is undefined.
545 * \param end End of data.
546 * \param alg The buffer to receive the OID.
547 * \param params The buffer to receive the parameters.
548 * This is zeroized if there are no parameters.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200549 *
550 * \return 0 if successful or a specific ASN.1 or MPI error code.
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552int mbedtls_asn1_get_alg( unsigned char **p,
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200553 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200555
556/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200557 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
558 * params.
559 * Updates the pointer to immediately behind the full
560 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200561 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100562 * \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.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200568 *
569 * \return 0 if successful or a specific ASN.1 or MPI error code.
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_asn1_get_alg_null( unsigned char **p,
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200572 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_asn1_buf *alg );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200574
Paul Bakkere5eae762013-08-26 12:05:14 +0200575/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200576 * \brief Find a specific named_data entry in a sequence or list based on
577 * the OID.
Paul Bakkere5eae762013-08-26 12:05:14 +0200578 *
579 * \param list The list to seek through
580 * \param oid The OID to look for
581 * \param len Size of the OID
582 *
583 * \return NULL if not found, or a pointer to the existing entry.
584 */
Mateusz Starzyk59961cf2021-01-26 13:57:43 +0100585const mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( const mbedtls_asn1_named_data *list,
Paul Bakkere5eae762013-08-26 12:05:14 +0200586 const char *oid, size_t len );
587
588/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 * \brief Free a mbedtls_asn1_named_data entry
Paul Bakkere5eae762013-08-26 12:05:14 +0200590 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100591 * \param entry The named data entry to free.
592 * This function calls mbedtls_free() on
593 * `entry->oid.p` and `entry->val.p`.
Paul Bakkere5eae762013-08-26 12:05:14 +0200594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
Paul Bakkere5eae762013-08-26 12:05:14 +0200596
Paul Bakkerc547cc92013-09-09 12:01:23 +0200597/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100598 * \brief Free all entries in a mbedtls_asn1_named_data list.
Paul Bakkerc547cc92013-09-09 12:01:23 +0200599 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100600 * \param head Pointer to the head of the list of named data entries to free.
601 * This function calls mbedtls_asn1_free_named_data() and
602 * mbedtls_free() on each list element and
603 * sets \c *head to \c NULL.
Paul Bakkerc547cc92013-09-09 12:01:23 +0200604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
Paul Bakkerc547cc92013-09-09 12:01:23 +0200606
Paul Bakkerefc30292011-11-10 14:43:23 +0000607#ifdef __cplusplus
608}
609#endif
610
611#endif /* asn1.h */