blob: 93c570a45c94666b108afc27a5343a4b8e36a764 [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úti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
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 * **********
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 Eldor0559c662018-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020061 g += ret; } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
Paul Bakker407a0da2013-06-27 14:29:21 +020063#ifdef __cplusplus
64extern "C" {
65#endif
66
Paul Bakker7accbce2013-08-26 17:34:53 +020067/**
68 * \brief Write a length field in ASN.1 format
69 * Note: function works backwards in data buffer
70 *
71 * \param p reference to current position pointer
72 * \param start start of the buffer (for bounds-checking)
73 * \param len the length to write
74 *
75 * \return the length written or a negative error code
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020078
79/**
80 * \brief Write a ASN.1 tag in ASN.1 format
81 * Note: function works backwards in data buffer
82 *
83 * \param p reference to current position pointer
84 * \param start start of the buffer (for bounds-checking)
85 * \param tag the tag to write
86 *
87 * \return the length written or a negative error code
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020090 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020091
Paul Bakker9852d002013-08-26 17:56:37 +020092/**
93 * \brief Write raw buffer data
94 * Note: function works backwards in data buffer
95 *
96 * \param p reference to current position pointer
97 * \param start start of the buffer (for bounds-checking)
98 * \param buf data buffer to write
99 * \param size length of the data buffer
100 *
101 * \return the length written or a negative error code
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200104 const unsigned char *buf, size_t size );
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +0200107/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200109 * Note: function works backwards in data buffer
110 *
111 * \param p reference to current position pointer
112 * \param start start of the buffer (for bounds-checking)
113 * \param X the MPI to write
114 *
115 * \return the length written or a negative error code
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
118#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200119
120/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200122 * Note: function works backwards in data buffer
123 *
124 * \param p reference to current position pointer
125 * \param start start of the buffer (for bounds-checking)
126 *
127 * \return the length written or a negative error code
128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200130
131/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200133 * Note: function works backwards in data buffer
134 *
135 * \param p reference to current position pointer
136 * \param start start of the buffer (for bounds-checking)
137 * \param oid the OID to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200138 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200139 *
140 * \return the length written or a negative error code
141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200143 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200144
145/**
146 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
147 * Note: function works backwards in data buffer
Paul Bakker7accbce2013-08-26 17:34:53 +0200148 *
149 * \param p reference to current position pointer
150 * \param start start of the buffer (for bounds-checking)
151 * \param oid the OID of the algorithm
Paul Bakker5f45e622013-09-09 12:02:36 +0200152 * \param oid_len length of the OID
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200153 * \param par_len length of parameters, which must be already written.
154 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200155 *
156 * \return the length written or a negative error code
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200159 const char *oid, size_t oid_len,
160 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200161
162/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
Paul Bakker329def32013-09-06 16:34:38 +0200164 * Note: function works backwards in data buffer
165 *
166 * \param p reference to current position pointer
167 * \param start start of the buffer (for bounds-checking)
168 * \param boolean 0 or 1
169 *
170 * \return the length written or a negative error code
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200173
174/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200176 * Note: function works backwards in data buffer
177 *
178 * \param p reference to current position pointer
179 * \param start start of the buffer (for bounds-checking)
180 * \param val the integer value
181 *
182 * \return the length written or a negative error code
183 */
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/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200188 * value in ASN.1 format
189 * Note: function works backwards in data buffer
190 *
191 * \param p reference to current position pointer
192 * \param start start of the buffer (for bounds-checking)
193 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200194 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200195 *
196 * \return the length written or a negative error code
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200199 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200200
201/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200203 * value in ASN.1 format
204 * Note: function works backwards in data buffer
205 *
206 * \param p reference to current position pointer
207 * \param start start of the buffer (for bounds-checking)
208 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200209 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200210 *
211 * \return the length written or a negative error code
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200214 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200215
216/**
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100217 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
218 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200219 *
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100220 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200221 *
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100222 * \param p The reference to the current position pointer.
223 * \param start The start of the buffer, for bounds-checking.
224 * \param buf The bitstring to write.
225 * \param bits The total number of bits in the bitstring.
226 *
227 * \return The number of bytes written to \p p on success.
228 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200231 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200232
233/**
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100234 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
235 * and value in ASN.1 format.
236 *
237 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200238 *
239 * \param p reference to current position pointer
240 * \param start start of the buffer (for bounds-checking)
241 * \param buf data buffer to write
242 * \param size length of the data buffer
243 *
244 * \return the length written or a negative error code
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200247 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200248
249/**
250 * \brief Create or find a specific named_data entry for writing in a
251 * sequence or list based on the OID. If not already in there,
252 * a new entry is added to the head of the list.
253 * Warning: Destructive behaviour for the val data!
254 *
255 * \param list Pointer to the location of the head of the list to seek
256 * through (will be updated in case of a new entry)
257 * \param oid The OID to look for
258 * \param oid_len Size of the OID
259 * \param val Data to store (can be NULL if you want to fill it by hand)
260 * \param val_len Minimum length of the data buffer needed
261 *
262 * \return NULL if if there was a memory allocation error, or a pointer
263 * to the new / existing entry.
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200266 const char *oid, size_t oid_len,
267 const unsigned char *val,
268 size_t val_len );
269
Paul Bakker407a0da2013-06-27 14:29:21 +0200270#ifdef __cplusplus
271}
272#endif
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_ASN1_WRITE_H */