asn1write.h 9.1 KB

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