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 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_ASN1_WRITE_H |
| 28 | #define POLARSSL_ASN1_WRITE_H |
| 29 | |
| 30 | #include "asn1.h" |
| 31 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 32 | #define ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \ |
| 33 | g += ret; } while( 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 34 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 39 | /** |
| 40 | * \brief Write a length field in ASN.1 format |
| 41 | * Note: function works backwards in data buffer |
| 42 | * |
| 43 | * \param p reference to current position pointer |
| 44 | * \param start start of the buffer (for bounds-checking) |
| 45 | * \param len the length to write |
| 46 | * |
| 47 | * \return the length written or a negative error code |
| 48 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 49 | 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] | 50 | |
| 51 | /** |
| 52 | * \brief Write a ASN.1 tag in ASN.1 format |
| 53 | * Note: function works backwards in data buffer |
| 54 | * |
| 55 | * \param p reference to current position pointer |
| 56 | * \param start start of the buffer (for bounds-checking) |
| 57 | * \param tag the tag to write |
| 58 | * |
| 59 | * \return the length written or a negative error code |
| 60 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 61 | int asn1_write_tag( unsigned char **p, unsigned char *start, |
| 62 | unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 63 | |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 64 | /** |
| 65 | * \brief Write raw buffer data |
| 66 | * Note: function works backwards in data buffer |
| 67 | * |
| 68 | * \param p reference to current position pointer |
| 69 | * \param start start of the buffer (for bounds-checking) |
| 70 | * \param buf data buffer to write |
| 71 | * \param size length of the data buffer |
| 72 | * |
| 73 | * \return the length written or a negative error code |
| 74 | */ |
| 75 | int asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
| 76 | const unsigned char *buf, size_t size ); |
| 77 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 78 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 79 | /** |
| 80 | * \brief Write a big number (ASN1_INTEGER) 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 X the MPI to write |
| 86 | * |
| 87 | * \return the length written or a negative error code |
| 88 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 89 | int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 90 | #endif /* POLARSSL_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * \brief Write a NULL tag (ASN1_NULL) with zero data in ASN.1 format |
| 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 | * |
| 99 | * \return the length written or a negative error code |
| 100 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 101 | int asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * \brief Write an OID tag (ASN1_OID) and data in ASN.1 format |
| 105 | * Note: function works backwards in data buffer |
| 106 | * |
| 107 | * \param p reference to current position pointer |
| 108 | * \param start start of the buffer (for bounds-checking) |
| 109 | * \param oid the OID to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 110 | * \param oid_len length of the OID |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 111 | * |
| 112 | * \return the length written or a negative error code |
| 113 | */ |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 114 | int asn1_write_oid( unsigned char **p, unsigned char *start, |
| 115 | const char *oid, size_t oid_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format |
| 119 | * Note: function works backwards in data buffer |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 120 | * |
| 121 | * \param p reference to current position pointer |
| 122 | * \param start start of the buffer (for bounds-checking) |
| 123 | * \param oid the OID of the algorithm |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 124 | * \param oid_len length of the OID |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 125 | * \param par_len length of parameters, which must be already written. |
| 126 | * If 0, NULL parameters are added |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 127 | * |
| 128 | * \return the length written or a negative error code |
| 129 | */ |
| 130 | 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] | 131 | const char *oid, size_t oid_len, |
| 132 | size_t par_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 133 | |
| 134 | /** |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 135 | * \brief Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format |
| 136 | * Note: function works backwards in data buffer |
| 137 | * |
| 138 | * \param p reference to current position pointer |
| 139 | * \param start start of the buffer (for bounds-checking) |
| 140 | * \param boolean 0 or 1 |
| 141 | * |
| 142 | * \return the length written or a negative error code |
| 143 | */ |
| 144 | int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ); |
| 145 | |
| 146 | /** |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 147 | * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format |
| 148 | * Note: function works backwards in data buffer |
| 149 | * |
| 150 | * \param p reference to current position pointer |
| 151 | * \param start start of the buffer (for bounds-checking) |
| 152 | * \param val the integer value |
| 153 | * |
| 154 | * \return the length written or a negative error code |
| 155 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 156 | int asn1_write_int( unsigned char **p, unsigned char *start, int val ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and |
| 160 | * value in ASN.1 format |
| 161 | * Note: function works backwards in data buffer |
| 162 | * |
| 163 | * \param p reference to current position pointer |
| 164 | * \param start start of the buffer (for bounds-checking) |
| 165 | * \param text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 166 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 167 | * |
| 168 | * \return the length written or a negative error code |
| 169 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 170 | int asn1_write_printable_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 171 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * \brief Write an IA5 string tag (ASN1_IA5_STRING) and |
| 175 | * value in ASN.1 format |
| 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 text the text to write |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 181 | * \param text_len length of the text |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 182 | * |
| 183 | * \return the length written or a negative error code |
| 184 | */ |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 185 | int asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 186 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * \brief Write a bitstring tag (ASN1_BIT_STRING) and |
| 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 buf the bitstring |
| 196 | * \param bits the total number of bits in the bitstring |
| 197 | * |
| 198 | * \return the length written or a negative error code |
| 199 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 200 | int asn1_write_bitstring( unsigned char **p, unsigned char *start, |
| 201 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * \brief Write an octet string tag (ASN1_OCTET_STRING) and |
| 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 buf data buffer to write |
| 211 | * \param size length of the data buffer |
| 212 | * |
| 213 | * \return the length written or a negative error code |
| 214 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 215 | int asn1_write_octet_string( unsigned char **p, unsigned char *start, |
| 216 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * \brief Create or find a specific named_data entry for writing in a |
| 220 | * sequence or list based on the OID. If not already in there, |
| 221 | * a new entry is added to the head of the list. |
| 222 | * Warning: Destructive behaviour for the val data! |
| 223 | * |
| 224 | * \param list Pointer to the location of the head of the list to seek |
| 225 | * through (will be updated in case of a new entry) |
| 226 | * \param oid The OID to look for |
| 227 | * \param oid_len Size of the OID |
| 228 | * \param val Data to store (can be NULL if you want to fill it by hand) |
| 229 | * \param val_len Minimum length of the data buffer needed |
| 230 | * |
| 231 | * \return NULL if if there was a memory allocation error, or a pointer |
| 232 | * to the new / existing entry. |
| 233 | */ |
| 234 | asn1_named_data *asn1_store_named_data( asn1_named_data **list, |
| 235 | const char *oid, size_t oid_len, |
| 236 | const unsigned char *val, |
| 237 | size_t val_len ); |
| 238 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |
| 242 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 243 | #endif /* POLARSSL_ASN1_WRITE_H */ |