Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ASN.1 buffer writing functionality |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 9 | |
Valerio Setti | 688f795 | 2024-01-16 09:18:40 +0100 | [diff] [blame] | 10 | #if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C) || \ |
| 11 | defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 12 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 13 | #include "mbedtls/asn1write.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 14 | #include "mbedtls/error.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 15 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 16 | #include <string.h> |
| 17 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | #include "mbedtls/platform.h" |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 19 | |
Dave Rodgman | 4935283 | 2023-09-11 17:09:13 +0100 | [diff] [blame] | 20 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 21 | #include "mbedtls/asn1.h" |
| 22 | #endif |
| 23 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 24 | int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 25 | { |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 26 | #if SIZE_MAX > 0xFFFFFFFF |
Dave Rodgman | 3bbedf6 | 2023-09-11 16:06:28 +0100 | [diff] [blame] | 27 | if (len > 0xFFFFFFFF) { |
| 28 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 29 | } |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 30 | #endif |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 31 | |
Dave Rodgman | 9f366b0 | 2023-09-11 15:47:00 +0100 | [diff] [blame] | 32 | int required = 1; |
Dave Rodgman | 33287ae | 2023-09-11 17:03:22 +0100 | [diff] [blame] | 33 | |
| 34 | if (len >= 0x80) { |
Dave Rodgman | 9f366b0 | 2023-09-11 15:47:00 +0100 | [diff] [blame] | 35 | for (size_t l = len; l != 0; l >>= 8) { |
| 36 | required++; |
| 37 | } |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Dave Rodgman | 9f366b0 | 2023-09-11 15:47:00 +0100 | [diff] [blame] | 40 | if (required > (*p - start)) { |
| 41 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 42 | } |
| 43 | |
| 44 | do { |
| 45 | *--(*p) = MBEDTLS_BYTE_0(len); |
| 46 | len >>= 8; |
| 47 | } while (len); |
| 48 | |
| 49 | if (required > 1) { |
Dave Rodgman | 33287ae | 2023-09-11 17:03:22 +0100 | [diff] [blame] | 50 | *--(*p) = (unsigned char) (0x80 + required - 1); |
Dave Rodgman | 9f366b0 | 2023-09-11 15:47:00 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return required; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 57 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | if (*p - start < 1) { |
| 59 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 60 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 61 | |
| 62 | *--(*p) = tag; |
| 63 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | return 1; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 65 | } |
Valerio Setti | 688f795 | 2024-01-16 09:18:40 +0100 | [diff] [blame] | 66 | #endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C || MBEDTLS_PSA_UTIL_HAVE_ECDSA */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 67 | |
Agathiyan Bragadeesh | 86dc085 | 2023-09-04 14:53:30 +0100 | [diff] [blame] | 68 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 69 | static int mbedtls_asn1_write_len_and_tag(unsigned char **p, |
Dave Rodgman | 0c9516e | 2023-09-15 18:30:09 +0100 | [diff] [blame] | 70 | const unsigned char *start, |
| 71 | size_t len, |
| 72 | unsigned char tag) |
Dave Rodgman | cf5f746 | 2023-09-11 16:27:34 +0100 | [diff] [blame] | 73 | { |
| 74 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 75 | |
| 76 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 77 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag)); |
| 78 | |
Dave Rodgman | dc669a1 | 2023-09-11 18:39:57 +0100 | [diff] [blame] | 79 | return (int) len; |
Dave Rodgman | cf5f746 | 2023-09-11 16:27:34 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start, |
| 83 | const unsigned char *buf, size_t size) |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 84 | { |
| 85 | size_t len = 0; |
| 86 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | if (*p < start || (size_t) (*p - start) < size) { |
| 88 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 89 | } |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 90 | |
| 91 | len = size; |
| 92 | (*p) -= len; |
Manuel Pégourié-Gonnard | e51bde0 | 2025-06-03 11:22:55 +0200 | [diff] [blame^] | 93 | if (len != 0) { |
| 94 | memcpy(*p, buf, len); |
| 95 | } |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 96 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | return (int) len; |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | #if defined(MBEDTLS_BIGNUM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 102 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 103 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 104 | size_t len = 0; |
| 105 | |
| 106 | // Write the MPI |
| 107 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | len = mbedtls_mpi_size(X); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 109 | |
Gilles Peskine | 321a089 | 2022-06-10 20:13:33 +0200 | [diff] [blame] | 110 | /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not |
| 111 | * as 0 digits. We need to end up with 020100, not with 0200. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | if (len == 0) { |
Gilles Peskine | 321a089 | 2022-06-10 20:13:33 +0200 | [diff] [blame] | 113 | len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | } |
Gilles Peskine | 321a089 | 2022-06-10 20:13:33 +0200 | [diff] [blame] | 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | if (*p < start || (size_t) (*p - start) < len) { |
| 117 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 118 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 119 | |
| 120 | (*p) -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len)); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 122 | |
| 123 | // DER format assumes 2s complement for numbers, so the leftmost bit |
| 124 | // should be 0 for positive numbers and 1 for negative numbers. |
| 125 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | if (X->s == 1 && **p & 0x80) { |
| 127 | if (*p - start < 1) { |
| 128 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 129 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 130 | |
| 131 | *--(*p) = 0x00; |
| 132 | len += 1; |
| 133 | } |
| 134 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 135 | ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER); |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 136 | |
| 137 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | return ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 139 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 143 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 144 | // Write NULL |
| 145 | // |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 146 | return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start, |
| 150 | const char *oid, size_t oid_len) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 151 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 152 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 153 | size_t len = 0; |
| 154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 156 | (const unsigned char *) oid, oid_len)); |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 157 | return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start, |
| 161 | const char *oid, size_t oid_len, |
| 162 | size_t par_len) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 163 | { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 164 | return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1); |
| 165 | } |
| 166 | |
| 167 | int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start, |
| 168 | const char *oid, size_t oid_len, |
| 169 | size_t par_len, int has_par) |
| 170 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 171 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 172 | size_t len = 0; |
| 173 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 174 | if (has_par) { |
| 175 | if (par_len == 0) { |
| 176 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start)); |
| 177 | } else { |
| 178 | len += par_len; |
| 179 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len)); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 183 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 184 | return mbedtls_asn1_write_len_and_tag(p, start, len, |
Dave Rodgman | 0c9516e | 2023-09-15 18:30:09 +0100 | [diff] [blame] | 185 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean) |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 189 | { |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 190 | size_t len = 0; |
| 191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | if (*p - start < 1) { |
| 193 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 194 | } |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 195 | |
Jonathan Leroy | 87c96c2 | 2015-10-14 09:41:56 +0200 | [diff] [blame] | 196 | *--(*p) = (boolean) ? 255 : 0; |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 197 | len++; |
| 198 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 199 | return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 203 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 204 | size_t len = 0; |
| 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | do { |
| 207 | if (*p - start < 1) { |
| 208 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 209 | } |
Gilles Peskine | 1dbab67 | 2019-03-01 18:15:18 +0100 | [diff] [blame] | 210 | len += 1; |
| 211 | *--(*p) = val & 0xff; |
| 212 | val >>= 8; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | } while (val > 0); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | if (**p & 0x80) { |
| 216 | if (*p - start < 1) { |
| 217 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 218 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 219 | *--(*p) = 0x00; |
| 220 | len += 1; |
| 221 | } |
| 222 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 223 | return mbedtls_asn1_write_len_and_tag(p, start, len, tag); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val) |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 227 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER); |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val) |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 232 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED); |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag, |
| 237 | const char *text, size_t text_len) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 238 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 239 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 240 | size_t len = 0; |
| 241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 243 | (const unsigned char *) text, |
| 244 | text_len)); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 245 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 246 | return mbedtls_asn1_write_len_and_tag(p, start, len, tag); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 247 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start, |
| 250 | const char *text, size_t text_len) |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 251 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start, |
| 256 | const char *text, size_t text_len) |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 257 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, |
| 259 | text_len); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start, |
| 263 | const char *text, size_t text_len) |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 264 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len); |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 266 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | int mbedtls_asn1_write_named_bitstring(unsigned char **p, |
| 269 | const unsigned char *start, |
| 270 | const unsigned char *buf, |
| 271 | size_t bits) |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 272 | { |
| 273 | size_t unused_bits, byte_len; |
| 274 | const unsigned char *cur_byte; |
| 275 | unsigned char cur_byte_shifted; |
| 276 | unsigned char bit; |
| 277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | byte_len = (bits + 7) / 8; |
| 279 | unused_bits = (byte_len * 8) - bits; |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * Named bitstrings require that trailing 0s are excluded in the encoding |
| 283 | * of the bitstring. Trailing 0s are considered part of the 'unused' bits |
| 284 | * when encoding this value in the first content octet |
| 285 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | if (bits != 0) { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 287 | cur_byte = buf + byte_len - 1; |
| 288 | cur_byte_shifted = *cur_byte >> unused_bits; |
| 289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | for (;;) { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 291 | bit = cur_byte_shifted & 0x1; |
| 292 | cur_byte_shifted >>= 1; |
| 293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | if (bit != 0) { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 295 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | } |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 297 | |
| 298 | bits--; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | if (bits == 0) { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 300 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | } |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | if (bits % 8 == 0) { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 304 | cur_byte_shifted = *--cur_byte; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | } |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | return mbedtls_asn1_write_bitstring(p, start, buf, bits); |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start, |
| 313 | const unsigned char *buf, size_t bits) |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 314 | { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 315 | size_t len = 0; |
| 316 | size_t unused_bits, byte_len; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | byte_len = (bits + 7) / 8; |
| 319 | unused_bits = (byte_len * 8) - bits; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | if (*p < start || (size_t) (*p - start) < byte_len + 1) { |
| 322 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 323 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 324 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 325 | len = byte_len + 1; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 326 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 327 | /* Write the bitstring. Ensure the unused bits are zeroed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if (byte_len > 0) { |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 329 | byte_len--; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1); |
| 331 | (*p) -= byte_len; |
| 332 | memcpy(*p, buf, byte_len); |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* Write unused bits */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | *--(*p) = (unsigned char) unused_bits; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 337 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 338 | return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start, |
| 342 | const unsigned char *buf, size_t size) |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 343 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 344 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 345 | size_t len = 0; |
| 346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size)); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 348 | |
Dave Rodgman | ecdfc1c | 2023-09-15 18:00:37 +0100 | [diff] [blame] | 349 | return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 350 | } |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 351 | |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 352 | |
Dave Rodgman | 4935283 | 2023-09-11 17:09:13 +0100 | [diff] [blame] | 353 | #if !defined(MBEDTLS_ASN1_PARSE_C) |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 354 | /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(), |
| 355 | * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */ |
| 356 | static mbedtls_asn1_named_data *asn1_find_named_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | mbedtls_asn1_named_data *list, |
| 358 | const char *oid, size_t len) |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 359 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | while (list != NULL) { |
| 361 | if (list->oid.len == len && |
| 362 | memcmp(list->oid.p, oid, len) == 0) { |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | |
| 366 | list = list->next; |
| 367 | } |
| 368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | return list; |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 370 | } |
Dave Rodgman | 4935283 | 2023-09-11 17:09:13 +0100 | [diff] [blame] | 371 | #else |
| 372 | #define asn1_find_named_data(list, oid, len) \ |
| 373 | ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len)) |
| 374 | #endif |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 375 | |
| 376 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | mbedtls_asn1_named_data **head, |
| 378 | const char *oid, size_t oid_len, |
| 379 | const unsigned char *val, |
| 380 | size_t val_len) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 382 | mbedtls_asn1_named_data *cur; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) { |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 385 | // Add new entry if not present yet based on OID |
| 386 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1, |
| 388 | sizeof(mbedtls_asn1_named_data)); |
| 389 | if (cur == NULL) { |
| 390 | return NULL; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | cur->oid.len = oid_len; |
| 394 | cur->oid.p = mbedtls_calloc(1, oid_len); |
| 395 | if (cur->oid.p == NULL) { |
| 396 | mbedtls_free(cur); |
| 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | memcpy(cur->oid.p, oid, oid_len); |
Manuel Pégourié-Gonnard | e5b0fc1 | 2014-11-12 22:27:42 +0100 | [diff] [blame] | 401 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 402 | cur->val.len = val_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | if (val_len != 0) { |
| 404 | cur->val.p = mbedtls_calloc(1, val_len); |
| 405 | if (cur->val.p == NULL) { |
| 406 | mbedtls_free(cur->oid.p); |
| 407 | mbedtls_free(cur); |
| 408 | return NULL; |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 409 | } |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 412 | cur->next = *head; |
| 413 | *head = cur; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | } else if (val_len == 0) { |
| 415 | mbedtls_free(cur->val.p); |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 416 | cur->val.p = NULL; |
Manuel Pégourié-Gonnard | 2df7ab7 | 2025-05-26 10:42:14 +0200 | [diff] [blame] | 417 | cur->val.len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | } else if (cur->val.len != val_len) { |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 419 | /* |
| 420 | * Enlarge existing value buffer if needed |
| 421 | * Preserve old data until the allocation succeeded, to leave list in |
| 422 | * a consistent state in case allocation fails. |
| 423 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | void *p = mbedtls_calloc(1, val_len); |
| 425 | if (p == NULL) { |
| 426 | return NULL; |
| 427 | } |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | mbedtls_free(cur->val.p); |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 430 | cur->val.p = p; |
| 431 | cur->val.len = val_len; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | if (val != NULL && val_len != 0) { |
| 435 | memcpy(cur->val.p, val, val_len); |
| 436 | } |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | return cur; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 439 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | #endif /* MBEDTLS_ASN1_WRITE_C */ |