mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file asn1write.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief ASN.1 buffer writing functionality
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 7 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 8 *
markrad 0:cdf462088d13 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 10 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 11 * You may obtain a copy of the License at
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 18 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 19 * limitations under the License.
markrad 0:cdf462088d13 20 *
markrad 0:cdf462088d13 21 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 22 */
markrad 0:cdf462088d13 23 #ifndef MBEDTLS_ASN1_WRITE_H
markrad 0:cdf462088d13 24 #define MBEDTLS_ASN1_WRITE_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #include "asn1.h"
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
markrad 0:cdf462088d13 29 g += ret; } while( 0 )
markrad 0:cdf462088d13 30
markrad 0:cdf462088d13 31 #ifdef __cplusplus
markrad 0:cdf462088d13 32 extern "C" {
markrad 0:cdf462088d13 33 #endif
markrad 0:cdf462088d13 34
markrad 0:cdf462088d13 35 /**
markrad 0:cdf462088d13 36 * \brief Write a length field in ASN.1 format
markrad 0:cdf462088d13 37 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 38 *
markrad 0:cdf462088d13 39 * \param p reference to current position pointer
markrad 0:cdf462088d13 40 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 41 * \param len the length to write
markrad 0:cdf462088d13 42 *
markrad 0:cdf462088d13 43 * \return the length written or a negative error code
markrad 0:cdf462088d13 44 */
markrad 0:cdf462088d13 45 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 /**
markrad 0:cdf462088d13 48 * \brief Write a ASN.1 tag in ASN.1 format
markrad 0:cdf462088d13 49 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 50 *
markrad 0:cdf462088d13 51 * \param p reference to current position pointer
markrad 0:cdf462088d13 52 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 53 * \param tag the tag to write
markrad 0:cdf462088d13 54 *
markrad 0:cdf462088d13 55 * \return the length written or a negative error code
markrad 0:cdf462088d13 56 */
markrad 0:cdf462088d13 57 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 58 unsigned char tag );
markrad 0:cdf462088d13 59
markrad 0:cdf462088d13 60 /**
markrad 0:cdf462088d13 61 * \brief Write raw buffer data
markrad 0:cdf462088d13 62 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 63 *
markrad 0:cdf462088d13 64 * \param p reference to current position pointer
markrad 0:cdf462088d13 65 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 66 * \param buf data buffer to write
markrad 0:cdf462088d13 67 * \param size length of the data buffer
markrad 0:cdf462088d13 68 *
markrad 0:cdf462088d13 69 * \return the length written or a negative error code
markrad 0:cdf462088d13 70 */
markrad 0:cdf462088d13 71 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 72 const unsigned char *buf, size_t size );
markrad 0:cdf462088d13 73
markrad 0:cdf462088d13 74 #if defined(MBEDTLS_BIGNUM_C)
markrad 0:cdf462088d13 75 /**
markrad 0:cdf462088d13 76 * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
markrad 0:cdf462088d13 77 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 78 *
markrad 0:cdf462088d13 79 * \param p reference to current position pointer
markrad 0:cdf462088d13 80 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 81 * \param X the MPI to write
markrad 0:cdf462088d13 82 *
markrad 0:cdf462088d13 83 * \return the length written or a negative error code
markrad 0:cdf462088d13 84 */
markrad 0:cdf462088d13 85 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
markrad 0:cdf462088d13 86 #endif /* MBEDTLS_BIGNUM_C */
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 /**
markrad 0:cdf462088d13 89 * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
markrad 0:cdf462088d13 90 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 91 *
markrad 0:cdf462088d13 92 * \param p reference to current position pointer
markrad 0:cdf462088d13 93 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 94 *
markrad 0:cdf462088d13 95 * \return the length written or a negative error code
markrad 0:cdf462088d13 96 */
markrad 0:cdf462088d13 97 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 /**
markrad 0:cdf462088d13 100 * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
markrad 0:cdf462088d13 101 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 102 *
markrad 0:cdf462088d13 103 * \param p reference to current position pointer
markrad 0:cdf462088d13 104 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 105 * \param oid the OID to write
markrad 0:cdf462088d13 106 * \param oid_len length of the OID
markrad 0:cdf462088d13 107 *
markrad 0:cdf462088d13 108 * \return the length written or a negative error code
markrad 0:cdf462088d13 109 */
markrad 0:cdf462088d13 110 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 111 const char *oid, size_t oid_len );
markrad 0:cdf462088d13 112
markrad 0:cdf462088d13 113 /**
markrad 0:cdf462088d13 114 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
markrad 0:cdf462088d13 115 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 116 *
markrad 0:cdf462088d13 117 * \param p reference to current position pointer
markrad 0:cdf462088d13 118 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 119 * \param oid the OID of the algorithm
markrad 0:cdf462088d13 120 * \param oid_len length of the OID
markrad 0:cdf462088d13 121 * \param par_len length of parameters, which must be already written.
markrad 0:cdf462088d13 122 * If 0, NULL parameters are added
markrad 0:cdf462088d13 123 *
markrad 0:cdf462088d13 124 * \return the length written or a negative error code
markrad 0:cdf462088d13 125 */
markrad 0:cdf462088d13 126 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 127 const char *oid, size_t oid_len,
markrad 0:cdf462088d13 128 size_t par_len );
markrad 0:cdf462088d13 129
markrad 0:cdf462088d13 130 /**
markrad 0:cdf462088d13 131 * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
markrad 0:cdf462088d13 132 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 133 *
markrad 0:cdf462088d13 134 * \param p reference to current position pointer
markrad 0:cdf462088d13 135 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 136 * \param boolean 0 or 1
markrad 0:cdf462088d13 137 *
markrad 0:cdf462088d13 138 * \return the length written or a negative error code
markrad 0:cdf462088d13 139 */
markrad 0:cdf462088d13 140 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
markrad 0:cdf462088d13 141
markrad 0:cdf462088d13 142 /**
markrad 0:cdf462088d13 143 * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
markrad 0:cdf462088d13 144 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 145 *
markrad 0:cdf462088d13 146 * \param p reference to current position pointer
markrad 0:cdf462088d13 147 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 148 * \param val the integer value
markrad 0:cdf462088d13 149 *
markrad 0:cdf462088d13 150 * \return the length written or a negative error code
markrad 0:cdf462088d13 151 */
markrad 0:cdf462088d13 152 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
markrad 0:cdf462088d13 153
markrad 0:cdf462088d13 154 /**
markrad 0:cdf462088d13 155 * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
markrad 0:cdf462088d13 156 * value in ASN.1 format
markrad 0:cdf462088d13 157 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 158 *
markrad 0:cdf462088d13 159 * \param p reference to current position pointer
markrad 0:cdf462088d13 160 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 161 * \param text the text to write
markrad 0:cdf462088d13 162 * \param text_len length of the text
markrad 0:cdf462088d13 163 *
markrad 0:cdf462088d13 164 * \return the length written or a negative error code
markrad 0:cdf462088d13 165 */
markrad 0:cdf462088d13 166 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 167 const char *text, size_t text_len );
markrad 0:cdf462088d13 168
markrad 0:cdf462088d13 169 /**
markrad 0:cdf462088d13 170 * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
markrad 0:cdf462088d13 171 * value in ASN.1 format
markrad 0:cdf462088d13 172 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 173 *
markrad 0:cdf462088d13 174 * \param p reference to current position pointer
markrad 0:cdf462088d13 175 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 176 * \param text the text to write
markrad 0:cdf462088d13 177 * \param text_len length of the text
markrad 0:cdf462088d13 178 *
markrad 0:cdf462088d13 179 * \return the length written or a negative error code
markrad 0:cdf462088d13 180 */
markrad 0:cdf462088d13 181 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 182 const char *text, size_t text_len );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 /**
markrad 0:cdf462088d13 185 * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
markrad 0:cdf462088d13 186 * value in ASN.1 format
markrad 0:cdf462088d13 187 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 188 *
markrad 0:cdf462088d13 189 * \param p reference to current position pointer
markrad 0:cdf462088d13 190 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 191 * \param buf the bitstring
markrad 0:cdf462088d13 192 * \param bits the total number of bits in the bitstring
markrad 0:cdf462088d13 193 *
markrad 0:cdf462088d13 194 * \return the length written or a negative error code
markrad 0:cdf462088d13 195 */
markrad 0:cdf462088d13 196 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 197 const unsigned char *buf, size_t bits );
markrad 0:cdf462088d13 198
markrad 0:cdf462088d13 199 /**
markrad 0:cdf462088d13 200 * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and
markrad 0:cdf462088d13 201 * value in ASN.1 format
markrad 0:cdf462088d13 202 * Note: function works backwards in data buffer
markrad 0:cdf462088d13 203 *
markrad 0:cdf462088d13 204 * \param p reference to current position pointer
markrad 0:cdf462088d13 205 * \param start start of the buffer (for bounds-checking)
markrad 0:cdf462088d13 206 * \param buf data buffer to write
markrad 0:cdf462088d13 207 * \param size length of the data buffer
markrad 0:cdf462088d13 208 *
markrad 0:cdf462088d13 209 * \return the length written or a negative error code
markrad 0:cdf462088d13 210 */
markrad 0:cdf462088d13 211 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
markrad 0:cdf462088d13 212 const unsigned char *buf, size_t size );
markrad 0:cdf462088d13 213
markrad 0:cdf462088d13 214 /**
markrad 0:cdf462088d13 215 * \brief Create or find a specific named_data entry for writing in a
markrad 0:cdf462088d13 216 * sequence or list based on the OID. If not already in there,
markrad 0:cdf462088d13 217 * a new entry is added to the head of the list.
markrad 0:cdf462088d13 218 * Warning: Destructive behaviour for the val data!
markrad 0:cdf462088d13 219 *
markrad 0:cdf462088d13 220 * \param list Pointer to the location of the head of the list to seek
markrad 0:cdf462088d13 221 * through (will be updated in case of a new entry)
markrad 0:cdf462088d13 222 * \param oid The OID to look for
markrad 0:cdf462088d13 223 * \param oid_len Size of the OID
markrad 0:cdf462088d13 224 * \param val Data to store (can be NULL if you want to fill it by hand)
markrad 0:cdf462088d13 225 * \param val_len Minimum length of the data buffer needed
markrad 0:cdf462088d13 226 *
markrad 0:cdf462088d13 227 * \return NULL if if there was a memory allocation error, or a pointer
markrad 0:cdf462088d13 228 * to the new / existing entry.
markrad 0:cdf462088d13 229 */
markrad 0:cdf462088d13 230 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
markrad 0:cdf462088d13 231 const char *oid, size_t oid_len,
markrad 0:cdf462088d13 232 const unsigned char *val,
markrad 0:cdf462088d13 233 size_t val_len );
markrad 0:cdf462088d13 234
markrad 0:cdf462088d13 235 #ifdef __cplusplus
markrad 0:cdf462088d13 236 }
markrad 0:cdf462088d13 237 #endif
markrad 0:cdf462088d13 238
markrad 0:cdf462088d13 239 #endif /* MBEDTLS_ASN1_WRITE_H */