ReactOS 0.4.16-dev-329-g9223134
asn1write.c
Go to the documentation of this file.
1/*
2 * ASN.1 buffer writing functionality
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_WRITE_C)
54
55#include "mbedtls/asn1write.h"
56
57#include <string.h>
58
59#if defined(MBEDTLS_PLATFORM_C)
60#include "mbedtls/platform.h"
61#else
62#include <stdlib.h>
63#define mbedtls_calloc calloc
64#define mbedtls_free free
65#endif
66
67int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
68{
69 if( len < 0x80 )
70 {
71 if( *p - start < 1 )
73
74 *--(*p) = (unsigned char) len;
75 return( 1 );
76 }
77
78 if( len <= 0xFF )
79 {
80 if( *p - start < 2 )
82
83 *--(*p) = (unsigned char) len;
84 *--(*p) = 0x81;
85 return( 2 );
86 }
87
88 if( len <= 0xFFFF )
89 {
90 if( *p - start < 3 )
92
93 *--(*p) = ( len ) & 0xFF;
94 *--(*p) = ( len >> 8 ) & 0xFF;
95 *--(*p) = 0x82;
96 return( 3 );
97 }
98
99 if( len <= 0xFFFFFF )
100 {
101 if( *p - start < 4 )
103
104 *--(*p) = ( len ) & 0xFF;
105 *--(*p) = ( len >> 8 ) & 0xFF;
106 *--(*p) = ( len >> 16 ) & 0xFF;
107 *--(*p) = 0x83;
108 return( 4 );
109 }
110
111#if SIZE_MAX > 0xFFFFFFFF
112 if( len <= 0xFFFFFFFF )
113#endif
114 {
115 if( *p - start < 5 )
117
118 *--(*p) = ( len ) & 0xFF;
119 *--(*p) = ( len >> 8 ) & 0xFF;
120 *--(*p) = ( len >> 16 ) & 0xFF;
121 *--(*p) = ( len >> 24 ) & 0xFF;
122 *--(*p) = 0x84;
123 return( 5 );
124 }
125
126#if SIZE_MAX > 0xFFFFFFFF
128#endif
129}
130
131int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
132{
133 if( *p - start < 1 )
135
136 *--(*p) = tag;
137
138 return( 1 );
139}
140
141int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
142 const unsigned char *buf, size_t size )
143{
144 size_t len = 0;
145
146 if( *p < start || (size_t)( *p - start ) < size )
148
149 len = size;
150 (*p) -= len;
151 memcpy( *p, buf, len );
152
153 return( (int) len );
154}
155
156#if defined(MBEDTLS_BIGNUM_C)
157int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
158{
159 int ret;
160 size_t len = 0;
161
162 // Write the MPI
163 //
165
166 if( *p < start || (size_t)( *p - start ) < len )
168
169 (*p) -= len;
171
172 // DER format assumes 2s complement for numbers, so the leftmost bit
173 // should be 0 for positive numbers and 1 for negative numbers.
174 //
175 if( X->s ==1 && **p & 0x80 )
176 {
177 if( *p - start < 1 )
179
180 *--(*p) = 0x00;
181 len += 1;
182 }
183
186
187 ret = (int) len;
188
189cleanup:
190 return( ret );
191}
192#endif /* MBEDTLS_BIGNUM_C */
193
194int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
195{
196 int ret;
197 size_t len = 0;
198
199 // Write NULL
200 //
203
204 return( (int) len );
205}
206
207int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
208 const char *oid, size_t oid_len )
209{
210 int ret;
211 size_t len = 0;
212
214 (const unsigned char *) oid, oid_len ) );
217
218 return( (int) len );
219}
220
221int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
222 const char *oid, size_t oid_len,
223 size_t par_len )
224{
225 int ret;
226 size_t len = 0;
227
228 if( par_len == 0 )
230 else
231 len += par_len;
232
234
238
239 return( (int) len );
240}
241
242int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
243{
244 int ret;
245 size_t len = 0;
246
247 if( *p - start < 1 )
249
250 *--(*p) = (boolean) ? 255 : 0;
251 len++;
252
255
256 return( (int) len );
257}
258
259int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
260{
261 int ret;
262 size_t len = 0;
263
264 if( *p - start < 1 )
266
267 len += 1;
268 *--(*p) = val;
269
270 if( val > 0 && **p & 0x80 )
271 {
272 if( *p - start < 1 )
274
275 *--(*p) = 0x00;
276 len += 1;
277 }
278
281
282 return( (int) len );
283}
284
285int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
286 const char *text, size_t text_len )
287{
288 int ret;
289 size_t len = 0;
290
292 (const unsigned char *) text, text_len ) );
293
296
297 return( (int) len );
298}
299
300int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
301 const char *text, size_t text_len )
302{
304}
305
306int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
307 const char *text, size_t text_len )
308{
310}
311
312int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
313 const char *text, size_t text_len )
314{
316}
317
318int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
319 const unsigned char *buf, size_t bits )
320{
321 int ret;
322 size_t len = 0;
323 size_t unused_bits, byte_len;
324
325 byte_len = ( bits + 7 ) / 8;
326 unused_bits = ( byte_len * 8 ) - bits;
327
328 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
330
331 len = byte_len + 1;
332
333 /* Write the bitstring. Ensure the unused bits are zeroed */
334 if( byte_len > 0 )
335 {
336 byte_len--;
337 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
338 ( *p ) -= byte_len;
339 memcpy( *p, buf, byte_len );
340 }
341
342 /* Write unused bits */
343 *--( *p ) = (unsigned char)unused_bits;
344
347
348 return( (int) len );
349}
350
351int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
352 const unsigned char *buf, size_t size )
353{
354 int ret;
355 size_t len = 0;
356
358
361
362 return( (int) len );
363}
364
365
366/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
367 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
370 const char *oid, size_t len )
371{
372 while( list != NULL )
373 {
374 if( list->oid.len == len &&
375 memcmp( list->oid.p, oid, len ) == 0 )
376 {
377 break;
378 }
379
380 list = list->next;
381 }
382
383 return( list );
384}
385
388 const char *oid, size_t oid_len,
389 const unsigned char *val,
390 size_t val_len )
391{
393
394 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
395 {
396 // Add new entry if not present yet based on OID
397 //
399 sizeof(mbedtls_asn1_named_data) );
400 if( cur == NULL )
401 return( NULL );
402
403 cur->oid.len = oid_len;
404 cur->oid.p = mbedtls_calloc( 1, oid_len );
405 if( cur->oid.p == NULL )
406 {
407 mbedtls_free( cur );
408 return( NULL );
409 }
410
411 memcpy( cur->oid.p, oid, oid_len );
412
413 cur->val.len = val_len;
414 cur->val.p = mbedtls_calloc( 1, val_len );
415 if( cur->val.p == NULL )
416 {
417 mbedtls_free( cur->oid.p );
418 mbedtls_free( cur );
419 return( NULL );
420 }
421
422 cur->next = *head;
423 *head = cur;
424 }
425 else if( cur->val.len < val_len )
426 {
427 /*
428 * Enlarge existing value buffer if needed
429 * Preserve old data until the allocation succeeded, to leave list in
430 * a consistent state in case allocation fails.
431 */
432 void *p = mbedtls_calloc( 1, val_len );
433 if( p == NULL )
434 return( NULL );
435
436 mbedtls_free( cur->val.p );
437 cur->val.p = p;
438 cur->val.len = val_len;
439 }
440
441 if( val != NULL )
442 memcpy( cur->val.p, val, val_len );
443
444 return( cur );
445}
446#endif /* MBEDTLS_ASN1_WRITE_C */
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
struct outqueuenode * head
Definition: adnsresfilter.c:66
ASN.1 buffer writing functionality.
int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write raw buffer data.
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write an ASN.1 tag in ASN.1 format.
int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits)
Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a string in ASN.1 format using the PrintableString string encoding tag (MBEDTLS_ASN1_PRINTABLE_...
#define MBEDTLS_ASN1_CHK_ADD(g, f)
Definition: asn1write.h:60
int mbedtls_asn1_write_utf8_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a UTF8 string in ASN.1 format using the UTF8String string encoding tag (MBEDTLS_ASN1_PRINTABLE_...
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and value in ASN.1 format.
mbedtls_asn1_named_data * mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list, const char *oid, size_t oid_len, const unsigned char *val, size_t val_len)
Create or find a specific named_data entry for writing in a sequence or list based on the OID....
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format.
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format.
int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len)
Write an AlgorithmIdentifier sequence in ASN.1 format.
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len)
Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format.
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
Write a arbitrary-precision number (MBEDTLS_ASN1_INTEGER) in ASN.1 format.
int mbedtls_asn1_write_tagged_string(unsigned char **p, unsigned char *start, int tag, const char *text, size_t text_len)
Write a string in ASN.1 format using a specific string encoding tag.
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format.
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format.
int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a string in ASN.1 format using the IA5String string encoding tag (MBEDTLS_ASN1_IA5_STRING).
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
#define MBEDTLS_MPI_CHK(f)
Definition: bignum.h:74
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
Definition: list.h:37
struct list * next
Definition: list.h:38
#define asn1_find_named_data
Definition: compat-1.3.h:1766
#define NULL
Definition: types.h:112
static void cleanup(void)
Definition: main.c:1335
const WCHAR * text
Definition: package.c:1794
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
unsigned char
Definition: typeof.h:29
FxCollectionEntry * cur
GLuint start
Definition: gl.h:1545
GLsizeiptr size
Definition: glext.h:5919
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
GLuint GLfloat * val
Definition: glext.h:7180
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
#define MBEDTLS_ASN1_OCTET_STRING
Definition: asn1.h:100
#define MBEDTLS_ERR_ASN1_INVALID_LENGTH
Definition: asn1.h:78
#define MBEDTLS_ASN1_BOOLEAN
Definition: asn1.h:97
#define MBEDTLS_ASN1_IA5_STRING
Definition: asn1.h:108
#define MBEDTLS_ASN1_PRINTABLE_STRING
Definition: asn1.h:106
#define MBEDTLS_ASN1_SEQUENCE
Definition: asn1.h:104
#define MBEDTLS_ASN1_INTEGER
Definition: asn1.h:98
#define MBEDTLS_ASN1_CONSTRUCTED
Definition: asn1.h:114
#define MBEDTLS_ASN1_OID
Definition: asn1.h:102
#define MBEDTLS_ASN1_NULL
Definition: asn1.h:101
#define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
Definition: asn1.h:82
#define MBEDTLS_ASN1_UTF8_STRING
Definition: asn1.h:103
#define MBEDTLS_ASN1_BIT_STRING
Definition: asn1.h:99
boolean
Definition: jmorecfg.h:365
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
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
MPI structure.
Definition: bignum.h:211
Definition: ecma_167.h:138
int ret