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