mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers asn1write.h Source File

asn1write.h

Go to the documentation of this file.
00001 /**
00002  * \file asn1write.h
00003  *
00004  * \brief ASN.1 buffer writing functionality
00005  *
00006  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00007  *  SPDX-License-Identifier: Apache-2.0
00008  *
00009  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  *  not use this file except in compliance with the License.
00011  *  You may obtain a copy of the License at
00012  *
00013  *  http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  *  Unless required by applicable law or agreed to in writing, software
00016  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  *  See the License for the specific language governing permissions and
00019  *  limitations under the License.
00020  *
00021  *  This file is part of mbed TLS (https://tls.mbed.org)
00022  */
00023 #ifndef MBEDTLS_ASN1_WRITE_H
00024 #define MBEDTLS_ASN1_WRITE_H
00025 
00026 #include "asn1.h"
00027 
00028 #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else   \
00029                                 g += ret; } while( 0 )
00030 
00031 #ifdef __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 /**
00036  * \brief           Write a length field in ASN.1 format
00037  *                  Note: function works backwards in data buffer
00038  *
00039  * \param p         reference to current position pointer
00040  * \param start     start of the buffer (for bounds-checking)
00041  * \param len       the length to write
00042  *
00043  * \return          the length written or a negative error code
00044  */
00045 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
00046 
00047 /**
00048  * \brief           Write a ASN.1 tag in ASN.1 format
00049  *                  Note: function works backwards in data buffer
00050  *
00051  * \param p         reference to current position pointer
00052  * \param start     start of the buffer (for bounds-checking)
00053  * \param tag       the tag to write
00054  *
00055  * \return          the length written or a negative error code
00056  */
00057 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
00058                     unsigned char tag );
00059 
00060 /**
00061  * \brief           Write raw buffer data
00062  *                  Note: function works backwards in data buffer
00063  *
00064  * \param p         reference to current position pointer
00065  * \param start     start of the buffer (for bounds-checking)
00066  * \param buf       data buffer to write
00067  * \param size      length of the data buffer
00068  *
00069  * \return          the length written or a negative error code
00070  */
00071 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
00072                            const unsigned char *buf, size_t size );
00073 
00074 #if defined(MBEDTLS_BIGNUM_C)
00075 /**
00076  * \brief           Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
00077  *                  Note: function works backwards in data buffer
00078  *
00079  * \param p         reference to current position pointer
00080  * \param start     start of the buffer (for bounds-checking)
00081  * \param X         the MPI to write
00082  *
00083  * \return          the length written or a negative error code
00084  */
00085 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
00086 #endif /* MBEDTLS_BIGNUM_C */
00087 
00088 /**
00089  * \brief           Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
00090  *                  Note: function works backwards in data buffer
00091  *
00092  * \param p         reference to current position pointer
00093  * \param start     start of the buffer (for bounds-checking)
00094  *
00095  * \return          the length written or a negative error code
00096  */
00097 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
00098 
00099 /**
00100  * \brief           Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
00101  *                  Note: function works backwards in data buffer
00102  *
00103  * \param p         reference to current position pointer
00104  * \param start     start of the buffer (for bounds-checking)
00105  * \param oid       the OID to write
00106  * \param oid_len   length of the OID
00107  *
00108  * \return          the length written or a negative error code
00109  */
00110 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
00111                     const char *oid, size_t oid_len );
00112 
00113 /**
00114  * \brief           Write an AlgorithmIdentifier sequence in ASN.1 format
00115  *                  Note: function works backwards in data buffer
00116  *
00117  * \param p         reference to current position pointer
00118  * \param start     start of the buffer (for bounds-checking)
00119  * \param oid       the OID of the algorithm
00120  * \param oid_len   length of the OID
00121  * \param par_len   length of parameters, which must be already written.
00122  *                  If 0, NULL parameters are added
00123  *
00124  * \return          the length written or a negative error code
00125  */
00126 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
00127                                      const char *oid, size_t oid_len,
00128                                      size_t par_len );
00129 
00130 /**
00131  * \brief           Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
00132  *                  Note: function works backwards in data buffer
00133  *
00134  * \param p         reference to current position pointer
00135  * \param start     start of the buffer (for bounds-checking)
00136  * \param boolean   0 or 1
00137  *
00138  * \return          the length written or a negative error code
00139  */
00140 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
00141 
00142 /**
00143  * \brief           Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
00144  *                  Note: function works backwards in data buffer
00145  *
00146  * \param p         reference to current position pointer
00147  * \param start     start of the buffer (for bounds-checking)
00148  * \param val       the integer value
00149  *
00150  * \return          the length written or a negative error code
00151  */
00152 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
00153 
00154 /**
00155  * \brief           Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
00156  *                  value in ASN.1 format
00157  *                  Note: function works backwards in data buffer
00158  *
00159  * \param p         reference to current position pointer
00160  * \param start     start of the buffer (for bounds-checking)
00161  * \param text      the text to write
00162  * \param text_len  length of the text
00163  *
00164  * \return          the length written or a negative error code
00165  */
00166 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
00167                                  const char *text, size_t text_len );
00168 
00169 /**
00170  * \brief           Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
00171  *                  value in ASN.1 format
00172  *                  Note: function works backwards in data buffer
00173  *
00174  * \param p         reference to current position pointer
00175  * \param start     start of the buffer (for bounds-checking)
00176  * \param text      the text to write
00177  * \param text_len  length of the text
00178  *
00179  * \return          the length written or a negative error code
00180  */
00181 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
00182                            const char *text, size_t text_len );
00183 
00184 /**
00185  * \brief           Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
00186  *                  value in ASN.1 format
00187  *                  Note: function works backwards in data buffer
00188  *
00189  * \param p         reference to current position pointer
00190  * \param start     start of the buffer (for bounds-checking)
00191  * \param buf       the bitstring
00192  * \param bits      the total number of bits in the bitstring
00193  *
00194  * \return          the length written or a negative error code
00195  */
00196 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
00197                           const unsigned char *buf, size_t bits );
00198 
00199 /**
00200  * \brief           Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and
00201  *                  value in ASN.1 format
00202  *                  Note: function works backwards in data buffer
00203  *
00204  * \param p         reference to current position pointer
00205  * \param start     start of the buffer (for bounds-checking)
00206  * \param buf       data buffer to write
00207  * \param size      length of the data buffer
00208  *
00209  * \return          the length written or a negative error code
00210  */
00211 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
00212                              const unsigned char *buf, size_t size );
00213 
00214 /**
00215  * \brief           Create or find a specific named_data entry for writing in a
00216  *                  sequence or list based on the OID. If not already in there,
00217  *                  a new entry is added to the head of the list.
00218  *                  Warning: Destructive behaviour for the val data!
00219  *
00220  * \param list      Pointer to the location of the head of the list to seek
00221  *                  through (will be updated in case of a new entry)
00222  * \param oid       The OID to look for
00223  * \param oid_len   Size of the OID
00224  * \param val       Data to store (can be NULL if you want to fill it by hand)
00225  * \param val_len   Minimum length of the data buffer needed
00226  *
00227  * \return      NULL if if there was a memory allocation error, or a pointer
00228  *              to the new / existing entry.
00229  */
00230 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
00231                                         const char *oid, size_t oid_len,
00232                                         const unsigned char *val,
00233                                         size_t val_len );
00234 
00235 #ifdef __cplusplus
00236 }
00237 #endif
00238 
00239 #endif /* MBEDTLS_ASN1_WRITE_H */