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