scorpiox-bot-plugin-webhook
←wh_tls.cc
248 lines · 7 KBHEADBlameDownload
1/*
2 * wh_tls.c - mbedTLS over an already-connected socket (POSIX).
3 *
4 * The socket arrives pre-validated and pre-connected by wh_http.c (the SSRF
5 * check ran on the numeric address before connect, and we connected to that
6 * exact address), so this layer only wraps TLS around it. No DNS here and no
7 * second connect, therefore no window in which a rebinding DNS answer could
8 * move the connection to a private address after the check.
9 *
10 * _WIN32: WinHTTP does TLS itself; this file compiles to nothing there.
11 *
12 * MIT License. Copyright (c) 2026 SCORPIOX INC.
13 */
14#include "wh_tls.h"
15
16#if !defined(_WIN32)
17
18#ifndef _POSIX_C_SOURCE
19#define _POSIX_C_SOURCE 200809L
20#endif
21#if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE)
22#define _DARWIN_C_SOURCE 1
23#endif
24
25#include <errno.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/socket.h>
30#include <sys/stat.h>
31#include <unistd.h>
32
33#include "mbedtls/build_info.h"
34#include "mbedtls/ctr_drbg.h"
35#include "mbedtls/entropy.h"
36#include "mbedtls/error.h"
37#include "mbedtls/net_sockets.h"
38#include "mbedtls/psa_util.h"
39#include "mbedtls/ssl.h"
40#include "mbedtls/x509_crt.h"
41#include "psa/crypto.h"
42
43#define WH_CA_FILE_ENV "WEBHOOK_TLS_CA_FILE"
44
45struct wh_tls {
46 int fd;
47 mbedtls_ssl_context ssl;
48 mbedtls_ssl_config conf;
49 mbedtls_entropy_context entropy;
50 mbedtls_ctr_drbg_context drbg;
51 mbedtls_x509_crt cas;
52 int alive;
53};
54
55static const char *const CA_BUNDLES[] = {
56 "/etc/ssl/certs/ca-certificates.crt", /* Debian/Ubuntu/Alpine */
57 "/etc/pki/tls/certs/ca-bundle.crt", /* RHEL/Fedora */
58 "/etc/ssl/ca-bundle.pem", /* openSUSE */
59 "/etc/ssl/cert.pem", /* Alpine / macOS-ish */
60 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", /* Fedora */
61 "/var/ssl/certs/ca-certificates.crt", /* Termux */
62 NULL
63};
64
65static int bio_send(void *ctx, const unsigned char *buf, size_t len)
66{
67 wh_tls *t = (wh_tls *)ctx;
68 ssize_t w;
69 do { w = send(t->fd, buf, len, MSG_NOSIGNAL); } while (w < 0 && errno == EINTR);
70 if (w < 0) return MBEDTLS_ERR_NET_SEND_FAILED;
71 return (int)w;
72}
73
74static int bio_recv(void *ctx, unsigned char *buf, size_t len)
75{
76 wh_tls *t = (wh_tls *)ctx;
77 ssize_t r;
78 do { r = recv(t->fd, buf, len, 0); } while (r < 0 && errno == EINTR);
79 if (r == 0) return MBEDTLS_ERR_SSL_CONN_EOF;
80 if (r < 0) {
81 if (errno == EAGAIN || errno == EWOULDBLOCK) return MBEDTLS_ERR_SSL_WANT_READ;
82 return MBEDTLS_ERR_NET_RECV_FAILED;
83 }
84 return (int)r;
85}
86
87static void wh_tls_free(wh_tls *t)
88{
89 if (!t) return;
90 if (t->fd >= 0) { close(t->fd); t->fd = -1; }
91 if (t->alive) {
92 mbedtls_ssl_free(&t->ssl);
93 mbedtls_ssl_config_free(&t->conf);
94 mbedtls_x509_crt_free(&t->cas);
95 mbedtls_ctr_drbg_free(&t->drbg);
96 mbedtls_entropy_free(&t->entropy);
97 t->alive = 0;
98 }
99 free(t);
100}
101
102wh_tls *wh_tls_connect_fd(int fd, const char *hostname, char *err, size_t errsz)
103{
104 wh_tls *t;
105 const char *ca;
106 int ret;
107 char m[200];
108
109 if (fd < 0) { snprintf(err, errsz, "TLS: no socket"); return NULL; }
110 if (!hostname || !*hostname) {
111 snprintf(err, errsz, "TLS: no hostname for verification");
112 close(fd);
113 return NULL;
114 }
115
116 t = (wh_tls *)calloc(1, sizeof *t);
117 if (!t) { snprintf(err, errsz, "out of memory"); close(fd); return NULL; }
118 t->fd = fd;
119
120 if (psa_crypto_init() != PSA_SUCCESS) {
121 snprintf(err, errsz, "TLS failed: psa init");
122 free(t);
123 close(fd);
124 return NULL;
125 }
126
127 mbedtls_entropy_init(&t->entropy);
128 mbedtls_ctr_drbg_init(&t->drbg);
129 mbedtls_ssl_config_init(&t->conf);
130 mbedtls_ssl_init(&t->ssl);
131 mbedtls_x509_crt_init(&t->cas);
132 t->alive = 1;
133
134 ret = mbedtls_ctr_drbg_seed(&t->drbg, mbedtls_entropy_func, &t->entropy,
135 (const unsigned char *)"plugin_webhook", 14);
136 if (ret) {
137 mbedtls_strerror(ret, m, sizeof m);
138 snprintf(err, errsz, "TLS failed: rng seed: %s", m);
139 goto fail;
140 }
141
142 ret = mbedtls_ssl_config_defaults(&t->conf, MBEDTLS_SSL_IS_CLIENT,
143 MBEDTLS_SSL_TRANSPORT_STREAM,
144 MBEDTLS_SSL_PRESET_DEFAULT);
145 if (ret) {
146 mbedtls_strerror(ret, m, sizeof m);
147 snprintf(err, errsz, "TLS failed: config: %s", m);
148 goto fail;
149 }
150 mbedtls_ssl_conf_rng(&t->conf, mbedtls_ctr_drbg_random, &t->drbg);
151 mbedtls_ssl_conf_min_tls_version(&t->conf, MBEDTLS_SSL_VERSION_TLS1_2);
152 mbedtls_ssl_conf_authmode(&t->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
153
154 ca = getenv(WH_CA_FILE_ENV);
155 if (ca && *ca) {
156 ret = mbedtls_x509_crt_parse_file(&t->cas, ca);
157 if (ret) {
158 mbedtls_strerror(ret, m, sizeof m);
159 snprintf(err, errsz, "TLS failed: cannot parse CA file %s: %s", ca, m);
160 goto fail;
161 }
162 } else {
163 int i, loaded = 0;
164 for (i = 0; CA_BUNDLES[i]; i++) {
165 struct stat st;
166 if (stat(CA_BUNDLES[i], &st) != 0 || !S_ISREG(st.st_mode)) continue;
167 if (mbedtls_x509_crt_parse_file(&t->cas, CA_BUNDLES[i]) == 0) {
168 loaded = 1;
169 break;
170 }
171 }
172 if (!loaded) {
173 snprintf(err, errsz,
174 "TLS failed: no trust anchors - no readable system CA bundle; "
175 "set " WH_CA_FILE_ENV " to a PEM bundle");
176 goto fail;
177 }
178 }
179 mbedtls_ssl_conf_ca_chain(&t->conf, &t->cas, NULL);
180
181 ret = mbedtls_ssl_setup(&t->ssl, &t->conf);
182 if (ret) {
183 mbedtls_strerror(ret, m, sizeof m);
184 snprintf(err, errsz, "TLS failed: setup: %s", m);
185 goto fail;
186 }
187 ret = mbedtls_ssl_set_hostname(&t->ssl, hostname);
188 if (ret) {
189 mbedtls_strerror(ret, m, sizeof m);
190 snprintf(err, errsz, "TLS failed: hostname: %s", m);
191 goto fail;
192 }
193 mbedtls_ssl_set_bio(&t->ssl, t, bio_send, bio_recv, NULL);
194
195 do {
196 ret = mbedtls_ssl_handshake(&t->ssl);
197 } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
198 if (ret) {
199 mbedtls_strerror(ret, m, sizeof m);
200 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
201 snprintf(err, errsz,
202 "TLS failed: certificate verification refused for %s (%s)",
203 hostname, m);
204 else
205 snprintf(err, errsz, "TLS failed: handshake with %s: %s", hostname, m);
206 goto fail;
207 }
208
209 return t;
210
211fail:
212 wh_tls_free(t);
213 return NULL;
214}
215
216int wh_tls_read(wh_tls *t, char *buf, size_t n)
217{
218 int r;
219 if (!t || !t->alive) return -1;
220 do { r = mbedtls_ssl_read(&t->ssl, (unsigned char *)buf, n); }
221 while (r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE);
222 if (r == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY || r == 0) return 0;
223 if (r == MBEDTLS_ERR_SSL_CONN_EOF) return 0;
224 if (r < 0) return -1;
225 return r;
226}
227
228int wh_tls_write(wh_tls *t, const char *buf, size_t n)
229{
230 size_t off = 0;
231 if (!t || !t->alive) return -1;
232 while (off < n) {
233 int r = mbedtls_ssl_write(&t->ssl, (const unsigned char *)buf + off, n - off);
234 if (r == MBEDTLS_ERR_SSL_WANT_WRITE || r == MBEDTLS_ERR_SSL_WANT_READ) continue;
235 if (r < 0) return -1;
236 off += (size_t)r;
237 }
238 return (int)off;
239}
240
241void wh_tls_close(wh_tls *t)
242{
243 if (!t) return;
244 if (t->alive) mbedtls_ssl_close_notify(&t->ssl);
245 wh_tls_free(t);
246}
247
248#endif /* !_WIN32 */