asn1write.h 9.2 KB

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