2 * Generic ASN.1 parsing
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_ASN1_PARSE_C)
30 #include "mbedtls/asn1.h"
31 #include "mbedtls/platform_util.h"
35 #if defined(MBEDTLS_BIGNUM_C)
36 #include "mbedtls/bignum.h"
39 #if defined(MBEDTLS_PLATFORM_C)
40 #include "mbedtls/platform.h"
43 #define mbedtls_calloc calloc
44 #define mbedtls_free free
48 * ASN.1 DER decoding routines
50 int mbedtls_asn1_get_len( unsigned char **p,
51 const unsigned char *end,
54 if( ( end - *p ) < 1 )
55 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
57 if( ( **p & 0x80 ) == 0 )
64 if( ( end - *p ) < 2 )
65 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
72 if( ( end - *p ) < 3 )
73 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
75 *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
80 if( ( end - *p ) < 4 )
81 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
83 *len = ( (size_t)(*p)[1] << 16 ) |
84 ( (size_t)(*p)[2] << 8 ) | (*p)[3];
89 if( ( end - *p ) < 5 )
90 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
92 *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
93 ( (size_t)(*p)[3] << 8 ) | (*p)[4];
98 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
102 if( *len > (size_t) ( end - *p ) )
103 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
108 int mbedtls_asn1_get_tag( unsigned char **p,
109 const unsigned char *end,
110 size_t *len, int tag )
112 if( ( end - *p ) < 1 )
113 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
116 return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
120 return( mbedtls_asn1_get_len( p, end, len ) );
123 int mbedtls_asn1_get_bool( unsigned char **p,
124 const unsigned char *end,
130 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
134 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
136 *val = ( **p != 0 ) ? 1 : 0;
142 int mbedtls_asn1_get_int( unsigned char **p,
143 const unsigned char *end,
149 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
152 if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 )
153 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
159 *val = ( *val << 8 ) | **p;
166 #if defined(MBEDTLS_BIGNUM_C)
167 int mbedtls_asn1_get_mpi( unsigned char **p,
168 const unsigned char *end,
174 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
177 ret = mbedtls_mpi_read_binary( X, *p, len );
183 #endif /* MBEDTLS_BIGNUM_C */
185 int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
186 mbedtls_asn1_bitstring *bs)
190 /* Certificate type is a single byte bitstring */
191 if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
194 /* Check length, subtract one for actual bit string length */
196 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
199 /* Get number of unused bits, ensure unused bits <= 7 */
200 bs->unused_bits = **p;
201 if( bs->unused_bits > 7 )
202 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
205 /* Get actual bitstring */
210 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
216 * Get a bit string without unused bits
218 int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
223 if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
226 if( (*len)-- < 2 || *(*p)++ != 0 )
227 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
235 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
237 int mbedtls_asn1_get_sequence_of( unsigned char **p,
238 const unsigned char *end,
239 mbedtls_asn1_sequence *cur,
244 mbedtls_asn1_buf *buf;
246 /* Get main sequence tag */
247 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
248 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
251 if( *p + len != end )
252 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
259 if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
265 /* Allocate and assign next pointer */
268 cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
269 sizeof( mbedtls_asn1_sequence ) );
271 if( cur->next == NULL )
272 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
278 /* Set final sequence entry's next pointer to NULL */
282 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
287 int mbedtls_asn1_get_alg( unsigned char **p,
288 const unsigned char *end,
289 mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
294 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
295 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
298 if( ( end - *p ) < 1 )
299 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
304 if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
312 mbedtls_platform_zeroize( params, sizeof(mbedtls_asn1_buf) );
319 if( ( ret = mbedtls_asn1_get_len( p, end, ¶ms->len ) ) != 0 )
326 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
331 int mbedtls_asn1_get_alg_null( unsigned char **p,
332 const unsigned char *end,
333 mbedtls_asn1_buf *alg )
336 mbedtls_asn1_buf params;
338 memset( ¶ms, 0, sizeof(mbedtls_asn1_buf) );
340 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, ¶ms ) ) != 0 )
343 if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
344 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
349 void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
354 mbedtls_free( cur->oid.p );
355 mbedtls_free( cur->val.p );
357 mbedtls_platform_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
360 void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
362 mbedtls_asn1_named_data *cur;
364 while( ( cur = *head ) != NULL )
367 mbedtls_asn1_free_named_data( cur );
372 mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
373 const char *oid, size_t len )
375 while( list != NULL )
377 if( list->oid.len == len &&
378 memcmp( list->oid.p, oid, len ) == 0 )
389 #endif /* MBEDTLS_ASN1_PARSE_C */