blob: 66119810f8ab357f620af1d23d2008528bd1a4b4 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkerefc30292011-11-10 14:43:23 +000031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_BIGNUM_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010035#include "mbedtls/bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000036#endif
37
Paul Bakker9af723c2014-05-01 13:03:14 +020038/**
Paul Bakkerefc30292011-11-10 14:43:23 +000039 * \addtogroup asn1_module
Paul Bakker9af723c2014-05-01 13:03:14 +020040 * \{
Paul Bakkerefc30292011-11-10 14:43:23 +000041 */
Paul Bakker9af723c2014-05-01 13:03:14 +020042
Paul Bakkerefc30292011-11-10 14:43:23 +000043/**
44 * \name ASN1 Error codes
45 * These error codes are OR'ed to X509 error codes for
Paul Bakker9af723c2014-05-01 13:03:14 +020046 * higher error granularity.
Paul Bakkerefc30292011-11-10 14:43:23 +000047 * ASN1 is a standard to specify data structures.
48 * \{
49 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define MBEDTLS_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */
51#define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */
52#define MBEDTLS_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */
53#define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */
Gilles Peskineaac38532019-09-11 16:52:01 +020054#define MBEDTLS_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020055#define MBEDTLS_ERR_ASN1_ALLOC_FAILED -0x006A /**< Memory allocation failed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
Paul Bakker05888152012-02-16 10:26:57 +000057
Paul Bakkerefc30292011-11-10 14:43:23 +000058/* \} name */
59
60/**
61 * \name DER constants
Andres Amaya Garcia9fb02052017-08-25 17:24:44 +010062 * These constants comply with the DER encoded ASN.1 type tags.
Paul Bakkerefc30292011-11-10 14:43:23 +000063 * DER encoding uses hexadecimal representation.
64 * An example DER sequence is:\n
65 * - 0x02 -- tag indicating INTEGER
66 * - 0x01 -- length in octets
67 * - 0x05 -- value
Gilles Peskine96ddb0a2020-02-26 19:50:01 +010068 * Such sequences are typically read into \c ::mbedtls_x509_buf.
Paul Bakkerefc30292011-11-10 14:43:23 +000069 * \{
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define MBEDTLS_ASN1_BOOLEAN 0x01
72#define MBEDTLS_ASN1_INTEGER 0x02
73#define MBEDTLS_ASN1_BIT_STRING 0x03
74#define MBEDTLS_ASN1_OCTET_STRING 0x04
75#define MBEDTLS_ASN1_NULL 0x05
76#define MBEDTLS_ASN1_OID 0x06
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +020077#define MBEDTLS_ASN1_ENUMERATED 0x0A
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#define MBEDTLS_ASN1_UTF8_STRING 0x0C
79#define MBEDTLS_ASN1_SEQUENCE 0x10
80#define MBEDTLS_ASN1_SET 0x11
81#define MBEDTLS_ASN1_PRINTABLE_STRING 0x13
82#define MBEDTLS_ASN1_T61_STRING 0x14
83#define MBEDTLS_ASN1_IA5_STRING 0x16
84#define MBEDTLS_ASN1_UTC_TIME 0x17
85#define MBEDTLS_ASN1_GENERALIZED_TIME 0x18
86#define MBEDTLS_ASN1_UNIVERSAL_STRING 0x1C
87#define MBEDTLS_ASN1_BMP_STRING 0x1E
88#define MBEDTLS_ASN1_PRIMITIVE 0x00
89#define MBEDTLS_ASN1_CONSTRUCTED 0x20
90#define MBEDTLS_ASN1_CONTEXT_SPECIFIC 0x80
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +010091
Hanno Beckercba71222019-09-11 14:15:10 +010092/* Slightly smaller way to check if tag is a string tag
93 * compared to canonical implementation. */
94#define MBEDTLS_ASN1_IS_STRING_TAG( tag ) \
95 ( ( tag ) < 32u && ( \
96 ( ( 1u << ( tag ) ) & ( ( 1u << MBEDTLS_ASN1_BMP_STRING ) | \
97 ( 1u << MBEDTLS_ASN1_UTF8_STRING ) | \
98 ( 1u << MBEDTLS_ASN1_T61_STRING ) | \
99 ( 1u << MBEDTLS_ASN1_IA5_STRING ) | \
100 ( 1u << MBEDTLS_ASN1_UNIVERSAL_STRING ) | \
101 ( 1u << MBEDTLS_ASN1_PRINTABLE_STRING ) | \
102 ( 1u << MBEDTLS_ASN1_BIT_STRING ) ) ) != 0 ) )
103
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100104/*
105 * Bit masks for each of the components of an ASN.1 tag as specified in
Gilles Peskine1ed45ea2018-03-08 18:16:45 +0100106 * ITU X.690 (08/2015), section 8.1 "General rules for encoding",
107 * paragraph 8.1.2.2:
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100108 *
109 * Bit 8 7 6 5 1
110 * +-------+-----+------------+
111 * | Class | P/C | Tag number |
112 * +-------+-----+------------+
113 */
Andres Amaya Garcia7786abc2017-11-07 20:21:56 +0000114#define MBEDTLS_ASN1_TAG_CLASS_MASK 0xC0
115#define MBEDTLS_ASN1_TAG_PC_MASK 0x20
116#define MBEDTLS_ASN1_TAG_VALUE_MASK 0x1F
Andres Amaya Garcia7512bf72017-08-25 17:12:11 +0100117
Paul Bakkerefc30292011-11-10 14:43:23 +0000118/* \} name */
119/* \} addtogroup asn1_module */
120
121/** Returns the size of the binary string, without the trailing \\0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
Paul Bakkerefc30292011-11-10 14:43:23 +0000123
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100124/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * Compares an mbedtls_asn1_buf structure to a reference OID.
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100126 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100128 * 'unsigned char *oid' here!
Paul Bakkere5eae762013-08-26 12:05:14 +0200129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#define MBEDTLS_OID_CMP(oid_str, oid_buf) \
131 ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \
Manuel Pégourié-Gonnard6e064372015-03-19 16:54:56 +0000132 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200133
Hanno Becker63e38fe2019-09-11 14:16:40 +0100134#define MBEDTLS_OID_CMP_RAW(oid_str, oid_buf, oid_buf_len) \
135 ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf_len) ) || \
136 memcmp( (oid_str), (oid_buf), (oid_buf_len) ) != 0 )
137
Paul Bakkerefc30292011-11-10 14:43:23 +0000138#ifdef __cplusplus
139extern "C" {
140#endif
141
142/**
143 * \name Functions to parse ASN.1 data structures
144 * \{
145 */
146
147/**
148 * Type-length-value structure that allows for ASN1 using DER.
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150typedef struct mbedtls_asn1_buf
Paul Bakkerefc30292011-11-10 14:43:23 +0000151{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200152 int MBEDTLS_PRIVATE(tag); /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
153 size_t MBEDTLS_PRIVATE(len); /**< ASN1 length, in octets. */
154 unsigned char *MBEDTLS_PRIVATE(p); /**< ASN1 data, e.g. in ASCII. */
Paul Bakkerefc30292011-11-10 14:43:23 +0000155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156mbedtls_asn1_buf;
Paul Bakkerefc30292011-11-10 14:43:23 +0000157
158/**
159 * Container for ASN1 bit strings.
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161typedef struct mbedtls_asn1_bitstring
Paul Bakkerefc30292011-11-10 14:43:23 +0000162{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200163 size_t MBEDTLS_PRIVATE(len); /**< ASN1 length, in octets. */
164 unsigned char MBEDTLS_PRIVATE(unused_bits); /**< Number of unused bits at the end of the string */
165 unsigned char *MBEDTLS_PRIVATE(p); /**< Raw ASN1 data for the bit string */
Paul Bakkerefc30292011-11-10 14:43:23 +0000166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167mbedtls_asn1_bitstring;
Paul Bakkerefc30292011-11-10 14:43:23 +0000168
169/**
170 * Container for a sequence of ASN.1 items
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172typedef struct mbedtls_asn1_sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000173{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200174 mbedtls_asn1_buf MBEDTLS_PRIVATE(buf); /**< Buffer containing the given ASN.1 item. */
175 struct mbedtls_asn1_sequence *MBEDTLS_PRIVATE(next); /**< The next entry in the sequence. */
Paul Bakkerefc30292011-11-10 14:43:23 +0000176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177mbedtls_asn1_sequence;
Paul Bakkerefc30292011-11-10 14:43:23 +0000178
179/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200180 * Container for a sequence or list of 'named' ASN.1 data items
181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182typedef struct mbedtls_asn1_named_data
Paul Bakkere5eae762013-08-26 12:05:14 +0200183{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200184 mbedtls_asn1_buf MBEDTLS_PRIVATE(oid); /**< The object identifier. */
185 mbedtls_asn1_buf MBEDTLS_PRIVATE(val); /**< The named value. */
186 struct mbedtls_asn1_named_data *MBEDTLS_PRIVATE(next); /**< The next entry in the sequence. */
187 unsigned char MBEDTLS_PRIVATE(next_merged); /**< Merge next item into the current one? */
Paul Bakkere5eae762013-08-26 12:05:14 +0200188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189mbedtls_asn1_named_data;
Paul Bakkere5eae762013-08-26 12:05:14 +0200190
191/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200192 * \brief Get the length of an ASN.1 element.
193 * Updates the pointer to immediately behind the length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000194 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100195 * \param p On entry, \c *p points to the first byte of the length,
196 * i.e. immediately after the tag.
197 * On successful completion, \c *p points to the first byte
198 * after the length, i.e. the first byte of the content.
199 * On error, the value of \c *p is undefined.
200 * \param end End of data.
Gilles Peskinedc2db482019-09-20 21:06:01 +0200201 * \param len On successful completion, \c *len contains the length
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100202 * read from the ASN.1 input.
Paul Bakkerefc30292011-11-10 14:43:23 +0000203 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100204 * \return 0 if successful.
205 * \return #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
206 * would end beyond \p end.
207 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparseable.
Paul Bakkerefc30292011-11-10 14:43:23 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_asn1_get_len( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100210 const unsigned char *end,
211 size_t *len );
Paul Bakkerefc30292011-11-10 14:43:23 +0000212
213/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100214 * \brief Get the tag and length of the element.
215 * Check for the requested tag.
Paul Bakkercdda0972013-09-09 12:51:29 +0200216 * Updates the pointer to immediately behind the tag and length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000217 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100218 * \param p On entry, \c *p points to the start of the ASN.1 element.
219 * On successful completion, \c *p points to the first byte
220 * after the length, i.e. the first byte of the content.
221 * On error, the value of \c *p is undefined.
222 * \param end End of data.
Gilles Peskinedc2db482019-09-20 21:06:01 +0200223 * \param len On successful completion, \c *len contains the length
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100224 * read from the ASN.1 input.
225 * \param tag The expected tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000226 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100227 * \return 0 if successful.
228 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the data does not start
229 * with the requested tag.
230 * \return #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
231 * would end beyond \p end.
232 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparseable.
Paul Bakkerefc30292011-11-10 14:43:23 +0000233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_asn1_get_tag( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100235 const unsigned char *end,
236 size_t *len, int tag );
Paul Bakkerefc30292011-11-10 14:43:23 +0000237
238/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200239 * \brief Retrieve a boolean ASN.1 tag and its value.
240 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000241 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100242 * \param p On entry, \c *p points to the start of the ASN.1 element.
243 * On successful completion, \c *p points to the first byte
244 * beyond the ASN.1 element.
245 * On error, the value of \c *p is undefined.
246 * \param end End of data.
247 * \param val On success, the parsed value (\c 0 or \c 1).
Paul Bakkerefc30292011-11-10 14:43:23 +0000248 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100249 * \return 0 if successful.
250 * \return An ASN.1 error code if the input does not start with
251 * a valid ASN.1 BOOLEAN.
Paul Bakkerefc30292011-11-10 14:43:23 +0000252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_asn1_get_bool( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100254 const unsigned char *end,
255 int *val );
Paul Bakkerefc30292011-11-10 14:43:23 +0000256
257/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200258 * \brief Retrieve an integer ASN.1 tag and its value.
259 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000260 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100261 * \param p On entry, \c *p points to the start of the ASN.1 element.
262 * On successful completion, \c *p points to the first byte
263 * beyond the ASN.1 element.
264 * On error, the value of \c *p is undefined.
265 * \param end End of data.
266 * \param val On success, the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000267 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100268 * \return 0 if successful.
269 * \return An ASN.1 error code if the input does not start with
270 * a valid ASN.1 INTEGER.
271 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
272 * not fit in an \c int.
Paul Bakkerefc30292011-11-10 14:43:23 +0000273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_asn1_get_int( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100275 const unsigned char *end,
276 int *val );
Paul Bakkerefc30292011-11-10 14:43:23 +0000277
278/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200279 * \brief Retrieve an enumerated ASN.1 tag and its value.
280 * Updates the pointer to immediately behind the full tag.
281 *
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.
288 *
289 * \return 0 if successful.
290 * \return An ASN.1 error code if the input does not start with
291 * a valid ASN.1 ENUMERATED.
292 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
293 * not fit in an \c int.
294 */
295int mbedtls_asn1_get_enum( unsigned char **p,
296 const unsigned char *end,
297 int *val );
298
299/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200300 * \brief Retrieve a bitstring ASN.1 tag and its value.
301 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000302 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100303 * \param p On entry, \c *p points to the start of the ASN.1 element.
304 * On successful completion, \c *p is equal to \p end.
305 * On error, the value of \c *p is undefined.
306 * \param end End of data.
307 * \param bs On success, ::mbedtls_asn1_bitstring information about
308 * the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000309 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100310 * \return 0 if successful.
311 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
312 * extra data after a valid BIT STRING.
313 * \return An ASN.1 error code if the input does not start with
314 * a valid ASN.1 BIT STRING.
Paul Bakkerefc30292011-11-10 14:43:23 +0000315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100317 mbedtls_asn1_bitstring *bs );
Paul Bakkerefc30292011-11-10 14:43:23 +0000318
319/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200320 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
321 * value.
322 * Updates the pointer to the beginning of the bit/octet string.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200323 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100324 * \param p On entry, \c *p points to the start of the ASN.1 element.
325 * On successful completion, \c *p points to the first byte
326 * of the content of the BIT STRING.
327 * On error, the value of \c *p is undefined.
328 * \param end End of data.
329 * \param len On success, \c *len is the length of the content in bytes.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200330 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100331 * \return 0 if successful.
332 * \return #MBEDTLS_ERR_ASN1_INVALID_DATA if the input starts with
333 * a valid BIT STRING with a nonzero number of unused bits.
334 * \return An ASN.1 error code if the input does not start with
335 * a valid ASN.1 BIT STRING.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200336 */
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100337int mbedtls_asn1_get_bitstring_null( unsigned char **p,
338 const unsigned char *end,
339 size_t *len );
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200340
341/**
Gilles Peskinedc2db482019-09-20 21:06:01 +0200342 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>".
343 * Updates the pointer to immediately behind the full sequence tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000344 *
Hanno Becker12ae27d2019-09-11 14:20:09 +0100345 * This function allocates memory for the sequence elements. You can free
346 * the allocated memory with mbedtls_asn1_sequence_free().
347 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100348 * \note On error, this function may return a partial list in \p cur.
349 * You must set `cur->next = NULL` before calling this function!
350 * Otherwise it is impossible to distinguish a previously non-null
351 * pointer from a pointer to an object allocated by this function.
Paul Bakkerefc30292011-11-10 14:43:23 +0000352 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100353 * \note If the sequence is empty, this function does not modify
354 * \c *cur. If the sequence is valid and non-empty, this
355 * function sets `cur->buf.tag` to \p tag. This allows
356 * callers to distinguish between an empty sequence and
357 * a one-element sequence.
358 *
359 * \param p On entry, \c *p points to the start of the ASN.1 element.
360 * On successful completion, \c *p is equal to \p end.
361 * On error, the value of \c *p is undefined.
362 * \param end End of data.
363 * \param cur A ::mbedtls_asn1_sequence which this function fills.
364 * When this function returns, \c *cur is the head of a linked
365 * list. Each node in this list is allocated with
366 * mbedtls_calloc() apart from \p cur itself, and should
367 * therefore be freed with mbedtls_free().
368 * The list describes the content of the sequence.
369 * The head of the list (i.e. \c *cur itself) describes the
370 * first element, `*cur->next` describes the second element, etc.
371 * For each element, `buf.tag == tag`, `buf.len` is the length
372 * of the content of the content of the element, and `buf.p`
373 * points to the first byte of the content (i.e. immediately
374 * past the length of the element).
375 * Note that list elements may be allocated even on error.
376 * \param tag Each element of the sequence must have this tag.
377 *
378 * \return 0 if successful.
379 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
380 * extra data after a valid SEQUENCE OF \p tag.
Gilles Peskineb5c74a52019-10-30 17:07:02 +0100381 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts with
382 * an ASN.1 SEQUENCE in which an element has a tag that
383 * is different from \p tag.
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100384 * \return #MBEDTLS_ERR_ASN1_ALLOC_FAILED if a memory allocation failed.
385 * \return An ASN.1 error code if the input does not start with
Gilles Peskineb5c74a52019-10-30 17:07:02 +0100386 * a valid ASN.1 SEQUENCE.
Paul Bakkerefc30292011-11-10 14:43:23 +0000387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388int mbedtls_asn1_get_sequence_of( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100389 const unsigned char *end,
390 mbedtls_asn1_sequence *cur,
391 int tag );
Hanno Becker12ae27d2019-09-11 14:20:09 +0100392/**
393 * \brief Free a heap-allocated linked list presentation of
394 * an ASN.1 sequence, including the first element.
395 *
396 * There are two common ways to manage the memory used for the representation
397 * of a parsed ASN.1 sequence:
398 * - Allocate a head node `mbedtls_asn1_sequence *head` with mbedtls_calloc().
399 * Pass this node as the `cur` argument to mbedtls_asn1_get_sequence_of().
400 * When you have finished processing the sequence,
401 * call mbedtls_asn1_sequence_free() on `head`.
402 * - Allocate a head node `mbedtls_asn1_sequence *head` in any manner,
403 * for example on the stack. Make sure that `head->next == NULL`.
404 * Pass `head` as the `cur` argument to mbedtls_asn1_get_sequence_of().
405 * When you have finished processing the sequence,
406 * call mbedtls_asn1_sequence_free() on `head->cur`,
407 * then free `head` itself in the appropriate manner.
408 *
409 * \param seq The address of the first sequence component. This may
410 * be \c NULL, in which case this functions returns
411 * immediately.
412 */
413void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq );
Paul Bakkerefc30292011-11-10 14:43:23 +0000414
Hanno Becker199b7092019-09-11 14:21:26 +0100415/**
416 * \brief Traverse an ASN.1 SEQUENCE container and
417 * call a callback for each entry.
418 *
419 * This function checks that the input is a SEQUENCE of elements that
420 * each have a "must" tag, and calls a callback function on the elements
421 * that have a "may" tag.
422 *
423 * For example, to validate that the input is a SEQUENCE of `tag1` and call
424 * `cb` on each element, use
425 * ```
426 * mbedtls_asn1_traverse_sequence_of(&p, end, 0xff, tag1, 0, 0, cb, ctx);
427 * ```
428 *
429 * To validate that the input is a SEQUENCE of ANY and call `cb` on
430 * each element, use
431 * ```
432 * mbedtls_asn1_traverse_sequence_of(&p, end, 0, 0, 0, 0, cb, ctx);
433 * ```
434 *
435 * To validate that the input is a SEQUENCE of CHOICE {NULL, OCTET STRING}
436 * and call `cb` on each element that is an OCTET STRING, use
437 * ```
438 * mbedtls_asn1_traverse_sequence_of(&p, end, 0xfe, 0x04, 0xff, 0x04, cb, ctx);
439 * ```
440 *
441 * The callback is called on the elements with a "may" tag from left to
442 * right. If the input is not a valid SEQUENCE of elements with a "must" tag,
443 * the callback is called on the elements up to the leftmost point where
444 * the input is invalid.
445 *
446 * \warning This function is still experimental and may change
447 * at any time.
448 *
449 * \param p The address of the pointer to the beginning of
450 * the ASN.1 SEQUENCE header. This is updated to
451 * point to the end of the ASN.1 SEQUENCE container
452 * on a successful invocation.
453 * \param end The end of the ASN.1 SEQUENCE container.
454 * \param tag_must_mask A mask to be applied to the ASN.1 tags found within
455 * the SEQUENCE before comparing to \p tag_must_value.
456 * \param tag_must_val The required value of each ASN.1 tag found in the
457 * SEQUENCE, after masking with \p tag_must_mask.
458 * Mismatching tags lead to an error.
459 * For example, a value of \c 0 for both \p tag_must_mask
460 * and \p tag_must_val means that every tag is allowed,
461 * while a value of \c 0xFF for \p tag_must_mask means
462 * that \p tag_must_val is the only allowed tag.
463 * \param tag_may_mask A mask to be applied to the ASN.1 tags found within
464 * the SEQUENCE before comparing to \p tag_may_value.
465 * \param tag_may_val The desired value of each ASN.1 tag found in the
466 * SEQUENCE, after masking with \p tag_may_mask.
467 * Mismatching tags will be silently ignored.
468 * For example, a value of \c 0 for \p tag_may_mask and
469 * \p tag_may_val means that any tag will be considered,
470 * while a value of \c 0xFF for \p tag_may_mask means
471 * that all tags with value different from \p tag_may_val
472 * will be ignored.
473 * \param cb The callback to trigger for each component
474 * in the ASN.1 SEQUENCE that matches \p tag_may_val.
475 * The callback function is called with the following
476 * parameters:
477 * - \p ctx.
478 * - The tag of the current element.
479 * - A pointer to the start of the current element's
480 * content inside the input.
481 * - The length of the content of the current element.
482 * If the callback returns a non-zero value,
483 * the function stops immediately,
484 * forwarding the callback's return value.
485 * \param ctx The context to be passed to the callback \p cb.
486 *
487 * \return \c 0 if successful the entire ASN.1 SEQUENCE
488 * was traversed without parsing or callback errors.
489 * \return #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input
490 * contains extra data after a valid SEQUENCE
491 * of elements with an accepted tag.
492 * \return #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts
493 * with an ASN.1 SEQUENCE in which an element has a tag
494 * that is not accepted.
495 * \return An ASN.1 error code if the input does not start with
496 * a valid ASN.1 SEQUENCE.
497 * \return A non-zero error code forwarded from the callback
498 * \p cb in case the latter returns a non-zero value.
499 */
500int mbedtls_asn1_traverse_sequence_of(
501 unsigned char **p,
502 const unsigned char *end,
Hanno Becker34aada22020-02-03 10:39:55 +0000503 unsigned char tag_must_mask, unsigned char tag_must_val,
504 unsigned char tag_may_mask, unsigned char tag_may_val,
Hanno Becker199b7092019-09-11 14:21:26 +0100505 int (*cb)( void *ctx, int tag,
506 unsigned char* start, size_t len ),
507 void *ctx );
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_BIGNUM_C)
Paul Bakkerefc30292011-11-10 14:43:23 +0000510/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100511 * \brief Retrieve an integer ASN.1 tag and its value.
Paul Bakkercdda0972013-09-09 12:51:29 +0200512 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000513 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100514 * \param p On entry, \c *p points to the start of the ASN.1 element.
515 * On successful completion, \c *p points to the first byte
516 * beyond the ASN.1 element.
517 * On error, the value of \c *p is undefined.
518 * \param end End of data.
519 * \param X On success, the parsed value.
Paul Bakkerefc30292011-11-10 14:43:23 +0000520 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100521 * \return 0 if successful.
522 * \return An ASN.1 error code if the input does not start with
523 * a valid ASN.1 INTEGER.
524 * \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
525 * not fit in an \c int.
526 * \return An MPI error code if the parsed value is too large.
Paul Bakkerefc30292011-11-10 14:43:23 +0000527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_asn1_get_mpi( unsigned char **p,
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100529 const unsigned char *end,
530 mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkerefc30292011-11-10 14:43:23 +0000532
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200533/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200534 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
535 * Updates the pointer to immediately behind the full
536 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200537 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100538 * \param p On entry, \c *p points to the start of the ASN.1 element.
539 * On successful completion, \c *p points to the first byte
540 * beyond the AlgorithmIdentifier element.
541 * On error, the value of \c *p is undefined.
542 * \param end End of data.
543 * \param alg The buffer to receive the OID.
544 * \param params The buffer to receive the parameters.
545 * This is zeroized if there are no parameters.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200546 *
547 * \return 0 if successful or a specific ASN.1 or MPI error code.
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549int mbedtls_asn1_get_alg( unsigned char **p,
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200550 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200552
553/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200554 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
555 * params.
556 * Updates the pointer to immediately behind the full
557 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200558 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100559 * \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.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200565 *
566 * \return 0 if successful or a specific ASN.1 or MPI error code.
567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568int mbedtls_asn1_get_alg_null( unsigned char **p,
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200569 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_asn1_buf *alg );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200571
Paul Bakkere5eae762013-08-26 12:05:14 +0200572/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200573 * \brief Find a specific named_data entry in a sequence or list based on
574 * the OID.
Paul Bakkere5eae762013-08-26 12:05:14 +0200575 *
576 * \param list The list to seek through
577 * \param oid The OID to look for
578 * \param len Size of the OID
579 *
580 * \return NULL if not found, or a pointer to the existing entry.
581 */
Mateusz Starzyk59961cf2021-01-26 13:57:43 +0100582const mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( const mbedtls_asn1_named_data *list,
Paul Bakkere5eae762013-08-26 12:05:14 +0200583 const char *oid, size_t len );
584
585/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 * \brief Free a mbedtls_asn1_named_data entry
Paul Bakkere5eae762013-08-26 12:05:14 +0200587 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100588 * \param entry The named data entry to free.
589 * This function calls mbedtls_free() on
590 * `entry->oid.p` and `entry->val.p`.
Paul Bakkere5eae762013-08-26 12:05:14 +0200591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
Paul Bakkere5eae762013-08-26 12:05:14 +0200593
Paul Bakkerc547cc92013-09-09 12:01:23 +0200594/**
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100595 * \brief Free all entries in a mbedtls_asn1_named_data list.
Paul Bakkerc547cc92013-09-09 12:01:23 +0200596 *
Gilles Peskinebcbe1df2019-03-01 17:57:43 +0100597 * \param head Pointer to the head of the list of named data entries to free.
598 * This function calls mbedtls_asn1_free_named_data() and
599 * mbedtls_free() on each list element and
600 * sets \c *head to \c NULL.
Paul Bakkerc547cc92013-09-09 12:01:23 +0200601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
Paul Bakkerc547cc92013-09-09 12:01:23 +0200603
Paul Bakkerefc30292011-11-10 14:43:23 +0000604#ifdef __cplusplus
605}
606#endif
607
608#endif /* asn1.h */