4 * \brief ASN.1 buffer writing functionality
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: GPL-2.0
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * This file is part of mbed TLS (https://tls.mbed.org)
26 #ifndef MBEDTLS_ASN1_WRITE_H
27 #define MBEDTLS_ASN1_WRITE_H
29 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include MBEDTLS_CONFIG_FILE
37 #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
38 g += ret; } while( 0 )
45 * \brief Write a length field in ASN.1 format
46 * Note: function works backwards in data buffer
48 * \param p reference to current position pointer
49 * \param start start of the buffer (for bounds-checking)
50 * \param len the length to write
52 * \return the length written or a negative error code
54 int mbedtls_asn1_write_len( unsigned char **p
, unsigned char *start
, size_t len
);
57 * \brief Write a ASN.1 tag in ASN.1 format
58 * Note: function works backwards in data buffer
60 * \param p reference to current position pointer
61 * \param start start of the buffer (for bounds-checking)
62 * \param tag the tag to write
64 * \return the length written or a negative error code
66 int mbedtls_asn1_write_tag( unsigned char **p
, unsigned char *start
,
70 * \brief Write raw buffer data
71 * Note: function works backwards in data buffer
73 * \param p reference to current position pointer
74 * \param start start of the buffer (for bounds-checking)
75 * \param buf data buffer to write
76 * \param size length of the data buffer
78 * \return the length written or a negative error code
80 int mbedtls_asn1_write_raw_buffer( unsigned char **p
, unsigned char *start
,
81 const unsigned char *buf
, size_t size
);
83 #if defined(MBEDTLS_BIGNUM_C)
85 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
86 * Note: function works backwards in data buffer
88 * \param p reference to current position pointer
89 * \param start start of the buffer (for bounds-checking)
90 * \param X the MPI to write
92 * \return the length written or a negative error code
94 int mbedtls_asn1_write_mpi( unsigned char **p
, unsigned char *start
, const mbedtls_mpi
*X
);
95 #endif /* MBEDTLS_BIGNUM_C */
98 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
99 * Note: function works backwards in data buffer
101 * \param p reference to current position pointer
102 * \param start start of the buffer (for bounds-checking)
104 * \return the length written or a negative error code
106 int mbedtls_asn1_write_null( unsigned char **p
, unsigned char *start
);
109 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
110 * Note: function works backwards in data buffer
112 * \param p reference to current position pointer
113 * \param start start of the buffer (for bounds-checking)
114 * \param oid the OID to write
115 * \param oid_len length of the OID
117 * \return the length written or a negative error code
119 int mbedtls_asn1_write_oid( unsigned char **p
, unsigned char *start
,
120 const char *oid
, size_t oid_len
);
123 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
124 * Note: function works backwards in data buffer
126 * \param p reference to current position pointer
127 * \param start start of the buffer (for bounds-checking)
128 * \param oid the OID of the algorithm
129 * \param oid_len length of the OID
130 * \param par_len length of parameters, which must be already written.
131 * If 0, NULL parameters are added
133 * \return the length written or a negative error code
135 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p
, unsigned char *start
,
136 const char *oid
, size_t oid_len
,
140 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
141 * Note: function works backwards in data buffer
143 * \param p reference to current position pointer
144 * \param start start of the buffer (for bounds-checking)
145 * \param boolean 0 or 1
147 * \return the length written or a negative error code
149 int mbedtls_asn1_write_bool( unsigned char **p
, unsigned char *start
, int boolean
);
152 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
153 * Note: function works backwards in data buffer
155 * \param p reference to current position pointer
156 * \param start start of the buffer (for bounds-checking)
157 * \param val the integer value
159 * \return the length written or a negative error code
161 int mbedtls_asn1_write_int( unsigned char **p
, unsigned char *start
, int val
);
164 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
165 * value in ASN.1 format
166 * Note: function works backwards in data buffer
168 * \param p reference to current position pointer
169 * \param start start of the buffer (for bounds-checking)
170 * \param text the text to write
171 * \param text_len length of the text
173 * \return the length written or a negative error code
175 int mbedtls_asn1_write_printable_string( unsigned char **p
, unsigned char *start
,
176 const char *text
, size_t text_len
);
179 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
180 * value in ASN.1 format
181 * Note: function works backwards in data buffer
183 * \param p reference to current position pointer
184 * \param start start of the buffer (for bounds-checking)
185 * \param text the text to write
186 * \param text_len length of the text
188 * \return the length written or a negative error code
190 int mbedtls_asn1_write_ia5_string( unsigned char **p
, unsigned char *start
,
191 const char *text
, size_t text_len
);
194 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
195 * value in ASN.1 format.
197 * \note This function works backwards in data buffer.
199 * \param p The reference to the current position pointer.
200 * \param start The start of the buffer, for bounds-checking.
201 * \param buf The bitstring to write.
202 * \param bits The total number of bits in the bitstring.
204 * \return The number of bytes written to \p p on success.
205 * \return A negative error code on failure.
207 int mbedtls_asn1_write_bitstring( unsigned char **p
, unsigned char *start
,
208 const unsigned char *buf
, size_t bits
);
211 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
212 * and value in ASN.1 format.
214 * \note This function works backwards in data buffer.
216 * \param p reference to current position pointer
217 * \param start start of the buffer (for bounds-checking)
218 * \param buf data buffer to write
219 * \param size length of the data buffer
221 * \return the length written or a negative error code
223 int mbedtls_asn1_write_octet_string( unsigned char **p
, unsigned char *start
,
224 const unsigned char *buf
, size_t size
);
227 * \brief Create or find a specific named_data entry for writing in a
228 * sequence or list based on the OID. If not already in there,
229 * a new entry is added to the head of the list.
230 * Warning: Destructive behaviour for the val data!
232 * \param list Pointer to the location of the head of the list to seek
233 * through (will be updated in case of a new entry)
234 * \param oid The OID to look for
235 * \param oid_len Size of the OID
236 * \param val Data to store (can be NULL if you want to fill it by hand)
237 * \param val_len Minimum length of the data buffer needed
239 * \return NULL if if there was a memory allocation error, or a pointer
240 * to the new / existing entry.
242 mbedtls_asn1_named_data
*mbedtls_asn1_store_named_data( mbedtls_asn1_named_data
**list
,
243 const char *oid
, size_t oid_len
,
244 const unsigned char *val
,
251 #endif /* MBEDTLS_ASN1_WRITE_H */