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 | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 62 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 63 | /** |
| 64 | * \brief Write a big number (ASN1_INTEGER) in ASN.1 format |
| 65 | * Note: function works backwards in data buffer |
| 66 | * |
| 67 | * \param p reference to current position pointer |
| 68 | * \param start start of the buffer (for bounds-checking) |
| 69 | * \param X the MPI to write |
| 70 | * |
| 71 | * \return the length written or a negative error code |
| 72 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 73 | int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 74 | #endif |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 75 | |
| 76 | /** |
| 77 | * \brief Write a NULL tag (ASN1_NULL) with zero data 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 | * |
| 83 | * \return the length written or a negative error code |
| 84 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 85 | int asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 86 | |
| 87 | /** |
| 88 | * \brief Write an OID tag (ASN1_OID) and data in ASN.1 format |
| 89 | * Note: function works backwards in data buffer |
| 90 | * |
| 91 | * \param p reference to current position pointer |
| 92 | * \param start start of the buffer (for bounds-checking) |
| 93 | * \param oid the OID to write |
| 94 | * |
| 95 | * \return the length written or a negative error code |
| 96 | */ |
Paul Bakker | 37de6be | 2013-04-07 13:11:31 +0200 | [diff] [blame] | 97 | 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^] | 98 | |
| 99 | /** |
| 100 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format |
| 101 | * Note: function works backwards in data buffer |
| 102 | * Note: Uses NULL as algorithm parameter |
| 103 | * |
| 104 | * \param p reference to current position pointer |
| 105 | * \param start start of the buffer (for bounds-checking) |
| 106 | * \param oid the OID of the algorithm |
| 107 | * |
| 108 | * \return the length written or a negative error code |
| 109 | */ |
| 110 | int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, |
| 111 | const char *oid ); |
| 112 | |
| 113 | /** |
| 114 | * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format |
| 115 | * Note: function works backwards in data buffer |
| 116 | * |
| 117 | * \param p reference to current position pointer |
| 118 | * \param start start of the buffer (for bounds-checking) |
| 119 | * \param val the integer value |
| 120 | * |
| 121 | * \return the length written or a negative error code |
| 122 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 123 | int asn1_write_int( unsigned char **p, unsigned char *start, int val ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 124 | |
| 125 | /** |
| 126 | * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and |
| 127 | * value in ASN.1 format |
| 128 | * Note: function works backwards in data buffer |
| 129 | * |
| 130 | * \param p reference to current position pointer |
| 131 | * \param start start of the buffer (for bounds-checking) |
| 132 | * \param text the text to write |
| 133 | * |
| 134 | * \return the length written or a negative error code |
| 135 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 136 | int asn1_write_printable_string( unsigned char **p, unsigned char *start, |
| 137 | char *text ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 138 | |
| 139 | /** |
| 140 | * \brief Write an IA5 string tag (ASN1_IA5_STRING) and |
| 141 | * value in ASN.1 format |
| 142 | * Note: function works backwards in data buffer |
| 143 | * |
| 144 | * \param p reference to current position pointer |
| 145 | * \param start start of the buffer (for bounds-checking) |
| 146 | * \param text the text to write |
| 147 | * |
| 148 | * \return the length written or a negative error code |
| 149 | */ |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 150 | int asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
| 151 | char *text ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 152 | |
| 153 | /** |
| 154 | * \brief Write a bitstring tag (ASN1_BIT_STRING) and |
| 155 | * value in ASN.1 format |
| 156 | * Note: function works backwards in data buffer |
| 157 | * |
| 158 | * \param p reference to current position pointer |
| 159 | * \param start start of the buffer (for bounds-checking) |
| 160 | * \param buf the bitstring |
| 161 | * \param bits the total number of bits in the bitstring |
| 162 | * |
| 163 | * \return the length written or a negative error code |
| 164 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 165 | int asn1_write_bitstring( unsigned char **p, unsigned char *start, |
| 166 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 167 | |
| 168 | /** |
| 169 | * \brief Write an octet string tag (ASN1_OCTET_STRING) and |
| 170 | * value in ASN.1 format |
| 171 | * Note: function works backwards in data buffer |
| 172 | * |
| 173 | * \param p reference to current position pointer |
| 174 | * \param start start of the buffer (for bounds-checking) |
| 175 | * \param buf data buffer to write |
| 176 | * \param size length of the data buffer |
| 177 | * |
| 178 | * \return the length written or a negative error code |
| 179 | */ |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 180 | int asn1_write_octet_string( unsigned char **p, unsigned char *start, |
| 181 | const unsigned char *buf, size_t size ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame^] | 182 | |
| 183 | /** |
| 184 | * \brief Write raw buffer data |
| 185 | * Note: function works backwards in data buffer |
| 186 | * |
| 187 | * \param p reference to current position pointer |
| 188 | * \param start start of the buffer (for bounds-checking) |
| 189 | * \param buf data buffer to write |
| 190 | * \param size length of the data buffer |
| 191 | * |
| 192 | * \return the length written or a negative error code |
| 193 | */ |
Paul Bakker | 6db915b | 2013-08-26 12:05:02 +0200 | [diff] [blame] | 194 | int asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
| 195 | const unsigned char *buf, size_t size ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 196 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 197 | #ifdef __cplusplus |
| 198 | } |
| 199 | #endif |
| 200 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 201 | #endif /* POLARSSL_ASN1_WRITE_H */ |