asn1write.h 9.1 KB

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