blob: 4fed59371ca3c1abe3d7b1952e245e716730701a [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_ASN1_WRITE_H
52#define MBEDTLS_ASN1_WRITE_H
Paul Bakkerbdb912d2012-02-13 23:11:30 +000053
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060#include "asn1.h"
61
Hanno Becker55177552018-10-24 12:29:53 +010062#define MBEDTLS_ASN1_CHK_ADD(g, f) \
Hanno Beckerd6028a12018-10-15 12:01:35 +010063 do \
64 { \
65 if( ( ret = (f) ) < 0 ) \
Hanno Becker55177552018-10-24 12:29:53 +010066 return( ret ); \
67 else \
Hanno Beckerd6028a12018-10-15 12:01:35 +010068 (g) += ret; \
Hanno Becker55177552018-10-24 12:29:53 +010069 } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Paul Bakker7accbce2013-08-26 17:34:53 +020075/**
Hanno Becker55177552018-10-24 12:29:53 +010076 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020077 *
Hanno Becker55177552018-10-24 12:29:53 +010078 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020079 *
Hanno Becker55177552018-10-24 12:29:53 +010080 * \param p The reference to the current position pointer.
81 * \param start The start of the buffer, for bounds-checking.
82 * \param len The length value to write.
83 *
84 * \return The number of bytes written to \p p on success.
85 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020086 */
Hanno Becker55177552018-10-24 12:29:53 +010087int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
88 size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020089/**
Hanno Becker55177552018-10-24 12:29:53 +010090 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020091 *
Hanno Becker55177552018-10-24 12:29:53 +010092 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020093 *
Hanno Becker55177552018-10-24 12:29:53 +010094 * \param p The reference to the current position pointer.
95 * \param start The start of the buffer, for bounds-checking.
96 * \param tag The tag to write.
97 *
98 * \return The number of bytes written to \p p on success.
99 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100102 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +0200103
Paul Bakker9852d002013-08-26 17:56:37 +0200104/**
Hanno Becker55177552018-10-24 12:29:53 +0100105 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +0200106 *
Hanno Becker55177552018-10-24 12:29:53 +0100107 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +0200108 *
Hanno Becker55177552018-10-24 12:29:53 +0100109 * \param p The reference to the current position pointer.
110 * \param start The start of the buffer, for bounds-checking.
111 * \param buf The data buffer to write.
112 * \param size The length of the data buffer.
113 *
114 * \return The number of bytes written to \p p on success.
115 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +0200116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100118 const unsigned char *buf, size_t size );
Paul Bakker9852d002013-08-26 17:56:37 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +0200121/**
Hanno Becker55177552018-10-24 12:29:53 +0100122 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
123 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200124 *
Hanno Becker55177552018-10-24 12:29:53 +0100125 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200126 *
Hanno Becker55177552018-10-24 12:29:53 +0100127 * \param p The reference to the current position pointer.
128 * \param start The start of the buffer, for bounds-checking.
129 * \param X The MPI to write.
130 *
131 * \return The number of bytes written to \p p on success.
132 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200133 */
Hanno Becker55177552018-10-24 12:29:53 +0100134int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
135 const mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200137
138/**
Hanno Becker55177552018-10-24 12:29:53 +0100139 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
140 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200141 *
Hanno Becker55177552018-10-24 12:29:53 +0100142 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200143 *
Hanno Becker55177552018-10-24 12:29:53 +0100144 * \param p The reference to the current position pointer.
145 * \param start The start of the buffer, for bounds-checking.
146 *
147 * \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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200151
152/**
Hanno Becker55177552018-10-24 12:29:53 +0100153 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
154 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200155 *
Hanno Becker55177552018-10-24 12:29:53 +0100156 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200157 *
Hanno Becker55177552018-10-24 12:29:53 +0100158 * \param p The reference to the current position pointer.
159 * \param start The start of the buffer, for bounds-checking.
160 * \param oid The OID to write.
161 * \param oid_len The length of the OID.
162 *
163 * \return The number of bytes written to \p p on success.
164 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100167 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200168
169/**
Hanno Becker55177552018-10-24 12:29:53 +0100170 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200171 *
Hanno Becker55177552018-10-24 12:29:53 +0100172 * \note This function works backwards in data buffer.
173 *
174 * \param p The reference to the current position pointer.
175 * \param start The start of the buffer, for bounds-checking.
176 * \param oid The OID of the algorithm to write.
177 * \param oid_len The length of the algorithm's OID.
178 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200179 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200180 *
Hanno Becker55177552018-10-24 12:29:53 +0100181 * \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 */
Hanno Becker55177552018-10-24 12:29:53 +0100184int mbedtls_asn1_write_algorithm_identifier( unsigned char **p,
185 unsigned char *start,
186 const char *oid, size_t oid_len,
187 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200188
189/**
Hanno Becker55177552018-10-24 12:29:53 +0100190 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
191 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200192 *
Hanno Becker55177552018-10-24 12:29:53 +0100193 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200194 *
Hanno Becker55177552018-10-24 12:29:53 +0100195 * \param p The reference to the current position pointer.
196 * \param start The start of the buffer, for bounds-checking.
197 * \param boolean The boolean value to write, either \c 0 or \c 1.
198 *
199 * \return The number of bytes written to \p p on success.
200 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200201 */
Hanno Becker55177552018-10-24 12:29:53 +0100202int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
203 int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200204
205/**
Hanno Becker55177552018-10-24 12:29:53 +0100206 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
207 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200208 *
Hanno Becker55177552018-10-24 12:29:53 +0100209 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200210 *
Hanno Becker55177552018-10-24 12:29:53 +0100211 * \param p The reference to the current position pointer.
212 * \param start The start of the buffer, for bounds-checking.
213 * \param val The integer value to write.
214 *
215 * \return The number of bytes written to \p p on success.
216 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200219
220/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100221 * \brief Write a string in ASN.1 format using a specific
222 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100223
224 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100225 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100226 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100227 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100228 * \param tag The string encoding tag to write, e.g.
229 * #MBEDTLS_ASN1_UTF8_STRING.
230 * \param text The string to write.
231 * \param text_len The length of \p text in bytes (which might
232 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100233 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100234 * \return The number of bytes written to \p p on success.
235 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100236 */
thomas-deeeba6c9b2018-09-19 09:10:37 +0200237int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100238 int tag, const char *text,
239 size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200240
241/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100242 * \brief Write a string in ASN.1 format using the PrintableString
243 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100244 *
245 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100246 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100247 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100248 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100249 * \param text The string to write.
250 * \param text_len The length of \p text in bytes (which might
251 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100252 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100253 * \return The number of bytes written to \p p on success.
254 * \return A negative error code on failure.
255 */
256int mbedtls_asn1_write_printable_string( unsigned char **p,
257 unsigned char *start,
258 const char *text, size_t text_len );
259
260/**
261 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
262 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100263 *
264 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100265 *
266 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100267 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100268 * \param text The string to write.
269 * \param text_len The length of \p text in bytes (which might
270 * be strictly larger than the number of characters).
271 *
272 * \return The number of bytes written to \p p on success.
273 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100274 */
275int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100276 const char *text, size_t text_len );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100277
278/**
Hanno Becker55177552018-10-24 12:29:53 +0100279 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100280 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100281 *
282 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200283 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100284 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100285 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100286 * \param text The string to write.
287 * \param text_len The length of \p text in bytes (which might
288 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200289 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100290 * \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_ia5_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100294 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200295
296/**
Hanno Becker55177552018-10-24 12:29:53 +0100297 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
298 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200299 *
Hanno Becker55177552018-10-24 12:29:53 +0100300 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200301 *
Hanno Becker55177552018-10-24 12:29:53 +0100302 * \param p The reference to the current position pointer.
303 * \param start The start of the buffer, for bounds-checking.
304 * \param buf The bitstring to write.
305 * \param bits The total number of bits in the bitstring.
306 *
307 * \return The number of bytes written to \p p on success.
308 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100311 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200312
313/**
Hanno Becker55177552018-10-24 12:29:53 +0100314 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
315 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200316 *
Hanno Becker55177552018-10-24 12:29:53 +0100317 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200318 *
Hanno Becker55177552018-10-24 12:29:53 +0100319 * \param p The reference to the current position pointer.
320 * \param start The start of the buffer, for bounds-checking.
321 * \param buf The buffer holding the data to write.
322 * \param size The length of the data buffer \p buf.
323 *
324 * \return The number of bytes written to \p p on success.
325 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100328 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200329
330/**
331 * \brief Create or find a specific named_data entry for writing in a
332 * sequence or list based on the OID. If not already in there,
333 * a new entry is added to the head of the list.
334 * Warning: Destructive behaviour for the val data!
335 *
Hanno Becker55177552018-10-24 12:29:53 +0100336 * \param list The pointer to the location of the head of the list to seek
337 * through (will be updated in case of a new entry).
338 * \param oid The OID to look for.
339 * \param oid_len The size of the OID.
340 * \param val The data to store (can be \c NULL if you want to fill
341 * it by hand).
342 * \param val_len The minimum length of the data buffer needed.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200343 *
Hanno Becker55177552018-10-24 12:29:53 +0100344 * \return A pointer to the new / existing entry on success.
345 * \return \c NULL if if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200348 const char *oid, size_t oid_len,
349 const unsigned char *val,
350 size_t val_len );
351
Paul Bakker407a0da2013-06-27 14:29:21 +0200352#ifdef __cplusplus
353}
354#endif
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_ASN1_WRITE_H */