blob: 97f9db039beb2e9cbe0f9ec1c6c46e3f8d785a74 [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;
Manuel Pégourié-Gonnarde51bde02025-06-03 11:22:55 +020093 if (len != 0) {
94 memcpy(*p, buf, len);
95 }
Paul Bakker9852d002013-08-26 17:56:37 +020096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +020098}
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102{
Janos Follath24eed8d2019-11-22 13:21:35 +0000103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104 size_t len = 0;
105
106 // Write the MPI
107 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109
Gilles Peskine321a0892022-06-10 20:13:33 +0200110 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
111 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200113 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (*p < start || (size_t) (*p - start) < len) {
117 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
118 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119
120 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122
123 // DER format assumes 2s complement for numbers, so the leftmost bit
124 // should be 0 for positive numbers and 1 for negative numbers.
125 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (X->s == 1 && **p & 0x80) {
127 if (*p - start < 1) {
128 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
129 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130
131 *--(*p) = 0x00;
132 len += 1;
133 }
134
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100135 ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200136
137cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144 // Write NULL
145 //
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100146 return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147}
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
150 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151{
Janos Follath24eed8d2019-11-22 13:21:35 +0000152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153 size_t len = 0;
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
156 (const unsigned char *) oid, oid_len));
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100157 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158}
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
161 const char *oid, size_t oid_len,
162 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163{
Jethro Beekman01672442023-04-19 14:08:14 +0200164 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
165}
166
167int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
168 const char *oid, size_t oid_len,
169 size_t par_len, int has_par)
170{
Janos Follath24eed8d2019-11-22 13:21:35 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172 size_t len = 0;
173
Jethro Beekman01672442023-04-19 14:08:14 +0200174 if (has_par) {
175 if (par_len == 0) {
176 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
177 } else {
178 len += par_len;
179 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100184 return mbedtls_asn1_write_len_and_tag(p, start, len,
Dave Rodgman0c9516e2023-09-15 18:30:09 +0100185 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186}
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200189{
Paul Bakker329def32013-09-06 16:34:38 +0200190 size_t len = 0;
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (*p - start < 1) {
193 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
194 }
Paul Bakker329def32013-09-06 16:34:38 +0200195
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200196 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200197 len++;
198
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100199 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200200}
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204 size_t len = 0;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 do {
207 if (*p - start < 1) {
208 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
209 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100210 len += 1;
211 *--(*p) = val & 0xff;
212 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (**p & 0x80) {
216 if (*p - start < 1) {
217 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
218 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219 *--(*p) = 0x00;
220 len += 1;
221 }
222
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100223 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224}
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200227{
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200229}
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200232{
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200234}
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
237 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238{
Janos Follath24eed8d2019-11-22 13:21:35 +0000239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240 size_t len = 0;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
243 (const unsigned char *) text,
244 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100246 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247}
Paul Bakker598e4502013-08-25 14:46:39 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
250 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100251{
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100253}
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
256 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100257{
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
259 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100260}
261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
263 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000264{
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000266}
Paul Bakker598e4502013-08-25 14:46:39 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_asn1_write_named_bitstring(unsigned char **p,
269 const unsigned char *start,
270 const unsigned char *buf,
271 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100272{
273 size_t unused_bits, byte_len;
274 const unsigned char *cur_byte;
275 unsigned char cur_byte_shifted;
276 unsigned char bit;
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 byte_len = (bits + 7) / 8;
279 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100280
281 /*
282 * Named bitstrings require that trailing 0s are excluded in the encoding
283 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
284 * when encoding this value in the first content octet
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100287 cur_byte = buf + byte_len - 1;
288 cur_byte_shifted = *cur_byte >> unused_bits;
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100291 bit = cur_byte_shifted & 0x1;
292 cur_byte_shifted >>= 1;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100295 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100297
298 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100300 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100304 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100306 }
307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100310}
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
313 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200314{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100315 size_t len = 0;
316 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 byte_len = (bits + 7) / 8;
319 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
322 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
323 }
Paul Bakker598e4502013-08-25 14:46:39 +0200324
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100325 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200326
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100327 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100329 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
331 (*p) -= byte_len;
332 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100333 }
334
335 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200337
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100338 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200339}
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
342 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200343{
Janos Follath24eed8d2019-11-22 13:21:35 +0000344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200345 size_t len = 0;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200348
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100349 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200350}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200351
Hanno Becker44da18a2018-10-12 10:42:13 +0100352
Dave Rodgman49352832023-09-11 17:09:13 +0100353#if !defined(MBEDTLS_ASN1_PARSE_C)
Hanno Becker44da18a2018-10-12 10:42:13 +0100354/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
355 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
356static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_asn1_named_data *list,
358 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100359{
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 while (list != NULL) {
361 if (list->oid.len == len &&
362 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100363 break;
364 }
365
366 list = list->next;
367 }
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100370}
Dave Rodgman49352832023-09-11 17:09:13 +0100371#else
372#define asn1_find_named_data(list, oid, len) \
373 ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
374#endif
Hanno Becker44da18a2018-10-12 10:42:13 +0100375
376mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_asn1_named_data **head,
378 const char *oid, size_t oid_len,
379 const unsigned char *val,
380 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200385 // Add new entry if not present yet based on OID
386 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
388 sizeof(mbedtls_asn1_named_data));
389 if (cur == NULL) {
390 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200391 }
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 cur->oid.len = oid_len;
394 cur->oid.p = mbedtls_calloc(1, oid_len);
395 if (cur->oid.p == NULL) {
396 mbedtls_free(cur);
397 return NULL;
398 }
399
400 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100401
Paul Bakker59ba59f2013-09-09 11:26:00 +0200402 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (val_len != 0) {
404 cur->val.p = mbedtls_calloc(1, val_len);
405 if (cur->val.p == NULL) {
406 mbedtls_free(cur->oid.p);
407 mbedtls_free(cur);
408 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100409 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200410 }
411
Paul Bakker59ba59f2013-09-09 11:26:00 +0200412 cur->next = *head;
413 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 } else if (val_len == 0) {
415 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100416 cur->val.p = NULL;
Manuel Pégourié-Gonnard2df7ab72025-05-26 10:42:14 +0200417 cur->val.len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100419 /*
420 * Enlarge existing value buffer if needed
421 * Preserve old data until the allocation succeeded, to leave list in
422 * a consistent state in case allocation fails.
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 void *p = mbedtls_calloc(1, val_len);
425 if (p == NULL) {
426 return NULL;
427 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100430 cur->val.p = p;
431 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200432 }
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (val != NULL && val_len != 0) {
435 memcpy(cur->val.p, val, val_len);
436 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_ASN1_WRITE_C */