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