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