2 * Generic ASN.1 parsing
4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
6 * This file is part of mbed TLS (https://tls.mbed.org)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #if !defined(POLARSSL_CONFIG_FILE)
24 #include "polarssl/config.h"
26 #include POLARSSL_CONFIG_FILE
29 #if defined(POLARSSL_ASN1_PARSE_C)
31 #include "polarssl/asn1.h"
35 #if defined(POLARSSL_BIGNUM_C)
36 #include "polarssl/bignum.h"
39 #if defined(POLARSSL_PLATFORM_C)
40 #include "polarssl/platform.h"
43 #define polarssl_malloc malloc
44 #define polarssl_free free
47 /* Implementation that should never be optimized out by the compiler */
48 static void polarssl_zeroize( void *v
, size_t n
) {
49 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
53 * ASN.1 DER decoding routines
55 int asn1_get_len( unsigned char **p
,
56 const unsigned char *end
,
59 if( ( end
- *p
) < 1 )
60 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
62 if( ( **p
& 0x80 ) == 0 )
69 if( ( end
- *p
) < 2 )
70 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
77 if( ( end
- *p
) < 3 )
78 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
80 *len
= ( (size_t)(*p
)[1] << 8 ) | (*p
)[2];
85 if( ( end
- *p
) < 4 )
86 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
88 *len
= ( (size_t)(*p
)[1] << 16 ) |
89 ( (size_t)(*p
)[2] << 8 ) | (*p
)[3];
94 if( ( end
- *p
) < 5 )
95 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
97 *len
= ( (size_t)(*p
)[1] << 24 ) | ( (size_t)(*p
)[2] << 16 ) |
98 ( (size_t)(*p
)[3] << 8 ) | (*p
)[4];
103 return( POLARSSL_ERR_ASN1_INVALID_LENGTH
);
107 if( *len
> (size_t) ( end
- *p
) )
108 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
113 int asn1_get_tag( unsigned char **p
,
114 const unsigned char *end
,
115 size_t *len
, int tag
)
117 if( ( end
- *p
) < 1 )
118 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
121 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG
);
125 return( asn1_get_len( p
, end
, len
) );
128 int asn1_get_bool( unsigned char **p
,
129 const unsigned char *end
,
135 if( ( ret
= asn1_get_tag( p
, end
, &len
, ASN1_BOOLEAN
) ) != 0 )
139 return( POLARSSL_ERR_ASN1_INVALID_LENGTH
);
141 *val
= ( **p
!= 0 ) ? 1 : 0;
147 int asn1_get_int( unsigned char **p
,
148 const unsigned char *end
,
154 if( ( ret
= asn1_get_tag( p
, end
, &len
, ASN1_INTEGER
) ) != 0 )
157 if( len
> sizeof( int ) || ( **p
& 0x80 ) != 0 )
158 return( POLARSSL_ERR_ASN1_INVALID_LENGTH
);
164 *val
= ( *val
<< 8 ) | **p
;
171 #if defined(POLARSSL_BIGNUM_C)
172 int asn1_get_mpi( unsigned char **p
,
173 const unsigned char *end
,
179 if( ( ret
= asn1_get_tag( p
, end
, &len
, ASN1_INTEGER
) ) != 0 )
182 ret
= mpi_read_binary( X
, *p
, len
);
188 #endif /* POLARSSL_BIGNUM_C */
190 int asn1_get_bitstring( unsigned char **p
, const unsigned char *end
,
195 /* Certificate type is a single byte bitstring */
196 if( ( ret
= asn1_get_tag( p
, end
, &bs
->len
, ASN1_BIT_STRING
) ) != 0 )
199 /* Check length, subtract one for actual bit string length */
201 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
204 /* Get number of unused bits, ensure unused bits <= 7 */
205 bs
->unused_bits
= **p
;
206 if( bs
->unused_bits
> 7 )
207 return( POLARSSL_ERR_ASN1_INVALID_LENGTH
);
210 /* Get actual bitstring */
215 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
221 * Get a bit string without unused bits
223 int asn1_get_bitstring_null( unsigned char **p
, const unsigned char *end
,
228 if( ( ret
= asn1_get_tag( p
, end
, len
, ASN1_BIT_STRING
) ) != 0 )
231 if( (*len
)-- < 2 || *(*p
)++ != 0 )
232 return( POLARSSL_ERR_ASN1_INVALID_DATA
);
240 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
242 int asn1_get_sequence_of( unsigned char **p
,
243 const unsigned char *end
,
251 /* Get main sequence tag */
252 if( ( ret
= asn1_get_tag( p
, end
, &len
,
253 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
256 if( *p
+ len
!= end
)
257 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
264 if( ( ret
= asn1_get_tag( p
, end
, &buf
->len
, tag
) ) != 0 )
270 /* Allocate and assign next pointer */
273 cur
->next
= polarssl_malloc( sizeof( asn1_sequence
) );
275 if( cur
->next
== NULL
)
276 return( POLARSSL_ERR_ASN1_MALLOC_FAILED
);
278 memset( cur
->next
, 0, sizeof( asn1_sequence
) );
284 /* Set final sequence entry's next pointer to NULL */
288 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
293 int asn1_get_alg( unsigned char **p
,
294 const unsigned char *end
,
295 asn1_buf
*alg
, asn1_buf
*params
)
300 if( ( ret
= asn1_get_tag( p
, end
, &len
,
301 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
304 if( ( end
- *p
) < 1 )
305 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
310 if( ( ret
= asn1_get_tag( p
, end
, &alg
->len
, ASN1_OID
) ) != 0 )
318 polarssl_zeroize( params
, sizeof(asn1_buf
) );
325 if( ( ret
= asn1_get_len( p
, end
, ¶ms
->len
) ) != 0 )
332 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
337 int asn1_get_alg_null( unsigned char **p
,
338 const unsigned char *end
,
344 memset( ¶ms
, 0, sizeof(asn1_buf
) );
346 if( ( ret
= asn1_get_alg( p
, end
, alg
, ¶ms
) ) != 0 )
349 if( ( params
.tag
!= ASN1_NULL
&& params
.tag
!= 0 ) || params
.len
!= 0 )
350 return( POLARSSL_ERR_ASN1_INVALID_DATA
);
355 void asn1_free_named_data( asn1_named_data
*cur
)
360 polarssl_free( cur
->oid
.p
);
361 polarssl_free( cur
->val
.p
);
363 polarssl_zeroize( cur
, sizeof( asn1_named_data
) );
366 void asn1_free_named_data_list( asn1_named_data
**head
)
368 asn1_named_data
*cur
;
370 while( ( cur
= *head
) != NULL
)
373 asn1_free_named_data( cur
);
374 polarssl_free( cur
);
378 asn1_named_data
*asn1_find_named_data( asn1_named_data
*list
,
379 const char *oid
, size_t len
)
381 while( list
!= NULL
)
383 if( list
->oid
.len
== len
&&
384 memcmp( list
->oid
.p
, oid
, len
) == 0 )
395 #endif /* POLARSSL_ASN1_PARSE_C */