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