| 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 | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | *  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 Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 21 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 23 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_ASN1_WRITE_H | 
|  | 25 | #define MBEDTLS_ASN1_WRITE_H | 
| Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 26 |  | 
|  | 27 | #include "asn1.h" | 
|  | 28 |  | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 29 | #define MBEDTLS_ASN1_CHK_ADD(g, f)                      \ | 
|  | 30 | do {                                                \ | 
|  | 31 | if( ( ret = f ) < 0 )                           \ | 
|  | 32 | return( ret );                              \ | 
|  | 33 | else                                            \ | 
|  | 34 | g += ret;                                   \ | 
|  | 35 | } while( 0 ) | 
| Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 36 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 37 | #ifdef __cplusplus | 
|  | 38 | extern "C" { | 
|  | 39 | #endif | 
|  | 40 |  | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 41 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 42 | * \brief           Write a length field in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 43 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 44 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 45 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 46 | * \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 Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 52 | */ | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 53 | int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, | 
|  | 54 | size_t len ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 55 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 56 | * \brief           Write an ASN.1 tag in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 57 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 58 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 59 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 60 | * \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 Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 66 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 68 | unsigned char tag ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 69 |  | 
| Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 70 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 71 | * \brief           Write raw buffer data. | 
| Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 72 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 73 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 74 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 75 | * \param p         The reference to the current position pointer. | 
|  | 76 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 77 | * \param buf       The data buffer to write. | 
|  | 78 | * \param size      The length of the data buffer. | 
|  | 79 | * | 
|  | 80 | * \return          The number of bytes written to \p p on success. | 
|  | 81 | * \return          A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. | 
| Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 82 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 84 | const unsigned char *buf, size_t size ); | 
| Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 85 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | #if defined(MBEDTLS_BIGNUM_C) | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 87 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 88 | * \brief           Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER) | 
|  | 89 | *                  in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 90 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 91 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 92 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 93 | * \param p         The reference to the current position pointer. | 
|  | 94 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 95 | * \param X         The MPI to write. | 
|  | 96 | * | 
|  | 97 | * \return          The number of bytes written to \p p on success. | 
|  | 98 | * \return          A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 99 | */ | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 100 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, | 
|  | 101 | const mbedtls_mpi *X ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | #endif /* MBEDTLS_BIGNUM_C */ | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 103 |  | 
|  | 104 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 105 | * \brief           Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data | 
|  | 106 | *                  in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 107 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 108 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 109 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 110 | * \param p         The reference to the current position pointer. | 
|  | 111 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 112 | * | 
|  | 113 | * \return          The number of bytes written to \p p on success. | 
|  | 114 | * \return          A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 115 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 117 |  | 
|  | 118 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 119 | * \brief           Write an OID tag (#MBEDTLS_ASN1_OID) and data | 
|  | 120 | *                  in ASN.1 format. | 
| 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 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 123 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 124 | * \param p         The reference to the current position pointer. | 
|  | 125 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 126 | * \param oid       The OID to write. | 
|  | 127 | * \param oid_len   The length of the OID. | 
|  | 128 | * | 
|  | 129 | * \return          The number of bytes written to \p p on success. | 
|  | 130 | * \return          A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 131 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 133 | const char *oid, size_t oid_len ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 134 |  | 
|  | 135 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 136 | * \brief           Write an AlgorithmIdentifier sequence in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 137 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 138 | * \note            This function works backwards in data buffer. | 
|  | 139 | * | 
|  | 140 | * \param p         The reference to the current position pointer. | 
|  | 141 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 142 | * \param oid       The OID of the algorithm to write. | 
|  | 143 | * \param oid_len   The length of the algorithm's OID. | 
|  | 144 | * \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] | 145 | *                  If 0, NULL parameters are added | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 146 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 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 | */ | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 150 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, | 
|  | 151 | unsigned char *start, | 
|  | 152 | const char *oid, size_t oid_len, | 
|  | 153 | size_t par_len ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 154 |  | 
|  | 155 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 156 | * \brief           Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value | 
|  | 157 | *                  in ASN.1 format. | 
| Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 158 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 159 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 160 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 161 | * \param p         The reference to the current position pointer. | 
|  | 162 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 163 | * \param boolean   The boolean value to write, either \c 0 or \c 1. | 
|  | 164 | * | 
|  | 165 | * \return          The number of bytes written to \p p on success. | 
|  | 166 | * \return          A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. | 
| Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 167 | */ | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 168 | int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, | 
|  | 169 | int boolean ); | 
| Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 170 |  | 
|  | 171 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 172 | * \brief           Write an int tag (#MBEDTLS_ASN1_INTEGER) and value | 
|  | 173 | *                  in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 174 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 175 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 176 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 177 | * \param p         The reference to the current position pointer. | 
|  | 178 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 179 | * \param val       The integer value to write. | 
|  | 180 | * | 
|  | 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 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | 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] | 185 |  | 
|  | 186 | /** | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 187 | * \brief           Write a string in ASN.1 format using a specific | 
|  | 188 | *                  string encoding tag. | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 189 |  | 
|  | 190 | * \note            This function works backwards in data buffer. | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 191 | * | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 192 | * \param p         The reference to the current position pointer. | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 193 | * \param start     The start of the buffer, for bounds-checking. | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 194 | * \param tag       The string encoding tag to write, e.g. | 
|  | 195 | *                  #MBEDTLS_ASN1_UTF8_STRING. | 
|  | 196 | * \param text      The string to write. | 
|  | 197 | * \param text_len  The length of \p text in bytes (which might | 
|  | 198 | *                  be strictly larger than the number of characters). | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 199 | * | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 200 | * \return          The number of bytes written to \p p on success. | 
|  | 201 | * \return          A negative error code on failure. | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 202 | */ | 
| thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 203 | int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 204 | int tag, const char *text, | 
|  | 205 | size_t text_len ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 206 |  | 
|  | 207 | /** | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 208 | * \brief           Write a string in ASN.1 format using the PrintableString | 
|  | 209 | *                  string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 210 | * | 
|  | 211 | * \note            This function works backwards in data buffer. | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 212 | * | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 213 | * \param p         The reference to the current position pointer. | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 214 | * \param start     The start of the buffer, for bounds-checking. | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 215 | * \param text      The string to write. | 
|  | 216 | * \param text_len  The length of \p text in bytes (which might | 
|  | 217 | *                  be strictly larger than the number of characters). | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 218 | * | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 219 | * \return          The number of bytes written to \p p on success. | 
|  | 220 | * \return          A negative error code on failure. | 
|  | 221 | */ | 
|  | 222 | int mbedtls_asn1_write_printable_string( unsigned char **p, | 
|  | 223 | unsigned char *start, | 
|  | 224 | const char *text, size_t text_len ); | 
|  | 225 |  | 
|  | 226 | /** | 
|  | 227 | * \brief           Write a UTF8 string in ASN.1 format using the UTF8String | 
|  | 228 | *                  string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 229 | * | 
|  | 230 | * \note            This function works backwards in data buffer. | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 231 | * | 
|  | 232 | * \param p         The reference to the current position pointer. | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 233 | * \param start     The start of the buffer, for bounds-checking. | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 234 | * \param text      The string to write. | 
|  | 235 | * \param text_len  The length of \p text in bytes (which might | 
|  | 236 | *                  be strictly larger than the number of characters). | 
|  | 237 | * | 
|  | 238 | * \return          The number of bytes written to \p p on success. | 
|  | 239 | * \return          A negative error code on failure. | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 240 | */ | 
|  | 241 | int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start, | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 242 | const char *text, size_t text_len ); | 
| Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 243 |  | 
|  | 244 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 245 | * \brief           Write a string in ASN.1 format using the IA5String | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 246 | *                  string encoding tag (#MBEDTLS_ASN1_IA5_STRING). | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 247 | * | 
|  | 248 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 249 | * | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 250 | * \param p         The reference to the current position pointer. | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 251 | * \param start     The start of the buffer, for bounds-checking. | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 252 | * \param text      The string to write. | 
|  | 253 | * \param text_len  The length of \p text in bytes (which might | 
|  | 254 | *                  be strictly larger than the number of characters). | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 255 | * | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 256 | * \return          The number of bytes written to \p p on success. | 
|  | 257 | * \return          A negative error code on failure. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 258 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, | 
| Hanno Becker | d0e21fb | 2018-10-08 14:41:31 +0100 | [diff] [blame] | 260 | const char *text, size_t text_len ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 261 |  | 
|  | 262 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 263 | * \brief           Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and | 
|  | 264 | *                  value in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 265 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 266 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 267 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 268 | * \param p         The reference to the current position pointer. | 
|  | 269 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 270 | * \param buf       The bitstring to write. | 
|  | 271 | * \param bits      The total number of bits in the bitstring. | 
|  | 272 | * | 
|  | 273 | * \return          The number of bytes written to \p p on success. | 
|  | 274 | * \return          A negative error code on failure. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 275 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 277 | const unsigned char *buf, size_t bits ); | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 278 |  | 
|  | 279 | /** | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 280 | * \brief           Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING) | 
|  | 281 | *                  and value in ASN.1 format. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 282 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 283 | * \note            This function works backwards in data buffer. | 
| Paul Bakker | 7accbce | 2013-08-26 17:34:53 +0200 | [diff] [blame] | 284 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 285 | * \param p         The reference to the current position pointer. | 
|  | 286 | * \param start     The start of the buffer, for bounds-checking. | 
|  | 287 | * \param buf       The buffer holding the data to write. | 
|  | 288 | * \param size      The length of the data buffer \p buf. | 
|  | 289 | * | 
|  | 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_octet_string( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 294 | const unsigned char *buf, size_t size ); | 
| Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 295 |  | 
|  | 296 | /** | 
|  | 297 | * \brief           Create or find a specific named_data entry for writing in a | 
|  | 298 | *                  sequence or list based on the OID. If not already in there, | 
|  | 299 | *                  a new entry is added to the head of the list. | 
|  | 300 | *                  Warning: Destructive behaviour for the val data! | 
|  | 301 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 302 | * \param list      The pointer to the location of the head of the list to seek | 
|  | 303 | *                  through (will be updated in case of a new entry). | 
|  | 304 | * \param oid       The OID to look for. | 
|  | 305 | * \param oid_len   The size of the OID. | 
|  | 306 | * \param val       The data to store (can be \c NULL if you want to fill | 
|  | 307 | *                  it by hand). | 
|  | 308 | * \param val_len   The minimum length of the data buffer needed. | 
| Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 309 | * | 
| Hanno Becker | 5517755 | 2018-10-24 12:29:53 +0100 | [diff] [blame] | 310 | * \return          A pointer to the new / existing entry on success. | 
|  | 311 | * \return          \c NULL if if there was a memory allocation error. | 
| Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 312 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 313 | 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] | 314 | const char *oid, size_t oid_len, | 
|  | 315 | const unsigned char *val, | 
|  | 316 | size_t val_len ); | 
|  | 317 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 318 | #ifdef __cplusplus | 
|  | 319 | } | 
|  | 320 | #endif | 
|  | 321 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | #endif /* MBEDTLS_ASN1_WRITE_H */ |