blob: 2e7c4a96f1ccdf969b48c18963e35f41b905aef4 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000025#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020030
Dave Rodgman49352832023-09-11 17:09:13 +010031#if defined(MBEDTLS_ASN1_PARSE_C)
32#include "mbedtls/asn1.h"
33#endif
34
Gilles Peskine449bd832023-01-11 14:50:10 +010035int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000036{
Azim Khan45b79cf2018-05-23 16:55:16 +010037#if SIZE_MAX > 0xFFFFFFFF
Dave Rodgman3bbedf62023-09-11 16:06:28 +010038 if (len > 0xFFFFFFFF) {
39 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
40 }
Azim Khan45b79cf2018-05-23 16:55:16 +010041#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010042
Dave Rodgman9f366b02023-09-11 15:47:00 +010043 int required = 1;
Dave Rodgman33287ae2023-09-11 17:03:22 +010044
45 if (len >= 0x80) {
Dave Rodgman9f366b02023-09-11 15:47:00 +010046 for (size_t l = len; l != 0; l >>= 8) {
47 required++;
48 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010049 }
50
Dave Rodgman9f366b02023-09-11 15:47:00 +010051 if (required > (*p - start)) {
52 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
53 }
54
55 do {
56 *--(*p) = MBEDTLS_BYTE_0(len);
57 len >>= 8;
58 } while (len);
59
60 if (required > 1) {
Dave Rodgman33287ae2023-09-11 17:03:22 +010061 *--(*p) = (unsigned char) (0x80 + required - 1);
Dave Rodgman9f366b02023-09-11 15:47:00 +010062 }
63
64 return required;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000065}
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068{
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (*p - start < 1) {
70 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
71 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000072
73 *--(*p) = tag;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076}
77
Dave Rodgmancf5f7462023-09-11 16:27:34 +010078static int mbedtls_write_len_and_tag(unsigned char **p,
79 const unsigned char *start,
80 int len,
81 unsigned char tag)
82{
83 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
84
85 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
86 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
87
88 return len;
89}
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
92 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020093{
94 size_t len = 0;
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (*p < start || (size_t) (*p - start) < size) {
97 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
98 }
Paul Bakker9852d002013-08-26 17:56:37 +020099
100 len = size;
101 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +0200105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100108int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109{
Janos Follath24eed8d2019-11-22 13:21:35 +0000110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111 size_t len = 0;
112
113 // Write the MPI
114 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116
Gilles Peskine321a0892022-06-10 20:13:33 +0200117 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
118 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200120 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (*p < start || (size_t) (*p - start) < len) {
124 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
125 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126
127 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129
130 // DER format assumes 2s complement for numbers, so the leftmost bit
131 // should be 0 for positive numbers and 1 for negative numbers.
132 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (X->s == 1 && **p & 0x80) {
134 if (*p - start < 1) {
135 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
136 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137
138 *--(*p) = 0x00;
139 len += 1;
140 }
141
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100142 ret = mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200143
144cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 // Write NULL
152 //
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100153 return mbedtls_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154}
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
157 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158{
Janos Follath24eed8d2019-11-22 13:21:35 +0000159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160 size_t len = 0;
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
163 (const unsigned char *) oid, oid_len));
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100164 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165}
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
168 const char *oid, size_t oid_len,
169 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170{
Jethro Beekman01672442023-04-19 14:08:14 +0200171 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
172}
173
174int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
175 const char *oid, size_t oid_len,
176 size_t par_len, int has_par)
177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179 size_t len = 0;
180
Jethro Beekman01672442023-04-19 14:08:14 +0200181 if (has_par) {
182 if (par_len == 0) {
183 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
184 } else {
185 len += par_len;
186 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100191 return mbedtls_write_len_and_tag(p, start, len,
192 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193}
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200196{
Paul Bakker329def32013-09-06 16:34:38 +0200197 size_t len = 0;
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (*p - start < 1) {
200 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
201 }
Paul Bakker329def32013-09-06 16:34:38 +0200202
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200203 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200204 len++;
205
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100206 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200207}
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211 size_t len = 0;
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 do {
214 if (*p - start < 1) {
215 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
216 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100217 len += 1;
218 *--(*p) = val & 0xff;
219 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (**p & 0x80) {
223 if (*p - start < 1) {
224 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
225 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226 *--(*p) = 0x00;
227 len += 1;
228 }
229
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100230 return mbedtls_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231}
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200234{
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200236}
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200239{
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200241}
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
244 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245{
Janos Follath24eed8d2019-11-22 13:21:35 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 size_t len = 0;
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
250 (const unsigned char *) text,
251 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000252
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100253 return mbedtls_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254}
Paul Bakker598e4502013-08-25 14:46:39 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
257 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100258{
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100260}
261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
263 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100264{
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
266 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100267}
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
270 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000271{
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000273}
Paul Bakker598e4502013-08-25 14:46:39 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275int mbedtls_asn1_write_named_bitstring(unsigned char **p,
276 const unsigned char *start,
277 const unsigned char *buf,
278 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100279{
280 size_t unused_bits, byte_len;
281 const unsigned char *cur_byte;
282 unsigned char cur_byte_shifted;
283 unsigned char bit;
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 byte_len = (bits + 7) / 8;
286 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100287
288 /*
289 * Named bitstrings require that trailing 0s are excluded in the encoding
290 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
291 * when encoding this value in the first content octet
292 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100294 cur_byte = buf + byte_len - 1;
295 cur_byte_shifted = *cur_byte >> unused_bits;
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100298 bit = cur_byte_shifted & 0x1;
299 cur_byte_shifted >>= 1;
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100302 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100304
305 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100307 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100311 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100313 }
314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100317}
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
320 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200321{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100322 size_t len = 0;
323 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 byte_len = (bits + 7) / 8;
326 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
329 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
330 }
Paul Bakker598e4502013-08-25 14:46:39 +0200331
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100332 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200333
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100334 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100336 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
338 (*p) -= byte_len;
339 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100340 }
341
342 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200344
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100345 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200346}
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
349 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200350{
Janos Follath24eed8d2019-11-22 13:21:35 +0000351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200352 size_t len = 0;
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200355
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100356 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200357}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200358
Hanno Becker44da18a2018-10-12 10:42:13 +0100359
Dave Rodgman49352832023-09-11 17:09:13 +0100360#if !defined(MBEDTLS_ASN1_PARSE_C)
Hanno Becker44da18a2018-10-12 10:42:13 +0100361/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
362 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
363static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_asn1_named_data *list,
365 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100366{
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 while (list != NULL) {
368 if (list->oid.len == len &&
369 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100370 break;
371 }
372
373 list = list->next;
374 }
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100377}
Dave Rodgman49352832023-09-11 17:09:13 +0100378#else
379#define asn1_find_named_data(list, oid, len) \
380 ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
381#endif
Hanno Becker44da18a2018-10-12 10:42:13 +0100382
383mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_asn1_named_data **head,
385 const char *oid, size_t oid_len,
386 const unsigned char *val,
387 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200392 // Add new entry if not present yet based on OID
393 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
395 sizeof(mbedtls_asn1_named_data));
396 if (cur == NULL) {
397 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200398 }
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 cur->oid.len = oid_len;
401 cur->oid.p = mbedtls_calloc(1, oid_len);
402 if (cur->oid.p == NULL) {
403 mbedtls_free(cur);
404 return NULL;
405 }
406
407 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100408
Paul Bakker59ba59f2013-09-09 11:26:00 +0200409 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (val_len != 0) {
411 cur->val.p = mbedtls_calloc(1, val_len);
412 if (cur->val.p == NULL) {
413 mbedtls_free(cur->oid.p);
414 mbedtls_free(cur);
415 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100416 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200417 }
418
Paul Bakker59ba59f2013-09-09 11:26:00 +0200419 cur->next = *head;
420 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 } else if (val_len == 0) {
422 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100423 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100425 /*
426 * Enlarge existing value buffer if needed
427 * Preserve old data until the allocation succeeded, to leave list in
428 * a consistent state in case allocation fails.
429 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 void *p = mbedtls_calloc(1, val_len);
431 if (p == NULL) {
432 return NULL;
433 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100436 cur->val.p = p;
437 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200438 }
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (val != NULL && val_len != 0) {
441 memcpy(cur->val.p, val, val_len);
442 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#endif /* MBEDTLS_ASN1_WRITE_C */