[MBEDTLS]
[reactos.git] / reactos / sdk / include / reactos / libs / mbedtls / asn1write.h
1 /**
2 * \file asn1write.h
3 *
4 * \brief ASN.1 buffer writing functionality
5 *
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: GPL-2.0
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25 #ifndef MBEDTLS_ASN1_WRITE_H
26 #define MBEDTLS_ASN1_WRITE_H
27
28 #include "asn1.h"
29
30 #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
31 g += ret; } while( 0 )
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /**
38 * \brief Write a length field in ASN.1 format
39 * Note: function works backwards in data buffer
40 *
41 * \param p reference to current position pointer
42 * \param start start of the buffer (for bounds-checking)
43 * \param len the length to write
44 *
45 * \return the length written or a negative error code
46 */
47 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
48
49 /**
50 * \brief Write a ASN.1 tag in ASN.1 format
51 * Note: function works backwards in data buffer
52 *
53 * \param p reference to current position pointer
54 * \param start start of the buffer (for bounds-checking)
55 * \param tag the tag to write
56 *
57 * \return the length written or a negative error code
58 */
59 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
60 unsigned char tag );
61
62 /**
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 */
73 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
74 const unsigned char *buf, size_t size );
75
76 #if defined(MBEDTLS_BIGNUM_C)
77 /**
78 * \brief Write a big number (MBEDTLS_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 */
87 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
88 #endif /* MBEDTLS_BIGNUM_C */
89
90 /**
91 * \brief Write a NULL tag (MBEDTLS_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 */
99 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
100
101 /**
102 * \brief Write an OID tag (MBEDTLS_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
108 * \param oid_len length of the OID
109 *
110 * \return the length written or a negative error code
111 */
112 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
113 const char *oid, size_t oid_len );
114
115 /**
116 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
117 * Note: function works backwards in data buffer
118 *
119 * \param p reference to current position pointer
120 * \param start start of the buffer (for bounds-checking)
121 * \param oid the OID of the algorithm
122 * \param oid_len length of the OID
123 * \param par_len length of parameters, which must be already written.
124 * If 0, NULL parameters are added
125 *
126 * \return the length written or a negative error code
127 */
128 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
129 const char *oid, size_t oid_len,
130 size_t par_len );
131
132 /**
133 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
134 * Note: function works backwards in data buffer
135 *
136 * \param p reference to current position pointer
137 * \param start start of the buffer (for bounds-checking)
138 * \param boolean 0 or 1
139 *
140 * \return the length written or a negative error code
141 */
142 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
143
144 /**
145 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
146 * Note: function works backwards in data buffer
147 *
148 * \param p reference to current position pointer
149 * \param start start of the buffer (for bounds-checking)
150 * \param val the integer value
151 *
152 * \return the length written or a negative error code
153 */
154 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
155
156 /**
157 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
158 * value in ASN.1 format
159 * Note: function works backwards in data buffer
160 *
161 * \param p reference to current position pointer
162 * \param start start of the buffer (for bounds-checking)
163 * \param text the text to write
164 * \param text_len length of the text
165 *
166 * \return the length written or a negative error code
167 */
168 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
169 const char *text, size_t text_len );
170
171 /**
172 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
173 * value in ASN.1 format
174 * Note: function works backwards in data buffer
175 *
176 * \param p reference to current position pointer
177 * \param start start of the buffer (for bounds-checking)
178 * \param text the text to write
179 * \param text_len length of the text
180 *
181 * \return the length written or a negative error code
182 */
183 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
184 const char *text, size_t text_len );
185
186 /**
187 * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
188 * value in ASN.1 format
189 * Note: function works backwards in data buffer
190 *
191 * \param p reference to current position pointer
192 * \param start start of the buffer (for bounds-checking)
193 * \param buf the bitstring
194 * \param bits the total number of bits in the bitstring
195 *
196 * \return the length written or a negative error code
197 */
198 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
199 const unsigned char *buf, size_t bits );
200
201 /**
202 * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and
203 * value in ASN.1 format
204 * Note: function works backwards in data buffer
205 *
206 * \param p reference to current position pointer
207 * \param start start of the buffer (for bounds-checking)
208 * \param buf data buffer to write
209 * \param size length of the data buffer
210 *
211 * \return the length written or a negative error code
212 */
213 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
214 const unsigned char *buf, size_t size );
215
216 /**
217 * \brief Create or find a specific named_data entry for writing in a
218 * sequence or list based on the OID. If not already in there,
219 * a new entry is added to the head of the list.
220 * Warning: Destructive behaviour for the val data!
221 *
222 * \param list Pointer to the location of the head of the list to seek
223 * through (will be updated in case of a new entry)
224 * \param oid The OID to look for
225 * \param oid_len Size of the OID
226 * \param val Data to store (can be NULL if you want to fill it by hand)
227 * \param val_len Minimum length of the data buffer needed
228 *
229 * \return NULL if if there was a memory allocation error, or a pointer
230 * to the new / existing entry.
231 */
232 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
233 const char *oid, size_t oid_len,
234 const unsigned char *val,
235 size_t val_len );
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241 #endif /* MBEDTLS_ASN1_WRITE_H */