blob: 0147c49f688aed4e5bef45b9f4ed4e5d2545228f [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerbdb912d2012-02-13 23:11:30 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +00009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000013#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000014
Rich Evans00ab4702015-02-06 13:43:58 +000015#include <string.h>
16
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010019int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021 if (len < 0x80) {
22 if (*p - start < 1) {
23 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
24 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020026 *--(*p) = (unsigned char) len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028 }
29
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010030 if (len <= 0xFF) {
31 if (*p - start < 2) {
32 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
33 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000034
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020035 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000036 *--(*p) = 0x81;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037 return 2;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000038 }
39
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 if (len <= 0xFFFF) {
41 if (*p - start < 3) {
42 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
43 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 *--(*p) = MBEDTLS_BYTE_0(len);
46 *--(*p) = MBEDTLS_BYTE_1(len);
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010047 *--(*p) = 0x82;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 return 3;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010049 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 if (len <= 0xFFFFFF) {
52 if (*p - start < 4) {
53 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
54 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 *--(*p) = MBEDTLS_BYTE_0(len);
57 *--(*p) = MBEDTLS_BYTE_1(len);
58 *--(*p) = MBEDTLS_BYTE_2(len);
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010059 *--(*p) = 0x83;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 return 4;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010061 }
62
David Horstmann864cc8d2022-10-25 10:16:45 +010063 int len_is_valid = 1;
Azim Khan45b79cf2018-05-23 16:55:16 +010064#if SIZE_MAX > 0xFFFFFFFF
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 len_is_valid = (len <= 0xFFFFFFFF);
Azim Khan45b79cf2018-05-23 16:55:16 +010066#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if (len_is_valid) {
68 if (*p - start < 5) {
69 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
70 }
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 *--(*p) = MBEDTLS_BYTE_0(len);
73 *--(*p) = MBEDTLS_BYTE_1(len);
74 *--(*p) = MBEDTLS_BYTE_2(len);
75 *--(*p) = MBEDTLS_BYTE_3(len);
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010076 *--(*p) = 0x84;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 return 5;
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010078 }
David Horstmann864cc8d2022-10-25 10:16:45 +010079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081}
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if (*p - start < 1) {
86 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
87 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
89 *--(*p) = tag;
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 return 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092}
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
95 const unsigned char *buf, size_t size)
Paul Bakker9852d002013-08-26 17:56:37 +020096{
97 size_t len = 0;
98
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 if (*p < start || (size_t) (*p - start) < size) {
100 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
101 }
Paul Bakker9852d002013-08-26 17:56:37 +0200102
103 len = size;
104 (*p) -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 memcpy(*p, buf, len);
Paul Bakker9852d002013-08-26 17:56:37 +0200106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 return (int) len;
Paul Bakker9852d002013-08-26 17:56:37 +0200108}
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112{
Janos Follath24eed8d2019-11-22 13:21:35 +0000113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000114 size_t len = 0;
115
116 // Write the MPI
117 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 len = mbedtls_mpi_size(X);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119
Gilles Peskinebb34fee2022-06-10 20:13:33 +0200120 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
121 * as 0 digits. We need to end up with 020100, not with 0200. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if (len == 0) {
Gilles Peskinebb34fee2022-06-10 20:13:33 +0200123 len = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 }
Gilles Peskinebb34fee2022-06-10 20:13:33 +0200125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 if (*p < start || (size_t) (*p - start) < len) {
127 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
128 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129
130 (*p) -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132
133 // DER format assumes 2s complement for numbers, so the leftmost bit
134 // should be 0 for positive numbers and 1 for negative numbers.
135 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 if (X->s == 1 && **p & 0x80) {
137 if (*p - start < 1) {
138 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
139 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140
141 *--(*p) = 0x00;
142 len += 1;
143 }
144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
146 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_INTEGER));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147
Paul Bakker3d8fb632014-04-17 12:42:41 +0200148 ret = (int) len;
149
150cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156{
Janos Follath24eed8d2019-11-22 13:21:35 +0000157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158 size_t len = 0;
159
160 // Write NULL
161 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, 0));
163 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_NULL));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166}
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
169 const char *oid, size_t oid_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170{
Janos Follath24eed8d2019-11-22 13:21:35 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172 size_t len = 0;
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
175 (const unsigned char *) oid, oid_len));
176 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
177 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180}
181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start,
183 const char *oid, size_t oid_len,
184 size_t par_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185{
Marek Janstaf5257c02023-07-31 14:49:38 +0200186 return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
187}
188
189int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, unsigned char *start,
190 const char *oid, size_t oid_len,
191 size_t par_len, int has_par)
192{
Janos Follath24eed8d2019-11-22 13:21:35 +0000193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194 size_t len = 0;
195
Marek Janstaf5257c02023-07-31 14:49:38 +0200196 if (has_par) {
197 if (par_len == 0) {
198 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
199 } else {
200 len += par_len;
201 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
207 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
Marek Jansta0a6743b2022-11-07 12:38:38 +0100208 MBEDTLS_ASN1_CONSTRUCTED |
209 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 return (int) len;
Marek Jansta0a6743b2022-11-07 12:38:38 +0100212}
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
Paul Bakker329def32013-09-06 16:34:38 +0200215{
Janos Follath24eed8d2019-11-22 13:21:35 +0000216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200217 size_t len = 0;
218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 if (*p - start < 1) {
220 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
221 }
Paul Bakker329def32013-09-06 16:34:38 +0200222
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200223 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200224 len++;
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
227 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BOOLEAN));
Paul Bakker329def32013-09-06 16:34:38 +0200228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 return (int) len;
Paul Bakker329def32013-09-06 16:34:38 +0200230}
231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232static int asn1_write_tagged_int(unsigned char **p, unsigned char *start, int val, int tag)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233{
Janos Follath24eed8d2019-11-22 13:21:35 +0000234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235 size_t len = 0;
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 do {
238 if (*p - start < 1) {
239 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
240 }
Gilles Peskine1dbab672019-03-01 18:15:18 +0100241 len += 1;
242 *--(*p) = val & 0xff;
243 val >>= 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 } while (val > 0);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 if (**p & 0x80) {
247 if (*p - start < 1) {
248 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
249 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250 *--(*p) = 0x00;
251 len += 1;
252 }
253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
255 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258}
259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200261{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200263}
264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265int mbedtls_asn1_write_enum(unsigned char **p, unsigned char *start, int val)
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200266{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200268}
269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270int mbedtls_asn1_write_tagged_string(unsigned char **p, unsigned char *start, int tag,
271 const char *text, size_t text_len)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272{
Janos Follath24eed8d2019-11-22 13:21:35 +0000273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 size_t len = 0;
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
277 (const unsigned char *) text,
278 text_len));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
281 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 return (int) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000284}
Paul Bakker598e4502013-08-25 14:46:39 +0200285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286int mbedtls_asn1_write_utf8_string(unsigned char **p, unsigned char *start,
287 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100288{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100290}
291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
293 const char *text, size_t text_len)
Jaeden Amero23f954d2018-05-17 11:46:13 +0100294{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
296 text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100297}
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
300 const char *text, size_t text_len)
Paul Bakker05888152012-02-16 10:26:57 +0000301{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
Paul Bakker05888152012-02-16 10:26:57 +0000303}
Paul Bakker598e4502013-08-25 14:46:39 +0200304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305int mbedtls_asn1_write_named_bitstring(unsigned char **p,
306 unsigned char *start,
307 const unsigned char *buf,
308 size_t bits)
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100309{
310 size_t unused_bits, byte_len;
311 const unsigned char *cur_byte;
312 unsigned char cur_byte_shifted;
313 unsigned char bit;
314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 byte_len = (bits + 7) / 8;
316 unused_bits = (byte_len * 8) - bits;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100317
318 /*
319 * Named bitstrings require that trailing 0s are excluded in the encoding
320 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
321 * when encoding this value in the first content octet
322 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (bits != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100324 cur_byte = buf + byte_len - 1;
325 cur_byte_shifted = *cur_byte >> unused_bits;
326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 for (;;) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100328 bit = cur_byte_shifted & 0x1;
329 cur_byte_shifted >>= 1;
330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 if (bit != 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100332 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100334
335 bits--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 if (bits == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100337 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 if (bits % 8 == 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100341 cur_byte_shifted = *--cur_byte;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 }
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100343 }
344 }
345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return mbedtls_asn1_write_bitstring(p, start, buf, bits);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100347}
348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
350 const unsigned char *buf, size_t bits)
Paul Bakker598e4502013-08-25 14:46:39 +0200351{
Janos Follath24eed8d2019-11-22 13:21:35 +0000352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100353 size_t len = 0;
354 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 byte_len = (bits + 7) / 8;
357 unused_bits = (byte_len * 8) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if (*p < start || (size_t) (*p - start) < byte_len + 1) {
360 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
361 }
Paul Bakker598e4502013-08-25 14:46:39 +0200362
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100363 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200364
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100365 /* Write the bitstring. Ensure the unused bits are zeroed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (byte_len > 0) {
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100367 byte_len--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
369 (*p) -= byte_len;
370 memcpy(*p, buf, byte_len);
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100371 }
372
373 /* Write unused bits */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 *--(*p) = (unsigned char) unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
377 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200380}
381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
383 const unsigned char *buf, size_t size)
Paul Bakker598e4502013-08-25 14:46:39 +0200384{
Janos Follath24eed8d2019-11-22 13:21:35 +0000385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200386 size_t len = 0;
387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
Paul Bakker598e4502013-08-25 14:46:39 +0200389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
391 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker598e4502013-08-25 14:46:39 +0200392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return (int) len;
Paul Bakker598e4502013-08-25 14:46:39 +0200394}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200395
Hanno Becker44da18a2018-10-12 10:42:13 +0100396
397/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
398 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
399static mbedtls_asn1_named_data *asn1_find_named_data(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 mbedtls_asn1_named_data *list,
401 const char *oid, size_t len)
Hanno Becker44da18a2018-10-12 10:42:13 +0100402{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 while (list != NULL) {
404 if (list->oid.len == len &&
405 memcmp(list->oid.p, oid, len) == 0) {
Hanno Becker44da18a2018-10-12 10:42:13 +0100406 break;
407 }
408
409 list = list->next;
410 }
411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 return list;
Hanno Becker44da18a2018-10-12 10:42:13 +0100413}
414
415mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 mbedtls_asn1_named_data **head,
417 const char *oid, size_t oid_len,
418 const unsigned char *val,
419 size_t val_len)
Paul Bakker59ba59f2013-09-09 11:26:00 +0200420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200424 // Add new entry if not present yet based on OID
425 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
427 sizeof(mbedtls_asn1_named_data));
428 if (cur == NULL) {
429 return NULL;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200430 }
431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 cur->oid.len = oid_len;
433 cur->oid.p = mbedtls_calloc(1, oid_len);
434 if (cur->oid.p == NULL) {
435 mbedtls_free(cur);
436 return NULL;
437 }
438
439 memcpy(cur->oid.p, oid, oid_len);
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100440
Paul Bakker59ba59f2013-09-09 11:26:00 +0200441 cur->val.len = val_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 if (val_len != 0) {
443 cur->val.p = mbedtls_calloc(1, val_len);
444 if (cur->val.p == NULL) {
445 mbedtls_free(cur->oid.p);
446 mbedtls_free(cur);
447 return NULL;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100448 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200449 }
450
Paul Bakker59ba59f2013-09-09 11:26:00 +0200451 cur->next = *head;
452 *head = cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 } else if (val_len == 0) {
454 mbedtls_free(cur->val.p);
Gilles Peskine09c0a232019-03-04 15:00:06 +0100455 cur->val.p = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 } else if (cur->val.len != val_len) {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100457 /*
458 * Enlarge existing value buffer if needed
459 * Preserve old data until the allocation succeeded, to leave list in
460 * a consistent state in case allocation fails.
461 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 void *p = mbedtls_calloc(1, val_len);
463 if (p == NULL) {
464 return NULL;
465 }
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 mbedtls_free(cur->val.p);
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100468 cur->val.p = p;
469 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200470 }
471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if (val != NULL && val_len != 0) {
473 memcpy(cur->val.p, val, val_len);
474 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_ASN1_WRITE_C */