Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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