blob: a194243696ac8254a09d131f0b24f16fb2d748db [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
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020027#if !defined(MBEDTLS_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
Paul Bakkerbdb912d2012-02-13 23:11:30 +000033#include "asn1.h"
34
Hanno Becker55177552018-10-24 12:29:53 +010035#define MBEDTLS_ASN1_CHK_ADD(g, f) \
Hanno Becker1eeca412018-10-15 12:01:35 +010036 do \
37 { \
38 if( ( ret = (f) ) < 0 ) \
Hanno Becker55177552018-10-24 12:29:53 +010039 return( ret ); \
40 else \
Hanno Becker1eeca412018-10-15 12:01:35 +010041 (g) += ret; \
Hanno Becker55177552018-10-24 12:29:53 +010042 } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker7accbce2013-08-26 17:34:53 +020048/**
Hanno Becker55177552018-10-24 12:29:53 +010049 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020050 *
Hanno Becker55177552018-10-24 12:29:53 +010051 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020052 *
Hanno Becker55177552018-10-24 12:29:53 +010053 * \param p The reference to the current position pointer.
54 * \param start The start of the buffer, for bounds-checking.
55 * \param len The length value to write.
56 *
57 * \return The number of bytes written to \p p on success.
58 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020059 */
Hanno Becker55177552018-10-24 12:29:53 +010060int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
61 size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020062/**
Hanno Becker55177552018-10-24 12:29:53 +010063 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020064 *
Hanno Becker55177552018-10-24 12:29:53 +010065 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020066 *
Hanno Becker55177552018-10-24 12:29:53 +010067 * \param p The reference to the current position pointer.
68 * \param start The start of the buffer, for bounds-checking.
69 * \param tag The tag to write.
70 *
71 * \return The number of bytes written to \p p on success.
72 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +010075 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020076
Paul Bakker9852d002013-08-26 17:56:37 +020077/**
Hanno Becker55177552018-10-24 12:29:53 +010078 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020079 *
Hanno Becker55177552018-10-24 12:29:53 +010080 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020081 *
Hanno Becker55177552018-10-24 12:29:53 +010082 * \param p The reference to the current position pointer.
83 * \param start The start of the buffer, for bounds-checking.
84 * \param buf The data buffer to write.
85 * \param size The length of the data buffer.
86 *
87 * \return The number of bytes written to \p p on success.
88 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +020089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +010091 const unsigned char *buf, size_t size );
Paul Bakker9852d002013-08-26 17:56:37 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020094/**
Hanno Becker55177552018-10-24 12:29:53 +010095 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
96 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020097 *
Hanno Becker55177552018-10-24 12:29:53 +010098 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020099 *
Hanno Becker55177552018-10-24 12:29:53 +0100100 * \param p The reference to the current position pointer.
101 * \param start The start of the buffer, for bounds-checking.
102 * \param X The MPI to write.
103 *
104 * \return The number of bytes written to \p p on success.
105 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200106 */
Hanno Becker55177552018-10-24 12:29:53 +0100107int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
108 const mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200110
111/**
Hanno Becker55177552018-10-24 12:29:53 +0100112 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
113 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200114 *
Hanno Becker55177552018-10-24 12:29:53 +0100115 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200116 *
Hanno Becker55177552018-10-24 12:29:53 +0100117 * \param p The reference to the current position pointer.
118 * \param start The start of the buffer, for bounds-checking.
119 *
120 * \return The number of bytes written to \p p on success.
121 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200124
125/**
Hanno Becker55177552018-10-24 12:29:53 +0100126 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
127 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200128 *
Hanno Becker55177552018-10-24 12:29:53 +0100129 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200130 *
Hanno Becker55177552018-10-24 12:29:53 +0100131 * \param p The reference to the current position pointer.
132 * \param start The start of the buffer, for bounds-checking.
133 * \param oid The OID to write.
134 * \param oid_len The length of the OID.
135 *
136 * \return The number of bytes written to \p p on success.
137 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100140 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200141
142/**
Hanno Becker55177552018-10-24 12:29:53 +0100143 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200144 *
Hanno Becker55177552018-10-24 12:29:53 +0100145 * \note This function works backwards in data buffer.
146 *
147 * \param p The reference to the current position pointer.
148 * \param start The start of the buffer, for bounds-checking.
149 * \param oid The OID of the algorithm to write.
150 * \param oid_len The length of the algorithm's OID.
151 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200152 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200153 *
Hanno Becker55177552018-10-24 12:29:53 +0100154 * \return The number of bytes written to \p p on success.
155 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200156 */
Hanno Becker55177552018-10-24 12:29:53 +0100157int mbedtls_asn1_write_algorithm_identifier( unsigned char **p,
158 unsigned char *start,
159 const char *oid, size_t oid_len,
160 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200161
162/**
Hanno Becker55177552018-10-24 12:29:53 +0100163 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
164 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200165 *
Hanno Becker55177552018-10-24 12:29:53 +0100166 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200167 *
Hanno Becker55177552018-10-24 12:29:53 +0100168 * \param p The reference to the current position pointer.
169 * \param start The start of the buffer, for bounds-checking.
170 * \param boolean The boolean value to write, either \c 0 or \c 1.
171 *
172 * \return The number of bytes written to \p p on success.
173 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200174 */
Hanno Becker55177552018-10-24 12:29:53 +0100175int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
176 int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200177
178/**
Hanno Becker55177552018-10-24 12:29:53 +0100179 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
180 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200181 *
Hanno Becker55177552018-10-24 12:29:53 +0100182 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200183 *
Hanno Becker55177552018-10-24 12:29:53 +0100184 * \param p The reference to the current position pointer.
185 * \param start The start of the buffer, for bounds-checking.
186 * \param val The integer value to write.
187 *
188 * \return The number of bytes written to \p p on success.
189 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200192
193/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100194 * \brief Write a string in ASN.1 format using a specific
195 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100196
197 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100198 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100199 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100200 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100201 * \param tag The string encoding tag to write, e.g.
202 * #MBEDTLS_ASN1_UTF8_STRING.
203 * \param text The string to write.
204 * \param text_len The length of \p text in bytes (which might
205 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100206 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100207 * \return The number of bytes written to \p p on success.
208 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100209 */
thomas-deeeba6c9b2018-09-19 09:10:37 +0200210int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100211 int tag, const char *text,
212 size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200213
214/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100215 * \brief Write a string in ASN.1 format using the PrintableString
216 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100217 *
218 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100219 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100220 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100221 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100222 * \param text The string to write.
223 * \param text_len The length of \p text in bytes (which might
224 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100225 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100226 * \return The number of bytes written to \p p on success.
227 * \return A negative error code on failure.
228 */
229int mbedtls_asn1_write_printable_string( unsigned char **p,
230 unsigned char *start,
231 const char *text, size_t text_len );
232
233/**
234 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
235 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100236 *
237 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100238 *
239 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100240 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100241 * \param text The string to write.
242 * \param text_len The length of \p text in bytes (which might
243 * be strictly larger than the number of characters).
244 *
245 * \return The number of bytes written to \p p on success.
246 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100247 */
248int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100249 const char *text, size_t text_len );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100250
251/**
Hanno Becker55177552018-10-24 12:29:53 +0100252 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100253 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100254 *
255 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200256 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100257 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100258 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100259 * \param text The string to write.
260 * \param text_len The length of \p text in bytes (which might
261 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200262 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100263 * \return The number of bytes written to \p p on success.
264 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100267 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200268
269/**
Hanno Becker55177552018-10-24 12:29:53 +0100270 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
271 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200272 *
Hanno Becker55177552018-10-24 12:29:53 +0100273 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200274 *
Hanno Becker55177552018-10-24 12:29:53 +0100275 * \param p The reference to the current position pointer.
276 * \param start The start of the buffer, for bounds-checking.
277 * \param buf The bitstring to write.
278 * \param bits The total number of bits in the bitstring.
279 *
280 * \return The number of bytes written to \p p on success.
281 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100284 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200285
286/**
Hanno Becker55177552018-10-24 12:29:53 +0100287 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
288 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200289 *
Hanno Becker55177552018-10-24 12:29:53 +0100290 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200291 *
Hanno Becker55177552018-10-24 12:29:53 +0100292 * \param p The reference to the current position pointer.
293 * \param start The start of the buffer, for bounds-checking.
294 * \param buf The buffer holding the data to write.
295 * \param size The length of the data buffer \p buf.
296 *
297 * \return The number of bytes written to \p p on success.
298 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100301 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200302
303/**
304 * \brief Create or find a specific named_data entry for writing in a
305 * sequence or list based on the OID. If not already in there,
306 * a new entry is added to the head of the list.
307 * Warning: Destructive behaviour for the val data!
308 *
Hanno Becker55177552018-10-24 12:29:53 +0100309 * \param list The pointer to the location of the head of the list to seek
310 * through (will be updated in case of a new entry).
311 * \param oid The OID to look for.
312 * \param oid_len The size of the OID.
313 * \param val The data to store (can be \c NULL if you want to fill
314 * it by hand).
315 * \param val_len The minimum length of the data buffer needed.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200316 *
Hanno Becker55177552018-10-24 12:29:53 +0100317 * \return A pointer to the new / existing entry on success.
318 * \return \c NULL if if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200321 const char *oid, size_t oid_len,
322 const unsigned char *val,
323 size_t val_len );
324
Paul Bakker407a0da2013-06-27 14:29:21 +0200325#ifdef __cplusplus
326}
327#endif
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#endif /* MBEDTLS_ASN1_WRITE_H */