Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

265 wiersze
11 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_BYTESTRING_H
  15. #define OPENSSL_HEADER_BYTESTRING_H
  16. #include <openssl/base.h>
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. /* Bytestrings are used for parsing and building TLS and ASN.1 messages.
  21. *
  22. * A "CBS" (CRYPTO ByteString) represents a string of bytes in memory and
  23. * provides utility functions for safely parsing length-prefixed structures
  24. * like TLS and ASN.1 from it.
  25. *
  26. * A "CBB" (CRYPTO ByteBuilder) is a memory buffer that grows as needed and
  27. * provides utility functions for building length-prefixed messages. */
  28. /* CRYPTO ByteString */
  29. struct cbs_st {
  30. const uint8_t *data;
  31. size_t len;
  32. };
  33. /* CBS_init sets |cbs| to point to |data|. It does not take ownership of
  34. * |data|. */
  35. void CBS_init(CBS *cbs, const uint8_t *data, size_t len);
  36. /* CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero
  37. * otherwise. */
  38. int CBS_skip(CBS *cbs, size_t len);
  39. /* CBS_data returns a pointer to the contains of |cbs|. */
  40. const uint8_t *CBS_data(const CBS *cbs);
  41. /* CBS_len returns the number of bytes remaining in |cbs|. */
  42. size_t CBS_len(const CBS *cbs);
  43. /* CBS_stow copies the current contents of |cbs| into |*out_ptr| and
  44. * |*out_len|. If |*out_ptr| is not NULL, the contents are freed with
  45. * OPENSSL_free. It returns one on success and zero on allocation failure. On
  46. * success, |*out_ptr| should be freed with OPENSSL_free. If |cbs| is empty,
  47. * |*out_ptr| will be NULL. */
  48. int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len);
  49. /* CBS_strdup copies the current contents of |cbs| into |*out_ptr| as a
  50. * NUL-terminated C string. If |*out_ptr| is not NULL, the contents are freed
  51. * with OPENSSL_free. It returns one on success and zero on allocation
  52. * failure. On success, |*out_ptr| should be freed with OPENSSL_free.
  53. *
  54. * NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call
  55. * |CBS_contains_zero_byte(cbs)| to check for NUL bytes. */
  56. int CBS_strdup(const CBS *cbs, char **out_ptr);
  57. /* CBS_contains_zero_byte returns one if the current contents of |cbs| contains
  58. * a NUL byte and zero otherwise. */
  59. int CBS_contains_zero_byte(const CBS *cbs);
  60. /* CBS_mem_equal compares the current contents of |cbs| with the |len| bytes
  61. * starting at |data|. If they're equal, it returns one, otherwise zero. If the
  62. * lengths match, it uses a constant-time comparison. */
  63. int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len);
  64. /* CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It
  65. * returns one on success and zero on error. */
  66. int CBS_get_u8(CBS *cbs, uint8_t *out);
  67. /* CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and
  68. * advances |cbs|. It returns one on success and zero on error. */
  69. int CBS_get_u16(CBS *cbs, uint16_t *out);
  70. /* CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and
  71. * advances |cbs|. It returns one on success and zero on error. */
  72. int CBS_get_u24(CBS *cbs, uint32_t *out);
  73. /* CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs|
  74. * and advances |cbs|. It returns one on success and zero on error. */
  75. int CBS_get_u32(CBS *cbs, uint32_t *out);
  76. /* CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances
  77. * |cbs|. It returns one on success and zero on error. */
  78. int CBS_get_bytes(CBS *cbs, CBS *out, size_t len);
  79. /* CBS_get_u8_length_prefixed sets |*out| to the contents of an 8-bit,
  80. * length-prefixed value from |cbs| and advances |cbs| over it. It returns one
  81. * on success and zero on error. */
  82. int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out);
  83. /* CBS_get_u16_length_prefixed sets |*out| to the contents of a 16-bit,
  84. * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It
  85. * returns one on success and zero on error. */
  86. int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out);
  87. /* CBS_get_u24_length_prefixed sets |*out| to the contents of a 24-bit,
  88. * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It
  89. * returns one on success and zero on error. */
  90. int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out);
  91. /* Parsing ASN.1 */
  92. #define CBS_ASN1_BOOLEAN 0x1
  93. #define CBS_ASN1_INTEGER 0x2
  94. #define CBS_ASN1_BITSTRING 0x3
  95. #define CBS_ASN1_OCTETSTRING 0x4
  96. #define CBS_ASN1_OBJECT 0x6
  97. #define CBS_ASN1_SEQUENCE (0x10 | CBS_ASN1_CONSTRUCTED)
  98. #define CBS_ASN1_SET (0x11 | CBS_ASN1_CONSTRUCTED)
  99. #define CBS_ASN1_CONSTRUCTED 0x20
  100. #define CBS_ASN1_CONTEXT_SPECIFIC 0x80
  101. #define CBS_ASN1_ANY 0x10000
  102. /* CBS_get_asn1 sets |*out| to the contents of DER-encoded, ASN.1 element (not
  103. * including tag and length bytes) and advances |cbs| over it. The ASN.1
  104. * element must match |tag_value|. It returns one on success and zero
  105. * on error.
  106. *
  107. * Tag numbers greater than 31 are not supported. */
  108. int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value);
  109. /* CBS_get_asn1_ber sets |*out| to the contents of BER-encoded, ASN.1 element
  110. * (not including tag and length bytes) and advances |cbs| over it. The ASN.1
  111. * element must match |tag_value|. It returns one on success and zero on error.
  112. *
  113. * The major difference between this function and |CBS_get_asn1| is that
  114. * indefinite-length elements may be processed by this function.
  115. *
  116. * Tag numbers greater than 31 are not supported. */
  117. int CBS_get_asn1_ber(CBS *cbs, CBS *out, unsigned tag_value);
  118. /* CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the
  119. * ASN.1 header bytes too. */
  120. int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value);
  121. /* CRYPTO ByteBuilder.
  122. *
  123. * |CBB| objects allow one to build length-prefixed serialisations. A |CBB|
  124. * object is associated with a buffer and new buffers are created with
  125. * |CBB_init|. Several |CBB| objects can point at the same buffer when a
  126. * length-prefix is pending, however only a single |CBB| can be 'current' at
  127. * any one time. For example, if one calls |CBB_add_u8_length_prefixed| then
  128. * the new |CBB| points at the same buffer as the original. But if the original
  129. * |CBB| is used then the length prefix is written out and the new |CBB| must
  130. * not be used again.
  131. *
  132. * If one needs to force a length prefix to be written out because a |CBB| is
  133. * going out of scope, use |CBB_flush|. */
  134. struct cbb_buffer_st {
  135. uint8_t *buf;
  136. size_t len; /* The number of valid bytes. */
  137. size_t cap; /* The size of buf. */
  138. char can_resize; /* One iff |buf| is owned by this object. If not then |buf|
  139. cannot be resized. */
  140. };
  141. struct cbb_st {
  142. struct cbb_buffer_st *base;
  143. /* offset is the offset from the start of |base->buf| to the position of any
  144. * pending length-prefix. */
  145. size_t offset;
  146. /* child points to a child CBB if a length-prefix is pending. */
  147. struct cbb_st *child;
  148. /* pending_len_len contains the number of bytes in a pending length-prefix,
  149. * or zero if no length-prefix is pending. */
  150. uint8_t pending_len_len;
  151. char pending_is_asn1;
  152. /* is_top_level is true iff this is a top-level |CBB| (as opposed to a child
  153. * |CBB|). Top-level objects are valid arguments for |CBB_finish|. */
  154. char is_top_level;
  155. };
  156. /* CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as
  157. * needed, the |initial_capacity| is just a hint. It returns one on success or
  158. * zero on error. */
  159. int CBB_init(CBB *cbb, size_t initial_capacity);
  160. /* CBB_init_fixed initialises |cbb| to write to |len| bytes at |buf|. Since
  161. * |buf| cannot grow, trying to write more than |len| bytes will cause CBB
  162. * functions to fail. It returns one on success or zero on error. */
  163. int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len);
  164. /* CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects
  165. * writing to the same buffer. This should be used in an error case where a
  166. * serialisation is abandoned. */
  167. void CBB_cleanup(CBB *cbb);
  168. /* CBB_finish completes any pending length prefix and sets |*out_data| to a
  169. * malloced buffer and |*out_len| to the length of that buffer. The caller
  170. * takes ownership of the buffer and, unless the buffer was fixed with
  171. * |CBB_init_fixed|, must call |OPENSSL_free| when done.
  172. *
  173. * It can only be called on a "top level" |CBB|, i.e. one initialised with
  174. * |CBB_init| or |CBB_init_fixed|. It returns one on success and zero on
  175. * error. */
  176. int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len);
  177. /* CBB_flush causes any pending length prefixes to be written out and any child
  178. * |CBB| objects of |cbb| to be invalidated. It returns one on success or zero
  179. * on error. */
  180. int CBB_flush(CBB *cbb);
  181. /* CBB_add_u8_length_prefixed sets |*out_contents| to a new child of |cbb|. The
  182. * data written to |*out_contents| will be prefixed in |cbb| with an 8-bit
  183. * length. It returns one on success or zero on error. */
  184. int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents);
  185. /* CBB_add_u16_length_prefixed sets |*out_contents| to a new child of |cbb|.
  186. * The data written to |*out_contents| will be prefixed in |cbb| with a 16-bit,
  187. * big-endian length. It returns one on success or zero on error. */
  188. int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents);
  189. /* CBB_add_u24_length_prefixed sets |*out_contents| to a new child of |cbb|.
  190. * The data written to |*out_contents| will be prefixed in |cbb| with a 24-bit,
  191. * big-endian length. It returns one on success or zero on error. */
  192. int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents);
  193. /* CBB_add_asn sets |*out_contents| to a |CBB| into which the contents of an
  194. * ASN.1 object can be written. The |tag| argument will be used as the tag for
  195. * the object. It returns one on success or zero on error. */
  196. int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag);
  197. /* CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on
  198. * success and zero otherwise. */
  199. int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len);
  200. /* CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on
  201. * success and zero otherwise. */
  202. int CBB_add_u8(CBB *cbb, uint8_t value);
  203. /* CBB_add_u8 appends a 16-bit, big-endian number from |value| to |cbb|. It
  204. * returns one on success and zero otherwise. */
  205. int CBB_add_u16(CBB *cbb, uint16_t value);
  206. /* CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It
  207. * returns one on success and zero otherwise. */
  208. int CBB_add_u24(CBB *cbb, uint32_t value);
  209. #if defined(__cplusplus)
  210. } /* extern C */
  211. #endif
  212. #endif /* OPENSSL_HEADER_BYTESTRING_H */