asn1parse.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Generic ASN.1 parsing
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_ASN1_PARSE_C)
  29. #include "mbedtls/asn1.h"
  30. #include <string.h>
  31. #if defined(MBEDTLS_BIGNUM_C)
  32. #include "mbedtls/bignum.h"
  33. #endif
  34. #if defined(MBEDTLS_PLATFORM_C)
  35. #include "mbedtls/platform.h"
  36. #else
  37. #include <stdlib.h>
  38. #define mbedtls_calloc calloc
  39. #define mbedtls_free free
  40. #endif
  41. /* Implementation that should never be optimized out by the compiler */
  42. static void mbedtls_zeroize( void *v, size_t n ) {
  43. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  44. }
  45. /*
  46. * ASN.1 DER decoding routines
  47. */
  48. int mbedtls_asn1_get_len( unsigned char **p,
  49. const unsigned char *end,
  50. size_t *len )
  51. {
  52. if( ( end - *p ) < 1 )
  53. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  54. if( ( **p & 0x80 ) == 0 )
  55. *len = *(*p)++;
  56. else
  57. {
  58. switch( **p & 0x7F )
  59. {
  60. case 1:
  61. if( ( end - *p ) < 2 )
  62. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  63. *len = (*p)[1];
  64. (*p) += 2;
  65. break;
  66. case 2:
  67. if( ( end - *p ) < 3 )
  68. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  69. *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
  70. (*p) += 3;
  71. break;
  72. case 3:
  73. if( ( end - *p ) < 4 )
  74. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  75. *len = ( (size_t)(*p)[1] << 16 ) |
  76. ( (size_t)(*p)[2] << 8 ) | (*p)[3];
  77. (*p) += 4;
  78. break;
  79. case 4:
  80. if( ( end - *p ) < 5 )
  81. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  82. *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
  83. ( (size_t)(*p)[3] << 8 ) | (*p)[4];
  84. (*p) += 5;
  85. break;
  86. default:
  87. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  88. }
  89. }
  90. if( *len > (size_t) ( end - *p ) )
  91. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  92. return( 0 );
  93. }
  94. int mbedtls_asn1_get_tag( unsigned char **p,
  95. const unsigned char *end,
  96. size_t *len, int tag )
  97. {
  98. if( ( end - *p ) < 1 )
  99. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  100. if( **p != tag )
  101. return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  102. (*p)++;
  103. return( mbedtls_asn1_get_len( p, end, len ) );
  104. }
  105. int mbedtls_asn1_get_bool( unsigned char **p,
  106. const unsigned char *end,
  107. int *val )
  108. {
  109. int ret;
  110. size_t len;
  111. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
  112. return( ret );
  113. if( len != 1 )
  114. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  115. *val = ( **p != 0 ) ? 1 : 0;
  116. (*p)++;
  117. return( 0 );
  118. }
  119. int mbedtls_asn1_get_int( unsigned char **p,
  120. const unsigned char *end,
  121. int *val )
  122. {
  123. int ret;
  124. size_t len;
  125. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  126. return( ret );
  127. if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 )
  128. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  129. *val = 0;
  130. while( len-- > 0 )
  131. {
  132. *val = ( *val << 8 ) | **p;
  133. (*p)++;
  134. }
  135. return( 0 );
  136. }
  137. #if defined(MBEDTLS_BIGNUM_C)
  138. int mbedtls_asn1_get_mpi( unsigned char **p,
  139. const unsigned char *end,
  140. mbedtls_mpi *X )
  141. {
  142. int ret;
  143. size_t len;
  144. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  145. return( ret );
  146. ret = mbedtls_mpi_read_binary( X, *p, len );
  147. *p += len;
  148. return( ret );
  149. }
  150. #endif /* MBEDTLS_BIGNUM_C */
  151. int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
  152. mbedtls_asn1_bitstring *bs)
  153. {
  154. int ret;
  155. /* Certificate type is a single byte bitstring */
  156. if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
  157. return( ret );
  158. /* Check length, subtract one for actual bit string length */
  159. if( bs->len < 1 )
  160. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  161. bs->len -= 1;
  162. /* Get number of unused bits, ensure unused bits <= 7 */
  163. bs->unused_bits = **p;
  164. if( bs->unused_bits > 7 )
  165. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  166. (*p)++;
  167. /* Get actual bitstring */
  168. bs->p = *p;
  169. *p += bs->len;
  170. if( *p != end )
  171. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  172. return( 0 );
  173. }
  174. /*
  175. * Get a bit string without unused bits
  176. */
  177. int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
  178. size_t *len )
  179. {
  180. int ret;
  181. if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
  182. return( ret );
  183. if( (*len)-- < 2 || *(*p)++ != 0 )
  184. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  185. return( 0 );
  186. }
  187. /*
  188. * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  189. */
  190. int mbedtls_asn1_get_sequence_of( unsigned char **p,
  191. const unsigned char *end,
  192. mbedtls_asn1_sequence *cur,
  193. int tag)
  194. {
  195. int ret;
  196. size_t len;
  197. mbedtls_asn1_buf *buf;
  198. /* Get main sequence tag */
  199. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  200. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  201. return( ret );
  202. if( *p + len != end )
  203. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  204. while( *p < end )
  205. {
  206. buf = &(cur->buf);
  207. buf->tag = **p;
  208. if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
  209. return( ret );
  210. buf->p = *p;
  211. *p += buf->len;
  212. /* Allocate and assign next pointer */
  213. if( *p < end )
  214. {
  215. cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
  216. sizeof( mbedtls_asn1_sequence ) );
  217. if( cur->next == NULL )
  218. return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
  219. cur = cur->next;
  220. }
  221. }
  222. /* Set final sequence entry's next pointer to NULL */
  223. cur->next = NULL;
  224. if( *p != end )
  225. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  226. return( 0 );
  227. }
  228. int mbedtls_asn1_get_alg( unsigned char **p,
  229. const unsigned char *end,
  230. mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
  231. {
  232. int ret;
  233. size_t len;
  234. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  235. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  236. return( ret );
  237. if( ( end - *p ) < 1 )
  238. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  239. alg->tag = **p;
  240. end = *p + len;
  241. if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
  242. return( ret );
  243. alg->p = *p;
  244. *p += alg->len;
  245. if( *p == end )
  246. {
  247. mbedtls_zeroize( params, sizeof(mbedtls_asn1_buf) );
  248. return( 0 );
  249. }
  250. params->tag = **p;
  251. (*p)++;
  252. if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
  253. return( ret );
  254. params->p = *p;
  255. *p += params->len;
  256. if( *p != end )
  257. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  258. return( 0 );
  259. }
  260. int mbedtls_asn1_get_alg_null( unsigned char **p,
  261. const unsigned char *end,
  262. mbedtls_asn1_buf *alg )
  263. {
  264. int ret;
  265. mbedtls_asn1_buf params;
  266. memset( &params, 0, sizeof(mbedtls_asn1_buf) );
  267. if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
  268. return( ret );
  269. if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
  270. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  271. return( 0 );
  272. }
  273. void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
  274. {
  275. if( cur == NULL )
  276. return;
  277. mbedtls_free( cur->oid.p );
  278. mbedtls_free( cur->val.p );
  279. mbedtls_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
  280. }
  281. void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
  282. {
  283. mbedtls_asn1_named_data *cur;
  284. while( ( cur = *head ) != NULL )
  285. {
  286. *head = cur->next;
  287. mbedtls_asn1_free_named_data( cur );
  288. mbedtls_free( cur );
  289. }
  290. }
  291. mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
  292. const char *oid, size_t len )
  293. {
  294. while( list != NULL )
  295. {
  296. if( list->oid.len == len &&
  297. memcmp( list->oid.p, oid, len ) == 0 )
  298. {
  299. break;
  300. }
  301. list = list->next;
  302. }
  303. return( list );
  304. }
  305. #endif /* MBEDTLS_ASN1_PARSE_C */