vtls_int.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /* see https://www.iana.org/assignments/tls-extensiontype-values/ */
  31. #define ALPN_HTTP_1_1_LENGTH 8
  32. #define ALPN_HTTP_1_1 "http/1.1"
  33. #define ALPN_H2_LENGTH 2
  34. #define ALPN_H2 "h2"
  35. #define ALPN_H3_LENGTH 2
  36. #define ALPN_H3 "h3"
  37. /* conservative sizes on the ALPN entries and count we are handling,
  38. * we can increase these if we ever feel the need or have to accommodate
  39. * ALPN strings from the "outside". */
  40. #define ALPN_NAME_MAX 10
  41. #define ALPN_ENTRIES_MAX 3
  42. #define ALPN_PROTO_BUF_MAX (ALPN_ENTRIES_MAX * (ALPN_NAME_MAX + 1))
  43. struct alpn_spec {
  44. const char entries[ALPN_ENTRIES_MAX][ALPN_NAME_MAX];
  45. size_t count; /* number of entries */
  46. };
  47. struct alpn_proto_buf {
  48. unsigned char data[ALPN_PROTO_BUF_MAX];
  49. int len;
  50. };
  51. CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
  52. const struct alpn_spec *spec);
  53. CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
  54. const struct alpn_spec *spec);
  55. CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
  56. struct Curl_easy *data,
  57. const unsigned char *proto,
  58. size_t proto_len);
  59. /* enum for the nonblocking SSL connection state machine */
  60. typedef enum {
  61. ssl_connect_1,
  62. ssl_connect_2,
  63. ssl_connect_3,
  64. ssl_connect_done
  65. } ssl_connect_state;
  66. typedef enum {
  67. ssl_connection_none,
  68. ssl_connection_negotiating,
  69. ssl_connection_complete
  70. } ssl_connection_state;
  71. #define CURL_SSL_IO_NEED_NONE (0)
  72. #define CURL_SSL_IO_NEED_RECV (1<<0)
  73. #define CURL_SSL_IO_NEED_SEND (1<<1)
  74. /* Information in each SSL cfilter context: cf->ctx */
  75. struct ssl_connect_data {
  76. struct ssl_peer peer;
  77. const struct alpn_spec *alpn; /* ALPN to use or NULL for none */
  78. void *backend; /* vtls backend specific props */
  79. struct cf_call_data call_data; /* data handle used in current call */
  80. struct curltime handshake_done; /* time when handshake finished */
  81. ssl_connection_state state;
  82. ssl_connect_state connecting_state;
  83. int io_need; /* TLS signals special SEND/RECV needs */
  84. BIT(use_alpn); /* if ALPN shall be used in handshake */
  85. BIT(peer_closed); /* peer has closed connection */
  86. };
  87. #undef CF_CTX_CALL_DATA
  88. #define CF_CTX_CALL_DATA(cf) \
  89. ((struct ssl_connect_data *)(cf)->ctx)->call_data
  90. /* Definitions for SSL Implementations */
  91. struct Curl_ssl {
  92. /*
  93. * This *must* be the first entry to allow returning the list of available
  94. * backends in curl_global_sslset().
  95. */
  96. curl_ssl_backend info;
  97. unsigned int supports; /* bitfield, see above */
  98. size_t sizeof_ssl_backend_data;
  99. int (*init)(void);
  100. void (*cleanup)(void);
  101. size_t (*version)(char *buffer, size_t size);
  102. int (*check_cxn)(struct Curl_cfilter *cf, struct Curl_easy *data);
  103. CURLcode (*shut_down)(struct Curl_cfilter *cf, struct Curl_easy *data,
  104. bool send_shutdown, bool *done);
  105. bool (*data_pending)(struct Curl_cfilter *cf,
  106. const struct Curl_easy *data);
  107. /* return 0 if a find random is filled in */
  108. CURLcode (*random)(struct Curl_easy *data, unsigned char *entropy,
  109. size_t length);
  110. bool (*cert_status_request)(void);
  111. CURLcode (*connect_blocking)(struct Curl_cfilter *cf,
  112. struct Curl_easy *data);
  113. CURLcode (*connect_nonblocking)(struct Curl_cfilter *cf,
  114. struct Curl_easy *data,
  115. bool *done);
  116. /* During handshake/shutdown, adjust the pollset to include the socket
  117. * for POLLOUT or POLLIN as needed. Mandatory. */
  118. void (*adjust_pollset)(struct Curl_cfilter *cf, struct Curl_easy *data,
  119. struct easy_pollset *ps);
  120. void *(*get_internals)(struct ssl_connect_data *connssl, CURLINFO info);
  121. void (*close)(struct Curl_cfilter *cf, struct Curl_easy *data);
  122. void (*close_all)(struct Curl_easy *data);
  123. CURLcode (*set_engine)(struct Curl_easy *data, const char *engine);
  124. CURLcode (*set_engine_default)(struct Curl_easy *data);
  125. struct curl_slist *(*engines_list)(struct Curl_easy *data);
  126. bool (*false_start)(void);
  127. CURLcode (*sha256sum)(const unsigned char *input, size_t inputlen,
  128. unsigned char *sha256sum, size_t sha256sumlen);
  129. bool (*attach_data)(struct Curl_cfilter *cf, struct Curl_easy *data);
  130. void (*detach_data)(struct Curl_cfilter *cf, struct Curl_easy *data);
  131. ssize_t (*recv_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
  132. char *buf, size_t len, CURLcode *code);
  133. ssize_t (*send_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
  134. const void *mem, size_t len, CURLcode *code);
  135. CURLcode (*get_channel_binding)(struct Curl_easy *data, int sockindex,
  136. struct dynbuf *binding);
  137. };
  138. extern const struct Curl_ssl *Curl_ssl;
  139. int Curl_none_init(void);
  140. void Curl_none_cleanup(void);
  141. CURLcode Curl_none_shutdown(struct Curl_cfilter *cf, struct Curl_easy *data,
  142. bool send_shutdown, bool *done);
  143. int Curl_none_check_cxn(struct Curl_cfilter *cf, struct Curl_easy *data);
  144. void Curl_none_close_all(struct Curl_easy *data);
  145. void Curl_none_session_free(void *ptr);
  146. bool Curl_none_data_pending(struct Curl_cfilter *cf,
  147. const struct Curl_easy *data);
  148. bool Curl_none_cert_status_request(void);
  149. CURLcode Curl_none_set_engine(struct Curl_easy *data, const char *engine);
  150. CURLcode Curl_none_set_engine_default(struct Curl_easy *data);
  151. struct curl_slist *Curl_none_engines_list(struct Curl_easy *data);
  152. bool Curl_none_false_start(void);
  153. void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data,
  154. struct easy_pollset *ps);
  155. /**
  156. * Get the SSL filter below the given one or NULL if there is none.
  157. */
  158. bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf);
  159. /* extract a session ID
  160. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  161. * Caller must make sure that the ownership of returned sessionid object
  162. * is properly taken (e.g. its refcount is incremented
  163. * under sessionid mutex).
  164. */
  165. bool Curl_ssl_getsessionid(struct Curl_cfilter *cf,
  166. struct Curl_easy *data,
  167. const struct ssl_peer *peer,
  168. void **ssl_sessionid,
  169. size_t *idsize); /* set 0 if unknown */
  170. /* Set a TLS session ID for `peer`. Replaces an existing session ID if
  171. * not already the very same.
  172. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  173. * Call takes ownership of `ssl_sessionid`, using `sessionid_free_cb`
  174. * to deallocate it. Is called in all outcomes, either right away or
  175. * later when the session cache is cleaned up.
  176. * Caller must ensure that it has properly shared ownership of this sessionid
  177. * object with cache (e.g. incrementing refcount on success)
  178. */
  179. CURLcode Curl_ssl_set_sessionid(struct Curl_cfilter *cf,
  180. struct Curl_easy *data,
  181. const struct ssl_peer *peer,
  182. void *sessionid,
  183. size_t sessionid_size,
  184. Curl_ssl_sessionid_dtor *sessionid_free_cb);
  185. #include "openssl.h" /* OpenSSL versions */
  186. #include "gtls.h" /* GnuTLS versions */
  187. #include "wolfssl.h" /* wolfSSL versions */
  188. #include "schannel.h" /* Schannel SSPI version */
  189. #include "sectransp.h" /* Secure Transport (Darwin) version */
  190. #include "mbedtls.h" /* mbedTLS versions */
  191. #include "bearssl.h" /* BearSSL versions */
  192. #include "rustls.h" /* Rustls versions */
  193. #endif /* USE_SSL */
  194. #endif /* HEADER_CURL_VTLS_INT_H */