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