blob: 4d2917ee9ada877933875dc46ce4f64c40aa31ab [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 *
Paul Bakker7eb12432016-07-14 10:27:08 +010043 * \note lengths over 65535 are not supported at the moment
44 *
Paul Bakker7accbce2013-08-26 17:34:53 +020045 * \return the length written or a negative error code
46 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020048
49/**
50 * \brief Write a ASN.1 tag in ASN.1 format
51 * Note: function works backwards in data buffer
52 *
53 * \param p reference to current position pointer
54 * \param start start of the buffer (for bounds-checking)
55 * \param tag the tag to write
56 *
57 * \return the length written or a negative error code
58 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020060 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020061
Paul Bakker9852d002013-08-26 17:56:37 +020062/**
63 * \brief Write raw buffer data
64 * Note: function works backwards in data buffer
65 *
66 * \param p reference to current position pointer
67 * \param start start of the buffer (for bounds-checking)
68 * \param buf data buffer to write
69 * \param size length of the data buffer
70 *
71 * \return the length written or a negative error code
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +020074 const unsigned char *buf, size_t size );
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020077/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +020079 * Note: function works backwards in data buffer
80 *
81 * \param p reference to current position pointer
82 * \param start start of the buffer (for bounds-checking)
83 * \param X the MPI to write
84 *
85 * \return the length written or a negative error code
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
88#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +020089
90/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +020092 * Note: function works backwards in data buffer
93 *
94 * \param p reference to current position pointer
95 * \param start start of the buffer (for bounds-checking)
96 *
97 * \return the length written or a negative error code
98 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200100
101/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200103 * Note: function works backwards in data buffer
104 *
105 * \param p reference to current position pointer
106 * \param start start of the buffer (for bounds-checking)
107 * \param oid the OID to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200108 * \param oid_len length of the OID
Paul Bakker7accbce2013-08-26 17:34:53 +0200109 *
110 * \return the length written or a negative error code
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200113 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200114
115/**
116 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
117 * Note: function works backwards in data buffer
Paul Bakker7accbce2013-08-26 17:34:53 +0200118 *
119 * \param p reference to current position pointer
120 * \param start start of the buffer (for bounds-checking)
121 * \param oid the OID of the algorithm
Paul Bakker5f45e622013-09-09 12:02:36 +0200122 * \param oid_len length of the OID
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200123 * \param par_len length of parameters, which must be already written.
124 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200125 *
126 * \return the length written or a negative error code
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200129 const char *oid, size_t oid_len,
130 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200131
132/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
Paul Bakker329def32013-09-06 16:34:38 +0200134 * Note: function works backwards in data buffer
135 *
136 * \param p reference to current position pointer
137 * \param start start of the buffer (for bounds-checking)
138 * \param boolean 0 or 1
139 *
140 * \return the length written or a negative error code
141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200143
144/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
Paul Bakker7accbce2013-08-26 17:34:53 +0200146 * Note: function works backwards in data buffer
147 *
148 * \param p reference to current position pointer
149 * \param start start of the buffer (for bounds-checking)
150 * \param val the integer value
151 *
152 * \return the length written or a negative error code
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200155
156/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200158 * value in ASN.1 format
159 * Note: function works backwards in data buffer
160 *
161 * \param p reference to current position pointer
162 * \param start start of the buffer (for bounds-checking)
163 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200164 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200165 *
166 * \return the length written or a negative error code
167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200169 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200170
171/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
Paul Bakker7accbce2013-08-26 17:34:53 +0200173 * value in ASN.1 format
174 * Note: function works backwards in data buffer
175 *
176 * \param p reference to current position pointer
177 * \param start start of the buffer (for bounds-checking)
178 * \param text the text to write
Paul Bakker5f45e622013-09-09 12:02:36 +0200179 * \param text_len length of the text
Paul Bakker7accbce2013-08-26 17:34:53 +0200180 *
181 * \return the length written or a negative error code
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200184 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200185
186/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_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 buf the bitstring
194 * \param bits the total number of bits in the bitstring
195 *
196 * \return the length written or a negative error code
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200199 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200200
201/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_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 buf data buffer to write
209 * \param size length of the data buffer
210 *
211 * \return the length written or a negative error code
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200214 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200215
216/**
217 * \brief Create or find a specific named_data entry for writing in a
218 * sequence or list based on the OID. If not already in there,
219 * a new entry is added to the head of the list.
220 * Warning: Destructive behaviour for the val data!
221 *
222 * \param list Pointer to the location of the head of the list to seek
223 * through (will be updated in case of a new entry)
224 * \param oid The OID to look for
225 * \param oid_len Size of the OID
226 * \param val Data to store (can be NULL if you want to fill it by hand)
227 * \param val_len Minimum length of the data buffer needed
228 *
229 * \return NULL if if there was a memory allocation error, or a pointer
230 * to the new / existing entry.
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200233 const char *oid, size_t oid_len,
234 const unsigned char *val,
235 size_t val_len );
236
Paul Bakker407a0da2013-06-27 14:29:21 +0200237#ifdef __cplusplus
238}
239#endif
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#endif /* MBEDTLS_ASN1_WRITE_H */