| 1 |
|
| #ifndef HM_TLS_H |
| 2 |
|
| #define HM_TLS_H |
| 3 |
|
| |
| 4 |
|
| /* Upper bound on a session-ticket key, which is a 16-byte key name plus an |
| 5 |
|
| * HMAC key plus an AES key. The last two widened from 16 to 32 bytes in |
| 6 |
|
| * OpenSSL 3.0, so the total is 48 through 1.1.1 and 80 on 3.x - and passing |
| 7 |
|
| * the wrong length makes the ctrl a no-op that merely returns 0, with nothing |
| 8 |
|
| * logged. The real length is therefore asked of the library at runtime and |
| 9 |
|
| * this only has to bound it. Defined outside the HM_HAVE_OPENSSL fence |
| 10 |
|
| * because hm_core.h sizes a buffer with it either way. */ |
| 11 |
|
| #define HM_TLS_TKEY_MAX 128 |
| 12 |
|
| |
| 13 |
|
| /* TLS/HTTPS via OpenSSL. Enabled by Makefile.PL when OpenSSL is found |
| 14 |
|
| * (-DHM_HAVE_OPENSSL, -lssl -lcrypto); otherwise the entry points compile to |
| 15 |
|
| * stubs and tls_cert/tls_key error at runtime. |
| 16 |
|
| * |
| 17 |
|
| * Provides: the server SSL_CTX (certificate/key, ALPN, client-certificate |
| 18 |
|
| * verification, and SNI-based multi-certificate selection), the per-connection |
| 19 |
|
| * SSL wrap, the non-blocking read/write shims hm_cread/hm_cwrite (WANT_READ/ |
| 20 |
|
| * WANT_WRITE -> EAGAIN plus a cross-direction flag), peer-certificate capture, |
| 21 |
|
| * and the $env keys apps expect (HTTPS, SSL_CLIENT_*). The handshake driver |
| 22 |
|
| * (hm_tls_handshake) lives in hm_core.h. */ |
| 23 |
|
| |
| 24 |
|
| #ifdef HM_HAVE_OPENSSL |
| 25 |
|
| |
| 26 |
|
| #include <openssl/ssl.h> |
| 27 |
|
| #include <openssl/err.h> |
| 28 |
|
| #include <openssl/x509.h> |
| 29 |
|
| /* The session-ticket-key macros live here, not in ssl.h, and 3.x stopped |
| 30 |
|
| * pulling this in transitively - without it the HM_HAVE_TICKET_KEYS probe |
| 31 |
|
| * below silently fails and every reload rotates to an unshared key. */ |
| 32 |
|
| #include <openssl/tls1.h> |
| 33 |
|
| #include <errno.h> |
| 34 |
|
| #include <stdlib.h> |
| 35 |
|
| #include <string.h> |
| 36 |
|
| #include <strings.h> |
| 37 |
|
| |
| 38 |
|
| /* Pre-1.1.0 OpenSSL compatibility. Before 1.1.0 the library-init function, |
| 39 |
|
| * the version-agnostic method constructor, and the min-proto-version setter |
| 40 |
|
| * did not exist; init was explicit and TLS was selected via SSLv23_*_method |
| 41 |
|
| * with SSL_OP_NO_* to fence off the obsolete protocols. LibreSSL reports a |
| 42 |
|
| * >= 1.1.0 version number and ships these, so it takes the modern path. */ |
| 43 |
|
| #if OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 44 |
|
| static int OPENSSL_init_ssl(unsigned long opts, const void *settings) { |
| 45 |
|
| (void)opts; (void)settings; |
| 46 |
|
| SSL_library_init(); |
| 47 |
|
| SSL_load_error_strings(); |
| 48 |
|
| OpenSSL_add_all_algorithms(); |
| 49 |
|
| return 1; |
| 50 |
|
| } |
| 51 |
|
| #define OPENSSL_INIT_LOAD_SSL_STRINGS 0 |
| 52 |
|
| #define OPENSSL_INIT_LOAD_CRYPTO_STRINGS 0 |
| 53 |
|
| #define TLS_server_method SSLv23_server_method |
| 54 |
|
| /* emulate set_min_proto_version(TLS1_2) with SSL_OP_NO_* below; the setter |
| 55 |
|
| * itself is a no-op so hm_tls_ctx_one compiles unchanged. */ |
| 56 |
|
| #define SSL_CTX_set_min_proto_version(ctx, v) \ |
| 57 |
|
| SSL_CTX_set_options((ctx), SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 \ |
| 58 |
|
| | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1) |
| 59 |
|
| #endif |
| 60 |
|
| |
| 61 |
|
| /* SSL_get1_peer_certificate is the 3.0 spelling of SSL_get_peer_certificate. */ |
| 62 |
|
| #if OPENSSL_VERSION_NUMBER < 0x30000000L || defined(LIBRESSL_VERSION_NUMBER) |
| 63 |
|
| #define SSL_get1_peer_certificate SSL_get_peer_certificate |
| 64 |
|
| #endif |
| 65 |
|
| |
| 66 |
|
| /* ALPN (server-side protocol negotiation, needed to offer HTTP/2 over TLS) |
| 67 |
|
| * arrived in OpenSSL 1.0.2 - SSL_CTX_set_alpn_select_cb does not exist before |
| 68 |
|
| * that. On an older library we simply do not install the callback: h2-over-TLS |
| 69 |
|
| * is not offered and HTTPS falls back to HTTP/1.1. Without this guard the .so |
| 70 |
|
| * links an undefined symbol and fails to load (see CPAN Testers on OpenSSL |
| 71 |
|
| * 1.0.1 smokers). LibreSSL reports >= 1.0.2 and ships ALPN, so it takes this |
| 72 |
|
| * path too. */ |
| 73 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10002000L |
| 74 |
|
| #define HM_HAVE_ALPN 1 |
| 75 |
|
| #endif |
| 76 |
|
| |
| 77 |
|
| /* SSL_CTX_set_num_tickets is 1.1.1+ (it only means anything under TLS 1.3). |
| 78 |
|
| * LibreSSL reports a >= 1.1.1 version number but has not always shipped it, |
| 79 |
|
| * so it is excluded rather than probed. */ |
| 80 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 81 |
|
| #define HM_HAVE_NUM_TICKETS 1 |
| 82 |
|
| #endif |
| 83 |
|
| |
| 84 |
|
| /* TLS 1.3 itself: the version constant is 1.1.1+, the max-version setter |
| 85 |
|
| * 1.1.0+. LibreSSL ships both once it defines TLS1_3_VERSION, so the constant |
| 86 |
|
| * alone is the probe. Without it a build serves TLS 1.2 and no QUIC. */ |
| 87 |
|
| #ifdef TLS1_3_VERSION |
| 88 |
|
| #define HM_HAVE_TLS13 1 |
| 89 |
|
| #endif |
| 90 |
|
| |
| 91 |
|
| /* HTTP/3 is TLS 1.3 or nothing. The Makefile.PL probe links against this same |
| 92 |
|
| * library, so this cannot normally fire - it fires if HM_HAVE_HTTP3 is forced |
| 93 |
|
| * by hand, and says why rather than failing later inside ngtcp2. */ |
| 94 |
|
| #if defined(HM_HAVE_HTTP3) && !defined(HM_HAVE_TLS13) |
| 95 |
|
| #error "HM_HAVE_HTTP3 needs a TLS 1.3 OpenSSL (1.1.1+)" |
| 96 |
|
| #endif |
| 97 |
|
| |
| 98 |
|
| /* The ticket-key get/set pair are macros over SSL_CTX_ctrl, absent when the |
| 99 |
|
| * library was built with OPENSSL_NO_TLSEXT. */ |
| 100 |
|
| #ifdef SSL_CTX_set_tlsext_ticket_keys |
| 101 |
|
| #define HM_HAVE_TICKET_KEYS 1 |
| 102 |
|
| #endif |
| 103 |
|
| |
| 104 |
|
| /* client-cert verification modes */ |
| 105 |
|
| #define HM_TLS_VERIFY_NONE 0 |
| 106 |
|
| #define HM_TLS_VERIFY_OPTIONAL 1 |
| 107 |
|
| #define HM_TLS_VERIFY_REQUIRE 2 |
| 108 |
|
| |
| 109 |
|
| /* one SNI host -> its own SSL_CTX; the default ctx carries the registry in |
| 110 |
|
| * ex_data so its servername callback can switch. Hosts are stored lowercased |
| 111 |
|
| * and the array kept sorted, so the callback binary-searches with memcmp |
| 112 |
|
| * rather than walking every entry through strcasecmp - the map is one entry |
| 113 |
|
| * per certificate, and a multi-tenant gateway runs to hundreds. */ |
| 114 |
|
| typedef struct { char *host; size_t hlen; SSL_CTX *ctx; } hm_sni_entry; |
| 115 |
|
| typedef struct { hm_sni_entry *entries; int n; } hm_sni_registry; |
| 116 |
|
| |
| 117 |
|
| /* Only allocated when the client actually presented a certificate; the |
| 118 |
|
| * protocol/cipher strings are OpenSSL-owned and live directly on hm_conn. */ |
| 119 |
|
| typedef struct { |
| 120 |
|
| int verified; |
| 121 |
|
| char *subject; |
| 122 |
|
| char *issuer; |
| 123 |
|
| } hm_tls_peer; |
| 124 |
|
| |
| 125 |
|
| /* ASCII lowercase, in place or into dst (which may equal src). DNS names are |
| 126 |
|
| * ASCII, and tolower() would fold per the locale - in tr_TR 'I' does not go |
| 127 |
|
| * to 'i', which would make a hostname match depend on the server's LANG. */ |
| 128 |
58 |
| static void hm_tls_lc(char *dst, const char *src, size_t n) { |
| 129 |
|
| size_t i; |
| 130 |
653 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
595 |
58 |
653 |
|
| 131 |
595 |
| unsigned char ch = (unsigned char)src[i]; |
| 132 |
595 |
▶ | dst[i] = (char)(ch >= 'A' && ch <= 'Z' ? ch + ('a' - 'A') : ch); |
Branch: 2 branches
| Branch | True | False | Total |
| dst[i] = (char)(ch >= 'A' && ch <= 'Z' ? ch + ('a' - 'A') : ch); |
538 |
57 |
595 |
| dst[i] = (char)(ch >= 'A' && ch <= 'Z' ? ch + ('a' - 'A') : ch); |
9 |
529 |
538 |
|
| 133 |
|
| } |
| 134 |
58 |
| } |
| 135 |
|
| |
| 136 |
|
| /* Order by length first, then bytes: any total order works as long as the |
| 137 |
|
| * sort and the search agree, and comparing lengths up front skips most |
| 138 |
|
| * memcmps outright. */ |
| 139 |
49 |
| static int hm_sni_cmp(const void *a, const void *b) { |
| 140 |
49 |
| const hm_sni_entry *x = (const hm_sni_entry *)a; |
| 141 |
49 |
| const hm_sni_entry *y = (const hm_sni_entry *)b; |
| 142 |
49 |
▶ | if (x->hlen != y->hlen) return x->hlen < y->hlen ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x->hlen != y->hlen) return x->hlen < y->hlen ? -1 : 1; |
31 |
18 |
49 |
| if (x->hlen != y->hlen) return x->hlen < y->hlen ? -1 : 1; |
19 |
12 |
31 |
|
| 143 |
18 |
| return memcmp(x->host, y->host, x->hlen); |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| static int hm_tls_sni_ex_idx = -1; |
| 147 |
|
| |
| 148 |
|
| /* ALPN: prefer h2, fall back to http/1.1. Installed only when HTTP/2 is on and |
| 149 |
|
| * the OpenSSL in use provides ALPN (1.0.2+). */ |
| 150 |
|
| #ifdef HM_HAVE_ALPN |
| 151 |
|
| /* QUIC serves h3 and nothing else - there is no "fall back to HTTP/1.1 over |
| 152 |
|
| * QUIC" - so this is a second, shorter callback rather than a parameter on |
| 153 |
|
| * the TCP one, which is on the h2 path and should not move. */ |
| 154 |
|
| static const unsigned char HM_TLS_ALPN_H3[] = { 2, 'h', '3' }; |
| 155 |
|
| |
| 156 |
|
| /* |
| 157 |
|
| # uncoverable statement class:ignore_covered_err |
| 158 |
|
| # uncoverable subroutine class:ignore_covered_err */ |
| 159 |
0 |
| static int hm_tls_alpn_h3_cb(SSL *ssl, const unsigned char **out, |
| 160 |
|
| unsigned char *outlen, const unsigned char *in, |
| 161 |
|
| unsigned int inlen, void *arg) { |
| 162 |
|
| (void)ssl; (void)arg; |
| 163 |
|
| /* |
| 164 |
|
| # uncoverable branch true class:ignore_covered_err |
| 165 |
|
| # uncoverable statement class:ignore_covered_err |
| 166 |
|
| # uncoverable branch false class:ignore_covered_err |
| 167 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 168 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 169 |
0 |
▶ | if (SSL_select_next_proto((unsigned char **)out, outlen, |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_select_next_proto((unsigned char **)out, outlen, |
0 |
0 |
0 |
|
| 170 |
|
| HM_TLS_ALPN_H3, sizeof(HM_TLS_ALPN_H3), |
| 171 |
|
| in, inlen) != OPENSSL_NPN_NEGOTIATED) |
| 172 |
0 |
| return SSL_TLSEXT_ERR_ALERT_FATAL; /* # uncoverable statement */ |
| 173 |
0 |
| return SSL_TLSEXT_ERR_OK; /* # uncoverable statement class:ignore_covered_err */ |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
0 |
| static int hm_tls_alpn_cb(SSL *ssl, const unsigned char **out, |
| 177 |
|
| unsigned char *outlen, const unsigned char *in, |
| 178 |
|
| unsigned int inlen, void *arg) { |
| 179 |
|
| static const unsigned char pref[] = |
| 180 |
|
| { 2, 'h','2', 8, 'h','t','t','p','/','1','.','1' }; |
| 181 |
|
| (void)ssl; (void)arg; |
| 182 |
0 |
▶ | if (SSL_select_next_proto((unsigned char **)out, outlen, |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_select_next_proto((unsigned char **)out, outlen, |
0 |
0 |
0 |
|
| 183 |
|
| pref, sizeof(pref), in, inlen) |
| 184 |
|
| != OPENSSL_NPN_NEGOTIATED) |
| 185 |
0 |
| return SSL_TLSEXT_ERR_NOACK; |
| 186 |
0 |
| return SSL_TLSEXT_ERR_OK; |
| 187 |
|
| } |
| 188 |
|
| #endif |
| 189 |
|
| |
| 190 |
|
| /* optional-mode verify: accept regardless; the real result is read back from |
| 191 |
|
| * SSL_get_verify_result after the handshake and reported in $env. */ |
| 192 |
|
| /* |
| 193 |
|
| # uncoverable statement class:ignore_covered_err |
| 194 |
|
| # uncoverable subroutine class:ignore_covered_err */ |
| 195 |
0 |
| static int hm_tls_verify_cb(int ok, X509_STORE_CTX *ctx) { |
| 196 |
|
| (void)ok; (void)ctx; |
| 197 |
0 |
| return 1; /* # uncoverable statement class:ignore_covered_err */ |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
|
| /* SNI: switch to the matching host's SSL_CTX (case-insensitive). */ |
| 201 |
40 |
| static int hm_tls_sni_cb(SSL *ssl, int *ad, void *arg) { |
| 202 |
40 |
| SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); |
| 203 |
0 |
| hm_sni_registry *reg = (hm_sni_registry *) /* # uncoverable statement class:ignore_covered_err */ |
| 204 |
40 |
▶ | (hm_tls_sni_ex_idx >= 0 ? SSL_CTX_get_ex_data(ctx, hm_tls_sni_ex_idx) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| (hm_tls_sni_ex_idx >= 0 ? SSL_CTX_get_ex_data(ctx, hm_tls_sni_ex_idx) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
40 |
0 |
40 |
|
| 205 |
40 |
| const char *host = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 206 |
|
| (void)ad; (void)arg; |
| 207 |
|
| /* |
| 208 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 209 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 210 |
40 |
▶ | if (host && reg && reg->n > 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (host && reg && reg->n > 0) { |
32 |
8 |
40 |
| if (host && reg && reg->n > 0) { |
32 |
0 |
32 |
| if (host && reg && reg->n > 0) { |
32 |
0 |
32 |
|
| 211 |
|
| char lc[256]; /* a DNS name is at most 253 bytes */ |
| 212 |
32 |
| size_t hl = strlen(host); |
| 213 |
|
| /* |
| 214 |
|
| # uncoverable branch false class:ignore_covered_err |
| 215 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 216 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 217 |
32 |
▶ | if (hl > 0 && hl < sizeof(lc)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (hl > 0 && hl < sizeof(lc)) { |
32 |
0 |
32 |
| if (hl > 0 && hl < sizeof(lc)) { |
32 |
0 |
32 |
|
| 218 |
32 |
| int lo = 0, hi = reg->n - 1; |
| 219 |
32 |
| hm_tls_lc(lc, host, hl); |
| 220 |
78 |
▶ | while (lo <= hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo <= hi) { |
74 |
4 |
78 |
|
| 221 |
74 |
| int mid = lo + (hi - lo) / 2; |
| 222 |
74 |
| hm_sni_entry *e = ®->entries[mid]; |
| 223 |
28 |
▶ | int cmp = hl != e->hlen ? (hl < e->hlen ? -1 : 1) |
Branch: 1 branch
| Branch | True | False | Total |
| int cmp = hl != e->hlen ? (hl < e->hlen ? -1 : 1) |
10 |
18 |
28 |
|
| 224 |
102 |
▶ | : memcmp(lc, e->host, hl); |
Branch: 1 branch
| Branch | True | False | Total |
| : memcmp(lc, e->host, hl); |
28 |
46 |
74 |
|
| 225 |
74 |
▶ | if (cmp == 0) { SSL_set_SSL_CTX(ssl, e->ctx); break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cmp == 0) { SSL_set_SSL_CTX(ssl, e->ctx); break; } |
28 |
46 |
74 |
|
| 226 |
46 |
▶ | if (cmp < 0) hi = mid - 1; else lo = mid + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cmp < 0) hi = mid - 1; else lo = mid + 1; |
22 |
24 |
46 |
|
| 227 |
|
| } |
| 228 |
|
| } |
| 229 |
|
| } |
| 230 |
40 |
| return SSL_TLSEXT_ERR_OK; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
14 |
| static void hm_tls_init(void) { |
| 234 |
|
| static int inited = 0; |
| 235 |
14 |
▶ | if (!inited) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!inited) { |
11 |
3 |
14 |
|
| 236 |
11 |
| OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS |
| 237 |
|
| | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
| 238 |
11 |
| hm_tls_sni_ex_idx = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 239 |
11 |
| inited = 1; |
| 240 |
|
| } |
| 241 |
14 |
| } |
| 242 |
|
| |
| 243 |
|
| /* Build and configure one server SSL_CTX from a cert/key pair, with optional |
| 244 |
|
| * ALPN and client-cert verification. tkey, when given, is a 48-byte |
| 245 |
|
| * session-ticket key to install instead of the random one SSL_CTX_new mints |
| 246 |
|
| * (see hm_tls_ctx_build). Returns NULL (reason on stderr) on error. */ |
| 247 |
|
| /* HM_TLSCTX_QUIC: build the context QUIC needs instead of the TCP one. It is |
| 248 |
|
| * a flag rather than a second builder because everything that matters is the |
| 249 |
|
| * same - the certificate, the chain, the CA and client-cert verification, and |
| 250 |
|
| * above all the SESSION TICKET KEY. A QUIC context carrying a different key |
| 251 |
|
| * from its TCP sibling silently degrades cross-protocol resumption to full |
| 252 |
|
| * handshakes, which is the identical bug the SNI note below describes. */ |
| 253 |
|
| #define HM_TLSCTX_QUIC 1 |
| 254 |
|
| |
| 255 |
41 |
| static SSL_CTX *hm_tls_ctx_one(const char *cert, const char *key, |
| 256 |
|
| const char *ca, int verify, int alpn_h2, |
| 257 |
|
| const unsigned char *tkey, size_t tkeylen, |
| 258 |
|
| int flags) { |
| 259 |
41 |
| const int quic = (flags & HM_TLSCTX_QUIC) != 0; |
| 260 |
|
| /* Any fixed non-empty string will do: it only has to be stable across the |
| 261 |
|
| * contexts a client might resume against, which one constant guarantees. */ |
| 262 |
|
| static const unsigned char sid[] = "hyperman"; |
| 263 |
41 |
| SSL_CTX *ctx = SSL_CTX_new(TLS_server_method()); |
| 264 |
|
| /* |
| 265 |
|
| # uncoverable branch true |
| 266 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 267 |
41 |
▶ | if (!ctx) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) return NULL; |
0 |
41 |
41 |
|
| 268 |
|
| /* QUIC is TLS 1.3 only - there is no QUIC over 1.2 - and pinning the max |
| 269 |
|
| * as well as the min says so rather than leaving it to negotiation. |
| 270 |
|
| * TLS1_3_VERSION and the max-version setter are 1.1.1 and 1.1.0; a library |
| 271 |
|
| * with neither cannot drive ngtcp2, so HM_HAVE_HTTP3 is never set against |
| 272 |
|
| * one and no QUIC context is ever built there. The pinning is therefore |
| 273 |
|
| * compiled out rather than emulated, leaving the TLS 1.2 floor the TCP |
| 274 |
|
| * listener has always had. */ |
| 275 |
|
| #ifdef HM_HAVE_TLS13 |
| 276 |
41 |
▶ | SSL_CTX_set_min_proto_version(ctx, quic ? TLS1_3_VERSION : TLS1_2_VERSION); /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SSL_CTX_set_min_proto_version(ctx, quic ? TLS1_3_VERSION : TLS1_2_VERSION); /* # uncoverable condition left count:1 class:ignore_covered_err */ |
0 |
41 |
41 |
|
| 277 |
|
| /* |
| 278 |
|
| # uncoverable branch true class:ignore_covered_err |
| 279 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 280 |
41 |
▶ | if (quic) SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
Branch: 1 branch
| Branch | True | False | Total |
| if (quic) SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
0 |
41 |
41 |
|
| 281 |
|
| #else |
| 282 |
|
| SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 283 |
|
| #endif |
| 284 |
41 |
| SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION | SSL_OP_CIPHER_SERVER_PREFERENCE |
| 285 |
|
| #ifdef HM_HAVE_KTLS |
| 286 |
|
| /* Ask for the kernel record layer. OpenSSL only |
| 287 |
|
| * takes it up if the kernel has the tls ULP and |
| 288 |
|
| * the negotiated cipher is one it can offload; |
| 289 |
|
| * otherwise this is inert and every connection |
| 290 |
|
| * encrypts in userspace exactly as before. */ |
| 291 |
|
| | SSL_OP_ENABLE_KTLS |
| 292 |
|
| #endif |
| 293 |
|
| #ifdef SSL_OP_NO_RENEGOTIATION |
| 294 |
|
| /* client-initiated TLS 1.2 renegotiation is an |
| 295 |
|
| * asymmetric-CPU DoS and nothing here wants it; |
| 296 |
|
| * refusing it also retires the only case that |
| 297 |
|
| * makes SSL_write block wanting a read */ |
| 298 |
|
| | SSL_OP_NO_RENEGOTIATION |
| 299 |
|
| #endif |
| 300 |
|
| ); |
| 301 |
|
| #ifdef HM_HAVE_KTLS |
| 302 |
|
| /* There is no kernel record layer for QUIC: ngtcp2 encrypts every packet |
| 303 |
|
| * itself, so asking for kTLS here is at best inert and at worst a |
| 304 |
|
| * handshake OpenSSL sets up and nothing uses. */ |
| 305 |
|
| /* |
| 306 |
|
| # uncoverable branch true |
| 307 |
|
| # uncoverable condition left count:1 */ |
| 308 |
41 |
▶ | if (quic) SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_KTLS); |
Branch: 1 branch
| Branch | True | False | Total |
| if (quic) SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_KTLS); |
0 |
41 |
41 |
|
| 309 |
|
| #endif |
| 310 |
|
| /* PARTIAL_WRITE is about SSL_write, which a QUIC context never calls. */ |
| 311 |
|
| /* |
| 312 |
|
| # uncoverable branch false class:ignore_covered_err |
| 313 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 314 |
41 |
▶ | if (!quic) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!quic) |
41 |
0 |
41 |
|
| 315 |
41 |
| SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
| 316 |
|
| | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
| 317 |
|
| #ifdef SSL_MODE_RELEASE_BUFFERS |
| 318 |
|
| /* hand OpenSSL's record buffers back whenever they |
| 319 |
|
| * drain, rather than pinning them for a |
| 320 |
|
| * connection's whole life. A keep-alive server is |
| 321 |
|
| * mostly idle connections, which is exactly the |
| 322 |
|
| * case this covers. How much it saves depends on |
| 323 |
|
| * the OpenSSL version's buffer sizing and on the |
| 324 |
|
| * allocator actually returning the pages - worth |
| 325 |
|
| * measuring on the deployment platform (Linux |
| 326 |
|
| * /proc/PID/status VmRSS; macOS RSS is far too |
| 327 |
|
| * noisy to read anything off). */ |
| 328 |
|
| | SSL_MODE_RELEASE_BUFFERS |
| 329 |
|
| #endif |
| 330 |
|
| ); |
| 331 |
|
| /* Without a session id context OpenSSL declines to resume a session on a |
| 332 |
|
| * context that verifies peers, so every mTLS connection would pay a full |
| 333 |
|
| * handshake plus a full chain verification. */ |
| 334 |
41 |
| SSL_CTX_set_session_id_context(ctx, sid, sizeof(sid) - 1); |
| 335 |
|
| #ifdef HM_HAVE_NUM_TICKETS |
| 336 |
|
| /* TLS 1.3 issues two NewSessionTickets per handshake by default; one is |
| 337 |
|
| * enough to resume with, and the second is pure post-handshake write. */ |
| 338 |
41 |
| SSL_CTX_set_num_tickets(ctx, 1); |
| 339 |
|
| #endif |
| 340 |
|
| #ifdef HM_HAVE_TICKET_KEYS |
| 341 |
|
| /* copied rather than cast: the setter takes a non-const void * */ |
| 342 |
|
| /* |
| 343 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 344 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 345 |
41 |
▶ | if (tkey && tkeylen && tkeylen <= HM_TLS_TKEY_MAX) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (tkey && tkeylen && tkeylen <= HM_TLS_TKEY_MAX) { |
30 |
11 |
41 |
| if (tkey && tkeylen && tkeylen <= HM_TLS_TKEY_MAX) { |
30 |
0 |
30 |
| if (tkey && tkeylen && tkeylen <= HM_TLS_TKEY_MAX) { |
30 |
0 |
30 |
|
| 346 |
|
| unsigned char tk[HM_TLS_TKEY_MAX]; |
| 347 |
30 |
| memcpy(tk, tkey, tkeylen); |
| 348 |
30 |
| SSL_CTX_set_tlsext_ticket_keys(ctx, tk, (long)tkeylen); |
| 349 |
|
| } |
| 350 |
|
| #else |
| 351 |
|
| (void)tkey; (void)tkeylen; |
| 352 |
|
| #endif |
| 353 |
41 |
▶ | if (SSL_CTX_use_certificate_chain_file(ctx, cert) <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_use_certificate_chain_file(ctx, cert) <= 0) { |
1 |
40 |
41 |
|
| 354 |
1 |
| hm_errlog("Hyperman TLS: cannot load certificate '%s'\n", cert); |
| 355 |
1 |
| ERR_print_errors_fp(stderr); SSL_CTX_free(ctx); return NULL; |
| 356 |
|
| } |
| 357 |
40 |
▶ | if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0) { |
2 |
38 |
40 |
|
| 358 |
2 |
| hm_errlog("Hyperman TLS: cannot load private key '%s'\n", key); |
| 359 |
2 |
| ERR_print_errors_fp(stderr); SSL_CTX_free(ctx); return NULL; |
| 360 |
|
| } |
| 361 |
|
| /* |
| 362 |
|
| # uncoverable branch true |
| 363 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 364 |
38 |
▶ | if (!SSL_CTX_check_private_key(ctx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SSL_CTX_check_private_key(ctx)) { |
0 |
38 |
38 |
|
| 365 |
0 |
| hm_errlog("Hyperman TLS: certificate and key do not match\n"); /* # uncoverable statement */ |
| 366 |
0 |
| SSL_CTX_free(ctx); return NULL; /* # uncoverable statement */ |
| 367 |
|
| } |
| 368 |
|
| #ifdef HM_HAVE_ALPN |
| 369 |
|
| /* h3 only on a QUIC context: offering h2 or http/1.1 over QUIC is a |
| 370 |
|
| * protocol error. The TCP list is untouched - h3 is discovered through |
| 371 |
|
| * Alt-Svc, never through ALPN over TCP. */ |
| 372 |
|
| /* |
| 373 |
|
| # uncoverable branch true class:ignore_covered_err |
| 374 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 375 |
38 |
▶ | if (quic) SSL_CTX_set_alpn_select_cb(ctx, hm_tls_alpn_h3_cb, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (quic) SSL_CTX_set_alpn_select_cb(ctx, hm_tls_alpn_h3_cb, NULL); |
0 |
38 |
38 |
|
| 376 |
38 |
▶ | else if (alpn_h2) SSL_CTX_set_alpn_select_cb(ctx, hm_tls_alpn_cb, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (alpn_h2) SSL_CTX_set_alpn_select_cb(ctx, hm_tls_alpn_cb, NULL); |
0 |
38 |
38 |
|
| 377 |
|
| #else |
| 378 |
|
| (void)alpn_h2; /* ALPN unavailable on this OpenSSL; h2-over-TLS not offered */ |
| 379 |
|
| #endif |
| 380 |
|
| /* 0-RTT refused explicitly rather than left at a default: accepting it |
| 381 |
|
| * means accepting replayable application data, and that is a decision |
| 382 |
|
| * this server has not made. The setter is 1.1.1+, and a library without |
| 383 |
|
| * it has no 0-RTT to refuse - nor any QUIC context to refuse it on. */ |
| 384 |
|
| #ifdef HM_HAVE_TLS13 |
| 385 |
|
| /* |
| 386 |
|
| # uncoverable branch true class:ignore_covered_err |
| 387 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 388 |
38 |
▶ | if (quic) SSL_CTX_set_max_early_data(ctx, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (quic) SSL_CTX_set_max_early_data(ctx, 0); |
0 |
38 |
38 |
|
| 389 |
|
| #endif |
| 390 |
38 |
▶ | if (verify != HM_TLS_VERIFY_NONE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify != HM_TLS_VERIFY_NONE) { |
2 |
36 |
38 |
|
| 391 |
2 |
| int flags = SSL_VERIFY_PEER; |
| 392 |
2 |
▶ | if (ca && SSL_CTX_load_verify_locations(ctx, ca, NULL) <= 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ca && SSL_CTX_load_verify_locations(ctx, ca, NULL) <= 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2 |
0 |
2 |
| if (ca && SSL_CTX_load_verify_locations(ctx, ca, NULL) <= 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1 |
1 |
2 |
|
| 393 |
1 |
| hm_errlog("Hyperman TLS: cannot load CA '%s'\n", ca); |
| 394 |
1 |
| ERR_print_errors_fp(stderr); SSL_CTX_free(ctx); return NULL; |
| 395 |
|
| } |
| 396 |
|
| /* |
| 397 |
|
| # uncoverable branch false class:ignore_covered_err |
| 398 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 399 |
1 |
▶ | if (ca) { /* tell the client which CAs we accept */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ca) { /* tell the client which CAs we accept */ |
1 |
0 |
1 |
|
| 400 |
1 |
| STACK_OF(X509_NAME) *names = SSL_load_client_CA_file(ca); |
| 401 |
|
| /* |
| 402 |
|
| # uncoverable branch false class:ignore_covered_err |
| 403 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 404 |
1 |
▶ | if (names) SSL_CTX_set_client_CA_list(ctx, names); |
Branch: 1 branch
| Branch | True | False | Total |
| if (names) SSL_CTX_set_client_CA_list(ctx, names); |
1 |
0 |
1 |
|
| 405 |
|
| } |
| 406 |
|
| /* |
| 407 |
|
| # uncoverable branch false class:ignore_covered_err |
| 408 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 409 |
1 |
▶ | if (verify == HM_TLS_VERIFY_REQUIRE) flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify == HM_TLS_VERIFY_REQUIRE) flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
1 |
0 |
1 |
|
| 410 |
1 |
▶ | SSL_CTX_set_verify(ctx, flags, |
Branch: 1 branch
| Branch | True | False | Total |
| SSL_CTX_set_verify(ctx, flags, |
0 |
1 |
1 |
|
| 411 |
|
| verify == HM_TLS_VERIFY_OPTIONAL ? hm_tls_verify_cb : NULL); |
| 412 |
|
| } |
| 413 |
37 |
| return ctx; |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
|
| /* Copy a context's session-ticket key into buf (at most HM_TLS_TKEY_MAX |
| 417 |
|
| * bytes) and report its length. The size is asked of the library rather than |
| 418 |
|
| * assumed - it differs between 1.1.1 and 3.x, and the getter answers a |
| 419 |
|
| * mismatched length with a plain 0. Returns 1 on success, 0 if the library |
| 420 |
|
| * cannot report one (built without TLSEXT, or TLS not compiled in here). */ |
| 421 |
11 |
| static int hm_tls_get_ticket_key(void *ctxv, unsigned char *buf, size_t *len) { |
| 422 |
|
| #ifdef HM_HAVE_TICKET_KEYS |
| 423 |
|
| long need; |
| 424 |
|
| /* |
| 425 |
|
| # uncoverable branch true |
| 426 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 427 |
11 |
▶ | if (!ctxv) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctxv) return 0; |
0 |
11 |
11 |
|
| 428 |
11 |
| need = SSL_CTX_ctrl((SSL_CTX *)ctxv, SSL_CTRL_GET_TLSEXT_TICKET_KEYS, |
| 429 |
|
| 0, NULL); /* 0/NULL asks the size */ |
| 430 |
|
| /* |
| 431 |
|
| # uncoverable branch true class:ignore_covered_err |
| 432 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 433 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 434 |
11 |
▶ | if (need <= 0 || (size_t)need > HM_TLS_TKEY_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (need <= 0 || (size_t)need > HM_TLS_TKEY_MAX) return 0; |
11 |
0 |
11 |
| if (need <= 0 || (size_t)need > HM_TLS_TKEY_MAX) return 0; |
0 |
11 |
11 |
|
| 435 |
|
| /* |
| 436 |
|
| # uncoverable branch true |
| 437 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 438 |
11 |
▶ | if (SSL_CTX_get_tlsext_ticket_keys((SSL_CTX *)ctxv, buf, need) != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_get_tlsext_ticket_keys((SSL_CTX *)ctxv, buf, need) != 1) |
0 |
11 |
11 |
|
| 439 |
0 |
| return 0; /* # uncoverable statement */ |
| 440 |
11 |
| *len = (size_t)need; |
| 441 |
11 |
| return 1; |
| 442 |
|
| #else |
| 443 |
|
| (void)ctxv; (void)buf; (void)len; |
| 444 |
|
| return 0; |
| 445 |
|
| #endif |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
|
| /* Build the default SSL_CTX plus any SNI per-host contexts (sni_hv: |
| 449 |
|
| * { host => { cert => ..., key => ... }, ... }). The registry is attached to |
| 450 |
|
| * the default ctx via ex_data and the servername callback installed. Returns |
| 451 |
|
| * the default ctx or NULL. |
| 452 |
|
| * |
| 453 |
|
| * tkey, when given, is the session-ticket key to install across every context |
| 454 |
|
| * built here; otherwise the default context's own random key is read back and |
| 455 |
|
| * shared with the per-host ones. Either way all of them end up with the SAME |
| 456 |
|
| * key, which is what makes a ticket usable: SSL_CTX_new mints an independent |
| 457 |
|
| * random key per context, so left alone a ticket issued while one certificate |
| 458 |
|
| * was in effect would not decrypt on the context that a later handshake |
| 459 |
|
| * selects, and the client would silently fall back to a full handshake. |
| 460 |
|
| * hm_tls_reload passes the running context's key in for the same reason |
| 461 |
|
| * across processes - it runs per worker, so each would otherwise rotate to a |
| 462 |
|
| * key its siblings cannot read. */ |
| 463 |
|
| /* The QUIC context for a listener, built through hm_tls_ctx_one so it shares |
| 464 |
|
| * the certificate, the verification policy and - the one that bites quietly - |
| 465 |
|
| * the SESSION TICKET KEY with its TCP sibling. |
| 466 |
|
| * |
| 467 |
|
| * `tkey` is the key already in use on that sibling. Pass it, always: a QUIC |
| 468 |
|
| * context left to mint its own means a client that resumes across protocols |
| 469 |
|
| * gets a full handshake and nothing reports it. */ |
| 470 |
|
| /* |
| 471 |
|
| # uncoverable statement class:ignore_covered_err |
| 472 |
|
| # uncoverable subroutine class:ignore_covered_err */ |
| 473 |
0 |
| static void *hm_tls_quic_ctx_build(const char *cert, const char *key, |
| 474 |
|
| const char *ca, int verify, |
| 475 |
|
| const unsigned char *tkey, size_t tkeylen) { |
| 476 |
0 |
| hm_tls_init(); /* # uncoverable statement class:ignore_covered_err */ |
| 477 |
0 |
| return (void *)hm_tls_ctx_one(cert, key, ca, verify, 0, tkey, tkeylen, /* # uncoverable statement class:ignore_covered_err */ |
| 478 |
|
| HM_TLSCTX_QUIC); |
| 479 |
|
| } |
| 480 |
|
| |
| 481 |
|
| /* The session-ticket key a built context is using, so the QUIC context can be |
| 482 |
|
| * given the same one. */ |
| 483 |
|
| /* |
| 484 |
|
| # uncoverable statement class:ignore_covered_err |
| 485 |
|
| # uncoverable subroutine class:ignore_covered_err */ |
| 486 |
0 |
| static int hm_tls_ctx_tkey(void *ctx, unsigned char *out, size_t *len) { |
| 487 |
0 |
| return hm_tls_get_ticket_key((SSL_CTX *)ctx, out, len); /* # uncoverable statement class:ignore_covered_err */ |
| 488 |
|
| } |
| 489 |
|
| |
| 490 |
14 |
| static void *hm_tls_ctx_build(pTHX_ const char *cert, const char *key, |
| 491 |
|
| const char *ca, int verify, |
| 492 |
|
| SV *sni_hv, int alpn_h2, |
| 493 |
|
| const unsigned char *tkey, size_t tkeylen) { |
| 494 |
|
| SSL_CTX *def; |
| 495 |
|
| unsigned char kbuf[HM_TLS_TKEY_MAX]; |
| 496 |
14 |
| hm_tls_init(); |
| 497 |
14 |
| def = hm_tls_ctx_one(cert, key, ca, verify, alpn_h2, tkey, tkeylen, 0); |
| 498 |
14 |
▶ | if (!def) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!def) return NULL; |
3 |
11 |
14 |
|
| 499 |
11 |
▶ | if (!tkey && hm_tls_get_ticket_key(def, kbuf, &tkeylen)) tkey = kbuf; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tkey && hm_tls_get_ticket_key(def, kbuf, &tkeylen)) tkey = kbuf; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
8 |
3 |
11 |
| if (!tkey && hm_tls_get_ticket_key(def, kbuf, &tkeylen)) tkey = kbuf; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
8 |
0 |
8 |
|
| 500 |
|
| |
| 501 |
|
| /* |
| 502 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 503 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 504 |
11 |
▶ | if (sni_hv && SvROK(sni_hv) && SvTYPE(SvRV(sni_hv)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (sni_hv && SvROK(sni_hv) && SvTYPE(SvRV(sni_hv)) == SVt_PVHV) { |
7 |
4 |
11 |
| if (sni_hv && SvROK(sni_hv) && SvTYPE(SvRV(sni_hv)) == SVt_PVHV) { |
7 |
0 |
7 |
| if (sni_hv && SvROK(sni_hv) && SvTYPE(SvRV(sni_hv)) == SVt_PVHV) { |
7 |
0 |
7 |
|
| 505 |
7 |
| HV *hv = (HV *)SvRV(sni_hv); |
| 506 |
7 |
| I32 n = hv_iterinit(hv), i = 0; |
| 507 |
7 |
| hm_sni_registry *reg = (hm_sni_registry *)hm_xcalloc(1, sizeof(hm_sni_registry)); |
| 508 |
|
| HE *he; |
| 509 |
7 |
| reg->entries = (hm_sni_entry *)hm_xcalloc(n > 0 ? n : 1, sizeof(hm_sni_entry)); |
| 510 |
36 |
▶ | while ((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hv))) { |
29 |
7 |
36 |
|
| 511 |
29 |
| I32 klen; char *host = hv_iterkey(he, &klen); |
| 512 |
29 |
| SV *val = hv_iterval(hv, he); |
| 513 |
29 |
| const char *hc = NULL, *hk = NULL; |
| 514 |
|
| SSL_CTX *hctx; |
| 515 |
|
| char *lchost; |
| 516 |
|
| /* |
| 517 |
|
| # uncoverable branch true class:ignore_covered_err |
| 518 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 519 |
32 |
▶ | if (klen <= 0) continue; /* a key we cannot match on */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (klen <= 0) continue; /* a key we cannot match on */ |
0 |
29 |
29 |
|
| 520 |
|
| /* |
| 521 |
|
| # uncoverable branch false class:ignore_covered_err |
| 522 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 523 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 524 |
29 |
▶ | if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { |
29 |
0 |
29 |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { |
29 |
0 |
29 |
|
| 525 |
29 |
| HV *e = (HV *)SvRV(val); |
| 526 |
29 |
| SV **c = hv_fetchs(e, "cert", 0); |
| 527 |
29 |
| SV **k = hv_fetchs(e, "key", 0); |
| 528 |
57 |
▶ | if (c && SvOK(*c)) hc = SvPV_nolen(*c); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (c && SvOK(*c)) hc = SvPV_nolen(*c); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
28 |
1 |
29 |
| if (c && SvOK(*c)) hc = SvPV_nolen(*c); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
28 |
0 |
28 |
|
| 529 |
57 |
▶ | if (k && SvOK(*k)) hk = SvPV_nolen(*k); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (k && SvOK(*k)) hk = SvPV_nolen(*k); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
28 |
1 |
29 |
| if (k && SvOK(*k)) hk = SvPV_nolen(*k); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
28 |
0 |
28 |
|
| 530 |
|
| } |
| 531 |
29 |
▶ | if (!hc || !hk) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hc || !hk) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
28 |
1 |
29 |
| if (!hc || !hk) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
28 |
28 |
|
| 532 |
1 |
| hm_errlog("Hyperman TLS: SNI host '%s' needs cert and key\n", host); |
| 533 |
1 |
| continue; |
| 534 |
|
| } |
| 535 |
28 |
▶ | if (strlen(host) != (size_t)klen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strlen(host) != (size_t)klen) { |
1 |
27 |
28 |
|
| 536 |
|
| /* a NUL inside the key: strdup stops at it and the fold |
| 537 |
|
| * below would write klen bytes past the copy */ |
| 538 |
1 |
| hm_errlog("Hyperman TLS: SNI host key carries a NUL; skipped\n"); |
| 539 |
1 |
| continue; |
| 540 |
|
| } |
| 541 |
27 |
| hctx = hm_tls_ctx_one(hc, hk, ca, verify, alpn_h2, tkey, tkeylen, 0); |
| 542 |
27 |
▶ | if (!hctx) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hctx) continue; |
1 |
26 |
27 |
|
| 543 |
26 |
| lchost = strdup(host); |
| 544 |
|
| /* |
| 545 |
|
| # uncoverable branch true |
| 546 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 547 |
26 |
▶ | if (!lchost) { SSL_CTX_free(hctx); continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lchost) { SSL_CTX_free(hctx); continue; } |
0 |
26 |
26 |
|
| 548 |
26 |
| hm_tls_lc(lchost, lchost, (size_t)klen); |
| 549 |
26 |
| reg->entries[i].host = lchost; |
| 550 |
26 |
| reg->entries[i].hlen = (size_t)klen; |
| 551 |
26 |
| reg->entries[i].ctx = hctx; |
| 552 |
26 |
| i++; |
| 553 |
|
| } |
| 554 |
7 |
| reg->n = i; |
| 555 |
7 |
▶ | if (i > 1) qsort(reg->entries, (size_t)i, sizeof(hm_sni_entry), hm_sni_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > 1) qsort(reg->entries, (size_t)i, sizeof(hm_sni_entry), hm_sni_cmp); |
3 |
4 |
7 |
|
| 556 |
7 |
| SSL_CTX_set_ex_data(def, hm_tls_sni_ex_idx, reg); |
| 557 |
7 |
| SSL_CTX_set_tlsext_servername_callback(def, hm_tls_sni_cb); |
| 558 |
|
| } |
| 559 |
11 |
| return def; |
| 560 |
|
| } |
| 561 |
|
| |
| 562 |
|
| /* How many SNI hosts a built context actually carries. -1 when it has no |
| 563 |
|
| * registry at all (a plain default with no SNI map). |
| 564 |
|
| * |
| 565 |
|
| * hm_tls_ctx_build SKIPS a host whose certificate will not load and |
| 566 |
|
| * still returns a usable default, which is right at boot - one bad PEM |
| 567 |
|
| * should not stop a server starting. It is wrong for a reload, where |
| 568 |
|
| * the same behaviour silently drops every domain that failed down to |
| 569 |
|
| * the fallback certificate. A reload compares this against what it |
| 570 |
|
| * asked for and refuses to install a context that carries fewer. */ |
| 571 |
4 |
| static int hm_tls_sni_count(void *ctxv) { |
| 572 |
|
| hm_sni_registry *reg; |
| 573 |
|
| /* |
| 574 |
|
| # uncoverable branch true class:ignore_covered_err |
| 575 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 576 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 577 |
4 |
▶ | if (!ctxv || hm_tls_sni_ex_idx < 0) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ctxv || hm_tls_sni_ex_idx < 0) return -1; |
4 |
0 |
4 |
| if (!ctxv || hm_tls_sni_ex_idx < 0) return -1; |
0 |
4 |
4 |
|
| 578 |
4 |
| reg = (hm_sni_registry *)SSL_CTX_get_ex_data((SSL_CTX *)ctxv, |
| 579 |
|
| hm_tls_sni_ex_idx); |
| 580 |
4 |
▶ | return reg ? reg->n : -1; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| return reg ? reg->n : -1; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
4 |
0 |
4 |
|
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
|
| /* Release a context built by hm_tls_ctx_build, and the SNI registry that |
| 584 |
|
| * hangs off it. |
| 585 |
|
| * |
| 586 |
|
| * The registry has to be freed by hand: hm_tls_sni_ex_idx is created with |
| 587 |
|
| * a NULL free function, so SSL_CTX_free walks past it. Nothing needed |
| 588 |
|
| * this until tls_reload, because a context used to live exactly as long |
| 589 |
|
| * as the process did. |
| 590 |
|
| * |
| 591 |
|
| * Safe on a context that was built before a fork. Copy-on-write means |
| 592 |
|
| * the caller is decrementing refcounts in its own copy of those pages; |
| 593 |
|
| * the parent's context and its siblings' are untouched. */ |
| 594 |
3 |
| static void hm_tls_ctx_free(void *ctxv) { |
| 595 |
3 |
| SSL_CTX *ctx = (SSL_CTX *)ctxv; |
| 596 |
|
| hm_sni_registry *reg; |
| 597 |
|
| /* |
| 598 |
|
| # uncoverable branch true |
| 599 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 600 |
3 |
▶ | if (!ctx) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) return; |
0 |
3 |
3 |
|
| 601 |
|
| |
| 602 |
3 |
▶ | reg = (hm_sni_registry *)(hm_tls_sni_ex_idx >= 0 |
Branch: 1 branch
| Branch | True | False | Total |
| reg = (hm_sni_registry *)(hm_tls_sni_ex_idx >= 0 |
3 |
0 |
3 |
|
| 603 |
3 |
| ? SSL_CTX_get_ex_data(ctx, hm_tls_sni_ex_idx) : NULL); |
| 604 |
|
| /* |
| 605 |
|
| # uncoverable branch false class:ignore_covered_err |
| 606 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 607 |
3 |
▶ | if (reg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (reg) { |
3 |
0 |
3 |
|
| 608 |
|
| int i; |
| 609 |
14 |
▶ | for (i = 0; i < reg->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < reg->n; i++) { |
11 |
3 |
14 |
|
| 610 |
11 |
| free(reg->entries[i].host); |
| 611 |
|
| /* |
| 612 |
|
| # uncoverable branch false class:ignore_covered_err |
| 613 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 614 |
11 |
▶ | if (reg->entries[i].ctx) SSL_CTX_free(reg->entries[i].ctx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (reg->entries[i].ctx) SSL_CTX_free(reg->entries[i].ctx); |
11 |
0 |
11 |
|
| 615 |
|
| } |
| 616 |
3 |
| free(reg->entries); |
| 617 |
3 |
| free(reg); |
| 618 |
3 |
| SSL_CTX_set_ex_data(ctx, hm_tls_sni_ex_idx, NULL); |
| 619 |
|
| } |
| 620 |
3 |
| SSL_CTX_free(ctx); |
| 621 |
|
| } |
| 622 |
|
| |
| 623 |
|
| /* Wrap an accepted fd in a server-side SSL and put it in handshake state. */ |
| 624 |
61 |
| static int hm_tls_wrap(hm_conn *c, void *ctx) { |
| 625 |
61 |
| SSL *ssl = SSL_new((SSL_CTX *)ctx); |
| 626 |
|
| /* |
| 627 |
|
| # uncoverable branch true |
| 628 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 629 |
61 |
▶ | if (!ssl) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ssl) return -1; |
0 |
61 |
61 |
|
| 630 |
61 |
| SSL_set_fd(ssl, HM_SSL_FD(c->fd)); |
| 631 |
61 |
| SSL_set_accept_state(ssl); |
| 632 |
61 |
| c->ssl = ssl; |
| 633 |
61 |
| c->tls_hs = 1; |
| 634 |
61 |
| return 0; |
| 635 |
|
| } |
| 636 |
|
| |
| 637 |
|
| /* After a successful handshake, record protocol/cipher and any client cert. |
| 638 |
|
| * The protocol and cipher names are OpenSSL-owned and valid for the life of |
| 639 |
|
| * the connection, so they sit on hm_conn directly; the heap struct is only |
| 640 |
|
| * paid for when a client certificate was actually presented, which on a |
| 641 |
|
| * public listener is never. */ |
| 642 |
|
| /* The handshake's results, off an SSL and into three out-parameters rather |
| 643 |
|
| * than into an hm_conn. |
| 644 |
|
| * |
| 645 |
|
| * QUIC's handshake is a TLS handshake and its connection is an hm_qconn, not |
| 646 |
|
| * an hm_conn - so a version of this that took hm_conn * could only ever serve |
| 647 |
|
| * the TCP half, and HTTP/3 would silently report no client certificate at |
| 648 |
|
| * all. mTLS working on one transport and quietly not on the other is a worse |
| 649 |
|
| * failure than not supporting it. */ |
| 650 |
54 |
| static void hm_tls_capture_ssl(SSL *ssl, const char **proto, |
| 651 |
|
| const char **cipher, void **peer_out) { |
| 652 |
|
| X509 *cert; |
| 653 |
|
| /* |
| 654 |
|
| # uncoverable branch false class:ignore_covered_err |
| 655 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 656 |
54 |
▶ | if (proto) *proto = SSL_get_version(ssl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (proto) *proto = SSL_get_version(ssl); |
54 |
0 |
54 |
|
| 657 |
|
| /* |
| 658 |
|
| # uncoverable branch false class:ignore_covered_err |
| 659 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 660 |
54 |
▶ | if (cipher) *cipher = SSL_get_cipher(ssl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cipher) *cipher = SSL_get_cipher(ssl); |
54 |
0 |
54 |
|
| 661 |
|
| /* |
| 662 |
|
| # uncoverable branch true |
| 663 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 664 |
54 |
▶ | if (!peer_out) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!peer_out) return; |
0 |
54 |
54 |
|
| 665 |
54 |
| cert = SSL_get1_peer_certificate(ssl); |
| 666 |
54 |
▶ | if (cert) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cert) { |
1 |
53 |
54 |
|
| 667 |
1 |
| hm_tls_peer *p = (hm_tls_peer *)hm_xcalloc(1, sizeof(hm_tls_peer)); |
| 668 |
|
| char buf[512]; |
| 669 |
1 |
| p->verified = (SSL_get_verify_result(ssl) == X509_V_OK); |
| 670 |
|
| /* |
| 671 |
|
| # uncoverable branch false class:ignore_covered_err |
| 672 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 673 |
1 |
▶ | if (X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf))) |
1 |
0 |
1 |
|
| 674 |
1 |
| p->subject = strdup(buf); |
| 675 |
|
| /* |
| 676 |
|
| # uncoverable branch false class:ignore_covered_err |
| 677 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 678 |
1 |
▶ | if (X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf))) |
1 |
0 |
1 |
|
| 679 |
1 |
| p->issuer = strdup(buf); |
| 680 |
1 |
| X509_free(cert); |
| 681 |
1 |
| *peer_out = p; |
| 682 |
|
| } |
| 683 |
|
| } |
| 684 |
|
| |
| 685 |
54 |
| static void hm_tls_capture_peer(hm_conn *c) { |
| 686 |
54 |
| hm_tls_capture_ssl((SSL *)c->ssl, &c->tls_proto, &c->tls_cipher, |
| 687 |
|
| &c->tls_peer); |
| 688 |
54 |
| } |
| 689 |
|
| |
| 690 |
61 |
| static void hm_tls_conn_free(hm_conn *c) { |
| 691 |
61 |
▶ | if (c->tls_peer) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->tls_peer) { |
1 |
60 |
61 |
|
| 692 |
1 |
| hm_tls_peer *p = (hm_tls_peer *)c->tls_peer; |
| 693 |
|
| /* |
| 694 |
|
| # uncoverable branch false class:ignore_covered_err |
| 695 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 696 |
1 |
▶ | if (p->subject) free(p->subject); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->subject) free(p->subject); |
1 |
0 |
1 |
|
| 697 |
|
| /* |
| 698 |
|
| # uncoverable branch false class:ignore_covered_err |
| 699 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 700 |
1 |
▶ | if (p->issuer) free(p->issuer); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->issuer) free(p->issuer); |
1 |
0 |
1 |
|
| 701 |
1 |
| free(p); |
| 702 |
1 |
| c->tls_peer = NULL; |
| 703 |
|
| } |
| 704 |
61 |
| c->tls_proto = c->tls_cipher = NULL; |
| 705 |
|
| /* |
| 706 |
|
| # uncoverable branch false class:ignore_covered_err |
| 707 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 708 |
61 |
▶ | if (c->ssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->ssl) { |
61 |
0 |
61 |
|
| 709 |
|
| /* close_notify costs a write syscall and only means anything once a |
| 710 |
|
| * session exists - a connection dropped mid-handshake has nothing to |
| 711 |
|
| * shut down, so tell OpenSSL not to emit the alert for it. */ |
| 712 |
61 |
▶ | if (c->tls_hs) SSL_set_quiet_shutdown((SSL *)c->ssl, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->tls_hs) SSL_set_quiet_shutdown((SSL *)c->ssl, 1); |
7 |
54 |
61 |
|
| 713 |
54 |
| else SSL_shutdown((SSL *)c->ssl); |
| 714 |
61 |
| SSL_free((SSL *)c->ssl); |
| 715 |
61 |
| c->ssl = NULL; |
| 716 |
|
| /* a shutdown on a reset socket leaves errors queued, and the queue |
| 717 |
|
| * is per thread: the application's own OpenSSL use would read them */ |
| 718 |
61 |
| ERR_clear_error(); |
| 719 |
|
| } |
| 720 |
61 |
| } |
| 721 |
|
| |
| 722 |
|
| /* The TLS $env keys as shared key SVs, made once. hv_stores against a literal |
| 723 |
|
| * hashes the string and cuts a fresh HEK out of the shared string table on |
| 724 |
|
| * every request, then releases it again at env teardown; storing through a |
| 725 |
|
| * shared SV reuses the precomputed hash and the one HEK for the life of the |
| 726 |
|
| * process. Same trick, and the same reasoning, as the HTTP_* table in |
| 727 |
|
| * hm_core.h - it just pays per TLS request rather than per header. |
| 728 |
|
| * |
| 729 |
|
| * The values stay freshly allocated per request even though they are constant |
| 730 |
|
| * for the connection: an env value is what a PSGI app assigns THROUGH when it |
| 731 |
|
| * writes $env->{HTTPS}, and perl assigns into the SV already in the hash slot |
| 732 |
|
| * rather than replacing it, so a shared value SV would let one request's |
| 733 |
|
| * rewrite bleed into the next on the same keep-alive connection. */ |
| 734 |
|
| /* Is this connection's SEND side offloaded to the kernel right now? |
| 735 |
|
| * |
| 736 |
|
| * When it is, the socket itself is in TLS mode: a plain writev(2) or |
| 737 |
|
| * sendfile(2) on the descriptor comes out as encrypted records, which is what |
| 738 |
|
| * lets an HTTPS response take the same two fast paths a plaintext one takes. |
| 739 |
|
| * |
| 740 |
|
| * ASKED EVERY TIME, NEVER CACHED, and that is the important part. OpenSSL |
| 741 |
|
| * hands the record layer back to userspace if it ever has to take it back - a |
| 742 |
|
| * TLS 1.3 KeyUpdate is the case that does - and a connection that answered |
| 743 |
|
| * "yes" at handshake would then have Hyperman writing straight to a socket |
| 744 |
|
| * that is no longer encrypting. That failure does not look like a crash or a |
| 745 |
|
| * corrupt response: it PUTS THE PLAINTEXT ON THE WIRE. A cached flag is not |
| 746 |
|
| * worth that, and the check is a flag test on the BIO, not a syscall. |
| 747 |
|
| * |
| 748 |
|
| * c->ktls_tx is a report of what happened at handshake, for stats and tests. |
| 749 |
|
| * It is deliberately not what the write paths consult. */ |
| 750 |
145 |
| static int hm_ktls_tx(hm_conn *c) { |
| 751 |
|
| #ifdef HM_HAVE_KTLS |
| 752 |
|
| /* |
| 753 |
|
| # uncoverable branch true |
| 754 |
|
| # uncoverable condition left count:1 */ |
| 755 |
145 |
▶ | if (!c->ssl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->ssl) return 0; |
0 |
145 |
145 |
|
| 756 |
145 |
| return BIO_get_ktls_send(SSL_get_wbio((SSL *)c->ssl)) > 0; |
| 757 |
|
| #else |
| 758 |
|
| (void)c; |
| 759 |
|
| return 0; |
| 760 |
|
| #endif |
| 761 |
|
| } |
| 762 |
|
| |
| 763 |
|
| enum { |
| 764 |
|
| HM_TLSK_HTTPS, HM_TLSK_PROTOCOL, HM_TLSK_CIPHER, |
| 765 |
|
| HM_TLSK_VERIFY, HM_TLSK_S_DN, HM_TLSK_I_DN, HM_TLSK_KTLS, HM_TLSK_COUNT |
| 766 |
|
| }; |
| 767 |
|
| static const char *const hm_tlsk_name[HM_TLSK_COUNT] = { |
| 768 |
|
| "HTTPS", "SSL_PROTOCOL", "SSL_CIPHER", |
| 769 |
|
| "SSL_CLIENT_VERIFY", "SSL_CLIENT_S_DN", "SSL_CLIENT_I_DN", |
| 770 |
|
| /* 1 when the kernel took the send side of this connection. The only way |
| 771 |
|
| * to SEE kTLS working: it depends on the kernel, the OpenSSL build and |
| 772 |
|
| * the negotiated cipher all agreeing, any of which can silently say no, |
| 773 |
|
| * and a server that quietly did not offload looks exactly like one that |
| 774 |
|
| * did. */ |
| 775 |
|
| "SSL_KTLS" |
| 776 |
|
| }; |
| 777 |
|
| static SV *hm_tlsk[HM_TLSK_COUNT]; |
| 778 |
|
| |
| 779 |
47 |
| static void hm_tls_env_init(pTHX) { |
| 780 |
|
| int i; |
| 781 |
47 |
▶ | if (hm_tlsk[0]) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_tlsk[0]) return; |
39 |
8 |
47 |
|
| 782 |
64 |
▶ | for (i = 0; i < HM_TLSK_COUNT; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < HM_TLSK_COUNT; i++) |
56 |
8 |
64 |
|
| 783 |
56 |
| hm_tlsk[i] = newSVpvn_share(hm_tlsk_name[i], |
| 784 |
|
| (I32)strlen(hm_tlsk_name[i]), 0); |
| 785 |
|
| } |
| 786 |
|
| |
| 787 |
|
| /* Add the TLS $env keys (mod_ssl-style) for a TLS connection. */ |
| 788 |
|
| /* The TLS half of $env, from the captured results rather than from a |
| 789 |
|
| * connection - so HTTP/3 reports SSL_CLIENT_* exactly as HTTP/1.1 and |
| 790 |
|
| * HTTP/2 do. ktls is passed in because there is no kernel record layer for |
| 791 |
|
| * QUIC and the caller is the only one that knows which it is. */ |
| 792 |
47 |
| static void hm_tls_env_from(pTHX_ HV *env, const char *proto, |
| 793 |
|
| const char *cipher, void *peer, int ktls) { |
| 794 |
|
| hm_tls_peer *p; |
| 795 |
47 |
| hm_tls_env_init(aTHX); |
| 796 |
47 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_HTTPS], newSVpvs("on"), 0); |
| 797 |
|
| /* |
| 798 |
|
| # uncoverable branch false class:ignore_covered_err |
| 799 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 800 |
47 |
▶ | if (proto) |
Branch: 1 branch
| Branch | True | False | Total |
| if (proto) |
47 |
0 |
47 |
|
| 801 |
47 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_PROTOCOL], |
| 802 |
|
| newSVpv(proto, 0), 0); |
| 803 |
|
| /* |
| 804 |
|
| # uncoverable branch false class:ignore_covered_err |
| 805 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 806 |
47 |
▶ | if (cipher) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cipher) |
47 |
0 |
47 |
|
| 807 |
47 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_CIPHER], |
| 808 |
|
| newSVpv(cipher, 0), 0); |
| 809 |
47 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_KTLS], newSViv(ktls ? 1 : 0), 0); |
| 810 |
47 |
| p = (hm_tls_peer *)peer; |
| 811 |
47 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
1 |
46 |
47 |
|
| 812 |
1 |
▶ | (void)hv_store_ent(env, hm_tlsk[HM_TLSK_VERIFY], |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_VERIFY], |
1 |
0 |
1 |
|
| 813 |
|
| newSVpv(p->verified ? "SUCCESS" : "FAILED", 0), 0); |
| 814 |
|
| /* |
| 815 |
|
| # uncoverable branch false class:ignore_covered_err |
| 816 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 817 |
1 |
▶ | if (p->subject) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->subject) |
1 |
0 |
1 |
|
| 818 |
1 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_S_DN], |
| 819 |
|
| newSVpv(p->subject, 0), 0); |
| 820 |
|
| /* |
| 821 |
|
| # uncoverable branch false class:ignore_covered_err |
| 822 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 823 |
1 |
▶ | if (p->issuer) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->issuer) |
1 |
0 |
1 |
|
| 824 |
1 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_I_DN], |
| 825 |
|
| newSVpv(p->issuer, 0), 0); |
| 826 |
|
| } else { |
| 827 |
46 |
| (void)hv_store_ent(env, hm_tlsk[HM_TLSK_VERIFY], newSVpvs("NONE"), 0); |
| 828 |
|
| } |
| 829 |
47 |
| } |
| 830 |
|
| |
| 831 |
47 |
| static void hm_tls_env(pTHX_ hm_conn *c, HV *env) { |
| 832 |
|
| /* |
| 833 |
|
| # uncoverable branch true |
| 834 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 835 |
47 |
▶ | if (!c->ssl) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->ssl) return; |
0 |
47 |
47 |
|
| 836 |
47 |
| hm_tls_env_from(aTHX_ env, c->tls_proto, c->tls_cipher, c->tls_peer, |
| 837 |
47 |
| hm_ktls_tx(c) ? 1 : 0); |
| 838 |
|
| } |
| 839 |
|
| |
| 840 |
|
| /* Non-blocking read. >0 bytes, 0 = EOF/closed, -1 = error (errno==EAGAIN when |
| 841 |
|
| * the TLS engine would block; c->tls_r_wants_w records a needed writable). */ |
| 842 |
1204 |
| static ssize_t hm_cread(hm_conn *c, void *buf, size_t n) { |
| 843 |
|
| int r, e; |
| 844 |
1204 |
▶ | if (!c->ssl) return hm_os_recv(c->fd, buf, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->ssl) return hm_os_recv(c->fd, buf, n); |
1074 |
130 |
1204 |
|
| 845 |
130 |
| ERR_clear_error(); |
| 846 |
130 |
▶ | r = SSL_read((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| r = SSL_read((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
130 |
0 |
130 |
|
| 847 |
130 |
▶ | if (r > 0) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r > 0) return r; |
56 |
74 |
130 |
|
| 848 |
74 |
| e = SSL_get_error((SSL *)c->ssl, r); |
| 849 |
74 |
▶ | if (e == SSL_ERROR_WANT_READ) { c->tls_r_wants_w = 0; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_READ) { c->tls_r_wants_w = 0; errno = EAGAIN; return -1; } |
51 |
23 |
74 |
|
| 850 |
|
| /* |
| 851 |
|
| # uncoverable branch true |
| 852 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 853 |
23 |
▶ | if (e == SSL_ERROR_WANT_WRITE) { c->tls_r_wants_w = 1; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_WRITE) { c->tls_r_wants_w = 1; errno = EAGAIN; return -1; } |
0 |
23 |
23 |
|
| 854 |
23 |
| return 0; /* ZERO_RETURN or fatal: treat as closed */ |
| 855 |
|
| } |
| 856 |
|
| |
| 857 |
|
| /* Non-blocking write. Mirrors hm_cread; c->tls_w_wants_r records a needed |
| 858 |
|
| * readable (e.g. during a TLS 1.2 renegotiation). */ |
| 859 |
3203 |
| static ssize_t hm_cwrite(hm_conn *c, const void *buf, size_t n) { |
| 860 |
|
| int r, e; |
| 861 |
3203 |
▶ | if (!c->ssl) return hm_os_send(c->fd, buf, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->ssl) return hm_os_send(c->fd, buf, n); |
2643 |
560 |
3203 |
|
| 862 |
|
| /* |
| 863 |
|
| # uncoverable branch true |
| 864 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 865 |
560 |
▶ | if (n == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return 0; |
0 |
560 |
560 |
|
| 866 |
560 |
| ERR_clear_error(); |
| 867 |
560 |
▶ | r = SSL_write((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| r = SSL_write((SSL *)c->ssl, buf, (int)(n > INT_MAX ? INT_MAX : n)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
560 |
0 |
560 |
|
| 868 |
560 |
▶ | if (r > 0) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r > 0) return r; |
555 |
5 |
560 |
|
| 869 |
5 |
| e = SSL_get_error((SSL *)c->ssl, r); |
| 870 |
5 |
▶ | if (e == SSL_ERROR_WANT_WRITE) { c->tls_w_wants_r = 0; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_WRITE) { c->tls_w_wants_r = 0; errno = EAGAIN; return -1; } |
4 |
1 |
5 |
|
| 871 |
|
| /* |
| 872 |
|
| # uncoverable branch true |
| 873 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 874 |
1 |
▶ | if (e == SSL_ERROR_WANT_READ) { c->tls_w_wants_r = 1; errno = EAGAIN; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SSL_ERROR_WANT_READ) { c->tls_w_wants_r = 1; errno = EAGAIN; return -1; } |
0 |
1 |
1 |
|
| 875 |
1 |
| errno = EIO; |
| 876 |
1 |
| return -1; |
| 877 |
|
| } |
| 878 |
|
| |
| 879 |
30 |
| static int hm_tls_available(void) { return 1; } |
| 880 |
|
| |
| 881 |
|
| /* The runtime library banner ("OpenSSL 3.0.13 30 Jan 2024", "LibreSSL |
| 882 |
|
| * 3.8.2"): which stack is actually loaded, not which headers built this. |
| 883 |
|
| * OpenSSL_version arrived in 1.1.0 and LibreSSL in 2.7.0; before those it |
| 884 |
|
| * is SSLeay_version, same string, older name. */ |
| 885 |
1 |
| static const char *hm_tls_library(void) { |
| 886 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10100000L \ |
| 887 |
|
| && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x2070000fL) |
| 888 |
1 |
| return OpenSSL_version(OPENSSL_VERSION); |
| 889 |
|
| #else |
| 890 |
|
| return SSLeay_version(SSLEAY_VERSION); |
| 891 |
|
| #endif |
| 892 |
|
| } |
| 893 |
|
| |
| 894 |
|
| #else /* !HM_HAVE_OPENSSL */ |
| 895 |
|
| |
| 896 |
|
| /* The stubs read and write through hm_os_recv/hm_os_send, which hm_win.h |
| 897 |
|
| * has already provided along with <unistd.h> where there is one. */ |
| 898 |
|
| static void *hm_tls_ctx_build(pTHX_ const char *cert, const char *key, |
| 899 |
|
| const char *ca, int verify, SV *sni, int alpn, |
| 900 |
|
| const unsigned char *tkey, size_t tkeylen) { |
| 901 |
|
| (void)cert; (void)key; (void)ca; (void)verify; (void)sni; (void)alpn; |
| 902 |
|
| (void)tkey; (void)tkeylen; |
| 903 |
|
| return 0; |
| 904 |
|
| } |
| 905 |
|
| static int hm_tls_get_ticket_key(void *ctx, unsigned char *buf, size_t *len) |
| 906 |
|
| { (void)ctx; (void)buf; (void)len; return 0; } |
| 907 |
|
| static void *hm_tls_quic_ctx_build(const char *cert, const char *key, |
| 908 |
|
| const char *ca, int verify, |
| 909 |
|
| const unsigned char *tkey, size_t tkeylen) { |
| 910 |
|
| (void)cert; (void)key; (void)ca; (void)verify; (void)tkey; (void)tkeylen; |
| 911 |
|
| return 0; |
| 912 |
|
| } |
| 913 |
|
| static int hm_tls_sni_count(void *ctx) { (void)ctx; return -1; } |
| 914 |
|
| static void hm_tls_ctx_free(void *ctx) { (void)ctx; } |
| 915 |
|
| static int hm_tls_wrap(hm_conn *c, void *ctx) { (void)c; (void)ctx; return -1; } |
| 916 |
|
| static void hm_tls_capture_peer(hm_conn *c) { (void)c; } |
| 917 |
|
| static void hm_tls_conn_free(hm_conn *c) { (void)c; } |
| 918 |
|
| static void hm_tls_env(pTHX_ hm_conn *c, HV *env) { (void)c; (void)env; } |
| 919 |
|
| static int hm_ktls_tx(hm_conn *c) { (void)c; return 0; } |
| 920 |
|
| static ssize_t hm_cread(hm_conn *c, void *buf, size_t n) |
| 921 |
|
| { return hm_os_recv(c->fd, buf, n); } |
| 922 |
|
| static ssize_t hm_cwrite(hm_conn *c, const void *buf, size_t n) |
| 923 |
|
| { return hm_os_send(c->fd, buf, n); } |
| 924 |
|
| static int hm_tls_available(void) { return 0; } |
| 925 |
|
| static const char *hm_tls_library(void) { return 0; } |
| 926 |
|
| |
| 927 |
|
| #endif /* HM_HAVE_OPENSSL */ |
| 928 |
|
| |
| 929 |
|
| #endif /* HM_TLS_H */ |