blob: b9d586aef4affbcb3f83c6cf2bf7cba97e925921 [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{
Gilles Peskine449bd832023-01-11 14:50:10 +010033 if (len < 0x80) {
34 if (*p - start < 1) {
35 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
36 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000037
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020038 *--(*p) = (unsigned char) len;
Gilles Peskine449bd832023-01-11 14:50:10 +010039 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040 }
41
Gilles Peskine449bd832023-01-11 14:50:10 +010042 if (len <= 0xFF) {
43 if (*p - start < 2) {
44 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
45 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000046
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020047 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048 *--(*p) = 0x81;
Gilles Peskine449bd832023-01-11 14:50:10 +010049 return 2;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000050 }
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (len <= 0xFFFF) {
53 if (*p - start < 3) {
54 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
55 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 *--(*p) = MBEDTLS_BYTE_0(len);
58 *--(*p) = MBEDTLS_BYTE_1(len);
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010059 *--(*p) = 0x82;
Gilles Peskine449bd832023-01-11 14:50:10 +010060 return 3;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010061 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (len <= 0xFFFFFF) {
64 if (*p - start < 4) {
65 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
66 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 *--(*p) = MBEDTLS_BYTE_0(len);
69 *--(*p) = MBEDTLS_BYTE_1(len);
70 *--(*p) = MBEDTLS_BYTE_2(len);
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010071 *--(*p) = 0x83;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return 4;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010073 }
74
David Horstmann059848f2022-10-25 10:16:45 +010075 int len_is_valid = 1;
Azim Khan45b79cf2018-05-23 16:55:16 +010076#if SIZE_MAX > 0xFFFFFFFF
Gilles Peskine449bd832023-01-11 14:50:10 +010077 len_is_valid = (len <= 0xFFFFFFFF);
Azim Khan45b79cf2018-05-23 16:55:16 +010078#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (len_is_valid) {
80 if (*p - start < 5) {
81 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
82 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 *--(*p) = MBEDTLS_BYTE_0(len);
85 *--(*p) = MBEDTLS_BYTE_1(len);
86 *--(*p) = MBEDTLS_BYTE_2(len);
87 *--(*p) = MBEDTLS_BYTE_3(len);
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010088 *--(*p) = 0x84;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return 5;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010090 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093}
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (*p - start < 1) {
98 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
99 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100
101 *--(*p) = tag;
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104}
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
107 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +0200108{
109 size_t len = 0;
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (*p < start || (size_t) (*p - start) < size) {
112 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
113 }
Paul Bakker9852d002013-08-26 17:56:37 +0200114
115 len = size;
116 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +0200120}
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100123int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126 size_t len = 0;
127
128 // Write the MPI
129 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131
Gilles Peskine321a0892022-06-10 20:13:33 +0200132 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
133 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (len == 0) {
Gilles Peskine321a0892022-06-10 20:13:33 +0200135 len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 }
Gilles Peskine321a0892022-06-10 20:13:33 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (*p < start || (size_t) (*p - start) < len) {
139 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
140 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141
142 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144
145 // DER format assumes 2s complement for numbers, so the leftmost bit
146 // should be 0 for positive numbers and 1 for negative numbers.
147 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (X->s == 1 && **p & 0x80) {
149 if (*p - start < 1) {
150 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
151 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152
153 *--(*p) = 0x00;
154 len += 1;
155 }
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 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_INTEGER));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159
Paul Bakker3d8fb632014-04-17 12:42:41 +0200160 ret = (int) len;
161
162cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168{
Janos Follath24eed8d2019-11-22 13:21:35 +0000169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 size_t len = 0;
171
172 // Write NULL
173 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, 0));
175 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_NULL));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178}
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
181 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182{
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184 size_t len = 0;
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
187 (const unsigned char *) oid, oid_len));
188 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
189 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192}
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
195 const char *oid, size_t oid_len,
196 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197{
Janos Follath24eed8d2019-11-22 13:21:35 +0000198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000199 size_t len = 0;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (par_len == 0) {
202 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
203 } else {
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200204 len += par_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
210 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
211 MBEDTLS_ASN1_CONSTRUCTED |
212 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215}
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200218{
Janos Follath24eed8d2019-11-22 13:21:35 +0000219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200220 size_t len = 0;
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (*p - start < 1) {
223 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
224 }
Paul Bakker329def32013-09-06 16:34:38 +0200225
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200226 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200227 len++;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
230 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BOOLEAN));
Paul Bakker329def32013-09-06 16:34:38 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return (int) len;
Paul Bakker329def32013-09-06 16:34:38 +0200233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236{
Janos Follath24eed8d2019-11-22 13:21:35 +0000237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238 size_t len = 0;
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 do {
241 if (*p - start < 1) {
242 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
243 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100244 len += 1;
245 *--(*p) = val & 0xff;
246 val >>= 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (**p & 0x80) {
250 if (*p - start < 1) {
251 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
252 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000253 *--(*p) = 0x00;
254 len += 1;
255 }
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
258 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261}
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200264{
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200266}
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200269{
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200271}
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
274 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000275{
Janos Follath24eed8d2019-11-22 13:21:35 +0000276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000277 size_t len = 0;
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
280 (const unsigned char *) text,
281 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
284 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000287}
Paul Bakker598e4502013-08-25 14:46:39 +0200288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
290 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100291{
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100293}
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
296 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100297{
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
299 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100300}
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
303 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000304{
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000306}
Paul Bakker598e4502013-08-25 14:46:39 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308int mbedtls_asn1_write_named_bitstring(unsigned char **p,
309 const unsigned char *start,
310 const unsigned char *buf,
311 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100312{
313 size_t unused_bits, byte_len;
314 const unsigned char *cur_byte;
315 unsigned char cur_byte_shifted;
316 unsigned char bit;
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 byte_len = (bits + 7) / 8;
319 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100320
321 /*
322 * Named bitstrings require that trailing 0s are excluded in the encoding
323 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
324 * when encoding this value in the first content octet
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100327 cur_byte = buf + byte_len - 1;
328 cur_byte_shifted = *cur_byte >> unused_bits;
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100331 bit = cur_byte_shifted & 0x1;
332 cur_byte_shifted >>= 1;
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100335 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100337
338 bits--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100340 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100344 cur_byte_shifted = *--cur_byte;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100346 }
347 }
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100350}
351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
353 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200354{
Janos Follath24eed8d2019-11-22 13:21:35 +0000355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100356 size_t len = 0;
357 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 byte_len = (bits + 7) / 8;
360 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
363 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
364 }
Paul Bakker598e4502013-08-25 14:46:39 +0200365
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100366 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200367
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100368 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100370 byte_len--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
372 (*p) -= byte_len;
373 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100374 }
375
376 /* Write unused bits */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
380 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200383}
384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
386 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200387{
Janos Follath24eed8d2019-11-22 13:21:35 +0000388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200389 size_t len = 0;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
394 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200397}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200398
Hanno Becker44da18a2018-10-12 10:42:13 +0100399
400/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
401 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
402static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 mbedtls_asn1_named_data *list,
404 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100405{
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 while (list != NULL) {
407 if (list->oid.len == len &&
408 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100409 break;
410 }
411
412 list = list->next;
413 }
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100416}
417
418mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_asn1_named_data **head,
420 const char *oid, size_t oid_len,
421 const unsigned char *val,
422 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200427 // Add new entry if not present yet based on OID
428 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
430 sizeof(mbedtls_asn1_named_data));
431 if (cur == NULL) {
432 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 cur->oid.len = oid_len;
436 cur->oid.p = mbedtls_calloc(1, oid_len);
437 if (cur->oid.p == NULL) {
438 mbedtls_free(cur);
439 return NULL;
440 }
441
442 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100443
Paul Bakker59ba59f2013-09-09 11:26:00 +0200444 cur->val.len = val_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (val_len != 0) {
446 cur->val.p = mbedtls_calloc(1, val_len);
447 if (cur->val.p == NULL) {
448 mbedtls_free(cur->oid.p);
449 mbedtls_free(cur);
450 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100451 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200452 }
453
Paul Bakker59ba59f2013-09-09 11:26:00 +0200454 cur->next = *head;
455 *head = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else if (val_len == 0) {
457 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100458 cur->val.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100460 /*
461 * Enlarge existing value buffer if needed
462 * Preserve old data until the allocation succeeded, to leave list in
463 * a consistent state in case allocation fails.
464 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 void *p = mbedtls_calloc(1, val_len);
466 if (p == NULL) {
467 return NULL;
468 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100471 cur->val.p = p;
472 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200473 }
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (val != NULL && val_len != 0) {
476 memcpy(cur->val.p, val, val_len);
477 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_ASN1_WRITE_C */