blob: f183c3d3078c0f6bc9924f5a6dce4f1f2af15ca8 [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/*
2 * Generic ASN.1 parsing
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerefc30292011-11-10 14:43:23 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.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
65 *len = ( (*p)[1] << 8 ) | (*p)[2];
66 (*p) += 3;
67 break;
68
69 case 3:
70 if( ( end - *p ) < 4 )
71 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
72
73 *len = ( (*p)[1] << 16 ) | ( (*p)[2] << 8 ) | (*p)[3];
74 (*p) += 4;
75 break;
76
77 case 4:
78 if( ( end - *p ) < 5 )
79 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
80
81 *len = ( (*p)[1] << 24 ) | ( (*p)[2] << 16 ) | ( (*p)[3] << 8 ) | (*p)[4];
82 (*p) += 5;
83 break;
84
85 default:
86 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
87 }
88 }
89
90 if( *len > (size_t) ( end - *p ) )
91 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
92
93 return( 0 );
94}
95
96int asn1_get_tag( unsigned char **p,
97 const unsigned char *end,
98 size_t *len, int tag )
99{
100 if( ( end - *p ) < 1 )
101 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
102
103 if( **p != tag )
104 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
105
106 (*p)++;
107
108 return( asn1_get_len( p, end, len ) );
109}
110
111int asn1_get_bool( unsigned char **p,
112 const unsigned char *end,
113 int *val )
114{
115 int ret;
116 size_t len;
117
118 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
119 return( ret );
120
121 if( len != 1 )
122 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
123
124 *val = ( **p != 0 ) ? 1 : 0;
125 (*p)++;
126
127 return( 0 );
128}
129
130int asn1_get_int( unsigned char **p,
131 const unsigned char *end,
132 int *val )
133{
134 int ret;
135 size_t len;
136
137 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
138 return( ret );
139
140 if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
141 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
142
143 *val = 0;
144
145 while( len-- > 0 )
146 {
147 *val = ( *val << 8 ) | **p;
148 (*p)++;
149 }
150
151 return( 0 );
152}
153
154#if defined(POLARSSL_BIGNUM_C)
155int asn1_get_mpi( unsigned char **p,
156 const unsigned char *end,
157 mpi *X )
158{
159 int ret;
160 size_t len;
161
162 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
163 return( ret );
164
165 ret = mpi_read_binary( X, *p, len );
166
167 *p += len;
168
169 return( ret );
170}
171#endif /* POLARSSL_BIGNUM_C */
172
173int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
174 asn1_bitstring *bs)
175{
176 int ret;
177
178 /* Certificate type is a single byte bitstring */
179 if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
180 return( ret );
181
182 /* Check length, subtract one for actual bit string length */
183 if ( bs->len < 1 )
184 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
185 bs->len -= 1;
186
187 /* Get number of unused bits, ensure unused bits <= 7 */
188 bs->unused_bits = **p;
189 if( bs->unused_bits > 7 )
190 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
191 (*p)++;
192
193 /* Get actual bitstring */
194 bs->p = *p;
195 *p += bs->len;
196
197 if( *p != end )
198 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
199
200 return 0;
201}
202
203
204/*
205 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
206 */
207int asn1_get_sequence_of( unsigned char **p,
208 const unsigned char *end,
209 asn1_sequence *cur,
210 int tag)
211{
212 int ret;
213 size_t len;
214 asn1_buf *buf;
215
216 /* Get main sequence tag */
217 if( ( ret = asn1_get_tag( p, end, &len,
218 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
219 return( ret );
220
221 if( *p + len != end )
222 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
223
224 while( *p < end )
225 {
226 buf = &(cur->buf);
227 buf->tag = **p;
228
229 if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
230 return( ret );
231
232 buf->p = *p;
233 *p += buf->len;
234
235 /* Allocate and assign next pointer */
236 if (*p < end)
237 {
238 cur->next = (asn1_sequence *) malloc(
239 sizeof( asn1_sequence ) );
240
241 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000242 return( POLARSSL_ERR_ASN1_MALLOC_FAILED );
Paul Bakkerefc30292011-11-10 14:43:23 +0000243
Manuel Pégourié-Gonnardd3ae4302014-11-11 22:17:26 +0100244 memset( cur->next, 0, sizeof( asn1_sequence ) );
245
Paul Bakkerefc30292011-11-10 14:43:23 +0000246 cur = cur->next;
247 }
248 }
249
250 /* Set final sequence entry's next pointer to NULL */
251 cur->next = NULL;
252
253 if( *p != end )
254 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
255
256 return( 0 );
257}
258
259#endif