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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | #ifndef POLARSSL_ASN1_WRITE_H |
| 25 | #define POLARSSL_ASN1_WRITE_H |
| 26 | |
| 27 | #include "asn1.h" |
| 28 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 29 | #define ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \ |
| 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 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 46 | int 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 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 58 | int asn1_write_tag( unsigned char **p, unsigned char *start, |
| 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 | */ |
| 72 | int asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
| 73 | const unsigned char *buf, size_t size ); |
| 74 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 75 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 76 | /** |
| 77 | * \brief Write a big number (ASN1_INTEGER) in ASN.1 format |
| 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 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 86 | int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 87 | #endif /* POLARSSL_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * \brief Write a NULL tag (ASN1_NULL) with zero data in ASN.1 format |
| 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 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 98 | int asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * \brief Write an OID tag (ASN1_OID) and data in ASN.1 format |
| 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 | */ |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 111 | int asn1_write_oid( unsigned char **p, unsigned char *start, |
| 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 | */ |
| 127 | int 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 | /** |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 132 | * \brief Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format |
| 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 | */ |
| 141 | int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ); |
| 142 | |
| 143 | /** |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 144 | * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format |
| 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 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 153 | int 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 | /** |
| 156 | * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and |
| 157 | * value in ASN.1 format |
| 158 | * Note: function works backwards in data buffer |
| 159 | * |
| 160 | * \param p reference to current position pointer |
| 161 | * \param start start of the buffer (for bounds-checking) |
| 162 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 163 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 164 | * |
| 165 | * \return the length written or a negative error code |
| 166 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 167 | int asn1_write_printable_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 168 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * \brief Write an IA5 string tag (ASN1_IA5_STRING) and |
| 172 | * value in ASN.1 format |
| 173 | * Note: function works backwards in data buffer |
| 174 | * |
| 175 | * \param p reference to current position pointer |
| 176 | * \param start start of the buffer (for bounds-checking) |
| 177 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 178 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 179 | * |
| 180 | * \return the length written or a negative error code |
| 181 | */ |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 182 | int asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 183 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * \brief Write a bitstring tag (ASN1_BIT_STRING) and |
| 187 | * value in ASN.1 format |
| 188 | * Note: function works backwards in data buffer |
| 189 | * |
| 190 | * \param p reference to current position pointer |
| 191 | * \param start start of the buffer (for bounds-checking) |
| 192 | * \param buf the bitstring |
| 193 | * \param bits the total number of bits in the bitstring |
| 194 | * |
| 195 | * \return the length written or a negative error code |
| 196 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 197 | int asn1_write_bitstring( unsigned char **p, unsigned char *start, |
| 198 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * \brief Write an octet string tag (ASN1_OCTET_STRING) and |
| 202 | * value in ASN.1 format |
| 203 | * Note: function works backwards in data buffer |
| 204 | * |
| 205 | * \param p reference to current position pointer |
| 206 | * \param start start of the buffer (for bounds-checking) |
| 207 | * \param buf data buffer to write |
| 208 | * \param size length of the data buffer |
| 209 | * |
| 210 | * \return the length written or a negative error code |
| 211 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 212 | int asn1_write_octet_string( unsigned char **p, unsigned char *start, |
| 213 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * \brief Create or find a specific named_data entry for writing in a |
| 217 | * sequence or list based on the OID. If not already in there, |
| 218 | * a new entry is added to the head of the list. |
| 219 | * Warning: Destructive behaviour for the val data! |
| 220 | * |
| 221 | * \param list Pointer to the location of the head of the list to seek |
| 222 | * through (will be updated in case of a new entry) |
| 223 | * \param oid The OID to look for |
| 224 | * \param oid_len Size of the OID |
| 225 | * \param val Data to store (can be NULL if you want to fill it by hand) |
| 226 | * \param val_len Minimum length of the data buffer needed |
| 227 | * |
| 228 | * \return NULL if if there was a memory allocation error, or a pointer |
| 229 | * to the new / existing entry. |
| 230 | */ |
| 231 | asn1_named_data *asn1_store_named_data( asn1_named_data **list, |
| 232 | const char *oid, size_t oid_len, |
| 233 | const unsigned char *val, |
| 234 | size_t val_len ); |
| 235 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 236 | #ifdef __cplusplus |
| 237 | } |
| 238 | #endif |
| 239 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 240 | #endif /* POLARSSL_ASN1_WRITE_H */ |