blob: 73ff32b669901ce1ef581218e0749ee9542ca3d7 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/**
2 * \file asn1write.h
3 *
4 * \brief ASN.1 buffer writing functionality
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ASN1_WRITE_H
24#define MBEDTLS_ASN1_WRITE_H
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025
26#include "asn1.h"
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020029 g += ret; } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000030
Paul Bakker407a0da2013-06-27 14:29:21 +020031#ifdef __cplusplus
32extern "C" {
33#endif
34
Paul Bakker7accbce2013-08-26 17:34:53 +020035/**
36 * \brief Write a length field in ASN.1 format
37 * Note: function works backwards in data buffer
38 *
39 * \param p reference to current position pointer
40 * \param start start of the buffer (for bounds-checking)
41 * \param len the length to write
42 *
43 * \return the length written or a negative error code
44 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020046
47/**
48 * \brief Write a ASN.1 tag in ASN.1 format
49 * Note: function works backwards in data buffer
50 *
51 * \param p reference to current position pointer
52 * \param start start of the buffer (for bounds-checking)
53 * \param tag the tag to write
54 *
55 * \return the length written or a negative error code
56 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020058 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020059
Paul Bakker9852d002013-08-26 17:56:37 +020060/**
61 * \brief Write raw buffer data
62 * Note: function works backwards in data buffer
63 *
64 * \param p reference to current position pointer
65 * \param start start of the buffer (for bounds-checking)
66 * \param buf data buffer to write
67 * \param size length of the data buffer
68 *
69 * \return the length written or a negative error code
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +020072 const unsigned char *buf, size_t size );
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020075/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +020077 * Note: function works backwards in data buffer
78 *
79 * \param p reference to current position pointer
80 * \param start start of the buffer (for bounds-checking)
81 * \param X the MPI to write
82 *
83 * \return the length written or a negative error code
84 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
86#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +020087
88/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +020090 * Note: function works backwards in data buffer
91 *
92 * \param p reference to current position pointer
93 * \param start start of the buffer (for bounds-checking)
94 *
95 * \return the length written or a negative error code
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +020098
99/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200101 * Note: function works backwards in data buffer
102 *
103 * \param p reference to current position pointer
104 * \param start start of the buffer (for bounds-checking)
105 * \param oid the OID to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200106 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200107 *
108 * \return the length written or a negative error code
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200111 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200112
113/**
114 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
115 * Note: function works backwards in data buffer
Paul Bakker7accbce2013-08-26 17:34:53 +0200116 *
117 * \param p reference to current position pointer
118 * \param start start of the buffer (for bounds-checking)
119 * \param oid the OID of the algorithm
Paul Bakker5f45e622013-09-09 12:02:36 +0200120 * \param oid_len length of the OID
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200121 * \param par_len length of parameters, which must be already written.
122 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200123 *
124 * \return the length written or a negative error code
125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200127 const char *oid, size_t oid_len,
128 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200129
130/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
Paul Bakker329def32013-09-06 16:34:38 +0200132 * Note: function works backwards in data buffer
133 *
134 * \param p reference to current position pointer
135 * \param start start of the buffer (for bounds-checking)
136 * \param boolean 0 or 1
137 *
138 * \return the length written or a negative error code
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200141
142/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200144 * Note: function works backwards in data buffer
145 *
146 * \param p reference to current position pointer
147 * \param start start of the buffer (for bounds-checking)
148 * \param val the integer value
149 *
150 * \return the length written or a negative error code
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200153
154/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200156 * value in ASN.1 format
157 * Note: function works backwards in data buffer
158 *
159 * \param p reference to current position pointer
160 * \param start start of the buffer (for bounds-checking)
161 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200162 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200163 *
164 * \return the length written or a negative error code
165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200167 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200168
169/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200171 * value in ASN.1 format
172 * Note: function works backwards in data buffer
173 *
174 * \param p reference to current position pointer
175 * \param start start of the buffer (for bounds-checking)
176 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200177 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200178 *
179 * \return the length written or a negative error code
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200182 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200183
184/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200186 * value in ASN.1 format
187 * Note: function works backwards in data buffer
188 *
189 * \param p reference to current position pointer
190 * \param start start of the buffer (for bounds-checking)
191 * \param buf the bitstring
192 * \param bits the total number of bits in the bitstring
193 *
194 * \return the length written or a negative error code
195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200197 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200198
199/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200201 * value in ASN.1 format
202 * Note: function works backwards in data buffer
203 *
204 * \param p reference to current position pointer
205 * \param start start of the buffer (for bounds-checking)
206 * \param buf data buffer to write
207 * \param size length of the data buffer
208 *
209 * \return the length written or a negative error code
210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200212 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200213
214/**
215 * \brief Create or find a specific named_data entry for writing in a
216 * sequence or list based on the OID. If not already in there,
217 * a new entry is added to the head of the list.
218 * Warning: Destructive behaviour for the val data!
219 *
220 * \param list Pointer to the location of the head of the list to seek
221 * through (will be updated in case of a new entry)
222 * \param oid The OID to look for
223 * \param oid_len Size of the OID
224 * \param val Data to store (can be NULL if you want to fill it by hand)
225 * \param val_len Minimum length of the data buffer needed
226 *
227 * \return NULL if if there was a memory allocation error, or a pointer
228 * to the new / existing entry.
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200231 const char *oid, size_t oid_len,
232 const unsigned char *val,
233 size_t val_len );
234
Paul Bakker407a0da2013-06-27 14:29:21 +0200235#ifdef __cplusplus
236}
237#endif
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_ASN1_WRITE_H */