blob: 636ab605c0af953aab2c05ee5f315c97eff37e0c [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/*
2 * Generic ASN.1 parsing
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerefc30292011-11-10 14:43:23 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerefc30292011-11-10 14:43:23 +00007 *
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.
12 *
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.
17 *
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.
21 */
22
23#include "polarssl/config.h"
24
25#if defined(POLARSSL_ASN1_PARSE_C)
26
27#include "polarssl/asn1.h"
28
29#if defined(POLARSSL_BIGNUM_C)
30#include "polarssl/bignum.h"
31#endif
32
33#include <string.h>
34#include <stdlib.h>
35#include <time.h>
36
37/*
38 * ASN.1 DER decoding routines
39 */
40int asn1_get_len( unsigned char **p,
41 const unsigned char *end,
42 size_t *len )
43{
44 if( ( end - *p ) < 1 )
45 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
46
47 if( ( **p & 0x80 ) == 0 )
48 *len = *(*p)++;
49 else
50 {
51 switch( **p & 0x7F )
52 {
53 case 1:
54 if( ( end - *p ) < 2 )
55 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
56
57 *len = (*p)[1];
58 (*p) += 2;
59 break;
60
61 case 2:
62 if( ( end - *p ) < 3 )
63 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
64
Manuel Pégourié-Gonnard64f65e82015-04-10 17:23:05 +020065 *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
Paul Bakkerefc30292011-11-10 14:43:23 +000066 (*p) += 3;
67 break;
68
69 case 3:
70 if( ( end - *p ) < 4 )
71 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
72
Manuel Pégourié-Gonnard64f65e82015-04-10 17:23:05 +020073 *len = ( (size_t)(*p)[1] << 16 ) |
74 ( (size_t)(*p)[2] << 8 ) | (*p)[3];
Paul Bakkerefc30292011-11-10 14:43:23 +000075 (*p) += 4;
76 break;
77
78 case 4:
79 if( ( end - *p ) < 5 )
80 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
81
Manuel Pégourié-Gonnard64f65e82015-04-10 17:23:05 +020082 *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
83 ( (size_t)(*p)[3] << 8 ) | (*p)[4];
Paul Bakkerefc30292011-11-10 14:43:23 +000084 (*p) += 5;
85 break;
86
87 default:
88 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
89 }
90 }
91
92 if( *len > (size_t) ( end - *p ) )
93 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
94
95 return( 0 );
96}
97
98int asn1_get_tag( unsigned char **p,
99 const unsigned char *end,
100 size_t *len, int tag )
101{
102 if( ( end - *p ) < 1 )
103 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
104
105 if( **p != tag )
106 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
107
108 (*p)++;
109
110 return( asn1_get_len( p, end, len ) );
111}
112
113int asn1_get_bool( unsigned char **p,
114 const unsigned char *end,
115 int *val )
116{
117 int ret;
118 size_t len;
119
120 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
121 return( ret );
122
123 if( len != 1 )
124 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
125
126 *val = ( **p != 0 ) ? 1 : 0;
127 (*p)++;
128
129 return( 0 );
130}
131
132int asn1_get_int( unsigned char **p,
133 const unsigned char *end,
134 int *val )
135{
136 int ret;
137 size_t len;
138
139 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
140 return( ret );
141
142 if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
143 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
144
145 *val = 0;
146
147 while( len-- > 0 )
148 {
149 *val = ( *val << 8 ) | **p;
150 (*p)++;
151 }
152
153 return( 0 );
154}
155
156#if defined(POLARSSL_BIGNUM_C)
157int asn1_get_mpi( unsigned char **p,
158 const unsigned char *end,
159 mpi *X )
160{
161 int ret;
162 size_t len;
163
164 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
165 return( ret );
166
167 ret = mpi_read_binary( X, *p, len );
168
169 *p += len;
170
171 return( ret );
172}
173#endif /* POLARSSL_BIGNUM_C */
174
175int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
176 asn1_bitstring *bs)
177{
178 int ret;
179
180 /* Certificate type is a single byte bitstring */
181 if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
182 return( ret );
183
184 /* Check length, subtract one for actual bit string length */
185 if ( bs->len < 1 )
186 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
187 bs->len -= 1;
188
189 /* Get number of unused bits, ensure unused bits <= 7 */
190 bs->unused_bits = **p;
191 if( bs->unused_bits > 7 )
192 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
193 (*p)++;
194
195 /* Get actual bitstring */
196 bs->p = *p;
197 *p += bs->len;
198
199 if( *p != end )
200 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
201
202 return 0;
203}
204
205
206/*
207 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
208 */
209int asn1_get_sequence_of( unsigned char **p,
210 const unsigned char *end,
211 asn1_sequence *cur,
212 int tag)
213{
214 int ret;
215 size_t len;
216 asn1_buf *buf;
217
218 /* Get main sequence tag */
219 if( ( ret = asn1_get_tag( p, end, &len,
220 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
221 return( ret );
222
223 if( *p + len != end )
224 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
225
226 while( *p < end )
227 {
228 buf = &(cur->buf);
229 buf->tag = **p;
230
231 if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
232 return( ret );
233
234 buf->p = *p;
235 *p += buf->len;
236
237 /* Allocate and assign next pointer */
238 if (*p < end)
239 {
240 cur->next = (asn1_sequence *) malloc(
241 sizeof( asn1_sequence ) );
242
243 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000244 return( POLARSSL_ERR_ASN1_MALLOC_FAILED );
Paul Bakkerefc30292011-11-10 14:43:23 +0000245
Manuel Pégourié-Gonnardd3ae4302014-11-11 22:17:26 +0100246 memset( cur->next, 0, sizeof( asn1_sequence ) );
247
Paul Bakkerefc30292011-11-10 14:43:23 +0000248 cur = cur->next;
249 }
250 }
251
252 /* Set final sequence entry's next pointer to NULL */
253 cur->next = NULL;
254
255 if( *p != end )
256 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
257
258 return( 0 );
259}
260
261#endif