blob: 0c5a85ac27f99137b5820ef1a28901a7276ef3cb [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
Valerio Setti688f7952024-01-16 09:18:40 +010039#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C) || \
40 defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
Paul Bakker7accbce2013-08-26 17:34:53 +020041/**
Hanno Becker55177552018-10-24 12:29:53 +010042 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020043 *
Hanno Becker55177552018-10-24 12:29:53 +010044 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020045 *
Hanno Becker55177552018-10-24 12:29:53 +010046 * \param p The reference to the current position pointer.
47 * \param start The start of the buffer, for bounds-checking.
48 * \param len The length value to write.
49 *
50 * \return The number of bytes written to \p p on success.
51 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020052 */
Gilles Peskine449bd832023-01-11 14:50:10 +010053int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start,
54 size_t len);
Paul Bakker7accbce2013-08-26 17:34:53 +020055/**
Hanno Becker55177552018-10-24 12:29:53 +010056 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020057 *
Hanno Becker55177552018-10-24 12:29:53 +010058 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020059 *
Hanno Becker55177552018-10-24 12:29:53 +010060 * \param p The reference to the current position pointer.
61 * \param start The start of the buffer, for bounds-checking.
62 * \param tag The tag to write.
63 *
64 * \return The number of bytes written to \p p on success.
65 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020066 */
Gilles Peskine449bd832023-01-11 14:50:10 +010067int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start,
68 unsigned char tag);
Valerio Setti688f7952024-01-16 09:18:40 +010069#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C || MBEDTLS_PSA_UTIL_HAVE_ECDSA*/
Paul Bakker7accbce2013-08-26 17:34:53 +020070
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010071#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakker9852d002013-08-26 17:56:37 +020072/**
Hanno Becker55177552018-10-24 12:29:53 +010073 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020074 *
Hanno Becker55177552018-10-24 12:29:53 +010075 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020076 *
Hanno Becker55177552018-10-24 12:29:53 +010077 * \param p The reference to the current position pointer.
78 * \param start The start of the buffer, for bounds-checking.
79 * \param buf The data buffer to write.
80 * \param size The length of the data buffer.
81 *
82 * \return The number of bytes written to \p p on success.
83 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +020084 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
86 const unsigned char *buf, size_t size);
Paul Bakker9852d002013-08-26 17:56:37 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020089/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010090 * \brief Write an arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
Hanno Becker55177552018-10-24 12:29:53 +010091 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020092 *
Hanno Becker55177552018-10-24 12:29:53 +010093 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020094 *
Hanno Becker55177552018-10-24 12:29:53 +010095 * \param p The reference to the current position pointer.
96 * \param start The start of the buffer, for bounds-checking.
97 * \param X The MPI to write.
Gilles Peskine105031b2019-03-01 19:28:41 +010098 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +010099 *
100 * \return The number of bytes written to \p p on success.
101 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start,
104 const mbedtls_mpi *X);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200106
107/**
Hanno Becker55177552018-10-24 12:29:53 +0100108 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
109 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200110 *
Hanno Becker55177552018-10-24 12:29:53 +0100111 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200112 *
Hanno Becker55177552018-10-24 12:29:53 +0100113 * \param p The reference to the current position pointer.
114 * \param start The start of the buffer, for bounds-checking.
115 *
116 * \return The number of bytes written to \p p on success.
117 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start);
Paul Bakker7accbce2013-08-26 17:34:53 +0200120
121/**
Hanno Becker55177552018-10-24 12:29:53 +0100122 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
123 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200124 *
Hanno Becker55177552018-10-24 12:29:53 +0100125 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200126 *
Hanno Becker55177552018-10-24 12:29:53 +0100127 * \param p The reference to the current position pointer.
128 * \param start The start of the buffer, for bounds-checking.
129 * \param oid The OID to write.
130 * \param oid_len The length of the OID.
131 *
132 * \return The number of bytes written to \p p on success.
133 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
136 const char *oid, size_t oid_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200137
138/**
Hanno Becker55177552018-10-24 12:29:53 +0100139 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200140 *
Hanno Becker55177552018-10-24 12:29:53 +0100141 * \note This function works backwards in data buffer.
142 *
143 * \param p The reference to the current position pointer.
144 * \param start The start of the buffer, for bounds-checking.
145 * \param oid The OID of the algorithm to write.
146 * \param oid_len The length of the algorithm's OID.
147 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200148 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200149 *
Hanno Becker55177552018-10-24 12:29:53 +0100150 * \return The number of bytes written to \p p on success.
151 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153int mbedtls_asn1_write_algorithm_identifier(unsigned char **p,
154 const unsigned char *start,
155 const char *oid, size_t oid_len,
156 size_t par_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200157
158/**
Jethro Beekman01672442023-04-19 14:08:14 +0200159 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
160 *
161 * \note This function works backwards in data buffer.
162 *
163 * \param p The reference to the current position pointer.
164 * \param start The start of the buffer, for bounds-checking.
165 * \param oid The OID of the algorithm to write.
166 * \param oid_len The length of the algorithm's OID.
167 * \param par_len The length of the parameters, which must be already written.
168 * \param has_par If there are any parameters. If 0, par_len must be 0. If 1
169 * and \p par_len is 0, NULL parameters are added.
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.
173 */
174int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p,
175 const unsigned char *start,
176 const char *oid, size_t oid_len,
177 size_t par_len, int has_par);
178
179/**
Hanno Becker55177552018-10-24 12:29:53 +0100180 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
181 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200182 *
Hanno Becker55177552018-10-24 12:29:53 +0100183 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200184 *
Hanno Becker55177552018-10-24 12:29:53 +0100185 * \param p The reference to the current position pointer.
186 * \param start The start of the buffer, for bounds-checking.
187 * \param boolean The boolean value to write, either \c 0 or \c 1.
188 *
189 * \return The number of bytes written to \p p on success.
190 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start,
193 int boolean);
Paul Bakker329def32013-09-06 16:34:38 +0200194
195/**
Hanno Becker55177552018-10-24 12:29:53 +0100196 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
197 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200198 *
Hanno Becker55177552018-10-24 12:29:53 +0100199 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200200 *
Hanno Becker55177552018-10-24 12:29:53 +0100201 * \param p The reference to the current position pointer.
202 * \param start The start of the buffer, for bounds-checking.
203 * \param val The integer value to write.
Gilles Peskine105031b2019-03-01 19:28:41 +0100204 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +0100205 *
206 * \return The number of bytes written to \p p on success.
207 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val);
Paul Bakker7accbce2013-08-26 17:34:53 +0200210
211/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200212 * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
213 * in ASN.1 format.
214 *
215 * \note This function works backwards in data buffer.
216 *
217 * \param p The reference to the current position pointer.
218 * \param start The start of the buffer, for bounds-checking.
219 * \param val The integer value to write.
220 *
221 * \return The number of bytes written to \p p on success.
222 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200225
226/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100227 * \brief Write a string in ASN.1 format using a specific
228 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100229
230 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100231 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100232 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100233 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100234 * \param tag The string encoding tag to write, e.g.
235 * #MBEDTLS_ASN1_UTF8_STRING.
236 * \param text The string to write.
237 * \param text_len The length of \p text in bytes (which might
238 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100239 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100240 * \return The number of bytes written to \p p on success.
241 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start,
244 int tag, const char *text,
245 size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200246
247/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100248 * \brief Write a string in ASN.1 format using the PrintableString
249 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100250 *
251 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100252 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100253 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100254 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100255 * \param text The string to write.
256 * \param text_len The length of \p text in bytes (which might
257 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100258 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100259 * \return The number of bytes written to \p p on success.
260 * \return A negative error code on failure.
261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int mbedtls_asn1_write_printable_string(unsigned char **p,
263 const unsigned char *start,
264 const char *text, size_t text_len);
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100265
266/**
267 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
Gilles Peskine105031b2019-03-01 19:28:41 +0100268 * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100269 *
270 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100271 *
272 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100273 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100274 * \param text The string to write.
275 * \param text_len The length of \p text in bytes (which might
276 * be strictly larger than the number of characters).
277 *
278 * \return The number of bytes written to \p p on success.
279 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
282 const char *text, size_t text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100283
284/**
Hanno Becker55177552018-10-24 12:29:53 +0100285 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100286 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100287 *
288 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200289 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100290 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100291 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100292 * \param text The string to write.
293 * \param text_len The length of \p text in bytes (which might
294 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200295 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100296 * \return The number of bytes written to \p p on success.
297 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200298 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
300 const char *text, size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200301
302/**
Hanno Becker55177552018-10-24 12:29:53 +0100303 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
304 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200305 *
Hanno Becker55177552018-10-24 12:29:53 +0100306 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200307 *
Hanno Becker55177552018-10-24 12:29:53 +0100308 * \param p The reference to the current position pointer.
309 * \param start The start of the buffer, for bounds-checking.
310 * \param buf The bitstring to write.
311 * \param bits The total number of bits in the bitstring.
312 *
313 * \return The number of bytes written to \p p on success.
314 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200315 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
317 const unsigned char *buf, size_t bits);
Paul Bakker7accbce2013-08-26 17:34:53 +0200318
319/**
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100320 * \brief This function writes a named bitstring tag
321 * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100322 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100323 * As stated in RFC 5280 Appendix B, trailing zeroes are
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100324 * omitted when encoding named bitstrings in DER.
325 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100326 * \note This function works backwards within the data buffer.
327 *
328 * \param p The reference to the current position pointer.
329 * \param start The start of the buffer which is used for bounds-checking.
330 * \param buf The bitstring to write.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100331 * \param bits The total number of bits in the bitstring.
332 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100333 * \return The number of bytes written to \p p on success.
334 * \return A negative error code on failure.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336int mbedtls_asn1_write_named_bitstring(unsigned char **p,
337 const unsigned char *start,
338 const unsigned char *buf,
339 size_t bits);
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100340
341/**
Hanno Becker55177552018-10-24 12:29:53 +0100342 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
343 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200344 *
Hanno Becker55177552018-10-24 12:29:53 +0100345 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200346 *
Hanno Becker55177552018-10-24 12:29:53 +0100347 * \param p The reference to the current position pointer.
348 * \param start The start of the buffer, for bounds-checking.
349 * \param buf The buffer holding the data to write.
350 * \param size The length of the data buffer \p buf.
351 *
352 * \return The number of bytes written to \p p on success.
353 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
356 const unsigned char *buf, size_t size);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200357
358/**
359 * \brief Create or find a specific named_data entry for writing in a
360 * sequence or list based on the OID. If not already in there,
361 * a new entry is added to the head of the list.
362 * Warning: Destructive behaviour for the val data!
363 *
Hanno Becker55177552018-10-24 12:29:53 +0100364 * \param list The pointer to the location of the head of the list to seek
365 * through (will be updated in case of a new entry).
366 * \param oid The OID to look for.
367 * \param oid_len The size of the OID.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100368 * \param val The associated data to store. If this is \c NULL,
369 * no data is copied to the new or existing buffer.
Hanno Becker55177552018-10-24 12:29:53 +0100370 * \param val_len The minimum length of the data buffer needed.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100371 * If this is 0, do not allocate a buffer for the associated
372 * data.
373 * If the OID was already present, enlarge, shrink or free
374 * the existing buffer to fit \p val_len.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200375 *
Hanno Becker55177552018-10-24 12:29:53 +0100376 * \return A pointer to the new / existing entry on success.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000377 * \return \c NULL if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200378 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list,
380 const char *oid, size_t oid_len,
381 const unsigned char *val,
382 size_t val_len);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200383
Paul Bakker407a0da2013-06-27 14:29:21 +0200384#ifdef __cplusplus
385}
386#endif
387
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +0100388#endif /* MBEDTLS_ASN1_WRITE_C */
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_ASN1_WRITE_H */