blob: 7a7fbf7b3f24122b69381c15617e5141dd2cf977 [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 Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, 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 Bakkerb9e4e2c2014-05-01 14:18:25 +020032#define ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
33 g += ret; } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000034
Paul Bakker407a0da2013-06-27 14:29:21 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
Paul Bakker7accbce2013-08-26 17:34:53 +020039/**
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 Bakkerbdb912d2012-02-13 23:11:30 +000049int asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020050
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 Bakkerb9e4e2c2014-05-01 14:18:25 +020061int asn1_write_tag( unsigned char **p, unsigned char *start,
62 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020063
Paul Bakker9852d002013-08-26 17:56:37 +020064/**
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 */
75int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
76 const unsigned char *buf, size_t size );
77
Paul Bakkered27a042013-04-18 22:46:23 +020078#if defined(POLARSSL_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020079/**
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 Bakkerbdb912d2012-02-13 23:11:30 +000089int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X );
Paul Bakker9af723c2014-05-01 13:03:14 +020090#endif /* POLARSSL_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +020091
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 Bakkerbdb912d2012-02-13 23:11:30 +0000101int asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200102
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 Bakker5f45e622013-09-09 12:02:36 +0200110 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200111 *
112 * \return the length written or a negative error code
113 */
Paul Bakker5f45e622013-09-09 12:02:36 +0200114int asn1_write_oid( unsigned char **p, unsigned char *start,
115 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200116
117/**
118 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
119 * Note: function works backwards in data buffer
Paul Bakker7accbce2013-08-26 17:34:53 +0200120 *
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 Bakker5f45e622013-09-09 12:02:36 +0200124 * \param oid_len length of the OID
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200125 * \param par_len length of parameters, which must be already written.
126 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200127 *
128 * \return the length written or a negative error code
129 */
130int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200131 const char *oid, size_t oid_len,
132 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200133
134/**
Paul Bakker329def32013-09-06 16:34:38 +0200135 * \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 */
144int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
145
146/**
Paul Bakker7accbce2013-08-26 17:34:53 +0200147 * \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 Bakkerbdb912d2012-02-13 23:11:30 +0000156int asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200157
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 Bakker5f45e622013-09-09 12:02:36 +0200166 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200167 *
168 * \return the length written or a negative error code
169 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200171 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200172
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 Bakker5f45e622013-09-09 12:02:36 +0200181 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200182 *
183 * \return the length written or a negative error code
184 */
Paul Bakker05888152012-02-16 10:26:57 +0000185int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200186 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200187
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 Bakker598e4502013-08-25 14:46:39 +0200200int asn1_write_bitstring( unsigned char **p, unsigned char *start,
201 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200202
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 Bakker598e4502013-08-25 14:46:39 +0200215int asn1_write_octet_string( unsigned char **p, unsigned char *start,
216 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200217
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 */
234asn1_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 Bakker407a0da2013-06-27 14:29:21 +0200239#ifdef __cplusplus
240}
241#endif
242
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243#endif /* POLARSSL_ASN1_WRITE_H */