ReactOS 0.4.16-dev-338-g34e76ad
asn1parse.c
Go to the documentation of this file.
1/*
2 * Generic ASN.1 parsing
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47#if !defined(MBEDTLS_CONFIG_FILE)
48#include "mbedtls/config.h"
49#else
50#include MBEDTLS_CONFIG_FILE
51#endif
52
53#if defined(MBEDTLS_ASN1_PARSE_C)
54
55#include "mbedtls/asn1.h"
57
58#include <string.h>
59
60#if defined(MBEDTLS_BIGNUM_C)
61#include "mbedtls/bignum.h"
62#endif
63
64#if defined(MBEDTLS_PLATFORM_C)
65#include "mbedtls/platform.h"
66#else
67#include <stdlib.h>
68#define mbedtls_calloc calloc
69#define mbedtls_free free
70#endif
71
72/*
73 * ASN.1 DER decoding routines
74 */
75int mbedtls_asn1_get_len( unsigned char **p,
76 const unsigned char *end,
77 size_t *len )
78{
79 if( ( end - *p ) < 1 )
81
82 if( ( **p & 0x80 ) == 0 )
83 *len = *(*p)++;
84 else
85 {
86 switch( **p & 0x7F )
87 {
88 case 1:
89 if( ( end - *p ) < 2 )
91
92 *len = (*p)[1];
93 (*p) += 2;
94 break;
95
96 case 2:
97 if( ( end - *p ) < 3 )
99
100 *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
101 (*p) += 3;
102 break;
103
104 case 3:
105 if( ( end - *p ) < 4 )
107
108 *len = ( (size_t)(*p)[1] << 16 ) |
109 ( (size_t)(*p)[2] << 8 ) | (*p)[3];
110 (*p) += 4;
111 break;
112
113 case 4:
114 if( ( end - *p ) < 5 )
116
117 *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
118 ( (size_t)(*p)[3] << 8 ) | (*p)[4];
119 (*p) += 5;
120 break;
121
122 default:
124 }
125 }
126
127 if( *len > (size_t) ( end - *p ) )
129
130 return( 0 );
131}
132
133int mbedtls_asn1_get_tag( unsigned char **p,
134 const unsigned char *end,
135 size_t *len, int tag )
136{
137 if( ( end - *p ) < 1 )
139
140 if( **p != tag )
142
143 (*p)++;
144
145 return( mbedtls_asn1_get_len( p, end, len ) );
146}
147
148int mbedtls_asn1_get_bool( 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_BOOLEAN ) ) != 0 )
156 return( ret );
157
158 if( len != 1 )
160
161 *val = ( **p != 0 ) ? 1 : 0;
162 (*p)++;
163
164 return( 0 );
165}
166
167int mbedtls_asn1_get_int( unsigned char **p,
168 const unsigned char *end,
169 int *val )
170{
171 int ret;
172 size_t len;
173
174 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
175 return( ret );
176
177 if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 )
179
180 *val = 0;
181
182 while( len-- > 0 )
183 {
184 *val = ( *val << 8 ) | **p;
185 (*p)++;
186 }
187
188 return( 0 );
189}
190
191#if defined(MBEDTLS_BIGNUM_C)
192int mbedtls_asn1_get_mpi( unsigned char **p,
193 const unsigned char *end,
194 mbedtls_mpi *X )
195{
196 int ret;
197 size_t len;
198
199 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
200 return( ret );
201
203
204 *p += len;
205
206 return( ret );
207}
208#endif /* MBEDTLS_BIGNUM_C */
209
210int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
212{
213 int ret;
214
215 /* Certificate type is a single byte bitstring */
216 if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
217 return( ret );
218
219 /* Check length, subtract one for actual bit string length */
220 if( bs->len < 1 )
222 bs->len -= 1;
223
224 /* Get number of unused bits, ensure unused bits <= 7 */
225 bs->unused_bits = **p;
226 if( bs->unused_bits > 7 )
228 (*p)++;
229
230 /* Get actual bitstring */
231 bs->p = *p;
232 *p += bs->len;
233
234 if( *p != end )
236
237 return( 0 );
238}
239
240/*
241 * Get a bit string without unused bits
242 */
243int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
244 size_t *len )
245{
246 int ret;
247
249 return( ret );
250
251 if( (*len)-- < 2 || *(*p)++ != 0 )
253
254 return( 0 );
255}
256
257
258
259/*
260 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
261 */
262int mbedtls_asn1_get_sequence_of( unsigned char **p,
263 const unsigned char *end,
265 int tag)
266{
267 int ret;
268 size_t len;
270
271 /* Get main sequence tag */
272 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
274 return( ret );
275
276 if( *p + len != end )
278
279 while( *p < end )
280 {
281 buf = &(cur->buf);
282 buf->tag = **p;
283
284 if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
285 return( ret );
286
287 buf->p = *p;
288 *p += buf->len;
289
290 /* Allocate and assign next pointer */
291 if( *p < end )
292 {
294 sizeof( mbedtls_asn1_sequence ) );
295
296 if( cur->next == NULL )
298
299 cur = cur->next;
300 }
301 }
302
303 /* Set final sequence entry's next pointer to NULL */
304 cur->next = NULL;
305
306 if( *p != end )
308
309 return( 0 );
310}
311
312int mbedtls_asn1_get_alg( unsigned char **p,
313 const unsigned char *end,
315{
316 int ret;
317 size_t len;
318
319 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
321 return( ret );
322
323 if( ( end - *p ) < 1 )
325
326 alg->tag = **p;
327 end = *p + len;
328
329 if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
330 return( ret );
331
332 alg->p = *p;
333 *p += alg->len;
334
335 if( *p == end )
336 {
338 return( 0 );
339 }
340
341 params->tag = **p;
342 (*p)++;
343
344 if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
345 return( ret );
346
347 params->p = *p;
348 *p += params->len;
349
350 if( *p != end )
352
353 return( 0 );
354}
355
356int mbedtls_asn1_get_alg_null( unsigned char **p,
357 const unsigned char *end,
358 mbedtls_asn1_buf *alg )
359{
360 int ret;
362
363 memset( &params, 0, sizeof(mbedtls_asn1_buf) );
364
365 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
366 return( ret );
367
368 if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
370
371 return( 0 );
372}
373
375{
376 if( cur == NULL )
377 return;
378
379 mbedtls_free( cur->oid.p );
380 mbedtls_free( cur->val.p );
381
383}
384
386{
388
389 while( ( cur = *head ) != NULL )
390 {
391 *head = cur->next;
393 mbedtls_free( cur );
394 }
395}
396
398 const char *oid, size_t len )
399{
400 while( list != NULL )
401 {
402 if( list->oid.len == len &&
403 memcmp( list->oid.p, oid, len ) == 0 )
404 {
405 break;
406 }
407
408 list = list->next;
409 }
410
411 return( list );
412}
413
414#endif /* MBEDTLS_ASN1_PARSE_C */
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
struct outqueuenode * head
Definition: adnsresfilter.c:66
Generic ASN.1 parsing.
Multi-precision integer library.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import an MPI from unsigned big endian binary data.
Definition: list.h:37
struct list * next
Definition: list.h:38
#define NULL
Definition: types.h:112
__kernel_size_t size_t
Definition: linux.h:237
FxCollectionEntry * cur
GLuint GLuint end
Definition: gl.h:1545
GLenum const GLfloat * params
Definition: glext.h:5645
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint GLfloat * val
Definition: glext.h:7180
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
size_t len
Definition: asn1.h:162
unsigned char * p
Definition: asn1.h:163
#define MBEDTLS_ERR_ASN1_INVALID_LENGTH
Definition: asn1.h:78
int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end, size_t *len)
Retrieve a bitstring ASN.1 tag without unused bits and its value. Updates the pointer to the beginnin...
#define MBEDTLS_ASN1_BOOLEAN
Definition: asn1.h:97
#define MBEDTLS_ERR_ASN1_OUT_OF_DATA
Definition: asn1.h:76
void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *entry)
Free a mbedtls_asn1_named_data entry.
int mbedtls_asn1_get_mpi(unsigned char **p, const unsigned char *end, mbedtls_mpi *X)
Retrieve a MPI value from an integer ASN.1 tag. Updates the pointer to immediately behind the full ta...
#define MBEDTLS_ASN1_SEQUENCE
Definition: asn1.h:104
#define MBEDTLS_ASN1_INTEGER
Definition: asn1.h:98
int mbedtls_asn1_get_int(unsigned char **p, const unsigned char *end, int *val)
Retrieve an integer ASN.1 tag and its value. Updates the pointer to immediately behind the full tag.
int mbedtls_asn1_get_sequence_of(unsigned char **p, const unsigned char *end, mbedtls_asn1_sequence *cur, int tag)
Parses and splits an ASN.1 "SEQUENCE OF <tag>" Updated the pointer to immediately behind the full seq...
#define MBEDTLS_ASN1_CONSTRUCTED
Definition: asn1.h:114
#define MBEDTLS_ERR_ASN1_INVALID_DATA
Definition: asn1.h:80
mbedtls_asn1_named_data * mbedtls_asn1_find_named_data(mbedtls_asn1_named_data *list, const char *oid, size_t len)
Find a specific named_data entry in a sequence or list based on the OID.
#define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
Definition: asn1.h:77
#define MBEDTLS_ERR_ASN1_ALLOC_FAILED
Definition: asn1.h:81
#define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
Definition: asn1.h:79
int mbedtls_asn1_get_len(unsigned char **p, const unsigned char *end, size_t *len)
Get the length of an ASN.1 element. Updates the pointer to immediately behind the length.
#define MBEDTLS_ASN1_OID
Definition: asn1.h:102
int mbedtls_asn1_get_alg_null(unsigned char **p, const unsigned char *end, mbedtls_asn1_buf *alg)
Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no params. Updates the pointer to immedia...
int mbedtls_asn1_get_alg(unsigned char **p, const unsigned char *end, mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params)
Retrieve an AlgorithmIdentifier ASN.1 sequence. Updates the pointer to immediately behind the full Al...
#define MBEDTLS_ASN1_NULL
Definition: asn1.h:101
int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end, mbedtls_asn1_bitstring *bs)
Retrieve a bitstring ASN.1 tag and its value. Updates the pointer to immediately behind the full tag.
int mbedtls_asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag. Check for the requested tag. Updates the pointer to immediately be...
int mbedtls_asn1_get_bool(unsigned char **p, const unsigned char *end, int *val)
Retrieve a boolean ASN.1 tag and its value. Updates the pointer to immediately behind the full tag.
void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head)
Free all entries in a mbedtls_asn1_named_data list Head will be set to NULL.
#define MBEDTLS_ASN1_BIT_STRING
Definition: asn1.h:99
static struct msdos_boot_sector bs
Definition: mkdosfs.c:539
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
Definition: platform_util.c:98
Common and shared functions used by multiple modules in the Mbed TLS library.
Configuration options (set of defines)
This file contains the definitions and functions of the Mbed TLS platform abstraction layer.
#define mbedtls_free
Definition: platform.h:168
#define mbedtls_calloc
Definition: platform.h:169
#define memset(x, y, z)
Definition: compat.h:39
MPI structure.
Definition: bignum.h:211
Definition: ecma_167.h:138
int ret