asn1.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /* \} name */
  85. /* \} addtogroup asn1_module */
  86. /** Returns the size of the binary string, without the trailing \\0 */
  87. #define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
  88. /**
  89. * Compares an mbedtls_asn1_buf structure to a reference OID.
  90. *
  91. * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
  92. * 'unsigned char *oid' here!
  93. */
  94. #define MBEDTLS_OID_CMP(oid_str, oid_buf) \
  95. ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \
  96. memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
  97. #ifdef __cplusplus
  98. extern "C" {
  99. #endif
  100. /**
  101. * \name Functions to parse ASN.1 data structures
  102. * \{
  103. */
  104. /**
  105. * Type-length-value structure that allows for ASN1 using DER.
  106. */
  107. typedef struct mbedtls_asn1_buf
  108. {
  109. int tag; /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
  110. size_t len; /**< ASN1 length, in octets. */
  111. unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
  112. }
  113. mbedtls_asn1_buf;
  114. /**
  115. * Container for ASN1 bit strings.
  116. */
  117. typedef struct mbedtls_asn1_bitstring
  118. {
  119. size_t len; /**< ASN1 length, in octets. */
  120. unsigned char unused_bits; /**< Number of unused bits at the end of the string */
  121. unsigned char *p; /**< Raw ASN1 data for the bit string */
  122. }
  123. mbedtls_asn1_bitstring;
  124. /**
  125. * Container for a sequence of ASN.1 items
  126. */
  127. typedef struct mbedtls_asn1_sequence
  128. {
  129. mbedtls_asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
  130. struct mbedtls_asn1_sequence *next; /**< The next entry in the sequence. */
  131. }
  132. mbedtls_asn1_sequence;
  133. /**
  134. * Container for a sequence or list of 'named' ASN.1 data items
  135. */
  136. typedef struct mbedtls_asn1_named_data
  137. {
  138. mbedtls_asn1_buf oid; /**< The object identifier. */
  139. mbedtls_asn1_buf val; /**< The named value. */
  140. struct mbedtls_asn1_named_data *next; /**< The next entry in the sequence. */
  141. unsigned char next_merged; /**< Merge next item into the current one? */
  142. }
  143. mbedtls_asn1_named_data;
  144. /**
  145. * \brief Get the length of an ASN.1 element.
  146. * Updates the pointer to immediately behind the length.
  147. *
  148. * \param p The position in the ASN.1 data
  149. * \param end End of data
  150. * \param len The variable that will receive the value
  151. *
  152. * \return 0 if successful, MBEDTLS_ERR_ASN1_OUT_OF_DATA on reaching
  153. * end of data, MBEDTLS_ERR_ASN1_INVALID_LENGTH if length is
  154. * unparseable.
  155. */
  156. int mbedtls_asn1_get_len( unsigned char **p,
  157. const unsigned char *end,
  158. size_t *len );
  159. /**
  160. * \brief Get the tag and length of the tag. Check for the requested tag.
  161. * Updates the pointer to immediately behind the tag and length.
  162. *
  163. * \param p The position in the ASN.1 data
  164. * \param end End of data
  165. * \param len The variable that will receive the length
  166. * \param tag The expected tag
  167. *
  168. * \return 0 if successful, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if tag did
  169. * not match requested tag, or another specific ASN.1 error code.
  170. */
  171. int mbedtls_asn1_get_tag( unsigned char **p,
  172. const unsigned char *end,
  173. size_t *len, int tag );
  174. /**
  175. * \brief Retrieve a boolean ASN.1 tag and its value.
  176. * Updates the pointer to immediately behind the full tag.
  177. *
  178. * \param p The position in the ASN.1 data
  179. * \param end End of data
  180. * \param val The variable that will receive the value
  181. *
  182. * \return 0 if successful or a specific ASN.1 error code.
  183. */
  184. int mbedtls_asn1_get_bool( unsigned char **p,
  185. const unsigned char *end,
  186. int *val );
  187. /**
  188. * \brief Retrieve an integer 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_int( unsigned char **p,
  198. const unsigned char *end,
  199. int *val );
  200. /**
  201. * \brief Retrieve a bitstring 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 bs 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_bitstring( unsigned char **p, const unsigned char *end,
  211. mbedtls_asn1_bitstring *bs);
  212. /**
  213. * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
  214. * value.
  215. * Updates the pointer to the beginning of the bit/octet string.
  216. *
  217. * \param p The position in the ASN.1 data
  218. * \param end End of data
  219. * \param len Length of the actual bit/octect string in bytes
  220. *
  221. * \return 0 if successful or a specific ASN.1 error code.
  222. */
  223. int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
  224. size_t *len );
  225. /**
  226. * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  227. * Updated the pointer to immediately behind the full sequence tag.
  228. *
  229. * \param p The position in the ASN.1 data
  230. * \param end End of data
  231. * \param cur First variable in the chain to fill
  232. * \param tag Type of sequence
  233. *
  234. * \return 0 if successful or a specific ASN.1 error code.
  235. */
  236. int mbedtls_asn1_get_sequence_of( unsigned char **p,
  237. const unsigned char *end,
  238. mbedtls_asn1_sequence *cur,
  239. int tag);
  240. #if defined(MBEDTLS_BIGNUM_C)
  241. /**
  242. * \brief Retrieve a MPI value from an integer ASN.1 tag.
  243. * Updates the pointer to immediately behind the full tag.
  244. *
  245. * \param p The position in the ASN.1 data
  246. * \param end End of data
  247. * \param X The MPI that will receive the value
  248. *
  249. * \return 0 if successful or a specific ASN.1 or MPI error code.
  250. */
  251. int mbedtls_asn1_get_mpi( unsigned char **p,
  252. const unsigned char *end,
  253. mbedtls_mpi *X );
  254. #endif /* MBEDTLS_BIGNUM_C */
  255. /**
  256. * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
  257. * Updates the pointer to immediately behind the full
  258. * AlgorithmIdentifier.
  259. *
  260. * \param p The position in the ASN.1 data
  261. * \param end End of data
  262. * \param alg The buffer to receive the OID
  263. * \param params The buffer to receive the params (if any)
  264. *
  265. * \return 0 if successful or a specific ASN.1 or MPI error code.
  266. */
  267. int mbedtls_asn1_get_alg( unsigned char **p,
  268. const unsigned char *end,
  269. mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
  270. /**
  271. * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
  272. * params.
  273. * Updates the pointer to immediately behind the full
  274. * AlgorithmIdentifier.
  275. *
  276. * \param p The position in the ASN.1 data
  277. * \param end End of data
  278. * \param alg The buffer to receive the OID
  279. *
  280. * \return 0 if successful or a specific ASN.1 or MPI error code.
  281. */
  282. int mbedtls_asn1_get_alg_null( unsigned char **p,
  283. const unsigned char *end,
  284. mbedtls_asn1_buf *alg );
  285. /**
  286. * \brief Find a specific named_data entry in a sequence or list based on
  287. * the OID.
  288. *
  289. * \param list The list to seek through
  290. * \param oid The OID to look for
  291. * \param len Size of the OID
  292. *
  293. * \return NULL if not found, or a pointer to the existing entry.
  294. */
  295. mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
  296. const char *oid, size_t len );
  297. /**
  298. * \brief Free a mbedtls_asn1_named_data entry
  299. *
  300. * \param entry The named data entry to free
  301. */
  302. void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
  303. /**
  304. * \brief Free all entries in a mbedtls_asn1_named_data list
  305. * Head will be set to NULL
  306. *
  307. * \param head Pointer to the head of the list of named data entries to free
  308. */
  309. void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* asn1.h */