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