blob: 43408e29d1514e881c2205757877a3294cd74ab9 [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 Bakkered27a042013-04-18 22:46:23 +020062#if defined(POLARSSL_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020063/**
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 Bakkerbdb912d2012-02-13 23:11:30 +000073int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X );
Paul Bakkered27a042013-04-18 22:46:23 +020074#endif
Paul Bakker7accbce2013-08-26 17:34:53 +020075
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 Bakkerbdb912d2012-02-13 23:11:30 +000085int asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +020086
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 Bakker37de6be2013-04-07 13:11:31 +020097int asn1_write_oid( unsigned char **p, unsigned char *start, const char *oid );
Paul Bakker7accbce2013-08-26 17:34:53 +020098
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 */
110int 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 Bakkerbdb912d2012-02-13 23:11:30 +0000123int asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200124
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 Bakkerbdb912d2012-02-13 23:11:30 +0000136int asn1_write_printable_string( unsigned char **p, unsigned char *start,
137 char *text );
Paul Bakker7accbce2013-08-26 17:34:53 +0200138
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 Bakker05888152012-02-16 10:26:57 +0000150int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
151 char *text );
Paul Bakker7accbce2013-08-26 17:34:53 +0200152
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 Bakker598e4502013-08-25 14:46:39 +0200165int asn1_write_bitstring( unsigned char **p, unsigned char *start,
166 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200167
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 Bakker598e4502013-08-25 14:46:39 +0200180int asn1_write_octet_string( unsigned char **p, unsigned char *start,
181 const unsigned char *buf, size_t size );
Paul Bakker7accbce2013-08-26 17:34:53 +0200182
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 Bakker6db915b2013-08-26 12:05:02 +0200194int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
195 const unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196
Paul Bakker407a0da2013-06-27 14:29:21 +0200197#ifdef __cplusplus
198}
199#endif
200
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000201#endif /* POLARSSL_ASN1_WRITE_H */