blob: 76c1780b595a918ce75105db669ed0f60a67432c [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/**
2 * \file asn1write.h
3 *
4 * \brief ASN.1 buffer writing functionality
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * 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.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_ASN1_WRITE_H
25#define MBEDTLS_ASN1_WRITE_H
Paul Bakkerbdb912d2012-02-13 23:11:30 +000026
27#include "asn1.h"
28
Hanno Becker55177552018-10-24 12:29:53 +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 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000036
Paul Bakker407a0da2013-06-27 14:29:21 +020037#ifdef __cplusplus
38extern "C" {
39#endif
40
Paul Bakker7accbce2013-08-26 17:34:53 +020041/**
Hanno Becker55177552018-10-24 12:29:53 +010042 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020043 *
Hanno Becker55177552018-10-24 12:29:53 +010044 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020045 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +020052 */
Hanno Becker55177552018-10-24 12:29:53 +010053int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
54 size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020055/**
Hanno Becker55177552018-10-24 12:29:53 +010056 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020057 *
Hanno Becker55177552018-10-24 12:29:53 +010058 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020059 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +010068 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020069
Paul Bakker9852d002013-08-26 17:56:37 +020070/**
Hanno Becker55177552018-10-24 12:29:53 +010071 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020072 *
Hanno Becker55177552018-10-24 12:29:53 +010073 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020074 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker9852d002013-08-26 17:56:37 +020082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +010084 const unsigned char *buf, size_t size );
Paul Bakker9852d002013-08-26 17:56:37 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020087/**
Hanno Becker55177552018-10-24 12:29:53 +010088 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
89 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020090 *
Hanno Becker55177552018-10-24 12:29:53 +010091 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020092 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +020099 */
Hanno Becker55177552018-10-24 12:29:53 +0100100int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
101 const mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200103
104/**
Hanno Becker55177552018-10-24 12:29:53 +0100105 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
106 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200107 *
Hanno Becker55177552018-10-24 12:29:53 +0100108 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200109 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +0200115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200117
118/**
Hanno Becker55177552018-10-24 12:29:53 +0100119 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
120 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200121 *
Hanno Becker55177552018-10-24 12:29:53 +0100122 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200123 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +0200131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100133 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200134
135/**
Hanno Becker55177552018-10-24 12:29:53 +0100136 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200137 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200145 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200146 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +0200149 */
Hanno Becker55177552018-10-24 12:29:53 +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 );
Paul Bakker7accbce2013-08-26 17:34:53 +0200154
155/**
Hanno Becker55177552018-10-24 12:29:53 +0100156 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
157 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200158 *
Hanno Becker55177552018-10-24 12:29:53 +0100159 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200160 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker329def32013-09-06 16:34:38 +0200167 */
Hanno Becker55177552018-10-24 12:29:53 +0100168int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
169 int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200170
171/**
Hanno Becker55177552018-10-24 12:29:53 +0100172 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
173 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200174 *
Hanno Becker55177552018-10-24 12:29:53 +0100175 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200176 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +0200183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200185
186/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100187 * \brief Write a string in ASN.1 format using a specific
188 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100189
190 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100191 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100192 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100193 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100194 * \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).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100199 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100200 * \return The number of bytes written to \p p on success.
201 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100202 */
thomas-deeeba6c9b2018-09-19 09:10:37 +0200203int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100204 int tag, const char *text,
205 size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200206
207/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100208 * \brief Write a string in ASN.1 format using the PrintableString
209 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100210 *
211 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100212 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100213 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100214 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100215 * \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).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100218 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100219 * \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).
Hanno Becker55177552018-10-24 12:29:53 +0100229 *
230 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100231 *
232 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100233 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100234 * \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.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100240 */
241int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100242 const char *text, size_t text_len );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100243
244/**
Hanno Becker55177552018-10-24 12:29:53 +0100245 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100246 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100247 *
248 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200249 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100250 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100251 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100252 * \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).
Paul Bakker7accbce2013-08-26 17:34:53 +0200255 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100256 * \return The number of bytes written to \p p on success.
257 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100260 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200261
262/**
Hanno Becker55177552018-10-24 12:29:53 +0100263 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
264 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200265 *
Hanno Becker55177552018-10-24 12:29:53 +0100266 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200267 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +0200275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100277 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200278
279/**
Hanno Becker55177552018-10-24 12:29:53 +0100280 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
281 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200282 *
Hanno Becker55177552018-10-24 12:29:53 +0100283 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200284 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker7accbce2013-08-26 17:34:53 +0200292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100294 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +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 *
Hanno Becker55177552018-10-24 12:29:53 +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.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200309 *
Hanno Becker55177552018-10-24 12:29:53 +0100310 * \return A pointer to the new / existing entry on success.
311 * \return \c NULL if if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200314 const char *oid, size_t oid_len,
315 const unsigned char *val,
316 size_t val_len );
317
Paul Bakker407a0da2013-06-27 14:29:21 +0200318#ifdef __cplusplus
319}
320#endif
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* MBEDTLS_ASN1_WRITE_H */