blob: 7d533491f3bfb945831ce725d350c43b019d5bbb [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;
Dave Rodgman33287ae2023-09-11 17:03:22 +010040
41 if (len >= 0x80) {
Dave Rodgman9f366b02023-09-11 15:47:00 +010042 for (size_t l = len; l != 0; l >>= 8) {
43 required++;
44 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010045 }
46
Dave Rodgman9f366b02023-09-11 15:47:00 +010047 if (required > (*p - start)) {
48 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
49 }
50
51 do {
52 *--(*p) = MBEDTLS_BYTE_0(len);
53 len >>= 8;
54 } while (len);
55
56 if (required > 1) {
Dave Rodgman33287ae2023-09-11 17:03:22 +010057 *--(*p) = (unsigned char) (0x80 + required - 1);
Dave Rodgman9f366b02023-09-11 15:47:00 +010058 }
59
60 return required;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061}
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064{
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (*p - start < 1) {
66 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
67 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
69 *--(*p) = tag;
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000072}
73
Dave Rodgmancf5f7462023-09-11 16:27:34 +010074static int mbedtls_write_len_and_tag(unsigned char **p,
75 const unsigned char *start,
76 int len,
77 unsigned char tag)
78{
79 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
80
81 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
82 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
83
84 return len;
85}
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
88 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020089{
90 size_t len = 0;
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (*p < start || (size_t) (*p - start) < size) {
93 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
94 }
Paul Bakker9852d002013-08-26 17:56:37 +020095
96 len = size;
97 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010098 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +0200101}
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105{
Janos Follath24eed8d2019-11-22 13:21:35 +0000106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107 size_t len = 0;
108
109 // Write the MPI
110 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112
Gilles Peskine321a0892022-06-10 20:13:33 +0200113 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
114 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200116 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (*p < start || (size_t) (*p - start) < len) {
120 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
121 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122
123 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125
126 // DER format assumes 2s complement for numbers, so the leftmost bit
127 // should be 0 for positive numbers and 1 for negative numbers.
128 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (X->s == 1 && **p & 0x80) {
130 if (*p - start < 1) {
131 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
132 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133
134 *--(*p) = 0x00;
135 len += 1;
136 }
137
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100138 ret = mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200139
140cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147 // Write NULL
148 //
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100149 return mbedtls_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150}
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
153 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154{
Janos Follath24eed8d2019-11-22 13:21:35 +0000155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156 size_t len = 0;
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
159 (const unsigned char *) oid, oid_len));
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100160 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161}
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
164 const char *oid, size_t oid_len,
165 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166{
Jethro Beekman01672442023-04-19 14:08:14 +0200167 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
168}
169
170int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
171 const char *oid, size_t oid_len,
172 size_t par_len, int has_par)
173{
Janos Follath24eed8d2019-11-22 13:21:35 +0000174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175 size_t len = 0;
176
Jethro Beekman01672442023-04-19 14:08:14 +0200177 if (has_par) {
178 if (par_len == 0) {
179 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
180 } else {
181 len += par_len;
182 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100187 return mbedtls_write_len_and_tag(p, start, len,
188 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189}
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200192{
Paul Bakker329def32013-09-06 16:34:38 +0200193 size_t len = 0;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (*p - start < 1) {
196 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
197 }
Paul Bakker329def32013-09-06 16:34:38 +0200198
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200199 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200200 len++;
201
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100202 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200203}
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000206{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207 size_t len = 0;
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 do {
210 if (*p - start < 1) {
211 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
212 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100213 len += 1;
214 *--(*p) = val & 0xff;
215 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (**p & 0x80) {
219 if (*p - start < 1) {
220 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
221 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222 *--(*p) = 0x00;
223 len += 1;
224 }
225
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100226 return mbedtls_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_asn1_write_int(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_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200232}
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200235{
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200237}
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
240 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241{
Janos Follath24eed8d2019-11-22 13:21:35 +0000242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243 size_t len = 0;
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
246 (const unsigned char *) text,
247 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100249 return mbedtls_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250}
Paul Bakker598e4502013-08-25 14:46:39 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252int mbedtls_asn1_write_utf8_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_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100256}
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
259 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100260{
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
262 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100263}
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
266 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000267{
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000269}
Paul Bakker598e4502013-08-25 14:46:39 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271int mbedtls_asn1_write_named_bitstring(unsigned char **p,
272 const unsigned char *start,
273 const unsigned char *buf,
274 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100275{
276 size_t unused_bits, byte_len;
277 const unsigned char *cur_byte;
278 unsigned char cur_byte_shifted;
279 unsigned char bit;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 byte_len = (bits + 7) / 8;
282 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100283
284 /*
285 * Named bitstrings require that trailing 0s are excluded in the encoding
286 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
287 * when encoding this value in the first content octet
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100290 cur_byte = buf + byte_len - 1;
291 cur_byte_shifted = *cur_byte >> unused_bits;
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100294 bit = cur_byte_shifted & 0x1;
295 cur_byte_shifted >>= 1;
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (bit != 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
301 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100303 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100307 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100309 }
310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100313}
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
316 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200317{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100318 size_t len = 0;
319 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 byte_len = (bits + 7) / 8;
322 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
325 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
326 }
Paul Bakker598e4502013-08-25 14:46:39 +0200327
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100328 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200329
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100330 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100332 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
334 (*p) -= byte_len;
335 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100336 }
337
338 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200340
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100341 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200342}
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
345 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200346{
Janos Follath24eed8d2019-11-22 13:21:35 +0000347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200348 size_t len = 0;
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200351
Dave Rodgmancf5f7462023-09-11 16:27:34 +0100352 return mbedtls_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200353}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200354
Hanno Becker44da18a2018-10-12 10:42:13 +0100355
356/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
357 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
358static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 mbedtls_asn1_named_data *list,
360 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100361{
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 while (list != NULL) {
363 if (list->oid.len == len &&
364 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100365 break;
366 }
367
368 list = list->next;
369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100372}
373
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 */