blob: 2e9b98ad58de63dba26a377f3320bffc82b41f0e [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
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010022#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_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}
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010077#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010079#if defined(MBEDTLS_ASN1_WRITE_C)
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +010080static int mbedtls_asn1_write_len_and_tag(unsigned char **p,
Dave Rodgman0c9516e2023-09-15 18:30:09 +010081 const unsigned char *start,
82 size_t len,
83 unsigned char tag)
Dave Rodgmancf5f7462023-09-11 16:27:34 +010084{
85 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
86
87 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
88 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
89
Dave Rodgmandc669a12023-09-11 18:39:57 +010090 return (int) len;
Dave Rodgmancf5f7462023-09-11 16:27:34 +010091}
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
94 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020095{
96 size_t len = 0;
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (*p < start || (size_t) (*p - start) < size) {
99 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
100 }
Paul Bakker9852d002013-08-26 17:56:37 +0200101
102 len = size;
103 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +0200107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100110int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111{
Janos Follath24eed8d2019-11-22 13:21:35 +0000112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113 size_t len = 0;
114
115 // Write the MPI
116 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118
Gilles Peskine321a0892022-06-10 20:13:33 +0200119 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
120 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200122 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (*p < start || (size_t) (*p - start) < len) {
126 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
127 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128
129 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131
132 // DER format assumes 2s complement for numbers, so the leftmost bit
133 // should be 0 for positive numbers and 1 for negative numbers.
134 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (X->s == 1 && **p & 0x80) {
136 if (*p - start < 1) {
137 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
138 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139
140 *--(*p) = 0x00;
141 len += 1;
142 }
143
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100144 ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
Paul Bakker3d8fb632014-04-17 12:42:41 +0200145
146cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153 // Write NULL
154 //
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100155 return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156}
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
159 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160{
Janos Follath24eed8d2019-11-22 13:21:35 +0000161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162 size_t len = 0;
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
165 (const unsigned char *) oid, oid_len));
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100166 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167}
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
170 const char *oid, size_t oid_len,
171 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172{
Jethro Beekman01672442023-04-19 14:08:14 +0200173 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
174}
175
176int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
177 const char *oid, size_t oid_len,
178 size_t par_len, int has_par)
179{
Janos Follath24eed8d2019-11-22 13:21:35 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181 size_t len = 0;
182
Jethro Beekman01672442023-04-19 14:08:14 +0200183 if (has_par) {
184 if (par_len == 0) {
185 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
186 } else {
187 len += par_len;
188 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100193 return mbedtls_asn1_write_len_and_tag(p, start, len,
Dave Rodgman0c9516e2023-09-15 18:30:09 +0100194 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195}
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200198{
Paul Bakker329def32013-09-06 16:34:38 +0200199 size_t len = 0;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (*p - start < 1) {
202 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
203 }
Paul Bakker329def32013-09-06 16:34:38 +0200204
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200205 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200206 len++;
207
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100208 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
Paul Bakker329def32013-09-06 16:34:38 +0200209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212{
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213 size_t len = 0;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 do {
216 if (*p - start < 1) {
217 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
218 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100219 len += 1;
220 *--(*p) = val & 0xff;
221 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (**p & 0x80) {
225 if (*p - start < 1) {
226 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
227 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228 *--(*p) = 0x00;
229 len += 1;
230 }
231
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100232 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_asn1_write_int(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_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200241{
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200243}
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
246 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247{
Janos Follath24eed8d2019-11-22 13:21:35 +0000248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 size_t len = 0;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
252 (const unsigned char *) text,
253 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100255 return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256}
Paul Bakker598e4502013-08-25 14:46:39 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_asn1_write_utf8_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_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100262}
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
265 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100266{
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
268 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100269}
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
272 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000273{
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000275}
Paul Bakker598e4502013-08-25 14:46:39 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277int mbedtls_asn1_write_named_bitstring(unsigned char **p,
278 const unsigned char *start,
279 const unsigned char *buf,
280 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100281{
282 size_t unused_bits, byte_len;
283 const unsigned char *cur_byte;
284 unsigned char cur_byte_shifted;
285 unsigned char bit;
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 byte_len = (bits + 7) / 8;
288 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100289
290 /*
291 * Named bitstrings require that trailing 0s are excluded in the encoding
292 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
293 * when encoding this value in the first content octet
294 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100296 cur_byte = buf + byte_len - 1;
297 cur_byte_shifted = *cur_byte >> unused_bits;
298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100300 bit = cur_byte_shifted & 0x1;
301 cur_byte_shifted >>= 1;
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (bit != 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
307 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100309 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100313 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100315 }
316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100319}
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
322 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200323{
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100324 size_t len = 0;
325 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 byte_len = (bits + 7) / 8;
328 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
331 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
332 }
Paul Bakker598e4502013-08-25 14:46:39 +0200333
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100334 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200335
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100336 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100338 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
340 (*p) -= byte_len;
341 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100342 }
343
344 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200346
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100347 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200348}
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
351 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200352{
Janos Follath24eed8d2019-11-22 13:21:35 +0000353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200354 size_t len = 0;
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200357
Dave Rodgmanecdfc1c2023-09-15 18:00:37 +0100358 return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
Paul Bakker598e4502013-08-25 14:46:39 +0200359}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200360
Hanno Becker44da18a2018-10-12 10:42:13 +0100361
Dave Rodgman49352832023-09-11 17:09:13 +0100362#if !defined(MBEDTLS_ASN1_PARSE_C)
Hanno Becker44da18a2018-10-12 10:42:13 +0100363/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
364 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
365static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_asn1_named_data *list,
367 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100368{
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 while (list != NULL) {
370 if (list->oid.len == len &&
371 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100372 break;
373 }
374
375 list = list->next;
376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100379}
Dave Rodgman49352832023-09-11 17:09:13 +0100380#else
381#define asn1_find_named_data(list, oid, len) \
382 ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
383#endif
Hanno Becker44da18a2018-10-12 10:42:13 +0100384
385mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_asn1_named_data **head,
387 const char *oid, size_t oid_len,
388 const unsigned char *val,
389 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200394 // Add new entry if not present yet based on OID
395 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
397 sizeof(mbedtls_asn1_named_data));
398 if (cur == NULL) {
399 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200400 }
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 cur->oid.len = oid_len;
403 cur->oid.p = mbedtls_calloc(1, oid_len);
404 if (cur->oid.p == NULL) {
405 mbedtls_free(cur);
406 return NULL;
407 }
408
409 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100410
Paul Bakker59ba59f2013-09-09 11:26:00 +0200411 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (val_len != 0) {
413 cur->val.p = mbedtls_calloc(1, val_len);
414 if (cur->val.p == NULL) {
415 mbedtls_free(cur->oid.p);
416 mbedtls_free(cur);
417 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100418 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200419 }
420
Paul Bakker59ba59f2013-09-09 11:26:00 +0200421 cur->next = *head;
422 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 } else if (val_len == 0) {
424 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100425 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100427 /*
428 * Enlarge existing value buffer if needed
429 * Preserve old data until the allocation succeeded, to leave list in
430 * a consistent state in case allocation fails.
431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 void *p = mbedtls_calloc(1, val_len);
433 if (p == NULL) {
434 return NULL;
435 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100438 cur->val.p = p;
439 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (val != NULL && val_len != 0) {
443 memcpy(cur->val.p, val, val_len);
444 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#endif /* MBEDTLS_ASN1_WRITE_C */