--- /dev/null
+/**\r
+ * \file asn1write.h\r
+ *\r
+ * \brief ASN.1 buffer writing functionality\r
+ */\r
+/*\r
+ * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may\r
+ * not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * This file is part of mbed TLS (https://tls.mbed.org)\r
+ */\r
+#ifndef MBEDTLS_ASN1_WRITE_H\r
+#define MBEDTLS_ASN1_WRITE_H\r
+\r
+#if !defined(MBEDTLS_CONFIG_FILE)\r
+#include "config.h"\r
+#else\r
+#include MBEDTLS_CONFIG_FILE\r
+#endif\r
+\r
+#include "asn1.h"\r
+\r
+#define MBEDTLS_ASN1_CHK_ADD(g, f) \\r
+ do \\r
+ { \\r
+ if( ( ret = (f) ) < 0 ) \\r
+ return( ret ); \\r
+ else \\r
+ (g) += ret; \\r
+ } while( 0 )\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+/**\r
+ * \brief Write a length field in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param len The length value to write.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start,\r
+ size_t len );\r
+/**\r
+ * \brief Write an ASN.1 tag in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param tag The tag to write.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,\r
+ unsigned char tag );\r
+\r
+/**\r
+ * \brief Write raw buffer data.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param buf The data buffer to write.\r
+ * \param size The length of the data buffer.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,\r
+ const unsigned char *buf, size_t size );\r
+\r
+#if defined(MBEDTLS_BIGNUM_C)\r
+/**\r
+ * \brief Write a arbitrary-precision number (#MBEDTLS_ASN1_INTEGER)\r
+ * in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param X The MPI to write.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start,\r
+ const mbedtls_mpi *X );\r
+#endif /* MBEDTLS_BIGNUM_C */\r
+\r
+/**\r
+ * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data\r
+ * in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );\r
+\r
+/**\r
+ * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data\r
+ * in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param oid The OID to write.\r
+ * \param oid_len The length of the OID.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,\r
+ const char *oid, size_t oid_len );\r
+\r
+/**\r
+ * \brief Write an AlgorithmIdentifier sequence in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param oid The OID of the algorithm to write.\r
+ * \param oid_len The length of the algorithm's OID.\r
+ * \param par_len The length of the parameters, which must be already written.\r
+ * If 0, NULL parameters are added\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_algorithm_identifier( unsigned char **p,\r
+ unsigned char *start,\r
+ const char *oid, size_t oid_len,\r
+ size_t par_len );\r
+\r
+/**\r
+ * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value\r
+ * in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param boolean The boolean value to write, either \c 0 or \c 1.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,\r
+ int boolean );\r
+\r
+/**\r
+ * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value\r
+ * in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param val The integer value to write.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.\r
+ */\r
+int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );\r
+\r
+/**\r
+ * \brief Write a string in ASN.1 format using a specific\r
+ * string encoding tag.\r
+\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param tag The string encoding tag to write, e.g.\r
+ * #MBEDTLS_ASN1_UTF8_STRING.\r
+ * \param text The string to write.\r
+ * \param text_len The length of \p text in bytes (which might\r
+ * be strictly larger than the number of characters).\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start,\r
+ int tag, const char *text,\r
+ size_t text_len );\r
+\r
+/**\r
+ * \brief Write a string in ASN.1 format using the PrintableString\r
+ * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param text The string to write.\r
+ * \param text_len The length of \p text in bytes (which might\r
+ * be strictly larger than the number of characters).\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_printable_string( unsigned char **p,\r
+ unsigned char *start,\r
+ const char *text, size_t text_len );\r
+\r
+/**\r
+ * \brief Write a UTF8 string in ASN.1 format using the UTF8String\r
+ * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param text The string to write.\r
+ * \param text_len The length of \p text in bytes (which might\r
+ * be strictly larger than the number of characters).\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,\r
+ const char *text, size_t text_len );\r
+\r
+/**\r
+ * \brief Write a string in ASN.1 format using the IA5String\r
+ * string encoding tag (#MBEDTLS_ASN1_IA5_STRING).\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param text The string to write.\r
+ * \param text_len The length of \p text in bytes (which might\r
+ * be strictly larger than the number of characters).\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,\r
+ const char *text, size_t text_len );\r
+\r
+/**\r
+ * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and\r
+ * value in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param buf The bitstring to write.\r
+ * \param bits The total number of bits in the bitstring.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,\r
+ const unsigned char *buf, size_t bits );\r
+\r
+/**\r
+ * \brief This function writes a named bitstring tag\r
+ * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.\r
+ *\r
+ * As stated in RFC 5280 Appendix B, trailing zeroes are\r
+ * omitted when encoding named bitstrings in DER.\r
+ *\r
+ * \note This function works backwards within the data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer which is used for bounds-checking.\r
+ * \param buf The bitstring to write.\r
+ * \param bits The total number of bits in the bitstring.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_named_bitstring( unsigned char **p,\r
+ unsigned char *start,\r
+ const unsigned char *buf,\r
+ size_t bits );\r
+\r
+/**\r
+ * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING)\r
+ * and value in ASN.1 format.\r
+ *\r
+ * \note This function works backwards in data buffer.\r
+ *\r
+ * \param p The reference to the current position pointer.\r
+ * \param start The start of the buffer, for bounds-checking.\r
+ * \param buf The buffer holding the data to write.\r
+ * \param size The length of the data buffer \p buf.\r
+ *\r
+ * \return The number of bytes written to \p p on success.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,\r
+ const unsigned char *buf, size_t size );\r
+\r
+/**\r
+ * \brief Create or find a specific named_data entry for writing in a\r
+ * sequence or list based on the OID. If not already in there,\r
+ * a new entry is added to the head of the list.\r
+ * Warning: Destructive behaviour for the val data!\r
+ *\r
+ * \param list The pointer to the location of the head of the list to seek\r
+ * through (will be updated in case of a new entry).\r
+ * \param oid The OID to look for.\r
+ * \param oid_len The size of the OID.\r
+ * \param val The data to store (can be \c NULL if you want to fill\r
+ * it by hand).\r
+ * \param val_len The minimum length of the data buffer needed.\r
+ *\r
+ * \return A pointer to the new / existing entry on success.\r
+ * \return \c NULL if if there was a memory allocation error.\r
+ */\r
+mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,\r
+ const char *oid, size_t oid_len,\r
+ const unsigned char *val,\r
+ size_t val_len );\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* MBEDTLS_ASN1_WRITE_H */\r