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 | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | * |
| 10 | * This file is provided under the Apache License 2.0, or the |
| 11 | * GNU General Public License v2.0 or later. |
| 12 | * |
| 13 | * ********** |
| 14 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 27 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 28 | * ********** |
| 29 | * |
| 30 | * ********** |
| 31 | * GNU General Public License v2.0 or later: |
| 32 | * |
| 33 | * This program is free software; you can redistribute it and/or modify |
| 34 | * it under the terms of the GNU General Public License as published by |
| 35 | * the Free Software Foundation; either version 2 of the License, or |
| 36 | * (at your option) any later version. |
| 37 | * |
| 38 | * This program is distributed in the hope that it will be useful, |
| 39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 | * GNU General Public License for more details. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License along |
| 44 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 45 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 46 | * |
| 47 | * ********** |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 48 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #ifndef MBEDTLS_ASN1_WRITE_H |
| 50 | #define MBEDTLS_ASN1_WRITE_H |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 51 | |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 52 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 53 | #include "config.h" |
| 54 | #else |
| 55 | #include MBEDTLS_CONFIG_FILE |
| 56 | #endif |
| 57 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 58 | #include "asn1.h" |
| 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #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] | 61 | g += ret; } while( 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 62 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 63 | #ifdef __cplusplus |
| 64 | extern "C" { |
| 65 | #endif |
| 66 | |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 67 | /** |
| 68 | * \brief Write a length field in ASN.1 format |
| 69 | * Note: function works backwards in data buffer |
| 70 | * |
| 71 | * \param p reference to current position pointer |
| 72 | * \param start start of the buffer (for bounds-checking) |
| 73 | * \param len the length to write |
| 74 | * |
| 75 | * \return the length written or a negative error code |
| 76 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | 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] | 78 | |
| 79 | /** |
| 80 | * \brief Write a ASN.1 tag in ASN.1 format |
| 81 | * Note: function works backwards in data buffer |
| 82 | * |
| 83 | * \param p reference to current position pointer |
| 84 | * \param start start of the buffer (for bounds-checking) |
| 85 | * \param tag the tag to write |
| 86 | * |
| 87 | * \return the length written or a negative error code |
| 88 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 90 | unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 91 | |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 92 | /** |
| 93 | * \brief Write raw buffer data |
| 94 | * Note: function works backwards in data buffer |
| 95 | * |
| 96 | * \param p reference to current position pointer |
| 97 | * \param start start of the buffer (for bounds-checking) |
| 98 | * \param buf data buffer to write |
| 99 | * \param size length of the data buffer |
| 100 | * |
| 101 | * \return the length written or a negative error code |
| 102 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 104 | const unsigned char *buf, size_t size ); |
| 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 107 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 109 | * Note: function works backwards in data buffer |
| 110 | * |
| 111 | * \param p reference to current position pointer |
| 112 | * \param start start of the buffer (for bounds-checking) |
| 113 | * \param X the MPI to write |
| 114 | * |
| 115 | * \return the length written or a negative error code |
| 116 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X ); |
| 118 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 119 | |
| 120 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | * \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] | 122 | * Note: function works backwards in data buffer |
| 123 | * |
| 124 | * \param p reference to current position pointer |
| 125 | * \param start start of the buffer (for bounds-checking) |
| 126 | * |
| 127 | * \return the length written or a negative error code |
| 128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ); |
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 an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +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 oid the OID to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 138 | * \param oid_len length of the OID |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 139 | * |
| 140 | * \return the length written or a negative error code |
| 141 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 143 | const char *oid, size_t oid_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format |
| 147 | * Note: function works backwards in data buffer |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 148 | * |
| 149 | * \param p reference to current position pointer |
| 150 | * \param start start of the buffer (for bounds-checking) |
| 151 | * \param oid the OID of the algorithm |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 152 | * \param oid_len length of the OID |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 153 | * \param par_len length of parameters, which must be already written. |
| 154 | * If 0, NULL parameters are added |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 155 | * |
| 156 | * \return the length written or a negative error code |
| 157 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | 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] | 159 | const char *oid, size_t oid_len, |
| 160 | size_t par_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 161 | |
| 162 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | * \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] | 164 | * Note: function works backwards in data buffer |
| 165 | * |
| 166 | * \param p reference to current position pointer |
| 167 | * \param start start of the buffer (for bounds-checking) |
| 168 | * \param boolean 0 or 1 |
| 169 | * |
| 170 | * \return the length written or a negative error code |
| 171 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | 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] | 173 | |
| 174 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | * \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] | 176 | * Note: function works backwards in data buffer |
| 177 | * |
| 178 | * \param p reference to current position pointer |
| 179 | * \param start start of the buffer (for bounds-checking) |
| 180 | * \param val the integer value |
| 181 | * |
| 182 | * \return the length written or a negative error code |
| 183 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | 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] | 185 | |
| 186 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 188 | * value in ASN.1 format |
| 189 | * Note: function works backwards in data buffer |
| 190 | * |
| 191 | * \param p reference to current position pointer |
| 192 | * \param start start of the buffer (for bounds-checking) |
| 193 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 194 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 195 | * |
| 196 | * \return the length written or a negative error code |
| 197 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 199 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 200 | |
| 201 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 203 | * value in ASN.1 format |
| 204 | * Note: function works backwards in data buffer |
| 205 | * |
| 206 | * \param p reference to current position pointer |
| 207 | * \param start start of the buffer (for bounds-checking) |
| 208 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 209 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 210 | * |
| 211 | * \return the length written or a negative error code |
| 212 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 214 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 215 | |
| 216 | /** |
Andres Amaya Garcia | e730ff6 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 217 | * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and |
| 218 | * value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 219 | * |
Andres Amaya Garcia | e730ff6 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 220 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 221 | * |
Andres Amaya Garcia | e730ff6 | 2018-10-08 19:44:55 +0100 | [diff] [blame] | 222 | * \param p The reference to the current position pointer. |
| 223 | * \param start The start of the buffer, for bounds-checking. |
| 224 | * \param buf The bitstring to write. |
| 225 | * \param bits The total number of bits in the bitstring. |
| 226 | * |
| 227 | * \return The number of bytes written to \p p on success. |
| 228 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 229 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 231 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 232 | |
| 233 | /** |
Andres Amaya Garcia | 04ee5e0 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 234 | * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING) |
| 235 | * and value in ASN.1 format. |
| 236 | * |
| 237 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 238 | * |
| 239 | * \param p reference to current position pointer |
| 240 | * \param start start of the buffer (for bounds-checking) |
| 241 | * \param buf data buffer to write |
| 242 | * \param size length of the data buffer |
| 243 | * |
| 244 | * \return the length written or a negative error code |
| 245 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 247 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * \brief Create or find a specific named_data entry for writing in a |
| 251 | * sequence or list based on the OID. If not already in there, |
| 252 | * a new entry is added to the head of the list. |
| 253 | * Warning: Destructive behaviour for the val data! |
| 254 | * |
| 255 | * \param list Pointer to the location of the head of the list to seek |
| 256 | * through (will be updated in case of a new entry) |
| 257 | * \param oid The OID to look for |
| 258 | * \param oid_len Size of the OID |
| 259 | * \param val Data to store (can be NULL if you want to fill it by hand) |
| 260 | * \param val_len Minimum length of the data buffer needed |
| 261 | * |
| 262 | * \return NULL if if there was a memory allocation error, or a pointer |
| 263 | * to the new / existing entry. |
| 264 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | 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] | 266 | const char *oid, size_t oid_len, |
| 267 | const unsigned char *val, |
| 268 | size_t val_len ); |
| 269 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 270 | #ifdef __cplusplus |
| 271 | } |
| 272 | #endif |
| 273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | #endif /* MBEDTLS_ASN1_WRITE_H */ |