4 * \brief ASN.1 buffer writing functionality
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * This file is part of mbed TLS (https://tls.mbed.org)
23 #ifndef MBEDTLS_ASN1_WRITE_H
24 #define MBEDTLS_ASN1_WRITE_H
28 #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
29 g += ret; } while( 0 )
36 * \brief Write a length field in ASN.1 format
37 * Note: function works backwards in data buffer
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
43 * \return the length written or a negative error code
45 int mbedtls_asn1_write_len( unsigned char **p
, unsigned char *start
, size_t len
);
48 * \brief Write a ASN.1 tag in ASN.1 format
49 * Note: function works backwards in data buffer
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
55 * \return the length written or a negative error code
57 int mbedtls_asn1_write_tag( unsigned char **p
, unsigned char *start
,
61 * \brief Write raw buffer data
62 * Note: function works backwards in data buffer
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
69 * \return the length written or a negative error code
71 int mbedtls_asn1_write_raw_buffer( unsigned char **p
, unsigned char *start
,
72 const unsigned char *buf
, size_t size
);
74 #if defined(MBEDTLS_BIGNUM_C)
76 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
77 * Note: function works backwards in data buffer
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
83 * \return the length written or a negative error code
85 int mbedtls_asn1_write_mpi( unsigned char **p
, unsigned char *start
, const mbedtls_mpi
*X
);
86 #endif /* MBEDTLS_BIGNUM_C */
89 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
90 * Note: function works backwards in data buffer
92 * \param p reference to current position pointer
93 * \param start start of the buffer (for bounds-checking)
95 * \return the length written or a negative error code
97 int mbedtls_asn1_write_null( unsigned char **p
, unsigned char *start
);
100 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
101 * Note: function works backwards in data buffer
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
108 * \return the length written or a negative error code
110 int mbedtls_asn1_write_oid( unsigned char **p
, unsigned char *start
,
111 const char *oid
, size_t oid_len
);
114 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
115 * Note: function works backwards in data buffer
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
124 * \return the length written or a negative error code
126 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p
, unsigned char *start
,
127 const char *oid
, size_t oid_len
,
131 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
132 * Note: function works backwards in data buffer
134 * \param p reference to current position pointer
135 * \param start start of the buffer (for bounds-checking)
136 * \param boolean 0 or 1
138 * \return the length written or a negative error code
140 int mbedtls_asn1_write_bool( unsigned char **p
, unsigned char *start
, int boolean
);
143 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
144 * Note: function works backwards in data buffer
146 * \param p reference to current position pointer
147 * \param start start of the buffer (for bounds-checking)
148 * \param val the integer value
150 * \return the length written or a negative error code
152 int mbedtls_asn1_write_int( unsigned char **p
, unsigned char *start
, int val
);
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
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
164 * \return the length written or a negative error code
166 int mbedtls_asn1_write_printable_string( unsigned char **p
, unsigned char *start
,
167 const char *text
, size_t text_len
);
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
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
179 * \return the length written or a negative error code
181 int mbedtls_asn1_write_ia5_string( unsigned char **p
, unsigned char *start
,
182 const char *text
, size_t text_len
);
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
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
194 * \return the length written or a negative error code
196 int mbedtls_asn1_write_bitstring( unsigned char **p
, unsigned char *start
,
197 const unsigned char *buf
, size_t bits
);
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
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
209 * \return the length written or a negative error code
211 int mbedtls_asn1_write_octet_string( unsigned char **p
, unsigned char *start
,
212 const unsigned char *buf
, size_t size
);
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!
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
227 * \return NULL if if there was a memory allocation error, or a pointer
228 * to the new / existing entry.
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
,
239 #endif /* MBEDTLS_ASN1_WRITE_H */