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 | { \ |
| 32 | if( ( ret = (f) ) < 0 ) \ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 33 | return( ret ); \ |
| 34 | else \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 35 | (g) += ret; \ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 36 | } while( 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 37 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
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 | * \brief Write a length field in ASN.1 format. |
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 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 46 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 47 | * \param p The reference to the current position pointer. |
| 48 | * \param start The start of the buffer, for bounds-checking. |
| 49 | * \param len The length value to write. |
| 50 | * |
| 51 | * \return The number of bytes written to \p p on success. |
| 52 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 53 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 54 | int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 55 | size_t len ); |
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 | * \brief Write an ASN.1 tag in ASN.1 format. |
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 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 60 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 61 | * \param p The reference to the current position pointer. |
| 62 | * \param start The start of the buffer, for bounds-checking. |
| 63 | * \param tag The tag to write. |
| 64 | * |
| 65 | * \return The number of bytes written to \p p on success. |
| 66 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 67 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 68 | int mbedtls_asn1_write_tag( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 69 | unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 70 | |
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 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 84 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 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 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 102 | int mbedtls_asn1_write_mpi( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 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 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +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 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 134 | int mbedtls_asn1_write_oid( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 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 | */ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 152 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 153 | const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 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 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 158 | * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value |
| 159 | * in ASN.1 format. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 160 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 161 | * \note This function works backwards in data buffer. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 162 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 163 | * \param p The reference to the current position pointer. |
| 164 | * \param start The start of the buffer, for bounds-checking. |
| 165 | * \param boolean The boolean value to write, either \c 0 or \c 1. |
| 166 | * |
| 167 | * \return The number of bytes written to \p p on success. |
| 168 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 169 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 170 | int mbedtls_asn1_write_bool( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 171 | int boolean ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 174 | * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value |
| 175 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 176 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 177 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 178 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 179 | * \param p The reference to the current position pointer. |
| 180 | * \param start The start of the buffer, for bounds-checking. |
| 181 | * \param val The integer value to write. |
Gilles Peskine | 105031b | 2019-03-01 19:28:41 +0100 | [diff] [blame] | 182 | * It must be non-negative. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 183 | * |
| 184 | * \return The number of bytes written to \p p on success. |
| 185 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 186 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 187 | 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] | 188 | |
| 189 | /** |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 190 | * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value |
| 191 | * in ASN.1 format. |
| 192 | * |
| 193 | * \note This function works backwards in data buffer. |
| 194 | * |
| 195 | * \param p The reference to the current position pointer. |
| 196 | * \param start The start of the buffer, for bounds-checking. |
| 197 | * \param val The integer value to write. |
| 198 | * |
| 199 | * \return The number of bytes written to \p p on success. |
| 200 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 201 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 202 | 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] | 203 | |
| 204 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 205 | * \brief Write a string in ASN.1 format using a specific |
| 206 | * string encoding tag. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 207 | |
| 208 | * \note This function works backwards in data buffer. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 209 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 210 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 211 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 212 | * \param tag The string encoding tag to write, e.g. |
| 213 | * #MBEDTLS_ASN1_UTF8_STRING. |
| 214 | * \param text The string to write. |
| 215 | * \param text_len The length of \p text in bytes (which might |
| 216 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 217 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 218 | * \return The number of bytes written to \p p on success. |
| 219 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 220 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 221 | int mbedtls_asn1_write_tagged_string( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 222 | int tag, const char *text, |
| 223 | size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +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 the PrintableString |
| 227 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). |
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 text The string to write. |
| 234 | * \param text_len The length of \p text in bytes (which might |
| 235 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 236 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 237 | * \return The number of bytes written to \p p on success. |
| 238 | * \return A negative error code on failure. |
| 239 | */ |
| 240 | int mbedtls_asn1_write_printable_string( unsigned char **p, |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 241 | const unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 242 | const char *text, size_t text_len ); |
| 243 | |
| 244 | /** |
| 245 | * \brief Write a UTF8 string in ASN.1 format using the UTF8String |
Gilles Peskine | 105031b | 2019-03-01 19:28:41 +0100 | [diff] [blame] | 246 | * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 247 | * |
| 248 | * \note This function works backwards in data buffer. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 249 | * |
| 250 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 251 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 252 | * \param text The string to write. |
| 253 | * \param text_len The length of \p text in bytes (which might |
| 254 | * be strictly larger than the number of characters). |
| 255 | * |
| 256 | * \return The number of bytes written to \p p on success. |
| 257 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 258 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 259 | int mbedtls_asn1_write_utf8_string( unsigned char **p, const unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 260 | const char *text, size_t text_len ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 261 | |
| 262 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 263 | * \brief Write a string in ASN.1 format using the IA5String |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 264 | * string encoding tag (#MBEDTLS_ASN1_IA5_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 265 | * |
| 266 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 267 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 268 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 269 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 270 | * \param text The string to write. |
| 271 | * \param text_len The length of \p text in bytes (which might |
| 272 | * be strictly larger than the number of characters). |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 273 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 274 | * \return The number of bytes written to \p p on success. |
| 275 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 276 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 277 | int mbedtls_asn1_write_ia5_string( unsigned char **p, const unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 278 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 279 | |
| 280 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 281 | * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and |
| 282 | * value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 283 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 284 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 285 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 286 | * \param p The reference to the current position pointer. |
| 287 | * \param start The start of the buffer, for bounds-checking. |
| 288 | * \param buf The bitstring to write. |
| 289 | * \param bits The total number of bits in the bitstring. |
| 290 | * |
| 291 | * \return The number of bytes written to \p p on success. |
| 292 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 293 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 294 | int mbedtls_asn1_write_bitstring( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 295 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 296 | |
| 297 | /** |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 298 | * \brief This function writes a named bitstring tag |
| 299 | * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format. |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 300 | * |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 301 | * As stated in RFC 5280 Appendix B, trailing zeroes are |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 302 | * omitted when encoding named bitstrings in DER. |
| 303 | * |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 304 | * \note This function works backwards within the data buffer. |
| 305 | * |
| 306 | * \param p The reference to the current position pointer. |
| 307 | * \param start The start of the buffer which is used for bounds-checking. |
| 308 | * \param buf The bitstring to write. |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 309 | * \param bits The total number of bits in the bitstring. |
| 310 | * |
Andres Amaya Garcia | d8233f7 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 311 | * \return The number of bytes written to \p p on success. |
| 312 | * \return A negative error code on failure. |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 313 | */ |
| 314 | int mbedtls_asn1_write_named_bitstring( unsigned char **p, |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 315 | const unsigned char *start, |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 316 | const unsigned char *buf, |
| 317 | size_t bits ); |
| 318 | |
| 319 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 320 | * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING) |
| 321 | * and value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 322 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 323 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 324 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 325 | * \param p The reference to the current position pointer. |
| 326 | * \param start The start of the buffer, for bounds-checking. |
| 327 | * \param buf The buffer holding the data to write. |
| 328 | * \param size The length of the data buffer \p buf. |
| 329 | * |
| 330 | * \return The number of bytes written to \p p on success. |
| 331 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 332 | */ |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 333 | int mbedtls_asn1_write_octet_string( unsigned char **p, const unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 334 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * \brief Create or find a specific named_data entry for writing in a |
| 338 | * sequence or list based on the OID. If not already in there, |
| 339 | * a new entry is added to the head of the list. |
| 340 | * Warning: Destructive behaviour for the val data! |
| 341 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 342 | * \param list The pointer to the location of the head of the list to seek |
| 343 | * through (will be updated in case of a new entry). |
| 344 | * \param oid The OID to look for. |
| 345 | * \param oid_len The size of the OID. |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 346 | * \param val The associated data to store. If this is \c NULL, |
| 347 | * no data is copied to the new or existing buffer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 348 | * \param val_len The minimum length of the data buffer needed. |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 349 | * If this is 0, do not allocate a buffer for the associated |
| 350 | * data. |
| 351 | * If the OID was already present, enlarge, shrink or free |
| 352 | * the existing buffer to fit \p val_len. |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 353 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 354 | * \return A pointer to the new / existing entry on success. |
| 355 | * \return \c NULL if if there was a memory allocation error. |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 356 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list, |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 358 | const char *oid, size_t oid_len, |
| 359 | const unsigned char *val, |
| 360 | size_t val_len ); |
| 361 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 362 | #ifdef __cplusplus |
| 363 | } |
| 364 | #endif |
| 365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | #endif /* MBEDTLS_ASN1_WRITE_H */ |