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