blob: 79d2bfce414cd573083d8c819bf3e5e410bab2d8 [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úti4e9f7122020-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úti4e9f7122020-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 Eldor0559c662018-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020063 g += ret; } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064
Paul Bakker407a0da2013-06-27 14:29:21 +020065#ifdef __cplusplus
66extern "C" {
67#endif
68
Paul Bakker7accbce2013-08-26 17:34:53 +020069/**
70 * \brief Write a length field in ASN.1 format
71 * Note: function works backwards in data buffer
72 *
73 * \param p reference to current position pointer
74 * \param start start of the buffer (for bounds-checking)
75 * \param len the length to write
76 *
77 * \return the length written or a negative error code
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020080
81/**
82 * \brief Write a ASN.1 tag in ASN.1 format
83 * Note: function works backwards in data buffer
84 *
85 * \param p reference to current position pointer
86 * \param start start of the buffer (for bounds-checking)
87 * \param tag the tag to write
88 *
89 * \return the length written or a negative error code
90 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020092 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020093
Paul Bakker9852d002013-08-26 17:56:37 +020094/**
95 * \brief Write raw buffer data
96 * Note: function works backwards in data buffer
97 *
98 * \param p reference to current position pointer
99 * \param start start of the buffer (for bounds-checking)
100 * \param buf data buffer to write
101 * \param size length of the data buffer
102 *
103 * \return the length written or a negative error code
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200106 const unsigned char *buf, size_t size );
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +0200109/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200111 * Note: function works backwards in data buffer
112 *
113 * \param p reference to current position pointer
114 * \param start start of the buffer (for bounds-checking)
115 * \param X the MPI to write
116 *
117 * \return the length written or a negative error code
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
120#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200121
122/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200124 * Note: function works backwards in data buffer
125 *
126 * \param p reference to current position pointer
127 * \param start start of the buffer (for bounds-checking)
128 *
129 * \return the length written or a negative error code
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200132
133/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200135 * Note: function works backwards in data buffer
136 *
137 * \param p reference to current position pointer
138 * \param start start of the buffer (for bounds-checking)
139 * \param oid the OID to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200140 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200141 *
142 * \return the length written or a negative error code
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200145 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200146
147/**
148 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
149 * Note: function works backwards in data buffer
Paul Bakker7accbce2013-08-26 17:34:53 +0200150 *
151 * \param p reference to current position pointer
152 * \param start start of the buffer (for bounds-checking)
153 * \param oid the OID of the algorithm
Paul Bakker5f45e622013-09-09 12:02:36 +0200154 * \param oid_len length of the OID
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200155 * \param par_len length of parameters, which must be already written.
156 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200157 *
158 * \return the length written or a negative error code
159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200161 const char *oid, size_t oid_len,
162 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200163
164/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
Paul Bakker329def32013-09-06 16:34:38 +0200166 * Note: function works backwards in data buffer
167 *
168 * \param p reference to current position pointer
169 * \param start start of the buffer (for bounds-checking)
170 * \param boolean 0 or 1
171 *
172 * \return the length written or a negative error code
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200175
176/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200178 * Note: function works backwards in data buffer
179 *
180 * \param p reference to current position pointer
181 * \param start start of the buffer (for bounds-checking)
182 * \param val the integer value
183 *
184 * \return the length written or a negative error code
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200187
188/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200190 * value in ASN.1 format
191 * Note: function works backwards in data buffer
192 *
193 * \param p reference to current position pointer
194 * \param start start of the buffer (for bounds-checking)
195 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200196 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200197 *
198 * \return the length written or a negative error code
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200201 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200202
203/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200205 * value in ASN.1 format
206 * Note: function works backwards in data buffer
207 *
208 * \param p reference to current position pointer
209 * \param start start of the buffer (for bounds-checking)
210 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200211 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200212 *
213 * \return the length written or a negative error code
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200216 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200217
218/**
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100219 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
220 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200221 *
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100222 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200223 *
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100224 * \param p The reference to the current position pointer.
225 * \param start The start of the buffer, for bounds-checking.
226 * \param buf The bitstring to write.
227 * \param bits The total number of bits in the bitstring.
228 *
229 * \return The number of bytes written to \p p on success.
230 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200233 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200234
235/**
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100236 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
237 * and value in ASN.1 format.
238 *
239 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200240 *
241 * \param p reference to current position pointer
242 * \param start start of the buffer (for bounds-checking)
243 * \param buf data buffer to write
244 * \param size length of the data buffer
245 *
246 * \return the length written or a negative error code
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200249 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200250
251/**
252 * \brief Create or find a specific named_data entry for writing in a
253 * sequence or list based on the OID. If not already in there,
254 * a new entry is added to the head of the list.
255 * Warning: Destructive behaviour for the val data!
256 *
257 * \param list Pointer to the location of the head of the list to seek
258 * through (will be updated in case of a new entry)
259 * \param oid The OID to look for
260 * \param oid_len Size of the OID
261 * \param val Data to store (can be NULL if you want to fill it by hand)
262 * \param val_len Minimum length of the data buffer needed
263 *
264 * \return NULL if if there was a memory allocation error, or a pointer
265 * to the new / existing entry.
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200268 const char *oid, size_t oid_len,
269 const unsigned char *val,
270 size_t val_len );
271
Paul Bakker407a0da2013-06-27 14:29:21 +0200272#ifdef __cplusplus
273}
274#endif
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_ASN1_WRITE_H */