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