blob: 1c7f15d8f91b2cbb66e7905e7f2447b8f75cf488 [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 Rodgman9f366b02023-09-11 15:47:00 +010034 if (len > 0xFFFFFFFF) return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
Azim Khan45b79cf2018-05-23 16:55:16 +010035#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010036
Dave Rodgman9f366b02023-09-11 15:47:00 +010037 int required = 1;
38 if (len < 0x80) {
39 required = 1;
40 } else {
41 for (size_t l = len; l != 0; l >>= 8) {
42 required++;
43 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010044 }
45
Dave Rodgman9f366b02023-09-11 15:47:00 +010046 if (required > (*p - start)) {
47 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
48 }
49
50 do {
51 *--(*p) = MBEDTLS_BYTE_0(len);
52 len >>= 8;
53 } while (len);
54
55 if (required > 1) {
56 *--(*p) = (unsigned char)(required + 0x7f);
57 }
58
59 return required;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060}
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000063{
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (*p - start < 1) {
65 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
66 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067
68 *--(*p) = tag;
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000071}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
74 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020075{
76 size_t len = 0;
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (*p < start || (size_t) (*p - start) < size) {
79 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
80 }
Paul Bakker9852d002013-08-26 17:56:37 +020081
82 len = size;
83 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010084 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +020087}
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010090int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000091{
Janos Follath24eed8d2019-11-22 13:21:35 +000092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093 size_t len = 0;
94
95 // Write the MPI
96 //
Gilles Peskine449bd832023-01-11 14:50:10 +010097 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098
Gilles Peskine321a0892022-06-10 20:13:33 +020099 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
100 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200102 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (*p < start || (size_t) (*p - start) < len) {
106 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
107 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108
109 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111
112 // DER format assumes 2s complement for numbers, so the leftmost bit
113 // should be 0 for positive numbers and 1 for negative numbers.
114 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (X->s == 1 && **p & 0x80) {
116 if (*p - start < 1) {
117 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
118 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119
120 *--(*p) = 0x00;
121 len += 1;
122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
125 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_INTEGER));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126
Paul Bakker3d8fb632014-04-17 12:42:41 +0200127 ret = (int) len;
128
129cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135{
Janos Follath24eed8d2019-11-22 13:21:35 +0000136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137 size_t len = 0;
138
139 // Write NULL
140 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, 0));
142 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_NULL));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145}
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
148 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 size_t len = 0;
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
154 (const unsigned char *) oid, oid_len));
155 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
156 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159}
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
162 const char *oid, size_t oid_len,
163 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164{
Jethro Beekman01672442023-04-19 14:08:14 +0200165 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
166}
167
168int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
169 const char *oid, size_t oid_len,
170 size_t par_len, int has_par)
171{
Janos Follath24eed8d2019-11-22 13:21:35 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173 size_t len = 0;
174
Jethro Beekman01672442023-04-19 14:08:14 +0200175 if (has_par) {
176 if (par_len == 0) {
177 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
178 } else {
179 len += par_len;
180 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
186 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
187 MBEDTLS_ASN1_CONSTRUCTED |
188 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191}
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200194{
Janos Follath24eed8d2019-11-22 13:21:35 +0000195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200196 size_t len = 0;
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (*p - start < 1) {
199 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
200 }
Paul Bakker329def32013-09-06 16:34:38 +0200201
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200202 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200203 len++;
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
206 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BOOLEAN));
Paul Bakker329def32013-09-06 16:34:38 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return (int) len;
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000214 size_t len = 0;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 do {
217 if (*p - start < 1) {
218 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
219 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100220 len += 1;
221 *--(*p) = val & 0xff;
222 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (**p & 0x80) {
226 if (*p - start < 1) {
227 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
228 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000229 *--(*p) = 0x00;
230 len += 1;
231 }
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
234 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237}
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200240{
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200242}
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200245{
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200247}
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
250 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000251{
Janos Follath24eed8d2019-11-22 13:21:35 +0000252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000253 size_t len = 0;
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
256 (const unsigned char *) text,
257 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
260 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263}
Paul Bakker598e4502013-08-25 14:46:39 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
266 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100267{
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100269}
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
272 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100273{
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
275 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100276}
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
279 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000280{
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000282}
Paul Bakker598e4502013-08-25 14:46:39 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284int mbedtls_asn1_write_named_bitstring(unsigned char **p,
285 const unsigned char *start,
286 const unsigned char *buf,
287 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100288{
289 size_t unused_bits, byte_len;
290 const unsigned char *cur_byte;
291 unsigned char cur_byte_shifted;
292 unsigned char bit;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 byte_len = (bits + 7) / 8;
295 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100296
297 /*
298 * Named bitstrings require that trailing 0s are excluded in the encoding
299 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
300 * when encoding this value in the first content octet
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100303 cur_byte = buf + byte_len - 1;
304 cur_byte_shifted = *cur_byte >> unused_bits;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100307 bit = cur_byte_shifted & 0x1;
308 cur_byte_shifted >>= 1;
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100311 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100313
314 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100316 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100320 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100322 }
323 }
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100326}
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
329 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200330{
Janos Follath24eed8d2019-11-22 13:21:35 +0000331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100332 size_t len = 0;
333 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 byte_len = (bits + 7) / 8;
336 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
339 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
340 }
Paul Bakker598e4502013-08-25 14:46:39 +0200341
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100342 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200343
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100344 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100346 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
348 (*p) -= byte_len;
349 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100350 }
351
352 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
356 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200359}
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
362 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200363{
Janos Follath24eed8d2019-11-22 13:21:35 +0000364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200365 size_t len = 0;
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
370 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200373}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200374
Hanno Becker44da18a2018-10-12 10:42:13 +0100375
376/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
377 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
378static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_asn1_named_data *list,
380 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100381{
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 while (list != NULL) {
383 if (list->oid.len == len &&
384 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100385 break;
386 }
387
388 list = list->next;
389 }
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100392}
393
394mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_asn1_named_data **head,
396 const char *oid, size_t oid_len,
397 const unsigned char *val,
398 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200403 // Add new entry if not present yet based on OID
404 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
406 sizeof(mbedtls_asn1_named_data));
407 if (cur == NULL) {
408 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200409 }
410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 cur->oid.len = oid_len;
412 cur->oid.p = mbedtls_calloc(1, oid_len);
413 if (cur->oid.p == NULL) {
414 mbedtls_free(cur);
415 return NULL;
416 }
417
418 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100419
Paul Bakker59ba59f2013-09-09 11:26:00 +0200420 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (val_len != 0) {
422 cur->val.p = mbedtls_calloc(1, val_len);
423 if (cur->val.p == NULL) {
424 mbedtls_free(cur->oid.p);
425 mbedtls_free(cur);
426 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100427 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200428 }
429
Paul Bakker59ba59f2013-09-09 11:26:00 +0200430 cur->next = *head;
431 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 } else if (val_len == 0) {
433 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100434 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100436 /*
437 * Enlarge existing value buffer if needed
438 * Preserve old data until the allocation succeeded, to leave list in
439 * a consistent state in case allocation fails.
440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 void *p = mbedtls_calloc(1, val_len);
442 if (p == NULL) {
443 return NULL;
444 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100447 cur->val.p = p;
448 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200449 }
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (val != NULL && val_len != 0) {
452 memcpy(cur->val.p, val, val_len);
453 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_ASN1_WRITE_C */