asn1parse.c 9.0 KB

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