blob: 114091d6357d1af9a389dc5c5e36ed1191845a92 [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
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010010#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000013#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000014
Rich Evans00ab4702015-02-06 13:43:58 +000015#include <string.h>
16
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020018
Dave Rodgman49352832023-09-11 17:09:13 +010019#if defined(MBEDTLS_ASN1_PARSE_C)
20#include "mbedtls/asn1.h"
21#endif
22
Gilles Peskine449bd832023-01-11 14:50:10 +010023int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000024{
Azim Khan45b79cf2018-05-23 16:55:16 +010025#if SIZE_MAX > 0xFFFFFFFF
Dave Rodgman3bbedf62023-09-11 16:06:28 +010026 if (len > 0xFFFFFFFF) {
27 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
28 }
Azim Khan45b79cf2018-05-23 16:55:16 +010029#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010030
Dave Rodgman9f366b02023-09-11 15:47:00 +010031 int required = 1;
Dave Rodgman33287ae2023-09-11 17:03:22 +010032
33 if (len >= 0x80) {
Dave Rodgman9f366b02023-09-11 15:47:00 +010034 for (size_t l = len; l != 0; l >>= 8) {
35 required++;
36 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010037 }
38
Dave Rodgman9f366b02023-09-11 15:47:00 +010039 if (required > (*p - start)) {
40 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
41 }
42
43 do {
44 *--(*p) = MBEDTLS_BYTE_0(len);
45 len >>= 8;
46 } while (len);
47
48 if (required > 1) {
Dave Rodgman33287ae2023-09-11 17:03:22 +010049 *--(*p) = (unsigned char) (0x80 + required - 1);
Dave Rodgman9f366b02023-09-11 15:47:00 +010050 }
51
52 return required;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000053}
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056{
Gilles Peskine449bd832023-01-11 14:50:10 +010057 if (*p - start < 1) {
58 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
59 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060
61 *--(*p) = tag;
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064}
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010065#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010067#if defined(MBEDTLS_ASN1_WRITE_C)
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +010068static int mbedtls_asn1_write_len_and_tag(unsigned char **p,
Dave Rodgman0c9516e2023-09-15 18:30:09 +010069 const unsigned char *start,
70 size_t len,
71 unsigned char tag)
Dave Rodgmancf5f7462023-09-11 16:27:34 +010072{
73 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
74
75 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
76 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
77
Dave Rodgmandc669a12023-09-11 18:39:57 +010078 return (int) len;
Dave Rodgmancf5f7462023-09-11 16:27:34 +010079}
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
82 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020083{
84 size_t len = 0;
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (*p < start || (size_t) (*p - start) < size) {
87 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
88 }
Paul Bakker9852d002013-08-26 17:56:37 +020089
90 len = size;
91 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +020093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +020095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010098int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099{
Janos Follath24eed8d2019-11-22 13:21:35 +0000100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101 size_t len = 0;
102
103 // Write the MPI
104 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106
Gilles Peskine321a0892022-06-10 20:13:33 +0200107 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
108 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200110 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (*p < start || (size_t) (*p - start) < len) {
114 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
115 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116
117 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119
120 // DER format assumes 2s complement for numbers, so the leftmost bit
121 // should be 0 for positive numbers and 1 for negative numbers.
122 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (X->s == 1 && **p & 0x80) {
124 if (*p - start < 1) {
125 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
126 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127
128 *--(*p) = 0x00;
129 len += 1;
130 }
131
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100132 ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200133
134cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141 // Write NULL
142 //
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100143 return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144}
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
147 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148{
Janos Follath24eed8d2019-11-22 13:21:35 +0000149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150 size_t len = 0;
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
153 (const unsigned char *) oid, oid_len));
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100154 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155}
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
158 const char *oid, size_t oid_len,
159 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160{
Jethro Beekman01672442023-04-19 14:08:14 +0200161 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
162}
163
164int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
165 const char *oid, size_t oid_len,
166 size_t par_len, int has_par)
167{
Janos Follath24eed8d2019-11-22 13:21:35 +0000168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169 size_t len = 0;
170
Jethro Beekman01672442023-04-19 14:08:14 +0200171 if (has_par) {
172 if (par_len == 0) {
173 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
174 } else {
175 len += par_len;
176 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100181 return mbedtls_asn1_write_len_and_tag(p, start, len,
Dave Rodgman0c9516e2023-09-15 18:30:09 +0100182 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183}
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200186{
Paul Bakker329def32013-09-06 16:34:38 +0200187 size_t len = 0;
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (*p - start < 1) {
190 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
191 }
Paul Bakker329def32013-09-06 16:34:38 +0200192
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200193 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200194 len++;
195
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100196 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200197}
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000201 size_t len = 0;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 do {
204 if (*p - start < 1) {
205 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
206 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100207 len += 1;
208 *--(*p) = val & 0xff;
209 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (**p & 0x80) {
213 if (*p - start < 1) {
214 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
215 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 *--(*p) = 0x00;
217 len += 1;
218 }
219
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100220 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221}
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200224{
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200229{
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200231}
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
234 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235{
Janos Follath24eed8d2019-11-22 13:21:35 +0000236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237 size_t len = 0;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
240 (const unsigned char *) text,
241 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100243 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244}
Paul Bakker598e4502013-08-25 14:46:39 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
247 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100248{
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100250}
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
253 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100254{
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
256 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100257}
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
260 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000261{
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000263}
Paul Bakker598e4502013-08-25 14:46:39 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265int mbedtls_asn1_write_named_bitstring(unsigned char **p,
266 const unsigned char *start,
267 const unsigned char *buf,
268 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100269{
270 size_t unused_bits, byte_len;
271 const unsigned char *cur_byte;
272 unsigned char cur_byte_shifted;
273 unsigned char bit;
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 byte_len = (bits + 7) / 8;
276 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100277
278 /*
279 * Named bitstrings require that trailing 0s are excluded in the encoding
280 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
281 * when encoding this value in the first content octet
282 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100284 cur_byte = buf + byte_len - 1;
285 cur_byte_shifted = *cur_byte >> unused_bits;
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100288 bit = cur_byte_shifted & 0x1;
289 cur_byte_shifted >>= 1;
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100292 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100294
295 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100297 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100301 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100303 }
304 }
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100307}
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
310 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200311{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100312 size_t len = 0;
313 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 byte_len = (bits + 7) / 8;
316 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
319 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
320 }
Paul Bakker598e4502013-08-25 14:46:39 +0200321
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100322 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200323
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100324 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100326 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
328 (*p) -= byte_len;
329 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100330 }
331
332 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200334
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100335 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200336}
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
339 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200340{
Janos Follath24eed8d2019-11-22 13:21:35 +0000341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200342 size_t len = 0;
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200345
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100346 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200347}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200348
Hanno Becker44da18a2018-10-12 10:42:13 +0100349
Dave Rodgman49352832023-09-11 17:09:13 +0100350#if !defined(MBEDTLS_ASN1_PARSE_C)
Hanno Becker44da18a2018-10-12 10:42:13 +0100351/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
352 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
353static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 mbedtls_asn1_named_data *list,
355 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100356{
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 while (list != NULL) {
358 if (list->oid.len == len &&
359 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100360 break;
361 }
362
363 list = list->next;
364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100367}
Dave Rodgman49352832023-09-11 17:09:13 +0100368#else
369#define asn1_find_named_data(list, oid, len) \
370 ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
371#endif
Hanno Becker44da18a2018-10-12 10:42:13 +0100372
373mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_asn1_named_data **head,
375 const char *oid, size_t oid_len,
376 const unsigned char *val,
377 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200382 // Add new entry if not present yet based on OID
383 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
385 sizeof(mbedtls_asn1_named_data));
386 if (cur == NULL) {
387 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200388 }
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 cur->oid.len = oid_len;
391 cur->oid.p = mbedtls_calloc(1, oid_len);
392 if (cur->oid.p == NULL) {
393 mbedtls_free(cur);
394 return NULL;
395 }
396
397 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100398
Paul Bakker59ba59f2013-09-09 11:26:00 +0200399 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (val_len != 0) {
401 cur->val.p = mbedtls_calloc(1, val_len);
402 if (cur->val.p == NULL) {
403 mbedtls_free(cur->oid.p);
404 mbedtls_free(cur);
405 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100406 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200407 }
408
Paul Bakker59ba59f2013-09-09 11:26:00 +0200409 cur->next = *head;
410 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 } else if (val_len == 0) {
412 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100413 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100415 /*
416 * Enlarge existing value buffer if needed
417 * Preserve old data until the allocation succeeded, to leave list in
418 * a consistent state in case allocation fails.
419 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 void *p = mbedtls_calloc(1, val_len);
421 if (p == NULL) {
422 return NULL;
423 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100426 cur->val.p = p;
427 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200428 }
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (val != NULL && val_len != 0) {
431 memcpy(cur->val.p, val, val_len);
432 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#endif /* MBEDTLS_ASN1_WRITE_C */