Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file asn1write.h |
| 3 | * |
| 4 | * \brief ASN.1 buffer writing functionality |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | * |
| 10 | * This file is provided under the Apache License 2.0, or the |
| 11 | * GNU General Public License v2.0 or later. |
| 12 | * |
| 13 | * ********** |
| 14 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 27 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 28 | * ********** |
| 29 | * |
| 30 | * ********** |
| 31 | * GNU General Public License v2.0 or later: |
| 32 | * |
| 33 | * This program is free software; you can redistribute it and/or modify |
| 34 | * it under the terms of the GNU General Public License as published by |
| 35 | * the Free Software Foundation; either version 2 of the License, or |
| 36 | * (at your option) any later version. |
| 37 | * |
| 38 | * This program is distributed in the hope that it will be useful, |
| 39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 | * GNU General Public License for more details. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License along |
| 44 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 45 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 46 | * |
| 47 | * ********** |
| 48 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 49 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 50 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #ifndef MBEDTLS_ASN1_WRITE_H |
| 52 | #define MBEDTLS_ASN1_WRITE_H |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 53 | |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 55 | #include "config.h" |
| 56 | #else |
| 57 | #include MBEDTLS_CONFIG_FILE |
| 58 | #endif |
| 59 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 60 | #include "asn1.h" |
| 61 | |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 62 | #define MBEDTLS_ASN1_CHK_ADD(g, f) \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 63 | do \ |
| 64 | { \ |
| 65 | if( ( ret = (f) ) < 0 ) \ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 66 | return( ret ); \ |
| 67 | else \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 68 | (g) += ret; \ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 69 | } while( 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 70 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 71 | #ifdef __cplusplus |
| 72 | extern "C" { |
| 73 | #endif |
| 74 | |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 75 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 76 | * \brief Write a length field in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 77 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 78 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 79 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 80 | * \param p The reference to the current position pointer. |
| 81 | * \param start The start of the buffer, for bounds-checking. |
| 82 | * \param len The length value to write. |
| 83 | * |
| 84 | * \return The number of bytes written to \p p on success. |
| 85 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 86 | */ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 87 | int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, |
| 88 | size_t len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 89 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 90 | * \brief Write an ASN.1 tag in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 91 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 92 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 93 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 94 | * \param p The reference to the current position pointer. |
| 95 | * \param start The start of the buffer, for bounds-checking. |
| 96 | * \param tag The tag to write. |
| 97 | * |
| 98 | * \return The number of bytes written to \p p on success. |
| 99 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 100 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 102 | unsigned char tag ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 103 | |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 104 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 105 | * \brief Write raw buffer data. |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 106 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 107 | * \note This function works backwards in data buffer. |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 108 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 109 | * \param p The reference to the current position pointer. |
| 110 | * \param start The start of the buffer, for bounds-checking. |
| 111 | * \param buf The data buffer to write. |
| 112 | * \param size The length of the data buffer. |
| 113 | * |
| 114 | * \return The number of bytes written to \p p on success. |
| 115 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 116 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 118 | const unsigned char *buf, size_t size ); |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 121 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 122 | * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER) |
| 123 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 124 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 125 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 126 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 127 | * \param p The reference to the current position pointer. |
| 128 | * \param start The start of the buffer, for bounds-checking. |
| 129 | * \param X The MPI to write. |
| 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 Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 133 | */ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 134 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, |
| 135 | const mbedtls_mpi *X ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 137 | |
| 138 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 139 | * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data |
| 140 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 141 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 142 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 143 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 144 | * \param p The reference to the current position pointer. |
| 145 | * \param start The start of the buffer, for bounds-checking. |
| 146 | * |
| 147 | * \return The number of bytes written to \p p on success. |
| 148 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 151 | |
| 152 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 153 | * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data |
| 154 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 155 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 156 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 157 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 158 | * \param p The reference to the current position pointer. |
| 159 | * \param start The start of the buffer, for bounds-checking. |
| 160 | * \param oid The OID to write. |
| 161 | * \param oid_len The length of the OID. |
| 162 | * |
| 163 | * \return The number of bytes written to \p p on success. |
| 164 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 165 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 167 | const char *oid, size_t oid_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 168 | |
| 169 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 170 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 171 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 179 | * If 0, NULL parameters are added |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 180 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 181 | * \return The number of bytes written to \p p on success. |
| 182 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 183 | */ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 184 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, |
| 185 | unsigned char *start, |
| 186 | const char *oid, size_t oid_len, |
| 187 | size_t par_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 188 | |
| 189 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 190 | * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value |
| 191 | * in ASN.1 format. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 192 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 193 | * \note This function works backwards in data buffer. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 194 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 195 | * \param p The reference to the current position pointer. |
| 196 | * \param start The start of the buffer, for bounds-checking. |
| 197 | * \param boolean The boolean value to write, either \c 0 or \c 1. |
| 198 | * |
| 199 | * \return The number of bytes written to \p p on success. |
| 200 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 201 | */ |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 202 | int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, |
| 203 | int boolean ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 204 | |
| 205 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 206 | * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value |
| 207 | * in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 208 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 209 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 210 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 211 | * \param p The reference to the current position pointer. |
| 212 | * \param start The start of the buffer, for bounds-checking. |
| 213 | * \param val The integer value to write. |
| 214 | * |
| 215 | * \return The number of bytes written to \p p on success. |
| 216 | * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 217 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 219 | |
| 220 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 221 | * \brief Write a string in ASN.1 format using a specific |
| 222 | * string encoding tag. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 223 | |
| 224 | * \note This function works backwards in data buffer. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 225 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 226 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 227 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 228 | * \param tag The string encoding tag to write, e.g. |
| 229 | * #MBEDTLS_ASN1_UTF8_STRING. |
| 230 | * \param text The string to write. |
| 231 | * \param text_len The length of \p text in bytes (which might |
| 232 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 233 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 234 | * \return The number of bytes written to \p p on success. |
| 235 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 236 | */ |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 237 | int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 238 | int tag, const char *text, |
| 239 | size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 240 | |
| 241 | /** |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 242 | * \brief Write a string in ASN.1 format using the PrintableString |
| 243 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 244 | * |
| 245 | * \note This function works backwards in data buffer. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 246 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 247 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 248 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 249 | * \param text The string to write. |
| 250 | * \param text_len The length of \p text in bytes (which might |
| 251 | * be strictly larger than the number of characters). |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 252 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 253 | * \return The number of bytes written to \p p on success. |
| 254 | * \return A negative error code on failure. |
| 255 | */ |
| 256 | int mbedtls_asn1_write_printable_string( unsigned char **p, |
| 257 | unsigned char *start, |
| 258 | const char *text, size_t text_len ); |
| 259 | |
| 260 | /** |
| 261 | * \brief Write a UTF8 string in ASN.1 format using the UTF8String |
| 262 | * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 263 | * |
| 264 | * \note This function works backwards in data buffer. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 265 | * |
| 266 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 267 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 268 | * \param text The string to write. |
| 269 | * \param text_len The length of \p text in bytes (which might |
| 270 | * be strictly larger than the number of characters). |
| 271 | * |
| 272 | * \return The number of bytes written to \p p on success. |
| 273 | * \return A negative error code on failure. |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 274 | */ |
| 275 | int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 276 | const char *text, size_t text_len ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 277 | |
| 278 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 279 | * \brief Write a string in ASN.1 format using the IA5String |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 280 | * string encoding tag (#MBEDTLS_ASN1_IA5_STRING). |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 281 | * |
| 282 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 283 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 284 | * \param p The reference to the current position pointer. |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 285 | * \param start The start of the buffer, for bounds-checking. |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 286 | * \param text The string to write. |
| 287 | * \param text_len The length of \p text in bytes (which might |
| 288 | * be strictly larger than the number of characters). |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 289 | * |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 290 | * \return The number of bytes written to \p p on success. |
| 291 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 292 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 294 | const char *text, size_t text_len ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 295 | |
| 296 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 297 | * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and |
| 298 | * value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 299 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 300 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 301 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 302 | * \param p The reference to the current position pointer. |
| 303 | * \param start The start of the buffer, for bounds-checking. |
| 304 | * \param buf The bitstring to write. |
| 305 | * \param bits The total number of bits in the bitstring. |
| 306 | * |
| 307 | * \return The number of bytes written to \p p on success. |
| 308 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 309 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 310 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 311 | const unsigned char *buf, size_t bits ); |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 312 | |
| 313 | /** |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 314 | * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING) |
| 315 | * and value in ASN.1 format. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 316 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 317 | * \note This function works backwards in data buffer. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 318 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 319 | * \param p The reference to the current position pointer. |
| 320 | * \param start The start of the buffer, for bounds-checking. |
| 321 | * \param buf The buffer holding the data to write. |
| 322 | * \param size The length of the data buffer \p buf. |
| 323 | * |
| 324 | * \return The number of bytes written to \p p on success. |
| 325 | * \return A negative error code on failure. |
Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 326 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 328 | const unsigned char *buf, size_t size ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * \brief Create or find a specific named_data entry for writing in a |
| 332 | * sequence or list based on the OID. If not already in there, |
| 333 | * a new entry is added to the head of the list. |
| 334 | * Warning: Destructive behaviour for the val data! |
| 335 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 336 | * \param list The pointer to the location of the head of the list to seek |
| 337 | * through (will be updated in case of a new entry). |
| 338 | * \param oid The OID to look for. |
| 339 | * \param oid_len The size of the OID. |
| 340 | * \param val The data to store (can be \c NULL if you want to fill |
| 341 | * it by hand). |
| 342 | * \param val_len The minimum length of the data buffer needed. |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 343 | * |
Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 344 | * \return A pointer to the new / existing entry on success. |
| 345 | * \return \c NULL if if there was a memory allocation error. |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 346 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list, |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 348 | const char *oid, size_t oid_len, |
| 349 | const unsigned char *val, |
| 350 | size_t val_len ); |
| 351 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 352 | #ifdef __cplusplus |
| 353 | } |
| 354 | #endif |
| 355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | #endif /* MBEDTLS_ASN1_WRITE_H */ |