blob: 61165020417860d042d872c768c25430921f281d [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/**
2 * \file asn1write.h
3 *
4 * \brief ASN.1 buffer writing functionality
5 *
Paul Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
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 Bakkerbdb912d2012-02-13 23:11:30 +000032#define ASN1_CHK_ADD(g, f) if( ( ret = f ) < 0 ) return( ret ); else g += ret
33
Paul Bakker407a0da2013-06-27 14:29:21 +020034#ifdef __cplusplus
35extern "C" {
36#endif
37
Paul Bakker7accbce2013-08-26 17:34:53 +020038/**
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 Bakkerbdb912d2012-02-13 23:11:30 +000048int asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020049
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 Bakkerbdb912d2012-02-13 23:11:30 +000060int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020061
Paul Bakker9852d002013-08-26 17:56:37 +020062/**
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 */
73int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
74 const unsigned char *buf, size_t size );
75
Paul Bakkered27a042013-04-18 22:46:23 +020076#if defined(POLARSSL_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020077/**
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 Bakkerbdb912d2012-02-13 23:11:30 +000087int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X );
Paul Bakkered27a042013-04-18 22:46:23 +020088#endif
Paul Bakker7accbce2013-08-26 17:34:53 +020089
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 Bakkerbdb912d2012-02-13 23:11:30 +000099int asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200100
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
Paul Bakker5f45e622013-09-09 12:02:36 +0200108 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200109 *
110 * \return the length written or a negative error code
111 */
Paul Bakker5f45e622013-09-09 12:02:36 +0200112int asn1_write_oid( unsigned char **p, unsigned char *start,
113 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200114
115/**
116 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
117 * Note: function works backwards in data buffer
118 * Note: Uses NULL as algorithm parameter
119 *
120 * \param p reference to current position pointer
121 * \param start start of the buffer (for bounds-checking)
122 * \param oid the OID of the algorithm
Paul Bakker5f45e622013-09-09 12:02:36 +0200123 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200124 *
125 * \return the length written or a negative error code
126 */
127int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200128 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200129
130/**
Paul Bakker329def32013-09-06 16:34:38 +0200131 * \brief Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format
132 * Note: function works backwards in data buffer
133 *
134 * \param p reference to current position pointer
135 * \param start start of the buffer (for bounds-checking)
136 * \param boolean 0 or 1
137 *
138 * \return the length written or a negative error code
139 */
140int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
141
142/**
Paul Bakker7accbce2013-08-26 17:34:53 +0200143 * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format
144 * Note: function works backwards in data buffer
145 *
146 * \param p reference to current position pointer
147 * \param start start of the buffer (for bounds-checking)
148 * \param val the integer value
149 *
150 * \return the length written or a negative error code
151 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152int asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200153
154/**
155 * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and
156 * value in ASN.1 format
157 * Note: function works backwards in data buffer
158 *
159 * \param p reference to current position pointer
160 * \param start start of the buffer (for bounds-checking)
161 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200162 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200163 *
164 * \return the length written or a negative error code
165 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200167 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200168
169/**
170 * \brief Write an IA5 string tag (ASN1_IA5_STRING) and
171 * value in ASN.1 format
172 * Note: function works backwards in data buffer
173 *
174 * \param p reference to current position pointer
175 * \param start start of the buffer (for bounds-checking)
176 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200177 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200178 *
179 * \return the length written or a negative error code
180 */
Paul Bakker05888152012-02-16 10:26:57 +0000181int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200182 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200183
184/**
185 * \brief Write a bitstring tag (ASN1_BIT_STRING) and
186 * value in ASN.1 format
187 * Note: function works backwards in data buffer
188 *
189 * \param p reference to current position pointer
190 * \param start start of the buffer (for bounds-checking)
191 * \param buf the bitstring
192 * \param bits the total number of bits in the bitstring
193 *
194 * \return the length written or a negative error code
195 */
Paul Bakker598e4502013-08-25 14:46:39 +0200196int asn1_write_bitstring( unsigned char **p, unsigned char *start,
197 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200198
199/**
200 * \brief Write an octet string tag (ASN1_OCTET_STRING) and
201 * value in ASN.1 format
202 * Note: function works backwards in data buffer
203 *
204 * \param p reference to current position pointer
205 * \param start start of the buffer (for bounds-checking)
206 * \param buf data buffer to write
207 * \param size length of the data buffer
208 *
209 * \return the length written or a negative error code
210 */
Paul Bakker598e4502013-08-25 14:46:39 +0200211int asn1_write_octet_string( unsigned char **p, unsigned char *start,
212 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200213
214/**
215 * \brief Create or find a specific named_data entry for writing in a
216 * sequence or list based on the OID. If not already in there,
217 * a new entry is added to the head of the list.
218 * Warning: Destructive behaviour for the val data!
219 *
220 * \param list Pointer to the location of the head of the list to seek
221 * through (will be updated in case of a new entry)
222 * \param oid The OID to look for
223 * \param oid_len Size of the OID
224 * \param val Data to store (can be NULL if you want to fill it by hand)
225 * \param val_len Minimum length of the data buffer needed
226 *
227 * \return NULL if if there was a memory allocation error, or a pointer
228 * to the new / existing entry.
229 */
230asn1_named_data *asn1_store_named_data( asn1_named_data **list,
231 const char *oid, size_t oid_len,
232 const unsigned char *val,
233 size_t val_len );
234
Paul Bakker407a0da2013-06-27 14:29:21 +0200235#ifdef __cplusplus
236}
237#endif
238
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000239#endif /* POLARSSL_ASN1_WRITE_H */