blob: 4887f1551a665694ed4dd0429706e20312999b9e [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
Gilles Peskine449bd832023-01-11 14:50:10 +010031int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032{
Azim Khan45b79cf2018-05-23 16:55:16 +010033#if SIZE_MAX > 0xFFFFFFFF
Dave Rodgman3bbedf62023-09-11 16:06:28 +010034 if (len > 0xFFFFFFFF) {
35 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
36 }
Azim Khan45b79cf2018-05-23 16:55:16 +010037#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010038
Dave Rodgman9f366b02023-09-11 15:47:00 +010039 int required = 1;
40 if (len < 0x80) {
41 required = 1;
42 } else {
43 for (size_t l = len; l != 0; l >>= 8) {
44 required++;
45 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010046 }
47
Dave Rodgman9f366b02023-09-11 15:47:00 +010048 if (required > (*p - start)) {
49 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
50 }
51
52 do {
53 *--(*p) = MBEDTLS_BYTE_0(len);
54 len >>= 8;
55 } while (len);
56
57 if (required > 1) {
Dave Rodgman3bbedf62023-09-11 16:06:28 +010058 *--(*p) = (unsigned char) (required + 0x7f);
Dave Rodgman9f366b02023-09-11 15:47:00 +010059 }
60
61 return required;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062}
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000065{
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (*p - start < 1) {
67 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
68 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000069
70 *--(*p) = tag;
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073}
74
Dave Rodgmancf5f7462023-09-11 16:27:34 +010075static int mbedtls_write_len_and_tag(unsigned char **p,
76 const unsigned char *start,
77 int len,
78 unsigned char tag)
79{
80 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
81
82 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
83 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
84
85 return len;
86}
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
89 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020090{
91 size_t len = 0;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (*p < start || (size_t) (*p - start) < size) {
94 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
95 }
Paul Bakker9852d002013-08-26 17:56:37 +020096
97 len = size;
98 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +0200102}
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100105int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106{
Janos Follath24eed8d2019-11-22 13:21:35 +0000107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108 size_t len = 0;
109
110 // Write the MPI
111 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113
Gilles Peskine321a0892022-06-10 20:13:33 +0200114 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
115 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200117 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (*p < start || (size_t) (*p - start) < len) {
121 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
122 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123
124 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126
127 // DER format assumes 2s complement for numbers, so the leftmost bit
128 // should be 0 for positive numbers and 1 for negative numbers.
129 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 if (X->s == 1 && **p & 0x80) {
131 if (*p - start < 1) {
132 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
133 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134
135 *--(*p) = 0x00;
136 len += 1;
137 }
138
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100139 ret = mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200140
141cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148 // Write NULL
149 //
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100150 return mbedtls_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151}
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
154 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155{
Janos Follath24eed8d2019-11-22 13:21:35 +0000156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157 size_t len = 0;
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
160 (const unsigned char *) oid, oid_len));
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100161 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
165 const char *oid, size_t oid_len,
166 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167{
Jethro Beekman01672442023-04-19 14:08:14 +0200168 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
169}
170
171int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
172 const char *oid, size_t oid_len,
173 size_t par_len, int has_par)
174{
Janos Follath24eed8d2019-11-22 13:21:35 +0000175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176 size_t len = 0;
177
Jethro Beekman01672442023-04-19 14:08:14 +0200178 if (has_par) {
179 if (par_len == 0) {
180 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
181 } else {
182 len += par_len;
183 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100188 return mbedtls_write_len_and_tag(p, start, len,
189 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190}
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200193{
Paul Bakker329def32013-09-06 16:34:38 +0200194 size_t len = 0;
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (*p - start < 1) {
197 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
198 }
Paul Bakker329def32013-09-06 16:34:38 +0200199
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200200 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200201 len++;
202
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100203 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200204}
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208 size_t len = 0;
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 do {
211 if (*p - start < 1) {
212 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
213 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100214 len += 1;
215 *--(*p) = val & 0xff;
216 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (**p & 0x80) {
220 if (*p - start < 1) {
221 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
222 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000223 *--(*p) = 0x00;
224 len += 1;
225 }
226
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100227 return mbedtls_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228}
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200236{
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
241 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242{
Janos Follath24eed8d2019-11-22 13:21:35 +0000243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244 size_t len = 0;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
247 (const unsigned char *) text,
248 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100250 return mbedtls_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000251}
Paul Bakker598e4502013-08-25 14:46:39 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253int mbedtls_asn1_write_utf8_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_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100257}
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
260 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100261{
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
263 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100264}
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
267 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000268{
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000270}
Paul Bakker598e4502013-08-25 14:46:39 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272int mbedtls_asn1_write_named_bitstring(unsigned char **p,
273 const unsigned char *start,
274 const unsigned char *buf,
275 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100276{
277 size_t unused_bits, byte_len;
278 const unsigned char *cur_byte;
279 unsigned char cur_byte_shifted;
280 unsigned char bit;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 byte_len = (bits + 7) / 8;
283 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100284
285 /*
286 * Named bitstrings require that trailing 0s are excluded in the encoding
287 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
288 * when encoding this value in the first content octet
289 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100291 cur_byte = buf + byte_len - 1;
292 cur_byte_shifted = *cur_byte >> unused_bits;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100295 bit = cur_byte_shifted & 0x1;
296 cur_byte_shifted >>= 1;
297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100299 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100301
302 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100304 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100308 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100310 }
311 }
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100314}
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
317 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200318{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100319 size_t len = 0;
320 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 byte_len = (bits + 7) / 8;
323 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
326 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
327 }
Paul Bakker598e4502013-08-25 14:46:39 +0200328
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100329 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200330
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100331 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100333 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
335 (*p) -= byte_len;
336 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100337 }
338
339 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200341
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100342 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200343}
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
346 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200347{
Janos Follath24eed8d2019-11-22 13:21:35 +0000348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200349 size_t len = 0;
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200352
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100353 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200354}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200355
Hanno Becker44da18a2018-10-12 10:42:13 +0100356
357/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
358 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
359static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 mbedtls_asn1_named_data *list,
361 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100362{
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 while (list != NULL) {
364 if (list->oid.len == len &&
365 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100366 break;
367 }
368
369 list = list->next;
370 }
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100373}
374
375mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_asn1_named_data **head,
377 const char *oid, size_t oid_len,
378 const unsigned char *val,
379 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200384 // Add new entry if not present yet based on OID
385 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
387 sizeof(mbedtls_asn1_named_data));
388 if (cur == NULL) {
389 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200390 }
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 cur->oid.len = oid_len;
393 cur->oid.p = mbedtls_calloc(1, oid_len);
394 if (cur->oid.p == NULL) {
395 mbedtls_free(cur);
396 return NULL;
397 }
398
399 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100400
Paul Bakker59ba59f2013-09-09 11:26:00 +0200401 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (val_len != 0) {
403 cur->val.p = mbedtls_calloc(1, val_len);
404 if (cur->val.p == NULL) {
405 mbedtls_free(cur->oid.p);
406 mbedtls_free(cur);
407 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100408 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200409 }
410
Paul Bakker59ba59f2013-09-09 11:26:00 +0200411 cur->next = *head;
412 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 } else if (val_len == 0) {
414 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100415 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100417 /*
418 * Enlarge existing value buffer if needed
419 * Preserve old data until the allocation succeeded, to leave list in
420 * a consistent state in case allocation fails.
421 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 void *p = mbedtls_calloc(1, val_len);
423 if (p == NULL) {
424 return NULL;
425 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100428 cur->val.p = p;
429 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200430 }
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (val != NULL && val_len != 0) {
433 memcpy(cur->val.p, val, val_len);
434 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_ASN1_WRITE_C */