mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
include/polarssl/asn1write.h@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file asn1write.h |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief ASN.1 buffer writing functionality |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 11 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 12 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 13 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 14 | * |
ansond | 0:137634ff4186 | 15 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 18 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 19 | * |
ansond | 0:137634ff4186 | 20 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 23 | */ |
ansond | 0:137634ff4186 | 24 | #ifndef POLARSSL_ASN1_WRITE_H |
ansond | 0:137634ff4186 | 25 | #define POLARSSL_ASN1_WRITE_H |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #include "asn1.h" |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #define ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \ |
ansond | 0:137634ff4186 | 30 | g += ret; } while( 0 ) |
ansond | 0:137634ff4186 | 31 | |
ansond | 0:137634ff4186 | 32 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 33 | extern "C" { |
ansond | 0:137634ff4186 | 34 | #endif |
ansond | 0:137634ff4186 | 35 | |
ansond | 0:137634ff4186 | 36 | /** |
ansond | 0:137634ff4186 | 37 | * \brief Write a length field in ASN.1 format |
ansond | 0:137634ff4186 | 38 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 39 | * |
ansond | 0:137634ff4186 | 40 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 41 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 42 | * \param len the length to write |
ansond | 0:137634ff4186 | 43 | * |
ansond | 0:137634ff4186 | 44 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 45 | */ |
ansond | 0:137634ff4186 | 46 | int asn1_write_len( unsigned char **p, unsigned char *start, size_t len ); |
ansond | 0:137634ff4186 | 47 | |
ansond | 0:137634ff4186 | 48 | /** |
ansond | 0:137634ff4186 | 49 | * \brief Write a ASN.1 tag in ASN.1 format |
ansond | 0:137634ff4186 | 50 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 51 | * |
ansond | 0:137634ff4186 | 52 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 53 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 54 | * \param tag the tag to write |
ansond | 0:137634ff4186 | 55 | * |
ansond | 0:137634ff4186 | 56 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 57 | */ |
ansond | 0:137634ff4186 | 58 | int asn1_write_tag( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 59 | unsigned char tag ); |
ansond | 0:137634ff4186 | 60 | |
ansond | 0:137634ff4186 | 61 | /** |
ansond | 0:137634ff4186 | 62 | * \brief Write raw buffer data |
ansond | 0:137634ff4186 | 63 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 64 | * |
ansond | 0:137634ff4186 | 65 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 66 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 67 | * \param buf data buffer to write |
ansond | 0:137634ff4186 | 68 | * \param size length of the data buffer |
ansond | 0:137634ff4186 | 69 | * |
ansond | 0:137634ff4186 | 70 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 71 | */ |
ansond | 0:137634ff4186 | 72 | int asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 73 | const unsigned char *buf, size_t size ); |
ansond | 0:137634ff4186 | 74 | |
ansond | 0:137634ff4186 | 75 | #if defined(POLARSSL_BIGNUM_C) |
ansond | 0:137634ff4186 | 76 | /** |
ansond | 0:137634ff4186 | 77 | * \brief Write a big number (ASN1_INTEGER) in ASN.1 format |
ansond | 0:137634ff4186 | 78 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 79 | * |
ansond | 0:137634ff4186 | 80 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 81 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 82 | * \param X the MPI to write |
ansond | 0:137634ff4186 | 83 | * |
ansond | 0:137634ff4186 | 84 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 85 | */ |
ansond | 0:137634ff4186 | 86 | int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ); |
ansond | 0:137634ff4186 | 87 | #endif /* POLARSSL_BIGNUM_C */ |
ansond | 0:137634ff4186 | 88 | |
ansond | 0:137634ff4186 | 89 | /** |
ansond | 0:137634ff4186 | 90 | * \brief Write a NULL tag (ASN1_NULL) with zero data in ASN.1 format |
ansond | 0:137634ff4186 | 91 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 92 | * |
ansond | 0:137634ff4186 | 93 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 94 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 95 | * |
ansond | 0:137634ff4186 | 96 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 97 | */ |
ansond | 0:137634ff4186 | 98 | int asn1_write_null( unsigned char **p, unsigned char *start ); |
ansond | 0:137634ff4186 | 99 | |
ansond | 0:137634ff4186 | 100 | /** |
ansond | 0:137634ff4186 | 101 | * \brief Write an OID tag (ASN1_OID) and data in ASN.1 format |
ansond | 0:137634ff4186 | 102 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 103 | * |
ansond | 0:137634ff4186 | 104 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 105 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 106 | * \param oid the OID to write |
ansond | 0:137634ff4186 | 107 | * \param oid_len length of the OID |
ansond | 0:137634ff4186 | 108 | * |
ansond | 0:137634ff4186 | 109 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 110 | */ |
ansond | 0:137634ff4186 | 111 | int asn1_write_oid( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 112 | const char *oid, size_t oid_len ); |
ansond | 0:137634ff4186 | 113 | |
ansond | 0:137634ff4186 | 114 | /** |
ansond | 0:137634ff4186 | 115 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format |
ansond | 0:137634ff4186 | 116 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 117 | * |
ansond | 0:137634ff4186 | 118 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 119 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 120 | * \param oid the OID of the algorithm |
ansond | 0:137634ff4186 | 121 | * \param oid_len length of the OID |
ansond | 0:137634ff4186 | 122 | * \param par_len length of parameters, which must be already written. |
ansond | 0:137634ff4186 | 123 | * If 0, NULL parameters are added |
ansond | 0:137634ff4186 | 124 | * |
ansond | 0:137634ff4186 | 125 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 126 | */ |
ansond | 0:137634ff4186 | 127 | int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 128 | const char *oid, size_t oid_len, |
ansond | 0:137634ff4186 | 129 | size_t par_len ); |
ansond | 0:137634ff4186 | 130 | |
ansond | 0:137634ff4186 | 131 | /** |
ansond | 0:137634ff4186 | 132 | * \brief Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format |
ansond | 0:137634ff4186 | 133 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 134 | * |
ansond | 0:137634ff4186 | 135 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 136 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 137 | * \param boolean 0 or 1 |
ansond | 0:137634ff4186 | 138 | * |
ansond | 0:137634ff4186 | 139 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 140 | */ |
ansond | 0:137634ff4186 | 141 | int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ); |
ansond | 0:137634ff4186 | 142 | |
ansond | 0:137634ff4186 | 143 | /** |
ansond | 0:137634ff4186 | 144 | * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format |
ansond | 0:137634ff4186 | 145 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 146 | * |
ansond | 0:137634ff4186 | 147 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 148 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 149 | * \param val the integer value |
ansond | 0:137634ff4186 | 150 | * |
ansond | 0:137634ff4186 | 151 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 152 | */ |
ansond | 0:137634ff4186 | 153 | int asn1_write_int( unsigned char **p, unsigned char *start, int val ); |
ansond | 0:137634ff4186 | 154 | |
ansond | 0:137634ff4186 | 155 | /** |
ansond | 0:137634ff4186 | 156 | * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and |
ansond | 0:137634ff4186 | 157 | * value in ASN.1 format |
ansond | 0:137634ff4186 | 158 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 159 | * |
ansond | 0:137634ff4186 | 160 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 161 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 162 | * \param text the text to write |
ansond | 0:137634ff4186 | 163 | * \param text_len length of the text |
ansond | 0:137634ff4186 | 164 | * |
ansond | 0:137634ff4186 | 165 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 166 | */ |
ansond | 0:137634ff4186 | 167 | int asn1_write_printable_string( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 168 | const char *text, size_t text_len ); |
ansond | 0:137634ff4186 | 169 | |
ansond | 0:137634ff4186 | 170 | /** |
ansond | 0:137634ff4186 | 171 | * \brief Write an IA5 string tag (ASN1_IA5_STRING) and |
ansond | 0:137634ff4186 | 172 | * value in ASN.1 format |
ansond | 0:137634ff4186 | 173 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 174 | * |
ansond | 0:137634ff4186 | 175 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 176 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 177 | * \param text the text to write |
ansond | 0:137634ff4186 | 178 | * \param text_len length of the text |
ansond | 0:137634ff4186 | 179 | * |
ansond | 0:137634ff4186 | 180 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 181 | */ |
ansond | 0:137634ff4186 | 182 | int asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 183 | const char *text, size_t text_len ); |
ansond | 0:137634ff4186 | 184 | |
ansond | 0:137634ff4186 | 185 | /** |
ansond | 0:137634ff4186 | 186 | * \brief Write a bitstring tag (ASN1_BIT_STRING) and |
ansond | 0:137634ff4186 | 187 | * value in ASN.1 format |
ansond | 0:137634ff4186 | 188 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 189 | * |
ansond | 0:137634ff4186 | 190 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 191 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 192 | * \param buf the bitstring |
ansond | 0:137634ff4186 | 193 | * \param bits the total number of bits in the bitstring |
ansond | 0:137634ff4186 | 194 | * |
ansond | 0:137634ff4186 | 195 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 196 | */ |
ansond | 0:137634ff4186 | 197 | int asn1_write_bitstring( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 198 | const unsigned char *buf, size_t bits ); |
ansond | 0:137634ff4186 | 199 | |
ansond | 0:137634ff4186 | 200 | /** |
ansond | 0:137634ff4186 | 201 | * \brief Write an octet string tag (ASN1_OCTET_STRING) and |
ansond | 0:137634ff4186 | 202 | * value in ASN.1 format |
ansond | 0:137634ff4186 | 203 | * Note: function works backwards in data buffer |
ansond | 0:137634ff4186 | 204 | * |
ansond | 0:137634ff4186 | 205 | * \param p reference to current position pointer |
ansond | 0:137634ff4186 | 206 | * \param start start of the buffer (for bounds-checking) |
ansond | 0:137634ff4186 | 207 | * \param buf data buffer to write |
ansond | 0:137634ff4186 | 208 | * \param size length of the data buffer |
ansond | 0:137634ff4186 | 209 | * |
ansond | 0:137634ff4186 | 210 | * \return the length written or a negative error code |
ansond | 0:137634ff4186 | 211 | */ |
ansond | 0:137634ff4186 | 212 | int asn1_write_octet_string( unsigned char **p, unsigned char *start, |
ansond | 0:137634ff4186 | 213 | const unsigned char *buf, size_t size ); |
ansond | 0:137634ff4186 | 214 | |
ansond | 0:137634ff4186 | 215 | /** |
ansond | 0:137634ff4186 | 216 | * \brief Create or find a specific named_data entry for writing in a |
ansond | 0:137634ff4186 | 217 | * sequence or list based on the OID. If not already in there, |
ansond | 0:137634ff4186 | 218 | * a new entry is added to the head of the list. |
ansond | 0:137634ff4186 | 219 | * Warning: Destructive behaviour for the val data! |
ansond | 0:137634ff4186 | 220 | * |
ansond | 0:137634ff4186 | 221 | * \param list Pointer to the location of the head of the list to seek |
ansond | 0:137634ff4186 | 222 | * through (will be updated in case of a new entry) |
ansond | 0:137634ff4186 | 223 | * \param oid The OID to look for |
ansond | 0:137634ff4186 | 224 | * \param oid_len Size of the OID |
ansond | 0:137634ff4186 | 225 | * \param val Data to store (can be NULL if you want to fill it by hand) |
ansond | 0:137634ff4186 | 226 | * \param val_len Minimum length of the data buffer needed |
ansond | 0:137634ff4186 | 227 | * |
ansond | 0:137634ff4186 | 228 | * \return NULL if if there was a memory allocation error, or a pointer |
ansond | 0:137634ff4186 | 229 | * to the new / existing entry. |
ansond | 0:137634ff4186 | 230 | */ |
ansond | 0:137634ff4186 | 231 | asn1_named_data *asn1_store_named_data( asn1_named_data **list, |
ansond | 0:137634ff4186 | 232 | const char *oid, size_t oid_len, |
ansond | 0:137634ff4186 | 233 | const unsigned char *val, |
ansond | 0:137634ff4186 | 234 | size_t val_len ); |
ansond | 0:137634ff4186 | 235 | |
ansond | 0:137634ff4186 | 236 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 237 | } |
ansond | 0:137634ff4186 | 238 | #endif |
ansond | 0:137634ff4186 | 239 | |
ansond | 0:137634ff4186 | 240 | #endif /* POLARSSL_ASN1_WRITE_H */ |
ansond | 0:137634ff4186 | 241 |