Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file asn1write.h |
| 3 | * |
| 4 | * \brief ASN.1 buffer writing functionality |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
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 | #ifndef MBEDTLS_ASN1_WRITE_H |
| 23 | #define MBEDTLS_ASN1_WRITE_H |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 24 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 25 | #include "mbedtls/build_info.h" |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 26 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 27 | #include "mbedtls/asn1.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 28 | |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 29 | #define MBEDTLS_ASN1_CHK_ADD(g, f) \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 30 | do \ |
| 31 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | if ((ret = (f)) < 0) \ |
| 33 | return ret; \ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 34 | else \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | (g) += ret; \ |
| 36 | } while (0) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 37 | |
Przemek Stekiel | 5720771 | 2023-02-24 14:03:30 +0100 | [diff] [blame] | 38 | #define MBEDTLS_ASN1_CHK_CLEANUP_ADD(g, f) \ |
| 39 | do \ |
| 40 | { \ |
| 41 | if ((ret = (f)) < 0) \ |
| 42 | goto cleanup; \ |
| 43 | else \ |
| 44 | (g) += ret; \ |
| 45 | } while (0) |
| 46 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Agathiyan Bragadeesh | 86dc085 | 2023-09-04 14:53:30 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 52 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 53 | * \brief Write a length field in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 54 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 55 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 56 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 57 | * \param p The reference to the current position pointer. |
| 58 | * \param start The start of the buffer, for bounds-checking. |
| 59 | * \param len The length value to write. |
| 60 | * |
| 61 | * \return The number of bytes written to \p p on success. |
| 62 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 63 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, |
| 65 | size_t len); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 66 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 67 | * \brief Write an ASN.1 tag in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 68 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 69 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 70 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 71 | * \param p The reference to the current position pointer. |
| 72 | * \param start The start of the buffer, for bounds-checking. |
| 73 | * \param tag The tag to write. |
| 74 | * |
| 75 | * \return The number of bytes written to \p p on success. |
| 76 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 77 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, |
| 79 | unsigned char tag); |
Agathiyan Bragadeesh | 86dc085 | 2023-09-04 14:53:30 +0100 | [diff] [blame] | 80 | #endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 81 | |
Agathiyan Bragadeesh | 86dc085 | 2023-09-04 14:53:30 +0100 | [diff] [blame] | 82 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 83 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 84 | * \brief Write raw buffer data. |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 85 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 86 | * \note This function works backwards in data buffer. |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 87 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 88 | * \param p The reference to the current position pointer. |
| 89 | * \param start The start of the buffer, for bounds-checking. |
| 90 | * \param buf The data buffer to write. |
| 91 | * \param size The length of the data buffer. |
| 92 | * |
| 93 | * \return The number of bytes written to \p p on success. |
| 94 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 95 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start, |
| 97 | const unsigned char *buf, size_t size); |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 100 | /** |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 101 | * \brief Write an arbitrary-precision number (#MBEDTLS_ASN1_INTEGER) |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 102 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 103 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 104 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 105 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 106 | * \param p The reference to the current position pointer. |
| 107 | * \param start The start of the buffer, for bounds-checking. |
| 108 | * \param X The MPI to write. |
Gilles Peskine | 105031b | 2019-03-01 19:28:41 +0100 | [diff] [blame] | 109 | * It must be non-negative. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 110 | * |
| 111 | * \return The number of bytes written to \p p on success. |
| 112 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 113 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, |
| 115 | const mbedtls_mpi *X); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 117 | |
| 118 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 119 | * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data |
| 120 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 121 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 122 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 123 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 124 | * \param p The reference to the current position pointer. |
| 125 | * \param start The start of the buffer, for bounds-checking. |
| 126 | * |
| 127 | * \return The number of bytes written to \p p on success. |
| 128 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 129 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 133 | * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data |
| 134 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 135 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 136 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 137 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 138 | * \param p The reference to the current position pointer. |
| 139 | * \param start The start of the buffer, for bounds-checking. |
| 140 | * \param oid The OID to write. |
| 141 | * \param oid_len The length of the OID. |
| 142 | * |
| 143 | * \return The number of bytes written to \p p on success. |
| 144 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start, |
| 147 | const char *oid, size_t oid_len); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 150 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 151 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 152 | * \note This function works backwards in data buffer. |
| 153 | * |
| 154 | * \param p The reference to the current position pointer. |
| 155 | * \param start The start of the buffer, for bounds-checking. |
| 156 | * \param oid The OID of the algorithm to write. |
| 157 | * \param oid_len The length of the algorithm's OID. |
| 158 | * \param par_len The length of the parameters, which must be already written. |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 159 | * If 0, NULL parameters are added |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 160 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 161 | * \return The number of bytes written to \p p on success. |
| 162 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 163 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, |
| 165 | const unsigned char *start, |
| 166 | const char *oid, size_t oid_len, |
| 167 | size_t par_len); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 168 | |
| 169 | /** |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 170 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format. |
| 171 | * |
| 172 | * \note This function works backwards in data buffer. |
| 173 | * |
| 174 | * \param p The reference to the current position pointer. |
| 175 | * \param start The start of the buffer, for bounds-checking. |
| 176 | * \param oid The OID of the algorithm to write. |
| 177 | * \param oid_len The length of the algorithm's OID. |
| 178 | * \param par_len The length of the parameters, which must be already written. |
| 179 | * \param has_par If there are any parameters. If 0, par_len must be 0. If 1 |
| 180 | * and \p par_len is 0, NULL parameters are added. |
| 181 | * |
| 182 | * \return The number of bytes written to \p p on success. |
| 183 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 184 | */ |
| 185 | int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, |
| 186 | const unsigned char *start, |
| 187 | const char *oid, size_t oid_len, |
| 188 | size_t par_len, int has_par); |
| 189 | |
| 190 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 191 | * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value |
| 192 | * in ASN.1 format. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 193 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 194 | * \note This function works backwards in data buffer. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 195 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 196 | * \param p The reference to the current position pointer. |
| 197 | * \param start The start of the buffer, for bounds-checking. |
| 198 | * \param boolean The boolean value to write, either \c 0 or \c 1. |
| 199 | * |
| 200 | * \return The number of bytes written to \p p on success. |
| 201 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 202 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, |
| 204 | int boolean); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 205 | |
| 206 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 207 | * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value |
| 208 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 209 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 210 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 211 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 212 | * \param p The reference to the current position pointer. |
| 213 | * \param start The start of the buffer, for bounds-checking. |
| 214 | * \param val The integer value to write. |
Gilles Peskine | 105031b | 2019-03-01 19:28:41 +0100 | [diff] [blame] | 215 | * It must be non-negative. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 216 | * |
| 217 | * \return The number of bytes written to \p p on success. |
| 218 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 219 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 221 | |
| 222 | /** |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 223 | * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value |
| 224 | * in ASN.1 format. |
| 225 | * |
| 226 | * \note This function works backwards in data buffer. |
| 227 | * |
| 228 | * \param p The reference to the current position pointer. |
| 229 | * \param start The start of the buffer, for bounds-checking. |
| 230 | * \param val The integer value to write. |
| 231 | * |
| 232 | * \return The number of bytes written to \p p on success. |
| 233 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 234 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | 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] | 236 | |
| 237 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 238 | * \brief Write a string in ASN.1 format using a specific |
| 239 | * string encoding tag. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 240 | |
| 241 | * \note This function works backwards in data buffer. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 242 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 243 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 244 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 245 | * \param tag The string encoding tag to write, e.g. |
| 246 | * #MBEDTLS_ASN1_UTF8_STRING. |
| 247 | * \param text The string to write. |
| 248 | * \param text_len The length of \p text in bytes (which might |
| 249 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 250 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 251 | * \return The number of bytes written to \p p on success. |
| 252 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 253 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, |
| 255 | int tag, const char *text, |
| 256 | size_t text_len); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 257 | |
| 258 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 259 | * \brief Write a string in ASN.1 format using the PrintableString |
| 260 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 261 | * |
| 262 | * \note This function works backwards in data buffer. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 263 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 264 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 265 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 266 | * \param text The string to write. |
| 267 | * \param text_len The length of \p text in bytes (which might |
| 268 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 269 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 270 | * \return The number of bytes written to \p p on success. |
| 271 | * \return A negative error code on failure. |
| 272 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | int mbedtls_asn1_write_printable_string(unsigned char **p, |
| 274 | const unsigned char *start, |
| 275 | const char *text, size_t text_len); |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * \brief Write a UTF8 string in ASN.1 format using the UTF8String |
Gilles Peskine | 105031b | 2019-03-01 19:28:41 +0100 | [diff] [blame] | 279 | * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 280 | * |
| 281 | * \note This function works backwards in data buffer. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 282 | * |
| 283 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 284 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 285 | * \param text The string to write. |
| 286 | * \param text_len The length of \p text in bytes (which might |
| 287 | * be strictly larger than the number of characters). |
| 288 | * |
| 289 | * \return The number of bytes written to \p p on success. |
| 290 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 291 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start, |
| 293 | const char *text, size_t text_len); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 294 | |
| 295 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 296 | * \brief Write a string in ASN.1 format using the IA5String |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 297 | * string encoding tag (#MBEDTLS_ASN1_IA5_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 298 | * |
| 299 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 300 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 301 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 302 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 303 | * \param text The string to write. |
| 304 | * \param text_len The length of \p text in bytes (which might |
| 305 | * be strictly larger than the number of characters). |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 306 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 307 | * \return The number of bytes written to \p p on success. |
| 308 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 309 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start, |
| 311 | const char *text, size_t text_len); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 312 | |
| 313 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 314 | * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and |
| 315 | * value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 316 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 317 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 318 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 319 | * \param p The reference to the current position pointer. |
| 320 | * \param start The start of the buffer, for bounds-checking. |
| 321 | * \param buf The bitstring to write. |
| 322 | * \param bits The total number of bits in the bitstring. |
| 323 | * |
| 324 | * \return The number of bytes written to \p p on success. |
| 325 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 326 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start, |
| 328 | const unsigned char *buf, size_t bits); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 331 | * \brief This function writes a named bitstring tag |
| 332 | * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format. |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 333 | * |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 334 | * As stated in RFC 5280 Appendix B, trailing zeroes are |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 335 | * omitted when encoding named bitstrings in DER. |
| 336 | * |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 337 | * \note This function works backwards within the data buffer. |
| 338 | * |
| 339 | * \param p The reference to the current position pointer. |
| 340 | * \param start The start of the buffer which is used for bounds-checking. |
| 341 | * \param buf The bitstring to write. |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 342 | * \param bits The total number of bits in the bitstring. |
| 343 | * |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 344 | * \return The number of bytes written to \p p on success. |
| 345 | * \return A negative error code on failure. |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 346 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | int mbedtls_asn1_write_named_bitstring(unsigned char **p, |
| 348 | const unsigned char *start, |
| 349 | const unsigned char *buf, |
| 350 | size_t bits); |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 351 | |
| 352 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 353 | * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING) |
| 354 | * and value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 355 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 356 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 357 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 358 | * \param p The reference to the current position pointer. |
| 359 | * \param start The start of the buffer, for bounds-checking. |
| 360 | * \param buf The buffer holding the data to write. |
| 361 | * \param size The length of the data buffer \p buf. |
| 362 | * |
| 363 | * \return The number of bytes written to \p p on success. |
| 364 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 365 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start, |
| 367 | const unsigned char *buf, size_t size); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 368 | |
| 369 | /** |
| 370 | * \brief Create or find a specific named_data entry for writing in a |
| 371 | * sequence or list based on the OID. If not already in there, |
| 372 | * a new entry is added to the head of the list. |
| 373 | * Warning: Destructive behaviour for the val data! |
| 374 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 375 | * \param list The pointer to the location of the head of the list to seek |
| 376 | * through (will be updated in case of a new entry). |
| 377 | * \param oid The OID to look for. |
| 378 | * \param oid_len The size of the OID. |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 379 | * \param val The associated data to store. If this is \c NULL, |
| 380 | * no data is copied to the new or existing buffer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 381 | * \param val_len The minimum length of the data buffer needed. |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 382 | * If this is 0, do not allocate a buffer for the associated |
| 383 | * data. |
| 384 | * If the OID was already present, enlarge, shrink or free |
| 385 | * the existing buffer to fit \p val_len. |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 386 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 387 | * \return A pointer to the new / existing entry on success. |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 388 | * \return \c NULL if there was a memory allocation error. |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 389 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list, |
| 391 | const char *oid, size_t oid_len, |
| 392 | const unsigned char *val, |
| 393 | size_t val_len); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 394 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 395 | #ifdef __cplusplus |
| 396 | } |
| 397 | #endif |
| 398 | |
Agathiyan Bragadeesh | 86dc085 | 2023-09-04 14:53:30 +0100 | [diff] [blame] | 399 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
| 400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | #endif /* MBEDTLS_ASN1_WRITE_H */ |