| 1 |
|
| #ifndef FT_TLS_H |
| 2 |
|
| #define FT_TLS_H |
| 3 |
|
| |
| 4 |
|
| /* Client-side TLS for Fetch: a shared client SSL_CTX, SNI, optional peer + |
| 5 |
|
| * hostname verification, ALPN, and non-blocking handshake/read/write shims |
| 6 |
|
| * that turn OpenSSL's WANT_READ/WANT_WRITE into a readiness direction the loop |
| 7 |
|
| * can arm. Included from ft_http.h after the ft_conn struct is defined. |
| 8 |
|
| * |
| 9 |
|
| * When OpenSSL is absent (no -DFT_HAVE_OPENSSL) everything compiles to stubs |
| 10 |
|
| * and starting a TLS request fails cleanly. */ |
| 11 |
|
| |
| 12 |
|
| #ifdef FT_HAVE_OPENSSL |
| 13 |
|
| # include <openssl/opensslv.h> |
| 14 |
|
| #endif |
| 15 |
|
| |
| 16 |
|
| /* The real implementation uses the OpenSSL 1.1.0 API (TLS_client_method, |
| 17 |
|
| * SSL_CTX_set_min_proto_version, SSL_set1_host, SSL_set_hostflags). Older |
| 18 |
|
| * libraries - e.g. OpenSSL 1.0.x / 0.9.8 on legacy BSDs, or LibreSSL before |
| 19 |
|
| * 2.7 - lack those symbols, which would make the loadable object fail to load |
| 20 |
|
| * (undefined symbol). There TLS compiles to the same stubs as when OpenSSL is |
| 21 |
|
| * absent: the module still loads and cleartext HTTP works, and an https |
| 22 |
|
| * request fails cleanly. */ |
| 23 |
|
| #if defined(FT_HAVE_OPENSSL) \ |
| 24 |
|
| && OPENSSL_VERSION_NUMBER >= 0x10100000L \ |
| 25 |
|
| && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x2070000fL) |
| 26 |
|
| |
| 27 |
|
| #include <openssl/ssl.h> |
| 28 |
|
| #include <openssl/err.h> |
| 29 |
|
| #include <openssl/x509v3.h> |
| 30 |
|
| #include <limits.h> |
| 31 |
|
| |
| 32 |
|
| /* one shared CTX per verify mode, built lazily */ |
| 33 |
10 |
| static SSL_CTX *ft_client_ctx(int verify) { |
| 34 |
|
| static SSL_CTX *ctx_v = NULL, *ctx_n = NULL; |
| 35 |
10 |
▶ | SSL_CTX **slot = verify ? &ctx_v : &ctx_n; |
Branch: 1 branch
| Branch | True | False | Total |
| SSL_CTX **slot = verify ? &ctx_v : &ctx_n; |
2 |
8 |
10 |
|
| 36 |
10 |
▶ | if (!*slot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*slot) { |
4 |
6 |
10 |
|
| 37 |
4 |
| SSL_CTX *c = SSL_CTX_new(TLS_client_method()); |
| 38 |
4 |
▶ | if (!c) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) return NULL; |
0 |
4 |
4 |
|
| 39 |
4 |
| SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION); |
| 40 |
4 |
| SSL_CTX_set_options(c, SSL_OP_NO_COMPRESSION); |
| 41 |
4 |
▶ | if (verify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify) { |
2 |
2 |
4 |
|
| 42 |
2 |
| SSL_CTX_set_verify(c, SSL_VERIFY_PEER, NULL); |
| 43 |
2 |
| SSL_CTX_set_default_verify_paths(c); |
| 44 |
|
| } else { |
| 45 |
2 |
| SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL); |
| 46 |
|
| } |
| 47 |
4 |
| *slot = c; |
| 48 |
|
| } |
| 49 |
10 |
| return *slot; |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
|
| /* Attach a client SSL to c->fd: SNI, ALPN (http/1.1), optional hostname |
| 53 |
|
| * verification. Returns 0 on success, -1 on error. */ |
| 54 |
7 |
| static int ft_tls_start(ft_conn *c, const char *host, int verify) { |
| 55 |
7 |
| SSL_CTX *ctx = ft_client_ctx(verify); |
| 56 |
|
| SSL *ssl; |
| 57 |
|
| #ifdef FT_HAVE_NGHTTP2 |
| 58 |
|
| static const unsigned char alpn[] = { 2,'h','2', 8,'h','t','t','p','/','1','.','1' }; |
| 59 |
|
| #else |
| 60 |
|
| static const unsigned char alpn[] = { 8,'h','t','t','p','/','1','.','1' }; |
| 61 |
|
| #endif |
| 62 |
7 |
▶ | if (!ctx) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) return -1; |
0 |
7 |
7 |
|
| 63 |
7 |
| ssl = SSL_new(ctx); |
| 64 |
7 |
▶ | if (!ssl) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ssl) return -1; |
0 |
7 |
7 |
|
| 65 |
7 |
| SSL_set_fd(ssl, FT_SSL_FD(c->fd)); /* the real SOCKET on Windows */ |
| 66 |
7 |
| SSL_set_connect_state(ssl); |
| 67 |
|
| #ifdef SSL_set_tlsext_host_name |
| 68 |
7 |
| SSL_set_tlsext_host_name(ssl, host); /* SNI */ |
| 69 |
|
| #else |
| 70 |
|
| SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, |
| 71 |
|
| (void *)host); |
| 72 |
|
| #endif |
| 73 |
7 |
▶ | if (verify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify) { |
1 |
6 |
7 |
|
| 74 |
1 |
| SSL_set1_host(ssl, host); /* verify cert matches host */ |
| 75 |
1 |
| SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 76 |
|
| } |
| 77 |
7 |
| SSL_set_alpn_protos(ssl, alpn, sizeof(alpn)); |
| 78 |
7 |
| c->ssl = ssl; |
| 79 |
7 |
| return 0; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
|
| /* Drive the handshake. 1 = complete, 0 = need more (sets *want to HM_EV_READ |
| 83 |
|
| * or HM_EV_WRITE), -1 = error. */ |
| 84 |
14 |
| static int ft_tls_handshake(ft_conn *c, int *want) { |
| 85 |
|
| int r, e; |
| 86 |
14 |
| ERR_clear_error(); |
| 87 |
14 |
| r = SSL_connect((SSL *)c->ssl); |
| 88 |
14 |
▶ | if (r == 1) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 1) return 1; |
6 |
8 |
14 |
|
| 89 |
8 |
| e = SSL_get_error((SSL *)c->ssl, r); |
| 90 |
8 |
▶ | if (e == SSL_ERROR_WANT_READ) { *want = HM_EV_READ; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_READ) { *want = HM_EV_READ; return 0; } |
7 |
1 |
8 |
|
| 91 |
1 |
▶ | if (e == SSL_ERROR_WANT_WRITE) { *want = HM_EV_WRITE; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_WRITE) { *want = HM_EV_WRITE; return 0; } |
0 |
1 |
1 |
|
| 92 |
1 |
| return -1; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| /* Non-blocking TLS read/write. Return >0 bytes, 0 = clean close, -1 = error or |
| 96 |
|
| * would-block (errno==EAGAIN, *want set to the needed readiness). */ |
| 97 |
17 |
| static ssize_t ft_tls_read(ft_conn *c, void *buf, size_t n, int *want) { |
| 98 |
|
| int r, e; |
| 99 |
17 |
| ERR_clear_error(); |
| 100 |
17 |
▶ | r = SSL_read((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); |
Branch: 1 branch
| Branch | True | False | Total |
| r = SSL_read((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); |
17 |
0 |
17 |
|
| 101 |
17 |
▶ | if (r > 0) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r > 0) return r; |
6 |
11 |
17 |
|
| 102 |
11 |
| e = SSL_get_error((SSL *)c->ssl, r); |
| 103 |
11 |
▶ | if (e == SSL_ERROR_ZERO_RETURN) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_ZERO_RETURN) return 0; |
0 |
11 |
11 |
|
| 104 |
11 |
▶ | if (e == SSL_ERROR_WANT_READ) { *want = HM_EV_READ; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_READ) { *want = HM_EV_READ; errno = EAGAIN; return -1; } |
11 |
0 |
11 |
|
| 105 |
0 |
▶ | if (e == SSL_ERROR_WANT_WRITE) { *want = HM_EV_WRITE; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_WRITE) { *want = HM_EV_WRITE; errno = EAGAIN; return -1; } |
0 |
0 |
0 |
|
| 106 |
0 |
| errno = EIO; |
| 107 |
0 |
| return -1; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
6 |
| static ssize_t ft_tls_write(ft_conn *c, const void *buf, size_t n, int *want) { |
| 111 |
|
| int r, e; |
| 112 |
6 |
| ERR_clear_error(); |
| 113 |
6 |
▶ | r = SSL_write((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); |
Branch: 1 branch
| Branch | True | False | Total |
| r = SSL_write((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); |
6 |
0 |
6 |
|
| 114 |
6 |
▶ | if (r > 0) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r > 0) return r; |
6 |
0 |
6 |
|
| 115 |
0 |
| e = SSL_get_error((SSL *)c->ssl, r); |
| 116 |
0 |
▶ | if (e == SSL_ERROR_WANT_WRITE) { *want = HM_EV_WRITE; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_WRITE) { *want = HM_EV_WRITE; errno = EAGAIN; return -1; } |
0 |
0 |
0 |
|
| 117 |
0 |
▶ | if (e == SSL_ERROR_WANT_READ) { *want = HM_EV_READ; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_READ) { *want = HM_EV_READ; errno = EAGAIN; return -1; } |
0 |
0 |
0 |
|
| 118 |
0 |
| errno = EIO; |
| 119 |
0 |
| return -1; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* true if ALPN negotiated "h2" on this connection */ |
| 123 |
0 |
| static int ft_tls_is_h2(ft_conn *c) { |
| 124 |
0 |
| const unsigned char *proto = NULL; |
| 125 |
0 |
| unsigned int len = 0; |
| 126 |
0 |
▶ | if (!c->ssl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->ssl) return 0; |
0 |
0 |
0 |
|
| 127 |
0 |
| SSL_get0_alpn_selected((SSL *)c->ssl, &proto, &len); |
| 128 |
0 |
▶ | return len == 2 && proto && proto[0] == 'h' && proto[1] == '2'; |
Branch: 4 branches
| Branch | True | False | Total |
| return len == 2 && proto && proto[0] == 'h' && proto[1] == '2'; |
0 |
0 |
0 |
| return len == 2 && proto && proto[0] == 'h' && proto[1] == '2'; |
0 |
0 |
0 |
| return len == 2 && proto && proto[0] == 'h' && proto[1] == '2'; |
0 |
0 |
0 |
| return len == 2 && proto && proto[0] == 'h' && proto[1] == '2'; |
0 |
0 |
0 |
|
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
120 |
| static void ft_tls_free(ft_conn *c) { |
| 132 |
120 |
▶ | if (c->ssl) { SSL_free((SSL *)c->ssl); c->ssl = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->ssl) { SSL_free((SSL *)c->ssl); c->ssl = NULL; } |
7 |
113 |
120 |
|
| 133 |
120 |
| } |
| 134 |
|
| |
| 135 |
|
| #define FT_TLS_AVAILABLE 1 |
| 136 |
|
| |
| 137 |
|
| #else /* no OpenSSL, or too old for the 1.1.0 API: stubs */ |
| 138 |
|
| |
| 139 |
|
| static int ft_tls_is_h2(ft_conn *c) { (void)c; return 0; } |
| 140 |
|
| |
| 141 |
|
| static int ft_tls_start(ft_conn *c, const char *host, int v) { (void)c;(void)host;(void)v; return -1; } |
| 142 |
|
| static int ft_tls_handshake(ft_conn *c, int *w) { (void)c;(void)w; return -1; } |
| 143 |
|
| static ssize_t ft_tls_read(ft_conn *c, void *b, size_t n, int *w) { (void)c;(void)b;(void)n;(void)w; return -1; } |
| 144 |
|
| static ssize_t ft_tls_write(ft_conn *c, const void *b, size_t n, int *w) { (void)c;(void)b;(void)n;(void)w; return -1; } |
| 145 |
|
| static void ft_tls_free(ft_conn *c) { (void)c; } |
| 146 |
|
| |
| 147 |
|
| #define FT_TLS_AVAILABLE 0 |
| 148 |
|
| |
| 149 |
|
| #endif |
| 150 |
|
| |
| 151 |
|
| #endif /* FT_TLS_H */ |