blob: 083601af320e495bbdd7d54bf0623fd4abc77ad3 [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 Eldor0559c662018-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020036 g += ret; } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000037
Paul Bakker407a0da2013-06-27 14:29:21 +020038#ifdef __cplusplus
39extern "C" {
40#endif
41
Paul Bakker7accbce2013-08-26 17:34:53 +020042/**
43 * \brief Write a length field in ASN.1 format
44 * Note: function works backwards in data buffer
45 *
46 * \param p reference to current position pointer
47 * \param start start of the buffer (for bounds-checking)
48 * \param len the length to write
49 *
50 * \return the length written or a negative error code
51 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020053
54/**
55 * \brief Write a ASN.1 tag in ASN.1 format
56 * Note: function works backwards in data buffer
57 *
58 * \param p reference to current position pointer
59 * \param start start of the buffer (for bounds-checking)
60 * \param tag the tag to write
61 *
62 * \return the length written or a negative error code
63 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020065 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020066
Paul Bakker9852d002013-08-26 17:56:37 +020067/**
68 * \brief Write raw buffer data
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 buf data buffer to write
74 * \param size length of the data buffer
75 *
76 * \return the length written or a negative error code
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +020079 const unsigned char *buf, size_t size );
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020082/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +020084 * Note: function works backwards in data buffer
85 *
86 * \param p reference to current position pointer
87 * \param start start of the buffer (for bounds-checking)
88 * \param X the MPI to write
89 *
90 * \return the length written or a negative error code
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
93#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +020094
95/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +020097 * Note: function works backwards in data buffer
98 *
99 * \param p reference to current position pointer
100 * \param start start of the buffer (for bounds-checking)
101 *
102 * \return the length written or a negative error code
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200105
106/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200108 * Note: function works backwards in data buffer
109 *
110 * \param p reference to current position pointer
111 * \param start start of the buffer (for bounds-checking)
112 * \param oid the OID to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200113 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200114 *
115 * \return the length written or a negative error code
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200118 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200119
120/**
121 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
122 * Note: function works backwards in data buffer
Paul Bakker7accbce2013-08-26 17:34:53 +0200123 *
124 * \param p reference to current position pointer
125 * \param start start of the buffer (for bounds-checking)
126 * \param oid the OID of the algorithm
Paul Bakker5f45e622013-09-09 12:02:36 +0200127 * \param oid_len length of the OID
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200128 * \param par_len length of parameters, which must be already written.
129 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200130 *
131 * \return the length written or a negative error code
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200134 const char *oid, size_t oid_len,
135 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200136
137/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
Paul Bakker329def32013-09-06 16:34:38 +0200139 * Note: function works backwards in data buffer
140 *
141 * \param p reference to current position pointer
142 * \param start start of the buffer (for bounds-checking)
143 * \param boolean 0 or 1
144 *
145 * \return the length written or a negative error code
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200148
149/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200151 * Note: function works backwards in data buffer
152 *
153 * \param p reference to current position pointer
154 * \param start start of the buffer (for bounds-checking)
155 * \param val the integer value
156 *
157 * \return the length written or a negative error code
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200160
161/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200163 * value in ASN.1 format
164 * 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 text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200169 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200170 *
171 * \return the length written or a negative error code
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200174 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200175
176/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200178 * value in ASN.1 format
179 * Note: function works backwards in data buffer
180 *
181 * \param p reference to current position pointer
182 * \param start start of the buffer (for bounds-checking)
183 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200184 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200185 *
186 * \return the length written or a negative error code
187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200189 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200190
191/**
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100192 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
193 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200194 *
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100195 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200196 *
Andres Amaya Garciae730ff62018-10-08 19:44:55 +0100197 * \param p The reference to the current position pointer.
198 * \param start The start of the buffer, for bounds-checking.
199 * \param buf The bitstring to write.
200 * \param bits The total number of bits in the bitstring.
201 *
202 * \return The number of bytes written to \p p on success.
203 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200206 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200207
208/**
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100209 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
210 * and value in ASN.1 format.
211 *
212 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200213 *
214 * \param p reference to current position pointer
215 * \param start start of the buffer (for bounds-checking)
216 * \param buf data buffer to write
217 * \param size length of the data buffer
218 *
219 * \return the length written or a negative error code
220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200222 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200223
224/**
225 * \brief Create or find a specific named_data entry for writing in a
226 * sequence or list based on the OID. If not already in there,
227 * a new entry is added to the head of the list.
228 * Warning: Destructive behaviour for the val data!
229 *
230 * \param list Pointer to the location of the head of the list to seek
231 * through (will be updated in case of a new entry)
232 * \param oid The OID to look for
233 * \param oid_len Size of the OID
234 * \param val Data to store (can be NULL if you want to fill it by hand)
235 * \param val_len Minimum length of the data buffer needed
236 *
237 * \return NULL if if there was a memory allocation error, or a pointer
238 * to the new / existing entry.
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200241 const char *oid, size_t oid_len,
242 const unsigned char *val,
243 size_t val_len );
244
Paul Bakker407a0da2013-06-27 14:29:21 +0200245#ifdef __cplusplus
246}
247#endif
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_ASN1_WRITE_H */