blob: 3c5072c018a25b7a16f2406deca4948426b9acc4 [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
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_ASN1_WRITE_H
23#define MBEDTLS_ASN1_WRITE_H
Paul Bakkerbdb912d2012-02-13 23:11:30 +000024
Bence Szépkútic662b362021-05-27 11:25:03 +020025#include "mbedtls/build_info.h"
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020026
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/asn1.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
Hanno Becker55177552018-10-24 12:29:53 +010029#define MBEDTLS_ASN1_CHK_ADD(g, f) \
Hanno Becker1eeca412018-10-15 12:01:35 +010030 do \
31 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010032 if ((ret = (f)) < 0) \
33 return ret; \
Hanno Becker55177552018-10-24 12:29:53 +010034 else \
Gilles Peskine449bd832023-01-11 14:50:10 +010035 (g) += ret; \
36 } while (0)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000037
Przemek Stekiel57207712023-02-24 14:03:30 +010038#define MBEDTLS_ASN1_CHK_CLEANUP_ADD(g, f) \
39 do \
40 { \
41 if ((ret = (f)) < 0) \
42 goto cleanup; \
43 else \
44 (g) += ret; \
45 } while (0)
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker7accbce2013-08-26 17:34:53 +020051/**
Hanno Becker55177552018-10-24 12:29:53 +010052 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020053 *
Hanno Becker55177552018-10-24 12:29:53 +010054 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020055 *
Hanno Becker55177552018-10-24 12:29:53 +010056 * \param p The reference to the current position pointer.
57 * \param start The start of the buffer, for bounds-checking.
58 * \param len The length value to write.
59 *
60 * \return The number of bytes written to \p p on success.
61 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start,
64 size_t len);
Paul Bakker7accbce2013-08-26 17:34:53 +020065/**
Hanno Becker55177552018-10-24 12:29:53 +010066 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020067 *
Hanno Becker55177552018-10-24 12:29:53 +010068 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020069 *
Hanno Becker55177552018-10-24 12:29:53 +010070 * \param p The reference to the current position pointer.
71 * \param start The start of the buffer, for bounds-checking.
72 * \param tag The tag to write.
73 *
74 * \return The number of bytes written to \p p on success.
75 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020076 */
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start,
78 unsigned char tag);
Paul Bakker7accbce2013-08-26 17:34:53 +020079
Paul Bakker9852d002013-08-26 17:56:37 +020080/**
Hanno Becker55177552018-10-24 12:29:53 +010081 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020082 *
Hanno Becker55177552018-10-24 12:29:53 +010083 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020084 *
Hanno Becker55177552018-10-24 12:29:53 +010085 * \param p The reference to the current position pointer.
86 * \param start The start of the buffer, for bounds-checking.
87 * \param buf The data buffer to write.
88 * \param size The length of the data buffer.
89 *
90 * \return The number of bytes written to \p p on success.
91 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +020092 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
94 const unsigned char *buf, size_t size);
Paul Bakker9852d002013-08-26 17:56:37 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020097/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010098 * \brief Write an arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
Hanno Becker55177552018-10-24 12:29:53 +010099 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200100 *
Hanno Becker55177552018-10-24 12:29:53 +0100101 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200102 *
Hanno Becker55177552018-10-24 12:29:53 +0100103 * \param p The reference to the current position pointer.
104 * \param start The start of the buffer, for bounds-checking.
105 * \param X The MPI to write.
Gilles Peskine105031b2019-03-01 19:28:41 +0100106 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +0100107 *
108 * \return The number of bytes written to \p p on success.
109 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start,
112 const mbedtls_mpi *X);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200114
115/**
Hanno Becker55177552018-10-24 12:29:53 +0100116 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
117 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200118 *
Hanno Becker55177552018-10-24 12:29:53 +0100119 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200120 *
Hanno Becker55177552018-10-24 12:29:53 +0100121 * \param p The reference to the current position pointer.
122 * \param start The start of the buffer, for bounds-checking.
123 *
124 * \return The number of bytes written to \p p on success.
125 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start);
Paul Bakker7accbce2013-08-26 17:34:53 +0200128
129/**
Hanno Becker55177552018-10-24 12:29:53 +0100130 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
131 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200132 *
Hanno Becker55177552018-10-24 12:29:53 +0100133 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200134 *
Hanno Becker55177552018-10-24 12:29:53 +0100135 * \param p The reference to the current position pointer.
136 * \param start The start of the buffer, for bounds-checking.
137 * \param oid The OID to write.
138 * \param oid_len The length of the OID.
139 *
140 * \return The number of bytes written to \p p on success.
141 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
144 const char *oid, size_t oid_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200145
146/**
Hanno Becker55177552018-10-24 12:29:53 +0100147 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200148 *
Hanno Becker55177552018-10-24 12:29:53 +0100149 * \note This function works backwards in data buffer.
150 *
151 * \param p The reference to the current position pointer.
152 * \param start The start of the buffer, for bounds-checking.
153 * \param oid The OID of the algorithm to write.
154 * \param oid_len The length of the algorithm's OID.
155 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200156 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200157 *
Hanno Becker55177552018-10-24 12:29:53 +0100158 * \return The number of bytes written to \p p on success.
159 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161int mbedtls_asn1_write_algorithm_identifier(unsigned char **p,
162 const unsigned char *start,
163 const char *oid, size_t oid_len,
164 size_t par_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200165
166/**
Jethro Beekman01672442023-04-19 14:08:14 +0200167 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
168 *
169 * \note This function works backwards in data buffer.
170 *
171 * \param p The reference to the current position pointer.
172 * \param start The start of the buffer, for bounds-checking.
173 * \param oid The OID of the algorithm to write.
174 * \param oid_len The length of the algorithm's OID.
175 * \param par_len The length of the parameters, which must be already written.
176 * \param has_par If there are any parameters. If 0, par_len must be 0. If 1
177 * and \p par_len is 0, NULL parameters are added.
178 *
179 * \return The number of bytes written to \p p on success.
180 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
181 */
182int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p,
183 const unsigned char *start,
184 const char *oid, size_t oid_len,
185 size_t par_len, int has_par);
186
187/**
Hanno Becker55177552018-10-24 12:29:53 +0100188 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
189 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200190 *
Hanno Becker55177552018-10-24 12:29:53 +0100191 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200192 *
Hanno Becker55177552018-10-24 12:29:53 +0100193 * \param p The reference to the current position pointer.
194 * \param start The start of the buffer, for bounds-checking.
195 * \param boolean The boolean value to write, either \c 0 or \c 1.
196 *
197 * \return The number of bytes written to \p p on success.
198 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start,
201 int boolean);
Paul Bakker329def32013-09-06 16:34:38 +0200202
203/**
Hanno Becker55177552018-10-24 12:29:53 +0100204 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
205 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200206 *
Hanno Becker55177552018-10-24 12:29:53 +0100207 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200208 *
Hanno Becker55177552018-10-24 12:29:53 +0100209 * \param p The reference to the current position pointer.
210 * \param start The start of the buffer, for bounds-checking.
211 * \param val The integer value to write.
Gilles Peskine105031b2019-03-01 19:28:41 +0100212 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +0100213 *
214 * \return The number of bytes written to \p p on success.
215 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200216 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val);
Paul Bakker7accbce2013-08-26 17:34:53 +0200218
219/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200220 * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
221 * in ASN.1 format.
222 *
223 * \note This function works backwards in data buffer.
224 *
225 * \param p The reference to the current position pointer.
226 * \param start The start of the buffer, for bounds-checking.
227 * \param val The integer value to write.
228 *
229 * \return The number of bytes written to \p p on success.
230 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200233
234/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100235 * \brief Write a string in ASN.1 format using a specific
236 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100237
238 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100239 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100240 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100241 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100242 * \param tag The string encoding tag to write, e.g.
243 * #MBEDTLS_ASN1_UTF8_STRING.
244 * \param text The string to write.
245 * \param text_len The length of \p text in bytes (which might
246 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100247 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100248 * \return The number of bytes written to \p p on success.
249 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start,
252 int tag, const char *text,
253 size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200254
255/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100256 * \brief Write a string in ASN.1 format using the PrintableString
257 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100258 *
259 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100260 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100261 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100262 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100263 * \param text The string to write.
264 * \param text_len The length of \p text in bytes (which might
265 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100266 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100267 * \return The number of bytes written to \p p on success.
268 * \return A negative error code on failure.
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270int mbedtls_asn1_write_printable_string(unsigned char **p,
271 const unsigned char *start,
272 const char *text, size_t text_len);
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100273
274/**
275 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
Gilles Peskine105031b2019-03-01 19:28:41 +0100276 * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100277 *
278 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100279 *
280 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100281 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100282 * \param text The string to write.
283 * \param text_len The length of \p text in bytes (which might
284 * be strictly larger than the number of characters).
285 *
286 * \return The number of bytes written to \p p on success.
287 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
290 const char *text, size_t text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100291
292/**
Hanno Becker55177552018-10-24 12:29:53 +0100293 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100294 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100295 *
296 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200297 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100298 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100299 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100300 * \param text The string to write.
301 * \param text_len The length of \p text in bytes (which might
302 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200303 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100304 * \return The number of bytes written to \p p on success.
305 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
308 const char *text, size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200309
310/**
Hanno Becker55177552018-10-24 12:29:53 +0100311 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
312 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200313 *
Hanno Becker55177552018-10-24 12:29:53 +0100314 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200315 *
Hanno Becker55177552018-10-24 12:29:53 +0100316 * \param p The reference to the current position pointer.
317 * \param start The start of the buffer, for bounds-checking.
318 * \param buf The bitstring to write.
319 * \param bits The total number of bits in the bitstring.
320 *
321 * \return The number of bytes written to \p p on success.
322 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200323 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
325 const unsigned char *buf, size_t bits);
Paul Bakker7accbce2013-08-26 17:34:53 +0200326
327/**
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100328 * \brief This function writes a named bitstring tag
329 * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100330 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100331 * As stated in RFC 5280 Appendix B, trailing zeroes are
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100332 * omitted when encoding named bitstrings in DER.
333 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100334 * \note This function works backwards within the data buffer.
335 *
336 * \param p The reference to the current position pointer.
337 * \param start The start of the buffer which is used for bounds-checking.
338 * \param buf The bitstring to write.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100339 * \param bits The total number of bits in the bitstring.
340 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100341 * \return The number of bytes written to \p p on success.
342 * \return A negative error code on failure.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100343 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344int mbedtls_asn1_write_named_bitstring(unsigned char **p,
345 const unsigned char *start,
346 const unsigned char *buf,
347 size_t bits);
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100348
349/**
Hanno Becker55177552018-10-24 12:29:53 +0100350 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
351 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200352 *
Hanno Becker55177552018-10-24 12:29:53 +0100353 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200354 *
Hanno Becker55177552018-10-24 12:29:53 +0100355 * \param p The reference to the current position pointer.
356 * \param start The start of the buffer, for bounds-checking.
357 * \param buf The buffer holding the data to write.
358 * \param size The length of the data buffer \p buf.
359 *
360 * \return The number of bytes written to \p p on success.
361 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
364 const unsigned char *buf, size_t size);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200365
366/**
367 * \brief Create or find a specific named_data entry for writing in a
368 * sequence or list based on the OID. If not already in there,
369 * a new entry is added to the head of the list.
370 * Warning: Destructive behaviour for the val data!
371 *
Hanno Becker55177552018-10-24 12:29:53 +0100372 * \param list The pointer to the location of the head of the list to seek
373 * through (will be updated in case of a new entry).
374 * \param oid The OID to look for.
375 * \param oid_len The size of the OID.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100376 * \param val The associated data to store. If this is \c NULL,
377 * no data is copied to the new or existing buffer.
Hanno Becker55177552018-10-24 12:29:53 +0100378 * \param val_len The minimum length of the data buffer needed.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100379 * If this is 0, do not allocate a buffer for the associated
380 * data.
381 * If the OID was already present, enlarge, shrink or free
382 * the existing buffer to fit \p val_len.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200383 *
Hanno Becker55177552018-10-24 12:29:53 +0100384 * \return A pointer to the new / existing entry on success.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000385 * \return \c NULL if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list,
388 const char *oid, size_t oid_len,
389 const unsigned char *val,
390 size_t val_len);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200391
Paul Bakker407a0da2013-06-27 14:29:21 +0200392#ifdef __cplusplus
393}
394#endif
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_ASN1_WRITE_H */