blob: b512cc7777fa6c110c704b11a217a3459824ec8c [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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020024# include "mbedtls/asn1write.h"
25# include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000026
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020027# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000028
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020029# if defined(MBEDTLS_PLATFORM_C)
30# include "mbedtls/platform.h"
31# else
32# include <stdlib.h>
33# define mbedtls_calloc calloc
34# define mbedtls_free free
35# endif
Paul Bakker59ba59f2013-09-09 11:26:00 +020036
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037int mbedtls_asn1_write_len(unsigned char **p,
38 const unsigned char *start,
39 size_t len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041 if (len < 0x80) {
42 if (*p - start < 1)
43 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000044
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045 *--(*p) = (unsigned char)len;
46 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000047 }
48
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049 if (len <= 0xFF) {
50 if (*p - start < 2)
51 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053 *--(*p) = (unsigned char)len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054 *--(*p) = 0x81;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055 return 2;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056 }
57
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058 if (len <= 0xFFFF) {
59 if (*p - start < 3)
60 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062 *--(*p) = (len)&0xFF;
63 *--(*p) = (len >> 8) & 0xFF;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010064 *--(*p) = 0x82;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065 return 3;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010066 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068 if (len <= 0xFFFFFF) {
69 if (*p - start < 4)
70 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072 *--(*p) = (len)&0xFF;
73 *--(*p) = (len >> 8) & 0xFF;
74 *--(*p) = (len >> 16) & 0xFF;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010075 *--(*p) = 0x83;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076 return 4;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010077 }
78
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079# if SIZE_MAX > 0xFFFFFFFF
80 if (len <= 0xFFFFFFFF)
81# endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010082 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020083 if (*p - start < 5)
84 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086 *--(*p) = (len)&0xFF;
87 *--(*p) = (len >> 8) & 0xFF;
88 *--(*p) = (len >> 16) & 0xFF;
89 *--(*p) = (len >> 24) & 0xFF;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010090 *--(*p) = 0x84;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091 return 5;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010092 }
93
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094# if SIZE_MAX > 0xFFFFFFFF
95 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
96# endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097}
98
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099int mbedtls_asn1_write_tag(unsigned char **p,
100 const unsigned char *start,
101 unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103 if (*p - start < 1)
104 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105
106 *--(*p) = tag;
107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109}
110
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111int mbedtls_asn1_write_raw_buffer(unsigned char **p,
112 const unsigned char *start,
113 const unsigned char *buf,
114 size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +0200115{
116 size_t len = 0;
117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118 if (*p < start || (size_t)(*p - start) < size)
119 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakker9852d002013-08-26 17:56:37 +0200120
121 len = size;
122 (*p) -= len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200123 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +0200124
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125 return (int)len;
Paul Bakker9852d002013-08-26 17:56:37 +0200126}
127
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128# if defined(MBEDTLS_BIGNUM_C)
129int mbedtls_asn1_write_mpi(unsigned char **p,
130 const unsigned char *start,
131 const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132{
Janos Follath24eed8d2019-11-22 13:21:35 +0000133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134 size_t len = 0;
135
136 // Write the MPI
137 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140 if (*p < start || (size_t)(*p - start) < len)
141 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142
143 (*p) -= len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145
146 // DER format assumes 2s complement for numbers, so the leftmost bit
147 // should be 0 for positive numbers and 1 for negative numbers.
148 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 if (X->s == 1 && **p & 0x80) {
150 if (*p - start < 1)
151 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152
153 *--(*p) = 0x00;
154 len += 1;
155 }
156
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
158 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
159 MBEDTLS_ASN1_INTEGER));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200161 ret = (int)len;
Paul Bakker3d8fb632014-04-17 12:42:41 +0200162
163cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200166# endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200167
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169{
Janos Follath24eed8d2019-11-22 13:21:35 +0000170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171 size_t len = 0;
172
173 // Write NULL
174 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, 0));
176 MBEDTLS_ASN1_CHK_ADD(len,
177 mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_NULL));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179 return (int)len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180}
181
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182int mbedtls_asn1_write_oid(unsigned char **p,
183 const unsigned char *start,
184 const char *oid,
185 size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186{
Janos Follath24eed8d2019-11-22 13:21:35 +0000187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188 size_t len = 0;
189
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190 MBEDTLS_ASN1_CHK_ADD(
191 len, mbedtls_asn1_write_raw_buffer(p, start, (const unsigned char *)oid,
192 oid_len));
193 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
194 MBEDTLS_ASN1_CHK_ADD(len,
195 mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200197 return (int)len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198}
199
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200int mbedtls_asn1_write_algorithm_identifier(unsigned char **p,
201 const unsigned char *start,
202 const char *oid,
203 size_t oid_len,
204 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205{
Janos Follath24eed8d2019-11-22 13:21:35 +0000206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207 size_t len = 0;
208
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209 if (par_len == 0)
210 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200211 else
212 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
217 MBEDTLS_ASN1_CHK_ADD(
218 len, mbedtls_asn1_write_tag(
219 p, start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 return (int)len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222}
223
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224int mbedtls_asn1_write_bool(unsigned char **p,
225 const unsigned char *start,
226 int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200227{
Janos Follath24eed8d2019-11-22 13:21:35 +0000228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200229 size_t len = 0;
230
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231 if (*p - start < 1)
232 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakker329def32013-09-06 16:34:38 +0200233
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200234 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200235 len++;
236
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
238 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
239 MBEDTLS_ASN1_BOOLEAN));
Paul Bakker329def32013-09-06 16:34:38 +0200240
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 return (int)len;
Paul Bakker329def32013-09-06 16:34:38 +0200242}
243
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244static int asn1_write_tagged_int(unsigned char **p,
245 const unsigned char *start,
246 int val,
247 int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248{
Janos Follath24eed8d2019-11-22 13:21:35 +0000249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250 size_t len = 0;
251
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252 do {
253 if (*p - start < 1)
254 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Gilles Peskine1dbab672019-03-01 18:15:18 +0100255 len += 1;
256 *--(*p) = val & 0xff;
257 val >>= 8;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 if (**p & 0x80) {
261 if (*p - start < 1)
262 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263 *--(*p) = 0x00;
264 len += 1;
265 }
266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
268 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000269
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200270 return (int)len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271}
272
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273int mbedtls_asn1_write_int(unsigned char **p,
274 const unsigned char *start,
275 int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200276{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200277 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200278}
279
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200280int mbedtls_asn1_write_enum(unsigned char **p,
281 const unsigned char *start,
282 int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200283{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200284 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200285}
286
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200287int mbedtls_asn1_write_tagged_string(unsigned char **p,
288 const unsigned char *start,
289 int tag,
290 const char *text,
291 size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292{
Janos Follath24eed8d2019-11-22 13:21:35 +0000293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294 size_t len = 0;
295
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296 MBEDTLS_ASN1_CHK_ADD(len,
297 mbedtls_asn1_write_raw_buffer(
298 p, start, (const unsigned char *)text, text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
301 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000302
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303 return (int)len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000304}
Paul Bakker598e4502013-08-25 14:46:39 +0200305
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306int mbedtls_asn1_write_utf8_string(unsigned char **p,
307 const unsigned char *start,
308 const char *text,
309 size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100310{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING,
312 text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100313}
314
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315int mbedtls_asn1_write_printable_string(unsigned char **p,
Mateusz Starzyk4e300d02021-01-27 15:37:12 +0100316 const unsigned char *start,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200317 const char *text,
318 size_t text_len)
319{
320 return mbedtls_asn1_write_tagged_string(
321 p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len);
322}
323
324int mbedtls_asn1_write_ia5_string(unsigned char **p,
325 const unsigned char *start,
326 const char *text,
327 size_t text_len)
328{
329 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING,
330 text, text_len);
331}
332
333int mbedtls_asn1_write_named_bitstring(unsigned char **p,
334 const unsigned char *start,
335 const unsigned char *buf,
336 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100337{
338 size_t unused_bits, byte_len;
339 const unsigned char *cur_byte;
340 unsigned char cur_byte_shifted;
341 unsigned char bit;
342
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200343 byte_len = (bits + 7) / 8;
344 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100345
346 /*
347 * Named bitstrings require that trailing 0s are excluded in the encoding
348 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
349 * when encoding this value in the first content octet
350 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100352 cur_byte = buf + byte_len - 1;
353 cur_byte_shifted = *cur_byte >> unused_bits;
354
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200355 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100356 bit = cur_byte_shifted & 0x1;
357 cur_byte_shifted >>= 1;
358
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200359 if (bit != 0)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100360 break;
361
362 bits--;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363 if (bits == 0)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100364 break;
365
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366 if (bits % 8 == 0)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100367 cur_byte_shifted = *--cur_byte;
368 }
369 }
370
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100372}
373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374int mbedtls_asn1_write_bitstring(unsigned char **p,
375 const unsigned char *start,
376 const unsigned char *buf,
377 size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200378{
Janos Follath24eed8d2019-11-22 13:21:35 +0000379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100380 size_t len = 0;
381 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200382
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200383 byte_len = (bits + 7) / 8;
384 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200385
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200386 if (*p < start || (size_t)(*p - start) < byte_len + 1)
387 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakker598e4502013-08-25 14:46:39 +0200388
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100389 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200390
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100391 /* Write the bitstring. Ensure the unused bits are zeroed */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100393 byte_len--;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
395 (*p) -= byte_len;
396 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100397 }
398
399 /* Write unused bits */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 *--(*p) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200401
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200402 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
403 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
404 MBEDTLS_ASN1_BIT_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200405
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200406 return (int)len;
Paul Bakker598e4502013-08-25 14:46:39 +0200407}
408
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200409int mbedtls_asn1_write_octet_string(unsigned char **p,
410 const unsigned char *start,
411 const unsigned char *buf,
412 size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200413{
Janos Follath24eed8d2019-11-22 13:21:35 +0000414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200415 size_t len = 0;
416
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200417 MBEDTLS_ASN1_CHK_ADD(len,
418 mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200419
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200420 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
421 MBEDTLS_ASN1_CHK_ADD(
422 len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200423
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200424 return (int)len;
Paul Bakker598e4502013-08-25 14:46:39 +0200425}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200426
Hanno Becker44da18a2018-10-12 10:42:13 +0100427/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
428 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200429static mbedtls_asn1_named_data *
430asn1_find_named_data(mbedtls_asn1_named_data *list, const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100431{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200432 while (list != NULL) {
433 if (list->oid.len == len && memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100434 break;
435 }
436
437 list = list->next;
438 }
439
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200440 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100441}
442
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443mbedtls_asn1_named_data *
444mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **head,
445 const char *oid,
446 size_t oid_len,
447 const unsigned char *val,
448 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200451
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200452 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200453 // Add new entry if not present yet based on OID
454 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200455 cur = (mbedtls_asn1_named_data *)mbedtls_calloc(
456 1, sizeof(mbedtls_asn1_named_data));
457 if (cur == NULL)
458 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200459
Paul Bakker59ba59f2013-09-09 11:26:00 +0200460 cur->oid.len = oid_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200461 cur->oid.p = mbedtls_calloc(1, oid_len);
462 if (cur->oid.p == NULL) {
463 mbedtls_free(cur);
464 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200465 }
466
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200467 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100468
Paul Bakker59ba59f2013-09-09 11:26:00 +0200469 cur->val.len = val_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200470 if (val_len != 0) {
471 cur->val.p = mbedtls_calloc(1, val_len);
472 if (cur->val.p == NULL) {
473 mbedtls_free(cur->oid.p);
474 mbedtls_free(cur);
475 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100476 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200477 }
478
Paul Bakker59ba59f2013-09-09 11:26:00 +0200479 cur->next = *head;
480 *head = cur;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200481 } else if (val_len == 0) {
482 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100483 cur->val.p = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200484 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100485 /*
486 * Enlarge existing value buffer if needed
487 * Preserve old data until the allocation succeeded, to leave list in
488 * a consistent state in case allocation fails.
489 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490 void *p = mbedtls_calloc(1, val_len);
491 if (p == NULL)
492 return NULL;
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100493
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200494 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100495 cur->val.p = p;
496 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200497 }
498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499 if (val != NULL)
500 memcpy(cur->val.p, val, val_len);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200501
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#endif /* MBEDTLS_ASN1_WRITE_C */