2 * ASN.1 buffer writing functionality
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #if defined(MBEDTLS_ASN1_WRITE_C)
24 #include "mbedtls/asn1write.h"
25 #include "mbedtls/error.h"
29 #if defined(MBEDTLS_PLATFORM_C)
30 #include "mbedtls/platform.h"
33 #define mbedtls_calloc calloc
34 #define mbedtls_free free
37 int mbedtls_asn1_write_len(unsigned char **p
, unsigned char *start
, size_t len
) {
40 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
42 *--(*p
) = (unsigned char) len
;
48 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
50 *--(*p
) = (unsigned char) len
;
57 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
59 *--(*p
) = (len
) & 0xFF;
60 *--(*p
) = (len
>> 8) & 0xFF;
65 if (len
<= 0xFFFFFF) {
67 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
69 *--(*p
) = (len
) & 0xFF;
70 *--(*p
) = (len
>> 8) & 0xFF;
71 *--(*p
) = (len
>> 16) & 0xFF;
76 #if SIZE_MAX > 0xFFFFFFFF
77 if (len
<= 0xFFFFFFFF)
81 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
83 *--(*p
) = (len
) & 0xFF;
84 *--(*p
) = (len
>> 8) & 0xFF;
85 *--(*p
) = (len
>> 16) & 0xFF;
86 *--(*p
) = (len
>> 24) & 0xFF;
91 #if SIZE_MAX > 0xFFFFFFFF
92 return (MBEDTLS_ERR_ASN1_INVALID_LENGTH
);
96 int mbedtls_asn1_write_tag(unsigned char **p
, unsigned char *start
, unsigned char tag
) {
98 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
105 int mbedtls_asn1_write_raw_buffer(unsigned char **p
, unsigned char *start
,
106 const unsigned char *buf
, size_t size
) {
109 if (*p
< start
|| (size_t)(*p
- start
) < size
)
110 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
114 memcpy(*p
, buf
, len
);
119 #if defined(MBEDTLS_BIGNUM_C)
120 int mbedtls_asn1_write_mpi(unsigned char **p
, unsigned char *start
, const mbedtls_mpi
*X
) {
121 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
126 len
= mbedtls_mpi_size(X
);
128 if (*p
< start
|| (size_t)(*p
- start
) < len
)
129 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
132 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X
, *p
, len
));
134 // DER format assumes 2s complement for numbers, so the leftmost bit
135 // should be 0 for positive numbers and 1 for negative numbers.
137 if (X
->s
== 1 && **p
& 0x80) {
139 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
145 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
146 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, MBEDTLS_ASN1_INTEGER
));
153 #endif /* MBEDTLS_BIGNUM_C */
155 int mbedtls_asn1_write_null(unsigned char **p
, unsigned char *start
) {
156 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
161 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, 0));
162 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, MBEDTLS_ASN1_NULL
));
167 int mbedtls_asn1_write_oid(unsigned char **p
, unsigned char *start
,
168 const char *oid
, size_t oid_len
) {
169 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
172 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_raw_buffer(p
, start
,
173 (const unsigned char *) oid
, oid_len
));
174 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
175 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, MBEDTLS_ASN1_OID
));
180 int mbedtls_asn1_write_algorithm_identifier(unsigned char **p
, unsigned char *start
,
181 const char *oid
, size_t oid_len
,
183 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
187 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_null(p
, start
));
191 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_oid(p
, start
, oid
, oid_len
));
193 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
194 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
,
195 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
));
200 int mbedtls_asn1_write_bool(unsigned char **p
, unsigned char *start
, int boolean
) {
201 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
205 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
207 *--(*p
) = (boolean
) ? 255 : 0;
210 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
211 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, MBEDTLS_ASN1_BOOLEAN
));
216 static int asn1_write_tagged_int(unsigned char **p
, unsigned char *start
, int val
, int tag
) {
217 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
222 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
224 *--(*p
) = val
& 0xff;
230 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
235 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
236 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, tag
));
241 int mbedtls_asn1_write_int(unsigned char **p
, unsigned char *start
, int val
) {
242 return (asn1_write_tagged_int(p
, start
, val
, MBEDTLS_ASN1_INTEGER
));
245 int mbedtls_asn1_write_enum(unsigned char **p
, unsigned char *start
, int val
) {
246 return (asn1_write_tagged_int(p
, start
, val
, MBEDTLS_ASN1_ENUMERATED
));
249 int mbedtls_asn1_write_tagged_string(unsigned char **p
, unsigned char *start
, int tag
,
250 const char *text
, size_t text_len
) {
251 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
254 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_raw_buffer(p
, start
,
255 (const unsigned char *) text
, text_len
));
257 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
258 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, tag
));
263 int mbedtls_asn1_write_utf8_string(unsigned char **p
, unsigned char *start
,
264 const char *text
, size_t text_len
) {
265 return (mbedtls_asn1_write_tagged_string(p
, start
, MBEDTLS_ASN1_UTF8_STRING
, text
, text_len
));
268 int mbedtls_asn1_write_printable_string(unsigned char **p
, unsigned char *start
,
269 const char *text
, size_t text_len
) {
270 return (mbedtls_asn1_write_tagged_string(p
, start
, MBEDTLS_ASN1_PRINTABLE_STRING
, text
, text_len
));
273 int mbedtls_asn1_write_ia5_string(unsigned char **p
, unsigned char *start
,
274 const char *text
, size_t text_len
) {
275 return (mbedtls_asn1_write_tagged_string(p
, start
, MBEDTLS_ASN1_IA5_STRING
, text
, text_len
));
278 int mbedtls_asn1_write_named_bitstring(unsigned char **p
,
279 unsigned char *start
,
280 const unsigned char *buf
,
282 size_t unused_bits
, byte_len
;
283 const unsigned char *cur_byte
;
284 unsigned char cur_byte_shifted
;
287 byte_len
= (bits
+ 7) / 8;
288 unused_bits
= (byte_len
* 8) - bits
;
291 * Named bitstrings require that trailing 0s are excluded in the encoding
292 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
293 * when encoding this value in the first content octet
296 cur_byte
= buf
+ byte_len
- 1;
297 cur_byte_shifted
= *cur_byte
>> unused_bits
;
300 bit
= cur_byte_shifted
& 0x1;
301 cur_byte_shifted
>>= 1;
311 cur_byte_shifted
= *--cur_byte
;
315 return (mbedtls_asn1_write_bitstring(p
, start
, buf
, bits
));
318 int mbedtls_asn1_write_bitstring(unsigned char **p
, unsigned char *start
,
319 const unsigned char *buf
, size_t bits
) {
320 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
322 size_t unused_bits
, byte_len
;
324 byte_len
= (bits
+ 7) / 8;
325 unused_bits
= (byte_len
* 8) - bits
;
327 if (*p
< start
|| (size_t)(*p
- start
) < byte_len
+ 1)
328 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
332 /* Write the bitstring. Ensure the unused bits are zeroed */
335 *--(*p
) = buf
[byte_len
] & ~((0x1 << unused_bits
) - 1);
337 memcpy(*p
, buf
, byte_len
);
340 /* Write unused bits */
341 *--(*p
) = (unsigned char)unused_bits
;
343 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
344 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, MBEDTLS_ASN1_BIT_STRING
));
349 int mbedtls_asn1_write_octet_string(unsigned char **p
, unsigned char *start
,
350 const unsigned char *buf
, size_t size
) {
351 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
354 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_raw_buffer(p
, start
, buf
, size
));
356 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
357 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
, MBEDTLS_ASN1_OCTET_STRING
));
363 /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
364 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
365 static mbedtls_asn1_named_data
*asn1_find_named_data(
366 mbedtls_asn1_named_data
*list
,
367 const char *oid
, size_t len
) {
368 while (list
!= NULL
) {
369 if (list
->oid
.len
== len
&&
370 memcmp(list
->oid
.p
, oid
, len
) == 0) {
380 mbedtls_asn1_named_data
*mbedtls_asn1_store_named_data(
381 mbedtls_asn1_named_data
**head
,
382 const char *oid
, size_t oid_len
,
383 const unsigned char *val
,
385 mbedtls_asn1_named_data
*cur
;
387 if ((cur
= asn1_find_named_data(*head
, oid
, oid_len
)) == NULL
) {
388 // Add new entry if not present yet based on OID
390 cur
= (mbedtls_asn1_named_data
*)mbedtls_calloc(1,
391 sizeof(mbedtls_asn1_named_data
));
395 cur
->oid
.len
= oid_len
;
396 cur
->oid
.p
= mbedtls_calloc(1, oid_len
);
397 if (cur
->oid
.p
== NULL
) {
402 memcpy(cur
->oid
.p
, oid
, oid_len
);
404 cur
->val
.len
= val_len
;
406 cur
->val
.p
= mbedtls_calloc(1, val_len
);
407 if (cur
->val
.p
== NULL
) {
408 mbedtls_free(cur
->oid
.p
);
416 } else if (val_len
== 0) {
417 mbedtls_free(cur
->val
.p
);
419 } else if (cur
->val
.len
!= val_len
) {
421 * Enlarge existing value buffer if needed
422 * Preserve old data until the allocation succeeded, to leave list in
423 * a consistent state in case allocation fails.
425 void *p
= mbedtls_calloc(1, val_len
);
429 mbedtls_free(cur
->val
.p
);
431 cur
->val
.len
= val_len
;
435 memcpy(cur
->val
.p
, val
, val_len
);
439 #endif /* MBEDTLS_ASN1_WRITE_C */