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