vtls_int.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #ifndef HEADER_CURL_VTLS_INT_H
  2. #define HEADER_CURL_VTLS_INT_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include "cfilters.h"
  28. #include "urldata.h"
  29. #ifdef USE_SSL
  30. struct ssl_connect_data;
  31. /* see https://www.iana.org/assignments/tls-extensiontype-values/ */
  32. #define ALPN_HTTP_1_1_LENGTH 8
  33. #define ALPN_HTTP_1_1 "http/1.1"
  34. #define ALPN_H2_LENGTH 2
  35. #define ALPN_H2 "h2"
  36. #define ALPN_H3_LENGTH 2
  37. #define ALPN_H3 "h3"
  38. /* conservative sizes on the ALPN entries and count we are handling,
  39. * we can increase these if we ever feel the need or have to accommodate
  40. * ALPN strings from the "outside". */
  41. #define ALPN_NAME_MAX 10
  42. #define ALPN_ENTRIES_MAX 3
  43. #define ALPN_PROTO_BUF_MAX (ALPN_ENTRIES_MAX * (ALPN_NAME_MAX + 1))
  44. struct alpn_spec {
  45. const char entries[ALPN_ENTRIES_MAX][ALPN_NAME_MAX];
  46. size_t count; /* number of entries */
  47. };
  48. struct alpn_proto_buf {
  49. unsigned char data[ALPN_PROTO_BUF_MAX];
  50. int len;
  51. };
  52. CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
  53. const struct alpn_spec *spec);
  54. CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
  55. const struct alpn_spec *spec);
  56. CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
  57. struct Curl_easy *data,
  58. struct ssl_connect_data *connssl,
  59. const unsigned char *proto,
  60. size_t proto_len);
  61. bool Curl_alpn_contains_proto(const struct alpn_spec *spec,
  62. const char *proto);
  63. /* enum for the nonblocking SSL connection state machine */
  64. typedef enum {
  65. ssl_connect_1,
  66. ssl_connect_2,
  67. ssl_connect_3,
  68. ssl_connect_done
  69. } ssl_connect_state;
  70. typedef enum {
  71. ssl_connection_none,
  72. ssl_connection_deferred,
  73. ssl_connection_negotiating,
  74. ssl_connection_complete
  75. } ssl_connection_state;
  76. typedef enum {
  77. ssl_earlydata_none,
  78. ssl_earlydata_use,
  79. ssl_earlydata_sending,
  80. ssl_earlydata_sent,
  81. ssl_earlydata_accepted,
  82. ssl_earlydata_rejected
  83. } ssl_earlydata_state;
  84. #define CURL_SSL_IO_NEED_NONE (0)
  85. #define CURL_SSL_IO_NEED_RECV (1<<0)
  86. #define CURL_SSL_IO_NEED_SEND (1<<1)
  87. /* Max earlydata payload we want to send */
  88. #define CURL_SSL_EARLY_MAX (64*1024)
  89. /* Information in each SSL cfilter context: cf->ctx */
  90. struct ssl_connect_data {
  91. struct ssl_peer peer;
  92. const struct alpn_spec *alpn; /* ALPN to use or NULL for none */
  93. void *backend; /* vtls backend specific props */
  94. struct cf_call_data call_data; /* data handle used in current call */
  95. struct curltime handshake_done; /* time when handshake finished */
  96. char *alpn_negotiated; /* negotiated ALPN value or NULL */
  97. struct bufq earlydata; /* earlydata to be send to peer */
  98. size_t earlydata_max; /* max earlydata allowed by peer */
  99. size_t earlydata_skip; /* sending bytes to skip when earlydata
  100. * is accepted by peer */
  101. ssl_connection_state state;
  102. ssl_connect_state connecting_state;
  103. ssl_earlydata_state earlydata_state;
  104. int io_need; /* TLS signals special SEND/RECV needs */
  105. BIT(use_alpn); /* if ALPN shall be used in handshake */
  106. BIT(peer_closed); /* peer has closed connection */
  107. };
  108. #undef CF_CTX_CALL_DATA
  109. #define CF_CTX_CALL_DATA(cf) \
  110. ((struct ssl_connect_data *)(cf)->ctx)->call_data
  111. /* Definitions for SSL Implementations */
  112. struct Curl_ssl {
  113. /*
  114. * This *must* be the first entry to allow returning the list of available
  115. * backends in curl_global_sslset().
  116. */
  117. curl_ssl_backend info;
  118. unsigned int supports; /* bitfield, see above */
  119. size_t sizeof_ssl_backend_data;
  120. int (*init)(void);
  121. void (*cleanup)(void);
  122. size_t (*version)(char *buffer, size_t size);
  123. int (*check_cxn)(struct Curl_cfilter *cf, struct Curl_easy *data);
  124. CURLcode (*shut_down)(struct Curl_cfilter *cf, struct Curl_easy *data,
  125. bool send_shutdown, bool *done);
  126. bool (*data_pending)(struct Curl_cfilter *cf,
  127. const struct Curl_easy *data);
  128. /* return 0 if a find random is filled in */
  129. CURLcode (*random)(struct Curl_easy *data, unsigned char *entropy,
  130. size_t length);
  131. bool (*cert_status_request)(void);
  132. CURLcode (*connect_blocking)(struct Curl_cfilter *cf,
  133. struct Curl_easy *data);
  134. CURLcode (*connect_nonblocking)(struct Curl_cfilter *cf,
  135. struct Curl_easy *data,
  136. bool *done);
  137. /* During handshake/shutdown, adjust the pollset to include the socket
  138. * for POLLOUT or POLLIN as needed. Mandatory. */
  139. void (*adjust_pollset)(struct Curl_cfilter *cf, struct Curl_easy *data,
  140. struct easy_pollset *ps);
  141. void *(*get_internals)(struct ssl_connect_data *connssl, CURLINFO info);
  142. void (*close)(struct Curl_cfilter *cf, struct Curl_easy *data);
  143. void (*close_all)(struct Curl_easy *data);
  144. CURLcode (*set_engine)(struct Curl_easy *data, const char *engine);
  145. CURLcode (*set_engine_default)(struct Curl_easy *data);
  146. struct curl_slist *(*engines_list)(struct Curl_easy *data);
  147. bool (*false_start)(void);
  148. CURLcode (*sha256sum)(const unsigned char *input, size_t inputlen,
  149. unsigned char *sha256sum, size_t sha256sumlen);
  150. bool (*attach_data)(struct Curl_cfilter *cf, struct Curl_easy *data);
  151. void (*detach_data)(struct Curl_cfilter *cf, struct Curl_easy *data);
  152. ssize_t (*recv_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
  153. char *buf, size_t len, CURLcode *code);
  154. ssize_t (*send_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
  155. const void *mem, size_t len, CURLcode *code);
  156. CURLcode (*get_channel_binding)(struct Curl_easy *data, int sockindex,
  157. struct dynbuf *binding);
  158. };
  159. extern const struct Curl_ssl *Curl_ssl;
  160. int Curl_none_init(void);
  161. void Curl_none_cleanup(void);
  162. CURLcode Curl_none_shutdown(struct Curl_cfilter *cf, struct Curl_easy *data,
  163. bool send_shutdown, bool *done);
  164. int Curl_none_check_cxn(struct Curl_cfilter *cf, struct Curl_easy *data);
  165. void Curl_none_close_all(struct Curl_easy *data);
  166. void Curl_none_session_free(void *ptr);
  167. bool Curl_none_data_pending(struct Curl_cfilter *cf,
  168. const struct Curl_easy *data);
  169. bool Curl_none_cert_status_request(void);
  170. CURLcode Curl_none_set_engine(struct Curl_easy *data, const char *engine);
  171. CURLcode Curl_none_set_engine_default(struct Curl_easy *data);
  172. struct curl_slist *Curl_none_engines_list(struct Curl_easy *data);
  173. bool Curl_none_false_start(void);
  174. void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data,
  175. struct easy_pollset *ps);
  176. /**
  177. * Get the SSL filter below the given one or NULL if there is none.
  178. */
  179. bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf);
  180. /* extract a session ID
  181. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  182. * Caller must make sure that the ownership of returned sessionid object
  183. * is properly taken (e.g. its refcount is incremented
  184. * under sessionid mutex).
  185. * @param cf the connection filter wanting to use it
  186. * @param data the transfer involved
  187. * @param peer the peer the filter wants to talk to
  188. * @param sessionid on return the TLS session
  189. * @param idsize on return the size of the TLS session data
  190. * @param palpn on return the ALPN string used by the session,
  191. * set to NULL when not interested
  192. */
  193. bool Curl_ssl_getsessionid(struct Curl_cfilter *cf,
  194. struct Curl_easy *data,
  195. const struct ssl_peer *peer,
  196. void **ssl_sessionid,
  197. size_t *idsize, /* set 0 if unknown */
  198. char **palpn);
  199. /* Set a TLS session ID for `peer`. Replaces an existing session ID if
  200. * not already the same.
  201. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  202. * Call takes ownership of `ssl_sessionid`, using `sessionid_free_cb`
  203. * to deallocate it. Is called in all outcomes, either right away or
  204. * later when the session cache is cleaned up.
  205. * Caller must ensure that it has properly shared ownership of this sessionid
  206. * object with cache (e.g. incrementing refcount on success)
  207. */
  208. CURLcode Curl_ssl_set_sessionid(struct Curl_cfilter *cf,
  209. struct Curl_easy *data,
  210. const struct ssl_peer *peer,
  211. const char *alpn,
  212. void *sessionid,
  213. size_t sessionid_size,
  214. Curl_ssl_sessionid_dtor *sessionid_free_cb);
  215. #endif /* USE_SSL */
  216. #endif /* HEADER_CURL_VTLS_INT_H */