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