blob: 04a8767c8e2652439698851c11164f6874844f3d [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 Eldor8b0cf2e2018-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
Hanno Becker55177552018-10-24 12:29:53 +010035#define MBEDTLS_ASN1_CHK_ADD(g, f) \
36 do { \
37 if( ( ret = f ) < 0 ) \
38 return( ret ); \
39 else \
40 g += ret; \
41 } while( 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Paul Bakker7accbce2013-08-26 17:34:53 +020047/**
Hanno Becker55177552018-10-24 12:29:53 +010048 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020049 *
Hanno Becker55177552018-10-24 12:29:53 +010050 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020051 *
Hanno Becker55177552018-10-24 12:29:53 +010052 * \param p The reference to the current position pointer.
53 * \param start The start of the buffer, for bounds-checking.
54 * \param len The length value to write.
55 *
56 * \return The number of bytes written to \p p on success.
57 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020058 */
Hanno Becker55177552018-10-24 12:29:53 +010059int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,
60 size_t len );
Paul Bakker7accbce2013-08-26 17:34:53 +020061/**
Hanno Becker55177552018-10-24 12:29:53 +010062 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020063 *
Hanno Becker55177552018-10-24 12:29:53 +010064 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020065 *
Hanno Becker55177552018-10-24 12:29:53 +010066 * \param p The reference to the current position pointer.
67 * \param start The start of the buffer, for bounds-checking.
68 * \param tag The tag to write.
69 *
70 * \return The number of bytes written to \p p on success.
71 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +010074 unsigned char tag );
Paul Bakker7accbce2013-08-26 17:34:53 +020075
Paul Bakker9852d002013-08-26 17:56:37 +020076/**
Hanno Becker55177552018-10-24 12:29:53 +010077 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020078 *
Hanno Becker55177552018-10-24 12:29:53 +010079 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020080 *
Hanno Becker55177552018-10-24 12:29:53 +010081 * \param p The reference to the current position pointer.
82 * \param start The start of the buffer, for bounds-checking.
83 * \param buf The data buffer to write.
84 * \param size The length of the data buffer.
85 *
86 * \return The number of bytes written to \p p on success.
87 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +020088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +010090 const unsigned char *buf, size_t size );
Paul Bakker9852d002013-08-26 17:56:37 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020093/**
Hanno Becker55177552018-10-24 12:29:53 +010094 * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
95 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020096 *
Hanno Becker55177552018-10-24 12:29:53 +010097 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020098 *
Hanno Becker55177552018-10-24 12:29:53 +010099 * \param p The reference to the current position pointer.
100 * \param start The start of the buffer, for bounds-checking.
101 * \param X The MPI to write.
102 *
103 * \return The number of bytes written to \p p on success.
104 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200105 */
Hanno Becker55177552018-10-24 12:29:53 +0100106int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,
107 const mbedtls_mpi *X );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200109
110/**
Hanno Becker55177552018-10-24 12:29:53 +0100111 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
112 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200113 *
Hanno Becker55177552018-10-24 12:29:53 +0100114 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200115 *
Hanno Becker55177552018-10-24 12:29:53 +0100116 * \param p The reference to the current position pointer.
117 * \param start The start of the buffer, for bounds-checking.
118 *
119 * \return The number of bytes written to \p p on success.
120 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
Paul Bakker7accbce2013-08-26 17:34:53 +0200123
124/**
Hanno Becker55177552018-10-24 12:29:53 +0100125 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
126 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200127 *
Hanno Becker55177552018-10-24 12:29:53 +0100128 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200129 *
Hanno Becker55177552018-10-24 12:29:53 +0100130 * \param p The reference to the current position pointer.
131 * \param start The start of the buffer, for bounds-checking.
132 * \param oid The OID to write.
133 * \param oid_len The length of the OID.
134 *
135 * \return The number of bytes written to \p p on success.
136 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100139 const char *oid, size_t oid_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200140
141/**
Hanno Becker55177552018-10-24 12:29:53 +0100142 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200143 *
Hanno Becker55177552018-10-24 12:29:53 +0100144 * \note This function works backwards in data buffer.
145 *
146 * \param p The reference to the current position pointer.
147 * \param start The start of the buffer, for bounds-checking.
148 * \param oid The OID of the algorithm to write.
149 * \param oid_len The length of the algorithm's OID.
150 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200151 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200152 *
Hanno Becker55177552018-10-24 12:29:53 +0100153 * \return The number of bytes written to \p p on success.
154 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200155 */
Hanno Becker55177552018-10-24 12:29:53 +0100156int mbedtls_asn1_write_algorithm_identifier( unsigned char **p,
157 unsigned char *start,
158 const char *oid, size_t oid_len,
159 size_t par_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200160
161/**
Hanno Becker55177552018-10-24 12:29:53 +0100162 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
163 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200164 *
Hanno Becker55177552018-10-24 12:29:53 +0100165 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200166 *
Hanno Becker55177552018-10-24 12:29:53 +0100167 * \param p The reference to the current position pointer.
168 * \param start The start of the buffer, for bounds-checking.
169 * \param boolean The boolean value to write, either \c 0 or \c 1.
170 *
171 * \return The number of bytes written to \p p on success.
172 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200173 */
Hanno Becker55177552018-10-24 12:29:53 +0100174int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
175 int boolean );
Paul Bakker329def32013-09-06 16:34:38 +0200176
177/**
Hanno Becker55177552018-10-24 12:29:53 +0100178 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
179 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200180 *
Hanno Becker55177552018-10-24 12:29:53 +0100181 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200182 *
Hanno Becker55177552018-10-24 12:29:53 +0100183 * \param p The reference to the current position pointer.
184 * \param start The start of the buffer, for bounds-checking.
185 * \param val The integer value to write.
186 *
187 * \return The number of bytes written to \p p on success.
188 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
Paul Bakker7accbce2013-08-26 17:34:53 +0200191
192/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100193 * \brief Write a string in ASN.1 format using a specific
194 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100195
196 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100197 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100198 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100199 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100200 * \param tag The string encoding tag to write, e.g.
201 * #MBEDTLS_ASN1_UTF8_STRING.
202 * \param text The string to write.
203 * \param text_len The length of \p text in bytes (which might
204 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100205 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100206 * \return The number of bytes written to \p p on success.
207 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100208 */
thomas-deeeba6c9b2018-09-19 09:10:37 +0200209int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100210 int tag, const char *text,
211 size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200212
213/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100214 * \brief Write a string in ASN.1 format using the PrintableString
215 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100216 *
217 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100218 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100219 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100220 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100221 * \param text The string to write.
222 * \param text_len The length of \p text in bytes (which might
223 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100224 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100225 * \return The number of bytes written to \p p on success.
226 * \return A negative error code on failure.
227 */
228int mbedtls_asn1_write_printable_string( unsigned char **p,
229 unsigned char *start,
230 const char *text, size_t text_len );
231
232/**
233 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
234 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100235 *
236 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100237 *
238 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100239 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100240 * \param text The string to write.
241 * \param text_len The length of \p text in bytes (which might
242 * be strictly larger than the number of characters).
243 *
244 * \return The number of bytes written to \p p on success.
245 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100246 */
247int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100248 const char *text, size_t text_len );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100249
250/**
Hanno Becker55177552018-10-24 12:29:53 +0100251 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100252 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100253 *
254 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200255 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100256 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100257 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100258 * \param text The string to write.
259 * \param text_len The length of \p text in bytes (which might
260 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200261 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100262 * \return The number of bytes written to \p p on success.
263 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100266 const char *text, size_t text_len );
Paul Bakker7accbce2013-08-26 17:34:53 +0200267
268/**
Hanno Becker55177552018-10-24 12:29:53 +0100269 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
270 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200271 *
Hanno Becker55177552018-10-24 12:29:53 +0100272 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200273 *
Hanno Becker55177552018-10-24 12:29:53 +0100274 * \param p The reference to the current position pointer.
275 * \param start The start of the buffer, for bounds-checking.
276 * \param buf The bitstring to write.
277 * \param bits The total number of bits in the bitstring.
278 *
279 * \return The number of bytes written to \p p on success.
280 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100283 const unsigned char *buf, size_t bits );
Paul Bakker7accbce2013-08-26 17:34:53 +0200284
285/**
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100286 * \brief Write a named bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
287 * value in ASN.1 format
288 * Note: function works backwards in data buffer
289 *
290 * As stated in RFC5280 Appending B, trailing zeroes are
291 * omitted when encoding named bitstrings in DER.
292 *
293 * \param p Reference to current position pointer.
294 * \param start Start of the buffer (for bounds-checking).
295 * \param buf The bitstring.
296 * \param bits The total number of bits in the bitstring.
297 *
298 * \return The length written or a negative error code.
299 */
300int mbedtls_asn1_write_named_bitstring( unsigned char **p,
301 unsigned char *start,
302 const unsigned char *buf,
303 size_t bits );
304
305/**
Hanno Becker55177552018-10-24 12:29:53 +0100306 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
307 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200308 *
Hanno Becker55177552018-10-24 12:29:53 +0100309 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200310 *
Hanno Becker55177552018-10-24 12:29:53 +0100311 * \param p The reference to the current position pointer.
312 * \param start The start of the buffer, for bounds-checking.
313 * \param buf The buffer holding the data to write.
314 * \param size The length of the data buffer \p buf.
315 *
316 * \return The number of bytes written to \p p on success.
317 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Hanno Becker55177552018-10-24 12:29:53 +0100320 const unsigned char *buf, size_t size );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200321
322/**
323 * \brief Create or find a specific named_data entry for writing in a
324 * sequence or list based on the OID. If not already in there,
325 * a new entry is added to the head of the list.
326 * Warning: Destructive behaviour for the val data!
327 *
Hanno Becker55177552018-10-24 12:29:53 +0100328 * \param list The pointer to the location of the head of the list to seek
329 * through (will be updated in case of a new entry).
330 * \param oid The OID to look for.
331 * \param oid_len The size of the OID.
332 * \param val The data to store (can be \c NULL if you want to fill
333 * it by hand).
334 * \param val_len The minimum length of the data buffer needed.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200335 *
Hanno Becker55177552018-10-24 12:29:53 +0100336 * \return A pointer to the new / existing entry on success.
337 * \return \c NULL if if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200340 const char *oid, size_t oid_len,
341 const unsigned char *val,
342 size_t val_len );
343
Paul Bakker407a0da2013-06-27 14:29:21 +0200344#ifdef __cplusplus
345}
346#endif
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_ASN1_WRITE_H */