blob: 28522d10d01a7b7afdc245a8f50ba5c1923bcf7c [file] [log] [blame]
Edison Ai7b079202018-02-28 15:01:47 +08001/* SPDX-License-Identifier: Apache-2.0 */
Jens Wiklander817466c2018-05-22 13:49:31 +02002/**
3 * \file asn1write.h
4 *
5 * \brief ASN.1 buffer writing functionality
Jens Wiklander50a57cf2019-03-13 10:41:54 +01006 */
7/*
Jens Wiklander817466c2018-05-22 13:49:31 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Jens Wiklander817466c2018-05-22 13:49:31 +02009 *
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
Jens Wiklander50a57cf2019-03-13 10:41:54 +010029#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 )
Jens Wiklander817466c2018-05-22 13:49:31 +020036
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +010042 * \brief Write a length field in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +020043 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010044 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +020045 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010046 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +020052 */
Jens Wiklander50a57cf2019-03-13 10:41:54 +010053int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
54 size_t len );
Jens Wiklander817466c2018-05-22 13:49:31 +020055/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +010056 * \brief Write an ASN.1 tag in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +020057 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010058 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +020059 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010060 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +020066 */
67int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Jens Wiklander50a57cf2019-03-13 10:41:54 +010068 unsigned char tag );
Jens Wiklander817466c2018-05-22 13:49:31 +020069
70/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +010071 * \brief Write raw buffer data.
Jens Wiklander817466c2018-05-22 13:49:31 +020072 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010073 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +020074 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010075 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +020082 */
83int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Jens Wiklander50a57cf2019-03-13 10:41:54 +010084 const unsigned char *buf, size_t size );
Jens Wiklander817466c2018-05-22 13:49:31 +020085
86#if defined(MBEDTLS_BIGNUM_C)
87/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +010088 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
89 * in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +020090 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010091 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +020092 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +010093 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +020099 */
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100100int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
101 const mbedtls_mpi *X );
Jens Wiklander817466c2018-05-22 13:49:31 +0200102#endif /* MBEDTLS_BIGNUM_C */
103
104/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100105 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
106 * in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200107 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100108 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200109 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100110 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200115 */
116int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
117
118/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100119 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
120 * in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200121 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100122 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200123 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100124 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200131 */
132int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100133 const char *oid, size_t oid_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200134
135/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100136 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200137 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100138 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200145 * If 0, NULL parameters are added
146 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100147 * \return The number of bytes written to \p p on success.
148 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Jens Wiklander817466c2018-05-22 13:49:31 +0200149 */
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100150int 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 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200154
155/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100156 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
157 * in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200158 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100159 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200160 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100161 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200167 */
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100168int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
169 int boolean );
Jens Wiklander817466c2018-05-22 13:49:31 +0200170
171/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100172 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
173 * in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200174 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100175 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200176 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100177 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200183 */
184int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
185
186/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100187 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200191 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100192 * \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).
Jens Wiklander817466c2018-05-22 13:49:31 +0200199 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100200 * \return The number of bytes written to \p p on success.
201 * \return A negative error code on failure.
Jens Wiklander817466c2018-05-22 13:49:31 +0200202 */
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100203int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
204 int tag, const char *text,
205 size_t text_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200206
207/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100208 * \brief Write a string in ASN.1 format using the PrintableString
209 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Jens Wiklander817466c2018-05-22 13:49:31 +0200210 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100211 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200212 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100213 * \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 */
222int 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 */
241int 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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200258 */
259int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100260 const char *text, size_t text_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200261
262/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100263 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
264 * value in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200265 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100266 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200267 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100268 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200275 */
276int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100277 const unsigned char *buf, size_t bits );
Jens Wiklander817466c2018-05-22 13:49:31 +0200278
279/**
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100280 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
281 * and value in ASN.1 format.
Jens Wiklander817466c2018-05-22 13:49:31 +0200282 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100283 * \note This function works backwards in data buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +0200284 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100285 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200292 */
293int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100294 const unsigned char *buf, size_t size );
Jens Wiklander817466c2018-05-22 13:49:31 +0200295
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 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100302 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200309 *
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100310 * \return A pointer to the new / existing entry on success.
311 * \return \c NULL if if there was a memory allocation error.
Jens Wiklander817466c2018-05-22 13:49:31 +0200312 */
313mbedtls_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 */