[AMSTREAM] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / dll / 3rdparty / mbedtls / asn1parse.c
1 /*
2 * Generic ASN.1 parsing
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29
30 #if defined(MBEDTLS_ASN1_PARSE_C)
31
32 #include "mbedtls/asn1.h"
33
34 #include <string.h>
35
36 #if defined(MBEDTLS_BIGNUM_C)
37 #include "mbedtls/bignum.h"
38 #endif
39
40 #if defined(MBEDTLS_PLATFORM_C)
41 #include "mbedtls/platform.h"
42 #else
43 #include <stdlib.h>
44 #define mbedtls_calloc calloc
45 #define mbedtls_free free
46 #endif
47
48 /* Implementation that should never be optimized out by the compiler */
49 static void mbedtls_zeroize( void *v, size_t n ) {
50 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
51 }
52
53 /*
54 * ASN.1 DER decoding routines
55 */
56 int mbedtls_asn1_get_len( unsigned char **p,
57 const unsigned char *end,
58 size_t *len )
59 {
60 if( ( end - *p ) < 1 )
61 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
62
63 if( ( **p & 0x80 ) == 0 )
64 *len = *(*p)++;
65 else
66 {
67 switch( **p & 0x7F )
68 {
69 case 1:
70 if( ( end - *p ) < 2 )
71 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
72
73 *len = (*p)[1];
74 (*p) += 2;
75 break;
76
77 case 2:
78 if( ( end - *p ) < 3 )
79 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
80
81 *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
82 (*p) += 3;
83 break;
84
85 case 3:
86 if( ( end - *p ) < 4 )
87 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
88
89 *len = ( (size_t)(*p)[1] << 16 ) |
90 ( (size_t)(*p)[2] << 8 ) | (*p)[3];
91 (*p) += 4;
92 break;
93
94 case 4:
95 if( ( end - *p ) < 5 )
96 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
97
98 *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
99 ( (size_t)(*p)[3] << 8 ) | (*p)[4];
100 (*p) += 5;
101 break;
102
103 default:
104 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
105 }
106 }
107
108 if( *len > (size_t) ( end - *p ) )
109 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
110
111 return( 0 );
112 }
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
121 if( **p != tag )
122 return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
123
124 (*p)++;
125
126 return( mbedtls_asn1_get_len( p, end, len ) );
127 }
128
129 int mbedtls_asn1_get_bool( unsigned char **p,
130 const unsigned char *end,
131 int *val )
132 {
133 int ret;
134 size_t len;
135
136 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
137 return( ret );
138
139 if( len != 1 )
140 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
141
142 *val = ( **p != 0 ) ? 1 : 0;
143 (*p)++;
144
145 return( 0 );
146 }
147
148 int mbedtls_asn1_get_int( unsigned char **p,
149 const unsigned char *end,
150 int *val )
151 {
152 int ret;
153 size_t len;
154
155 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
156 return( ret );
157
158 if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 )
159 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
160
161 *val = 0;
162
163 while( len-- > 0 )
164 {
165 *val = ( *val << 8 ) | **p;
166 (*p)++;
167 }
168
169 return( 0 );
170 }
171
172 #if defined(MBEDTLS_BIGNUM_C)
173 int mbedtls_asn1_get_mpi( unsigned char **p,
174 const unsigned char *end,
175 mbedtls_mpi *X )
176 {
177 int ret;
178 size_t len;
179
180 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
181 return( ret );
182
183 ret = mbedtls_mpi_read_binary( X, *p, len );
184
185 *p += len;
186
187 return( ret );
188 }
189 #endif /* MBEDTLS_BIGNUM_C */
190
191 int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
192 mbedtls_asn1_bitstring *bs)
193 {
194 int ret;
195
196 /* Certificate type is a single byte bitstring */
197 if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
198 return( ret );
199
200 /* Check length, subtract one for actual bit string length */
201 if( bs->len < 1 )
202 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
203 bs->len -= 1;
204
205 /* Get number of unused bits, ensure unused bits <= 7 */
206 bs->unused_bits = **p;
207 if( bs->unused_bits > 7 )
208 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
209 (*p)++;
210
211 /* Get actual bitstring */
212 bs->p = *p;
213 *p += bs->len;
214
215 if( *p != end )
216 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
217
218 return( 0 );
219 }
220
221 /*
222 * Get a bit string without unused bits
223 */
224 int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
225 size_t *len )
226 {
227 int ret;
228
229 if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
230 return( ret );
231
232 if( (*len)-- < 2 || *(*p)++ != 0 )
233 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
234
235 return( 0 );
236 }
237
238
239
240 /*
241 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
242 */
243 int mbedtls_asn1_get_sequence_of( unsigned char **p,
244 const unsigned char *end,
245 mbedtls_asn1_sequence *cur,
246 int tag)
247 {
248 int ret;
249 size_t len;
250 mbedtls_asn1_buf *buf;
251
252 /* Get main sequence tag */
253 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
254 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
255 return( ret );
256
257 if( *p + len != end )
258 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
259
260 while( *p < end )
261 {
262 buf = &(cur->buf);
263 buf->tag = **p;
264
265 if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
266 return( ret );
267
268 buf->p = *p;
269 *p += buf->len;
270
271 /* Allocate and assign next pointer */
272 if( *p < end )
273 {
274 cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
275 sizeof( mbedtls_asn1_sequence ) );
276
277 if( cur->next == NULL )
278 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
279
280 cur = cur->next;
281 }
282 }
283
284 /* Set final sequence entry's next pointer to NULL */
285 cur->next = NULL;
286
287 if( *p != end )
288 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
289
290 return( 0 );
291 }
292
293 int mbedtls_asn1_get_alg( unsigned char **p,
294 const unsigned char *end,
295 mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
296 {
297 int ret;
298 size_t len;
299
300 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
301 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
302 return( ret );
303
304 if( ( end - *p ) < 1 )
305 return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
306
307 alg->tag = **p;
308 end = *p + len;
309
310 if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
311 return( ret );
312
313 alg->p = *p;
314 *p += alg->len;
315
316 if( *p == end )
317 {
318 mbedtls_zeroize( params, sizeof(mbedtls_asn1_buf) );
319 return( 0 );
320 }
321
322 params->tag = **p;
323 (*p)++;
324
325 if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
326 return( ret );
327
328 params->p = *p;
329 *p += params->len;
330
331 if( *p != end )
332 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
333
334 return( 0 );
335 }
336
337 int mbedtls_asn1_get_alg_null( unsigned char **p,
338 const unsigned char *end,
339 mbedtls_asn1_buf *alg )
340 {
341 int ret;
342 mbedtls_asn1_buf params;
343
344 memset( &params, 0, sizeof(mbedtls_asn1_buf) );
345
346 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
347 return( ret );
348
349 if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
350 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
351
352 return( 0 );
353 }
354
355 void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
356 {
357 if( cur == NULL )
358 return;
359
360 mbedtls_free( cur->oid.p );
361 mbedtls_free( cur->val.p );
362
363 mbedtls_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
364 }
365
366 void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
367 {
368 mbedtls_asn1_named_data *cur;
369
370 while( ( cur = *head ) != NULL )
371 {
372 *head = cur->next;
373 mbedtls_asn1_free_named_data( cur );
374 mbedtls_free( cur );
375 }
376 }
377
378 mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
379 const char *oid, size_t len )
380 {
381 while( list != NULL )
382 {
383 if( list->oid.len == len &&
384 memcmp( list->oid.p, oid, len ) == 0 )
385 {
386 break;
387 }
388
389 list = list->next;
390 }
391
392 return( list );
393 }
394
395 #endif /* MBEDTLS_ASN1_PARSE_C */