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 |
| 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 22 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_ASN1_WRITE_H |
| 24 | #define MBEDTLS_ASN1_WRITE_H |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 25 | |
| 26 | #include "asn1.h" |
| 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #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] | 29 | g += ret; } while( 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 30 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 35 | /** |
| 36 | * \brief Write a length field in ASN.1 format |
| 37 | * Note: function works backwards in data buffer |
| 38 | * |
| 39 | * \param p reference to current position pointer |
| 40 | * \param start start of the buffer (for bounds-checking) |
| 41 | * \param len the length to write |
| 42 | * |
| 43 | * \return the length written or a negative error code |
| 44 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | 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] | 46 | |
| 47 | /** |
| 48 | * \brief Write a ASN.1 tag in ASN.1 format |
| 49 | * Note: function works backwards in data buffer |
| 50 | * |
| 51 | * \param p reference to current position pointer |
| 52 | * \param start start of the buffer (for bounds-checking) |
| 53 | * \param tag the tag to write |
| 54 | * |
| 55 | * \return the length written or a negative error code |
| 56 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 58 | unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 59 | |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 60 | /** |
| 61 | * \brief Write raw buffer data |
| 62 | * Note: function works backwards in data buffer |
| 63 | * |
| 64 | * \param p reference to current position pointer |
| 65 | * \param start start of the buffer (for bounds-checking) |
| 66 | * \param buf data buffer to write |
| 67 | * \param size length of the data buffer |
| 68 | * |
| 69 | * \return the length written or a negative error code |
| 70 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 72 | const unsigned char *buf, size_t size ); |
| 73 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 75 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 77 | * Note: function works backwards in data buffer |
| 78 | * |
| 79 | * \param p reference to current position pointer |
| 80 | * \param start start of the buffer (for bounds-checking) |
| 81 | * \param X the MPI to write |
| 82 | * |
| 83 | * \return the length written or a negative error code |
| 84 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X ); |
| 86 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | * \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] | 90 | * Note: function works backwards in data buffer |
| 91 | * |
| 92 | * \param p reference to current position pointer |
| 93 | * \param start start of the buffer (for bounds-checking) |
| 94 | * |
| 95 | * \return the length written or a negative error code |
| 96 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | * \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] | 101 | * Note: function works backwards in data buffer |
| 102 | * |
| 103 | * \param p reference to current position pointer |
| 104 | * \param start start of the buffer (for bounds-checking) |
| 105 | * \param oid the OID to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 106 | * \param oid_len length of the OID |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 107 | * |
| 108 | * \return the length written or a negative error code |
| 109 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 111 | const char *oid, size_t oid_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format |
| 115 | * Note: function works backwards in data buffer |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 116 | * |
| 117 | * \param p reference to current position pointer |
| 118 | * \param start start of the buffer (for bounds-checking) |
| 119 | * \param oid the OID of the algorithm |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 120 | * \param oid_len length of the OID |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 121 | * \param par_len length of parameters, which must be already written. |
| 122 | * If 0, NULL parameters are added |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 123 | * |
| 124 | * \return the length written or a negative error code |
| 125 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | 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] | 127 | const char *oid, size_t oid_len, |
| 128 | size_t par_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 129 | |
| 130 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | * \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] | 132 | * Note: function works backwards in data buffer |
| 133 | * |
| 134 | * \param p reference to current position pointer |
| 135 | * \param start start of the buffer (for bounds-checking) |
| 136 | * \param boolean 0 or 1 |
| 137 | * |
| 138 | * \return the length written or a negative error code |
| 139 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | 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] | 141 | |
| 142 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | * \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] | 144 | * Note: function works backwards in data buffer |
| 145 | * |
| 146 | * \param p reference to current position pointer |
| 147 | * \param start start of the buffer (for bounds-checking) |
| 148 | * \param val the integer value |
| 149 | * |
| 150 | * \return the length written or a negative error code |
| 151 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | 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] | 153 | |
| 154 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 156 | * value in ASN.1 format |
| 157 | * Note: function works backwards in data buffer |
| 158 | * |
| 159 | * \param p reference to current position pointer |
| 160 | * \param start start of the buffer (for bounds-checking) |
| 161 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 162 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 163 | * |
| 164 | * \return the length written or a negative error code |
| 165 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 167 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 168 | |
| 169 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 171 | * value in ASN.1 format |
| 172 | * Note: function works backwards in data buffer |
| 173 | * |
| 174 | * \param p reference to current position pointer |
| 175 | * \param start start of the buffer (for bounds-checking) |
| 176 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 177 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 178 | * |
| 179 | * \return the length written or a negative error code |
| 180 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 182 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 183 | |
| 184 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 186 | * value in ASN.1 format |
| 187 | * Note: function works backwards in data buffer |
| 188 | * |
| 189 | * \param p reference to current position pointer |
| 190 | * \param start start of the buffer (for bounds-checking) |
| 191 | * \param buf the bitstring |
| 192 | * \param bits the total number of bits in the bitstring |
| 193 | * |
| 194 | * \return the length written or a negative error code |
| 195 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 197 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 198 | |
| 199 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 201 | * value in ASN.1 format |
| 202 | * Note: function works backwards in data buffer |
| 203 | * |
| 204 | * \param p reference to current position pointer |
| 205 | * \param start start of the buffer (for bounds-checking) |
| 206 | * \param buf data buffer to write |
| 207 | * \param size length of the data buffer |
| 208 | * |
| 209 | * \return the length written or a negative error code |
| 210 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 212 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * \brief Create or find a specific named_data entry for writing in a |
| 216 | * sequence or list based on the OID. If not already in there, |
| 217 | * a new entry is added to the head of the list. |
| 218 | * Warning: Destructive behaviour for the val data! |
| 219 | * |
| 220 | * \param list Pointer to the location of the head of the list to seek |
| 221 | * through (will be updated in case of a new entry) |
| 222 | * \param oid The OID to look for |
| 223 | * \param oid_len Size of the OID |
| 224 | * \param val Data to store (can be NULL if you want to fill it by hand) |
| 225 | * \param val_len Minimum length of the data buffer needed |
| 226 | * |
| 227 | * \return NULL if if there was a memory allocation error, or a pointer |
| 228 | * to the new / existing entry. |
| 229 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | 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] | 231 | const char *oid, size_t oid_len, |
| 232 | const unsigned char *val, |
| 233 | size_t val_len ); |
| 234 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 235 | #ifdef __cplusplus |
| 236 | } |
| 237 | #endif |
| 238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | #endif /* MBEDTLS_ASN1_WRITE_H */ |