blob: 5dde3fe7aba94a05571d8b07fb4c39ee5e4b8002 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010075int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
76 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020077{
78 size_t len = 0;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (*p < start || (size_t) (*p - start) < size) {
81 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
82 }
Paul Bakker9852d002013-08-26 17:56:37 +020083
84 len = size;
85 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010086 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +020089}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010092int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093{
Janos Follath24eed8d2019-11-22 13:21:35 +000094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095 size_t len = 0;
96
97 // Write the MPI
98 //
Gilles Peskine449bd832023-01-11 14:50:10 +010099 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100
Gilles Peskine321a0892022-06-10 20:13:33 +0200101 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
102 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200104 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (*p < start || (size_t) (*p - start) < len) {
108 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
109 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110
111 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113
114 // DER format assumes 2s complement for numbers, so the leftmost bit
115 // should be 0 for positive numbers and 1 for negative numbers.
116 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (X->s == 1 && **p & 0x80) {
118 if (*p - start < 1) {
119 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
120 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000121
122 *--(*p) = 0x00;
123 len += 1;
124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
127 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_INTEGER));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128
Paul Bakker3d8fb632014-04-17 12:42:41 +0200129 ret = (int) len;
130
131cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137{
Janos Follath24eed8d2019-11-22 13:21:35 +0000138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139 size_t len = 0;
140
141 // Write NULL
142 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, 0));
144 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_NULL));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147}
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
150 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151{
Janos Follath24eed8d2019-11-22 13:21:35 +0000152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153 size_t len = 0;
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
156 (const unsigned char *) oid, oid_len));
157 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
158 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return (int) len;
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
188 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
189 MBEDTLS_ASN1_CONSTRUCTED |
190 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return (int) len;
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200198 size_t len = 0;
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (*p - start < 1) {
201 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
202 }
Paul Bakker329def32013-09-06 16:34:38 +0200203
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200204 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200205 len++;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
208 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BOOLEAN));
Paul Bakker329def32013-09-06 16:34:38 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return (int) len;
Paul Bakker329def32013-09-06 16:34:38 +0200211}
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000214{
Janos Follath24eed8d2019-11-22 13:21:35 +0000215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 size_t len = 0;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 do {
219 if (*p - start < 1) {
220 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
221 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100222 len += 1;
223 *--(*p) = val & 0xff;
224 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (**p & 0x80) {
228 if (*p - start < 1) {
229 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
230 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231 *--(*p) = 0x00;
232 len += 1;
233 }
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
236 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000239}
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200242{
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200244}
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200247{
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200249}
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
252 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000253{
Janos Follath24eed8d2019-11-22 13:21:35 +0000254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255 size_t len = 0;
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
258 (const unsigned char *) text,
259 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
262 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000265}
Paul Bakker598e4502013-08-25 14:46:39 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
268 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100269{
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100271}
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
274 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100275{
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
277 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100278}
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
281 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000282{
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000284}
Paul Bakker598e4502013-08-25 14:46:39 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_asn1_write_named_bitstring(unsigned char **p,
287 const unsigned char *start,
288 const unsigned char *buf,
289 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100290{
291 size_t unused_bits, byte_len;
292 const unsigned char *cur_byte;
293 unsigned char cur_byte_shifted;
294 unsigned char bit;
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 byte_len = (bits + 7) / 8;
297 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100298
299 /*
300 * Named bitstrings require that trailing 0s are excluded in the encoding
301 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
302 * when encoding this value in the first content octet
303 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100305 cur_byte = buf + byte_len - 1;
306 cur_byte_shifted = *cur_byte >> unused_bits;
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100309 bit = cur_byte_shifted & 0x1;
310 cur_byte_shifted >>= 1;
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100313 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100315
316 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100318 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100322 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100324 }
325 }
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100328}
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
331 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200332{
Janos Follath24eed8d2019-11-22 13:21:35 +0000333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100334 size_t len = 0;
335 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 byte_len = (bits + 7) / 8;
338 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
341 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
342 }
Paul Bakker598e4502013-08-25 14:46:39 +0200343
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100344 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200345
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100346 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100348 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
350 (*p) -= byte_len;
351 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100352 }
353
354 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
358 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200361}
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
364 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200365{
Janos Follath24eed8d2019-11-22 13:21:35 +0000366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200367 size_t len = 0;
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
372 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200375}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200376
Hanno Becker44da18a2018-10-12 10:42:13 +0100377
378/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
379 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
380static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_asn1_named_data *list,
382 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100383{
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 while (list != NULL) {
385 if (list->oid.len == len &&
386 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100387 break;
388 }
389
390 list = list->next;
391 }
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100394}
395
396mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_asn1_named_data **head,
398 const char *oid, size_t oid_len,
399 const unsigned char *val,
400 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200405 // Add new entry if not present yet based on OID
406 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
408 sizeof(mbedtls_asn1_named_data));
409 if (cur == NULL) {
410 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200411 }
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 cur->oid.len = oid_len;
414 cur->oid.p = mbedtls_calloc(1, oid_len);
415 if (cur->oid.p == NULL) {
416 mbedtls_free(cur);
417 return NULL;
418 }
419
420 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100421
Paul Bakker59ba59f2013-09-09 11:26:00 +0200422 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (val_len != 0) {
424 cur->val.p = mbedtls_calloc(1, val_len);
425 if (cur->val.p == NULL) {
426 mbedtls_free(cur->oid.p);
427 mbedtls_free(cur);
428 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100429 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200430 }
431
Paul Bakker59ba59f2013-09-09 11:26:00 +0200432 cur->next = *head;
433 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 } else if (val_len == 0) {
435 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100436 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100438 /*
439 * Enlarge existing value buffer if needed
440 * Preserve old data until the allocation succeeded, to leave list in
441 * a consistent state in case allocation fails.
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 void *p = mbedtls_calloc(1, val_len);
444 if (p == NULL) {
445 return NULL;
446 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100449 cur->val.p = p;
450 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200451 }
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (val != NULL && val_len != 0) {
454 memcpy(cur->val.p, val, val_len);
455 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#endif /* MBEDTLS_ASN1_WRITE_C */