asn1.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * \file asn1.h
  3. *
  4. * \brief Generic ASN.1 parsing
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_ASN1_H
  26. #define MBEDTLS_ASN1_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. #if defined(MBEDTLS_BIGNUM_C)
  34. #include "bignum.h"
  35. #endif
  36. /**
  37. * \addtogroup asn1_module
  38. * \{
  39. */
  40. /**
  41. * \name ASN1 Error codes
  42. * These error codes are OR'ed to X509 error codes for
  43. * higher error granularity.
  44. * ASN1 is a standard to specify data structures.
  45. * \{
  46. */
  47. #define MBEDTLS_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */
  48. #define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */
  49. #define MBEDTLS_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */
  50. #define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */
  51. #define MBEDTLS_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */
  52. #define MBEDTLS_ERR_ASN1_ALLOC_FAILED -0x006A /**< Memory allocation failed */
  53. #define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
  54. /* \} name */
  55. /**
  56. * \name DER constants
  57. * These constants comply with DER encoded the ANS1 type tags.
  58. * DER encoding uses hexadecimal representation.
  59. * An example DER sequence is:\n
  60. * - 0x02 -- tag indicating INTEGER
  61. * - 0x01 -- length in octets
  62. * - 0x05 -- value
  63. * Such sequences are typically read into \c ::mbedtls_x509_buf.
  64. * \{
  65. */
  66. #define MBEDTLS_ASN1_BOOLEAN 0x01
  67. #define MBEDTLS_ASN1_INTEGER 0x02
  68. #define MBEDTLS_ASN1_BIT_STRING 0x03
  69. #define MBEDTLS_ASN1_OCTET_STRING 0x04
  70. #define MBEDTLS_ASN1_NULL 0x05
  71. #define MBEDTLS_ASN1_OID 0x06
  72. #define MBEDTLS_ASN1_UTF8_STRING 0x0C
  73. #define MBEDTLS_ASN1_SEQUENCE 0x10
  74. #define MBEDTLS_ASN1_SET 0x11
  75. #define MBEDTLS_ASN1_PRINTABLE_STRING 0x13
  76. #define MBEDTLS_ASN1_T61_STRING 0x14
  77. #define MBEDTLS_ASN1_IA5_STRING 0x16
  78. #define MBEDTLS_ASN1_UTC_TIME 0x17
  79. #define MBEDTLS_ASN1_GENERALIZED_TIME 0x18
  80. #define MBEDTLS_ASN1_UNIVERSAL_STRING 0x1C
  81. #define MBEDTLS_ASN1_BMP_STRING 0x1E
  82. #define MBEDTLS_ASN1_PRIMITIVE 0x00
  83. #define MBEDTLS_ASN1_CONSTRUCTED 0x20
  84. #define MBEDTLS_ASN1_CONTEXT_SPECIFIC 0x80
  85. /* \} name */
  86. /* \} addtogroup asn1_module */
  87. /** Returns the size of the binary string, without the trailing \\0 */
  88. #define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
  89. /**
  90. * Compares an mbedtls_asn1_buf structure to a reference OID.
  91. *
  92. * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
  93. * 'unsigned char *oid' here!
  94. */
  95. #define MBEDTLS_OID_CMP(oid_str, oid_buf) \
  96. ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \
  97. memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. /**
  102. * \name Functions to parse ASN.1 data structures
  103. * \{
  104. */
  105. /**
  106. * Type-length-value structure that allows for ASN1 using DER.
  107. */
  108. typedef struct mbedtls_asn1_buf
  109. {
  110. int tag; /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
  111. size_t len; /**< ASN1 length, in octets. */
  112. unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
  113. }
  114. mbedtls_asn1_buf;
  115. /**
  116. * Container for ASN1 bit strings.
  117. */
  118. typedef struct mbedtls_asn1_bitstring
  119. {
  120. size_t len; /**< ASN1 length, in octets. */
  121. unsigned char unused_bits; /**< Number of unused bits at the end of the string */
  122. unsigned char *p; /**< Raw ASN1 data for the bit string */
  123. }
  124. mbedtls_asn1_bitstring;
  125. /**
  126. * Container for a sequence of ASN.1 items
  127. */
  128. typedef struct mbedtls_asn1_sequence
  129. {
  130. mbedtls_asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
  131. struct mbedtls_asn1_sequence *next; /**< The next entry in the sequence. */
  132. }
  133. mbedtls_asn1_sequence;
  134. /**
  135. * Container for a sequence or list of 'named' ASN.1 data items
  136. */
  137. typedef struct mbedtls_asn1_named_data
  138. {
  139. mbedtls_asn1_buf oid; /**< The object identifier. */
  140. mbedtls_asn1_buf val; /**< The named value. */
  141. struct mbedtls_asn1_named_data *next; /**< The next entry in the sequence. */
  142. unsigned char next_merged; /**< Merge next item into the current one? */
  143. }
  144. mbedtls_asn1_named_data;
  145. /**
  146. * \brief Get the length of an ASN.1 element.
  147. * Updates the pointer to immediately behind the length.
  148. *
  149. * \param p The position in the ASN.1 data
  150. * \param end End of data
  151. * \param len The variable that will receive the value
  152. *
  153. * \return 0 if successful, MBEDTLS_ERR_ASN1_OUT_OF_DATA on reaching
  154. * end of data, MBEDTLS_ERR_ASN1_INVALID_LENGTH if length is
  155. * unparseable.
  156. */
  157. int mbedtls_asn1_get_len( unsigned char **p,
  158. const unsigned char *end,
  159. size_t *len );
  160. /**
  161. * \brief Get the tag and length of the tag. Check for the requested tag.
  162. * Updates the pointer to immediately behind the tag and length.
  163. *
  164. * \param p The position in the ASN.1 data
  165. * \param end End of data
  166. * \param len The variable that will receive the length
  167. * \param tag The expected tag
  168. *
  169. * \return 0 if successful, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if tag did
  170. * not match requested tag, or another specific ASN.1 error code.
  171. */
  172. int mbedtls_asn1_get_tag( unsigned char **p,
  173. const unsigned char *end,
  174. size_t *len, int tag );
  175. /**
  176. * \brief Retrieve a boolean ASN.1 tag and its value.
  177. * Updates the pointer to immediately behind the full tag.
  178. *
  179. * \param p The position in the ASN.1 data
  180. * \param end End of data
  181. * \param val The variable that will receive the value
  182. *
  183. * \return 0 if successful or a specific ASN.1 error code.
  184. */
  185. int mbedtls_asn1_get_bool( unsigned char **p,
  186. const unsigned char *end,
  187. int *val );
  188. /**
  189. * \brief Retrieve an integer ASN.1 tag and its value.
  190. * Updates the pointer to immediately behind the full tag.
  191. *
  192. * \param p The position in the ASN.1 data
  193. * \param end End of data
  194. * \param val The variable that will receive the value
  195. *
  196. * \return 0 if successful or a specific ASN.1 error code.
  197. */
  198. int mbedtls_asn1_get_int( unsigned char **p,
  199. const unsigned char *end,
  200. int *val );
  201. /**
  202. * \brief Retrieve a bitstring ASN.1 tag and its value.
  203. * Updates the pointer to immediately behind the full tag.
  204. *
  205. * \param p The position in the ASN.1 data
  206. * \param end End of data
  207. * \param bs The variable that will receive the value
  208. *
  209. * \return 0 if successful or a specific ASN.1 error code.
  210. */
  211. int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
  212. mbedtls_asn1_bitstring *bs);
  213. /**
  214. * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
  215. * value.
  216. * Updates the pointer to the beginning of the bit/octet string.
  217. *
  218. * \param p The position in the ASN.1 data
  219. * \param end End of data
  220. * \param len Length of the actual bit/octect string in bytes
  221. *
  222. * \return 0 if successful or a specific ASN.1 error code.
  223. */
  224. int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
  225. size_t *len );
  226. /**
  227. * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  228. * Updated the pointer to immediately behind the full sequence tag.
  229. *
  230. * \param p The position in the ASN.1 data
  231. * \param end End of data
  232. * \param cur First variable in the chain to fill
  233. * \param tag Type of sequence
  234. *
  235. * \return 0 if successful or a specific ASN.1 error code.
  236. */
  237. int mbedtls_asn1_get_sequence_of( unsigned char **p,
  238. const unsigned char *end,
  239. mbedtls_asn1_sequence *cur,
  240. int tag);
  241. #if defined(MBEDTLS_BIGNUM_C)
  242. /**
  243. * \brief Retrieve a MPI value from an integer ASN.1 tag.
  244. * Updates the pointer to immediately behind the full tag.
  245. *
  246. * \param p The position in the ASN.1 data
  247. * \param end End of data
  248. * \param X The MPI that will receive the value
  249. *
  250. * \return 0 if successful or a specific ASN.1 or MPI error code.
  251. */
  252. int mbedtls_asn1_get_mpi( unsigned char **p,
  253. const unsigned char *end,
  254. mbedtls_mpi *X );
  255. #endif /* MBEDTLS_BIGNUM_C */
  256. /**
  257. * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
  258. * Updates the pointer to immediately behind the full
  259. * AlgorithmIdentifier.
  260. *
  261. * \param p The position in the ASN.1 data
  262. * \param end End of data
  263. * \param alg The buffer to receive the OID
  264. * \param params The buffer to receive the params (if any)
  265. *
  266. * \return 0 if successful or a specific ASN.1 or MPI error code.
  267. */
  268. int mbedtls_asn1_get_alg( unsigned char **p,
  269. const unsigned char *end,
  270. mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
  271. /**
  272. * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
  273. * params.
  274. * Updates the pointer to immediately behind the full
  275. * AlgorithmIdentifier.
  276. *
  277. * \param p The position in the ASN.1 data
  278. * \param end End of data
  279. * \param alg The buffer to receive the OID
  280. *
  281. * \return 0 if successful or a specific ASN.1 or MPI error code.
  282. */
  283. int mbedtls_asn1_get_alg_null( unsigned char **p,
  284. const unsigned char *end,
  285. mbedtls_asn1_buf *alg );
  286. /**
  287. * \brief Find a specific named_data entry in a sequence or list based on
  288. * the OID.
  289. *
  290. * \param list The list to seek through
  291. * \param oid The OID to look for
  292. * \param len Size of the OID
  293. *
  294. * \return NULL if not found, or a pointer to the existing entry.
  295. */
  296. mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
  297. const char *oid, size_t len );
  298. /**
  299. * \brief Free a mbedtls_asn1_named_data entry
  300. *
  301. * \param entry The named data entry to free
  302. */
  303. void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
  304. /**
  305. * \brief Free all entries in a mbedtls_asn1_named_data list
  306. * Head will be set to NULL
  307. *
  308. * \param head Pointer to the head of the list of named data entries to free
  309. */
  310. void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
  311. #ifdef __cplusplus
  312. }
  313. #endif
  314. #endif /* asn1.h */