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