Mbed OS Reference
Loading...
Searching...
No Matches
asn1write.h
Go to the documentation of this file.
1/**
2 * \file asn1write.h
3 *
4 * \brief ASN.1 buffer writing functionality
5 */
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22#ifndef MBEDTLS_ASN1_WRITE_H
23#define MBEDTLS_ASN1_WRITE_H
24
25#if !defined(MBEDTLS_CONFIG_FILE)
26#include "mbedtls/config.h"
27#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
31#include "mbedtls/asn1.h"
32
33/**
34 * \addtogroup mbedtls
35 * \{
36 * \defgroup mbedtls_asn1write_module ASN.1 Write
37 * \{
38 */
39
40#define MBEDTLS_ASN1_CHK_ADD(g, f) \
41 do \
42 { \
43 if( ( ret = (f) ) < 0 ) \
44 return( ret ); \
45 else \
46 (g) += ret; \
47 } while( 0 )
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * \brief Write a length field in ASN.1 format.
55 *
56 * \note This function works backwards in data buffer.
57 *
58 * \param p The reference to the current position pointer.
59 * \param start The start of the buffer, for bounds-checking.
60 * \param len The length value to write.
61 *
62 * \return The number of bytes written to \p p on success.
63 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
64 */
65int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
66 size_t len );
67/**
68 * \brief Write an ASN.1 tag in ASN.1 format.
69 *
70 * \note This function works backwards in data buffer.
71 *
72 * \param p The reference to the current position pointer.
73 * \param start The start of the buffer, for bounds-checking.
74 * \param tag The tag to write.
75 *
76 * \return The number of bytes written to \p p on success.
77 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
78 */
79int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
80 unsigned char tag );
81
82/**
83 * \brief Write raw buffer data.
84 *
85 * \note This function works backwards in data buffer.
86 *
87 * \param p The reference to the current position pointer.
88 * \param start The start of the buffer, for bounds-checking.
89 * \param buf The data buffer to write.
90 * \param size The length of the data buffer.
91 *
92 * \return The number of bytes written to \p p on success.
93 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
94 */
95int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
96 const unsigned char *buf, size_t size );
97
98#if defined(MBEDTLS_BIGNUM_C)
99/**
100 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
101 * in ASN.1 format.
102 *
103 * \note This function works backwards in data buffer.
104 *
105 * \param p The reference to the current position pointer.
106 * \param start The start of the buffer, for bounds-checking.
107 * \param X The MPI to write.
108 * It must be non-negative.
109 *
110 * \return The number of bytes written to \p p on success.
111 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
112 */
113int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
114 const mbedtls_mpi *X );
115#endif /* MBEDTLS_BIGNUM_C */
116
117/**
118 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
119 * in ASN.1 format.
120 *
121 * \note This function works backwards in data buffer.
122 *
123 * \param p The reference to the current position pointer.
124 * \param start The start of the buffer, for bounds-checking.
125 *
126 * \return The number of bytes written to \p p on success.
127 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
128 */
129int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
130
131/**
132 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
133 * in ASN.1 format.
134 *
135 * \note This function works backwards in data buffer.
136 *
137 * \param p The reference to the current position pointer.
138 * \param start The start of the buffer, for bounds-checking.
139 * \param oid The OID to write.
140 * \param oid_len The length of the OID.
141 *
142 * \return The number of bytes written to \p p on success.
143 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
144 */
145int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
146 const char *oid, size_t oid_len );
147
148/**
149 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
150 *
151 * \note This function works backwards in data buffer.
152 *
153 * \param p The reference to the current position pointer.
154 * \param start The start of the buffer, for bounds-checking.
155 * \param oid The OID of the algorithm to write.
156 * \param oid_len The length of the algorithm's OID.
157 * \param par_len The length of the parameters, which must be already written.
158 * If 0, NULL parameters are added
159 *
160 * \return The number of bytes written to \p p on success.
161 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
162 */
164 unsigned char *start,
165 const char *oid, size_t oid_len,
166 size_t par_len );
167
168/**
169 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
170 * in ASN.1 format.
171 *
172 * \note This function works backwards in data buffer.
173 *
174 * \param p The reference to the current position pointer.
175 * \param start The start of the buffer, for bounds-checking.
176 * \param boolean The boolean value to write, either \c 0 or \c 1.
177 *
178 * \return The number of bytes written to \p p on success.
179 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
180 */
181int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
182 int boolean );
183
184/**
185 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
186 * in ASN.1 format.
187 *
188 * \note This function works backwards in data buffer.
189 *
190 * \param p The reference to the current position pointer.
191 * \param start The start of the buffer, for bounds-checking.
192 * \param val The integer value to write.
193 * It must be non-negative.
194 *
195 * \return The number of bytes written to \p p on success.
196 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
197 */
198int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
199
200/**
201 * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
202 * in ASN.1 format.
203 *
204 * \note This function works backwards in data buffer.
205 *
206 * \param p The reference to the current position pointer.
207 * \param start The start of the buffer, for bounds-checking.
208 * \param val The integer value to write.
209 *
210 * \return The number of bytes written to \p p on success.
211 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
212 */
213int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val );
214
215/**
216 * \brief Write a string in ASN.1 format using a specific
217 * string encoding tag.
218
219 * \note This function works backwards in data buffer.
220 *
221 * \param p The reference to the current position pointer.
222 * \param start The start of the buffer, for bounds-checking.
223 * \param tag The string encoding tag to write, e.g.
224 * #MBEDTLS_ASN1_UTF8_STRING.
225 * \param text The string to write.
226 * \param text_len The length of \p text in bytes (which might
227 * be strictly larger than the number of characters).
228 *
229 * \return The number of bytes written to \p p on success.
230 * \return A negative error code on failure.
231 */
232int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
233 int tag, const char *text,
234 size_t text_len );
235
236/**
237 * \brief Write a string in ASN.1 format using the PrintableString
238 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
239 *
240 * \note This function works backwards in data buffer.
241 *
242 * \param p The reference to the current position pointer.
243 * \param start The start of the buffer, for bounds-checking.
244 * \param text The string to write.
245 * \param text_len The length of \p text in bytes (which might
246 * be strictly larger than the number of characters).
247 *
248 * \return The number of bytes written to \p p on success.
249 * \return A negative error code on failure.
250 */
252 unsigned char *start,
253 const char *text, size_t text_len );
254
255/**
256 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
257 * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING).
258 *
259 * \note This function works backwards in data buffer.
260 *
261 * \param p The reference to the current position pointer.
262 * \param start The start of the buffer, for bounds-checking.
263 * \param text The string to write.
264 * \param text_len The length of \p text in bytes (which might
265 * be strictly larger than the number of characters).
266 *
267 * \return The number of bytes written to \p p on success.
268 * \return A negative error code on failure.
269 */
270int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
271 const char *text, size_t text_len );
272
273/**
274 * \brief Write a string in ASN.1 format using the IA5String
275 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
276 *
277 * \note This function works backwards in data buffer.
278 *
279 * \param p The reference to the current position pointer.
280 * \param start The start of the buffer, for bounds-checking.
281 * \param text The string to write.
282 * \param text_len The length of \p text in bytes (which might
283 * be strictly larger than the number of characters).
284 *
285 * \return The number of bytes written to \p p on success.
286 * \return A negative error code on failure.
287 */
288int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
289 const char *text, size_t text_len );
290
291/**
292 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
293 * value in ASN.1 format.
294 *
295 * \note This function works backwards in data buffer.
296 *
297 * \param p The reference to the current position pointer.
298 * \param start The start of the buffer, for bounds-checking.
299 * \param buf The bitstring to write.
300 * \param bits The total number of bits in the bitstring.
301 *
302 * \return The number of bytes written to \p p on success.
303 * \return A negative error code on failure.
304 */
305int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
306 const unsigned char *buf, size_t bits );
307
308/**
309 * \brief This function writes a named bitstring tag
310 * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
311 *
312 * As stated in RFC 5280 Appendix B, trailing zeroes are
313 * omitted when encoding named bitstrings in DER.
314 *
315 * \note This function works backwards within the data buffer.
316 *
317 * \param p The reference to the current position pointer.
318 * \param start The start of the buffer which is used for bounds-checking.
319 * \param buf The bitstring to write.
320 * \param bits The total number of bits in the bitstring.
321 *
322 * \return The number of bytes written to \p p on success.
323 * \return A negative error code on failure.
324 */
326 unsigned char *start,
327 const unsigned char *buf,
328 size_t bits );
329
330/**
331 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
332 * and value in ASN.1 format.
333 *
334 * \note This function works backwards in data buffer.
335 *
336 * \param p The reference to the current position pointer.
337 * \param start The start of the buffer, for bounds-checking.
338 * \param buf The buffer holding the data to write.
339 * \param size The length of the data buffer \p buf.
340 *
341 * \return The number of bytes written to \p p on success.
342 * \return A negative error code on failure.
343 */
344int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
345 const unsigned char *buf, size_t size );
346
347/**
348 * \brief Create or find a specific named_data entry for writing in a
349 * sequence or list based on the OID. If not already in there,
350 * a new entry is added to the head of the list.
351 * Warning: Destructive behaviour for the val data!
352 *
353 * \param list The pointer to the location of the head of the list to seek
354 * through (will be updated in case of a new entry).
355 * \param oid The OID to look for.
356 * \param oid_len The size of the OID.
357 * \param val The associated data to store. If this is \c NULL,
358 * no data is copied to the new or existing buffer.
359 * \param val_len The minimum length of the data buffer needed.
360 * If this is 0, do not allocate a buffer for the associated
361 * data.
362 * If the OID was already present, enlarge, shrink or free
363 * the existing buffer to fit \p val_len.
364 *
365 * \return A pointer to the new / existing entry on success.
366 * \return \c NULL if if there was a memory allocation error.
367 */
369 const char *oid, size_t oid_len,
370 const unsigned char *val,
371 size_t val_len );
372
373/// \}
374/// \}
375
376#ifdef __cplusplus
377}
378#endif
379
380#endif /* MBEDTLS_ASN1_WRITE_H */
Generic ASN.1 parsing.
Configuration options (set of defines)
int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write raw buffer data.
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write an ASN.1 tag in ASN.1 format.
int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits)
Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a string in ASN.1 format using the PrintableString string encoding tag (MBEDTLS_ASN1_PRINTABLE_...
int mbedtls_asn1_write_utf8_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a UTF8 string in ASN.1 format using the UTF8String string encoding tag (MBEDTLS_ASN1_UTF8_STRIN...
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and value in ASN.1 format.
mbedtls_asn1_named_data * mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list, const char *oid, size_t oid_len, const unsigned char *val, size_t val_len)
Create or find a specific named_data entry for writing in a sequence or list based on the OID.
int mbedtls_asn1_write_named_bitstring(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits)
This function writes a named bitstring tag (MBEDTLS_ASN1_BIT_STRING) and value in ASN....
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format.
int mbedtls_asn1_write_enum(unsigned char **p, unsigned char *start, int val)
Write an enum tag (MBEDTLS_ASN1_ENUMERATED) and value in ASN.1 format.
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format.
int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len)
Write an AlgorithmIdentifier sequence in ASN.1 format.
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len)
Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format.
int mbedtls_asn1_write_tagged_string(unsigned char **p, unsigned char *start, int tag, const char *text, size_t text_len)
Write a string in ASN.1 format using a specific string encoding tag.
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format.
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format.
int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a string in ASN.1 format using the IA5String string encoding tag (MBEDTLS_ASN1_IA5_STRING).
Container for a sequence or list of 'named' ASN.1 data items.
Definition: asn1.h:178
MPI structure.
Definition: bignum.h:185