asn1parse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Generic ASN.1 parsing
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_ASN1_PARSE_C)
  21. #include "mbedtls/asn1.h"
  22. #include "mbedtls/platform_util.h"
  23. #include "mbedtls/error.h"
  24. #include <string.h>
  25. #if defined(MBEDTLS_BIGNUM_C)
  26. #include "mbedtls/bignum.h"
  27. #endif
  28. #if defined(MBEDTLS_PLATFORM_C)
  29. #include "mbedtls/platform.h"
  30. #else
  31. #include <stdlib.h>
  32. #define mbedtls_calloc calloc
  33. #define mbedtls_free free
  34. #endif
  35. /*
  36. * ASN.1 DER decoding routines
  37. */
  38. int mbedtls_asn1_get_len( unsigned char **p,
  39. const unsigned char *end,
  40. size_t *len )
  41. {
  42. if( ( end - *p ) < 1 )
  43. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  44. if( ( **p & 0x80 ) == 0 )
  45. *len = *(*p)++;
  46. else
  47. {
  48. switch( **p & 0x7F )
  49. {
  50. case 1:
  51. if( ( end - *p ) < 2 )
  52. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  53. *len = (*p)[1];
  54. (*p) += 2;
  55. break;
  56. case 2:
  57. if( ( end - *p ) < 3 )
  58. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  59. *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
  60. (*p) += 3;
  61. break;
  62. case 3:
  63. if( ( end - *p ) < 4 )
  64. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  65. *len = ( (size_t)(*p)[1] << 16 ) |
  66. ( (size_t)(*p)[2] << 8 ) | (*p)[3];
  67. (*p) += 4;
  68. break;
  69. case 4:
  70. if( ( end - *p ) < 5 )
  71. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  72. *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
  73. ( (size_t)(*p)[3] << 8 ) | (*p)[4];
  74. (*p) += 5;
  75. break;
  76. default:
  77. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  78. }
  79. }
  80. if( *len > (size_t) ( end - *p ) )
  81. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  82. return( 0 );
  83. }
  84. int mbedtls_asn1_get_tag( unsigned char **p,
  85. const unsigned char *end,
  86. size_t *len, int tag )
  87. {
  88. if( ( end - *p ) < 1 )
  89. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  90. if( **p != tag )
  91. return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  92. (*p)++;
  93. return( mbedtls_asn1_get_len( p, end, len ) );
  94. }
  95. int mbedtls_asn1_get_bool( unsigned char **p,
  96. const unsigned char *end,
  97. int *val )
  98. {
  99. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  100. size_t len;
  101. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
  102. return( ret );
  103. if( len != 1 )
  104. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  105. *val = ( **p != 0 ) ? 1 : 0;
  106. (*p)++;
  107. return( 0 );
  108. }
  109. static int asn1_get_tagged_int( unsigned char **p,
  110. const unsigned char *end,
  111. int tag, int *val )
  112. {
  113. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  114. size_t len;
  115. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, tag ) ) != 0 )
  116. return( ret );
  117. /*
  118. * len==0 is malformed (0 must be represented as 020100 for INTEGER,
  119. * or 0A0100 for ENUMERATED tags
  120. */
  121. if( len == 0 )
  122. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  123. /* This is a cryptography library. Reject negative integers. */
  124. if( ( **p & 0x80 ) != 0 )
  125. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  126. /* Skip leading zeros. */
  127. while( len > 0 && **p == 0 )
  128. {
  129. ++( *p );
  130. --len;
  131. }
  132. /* Reject integers that don't fit in an int. This code assumes that
  133. * the int type has no padding bit. */
  134. if( len > sizeof( int ) )
  135. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  136. if( len == sizeof( int ) && ( **p & 0x80 ) != 0 )
  137. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  138. *val = 0;
  139. while( len-- > 0 )
  140. {
  141. *val = ( *val << 8 ) | **p;
  142. (*p)++;
  143. }
  144. return( 0 );
  145. }
  146. int mbedtls_asn1_get_int( unsigned char **p,
  147. const unsigned char *end,
  148. int *val )
  149. {
  150. return( asn1_get_tagged_int( p, end, MBEDTLS_ASN1_INTEGER, val) );
  151. }
  152. int mbedtls_asn1_get_enum( unsigned char **p,
  153. const unsigned char *end,
  154. int *val )
  155. {
  156. return( asn1_get_tagged_int( p, end, MBEDTLS_ASN1_ENUMERATED, val) );
  157. }
  158. #if defined(MBEDTLS_BIGNUM_C)
  159. int mbedtls_asn1_get_mpi( unsigned char **p,
  160. const unsigned char *end,
  161. mbedtls_mpi *X )
  162. {
  163. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  164. size_t len;
  165. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  166. return( ret );
  167. ret = mbedtls_mpi_read_binary( X, *p, len );
  168. *p += len;
  169. return( ret );
  170. }
  171. #endif /* MBEDTLS_BIGNUM_C */
  172. int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
  173. mbedtls_asn1_bitstring *bs)
  174. {
  175. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  176. /* Certificate type is a single byte bitstring */
  177. if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
  178. return( ret );
  179. /* Check length, subtract one for actual bit string length */
  180. if( bs->len < 1 )
  181. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  182. bs->len -= 1;
  183. /* Get number of unused bits, ensure unused bits <= 7 */
  184. bs->unused_bits = **p;
  185. if( bs->unused_bits > 7 )
  186. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  187. (*p)++;
  188. /* Get actual bitstring */
  189. bs->p = *p;
  190. *p += bs->len;
  191. if( *p != end )
  192. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  193. return( 0 );
  194. }
  195. /*
  196. * Traverse an ASN.1 "SEQUENCE OF <tag>"
  197. * and call a callback for each entry found.
  198. */
  199. int mbedtls_asn1_traverse_sequence_of(
  200. unsigned char **p,
  201. const unsigned char *end,
  202. unsigned char tag_must_mask, unsigned char tag_must_val,
  203. unsigned char tag_may_mask, unsigned char tag_may_val,
  204. int (*cb)( void *ctx, int tag,
  205. unsigned char *start, size_t len ),
  206. void *ctx )
  207. {
  208. int ret;
  209. size_t len;
  210. /* Get main sequence tag */
  211. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  212. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  213. {
  214. return( ret );
  215. }
  216. if( *p + len != end )
  217. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  218. while( *p < end )
  219. {
  220. unsigned char const tag = *(*p)++;
  221. if( ( tag & tag_must_mask ) != tag_must_val )
  222. return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  223. if( ( ret = mbedtls_asn1_get_len( p, end, &len ) ) != 0 )
  224. return( ret );
  225. if( ( tag & tag_may_mask ) == tag_may_val )
  226. {
  227. if( cb != NULL )
  228. {
  229. ret = cb( ctx, tag, *p, len );
  230. if( ret != 0 )
  231. return( ret );
  232. }
  233. }
  234. *p += len;
  235. }
  236. return( 0 );
  237. }
  238. /*
  239. * Get a bit string without unused bits
  240. */
  241. int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
  242. size_t *len )
  243. {
  244. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  245. if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
  246. return( ret );
  247. if( *len == 0 )
  248. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  249. --( *len );
  250. if( **p != 0 )
  251. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  252. ++( *p );
  253. return( 0 );
  254. }
  255. void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq )
  256. {
  257. while( seq != NULL )
  258. {
  259. mbedtls_asn1_sequence *next = seq->next;
  260. mbedtls_platform_zeroize( seq, sizeof( *seq ) );
  261. mbedtls_free( seq );
  262. seq = next;
  263. }
  264. }
  265. typedef struct
  266. {
  267. int tag;
  268. mbedtls_asn1_sequence *cur;
  269. } asn1_get_sequence_of_cb_ctx_t;
  270. static int asn1_get_sequence_of_cb( void *ctx,
  271. int tag,
  272. unsigned char *start,
  273. size_t len )
  274. {
  275. asn1_get_sequence_of_cb_ctx_t *cb_ctx =
  276. (asn1_get_sequence_of_cb_ctx_t *) ctx;
  277. mbedtls_asn1_sequence *cur =
  278. cb_ctx->cur;
  279. if( cur->buf.p != NULL )
  280. {
  281. cur->next =
  282. mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
  283. if( cur->next == NULL )
  284. return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
  285. cur = cur->next;
  286. }
  287. cur->buf.p = start;
  288. cur->buf.len = len;
  289. cur->buf.tag = tag;
  290. cb_ctx->cur = cur;
  291. return( 0 );
  292. }
  293. /*
  294. * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  295. */
  296. int mbedtls_asn1_get_sequence_of( unsigned char **p,
  297. const unsigned char *end,
  298. mbedtls_asn1_sequence *cur,
  299. int tag)
  300. {
  301. asn1_get_sequence_of_cb_ctx_t cb_ctx = { tag, cur };
  302. memset( cur, 0, sizeof( mbedtls_asn1_sequence ) );
  303. return( mbedtls_asn1_traverse_sequence_of(
  304. p, end, 0xFF, tag, 0, 0,
  305. asn1_get_sequence_of_cb, &cb_ctx ) );
  306. }
  307. int mbedtls_asn1_get_alg( unsigned char **p,
  308. const unsigned char *end,
  309. mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
  310. {
  311. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  312. size_t len;
  313. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  314. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  315. return( ret );
  316. if( ( end - *p ) < 1 )
  317. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  318. alg->tag = **p;
  319. end = *p + len;
  320. if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
  321. return( ret );
  322. alg->p = *p;
  323. *p += alg->len;
  324. if( *p == end )
  325. {
  326. mbedtls_platform_zeroize( params, sizeof(mbedtls_asn1_buf) );
  327. return( 0 );
  328. }
  329. params->tag = **p;
  330. (*p)++;
  331. if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
  332. return( ret );
  333. params->p = *p;
  334. *p += params->len;
  335. if( *p != end )
  336. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  337. return( 0 );
  338. }
  339. int mbedtls_asn1_get_alg_null( unsigned char **p,
  340. const unsigned char *end,
  341. mbedtls_asn1_buf *alg )
  342. {
  343. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  344. mbedtls_asn1_buf params;
  345. memset( &params, 0, sizeof(mbedtls_asn1_buf) );
  346. if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
  347. return( ret );
  348. if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
  349. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  350. return( 0 );
  351. }
  352. void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
  353. {
  354. if( cur == NULL )
  355. return;
  356. mbedtls_free( cur->oid.p );
  357. mbedtls_free( cur->val.p );
  358. mbedtls_platform_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
  359. }
  360. void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
  361. {
  362. mbedtls_asn1_named_data *cur;
  363. while( ( cur = *head ) != NULL )
  364. {
  365. *head = cur->next;
  366. mbedtls_asn1_free_named_data( cur );
  367. mbedtls_free( cur );
  368. }
  369. }
  370. mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
  371. const char *oid, size_t len )
  372. {
  373. while( list != NULL )
  374. {
  375. if( list->oid.len == len &&
  376. memcmp( list->oid.p, oid, len ) == 0 )
  377. {
  378. break;
  379. }
  380. list = list->next;
  381. }
  382. return( list );
  383. }
  384. #endif /* MBEDTLS_ASN1_PARSE_C */