asn1parse.c 10.0 KB

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