asn1parse.c 9.1 KB

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