asn1parse.c 9.2 KB

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