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