asn1.h 27 KB

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