asn1.h 11 KB

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