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