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