blob: 3c7cdd6b469f1ec5040b237647285e0ea418dcf1 [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/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +02007 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_ASN1_WRITE_H
50#define MBEDTLS_ASN1_WRITE_H
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020052#if !defined(MBEDTLS_CONFIG_FILE)
53#include "config.h"
54#else
55#include MBEDTLS_CONFIG_FILE
56#endif
57
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058#include "asn1.h"
59
Hanno Becker55177552018-10-24 12:29:53 +010060#define MBEDTLS_ASN1_CHK_ADD(g, f) \
Hanno Beckerd6028a12018-10-15 12:01:35 +010061 do \
62 { \
63 if( ( ret = (f) ) < 0 ) \
Hanno Becker55177552018-10-24 12:29:53 +010064 return( ret ); \
65 else \
Hanno Beckerd6028a12018-10-15 12:01:35 +010066 (g) += ret; \
Hanno Becker55177552018-10-24 12:29:53 +010067 } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
Paul Bakker407a0da2013-06-27 14:29:21 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
Paul Bakker7accbce2013-08-26 17:34:53 +020073/**
Hanno Becker55177552018-10-24 12:29:53 +010074 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020075 *
Hanno Becker55177552018-10-24 12:29:53 +010076 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020077 *
Hanno Becker55177552018-10-24 12:29:53 +010078 * \param p The reference to the current position pointer.
79 * \param start The start of the buffer, for bounds-checking.
80 * \param len The length value to write.
81 *
82 * \return The number of bytes written to \p p on success.
83 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020084 */
Hanno Becker55177552018-10-24 12:29:53 +010085int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
86 size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020087/**
Hanno Becker55177552018-10-24 12:29:53 +010088 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020089 *
Hanno Becker55177552018-10-24 12:29:53 +010090 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020091 *
Hanno Becker55177552018-10-24 12:29:53 +010092 * \param p The reference to the current position pointer.
93 * \param start The start of the buffer, for bounds-checking.
94 * \param tag The tag to write.
95 *
96 * \return The number of bytes written to \p p on success.
97 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100100 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +0200101
Paul Bakker9852d002013-08-26 17:56:37 +0200102/**
Hanno Becker55177552018-10-24 12:29:53 +0100103 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +0200104 *
Hanno Becker55177552018-10-24 12:29:53 +0100105 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +0200106 *
Hanno Becker55177552018-10-24 12:29:53 +0100107 * \param p The reference to the current position pointer.
108 * \param start The start of the buffer, for bounds-checking.
109 * \param buf The data buffer to write.
110 * \param size The length of the data buffer.
111 *
112 * \return The number of bytes written to \p p on success.
113 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +0200114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100116 const unsigned char *buf, size_t size );
Paul Bakker9852d002013-08-26 17:56:37 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +0200119/**
Hanno Becker55177552018-10-24 12:29:53 +0100120 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
121 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200122 *
Hanno Becker55177552018-10-24 12:29:53 +0100123 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200124 *
Hanno Becker55177552018-10-24 12:29:53 +0100125 * \param p The reference to the current position pointer.
126 * \param start The start of the buffer, for bounds-checking.
127 * \param X The MPI to write.
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 */
Hanno Becker55177552018-10-24 12:29:53 +0100132int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
133 const mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200135
136/**
Hanno Becker55177552018-10-24 12:29:53 +0100137 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
138 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200139 *
Hanno Becker55177552018-10-24 12:29:53 +0100140 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200141 *
Hanno Becker55177552018-10-24 12:29:53 +0100142 * \param p The reference to the current position pointer.
143 * \param start The start of the buffer, for bounds-checking.
144 *
145 * \return The number of bytes written to \p p on success.
146 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200149
150/**
Hanno Becker55177552018-10-24 12:29:53 +0100151 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
152 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200153 *
Hanno Becker55177552018-10-24 12:29:53 +0100154 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200155 *
Hanno Becker55177552018-10-24 12:29:53 +0100156 * \param p The reference to the current position pointer.
157 * \param start The start of the buffer, for bounds-checking.
158 * \param oid The OID to write.
159 * \param oid_len The length of the OID.
160 *
161 * \return The number of bytes written to \p p on success.
162 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100165 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200166
167/**
Hanno Becker55177552018-10-24 12:29:53 +0100168 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200169 *
Hanno Becker55177552018-10-24 12:29:53 +0100170 * \note This function works backwards in data buffer.
171 *
172 * \param p The reference to the current position pointer.
173 * \param start The start of the buffer, for bounds-checking.
174 * \param oid The OID of the algorithm to write.
175 * \param oid_len The length of the algorithm's OID.
176 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200177 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200178 *
Hanno Becker55177552018-10-24 12:29:53 +0100179 * \return The number of bytes written to \p p on success.
180 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200181 */
Hanno Becker55177552018-10-24 12:29:53 +0100182int mbedtls_asn1_write_algorithm_identifier( unsigned char **p,
183 unsigned char *start,
184 const char *oid, size_t oid_len,
185 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200186
187/**
Hanno Becker55177552018-10-24 12:29:53 +0100188 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
189 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200190 *
Hanno Becker55177552018-10-24 12:29:53 +0100191 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200192 *
Hanno Becker55177552018-10-24 12:29:53 +0100193 * \param p The reference to the current position pointer.
194 * \param start The start of the buffer, for bounds-checking.
195 * \param boolean The boolean value to write, either \c 0 or \c 1.
196 *
197 * \return The number of bytes written to \p p on success.
198 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200199 */
Hanno Becker55177552018-10-24 12:29:53 +0100200int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
201 int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200202
203/**
Hanno Becker55177552018-10-24 12:29:53 +0100204 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
205 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200206 *
Hanno Becker55177552018-10-24 12:29:53 +0100207 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200208 *
Hanno Becker55177552018-10-24 12:29:53 +0100209 * \param p The reference to the current position pointer.
210 * \param start The start of the buffer, for bounds-checking.
211 * \param val The integer value to write.
212 *
213 * \return The number of bytes written to \p p on success.
214 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200217
218/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100219 * \brief Write a string in ASN.1 format using a specific
220 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100221
222 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100223 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100224 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100225 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100226 * \param tag The string encoding tag to write, e.g.
227 * #MBEDTLS_ASN1_UTF8_STRING.
228 * \param text The string to write.
229 * \param text_len The length of \p text in bytes (which might
230 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100231 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100232 * \return The number of bytes written to \p p on success.
233 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100234 */
thomas-deeeba6c9b2018-09-19 09:10:37 +0200235int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100236 int tag, const char *text,
237 size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200238
239/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100240 * \brief Write a string in ASN.1 format using the PrintableString
241 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100242 *
243 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100244 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100245 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100246 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100247 * \param text The string to write.
248 * \param text_len The length of \p text in bytes (which might
249 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100250 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100251 * \return The number of bytes written to \p p on success.
252 * \return A negative error code on failure.
253 */
254int mbedtls_asn1_write_printable_string( unsigned char **p,
255 unsigned char *start,
256 const char *text, size_t text_len );
257
258/**
259 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
260 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100261 *
262 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100263 *
264 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100265 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100266 * \param text The string to write.
267 * \param text_len The length of \p text in bytes (which might
268 * be strictly larger than the number of characters).
269 *
270 * \return The number of bytes written to \p p on success.
271 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100272 */
273int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100274 const char *text, size_t text_len );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100275
276/**
Hanno Becker55177552018-10-24 12:29:53 +0100277 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100278 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100279 *
280 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200281 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100282 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100283 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100284 * \param text The string to write.
285 * \param text_len The length of \p text in bytes (which might
286 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200287 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100288 * \return The number of bytes written to \p p on success.
289 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100292 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200293
294/**
Hanno Becker55177552018-10-24 12:29:53 +0100295 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
296 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200297 *
Hanno Becker55177552018-10-24 12:29:53 +0100298 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200299 *
Hanno Becker55177552018-10-24 12:29:53 +0100300 * \param p The reference to the current position pointer.
301 * \param start The start of the buffer, for bounds-checking.
302 * \param buf The bitstring to write.
303 * \param bits The total number of bits in the bitstring.
304 *
305 * \return The number of bytes written to \p p on success.
306 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100309 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200310
311/**
Hanno Becker55177552018-10-24 12:29:53 +0100312 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
313 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200314 *
Hanno Becker55177552018-10-24 12:29:53 +0100315 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200316 *
Hanno Becker55177552018-10-24 12:29:53 +0100317 * \param p The reference to the current position pointer.
318 * \param start The start of the buffer, for bounds-checking.
319 * \param buf The buffer holding the data to write.
320 * \param size The length of the data buffer \p buf.
321 *
322 * \return The number of bytes written to \p p on success.
323 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100326 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200327
328/**
329 * \brief Create or find a specific named_data entry for writing in a
330 * sequence or list based on the OID. If not already in there,
331 * a new entry is added to the head of the list.
332 * Warning: Destructive behaviour for the val data!
333 *
Hanno Becker55177552018-10-24 12:29:53 +0100334 * \param list The pointer to the location of the head of the list to seek
335 * through (will be updated in case of a new entry).
336 * \param oid The OID to look for.
337 * \param oid_len The size of the OID.
338 * \param val The data to store (can be \c NULL if you want to fill
339 * it by hand).
340 * \param val_len The minimum length of the data buffer needed.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200341 *
Hanno Becker55177552018-10-24 12:29:53 +0100342 * \return A pointer to the new / existing entry on success.
343 * \return \c NULL if if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200346 const char *oid, size_t oid_len,
347 const unsigned char *val,
348 size_t val_len );
349
Paul Bakker407a0da2013-06-27 14:29:21 +0200350#ifdef __cplusplus
351}
352#endif
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_ASN1_WRITE_H */