blob: 7af4aba41f94a54735471241211cfb15fe3753ac [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úti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerbdb912d2012-02-13 23:11:30 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_ASN1_WRITE_H
11#define MBEDTLS_ASN1_WRITE_H
Paul Bakkerbdb912d2012-02-13 23:11:30 +000012
Bence Szépkútic662b362021-05-27 11:25:03 +020013#include "mbedtls/build_info.h"
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020014
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010015#include "mbedtls/asn1.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000016
Hanno Becker55177552018-10-24 12:29:53 +010017#define MBEDTLS_ASN1_CHK_ADD(g, f) \
Hanno Becker1eeca412018-10-15 12:01:35 +010018 do \
19 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010020 if ((ret = (f)) < 0) \
21 return ret; \
Hanno Becker55177552018-10-24 12:29:53 +010022 else \
Gilles Peskine449bd832023-01-11 14:50:10 +010023 (g) += ret; \
24 } while (0)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025
Przemek Stekiel57207712023-02-24 14:03:30 +010026#define MBEDTLS_ASN1_CHK_CLEANUP_ADD(g, f) \
27 do \
28 { \
29 if ((ret = (f)) < 0) \
30 goto cleanup; \
31 else \
32 (g) += ret; \
33 } while (0)
34
Paul Bakker407a0da2013-06-27 14:29:21 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010039#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020040/**
Hanno Becker55177552018-10-24 12:29:53 +010041 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020042 *
Hanno Becker55177552018-10-24 12:29:53 +010043 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020044 *
Hanno Becker55177552018-10-24 12:29:53 +010045 * \param p The reference to the current position pointer.
46 * \param start The start of the buffer, for bounds-checking.
47 * \param len The length value to write.
48 *
49 * \return The number of bytes written to \p p on success.
50 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020051 */
Gilles Peskine449bd832023-01-11 14:50:10 +010052int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start,
53 size_t len);
Paul Bakker7accbce2013-08-26 17:34:53 +020054/**
Hanno Becker55177552018-10-24 12:29:53 +010055 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020056 *
Hanno Becker55177552018-10-24 12:29:53 +010057 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020058 *
Hanno Becker55177552018-10-24 12:29:53 +010059 * \param p The reference to the current position pointer.
60 * \param start The start of the buffer, for bounds-checking.
61 * \param tag The tag to write.
62 *
63 * \return The number of bytes written to \p p on success.
64 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020065 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start,
67 unsigned char tag);
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010068#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */
Paul Bakker7accbce2013-08-26 17:34:53 +020069
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010070#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakker9852d002013-08-26 17:56:37 +020071/**
Hanno Becker55177552018-10-24 12:29:53 +010072 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020073 *
Hanno Becker55177552018-10-24 12:29:53 +010074 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020075 *
Hanno Becker55177552018-10-24 12:29:53 +010076 * \param p The reference to the current position pointer.
77 * \param start The start of the buffer, for bounds-checking.
78 * \param buf The data buffer to write.
79 * \param size The length of the data buffer.
80 *
81 * \return The number of bytes written to \p p on success.
82 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +020083 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
85 const unsigned char *buf, size_t size);
Paul Bakker9852d002013-08-26 17:56:37 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020088/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010089 * \brief Write an arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
Hanno Becker55177552018-10-24 12:29:53 +010090 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020091 *
Hanno Becker55177552018-10-24 12:29:53 +010092 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020093 *
Hanno Becker55177552018-10-24 12:29:53 +010094 * \param p The reference to the current position pointer.
95 * \param start The start of the buffer, for bounds-checking.
96 * \param X The MPI to write.
Gilles Peskine105031b2019-03-01 19:28:41 +010097 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +010098 *
99 * \return The number of bytes written to \p p on success.
100 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start,
103 const mbedtls_mpi *X);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200105
106/**
Hanno Becker55177552018-10-24 12:29:53 +0100107 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
108 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200109 *
Hanno Becker55177552018-10-24 12:29:53 +0100110 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200111 *
Hanno Becker55177552018-10-24 12:29:53 +0100112 * \param p The reference to the current position pointer.
113 * \param start The start of the buffer, for bounds-checking.
114 *
115 * \return The number of bytes written to \p p on success.
116 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start);
Paul Bakker7accbce2013-08-26 17:34:53 +0200119
120/**
Hanno Becker55177552018-10-24 12:29:53 +0100121 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
122 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200123 *
Hanno Becker55177552018-10-24 12:29:53 +0100124 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200125 *
Hanno Becker55177552018-10-24 12:29:53 +0100126 * \param p The reference to the current position pointer.
127 * \param start The start of the buffer, for bounds-checking.
128 * \param oid The OID to write.
129 * \param oid_len The length of the OID.
130 *
131 * \return The number of bytes written to \p p on success.
132 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
135 const char *oid, size_t oid_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200136
137/**
Hanno Becker55177552018-10-24 12:29:53 +0100138 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200139 *
Hanno Becker55177552018-10-24 12:29:53 +0100140 * \note This function works backwards in data buffer.
141 *
142 * \param p The reference to the current position pointer.
143 * \param start The start of the buffer, for bounds-checking.
144 * \param oid The OID of the algorithm to write.
145 * \param oid_len The length of the algorithm's OID.
146 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200147 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200148 *
Hanno Becker55177552018-10-24 12:29:53 +0100149 * \return The number of bytes written to \p p on success.
150 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200151 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152int mbedtls_asn1_write_algorithm_identifier(unsigned char **p,
153 const unsigned char *start,
154 const char *oid, size_t oid_len,
155 size_t par_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200156
157/**
Jethro Beekman01672442023-04-19 14:08:14 +0200158 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
159 *
160 * \note This function works backwards in data buffer.
161 *
162 * \param p The reference to the current position pointer.
163 * \param start The start of the buffer, for bounds-checking.
164 * \param oid The OID of the algorithm to write.
165 * \param oid_len The length of the algorithm's OID.
166 * \param par_len The length of the parameters, which must be already written.
167 * \param has_par If there are any parameters. If 0, par_len must be 0. If 1
168 * and \p par_len is 0, NULL parameters are added.
169 *
170 * \return The number of bytes written to \p p on success.
171 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
172 */
173int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p,
174 const unsigned char *start,
175 const char *oid, size_t oid_len,
176 size_t par_len, int has_par);
177
178/**
Hanno Becker55177552018-10-24 12:29:53 +0100179 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
180 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200181 *
Hanno Becker55177552018-10-24 12:29:53 +0100182 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200183 *
Hanno Becker55177552018-10-24 12:29:53 +0100184 * \param p The reference to the current position pointer.
185 * \param start The start of the buffer, for bounds-checking.
186 * \param boolean The boolean value to write, either \c 0 or \c 1.
187 *
188 * \return The number of bytes written to \p p on success.
189 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start,
192 int boolean);
Paul Bakker329def32013-09-06 16:34:38 +0200193
194/**
Hanno Becker55177552018-10-24 12:29:53 +0100195 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
196 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200197 *
Hanno Becker55177552018-10-24 12:29:53 +0100198 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200199 *
Hanno Becker55177552018-10-24 12:29:53 +0100200 * \param p The reference to the current position pointer.
201 * \param start The start of the buffer, for bounds-checking.
202 * \param val The integer value to write.
Gilles Peskine105031b2019-03-01 19:28:41 +0100203 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +0100204 *
205 * \return The number of bytes written to \p p on success.
206 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200207 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val);
Paul Bakker7accbce2013-08-26 17:34:53 +0200209
210/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200211 * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
212 * in ASN.1 format.
213 *
214 * \note This function works backwards in data buffer.
215 *
216 * \param p The reference to the current position pointer.
217 * \param start The start of the buffer, for bounds-checking.
218 * \param val The integer value to write.
219 *
220 * \return The number of bytes written to \p p on success.
221 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200224
225/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100226 * \brief Write a string in ASN.1 format using a specific
227 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100228
229 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100230 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100231 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100232 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100233 * \param tag The string encoding tag to write, e.g.
234 * #MBEDTLS_ASN1_UTF8_STRING.
235 * \param text The string to write.
236 * \param text_len The length of \p text in bytes (which might
237 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100238 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100239 * \return The number of bytes written to \p p on success.
240 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start,
243 int tag, const char *text,
244 size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200245
246/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100247 * \brief Write a string in ASN.1 format using the PrintableString
248 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100249 *
250 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100251 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100252 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100253 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100254 * \param text The string to write.
255 * \param text_len The length of \p text in bytes (which might
256 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100257 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100258 * \return The number of bytes written to \p p on success.
259 * \return A negative error code on failure.
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int mbedtls_asn1_write_printable_string(unsigned char **p,
262 const unsigned char *start,
263 const char *text, size_t text_len);
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100264
265/**
266 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
Gilles Peskine105031b2019-03-01 19:28:41 +0100267 * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100268 *
269 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100270 *
271 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100272 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100273 * \param text The string to write.
274 * \param text_len The length of \p text in bytes (which might
275 * be strictly larger than the number of characters).
276 *
277 * \return The number of bytes written to \p p on success.
278 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
281 const char *text, size_t text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100282
283/**
Hanno Becker55177552018-10-24 12:29:53 +0100284 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100285 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100286 *
287 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200288 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100289 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100290 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100291 * \param text The string to write.
292 * \param text_len The length of \p text in bytes (which might
293 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200294 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100295 * \return The number of bytes written to \p p on success.
296 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200297 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
299 const char *text, size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200300
301/**
Hanno Becker55177552018-10-24 12:29:53 +0100302 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
303 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200304 *
Hanno Becker55177552018-10-24 12:29:53 +0100305 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200306 *
Hanno Becker55177552018-10-24 12:29:53 +0100307 * \param p The reference to the current position pointer.
308 * \param start The start of the buffer, for bounds-checking.
309 * \param buf The bitstring to write.
310 * \param bits The total number of bits in the bitstring.
311 *
312 * \return The number of bytes written to \p p on success.
313 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200314 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
316 const unsigned char *buf, size_t bits);
Paul Bakker7accbce2013-08-26 17:34:53 +0200317
318/**
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100319 * \brief This function writes a named bitstring tag
320 * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100321 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100322 * As stated in RFC 5280 Appendix B, trailing zeroes are
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100323 * omitted when encoding named bitstrings in DER.
324 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100325 * \note This function works backwards within the data buffer.
326 *
327 * \param p The reference to the current position pointer.
328 * \param start The start of the buffer which is used for bounds-checking.
329 * \param buf The bitstring to write.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100330 * \param bits The total number of bits in the bitstring.
331 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100332 * \return The number of bytes written to \p p on success.
333 * \return A negative error code on failure.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100334 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335int mbedtls_asn1_write_named_bitstring(unsigned char **p,
336 const unsigned char *start,
337 const unsigned char *buf,
338 size_t bits);
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100339
340/**
Hanno Becker55177552018-10-24 12:29:53 +0100341 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
342 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200343 *
Hanno Becker55177552018-10-24 12:29:53 +0100344 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200345 *
Hanno Becker55177552018-10-24 12:29:53 +0100346 * \param p The reference to the current position pointer.
347 * \param start The start of the buffer, for bounds-checking.
348 * \param buf The buffer holding the data to write.
349 * \param size The length of the data buffer \p buf.
350 *
351 * \return The number of bytes written to \p p on success.
352 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
355 const unsigned char *buf, size_t size);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200356
357/**
358 * \brief Create or find a specific named_data entry for writing in a
359 * sequence or list based on the OID. If not already in there,
360 * a new entry is added to the head of the list.
361 * Warning: Destructive behaviour for the val data!
362 *
Hanno Becker55177552018-10-24 12:29:53 +0100363 * \param list The pointer to the location of the head of the list to seek
364 * through (will be updated in case of a new entry).
365 * \param oid The OID to look for.
366 * \param oid_len The size of the OID.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100367 * \param val The associated data to store. If this is \c NULL,
368 * no data is copied to the new or existing buffer.
Hanno Becker55177552018-10-24 12:29:53 +0100369 * \param val_len The minimum length of the data buffer needed.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100370 * If this is 0, do not allocate a buffer for the associated
371 * data.
372 * If the OID was already present, enlarge, shrink or free
373 * the existing buffer to fit \p val_len.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200374 *
Hanno Becker55177552018-10-24 12:29:53 +0100375 * \return A pointer to the new / existing entry on success.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000376 * \return \c NULL if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200377 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list,
379 const char *oid, size_t oid_len,
380 const unsigned char *val,
381 size_t val_len);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200382
Paul Bakker407a0da2013-06-27 14:29:21 +0200383#ifdef __cplusplus
384}
385#endif
386
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +0100387#endif /* MBEDTLS_ASN1_WRITE_C */
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_ASN1_WRITE_H */