blob: 775a9ef530fb469a01c50f06059794fdebc8b58b [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerbdb912d2012-02-13 23:11:30 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +00009
Valerio Setti688f7952024-01-16 09:18:40 +010010#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C) || \
11 defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000012
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000013#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000014#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000015
Rich Evans00ab4702015-02-06 13:43:58 +000016#include <string.h>
17
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020019
Dave Rodgman49352832023-09-11 17:09:13 +010020#if defined(MBEDTLS_ASN1_PARSE_C)
21#include "mbedtls/asn1.h"
22#endif
23
Gilles Peskine449bd832023-01-11 14:50:10 +010024int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025{
Azim Khan45b79cf2018-05-23 16:55:16 +010026#if SIZE_MAX > 0xFFFFFFFF
Dave Rodgman3bbedf62023-09-11 16:06:28 +010027 if (len > 0xFFFFFFFF) {
28 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
29 }
Azim Khan45b79cf2018-05-23 16:55:16 +010030#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010031
Dave Rodgman9f366b02023-09-11 15:47:00 +010032 int required = 1;
Dave Rodgman33287ae2023-09-11 17:03:22 +010033
34 if (len >= 0x80) {
Dave Rodgman9f366b02023-09-11 15:47:00 +010035 for (size_t l = len; l != 0; l >>= 8) {
36 required++;
37 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010038 }
39
Dave Rodgman9f366b02023-09-11 15:47:00 +010040 if (required > (*p - start)) {
41 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
42 }
43
44 do {
45 *--(*p) = MBEDTLS_BYTE_0(len);
46 len >>= 8;
47 } while (len);
48
49 if (required > 1) {
Dave Rodgman33287ae2023-09-11 17:03:22 +010050 *--(*p) = (unsigned char) (0x80 + required - 1);
Dave Rodgman9f366b02023-09-11 15:47:00 +010051 }
52
53 return required;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054}
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000057{
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if (*p - start < 1) {
59 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
60 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061
62 *--(*p) = tag;
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000065}
Valerio Setti688f7952024-01-16 09:18:40 +010066#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C || MBEDTLS_PSA_UTIL_HAVE_ECDSA */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010068#if defined(MBEDTLS_ASN1_WRITE_C)
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +010069static int mbedtls_asn1_write_len_and_tag(unsigned char **p,
Dave Rodgman0c9516e2023-09-15 18:30:09 +010070 const unsigned char *start,
71 size_t len,
72 unsigned char tag)
Dave Rodgmancf5f7462023-09-11 16:27:34 +010073{
74 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
75
76 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
77 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
78
Dave Rodgmandc669a12023-09-11 18:39:57 +010079 return (int) len;
Dave Rodgmancf5f7462023-09-11 16:27:34 +010080}
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
83 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020084{
85 size_t len = 0;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (*p < start || (size_t) (*p - start) < size) {
88 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
89 }
Paul Bakker9852d002013-08-26 17:56:37 +020090
91 len = size;
92 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +020096}
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010099int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100{
Janos Follath24eed8d2019-11-22 13:21:35 +0000101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102 size_t len = 0;
103
104 // Write the MPI
105 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107
Gilles Peskine321a0892022-06-10 20:13:33 +0200108 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
109 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200111 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (*p < start || (size_t) (*p - start) < len) {
115 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
116 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117
118 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120
121 // DER format assumes 2s complement for numbers, so the leftmost bit
122 // should be 0 for positive numbers and 1 for negative numbers.
123 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (X->s == 1 && **p & 0x80) {
125 if (*p - start < 1) {
126 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
127 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128
129 *--(*p) = 0x00;
130 len += 1;
131 }
132
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100133 ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200134
135cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142 // Write NULL
143 //
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100144 return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145}
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
148 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 size_t len = 0;
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
154 (const unsigned char *) oid, oid_len));
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100155 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156}
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
159 const char *oid, size_t oid_len,
160 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161{
Jethro Beekman01672442023-04-19 14:08:14 +0200162 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
163}
164
165int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
166 const char *oid, size_t oid_len,
167 size_t par_len, int has_par)
168{
Janos Follath24eed8d2019-11-22 13:21:35 +0000169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 size_t len = 0;
171
Jethro Beekman01672442023-04-19 14:08:14 +0200172 if (has_par) {
173 if (par_len == 0) {
174 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
175 } else {
176 len += par_len;
177 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100182 return mbedtls_asn1_write_len_and_tag(p, start, len,
Dave Rodgman0c9516e2023-09-15 18:30:09 +0100183 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184}
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200187{
Paul Bakker329def32013-09-06 16:34:38 +0200188 size_t len = 0;
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (*p - start < 1) {
191 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
192 }
Paul Bakker329def32013-09-06 16:34:38 +0200193
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200194 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200195 len++;
196
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100197 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000201{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202 size_t len = 0;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 do {
205 if (*p - start < 1) {
206 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
207 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100208 len += 1;
209 *--(*p) = val & 0xff;
210 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (**p & 0x80) {
214 if (*p - start < 1) {
215 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
216 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217 *--(*p) = 0x00;
218 len += 1;
219 }
220
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100221 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222}
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200225{
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200230{
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200232}
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
235 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236{
Janos Follath24eed8d2019-11-22 13:21:35 +0000237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238 size_t len = 0;
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
241 (const unsigned char *) text,
242 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100244 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245}
Paul Bakker598e4502013-08-25 14:46:39 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
248 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100249{
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100251}
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
254 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100255{
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
257 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100258}
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
261 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000262{
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000264}
Paul Bakker598e4502013-08-25 14:46:39 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266int mbedtls_asn1_write_named_bitstring(unsigned char **p,
267 const unsigned char *start,
268 const unsigned char *buf,
269 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100270{
271 size_t unused_bits, byte_len;
272 const unsigned char *cur_byte;
273 unsigned char cur_byte_shifted;
274 unsigned char bit;
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 byte_len = (bits + 7) / 8;
277 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100278
279 /*
280 * Named bitstrings require that trailing 0s are excluded in the encoding
281 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
282 * when encoding this value in the first content octet
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100285 cur_byte = buf + byte_len - 1;
286 cur_byte_shifted = *cur_byte >> unused_bits;
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100289 bit = cur_byte_shifted & 0x1;
290 cur_byte_shifted >>= 1;
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100293 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100295
296 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100298 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100302 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100304 }
305 }
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100308}
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
311 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200312{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100313 size_t len = 0;
314 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 byte_len = (bits + 7) / 8;
317 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
320 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
321 }
Paul Bakker598e4502013-08-25 14:46:39 +0200322
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100323 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200324
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100325 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100327 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
329 (*p) -= byte_len;
330 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100331 }
332
333 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200335
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100336 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200337}
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
340 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200341{
Janos Follath24eed8d2019-11-22 13:21:35 +0000342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200343 size_t len = 0;
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200346
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100347 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200348}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200349
Hanno Becker44da18a2018-10-12 10:42:13 +0100350
Dave Rodgman49352832023-09-11 17:09:13 +0100351#if !defined(MBEDTLS_ASN1_PARSE_C)
Hanno Becker44da18a2018-10-12 10:42:13 +0100352/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
353 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
354static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 mbedtls_asn1_named_data *list,
356 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100357{
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 while (list != NULL) {
359 if (list->oid.len == len &&
360 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100361 break;
362 }
363
364 list = list->next;
365 }
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100368}
Dave Rodgman49352832023-09-11 17:09:13 +0100369#else
370#define asn1_find_named_data(list, oid, len) \
371 ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
372#endif
Hanno Becker44da18a2018-10-12 10:42:13 +0100373
374mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 mbedtls_asn1_named_data **head,
376 const char *oid, size_t oid_len,
377 const unsigned char *val,
378 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200383 // Add new entry if not present yet based on OID
384 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
386 sizeof(mbedtls_asn1_named_data));
387 if (cur == NULL) {
388 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200389 }
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 cur->oid.len = oid_len;
392 cur->oid.p = mbedtls_calloc(1, oid_len);
393 if (cur->oid.p == NULL) {
394 mbedtls_free(cur);
395 return NULL;
396 }
397
398 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100399
Paul Bakker59ba59f2013-09-09 11:26:00 +0200400 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (val_len != 0) {
402 cur->val.p = mbedtls_calloc(1, val_len);
403 if (cur->val.p == NULL) {
404 mbedtls_free(cur->oid.p);
405 mbedtls_free(cur);
406 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100407 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200408 }
409
Paul Bakker59ba59f2013-09-09 11:26:00 +0200410 cur->next = *head;
411 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 } else if (val_len == 0) {
413 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100414 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100416 /*
417 * Enlarge existing value buffer if needed
418 * Preserve old data until the allocation succeeded, to leave list in
419 * a consistent state in case allocation fails.
420 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 void *p = mbedtls_calloc(1, val_len);
422 if (p == NULL) {
423 return NULL;
424 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100427 cur->val.p = p;
428 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200429 }
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (val != NULL && val_len != 0) {
432 memcpy(cur->val.p, val, val_len);
433 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#endif /* MBEDTLS_ASN1_WRITE_C */