Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:29

0001 /**
0002  * \file asn1.h
0003  *
0004  * \brief Generic ASN.1 parsing
0005  */
0006 /*
0007  *  Copyright The Mbed TLS Contributors
0008  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0009  */
0010 #ifndef MBEDTLS_ASN1_H
0011 #define MBEDTLS_ASN1_H
0012 #include "mbedtls/private_access.h"
0013 
0014 #include "mbedtls/build_info.h"
0015 #include "mbedtls/platform_util.h"
0016 
0017 #include <stddef.h>
0018 
0019 #if defined(MBEDTLS_BIGNUM_C)
0020 #include "mbedtls/bignum.h"
0021 #endif
0022 
0023 /**
0024  * \addtogroup asn1_module
0025  * \{
0026  */
0027 
0028 /**
0029  * \name ASN1 Error codes
0030  * These error codes are combined with other error codes for
0031  * higher error granularity.
0032  * e.g. X.509 and PKCS #7 error codes
0033  * ASN1 is a standard to specify data structures.
0034  * \{
0035  */
0036 /** Out of data when parsing an ASN1 data structure. */
0037 #define MBEDTLS_ERR_ASN1_OUT_OF_DATA                      -0x0060
0038 /** ASN1 tag was of an unexpected value. */
0039 #define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG                   -0x0062
0040 /** Error when trying to determine the length or invalid length. */
0041 #define MBEDTLS_ERR_ASN1_INVALID_LENGTH                   -0x0064
0042 /** Actual length differs from expected length. */
0043 #define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH                  -0x0066
0044 /** Data is invalid. */
0045 #define MBEDTLS_ERR_ASN1_INVALID_DATA                     -0x0068
0046 /** Memory allocation failed */
0047 #define MBEDTLS_ERR_ASN1_ALLOC_FAILED                     -0x006A
0048 /** Buffer too small when writing ASN.1 data structure. */
0049 #define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL                    -0x006C
0050 
0051 /** \} name ASN1 Error codes */
0052 
0053 /**
0054  * \name DER constants
0055  * These constants comply with the DER encoded ASN.1 type tags.
0056  * DER encoding uses hexadecimal representation.
0057  * An example DER sequence is:\n
0058  * - 0x02 -- tag indicating INTEGER
0059  * - 0x01 -- length in octets
0060  * - 0x05 -- value
0061  * Such sequences are typically read into \c ::mbedtls_x509_buf.
0062  * \{
0063  */
0064 #define MBEDTLS_ASN1_BOOLEAN                 0x01
0065 #define MBEDTLS_ASN1_INTEGER                 0x02
0066 #define MBEDTLS_ASN1_BIT_STRING              0x03
0067 #define MBEDTLS_ASN1_OCTET_STRING            0x04
0068 #define MBEDTLS_ASN1_NULL                    0x05
0069 #define MBEDTLS_ASN1_OID                     0x06
0070 #define MBEDTLS_ASN1_ENUMERATED              0x0A
0071 #define MBEDTLS_ASN1_UTF8_STRING             0x0C
0072 #define MBEDTLS_ASN1_SEQUENCE                0x10
0073 #define MBEDTLS_ASN1_SET                     0x11
0074 #define MBEDTLS_ASN1_PRINTABLE_STRING        0x13
0075 #define MBEDTLS_ASN1_T61_STRING              0x14
0076 #define MBEDTLS_ASN1_IA5_STRING              0x16
0077 #define MBEDTLS_ASN1_UTC_TIME                0x17
0078 #define MBEDTLS_ASN1_GENERALIZED_TIME        0x18
0079 #define MBEDTLS_ASN1_UNIVERSAL_STRING        0x1C
0080 #define MBEDTLS_ASN1_BMP_STRING              0x1E
0081 #define MBEDTLS_ASN1_PRIMITIVE               0x00
0082 #define MBEDTLS_ASN1_CONSTRUCTED             0x20
0083 #define MBEDTLS_ASN1_CONTEXT_SPECIFIC        0x80
0084 
0085 /* Slightly smaller way to check if tag is a string tag
0086  * compared to canonical implementation. */
0087 #define MBEDTLS_ASN1_IS_STRING_TAG(tag)                                \
0088     ((unsigned int) (tag) < 32u && (                                   \
0089          ((1u << (tag)) & ((1u << MBEDTLS_ASN1_BMP_STRING)       |     \
0090                            (1u << MBEDTLS_ASN1_UTF8_STRING)      |     \
0091                            (1u << MBEDTLS_ASN1_T61_STRING)       |     \
0092                            (1u << MBEDTLS_ASN1_IA5_STRING)       |     \
0093                            (1u << MBEDTLS_ASN1_UNIVERSAL_STRING) |     \
0094                            (1u << MBEDTLS_ASN1_PRINTABLE_STRING))) != 0))
0095 
0096 /*
0097  * Bit masks for each of the components of an ASN.1 tag as specified in
0098  * ITU X.690 (08/2015), section 8.1 "General rules for encoding",
0099  * paragraph 8.1.2.2:
0100  *
0101  * Bit  8     7   6   5          1
0102  *     +-------+-----+------------+
0103  *     | Class | P/C | Tag number |
0104  *     +-------+-----+------------+
0105  */
0106 #define MBEDTLS_ASN1_TAG_CLASS_MASK          0xC0
0107 #define MBEDTLS_ASN1_TAG_PC_MASK             0x20
0108 #define MBEDTLS_ASN1_TAG_VALUE_MASK          0x1F
0109 
0110 /** \} name DER constants */
0111 
0112 /** Returns the size of the binary string, without the trailing \\0 */
0113 #define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
0114 
0115 /**
0116  * Compares an mbedtls_asn1_buf structure to a reference OID.
0117  *
0118  * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
0119  * 'unsigned char *oid' here!
0120  */
0121 #define MBEDTLS_OID_CMP(oid_str, oid_buf)                                   \
0122     ((MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len) ||                \
0123      memcmp((oid_str), (oid_buf)->p, (oid_buf)->len) != 0)
0124 
0125 #define MBEDTLS_OID_CMP_RAW(oid_str, oid_buf, oid_buf_len)              \
0126     ((MBEDTLS_OID_SIZE(oid_str) != (oid_buf_len)) ||             \
0127      memcmp((oid_str), (oid_buf), (oid_buf_len)) != 0)
0128 
0129 #ifdef __cplusplus
0130 extern "C" {
0131 #endif
0132 
0133 /**
0134  * \name Functions to parse ASN.1 data structures
0135  * \{
0136  */
0137 
0138 /**
0139  * Type-length-value structure that allows for ASN1 using DER.
0140  */
0141 typedef struct mbedtls_asn1_buf {
0142     int tag;                /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
0143     size_t len;             /**< ASN1 length, in octets. */
0144     unsigned char *p;       /**< ASN1 data, e.g. in ASCII. */
0145 }
0146 mbedtls_asn1_buf;
0147 
0148 /**
0149  * Container for ASN1 bit strings.
0150  */
0151 typedef struct mbedtls_asn1_bitstring {
0152     size_t len;                 /**< ASN1 length, in octets. */
0153     unsigned char unused_bits;  /**< Number of unused bits at the end of the string */
0154     unsigned char *p;           /**< Raw ASN1 data for the bit string */
0155 }
0156 mbedtls_asn1_bitstring;
0157 
0158 /**
0159  * Container for a sequence of ASN.1 items
0160  */
0161 typedef struct mbedtls_asn1_sequence {
0162     mbedtls_asn1_buf buf;                   /**< Buffer containing the given ASN.1 item. */
0163 
0164     /** The next entry in the sequence.
0165      *
0166      * The details of memory management for sequences are not documented and
0167      * may change in future versions. Set this field to \p NULL when
0168      * initializing a structure, and do not modify it except via Mbed TLS
0169      * library functions.
0170      */
0171     struct mbedtls_asn1_sequence *next;
0172 }
0173 mbedtls_asn1_sequence;
0174 
0175 /**
0176  * Container for a sequence or list of 'named' ASN.1 data items
0177  */
0178 typedef struct mbedtls_asn1_named_data {
0179     mbedtls_asn1_buf oid;                   /**< The object identifier. */
0180     mbedtls_asn1_buf val;                   /**< The named value. */
0181 
0182     /** The next entry in the sequence.
0183      *
0184      * The details of memory management for named data sequences are not
0185      * documented and may change in future versions. Set this field to \p NULL
0186      * when initializing a structure, and do not modify it except via Mbed TLS
0187      * library functions.
0188      */
0189     struct mbedtls_asn1_named_data *next;
0190 
0191     /** Merge next item into the current one?
0192      *
0193      * This field exists for the sake of Mbed TLS's X.509 certificate parsing
0194      * code and may change in future versions of the library.
0195      */
0196     unsigned char MBEDTLS_PRIVATE(next_merged);
0197 }
0198 mbedtls_asn1_named_data;
0199 
0200 #if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_X509_CREATE_C) || \
0201     defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
0202 /**
0203  * \brief       Get the length of an ASN.1 element.
0204  *              Updates the pointer to immediately behind the length.
0205  *
0206  * \param p     On entry, \c *p points to the first byte of the length,
0207  *              i.e. immediately after the tag.
0208  *              On successful completion, \c *p points to the first byte
0209  *              after the length, i.e. the first byte of the content.
0210  *              On error, the value of \c *p is undefined.
0211  * \param end   End of data.
0212  * \param len   On successful completion, \c *len contains the length
0213  *              read from the ASN.1 input.
0214  *
0215  * \return      0 if successful.
0216  * \return      #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
0217  *              would end beyond \p end.
0218  * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparsable.
0219  */
0220 int mbedtls_asn1_get_len(unsigned char **p,
0221                          const unsigned char *end,
0222                          size_t *len);
0223 
0224 /**
0225  * \brief       Get the tag and length of the element.
0226  *              Check for the requested tag.
0227  *              Updates the pointer to immediately behind the tag and length.
0228  *
0229  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0230  *              On successful completion, \c *p points to the first byte
0231  *              after the length, i.e. the first byte of the content.
0232  *              On error, the value of \c *p is undefined.
0233  * \param end   End of data.
0234  * \param len   On successful completion, \c *len contains the length
0235  *              read from the ASN.1 input.
0236  * \param tag   The expected tag.
0237  *
0238  * \return      0 if successful.
0239  * \return      #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the data does not start
0240  *              with the requested tag.
0241  * \return      #MBEDTLS_ERR_ASN1_OUT_OF_DATA if the ASN.1 element
0242  *              would end beyond \p end.
0243  * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the length is unparsable.
0244  */
0245 int mbedtls_asn1_get_tag(unsigned char **p,
0246                          const unsigned char *end,
0247                          size_t *len, int tag);
0248 #endif /* MBEDTLS_ASN1_PARSE_C || MBEDTLS_X509_CREATE_C || MBEDTLS_PSA_UTIL_HAVE_ECDSA */
0249 
0250 #if defined(MBEDTLS_ASN1_PARSE_C)
0251 /**
0252  * \brief       Retrieve a boolean ASN.1 tag and its value.
0253  *              Updates the pointer to immediately behind the full tag.
0254  *
0255  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0256  *              On successful completion, \c *p points to the first byte
0257  *              beyond the ASN.1 element.
0258  *              On error, the value of \c *p is undefined.
0259  * \param end   End of data.
0260  * \param val   On success, the parsed value (\c 0 or \c 1).
0261  *
0262  * \return      0 if successful.
0263  * \return      An ASN.1 error code if the input does not start with
0264  *              a valid ASN.1 BOOLEAN.
0265  */
0266 int mbedtls_asn1_get_bool(unsigned char **p,
0267                           const unsigned char *end,
0268                           int *val);
0269 
0270 /**
0271  * \brief       Retrieve an integer ASN.1 tag and its value.
0272  *              Updates the pointer to immediately behind the full tag.
0273  *
0274  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0275  *              On successful completion, \c *p points to the first byte
0276  *              beyond the ASN.1 element.
0277  *              On error, the value of \c *p is undefined.
0278  * \param end   End of data.
0279  * \param val   On success, the parsed value.
0280  *
0281  * \return      0 if successful.
0282  * \return      An ASN.1 error code if the input does not start with
0283  *              a valid ASN.1 INTEGER.
0284  * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
0285  *              not fit in an \c int.
0286  */
0287 int mbedtls_asn1_get_int(unsigned char **p,
0288                          const unsigned char *end,
0289                          int *val);
0290 
0291 /**
0292  * \brief       Retrieve an enumerated ASN.1 tag and its value.
0293  *              Updates the pointer to immediately behind the full tag.
0294  *
0295  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0296  *              On successful completion, \c *p points to the first byte
0297  *              beyond the ASN.1 element.
0298  *              On error, the value of \c *p is undefined.
0299  * \param end   End of data.
0300  * \param val   On success, the parsed value.
0301  *
0302  * \return      0 if successful.
0303  * \return      An ASN.1 error code if the input does not start with
0304  *              a valid ASN.1 ENUMERATED.
0305  * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
0306  *              not fit in an \c int.
0307  */
0308 int mbedtls_asn1_get_enum(unsigned char **p,
0309                           const unsigned char *end,
0310                           int *val);
0311 
0312 /**
0313  * \brief       Retrieve a bitstring ASN.1 tag and its value.
0314  *              Updates the pointer to immediately behind the full tag.
0315  *
0316  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0317  *              On successful completion, \c *p is equal to \p end.
0318  *              On error, the value of \c *p is undefined.
0319  * \param end   End of data.
0320  * \param bs    On success, ::mbedtls_asn1_bitstring information about
0321  *              the parsed value.
0322  *
0323  * \return      0 if successful.
0324  * \return      #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
0325  *              extra data after a valid BIT STRING.
0326  * \return      An ASN.1 error code if the input does not start with
0327  *              a valid ASN.1 BIT STRING.
0328  */
0329 int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end,
0330                                mbedtls_asn1_bitstring *bs);
0331 
0332 /**
0333  * \brief       Retrieve a bitstring ASN.1 tag without unused bits and its
0334  *              value.
0335  *              Updates the pointer to the beginning of the bit/octet string.
0336  *
0337  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0338  *              On successful completion, \c *p points to the first byte
0339  *              of the content of the BIT STRING.
0340  *              On error, the value of \c *p is undefined.
0341  * \param end   End of data.
0342  * \param len   On success, \c *len is the length of the content in bytes.
0343  *
0344  * \return      0 if successful.
0345  * \return      #MBEDTLS_ERR_ASN1_INVALID_DATA if the input starts with
0346  *              a valid BIT STRING with a nonzero number of unused bits.
0347  * \return      An ASN.1 error code if the input does not start with
0348  *              a valid ASN.1 BIT STRING.
0349  */
0350 int mbedtls_asn1_get_bitstring_null(unsigned char **p,
0351                                     const unsigned char *end,
0352                                     size_t *len);
0353 
0354 /**
0355  * \brief       Parses and splits an ASN.1 "SEQUENCE OF <tag>".
0356  *              Updates the pointer to immediately behind the full sequence tag.
0357  *
0358  * This function allocates memory for the sequence elements. You can free
0359  * the allocated memory with mbedtls_asn1_sequence_free().
0360  *
0361  * \note        On error, this function may return a partial list in \p cur.
0362  *              You must set `cur->next = NULL` before calling this function!
0363  *              Otherwise it is impossible to distinguish a previously non-null
0364  *              pointer from a pointer to an object allocated by this function.
0365  *
0366  * \note        If the sequence is empty, this function does not modify
0367  *              \c *cur. If the sequence is valid and non-empty, this
0368  *              function sets `cur->buf.tag` to \p tag. This allows
0369  *              callers to distinguish between an empty sequence and
0370  *              a one-element sequence.
0371  *
0372  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0373  *              On successful completion, \c *p is equal to \p end.
0374  *              On error, the value of \c *p is undefined.
0375  * \param end   End of data.
0376  * \param cur   A ::mbedtls_asn1_sequence which this function fills.
0377  *              When this function returns, \c *cur is the head of a linked
0378  *              list. Each node in this list is allocated with
0379  *              mbedtls_calloc() apart from \p cur itself, and should
0380  *              therefore be freed with mbedtls_free().
0381  *              The list describes the content of the sequence.
0382  *              The head of the list (i.e. \c *cur itself) describes the
0383  *              first element, `*cur->next` describes the second element, etc.
0384  *              For each element, `buf.tag == tag`, `buf.len` is the length
0385  *              of the content of the content of the element, and `buf.p`
0386  *              points to the first byte of the content (i.e. immediately
0387  *              past the length of the element).
0388  *              Note that list elements may be allocated even on error.
0389  * \param tag   Each element of the sequence must have this tag.
0390  *
0391  * \return      0 if successful.
0392  * \return      #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input contains
0393  *              extra data after a valid SEQUENCE OF \p tag.
0394  * \return      #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts with
0395  *              an ASN.1 SEQUENCE in which an element has a tag that
0396  *              is different from \p tag.
0397  * \return      #MBEDTLS_ERR_ASN1_ALLOC_FAILED if a memory allocation failed.
0398  * \return      An ASN.1 error code if the input does not start with
0399  *              a valid ASN.1 SEQUENCE.
0400  */
0401 int mbedtls_asn1_get_sequence_of(unsigned char **p,
0402                                  const unsigned char *end,
0403                                  mbedtls_asn1_sequence *cur,
0404                                  int tag);
0405 /**
0406  * \brief          Free a heap-allocated linked list presentation of
0407  *                 an ASN.1 sequence, including the first element.
0408  *
0409  * There are two common ways to manage the memory used for the representation
0410  * of a parsed ASN.1 sequence:
0411  * - Allocate a head node `mbedtls_asn1_sequence *head` with mbedtls_calloc().
0412  *   Pass this node as the `cur` argument to mbedtls_asn1_get_sequence_of().
0413  *   When you have finished processing the sequence,
0414  *   call mbedtls_asn1_sequence_free() on `head`.
0415  * - Allocate a head node `mbedtls_asn1_sequence *head` in any manner,
0416  *   for example on the stack. Make sure that `head->next == NULL`.
0417  *   Pass `head` as the `cur` argument to mbedtls_asn1_get_sequence_of().
0418  *   When you have finished processing the sequence,
0419  *   call mbedtls_asn1_sequence_free() on `head->cur`,
0420  *   then free `head` itself in the appropriate manner.
0421  *
0422  * \param seq      The address of the first sequence component. This may
0423  *                 be \c NULL, in which case this functions returns
0424  *                 immediately.
0425  */
0426 void mbedtls_asn1_sequence_free(mbedtls_asn1_sequence *seq);
0427 
0428 /**
0429  * \brief                Traverse an ASN.1 SEQUENCE container and
0430  *                       call a callback for each entry.
0431  *
0432  * This function checks that the input is a SEQUENCE of elements that
0433  * each have a "must" tag, and calls a callback function on the elements
0434  * that have a "may" tag.
0435  *
0436  * For example, to validate that the input is a SEQUENCE of `tag1` and call
0437  * `cb` on each element, use
0438  * ```
0439  * mbedtls_asn1_traverse_sequence_of(&p, end, 0xff, tag1, 0, 0, cb, ctx);
0440  * ```
0441  *
0442  * To validate that the input is a SEQUENCE of ANY and call `cb` on
0443  * each element, use
0444  * ```
0445  * mbedtls_asn1_traverse_sequence_of(&p, end, 0, 0, 0, 0, cb, ctx);
0446  * ```
0447  *
0448  * To validate that the input is a SEQUENCE of CHOICE {NULL, OCTET STRING}
0449  * and call `cb` on each element that is an OCTET STRING, use
0450  * ```
0451  * mbedtls_asn1_traverse_sequence_of(&p, end, 0xfe, 0x04, 0xff, 0x04, cb, ctx);
0452  * ```
0453  *
0454  * The callback is called on the elements with a "may" tag from left to
0455  * right. If the input is not a valid SEQUENCE of elements with a "must" tag,
0456  * the callback is called on the elements up to the leftmost point where
0457  * the input is invalid.
0458  *
0459  * \warning              This function is still experimental and may change
0460  *                       at any time.
0461  *
0462  * \param p              The address of the pointer to the beginning of
0463  *                       the ASN.1 SEQUENCE header. This is updated to
0464  *                       point to the end of the ASN.1 SEQUENCE container
0465  *                       on a successful invocation.
0466  * \param end            The end of the ASN.1 SEQUENCE container.
0467  * \param tag_must_mask  A mask to be applied to the ASN.1 tags found within
0468  *                       the SEQUENCE before comparing to \p tag_must_val.
0469  * \param tag_must_val   The required value of each ASN.1 tag found in the
0470  *                       SEQUENCE, after masking with \p tag_must_mask.
0471  *                       Mismatching tags lead to an error.
0472  *                       For example, a value of \c 0 for both \p tag_must_mask
0473  *                       and \p tag_must_val means that every tag is allowed,
0474  *                       while a value of \c 0xFF for \p tag_must_mask means
0475  *                       that \p tag_must_val is the only allowed tag.
0476  * \param tag_may_mask   A mask to be applied to the ASN.1 tags found within
0477  *                       the SEQUENCE before comparing to \p tag_may_val.
0478  * \param tag_may_val    The desired value of each ASN.1 tag found in the
0479  *                       SEQUENCE, after masking with \p tag_may_mask.
0480  *                       Mismatching tags will be silently ignored.
0481  *                       For example, a value of \c 0 for \p tag_may_mask and
0482  *                       \p tag_may_val means that any tag will be considered,
0483  *                       while a value of \c 0xFF for \p tag_may_mask means
0484  *                       that all tags with value different from \p tag_may_val
0485  *                       will be ignored.
0486  * \param cb             The callback to trigger for each component
0487  *                       in the ASN.1 SEQUENCE that matches \p tag_may_val.
0488  *                       The callback function is called with the following
0489  *                       parameters:
0490  *                       - \p ctx.
0491  *                       - The tag of the current element.
0492  *                       - A pointer to the start of the current element's
0493  *                         content inside the input.
0494  *                       - The length of the content of the current element.
0495  *                       If the callback returns a non-zero value,
0496  *                       the function stops immediately,
0497  *                       forwarding the callback's return value.
0498  * \param ctx            The context to be passed to the callback \p cb.
0499  *
0500  * \return               \c 0 if successful the entire ASN.1 SEQUENCE
0501  *                       was traversed without parsing or callback errors.
0502  * \return               #MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the input
0503  *                       contains extra data after a valid SEQUENCE
0504  *                       of elements with an accepted tag.
0505  * \return               #MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the input starts
0506  *                       with an ASN.1 SEQUENCE in which an element has a tag
0507  *                       that is not accepted.
0508  * \return               An ASN.1 error code if the input does not start with
0509  *                       a valid ASN.1 SEQUENCE.
0510  * \return               A non-zero error code forwarded from the callback
0511  *                       \p cb in case the latter returns a non-zero value.
0512  */
0513 int mbedtls_asn1_traverse_sequence_of(
0514     unsigned char **p,
0515     const unsigned char *end,
0516     unsigned char tag_must_mask, unsigned char tag_must_val,
0517     unsigned char tag_may_mask, unsigned char tag_may_val,
0518     int (*cb)(void *ctx, int tag,
0519               unsigned char *start, size_t len),
0520     void *ctx);
0521 
0522 #if defined(MBEDTLS_BIGNUM_C)
0523 /**
0524  * \brief       Retrieve an integer ASN.1 tag and its value.
0525  *              Updates the pointer to immediately behind the full tag.
0526  *
0527  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0528  *              On successful completion, \c *p points to the first byte
0529  *              beyond the ASN.1 element.
0530  *              On error, the value of \c *p is undefined.
0531  * \param end   End of data.
0532  * \param X     On success, the parsed value.
0533  *
0534  * \return      0 if successful.
0535  * \return      An ASN.1 error code if the input does not start with
0536  *              a valid ASN.1 INTEGER.
0537  * \return      #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
0538  *              not fit in an \c int.
0539  * \return      An MPI error code if the parsed value is too large.
0540  */
0541 int mbedtls_asn1_get_mpi(unsigned char **p,
0542                          const unsigned char *end,
0543                          mbedtls_mpi *X);
0544 #endif /* MBEDTLS_BIGNUM_C */
0545 
0546 /**
0547  * \brief       Retrieve an AlgorithmIdentifier ASN.1 sequence.
0548  *              Updates the pointer to immediately behind the full
0549  *              AlgorithmIdentifier.
0550  *
0551  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0552  *              On successful completion, \c *p points to the first byte
0553  *              beyond the AlgorithmIdentifier element.
0554  *              On error, the value of \c *p is undefined.
0555  * \param end   End of data.
0556  * \param alg   The buffer to receive the OID.
0557  * \param params The buffer to receive the parameters.
0558  *              This is zeroized if there are no parameters.
0559  *
0560  * \return      0 if successful or a specific ASN.1 or MPI error code.
0561  */
0562 int mbedtls_asn1_get_alg(unsigned char **p,
0563                          const unsigned char *end,
0564                          mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params);
0565 
0566 /**
0567  * \brief       Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
0568  *              params.
0569  *              Updates the pointer to immediately behind the full
0570  *              AlgorithmIdentifier.
0571  *
0572  * \param p     On entry, \c *p points to the start of the ASN.1 element.
0573  *              On successful completion, \c *p points to the first byte
0574  *              beyond the AlgorithmIdentifier element.
0575  *              On error, the value of \c *p is undefined.
0576  * \param end   End of data.
0577  * \param alg   The buffer to receive the OID.
0578  *
0579  * \return      0 if successful or a specific ASN.1 or MPI error code.
0580  */
0581 int mbedtls_asn1_get_alg_null(unsigned char **p,
0582                               const unsigned char *end,
0583                               mbedtls_asn1_buf *alg);
0584 
0585 /**
0586  * \brief       Find a specific named_data entry in a sequence or list based on
0587  *              the OID.
0588  *
0589  * \param list  The list to seek through
0590  * \param oid   The OID to look for
0591  * \param len   Size of the OID
0592  *
0593  * \return      NULL if not found, or a pointer to the existing entry.
0594  */
0595 const mbedtls_asn1_named_data *mbedtls_asn1_find_named_data(const mbedtls_asn1_named_data *list,
0596                                                             const char *oid, size_t len);
0597 
0598 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
0599 /**
0600  * \brief       Free a mbedtls_asn1_named_data entry
0601  *
0602  * \deprecated  This function is deprecated and will be removed in a
0603  *              future version of the library.
0604  *              Please use mbedtls_asn1_free_named_data_list()
0605  *              or mbedtls_asn1_free_named_data_list_shallow().
0606  *
0607  * \param entry The named data entry to free.
0608  *              This function calls mbedtls_free() on
0609  *              `entry->oid.p` and `entry->val.p`.
0610  */
0611 void MBEDTLS_DEPRECATED mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *entry);
0612 #endif /* MBEDTLS_DEPRECATED_REMOVED */
0613 
0614 /**
0615  * \brief       Free all entries in a mbedtls_asn1_named_data list.
0616  *
0617  * \param head  Pointer to the head of the list of named data entries to free.
0618  *              This function calls mbedtls_free() on
0619  *              `entry->oid.p` and `entry->val.p` and then on `entry`
0620  *              for each list entry, and sets \c *head to \c NULL.
0621  */
0622 void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head);
0623 
0624 /**
0625  * \brief       Free all shallow entries in a mbedtls_asn1_named_data list,
0626  *              but do not free internal pointer targets.
0627  *
0628  * \param name  Head of the list of named data entries to free.
0629  *              This function calls mbedtls_free() on each list element.
0630  */
0631 void mbedtls_asn1_free_named_data_list_shallow(mbedtls_asn1_named_data *name);
0632 
0633 /** \} name Functions to parse ASN.1 data structures */
0634 /** \} addtogroup asn1_module */
0635 
0636 #endif /* MBEDTLS_ASN1_PARSE_C */
0637 
0638 #ifdef __cplusplus
0639 }
0640 #endif
0641 
0642 #endif /* asn1.h */