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: Apache-2.0
8  *
9  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
10  *  not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *  http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_ASN1_WRITE_H
24 #define MBEDTLS_ASN1_WRITE_H
25 
26 #include "asn1.h"
27 
28 #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else   \
29                                 g += ret; } while( 0 )
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * \brief           Write a length field in ASN.1 format
37  *                  Note: function works backwards in data buffer
38  *
39  * \param p         reference to current position pointer
40  * \param start     start of the buffer (for bounds-checking)
41  * \param len       the length to write
42  *
43  * \return          the length written or a negative error code
44  */
45 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
46 
47 /**
48  * \brief           Write a ASN.1 tag in ASN.1 format
49  *                  Note: function works backwards in data buffer
50  *
51  * \param p         reference to current position pointer
52  * \param start     start of the buffer (for bounds-checking)
53  * \param tag       the tag to write
54  *
55  * \return          the length written or a negative error code
56  */
57 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
58                     unsigned char tag );
59 
60 /**
61  * \brief           Write raw buffer data
62  *                  Note: function works backwards in data buffer
63  *
64  * \param p         reference to current position pointer
65  * \param start     start of the buffer (for bounds-checking)
66  * \param buf       data buffer to write
67  * \param size      length of the data buffer
68  *
69  * \return          the length written or a negative error code
70  */
71 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
72                            const unsigned char *buf, size_t size );
73 
74 #if defined(MBEDTLS_BIGNUM_C)
75 /**
76  * \brief           Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
77  *                  Note: function works backwards in data buffer
78  *
79  * \param p         reference to current position pointer
80  * \param start     start of the buffer (for bounds-checking)
81  * \param X         the MPI to write
82  *
83  * \return          the length written or a negative error code
84  */
85 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
86 #endif /* MBEDTLS_BIGNUM_C */
87 
88 /**
89  * \brief           Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
90  *                  Note: function works backwards in data buffer
91  *
92  * \param p         reference to current position pointer
93  * \param start     start of the buffer (for bounds-checking)
94  *
95  * \return          the length written or a negative error code
96  */
97 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
98 
99 /**
100  * \brief           Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
101  *                  Note: function works backwards in data buffer
102  *
103  * \param p         reference to current position pointer
104  * \param start     start of the buffer (for bounds-checking)
105  * \param oid       the OID to write
106  * \param oid_len   length of the OID
107  *
108  * \return          the length written or a negative error code
109  */
110 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
111                     const char *oid, size_t oid_len );
112 
113 /**
114  * \brief           Write an AlgorithmIdentifier sequence in ASN.1 format
115  *                  Note: function works backwards in data buffer
116  *
117  * \param p         reference to current position pointer
118  * \param start     start of the buffer (for bounds-checking)
119  * \param oid       the OID of the algorithm
120  * \param oid_len   length of the OID
121  * \param par_len   length of parameters, which must be already written.
122  *                  If 0, NULL parameters are added
123  *
124  * \return          the length written or a negative error code
125  */
126 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
127                                      const char *oid, size_t oid_len,
128                                      size_t par_len );
129 
130 /**
131  * \brief           Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
132  *                  Note: function works backwards in data buffer
133  *
134  * \param p         reference to current position pointer
135  * \param start     start of the buffer (for bounds-checking)
136  * \param boolean   0 or 1
137  *
138  * \return          the length written or a negative error code
139  */
140 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
141 
142 /**
143  * \brief           Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
144  *                  Note: function works backwards in data buffer
145  *
146  * \param p         reference to current position pointer
147  * \param start     start of the buffer (for bounds-checking)
148  * \param val       the integer value
149  *
150  * \return          the length written or a negative error code
151  */
152 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
153 
154 /**
155  * \brief           Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
156  *                  value in ASN.1 format
157  *                  Note: function works backwards in data buffer
158  *
159  * \param p         reference to current position pointer
160  * \param start     start of the buffer (for bounds-checking)
161  * \param text      the text to write
162  * \param text_len  length of the text
163  *
164  * \return          the length written or a negative error code
165  */
166 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
167                                  const char *text, size_t text_len );
168 
169 /**
170  * \brief           Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
171  *                  value in ASN.1 format
172  *                  Note: function works backwards in data buffer
173  *
174  * \param p         reference to current position pointer
175  * \param start     start of the buffer (for bounds-checking)
176  * \param text      the text to write
177  * \param text_len  length of the text
178  *
179  * \return          the length written or a negative error code
180  */
181 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
182                            const char *text, size_t text_len );
183 
184 /**
185  * \brief           Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
186  *                  value in ASN.1 format
187  *                  Note: function works backwards in data buffer
188  *
189  * \param p         reference to current position pointer
190  * \param start     start of the buffer (for bounds-checking)
191  * \param buf       the bitstring
192  * \param bits      the total number of bits in the bitstring
193  *
194  * \return          the length written or a negative error code
195  */
196 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
197                           const unsigned char *buf, size_t bits );
198 
199 /**
200  * \brief           Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and
201  *                  value in ASN.1 format
202  *                  Note: function works backwards in data buffer
203  *
204  * \param p         reference to current position pointer
205  * \param start     start of the buffer (for bounds-checking)
206  * \param buf       data buffer to write
207  * \param size      length of the data buffer
208  *
209  * \return          the length written or a negative error code
210  */
211 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
212                              const unsigned char *buf, size_t size );
213 
214 /**
215  * \brief           Create or find a specific named_data entry for writing in a
216  *                  sequence or list based on the OID. If not already in there,
217  *                  a new entry is added to the head of the list.
218  *                  Warning: Destructive behaviour for the val data!
219  *
220  * \param list      Pointer to the location of the head of the list to seek
221  *                  through (will be updated in case of a new entry)
222  * \param oid       The OID to look for
223  * \param oid_len   Size of the OID
224  * \param val       Data to store (can be NULL if you want to fill it by hand)
225  * \param val_len   Minimum length of the data buffer needed
226  *
227  * \return      NULL if if there was a memory allocation error, or a pointer
228  *              to the new / existing entry.
229  */
230 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
231                                         const char *oid, size_t oid_len,
232                                         const unsigned char *val,
233                                         size_t val_len );
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif /* MBEDTLS_ASN1_WRITE_H */
240