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: 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 | * This file is part of mbed TLS (https://tls.mbed.org)
|
---|
23 | */
|
---|
24 | #ifndef MBEDTLS_ASN1_WRITE_H
|
---|
25 | #define MBEDTLS_ASN1_WRITE_H
|
---|
26 |
|
---|
27 | #if !defined(MBEDTLS_CONFIG_FILE)
|
---|
28 | #include "config.h"
|
---|
29 | #else
|
---|
30 | #include MBEDTLS_CONFIG_FILE
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "asn1.h"
|
---|
34 |
|
---|
35 | #define MBEDTLS_ASN1_CHK_ADD(g, f) \
|
---|
36 | do { \
|
---|
37 | if( ( ret = f ) < 0 ) \
|
---|
38 | return( ret ); \
|
---|
39 | else \
|
---|
40 | g += ret; \
|
---|
41 | } while( 0 )
|
---|
42 |
|
---|
43 | #ifdef __cplusplus
|
---|
44 | extern "C" {
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * \brief Write a length field in ASN.1 format.
|
---|
49 | *
|
---|
50 | * \note This function works backwards in data buffer.
|
---|
51 | *
|
---|
52 | * \param p The reference to the current position pointer.
|
---|
53 | * \param start The start of the buffer, for bounds-checking.
|
---|
54 | * \param len The length value to write.
|
---|
55 | *
|
---|
56 | * \return The number of bytes written to \p p on success.
|
---|
57 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
58 | */
|
---|
59 | int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
|
---|
60 | size_t len );
|
---|
61 | /**
|
---|
62 | * \brief Write an ASN.1 tag in ASN.1 format.
|
---|
63 | *
|
---|
64 | * \note This function works backwards in data buffer.
|
---|
65 | *
|
---|
66 | * \param p The reference to the current position pointer.
|
---|
67 | * \param start The start of the buffer, for bounds-checking.
|
---|
68 | * \param tag The tag to write.
|
---|
69 | *
|
---|
70 | * \return The number of bytes written to \p p on success.
|
---|
71 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
72 | */
|
---|
73 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
|
---|
74 | unsigned char tag );
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * \brief Write raw buffer data.
|
---|
78 | *
|
---|
79 | * \note This function works backwards in data buffer.
|
---|
80 | *
|
---|
81 | * \param p The reference to the current position pointer.
|
---|
82 | * \param start The start of the buffer, for bounds-checking.
|
---|
83 | * \param buf The data buffer to write.
|
---|
84 | * \param size The length of the data buffer.
|
---|
85 | *
|
---|
86 | * \return The number of bytes written to \p p on success.
|
---|
87 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
88 | */
|
---|
89 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
|
---|
90 | const unsigned char *buf, size_t size );
|
---|
91 |
|
---|
92 | #if defined(MBEDTLS_BIGNUM_C)
|
---|
93 | /**
|
---|
94 | * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
|
---|
95 | * in ASN.1 format.
|
---|
96 | *
|
---|
97 | * \note This function works backwards in data buffer.
|
---|
98 | *
|
---|
99 | * \param p The reference to the current position pointer.
|
---|
100 | * \param start The start of the buffer, for bounds-checking.
|
---|
101 | * \param X The MPI to write.
|
---|
102 | *
|
---|
103 | * \return The number of bytes written to \p p on success.
|
---|
104 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
105 | */
|
---|
106 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
|
---|
107 | const mbedtls_mpi *X );
|
---|
108 | #endif /* MBEDTLS_BIGNUM_C */
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
|
---|
112 | * in ASN.1 format.
|
---|
113 | *
|
---|
114 | * \note This function works backwards in data buffer.
|
---|
115 | *
|
---|
116 | * \param p The reference to the current position pointer.
|
---|
117 | * \param start The start of the buffer, for bounds-checking.
|
---|
118 | *
|
---|
119 | * \return The number of bytes written to \p p on success.
|
---|
120 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
121 | */
|
---|
122 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
|
---|
126 | * in ASN.1 format.
|
---|
127 | *
|
---|
128 | * \note This function works backwards in data buffer.
|
---|
129 | *
|
---|
130 | * \param p The reference to the current position pointer.
|
---|
131 | * \param start The start of the buffer, for bounds-checking.
|
---|
132 | * \param oid The OID to write.
|
---|
133 | * \param oid_len The length of the OID.
|
---|
134 | *
|
---|
135 | * \return The number of bytes written to \p p on success.
|
---|
136 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
137 | */
|
---|
138 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
|
---|
139 | const char *oid, size_t oid_len );
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
|
---|
143 | *
|
---|
144 | * \note This function works backwards in data buffer.
|
---|
145 | *
|
---|
146 | * \param p The reference to the current position pointer.
|
---|
147 | * \param start The start of the buffer, for bounds-checking.
|
---|
148 | * \param oid The OID of the algorithm to write.
|
---|
149 | * \param oid_len The length of the algorithm's OID.
|
---|
150 | * \param par_len The length of the parameters, which must be already written.
|
---|
151 | * If 0, NULL parameters are added
|
---|
152 | *
|
---|
153 | * \return The number of bytes written to \p p on success.
|
---|
154 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
155 | */
|
---|
156 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p,
|
---|
157 | unsigned char *start,
|
---|
158 | const char *oid, size_t oid_len,
|
---|
159 | size_t par_len );
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
|
---|
163 | * in ASN.1 format.
|
---|
164 | *
|
---|
165 | * \note This function works backwards in data buffer.
|
---|
166 | *
|
---|
167 | * \param p The reference to the current position pointer.
|
---|
168 | * \param start The start of the buffer, for bounds-checking.
|
---|
169 | * \param boolean The boolean value to write, either \c 0 or \c 1.
|
---|
170 | *
|
---|
171 | * \return The number of bytes written to \p p on success.
|
---|
172 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
173 | */
|
---|
174 | int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
|
---|
175 | int boolean );
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
|
---|
179 | * in ASN.1 format.
|
---|
180 | *
|
---|
181 | * \note This function works backwards in data buffer.
|
---|
182 | *
|
---|
183 | * \param p The reference to the current position pointer.
|
---|
184 | * \param start The start of the buffer, for bounds-checking.
|
---|
185 | * \param val The integer value to write.
|
---|
186 | *
|
---|
187 | * \return The number of bytes written to \p p on success.
|
---|
188 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
|
---|
189 | */
|
---|
190 | int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * \brief Write a string in ASN.1 format using a specific
|
---|
194 | * string encoding tag.
|
---|
195 |
|
---|
196 | * \note This function works backwards in data buffer.
|
---|
197 | *
|
---|
198 | * \param p The reference to the current position pointer.
|
---|
199 | * \param start The start of the buffer, for bounds-checking.
|
---|
200 | * \param tag The string encoding tag to write, e.g.
|
---|
201 | * #MBEDTLS_ASN1_UTF8_STRING.
|
---|
202 | * \param text The string to write.
|
---|
203 | * \param text_len The length of \p text in bytes (which might
|
---|
204 | * be strictly larger than the number of characters).
|
---|
205 | *
|
---|
206 | * \return The number of bytes written to \p p on success.
|
---|
207 | * \return A negative error code on failure.
|
---|
208 | */
|
---|
209 | int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
|
---|
210 | int tag, const char *text,
|
---|
211 | size_t text_len );
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * \brief Write a string in ASN.1 format using the PrintableString
|
---|
215 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
|
---|
216 | *
|
---|
217 | * \note This function works backwards in data buffer.
|
---|
218 | *
|
---|
219 | * \param p The reference to the current position pointer.
|
---|
220 | * \param start The start of the buffer, for bounds-checking.
|
---|
221 | * \param text The string to write.
|
---|
222 | * \param text_len The length of \p text in bytes (which might
|
---|
223 | * be strictly larger than the number of characters).
|
---|
224 | *
|
---|
225 | * \return The number of bytes written to \p p on success.
|
---|
226 | * \return A negative error code on failure.
|
---|
227 | */
|
---|
228 | int mbedtls_asn1_write_printable_string( unsigned char **p,
|
---|
229 | unsigned char *start,
|
---|
230 | const char *text, size_t text_len );
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * \brief Write a UTF8 string in ASN.1 format using the UTF8String
|
---|
234 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
|
---|
235 | *
|
---|
236 | * \note This function works backwards in data buffer.
|
---|
237 | *
|
---|
238 | * \param p The reference to the current position pointer.
|
---|
239 | * \param start The start of the buffer, for bounds-checking.
|
---|
240 | * \param text The string to write.
|
---|
241 | * \param text_len The length of \p text in bytes (which might
|
---|
242 | * be strictly larger than the number of characters).
|
---|
243 | *
|
---|
244 | * \return The number of bytes written to \p p on success.
|
---|
245 | * \return A negative error code on failure.
|
---|
246 | */
|
---|
247 | int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
|
---|
248 | const char *text, size_t text_len );
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * \brief Write a string in ASN.1 format using the IA5String
|
---|
252 | * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
|
---|
253 | *
|
---|
254 | * \note This function works backwards in data buffer.
|
---|
255 | *
|
---|
256 | * \param p The reference to the current position pointer.
|
---|
257 | * \param start The start of the buffer, for bounds-checking.
|
---|
258 | * \param text The string to write.
|
---|
259 | * \param text_len The length of \p text in bytes (which might
|
---|
260 | * be strictly larger than the number of characters).
|
---|
261 | *
|
---|
262 | * \return The number of bytes written to \p p on success.
|
---|
263 | * \return A negative error code on failure.
|
---|
264 | */
|
---|
265 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
|
---|
266 | const char *text, size_t text_len );
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
|
---|
270 | * value in ASN.1 format.
|
---|
271 | *
|
---|
272 | * \note This function works backwards in data buffer.
|
---|
273 | *
|
---|
274 | * \param p The reference to the current position pointer.
|
---|
275 | * \param start The start of the buffer, for bounds-checking.
|
---|
276 | * \param buf The bitstring to write.
|
---|
277 | * \param bits The total number of bits in the bitstring.
|
---|
278 | *
|
---|
279 | * \return The number of bytes written to \p p on success.
|
---|
280 | * \return A negative error code on failure.
|
---|
281 | */
|
---|
282 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
|
---|
283 | const unsigned char *buf, size_t bits );
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
|
---|
287 | * and value in ASN.1 format.
|
---|
288 | *
|
---|
289 | * \note This function works backwards in data buffer.
|
---|
290 | *
|
---|
291 | * \param p The reference to the current position pointer.
|
---|
292 | * \param start The start of the buffer, for bounds-checking.
|
---|
293 | * \param buf The buffer holding the data to write.
|
---|
294 | * \param size The length of the data buffer \p buf.
|
---|
295 | *
|
---|
296 | * \return The number of bytes written to \p p on success.
|
---|
297 | * \return A negative error code on failure.
|
---|
298 | */
|
---|
299 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
|
---|
300 | const unsigned char *buf, size_t size );
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * \brief Create or find a specific named_data entry for writing in a
|
---|
304 | * sequence or list based on the OID. If not already in there,
|
---|
305 | * a new entry is added to the head of the list.
|
---|
306 | * Warning: Destructive behaviour for the val data!
|
---|
307 | *
|
---|
308 | * \param list The pointer to the location of the head of the list to seek
|
---|
309 | * through (will be updated in case of a new entry).
|
---|
310 | * \param oid The OID to look for.
|
---|
311 | * \param oid_len The size of the OID.
|
---|
312 | * \param val The data to store (can be \c NULL if you want to fill
|
---|
313 | * it by hand).
|
---|
314 | * \param val_len The minimum length of the data buffer needed.
|
---|
315 | *
|
---|
316 | * \return A pointer to the new / existing entry on success.
|
---|
317 | * \return \c NULL if if there was a memory allocation error.
|
---|
318 | */
|
---|
319 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
|
---|
320 | const char *oid, size_t oid_len,
|
---|
321 | const unsigned char *val,
|
---|
322 | size_t val_len );
|
---|
323 |
|
---|
324 | #ifdef __cplusplus
|
---|
325 | }
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | #endif /* MBEDTLS_ASN1_WRITE_H */
|
---|