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 | |
| 27 | #include "asn1.h" |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 30 | g += ret; } while( 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 31 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 36 | /** |
| 37 | * \brief Write a length field in ASN.1 format |
| 38 | * Note: function works backwards in data buffer |
| 39 | * |
| 40 | * \param p reference to current position pointer |
| 41 | * \param start start of the buffer (for bounds-checking) |
| 42 | * \param len the length to write |
| 43 | * |
| 44 | * \return the length written or a negative error code |
| 45 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * \brief Write a ASN.1 tag in ASN.1 format |
| 50 | * Note: function works backwards in data buffer |
| 51 | * |
| 52 | * \param p reference to current position pointer |
| 53 | * \param start start of the buffer (for bounds-checking) |
| 54 | * \param tag the tag to write |
| 55 | * |
| 56 | * \return the length written or a negative error code |
| 57 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 59 | unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 60 | |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 61 | /** |
| 62 | * \brief Write raw buffer data |
| 63 | * Note: function works backwards in data buffer |
| 64 | * |
| 65 | * \param p reference to current position pointer |
| 66 | * \param start start of the buffer (for bounds-checking) |
| 67 | * \param buf data buffer to write |
| 68 | * \param size length of the data buffer |
| 69 | * |
| 70 | * \return the length written or a negative error code |
| 71 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 73 | const unsigned char *buf, size_t size ); |
| 74 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 76 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 78 | * Note: function works backwards in data buffer |
| 79 | * |
| 80 | * \param p reference to current position pointer |
| 81 | * \param start start of the buffer (for bounds-checking) |
| 82 | * \param X the MPI to write |
| 83 | * |
| 84 | * \return the length written or a negative error code |
| 85 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X ); |
| 87 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 91 | * Note: function works backwards in data buffer |
| 92 | * |
| 93 | * \param p reference to current position pointer |
| 94 | * \param start start of the buffer (for bounds-checking) |
| 95 | * |
| 96 | * \return the length written or a negative error code |
| 97 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 102 | * Note: function works backwards in data buffer |
| 103 | * |
| 104 | * \param p reference to current position pointer |
| 105 | * \param start start of the buffer (for bounds-checking) |
| 106 | * \param oid the OID to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 107 | * \param oid_len length of the OID |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 108 | * |
| 109 | * \return the length written or a negative error code |
| 110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 112 | const char *oid, size_t oid_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format |
| 116 | * Note: function works backwards in data buffer |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 117 | * |
| 118 | * \param p reference to current position pointer |
| 119 | * \param start start of the buffer (for bounds-checking) |
| 120 | * \param oid the OID of the algorithm |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 121 | * \param oid_len length of the OID |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 122 | * \param par_len length of parameters, which must be already written. |
| 123 | * If 0, NULL parameters are added |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 124 | * |
| 125 | * \return the length written or a negative error code |
| 126 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 128 | const char *oid, size_t oid_len, |
| 129 | size_t par_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 130 | |
| 131 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 133 | * Note: function works backwards in data buffer |
| 134 | * |
| 135 | * \param p reference to current position pointer |
| 136 | * \param start start of the buffer (for bounds-checking) |
| 137 | * \param boolean 0 or 1 |
| 138 | * |
| 139 | * \return the length written or a negative error code |
| 140 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 142 | |
| 143 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 145 | * Note: function works backwards in data buffer |
| 146 | * |
| 147 | * \param p reference to current position pointer |
| 148 | * \param start start of the buffer (for bounds-checking) |
| 149 | * \param val the integer value |
| 150 | * |
| 151 | * \return the length written or a negative error code |
| 152 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | 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] | 154 | |
| 155 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 156 | * \brief Write a string in ASN.1 format using a specific |
| 157 | * string encoding tag. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 158 | * Note: function works backwards in data buffer |
| 159 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 160 | * \param p The reference to the current position pointer. |
| 161 | * \param start The start of the buffer (for bounds-checking). |
| 162 | * \param tag The string encoding tag to write, e.g. |
| 163 | * #MBEDTLS_ASN1_UTF8_STRING. |
| 164 | * \param text The string to write. |
| 165 | * \param text_len The length of \p text in bytes (which might |
| 166 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 167 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 168 | * \return The number of bytes written to \p p on success. |
| 169 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 170 | */ |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 171 | int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 172 | int tag, const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 173 | |
| 174 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 175 | * \brief Write a string in ASN.1 format using the PrintableString |
| 176 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). |
| 177 | * Note: The function works backwards in data buffer. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 178 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +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 text The string to write. |
| 182 | * \param text_len The length of \p text in bytes (which might |
| 183 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 184 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 185 | * \return The number of bytes written to \p p on success. |
| 186 | * \return A negative error code on failure. |
| 187 | */ |
| 188 | int mbedtls_asn1_write_printable_string( unsigned char **p, |
| 189 | unsigned char *start, |
| 190 | const char *text, size_t text_len ); |
| 191 | |
| 192 | /** |
| 193 | * \brief Write a UTF8 string in ASN.1 format using the UTF8String |
| 194 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). |
| 195 | * Note: The function works backwards in data buffer. |
| 196 | * |
| 197 | * \param p The reference to the current position pointer. |
| 198 | * \param start The start of the buffer (for bounds-checking). |
| 199 | * \param text The string to write. |
| 200 | * \param text_len The length of \p text in bytes (which might |
| 201 | * be strictly larger than the number of characters). |
| 202 | * |
| 203 | * \return The number of bytes written to \p p on success. |
| 204 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 205 | */ |
| 206 | int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 207 | const char *text, size_t text_len ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 208 | |
| 209 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 210 | * \brief Write a string in ASN.1 format using the IA5tring |
| 211 | * string encoding tag (#MBEDTLS_ASN1_IA5_STRING). |
| 212 | * Note: The function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 213 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 214 | * \param p The reference to the current position pointer. |
| 215 | * \param start The start of the buffer (for bounds-checking). |
| 216 | * \param text The string to write. |
| 217 | * \param text_len The length of \p text in bytes (which might |
| 218 | * be strictly larger than the number of characters). |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 219 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 220 | * \return The number of bytes written to \p p on success. |
| 221 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 222 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame^] | 224 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 225 | |
| 226 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 228 | * value in ASN.1 format |
| 229 | * Note: function works backwards in data buffer |
| 230 | * |
| 231 | * \param p reference to current position pointer |
| 232 | * \param start start of the buffer (for bounds-checking) |
| 233 | * \param buf the bitstring |
| 234 | * \param bits the total number of bits in the bitstring |
| 235 | * |
| 236 | * \return the length written or a negative error code |
| 237 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 239 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 240 | |
| 241 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 243 | * value in ASN.1 format |
| 244 | * Note: function works backwards in data buffer |
| 245 | * |
| 246 | * \param p reference to current position pointer |
| 247 | * \param start start of the buffer (for bounds-checking) |
| 248 | * \param buf data buffer to write |
| 249 | * \param size length of the data buffer |
| 250 | * |
| 251 | * \return the length written or a negative error code |
| 252 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 254 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * \brief Create or find a specific named_data entry for writing in a |
| 258 | * sequence or list based on the OID. If not already in there, |
| 259 | * a new entry is added to the head of the list. |
| 260 | * Warning: Destructive behaviour for the val data! |
| 261 | * |
| 262 | * \param list Pointer to the location of the head of the list to seek |
| 263 | * through (will be updated in case of a new entry) |
| 264 | * \param oid The OID to look for |
| 265 | * \param oid_len Size of the OID |
| 266 | * \param val Data to store (can be NULL if you want to fill it by hand) |
| 267 | * \param val_len Minimum length of the data buffer needed |
| 268 | * |
| 269 | * \return NULL if if there was a memory allocation error, or a pointer |
| 270 | * to the new / existing entry. |
| 271 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | 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] | 273 | const char *oid, size_t oid_len, |
| 274 | const unsigned char *val, |
| 275 | size_t val_len ); |
| 276 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 277 | #ifdef __cplusplus |
| 278 | } |
| 279 | #endif |
| 280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | #endif /* MBEDTLS_ASN1_WRITE_H */ |