asn1.h 12 KB

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