blob: 6fe57c8f0efdd74cbcb207b128821dcc812a5ffc [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
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010051#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C)
Paul Bakker7accbce2013-08-26 17:34:53 +020052/**
Hanno Becker55177552018-10-24 12:29:53 +010053 * \brief Write a length field in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020054 *
Hanno Becker55177552018-10-24 12:29:53 +010055 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020056 *
Hanno Becker55177552018-10-24 12:29:53 +010057 * \param p The reference to the current position pointer.
58 * \param start The start of the buffer, for bounds-checking.
59 * \param len The length value to write.
60 *
61 * \return The number of bytes written to \p p on success.
62 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020063 */
Gilles Peskine449bd832023-01-11 14:50:10 +010064int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start,
65 size_t len);
Paul Bakker7accbce2013-08-26 17:34:53 +020066/**
Hanno Becker55177552018-10-24 12:29:53 +010067 * \brief Write an ASN.1 tag in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +020068 *
Hanno Becker55177552018-10-24 12:29:53 +010069 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +020070 *
Hanno Becker55177552018-10-24 12:29:53 +010071 * \param p The reference to the current position pointer.
72 * \param start The start of the buffer, for bounds-checking.
73 * \param tag The tag to write.
74 *
75 * \return The number of bytes written to \p p on success.
76 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +020077 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start,
79 unsigned char tag);
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010080#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */
Paul Bakker7accbce2013-08-26 17:34:53 +020081
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +010082#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakker9852d002013-08-26 17:56:37 +020083/**
Hanno Becker55177552018-10-24 12:29:53 +010084 * \brief Write raw buffer data.
Paul Bakker9852d002013-08-26 17:56:37 +020085 *
Hanno Becker55177552018-10-24 12:29:53 +010086 * \note This function works backwards in data buffer.
Paul Bakker9852d002013-08-26 17:56:37 +020087 *
Hanno Becker55177552018-10-24 12:29:53 +010088 * \param p The reference to the current position pointer.
89 * \param start The start of the buffer, for bounds-checking.
90 * \param buf The data buffer to write.
91 * \param size The length of the data buffer.
92 *
93 * \return The number of bytes written to \p p on success.
94 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker9852d002013-08-26 17:56:37 +020095 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
97 const unsigned char *buf, size_t size);
Paul Bakker9852d002013-08-26 17:56:37 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker7accbce2013-08-26 17:34:53 +0200100/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100101 * \brief Write an arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)
Hanno Becker55177552018-10-24 12:29:53 +0100102 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200103 *
Hanno Becker55177552018-10-24 12:29:53 +0100104 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200105 *
Hanno Becker55177552018-10-24 12:29:53 +0100106 * \param p The reference to the current position pointer.
107 * \param start The start of the buffer, for bounds-checking.
108 * \param X The MPI to write.
Gilles Peskine105031b2019-03-01 19:28:41 +0100109 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +0100110 *
111 * \return The number of bytes written to \p p on success.
112 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start,
115 const mbedtls_mpi *X);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker7accbce2013-08-26 17:34:53 +0200117
118/**
Hanno Becker55177552018-10-24 12:29:53 +0100119 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data
120 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200121 *
Hanno Becker55177552018-10-24 12:29:53 +0100122 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200123 *
Hanno Becker55177552018-10-24 12:29:53 +0100124 * \param p The reference to the current position pointer.
125 * \param start The start of the buffer, for bounds-checking.
126 *
127 * \return The number of bytes written to \p p on success.
128 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start);
Paul Bakker7accbce2013-08-26 17:34:53 +0200131
132/**
Hanno Becker55177552018-10-24 12:29:53 +0100133 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data
134 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200135 *
Hanno Becker55177552018-10-24 12:29:53 +0100136 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200137 *
Hanno Becker55177552018-10-24 12:29:53 +0100138 * \param p The reference to the current position pointer.
139 * \param start The start of the buffer, for bounds-checking.
140 * \param oid The OID to write.
141 * \param oid_len The length of the OID.
142 *
143 * \return The number of bytes written to \p p on success.
144 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
147 const char *oid, size_t oid_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200148
149/**
Hanno Becker55177552018-10-24 12:29:53 +0100150 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200151 *
Hanno Becker55177552018-10-24 12:29:53 +0100152 * \note This function works backwards in data buffer.
153 *
154 * \param p The reference to the current position pointer.
155 * \param start The start of the buffer, for bounds-checking.
156 * \param oid The OID of the algorithm to write.
157 * \param oid_len The length of the algorithm's OID.
158 * \param par_len The length of the parameters, which must be already written.
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200159 * If 0, NULL parameters are added
Paul Bakker7accbce2013-08-26 17:34:53 +0200160 *
Hanno Becker55177552018-10-24 12:29:53 +0100161 * \return The number of bytes written to \p p on success.
162 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200163 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_asn1_write_algorithm_identifier(unsigned char **p,
165 const unsigned char *start,
166 const char *oid, size_t oid_len,
167 size_t par_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200168
169/**
Jethro Beekman01672442023-04-19 14:08:14 +0200170 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.
171 *
172 * \note This function works backwards in data buffer.
173 *
174 * \param p The reference to the current position pointer.
175 * \param start The start of the buffer, for bounds-checking.
176 * \param oid The OID of the algorithm to write.
177 * \param oid_len The length of the algorithm's OID.
178 * \param par_len The length of the parameters, which must be already written.
179 * \param has_par If there are any parameters. If 0, par_len must be 0. If 1
180 * and \p par_len is 0, NULL parameters are added.
181 *
182 * \return The number of bytes written to \p p on success.
183 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
184 */
185int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p,
186 const unsigned char *start,
187 const char *oid, size_t oid_len,
188 size_t par_len, int has_par);
189
190/**
Hanno Becker55177552018-10-24 12:29:53 +0100191 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value
192 * in ASN.1 format.
Paul Bakker329def32013-09-06 16:34:38 +0200193 *
Hanno Becker55177552018-10-24 12:29:53 +0100194 * \note This function works backwards in data buffer.
Paul Bakker329def32013-09-06 16:34:38 +0200195 *
Hanno Becker55177552018-10-24 12:29:53 +0100196 * \param p The reference to the current position pointer.
197 * \param start The start of the buffer, for bounds-checking.
198 * \param boolean The boolean value to write, either \c 0 or \c 1.
199 *
200 * \return The number of bytes written to \p p on success.
201 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker329def32013-09-06 16:34:38 +0200202 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start,
204 int boolean);
Paul Bakker329def32013-09-06 16:34:38 +0200205
206/**
Hanno Becker55177552018-10-24 12:29:53 +0100207 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value
208 * in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200209 *
Hanno Becker55177552018-10-24 12:29:53 +0100210 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200211 *
Hanno Becker55177552018-10-24 12:29:53 +0100212 * \param p The reference to the current position pointer.
213 * \param start The start of the buffer, for bounds-checking.
214 * \param val The integer value to write.
Gilles Peskine105031b2019-03-01 19:28:41 +0100215 * It must be non-negative.
Hanno Becker55177552018-10-24 12:29:53 +0100216 *
217 * \return The number of bytes written to \p p on success.
218 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200219 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val);
Paul Bakker7accbce2013-08-26 17:34:53 +0200221
222/**
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200223 * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
224 * in ASN.1 format.
225 *
226 * \note This function works backwards in data buffer.
227 *
228 * \param p The reference to the current position pointer.
229 * \param start The start of the buffer, for bounds-checking.
230 * \param val The integer value to write.
231 *
232 * \return The number of bytes written to \p p on success.
233 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
234 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val);
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200236
237/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100238 * \brief Write a string in ASN.1 format using a specific
239 * string encoding tag.
Hanno Becker55177552018-10-24 12:29:53 +0100240
241 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100242 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100243 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100244 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100245 * \param tag The string encoding tag to write, e.g.
246 * #MBEDTLS_ASN1_UTF8_STRING.
247 * \param text The string to write.
248 * \param text_len The length of \p text in bytes (which might
249 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100250 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100251 * \return The number of bytes written to \p p on success.
252 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100253 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start,
255 int tag, const char *text,
256 size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200257
258/**
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100259 * \brief Write a string in ASN.1 format using the PrintableString
260 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100261 *
262 * \note This function works backwards in data buffer.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100263 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100264 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100265 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100266 * \param text The string to write.
267 * \param text_len The length of \p text in bytes (which might
268 * be strictly larger than the number of characters).
Jaeden Amero23f954d2018-05-17 11:46:13 +0100269 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100270 * \return The number of bytes written to \p p on success.
271 * \return A negative error code on failure.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_asn1_write_printable_string(unsigned char **p,
274 const unsigned char *start,
275 const char *text, size_t text_len);
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100276
277/**
278 * \brief Write a UTF8 string in ASN.1 format using the UTF8String
Gilles Peskine105031b2019-03-01 19:28:41 +0100279 * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100280 *
281 * \note This function works backwards in data buffer.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100282 *
283 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100284 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100285 * \param text The string to write.
286 * \param text_len The length of \p text in bytes (which might
287 * be strictly larger than the number of characters).
288 *
289 * \return The number of bytes written to \p p on success.
290 * \return A negative error code on failure.
Jaeden Amero23f954d2018-05-17 11:46:13 +0100291 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
293 const char *text, size_t text_len);
Jaeden Amero23f954d2018-05-17 11:46:13 +0100294
295/**
Hanno Becker55177552018-10-24 12:29:53 +0100296 * \brief Write a string in ASN.1 format using the IA5String
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100297 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).
Hanno Becker55177552018-10-24 12:29:53 +0100298 *
299 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200300 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100301 * \param p The reference to the current position pointer.
Hanno Becker55177552018-10-24 12:29:53 +0100302 * \param start The start of the buffer, for bounds-checking.
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100303 * \param text The string to write.
304 * \param text_len The length of \p text in bytes (which might
305 * be strictly larger than the number of characters).
Paul Bakker7accbce2013-08-26 17:34:53 +0200306 *
Hanno Beckerd0e21fb2018-10-08 14:41:31 +0100307 * \return The number of bytes written to \p p on success.
308 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
311 const char *text, size_t text_len);
Paul Bakker7accbce2013-08-26 17:34:53 +0200312
313/**
Hanno Becker55177552018-10-24 12:29:53 +0100314 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and
315 * value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200316 *
Hanno Becker55177552018-10-24 12:29:53 +0100317 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200318 *
Hanno Becker55177552018-10-24 12:29:53 +0100319 * \param p The reference to the current position pointer.
320 * \param start The start of the buffer, for bounds-checking.
321 * \param buf The bitstring to write.
322 * \param bits The total number of bits in the bitstring.
323 *
324 * \return The number of bytes written to \p p on success.
325 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200326 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
328 const unsigned char *buf, size_t bits);
Paul Bakker7accbce2013-08-26 17:34:53 +0200329
330/**
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100331 * \brief This function writes a named bitstring tag
332 * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100333 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100334 * As stated in RFC 5280 Appendix B, trailing zeroes are
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100335 * omitted when encoding named bitstrings in DER.
336 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100337 * \note This function works backwards within the data buffer.
338 *
339 * \param p The reference to the current position pointer.
340 * \param start The start of the buffer which is used for bounds-checking.
341 * \param buf The bitstring to write.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100342 * \param bits The total number of bits in the bitstring.
343 *
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100344 * \return The number of bytes written to \p p on success.
345 * \return A negative error code on failure.
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347int mbedtls_asn1_write_named_bitstring(unsigned char **p,
348 const unsigned char *start,
349 const unsigned char *buf,
350 size_t bits);
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100351
352/**
Hanno Becker55177552018-10-24 12:29:53 +0100353 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)
354 * and value in ASN.1 format.
Paul Bakker7accbce2013-08-26 17:34:53 +0200355 *
Hanno Becker55177552018-10-24 12:29:53 +0100356 * \note This function works backwards in data buffer.
Paul Bakker7accbce2013-08-26 17:34:53 +0200357 *
Hanno Becker55177552018-10-24 12:29:53 +0100358 * \param p The reference to the current position pointer.
359 * \param start The start of the buffer, for bounds-checking.
360 * \param buf The buffer holding the data to write.
361 * \param size The length of the data buffer \p buf.
362 *
363 * \return The number of bytes written to \p p on success.
364 * \return A negative error code on failure.
Paul Bakker7accbce2013-08-26 17:34:53 +0200365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
367 const unsigned char *buf, size_t size);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200368
369/**
370 * \brief Create or find a specific named_data entry for writing in a
371 * sequence or list based on the OID. If not already in there,
372 * a new entry is added to the head of the list.
373 * Warning: Destructive behaviour for the val data!
374 *
Hanno Becker55177552018-10-24 12:29:53 +0100375 * \param list The pointer to the location of the head of the list to seek
376 * through (will be updated in case of a new entry).
377 * \param oid The OID to look for.
378 * \param oid_len The size of the OID.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100379 * \param val The associated data to store. If this is \c NULL,
380 * no data is copied to the new or existing buffer.
Hanno Becker55177552018-10-24 12:29:53 +0100381 * \param val_len The minimum length of the data buffer needed.
Gilles Peskine09c0a232019-03-04 15:00:06 +0100382 * If this is 0, do not allocate a buffer for the associated
383 * data.
384 * If the OID was already present, enlarge, shrink or free
385 * the existing buffer to fit \p val_len.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200386 *
Hanno Becker55177552018-10-24 12:29:53 +0100387 * \return A pointer to the new / existing entry on success.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000388 * \return \c NULL if there was a memory allocation error.
Paul Bakker59ba59f2013-09-09 11:26:00 +0200389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list,
391 const char *oid, size_t oid_len,
392 const unsigned char *val,
393 size_t val_len);
Paul Bakker59ba59f2013-09-09 11:26:00 +0200394
Paul Bakker407a0da2013-06-27 14:29:21 +0200395#ifdef __cplusplus
396}
397#endif
398
Agathiyan Bragadeesh86dc0852023-09-04 14:53:30 +0100399#endif /* MBEDTLS_ASN1_WRITE_C */
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#endif /* MBEDTLS_ASN1_WRITE_H */