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