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 | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, 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 | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 32 | #define ASN1_CHK_ADD(g, f) if( ( ret = f ) < 0 ) return( ret ); else g += ret |
| 33 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 38 | /** |
| 39 | * \brief Write a length field in ASN.1 format |
| 40 | * Note: function works backwards in data buffer |
| 41 | * |
| 42 | * \param p reference to current position pointer |
| 43 | * \param start start of the buffer (for bounds-checking) |
| 44 | * \param len the length to write |
| 45 | * |
| 46 | * \return the length written or a negative error code |
| 47 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 48 | 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] | 49 | |
| 50 | /** |
| 51 | * \brief Write a ASN.1 tag in ASN.1 format |
| 52 | * Note: function works backwards in data buffer |
| 53 | * |
| 54 | * \param p reference to current position pointer |
| 55 | * \param start start of the buffer (for bounds-checking) |
| 56 | * \param tag the tag to write |
| 57 | * |
| 58 | * \return the length written or a negative error code |
| 59 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 60 | int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 61 | |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 62 | /** |
| 63 | * \brief Write raw buffer data |
| 64 | * Note: function works backwards in data buffer |
| 65 | * |
| 66 | * \param p reference to current position pointer |
| 67 | * \param start start of the buffer (for bounds-checking) |
| 68 | * \param buf data buffer to write |
| 69 | * \param size length of the data buffer |
| 70 | * |
| 71 | * \return the length written or a negative error code |
| 72 | */ |
| 73 | int asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
| 74 | const unsigned char *buf, size_t size ); |
| 75 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 76 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 77 | /** |
| 78 | * \brief Write a big number (ASN1_INTEGER) in ASN.1 format |
| 79 | * Note: function works backwards in data buffer |
| 80 | * |
| 81 | * \param p reference to current position pointer |
| 82 | * \param start start of the buffer (for bounds-checking) |
| 83 | * \param X the MPI to write |
| 84 | * |
| 85 | * \return the length written or a negative error code |
| 86 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 87 | int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 88 | #endif |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * \brief Write a NULL tag (ASN1_NULL) with zero data in ASN.1 format |
| 92 | * Note: function works backwards in data buffer |
| 93 | * |
| 94 | * \param p reference to current position pointer |
| 95 | * \param start start of the buffer (for bounds-checking) |
| 96 | * |
| 97 | * \return the length written or a negative error code |
| 98 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 99 | int asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * \brief Write an OID tag (ASN1_OID) and data in ASN.1 format |
| 103 | * Note: function works backwards in data buffer |
| 104 | * |
| 105 | * \param p reference to current position pointer |
| 106 | * \param start start of the buffer (for bounds-checking) |
| 107 | * \param oid the OID to write |
| 108 | * |
| 109 | * \return the length written or a negative error code |
| 110 | */ |
Paul Bakker | 37de6be | 2013-04-07 13:11:31 +0200 | [diff] [blame] | 111 | int asn1_write_oid( unsigned char **p, unsigned char *start, const char *oid ); |
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 |
| 116 | * Note: Uses NULL as algorithm parameter |
| 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 |
| 121 | * |
| 122 | * \return the length written or a negative error code |
| 123 | */ |
| 124 | int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, |
| 125 | const char *oid ); |
| 126 | |
| 127 | /** |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 128 | * \brief Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format |
| 129 | * Note: function works backwards in data buffer |
| 130 | * |
| 131 | * \param p reference to current position pointer |
| 132 | * \param start start of the buffer (for bounds-checking) |
| 133 | * \param boolean 0 or 1 |
| 134 | * |
| 135 | * \return the length written or a negative error code |
| 136 | */ |
| 137 | int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ); |
| 138 | |
| 139 | /** |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 140 | * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format |
| 141 | * Note: function works backwards in data buffer |
| 142 | * |
| 143 | * \param p reference to current position pointer |
| 144 | * \param start start of the buffer (for bounds-checking) |
| 145 | * \param val the integer value |
| 146 | * |
| 147 | * \return the length written or a negative error code |
| 148 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 149 | int asn1_write_int( unsigned char **p, unsigned char *start, int val ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and |
| 153 | * value in ASN.1 format |
| 154 | * Note: function works backwards in data buffer |
| 155 | * |
| 156 | * \param p reference to current position pointer |
| 157 | * \param start start of the buffer (for bounds-checking) |
| 158 | * \param text the text to write |
| 159 | * |
| 160 | * \return the length written or a negative error code |
| 161 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 162 | int asn1_write_printable_string( unsigned char **p, unsigned char *start, |
| 163 | char *text ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * \brief Write an IA5 string tag (ASN1_IA5_STRING) and |
| 167 | * value in ASN.1 format |
| 168 | * Note: function works backwards in data buffer |
| 169 | * |
| 170 | * \param p reference to current position pointer |
| 171 | * \param start start of the buffer (for bounds-checking) |
| 172 | * \param text the text to write |
| 173 | * |
| 174 | * \return the length written or a negative error code |
| 175 | */ |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 176 | int asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
| 177 | char *text ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * \brief Write a bitstring tag (ASN1_BIT_STRING) and |
| 181 | * value in ASN.1 format |
| 182 | * Note: function works backwards in data buffer |
| 183 | * |
| 184 | * \param p reference to current position pointer |
| 185 | * \param start start of the buffer (for bounds-checking) |
| 186 | * \param buf the bitstring |
| 187 | * \param bits the total number of bits in the bitstring |
| 188 | * |
| 189 | * \return the length written or a negative error code |
| 190 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 191 | int asn1_write_bitstring( unsigned char **p, unsigned char *start, |
| 192 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * \brief Write an octet string tag (ASN1_OCTET_STRING) and |
| 196 | * value in ASN.1 format |
| 197 | * Note: function works backwards in data buffer |
| 198 | * |
| 199 | * \param p reference to current position pointer |
| 200 | * \param start start of the buffer (for bounds-checking) |
| 201 | * \param buf data buffer to write |
| 202 | * \param size length of the data buffer |
| 203 | * |
| 204 | * \return the length written or a negative error code |
| 205 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 206 | int asn1_write_octet_string( unsigned char **p, unsigned char *start, |
| 207 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame^] | 208 | |
| 209 | /** |
| 210 | * \brief Create or find a specific named_data entry for writing in a |
| 211 | * sequence or list based on the OID. If not already in there, |
| 212 | * a new entry is added to the head of the list. |
| 213 | * Warning: Destructive behaviour for the val data! |
| 214 | * |
| 215 | * \param list Pointer to the location of the head of the list to seek |
| 216 | * through (will be updated in case of a new entry) |
| 217 | * \param oid The OID to look for |
| 218 | * \param oid_len Size of the OID |
| 219 | * \param val Data to store (can be NULL if you want to fill it by hand) |
| 220 | * \param val_len Minimum length of the data buffer needed |
| 221 | * |
| 222 | * \return NULL if if there was a memory allocation error, or a pointer |
| 223 | * to the new / existing entry. |
| 224 | */ |
| 225 | asn1_named_data *asn1_store_named_data( asn1_named_data **list, |
| 226 | const char *oid, size_t oid_len, |
| 227 | const unsigned char *val, |
| 228 | size_t val_len ); |
| 229 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 230 | #ifdef __cplusplus |
| 231 | } |
| 232 | #endif |
| 233 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 234 | #endif /* POLARSSL_ASN1_WRITE_H */ |