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