Debian Codegraph @

src/tls_openssl.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /*
2 * Copyright (c) 2022 Joris Vink <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /*
18 * This TLS backend is the original TLS code used in Kore.
19 */
20
21 #include <sys/types.h>
22
23 #include <openssl/bio.h>
24 #include <openssl/dh.h>
25 #include <openssl/ec.h>
26 #include <openssl/ecdsa.h>
27 #include <openssl/err.h>
28 #include <openssl/ssl.h>
29 #include <openssl/evp.h>
30 #include <openssl/rand.h>
31 #include <openssl/x509.h>
32 #include <openssl/x509v3.h>
33
34 #include <poll.h>
35
36 #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/
37 #include "http.h"cxx . . $ `<file>/include/kore/http.h`/
38
•39 /*
40 * Disable deprecated declaration warnings if we're building against
41 * OpenSSL 3 as they marked all low-level APIs as deprecated.
42 *
43 * Work is being done to replace these, but for now let things build.
44 */
45 #if defined(OPENSSL_VERSION_MAJORcxx . . $ `<external>/4166512202731279593/opensslv.h:31:10`!) && OPENSSL_VERSION_MAJORcxx . . $ `<external>/4166512202731279593/opensslv.h:31:10`! >= 3
46 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
47 #endif
48
49 #define TLS_SESSION_IDcxx . . $ `src/tls_openssl.c:49:9`! "kore_tls_sessionid"
50
•51 /* Helper for weird API designs (looking at you OpenSSL). */
52 union deconstcxx . . $ deconst# {
53 void *pcxx . . $ deconst#p.;
54 const void *cpcxx . . $ deconst#cp.;
55 };
56
57 static int tls_domain_x509_verifycxx . . $ tls_domain_x509_verify(8bfeb6703ed00cb2).(int, X509_STORE_CTXcxx . . $ X509_STORE_CTX# *);
58 static X509cxx . . $ X509# *tls_domain_load_certificate_chaincxx . . $ tls_domain_load_certificate_chain(ed4c25dcdeef8a11).(SSL_CTXcxx . . $ SSL_CTX# *,
59 const void *, size_t);
•60 static EVP_PKEYcxx . . $ EVP_PKEY# *tls_privsep_private_keycxx . . $ tls_privsep_private_key(20fe4a5192f6b463).(EVP_PKEYcxx . . $ EVP_PKEY# *, struct kore_domaincxx . . $ kore_domain# *);
61
62 static int tls_sni_cbcxx . . $ tls_sni_cb(319e886ac3c8f843).(SSLcxx . . $ SSL# *, int *, void *);
63 static void tls_info_callbackcxx . . $ tls_info_callback(e8fec3d180844bc0).(const SSLcxx . . $ SSL# *, int, int);
64
65 #if defined(KORE_USE_ACME)
66 static void tls_acme_challenge_set_cert(SSL *, struct kore_domain *);
67 static int tls_acme_alpn(SSL *, const unsigned char **, unsigned char *,
68 const unsigned char *, unsigned int, void *);
69 #endif
70
71 static void tls_keymgr_await_datacxx . . $ tls_keymgr_await_data(49f6e7a06ebc5aa8).(void);
72 static void tls_keymgr_msg_responsecxx . . $ tls_keymgr_msg_response(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *);
73
74 static int tls_keymgr_rsa_initcxx . . $ tls_keymgr_rsa_init(fbdff9eb75bf4b40).(RSAcxx . . $ RSA# *);
75 static int tls_keymgr_rsa_finishcxx . . $ tls_keymgr_rsa_finish(fbdff9eb75bf4b40).(RSAcxx . . $ RSA# *);
76 static int tls_keymgr_rsa_privenccxx . . $ tls_keymgr_rsa_privenc(8dc5f0d62264ec93).(int, const unsigned char *,
77 unsigned char *, RSAcxx . . $ RSA# *, int);
78
79 static DHcxx . . $ DH# *dh_paramscxx . . $ dh_params. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
•80 static RSA_METHODcxx . . $ RSA_METHOD# *keymgr_rsa_methcxx . . $ keymgr_rsa_meth. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
81 static int tls_versioncxx . . $ tls_version. = KORE_TLS_VERSION_BOTHcxx . . $ `include/kore/kore.h:93:9`!;
82 static char *tls_cipher_listcxx . . $ tls_cipher_list. = KORE_DEFAULT_CIPHER_LISTcxx . . $ `include/kore/kore.h:105:9`!;
83
84 static u_int8_tcxx . . $ u_int8_t# keymgr_bufcxx . . $ keymgr_buf.[2048];
85 static size_t keymgr_buflencxx . . $ keymgr_buflen. = 0;
86 static int keymgr_responsecxx . . $ keymgr_response. = 0;
87
88 #if defined(KORE_USE_ACME)
89 static u_int8_t acme_alpn_name[] =
90 { 0xa, 'a', 'c', 'm', 'e', '-', 't', 'l', 's', '/', '1' };
91 #endif
92
93 struct kore_privsepcxx . . $ kore_privsep# keymgr_privsepcxx . . $ keymgr_privsep.;
94 int kore_keymgr_activecxx . . $ kore_keymgr_active. = 0;
95
96 int
97 kore_tls_supportedcxx . . $ kore_tls_supported(b126dc7c1de90089).(void)
98 {
99 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
100 }
101
102 void
103 kore_tls_initcxx . . $ kore_tls_init(49f6e7a06ebc5aa8).(void)
104 {
105 SSL_library_initcxx . . $ `<external>/17793290034335270529/ssl.h:2161:9`!();
106 SSL_load_error_stringscxx . . $ `<external>/17793290034335270529/ssl.h:1746:9`!();
107 ERR_load_crypto_stringscxx . . $ `<external>/10274939680011219511/err.h:483:9`!();
108
109 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "TLS backend %s", OPENSSL_VERSION_TEXTcxx . . $ `<external>/4166512202731279593/opensslv.h:110:10`!);
110 #if !defined(TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!)
111 if (!kore_quiet) {
112 kore_log(LOG_NOTICE,
113 "%s has no TLS 1.3 - will only use TLS 1.2",
114 OPENSSL_VERSION_TEXT);
115 }
116 #endif
117 }
118
119 void
120 kore_tls_cleanupcxx . . $ kore_tls_cleanup(49f6e7a06ebc5aa8).(void)
121 {
122 RSA_meth_freecxx . . $ RSA_meth_free(5967c5eb144373ab).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth.);
123 }
124
125 void
126 kore_tls_version_setcxx . . $ kore_tls_version_set(d4f767463ce0a6b3).(int version)
127 {
128 tls_versioncxx . . $ tls_version. = version;
129 }
130
131 void
132 kore_tls_dh_checkcxx . . $ kore_tls_dh_check(49f6e7a06ebc5aa8).(void)
133 {
134 if (dh_paramscxx . . $ dh_params. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
135 return;
136
137 if (!kore_tls_dh_loadcxx . . $ kore_tls_dh_load(5c4306d8553b3696).(KORE_DHPARAM_PATHcxx . . $ `include/kore/kore.h:104:9`!))
138 fatalcxx . . $ fatal(f6bc48f0426f26ee).("failed to load default DH parameters");
139 }
140
141 int
142 kore_tls_dh_loadcxx . . $ kore_tls_dh_load(5c4306d8553b3696).(const char *path)
143 {
144 BIOcxx . . $ BIO# *bio;
145
146 if (dh_paramscxx . . $ dh_params. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
147 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "tls_dhparam already specified");
148 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
149 }
150
151 if ((bio = BIO_new_filecxx . . $ BIO_new_file(31d6134a581165c7).(path, "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
152 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "tls_dhparam file '%s' not accessible", path);
153 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
154 }
155
156 dh_paramscxx . . $ dh_params. = PEM_read_bio_DHparamscxx . . $ PEM_read_bio_DHparams(5fc89a6bcd7b5654).(bio, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
157 BIO_freecxx . . $ BIO_free(835332762aa40168).(bio);
158
159 if (dh_paramscxx . . $ dh_params. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
160 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "PEM_read_bio_DHparams(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
161 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
162 }
163
164 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
165 }
166
167 int
168 kore_tls_ciphersuite_setcxx . . $ kore_tls_ciphersuite_set(5c4306d8553b3696).(const char *list)
169 {
170 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(tls_cipher_listcxx . . $ tls_cipher_list., KORE_DEFAULT_CIPHER_LISTcxx . . $ `include/kore/kore.h:105:9`!)) {
171 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "tls_cipher specified twice");
172 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
173 }
174
175 tls_cipher_listcxx . . $ tls_cipher_list. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(list);
176
177 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
178 }
179
180 void
181 kore_tls_keymgr_initcxx . . $ kore_tls_keymgr_init(49f6e7a06ebc5aa8).(void)
182 {
183 const RSA_METHODcxx . . $ RSA_METHOD# *meth;
184
185 if ((meth = RSA_get_default_methodcxx . . $ RSA_get_default_method(be85e86bfd825279).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
186 fatalcxx . . $ fatal(f6bc48f0426f26ee).("failed to obtain RSA method");
187
•188 if ((keymgr_rsa_methcxx . . $ keymgr_rsa_meth. = RSA_meth_newcxx . . $ RSA_meth_new(638d7fcab1f2c65d).("kore RSA keymgr method",
189 RSA_METHOD_FLAG_NO_CHECKcxx . . $ `<external>/18132502141450429728/rsa.h:64:9`!)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
190 fatalcxx . . $ fatal(f6bc48f0426f26ee).("failed to setup RSA method");
191
192 RSA_meth_set_initcxx . . $ RSA_meth_set_init(13631ff488417d24).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth., tls_keymgr_rsa_initcxx . . $ tls_keymgr_rsa_init(fbdff9eb75bf4b40).);
193 RSA_meth_set_finishcxx . . $ RSA_meth_set_finish(13631ff488417d24).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth., tls_keymgr_rsa_finishcxx . . $ tls_keymgr_rsa_finish(fbdff9eb75bf4b40).);
194 RSA_meth_set_priv_enccxx . . $ RSA_meth_set_priv_enc(a4112c7de1a91baa).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth., tls_keymgr_rsa_privenccxx . . $ tls_keymgr_rsa_privenc(8dc5f0d62264ec93).);
195
196 RSA_meth_set_pub_enccxx . . $ RSA_meth_set_pub_enc(a4112c7de1a91baa).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth., RSA_meth_get_pub_enccxx . . $ RSA_meth_get_pub_enc(9fd4475d707225f2).(meth));
197 RSA_meth_set_pub_deccxx . . $ RSA_meth_set_pub_dec(a4112c7de1a91baa).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth., RSA_meth_get_pub_deccxx . . $ RSA_meth_get_pub_dec(9fd4475d707225f2).(meth));
198 RSA_meth_set_bn_mod_expcxx . . $ RSA_meth_set_bn_mod_exp(855310d8778af583).(keymgr_rsa_methcxx . . $ keymgr_rsa_meth., RSA_meth_get_bn_mod_expcxx . . $ RSA_meth_get_bn_mod_exp(65055e47eb031e2b).(meth));
199
200 kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_KEYMGR_RESPcxx . . $ `include/kore/kore.h:662:9`!, tls_keymgr_msg_responsecxx . . $ tls_keymgr_msg_response(72fdcea79115518d).);
201 }
202
203 void
204 kore_tls_domain_setupcxx . . $ kore_tls_domain_setup(c3871c97dc621353).(struct kore_domaincxx . . $ kore_domain# *dom, int type,
205 const void *data, size_t datalen)
206 {
207 const u_int8_tcxx . . $ u_int8_t# *ptr;
208 EVP_PKEYcxx . . $ EVP_PKEY# *pkey;
•209 X509cxx . . $ X509# *x509;
210 STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509_NAME) *certs;
211 const SSL_METHODcxx . . $ SSL_METHOD# *method;
212
213 if (dom->tls_ctxcxx . . $ kore_domain#tls_ctx. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
214 SSL_CTX_freecxx . . $ SSL_CTX_free(5d51ff699a7fe4d4).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.);
215
216 if ((method = TLS_methodcxx . . $ TLS_method(969f1c0d04d9ca89).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
217 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("TLS_method(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
218
219 if ((dom->tls_ctxcxx . . $ kore_domain#tls_ctx. = SSL_CTX_newcxx . . $ SSL_CTX_new(3ed1770fe28bc1b2).(method)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
220 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_ctx_new(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
221
222 if (!SSL_CTX_set_min_proto_versioncxx . . $ `<external>/17793290034335270529/ssl.h:1556:9`!(dom->tls_ctx, TLS1_2_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:26:9`!))
223 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_set_min_proto_version: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
224
225 #if defined(TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!)
226 if (!SSL_CTX_set_max_proto_versioncxx . . $ `<external>/17793290034335270529/ssl.h:1558:9`!(dom->tls_ctx, TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!))
227 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_set_max_proto_version: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
228 #else
229 if (!SSL_CTX_set_max_proto_version(dom->tls_ctx, TLS1_2_VERSION))
230 fatalx("SSL_CTX_set_min_proto_version: %s", ssl_errno_s);
231 #endif
232
233 switch (tls_versioncxx . . $ tls_version.) {
234 case KORE_TLS_VERSION_1_3cxx . . $ `include/kore/kore.h:91:9`!:
235 #if defined(TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!)
236 if (!SSL_CTX_set_min_proto_versioncxx . . $ `<external>/17793290034335270529/ssl.h:1556:9`!(dom->tls_ctx,
237 TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!)) {
238 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_set_min_proto_version: %s",
239 ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
240 }
241 break;
242 #endif
243 case KORE_TLS_VERSION_1_2cxx . . $ `include/kore/kore.h:92:9`!:
244 if (!SSL_CTX_set_max_proto_versioncxx . . $ `<external>/17793290034335270529/ssl.h:1558:9`!(dom->tls_ctx,
245 TLS1_2_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:26:9`!)) {
246 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_set_min_proto_version: %s",
247 ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
248 }
249 break;
250 case KORE_TLS_VERSION_BOTHcxx . . $ `include/kore/kore.h:93:9`!:
251 break;
252 default:
253 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("unknown tls_version: %d", tls_versioncxx . . $ tls_version.);
254 return;
255 }
256
257 switch (type) {
258 case KORE_PEM_CERT_CHAINcxx . . $ `include/kore/kore.h:123:9`!:
259 x509 = tls_domain_load_certificate_chaincxx . . $ tls_domain_load_certificate_chain(ed4c25dcdeef8a11).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.,
260 data, datalen);
261 break;
262 case KORE_DER_CERT_DATAcxx . . $ `include/kore/kore.h:124:9`!:
263 ptr = data;
264 if ((x509 = d2i_X509cxx . . $ d2i_X509(6f09848defde1f91).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, &ptr, datalen)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
265 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("d2i_X509: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
266 if (SSL_CTX_use_certificatecxx . . $ SSL_CTX_use_certificate(7e6e14ced7e9e02d).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., x509) == 0)
267 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_use_certificate: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
268 break;
269 default:
270 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s: unknown type %d", __func__, type);
271 }
272
273 if (x509 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
274 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "failed to load certificate for '%s': %s",
275 dom->domaincxx . . $ kore_domain#domain., ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
276 SSL_CTX_freecxx . . $ SSL_CTX_free(5d51ff699a7fe4d4).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.);
277 dom->tls_ctxcxx . . $ kore_domain#tls_ctx. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
278 return;
279 }
280
281 if ((pkey = X509_get_pubkeycxx . . $ X509_get_pubkey(617630acdc35dda).(x509)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
282 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("certificate has no public key");
283
284 switch (EVP_PKEY_idcxx . . $ `<external>/2497497914425334111/evp.h:1365:9`!(pkey)) {
285 case EVP_PKEY_RSAcxx . . $ `<external>/2497497914425334111/evp.h:63:9`!:
•286 pkey = tls_privsep_private_keycxx . . $ tls_privsep_private_key(20fe4a5192f6b463).(pkey, dom);
287 break;
288 default:
289 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("unknown public key in certificate");
290 }
291
292 if (!SSL_CTX_use_PrivateKeycxx . . $ SSL_CTX_use_PrivateKey(867d055a99a16471).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., pkey))
293 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_use_PrivateKey(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
294
295 if (!SSL_CTX_check_private_keycxx . . $ SSL_CTX_check_private_key(bc768423947261c3).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.)) {
296 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("Public/Private key for %s do not match (%s)",
297 dom->domaincxx . . $ kore_domain#domain., ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
298 }
299
300 if (dh_paramscxx . . $ dh_params. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
301 fatalcxx . . $ fatal(f6bc48f0426f26ee).("no DH parameters specified");
302
303 SSL_CTX_set_tmp_dhcxx . . $ `<external>/17793290034335270529/ssl.h:1412:9`!(dom->tls_ctx, dh_params);
304 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_SINGLE_DH_USEcxx . . $ `<external>/17793290034335270529/ssl.h:485:9`!);
305
306 if (!SSL_CTX_set_ecdh_autocxx . . $ `<external>/17793290034335270529/ssl.h:1607:9`!(dom->tls_ctx, 1))
307 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_CTX_set_ecdh_auto: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
308
309 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_SINGLE_ECDH_USEcxx . . $ `<external>/17793290034335270529/ssl.h:484:9`!);
310 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_NO_COMPRESSIONcxx . . $ `<external>/17793290034335270529/ssl.h:394:9`!);
311
312 if (dom->cafilecxx . . $ kore_domain#cafile. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
313 if ((certs = SSL_load_client_CA_filecxx . . $ SSL_load_client_CA_file(35edf84d5bd07bd3).(dom->cafilecxx . . $ kore_domain#cafile.)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
314 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("SSL_load_client_CA_file(%s): %s",
315 dom->cafilecxx . . $ kore_domain#cafile., ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
316 }
317
318 SSL_CTX_load_verify_locationscxx . . $ SSL_CTX_load_verify_locations(188176d7f812b37b).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., dom->cafilecxx . . $ kore_domain#cafile., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
319 SSL_CTX_set_verify_depthcxx . . $ SSL_CTX_set_verify_depth(2fe048f9c63a01f8).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., dom->x509_verify_depthcxx . . $ kore_domain#x509_verify_depth.);
320 SSL_CTX_set_client_CA_listcxx . . $ SSL_CTX_set_client_CA_list(9b81523c1ece7f79).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., certs);
321 SSL_CTX_set_verifycxx . . $ SSL_CTX_set_verify(65118e80868657a3).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_VERIFY_PEERcxx . . $ `<external>/17793290034335270529/ssl.h:1178:9`! |
322 SSL_VERIFY_FAIL_IF_NO_PEER_CERTcxx . . $ `<external>/17793290034335270529/ssl.h:1179:9`!, tls_domain_x509_verifycxx . . $ tls_domain_x509_verify(8bfeb6703ed00cb2).);
323 }
324
325 SSL_CTX_set_session_id_contextcxx . . $ SSL_CTX_set_session_id_context(8909849e7f01c12f).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.,
326 (unsigned char *)TLS_SESSION_IDcxx . . $ `src/tls_openssl.c:49:9`!, strlencxx . . $ strlen(751346f8406fd082).(TLS_SESSION_IDcxx . . $ `src/tls_openssl.c:49:9`!));
327 SSL_CTX_set_modecxx . . $ `<external>/17793290034335270529/ssl.h:633:9`!(dom->tls_ctx, SSL_MODE_ENABLE_PARTIAL_WRITEcxx . . $ `<external>/17793290034335270529/ssl.h:497:9`!);
328
329 if (tls_versioncxx . . $ tls_version. == KORE_TLS_VERSION_BOTHcxx . . $ `include/kore/kore.h:93:9`!) {
330 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_NO_SSLv2cxx . . $ `<external>/17793290034335270529/ssl.h:487:9`!);
331 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_NO_SSLv3cxx . . $ `<external>/17793290034335270529/ssl.h:428:9`!);
332 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_NO_TLSv1cxx . . $ `<external>/17793290034335270529/ssl.h:429:9`!);
333 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_NO_TLSv1_1cxx . . $ `<external>/17793290034335270529/ssl.h:431:9`!);
334 }
335
336 SSL_CTX_set_optionscxx . . $ SSL_CTX_set_options(2799f20c7776e03).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., SSL_OP_CIPHER_SERVER_PREFERENCEcxx . . $ `<external>/17793290034335270529/ssl.h:415:9`!);
337 SSL_CTX_set_cipher_listcxx . . $ SSL_CTX_set_cipher_list(fba4022a65a941dd).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., tls_cipher_listcxx . . $ tls_cipher_list.);
338
339 SSL_CTX_set_info_callbackcxx . . $ SSL_CTX_set_info_callback(3f5fc46497971768).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx., tls_info_callbackcxx . . $ tls_info_callback(e8fec3d180844bc0).);
340 SSL_CTX_set_tlsext_servername_callbackcxx . . $ `<external>/8732523339078948436/tls1.h:333:9`!(dom->tls_ctx, tls_sni_cb);
341
342 #if defined(KORE_USE_ACME)
343 SSL_CTX_set_alpn_select_cb(dom->tls_ctx, tls_acme_alpn, dom);
344 #endif
345
346 X509_freecxx . . $ X509_free(6d28345afe631eba).(x509);
347 }
348
349 void
350 kore_tls_domain_crlcxx . . $ kore_tls_domain_crl(c5a41c3f71e20c15).(struct kore_domaincxx . . $ kore_domain# *dom, const void *pem, size_t pemlen)
351 {
352 int err;
353 BIOcxx . . $ BIO# *in;
354 X509_CRLcxx . . $ X509_CRL# *crl;
355 X509_STOREcxx . . $ X509_STORE# *store;
356
357 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
358 in = BIO_new_mem_bufcxx . . $ BIO_new_mem_buf(fff3f1d170340e13).(pem, pemlen);
359
360 if ((store = SSL_CTX_get_cert_storecxx . . $ SSL_CTX_get_cert_store(e76e3e83200727c7).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
361 BIO_freecxx . . $ BIO_free(835332762aa40168).(in);
362 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "SSL_CTX_get_cert_store(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
363 return;
364 }
365
366 for (;;) {
367 crl = PEM_read_bio_X509_CRLcxx . . $ PEM_read_bio_X509_CRL(411d37ec6e91064f).(in, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
368 if (crl == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
369 err = ERR_GET_REASONcxx . . $ ERR_GET_REASON(15f694ef3c96d230).(ERR_peek_last_errorcxx . . $ ERR_peek_last_error(f708dce9a8856c70).());
370 if (err == PEM_R_NO_START_LINEcxx . . $ `<external>/4786226268131619264/pemerr.h:44:9`!) {
371 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
372 break;
373 }
374
375 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "failed to read CRL %s: %s",
376 dom->crlfilecxx . . $ kore_domain#crlfile., ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
377 continue;
378 }
379
380 if (!X509_STORE_add_crlcxx . . $ X509_STORE_add_crl(cea6d0529634c8ca).(store, crl)) {
381 err = ERR_GET_REASONcxx . . $ ERR_GET_REASON(15f694ef3c96d230).(ERR_peek_last_errorcxx . . $ ERR_peek_last_error(f708dce9a8856c70).());
382 if (err == X509_R_CERT_ALREADY_IN_HASH_TABLEcxx . . $ `<external>/7887149254891609868/x509err.h:28:9`!) {
383 X509_CRL_freecxx . . $ X509_CRL_free(a4d887205112523c).(crl);
384 continue;
385 }
386
387 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "failed to add CRL %s: %s",
388 dom->crlfilecxx . . $ kore_domain#crlfile., ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
389 X509_CRL_freecxx . . $ X509_CRL_free(a4d887205112523c).(crl);
390 continue;
391 }
392 }
393
394 BIO_freecxx . . $ BIO_free(835332762aa40168).(in);
395
396 X509_STORE_set_flagscxx . . $ X509_STORE_set_flags(15bcaf3073171d01).(store,
397 X509_V_FLAG_CRL_CHECKcxx . . $ `<external>/18019146896230236826/x509_vfy.h:440:9`! | X509_V_FLAG_CRL_CHECK_ALLcxx . . $ `<external>/18019146896230236826/x509_vfy.h:442:9`!);
398 }
399
400 void
401 kore_tls_domain_cleanupcxx . . $ kore_tls_domain_cleanup(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *dom)
402 {
403 if (dom->tls_ctxcxx . . $ kore_domain#tls_ctx. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
404 SSL_CTX_freecxx . . $ SSL_CTX_free(5d51ff699a7fe4d4).(dom->tls_ctxcxx . . $ kore_domain#tls_ctx.);
405 }
406
407 int
408 kore_tls_connection_acceptcxx . . $ kore_tls_connection_accept(d3ce87d3c6fa6963).(struct connectioncxx . . $ connection# *c)
409 {
410 int r;
411
412 if (primary_domcxx . . $ primary_dom. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
413 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
414 "TLS handshake but no TLS configured on server");
415 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
416 }
417
418 if (primary_domcxx . . $ primary_dom.->tls_ctxcxx . . $ kore_domain#tls_ctx. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
419 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
420 "TLS configuration for %s not yet complete",
421 primary_domcxx . . $ primary_dom.->domaincxx . . $ kore_domain#domain.);
422 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
423 }
424
425 if (c->tlscxx . . $ connection#tls. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
426 c->tlscxx . . $ connection#tls. = SSL_newcxx . . $ SSL_new(d57ab211dea37115).(primary_domcxx . . $ primary_dom.->tls_ctxcxx . . $ kore_domain#tls_ctx.);
427 if (c->tlscxx . . $ connection#tls. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
428 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
429
430 SSL_set_fdcxx . . $ SSL_set_fd(743bae8d36fc347).(c->tlscxx . . $ connection#tls., c->fdcxx . . $ connection#fd.);
431 SSL_set_accept_statecxx . . $ SSL_set_accept_state(8163decced8681d3).(c->tlscxx . . $ connection#tls.);
432
433 if (!SSL_set_ex_datacxx . . $ SSL_set_ex_data(92cdf72889a332b4).(c->tlscxx . . $ connection#tls., 0, c))
434 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
435
436 if (primary_domcxx . . $ primary_dom.->cafilecxx . . $ kore_domain#cafile. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
437 c->flagscxx . . $ connection#flags. |= CONN_LOG_TLS_FAILUREcxx . . $ `include/kore/kore.h:195:9`!;
438 }
439
440 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
441 r = SSL_acceptcxx . . $ SSL_accept(1fba8404fe30de9a).(c->tlscxx . . $ connection#tls.);
442 if (r <= 0) {
443 r = SSL_get_errorcxx . . $ SSL_get_error(ab5421b0c6abcd5d).(c->tlscxx . . $ connection#tls., r);
444 switch (r) {
445 case SSL_ERROR_WANT_READcxx . . $ `<external>/17793290034335270529/ssl.h:1260:9`!:
446 case SSL_ERROR_WANT_WRITEcxx . . $ `<external>/17793290034335270529/ssl.h:1261:9`!:
447 kore_connection_start_idletimercxx . . $ kore_connection_start_idletimer(8475afdb72fd1ebb).(c);
448 return (KORE_RESULT_RETRYcxx . . $ `include/kore/kore.h:89:9`!);
449 default:
450 if (c->flagscxx . . $ connection#flags. & CONN_LOG_TLS_FAILUREcxx . . $ `include/kore/kore.h:195:9`!) {
451 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
452 "SSL_accept: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
453 }
454 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
455 }
456 }
457
458 #if defined(KORE_USE_ACME)
459 if (c->proto == CONN_PROTO_ACME_ALPN) {
460 kore_log(LOG_INFO, "disconnecting acme client");
461 kore_connection_disconnect(c);
462 return (KORE_RESULT_ERROR);
463 }
464 #endif
465
466 if (SSL_get_verify_modecxx . . $ SSL_get_verify_mode(545d7b13bd4423d0).(c->tlscxx . . $ connection#tls.) & SSL_VERIFY_PEERcxx . . $ `<external>/17793290034335270529/ssl.h:1178:9`!) {
467 c->tls_certcxx . . $ connection#tls_cert. = SSL_get_peer_certificatecxx . . $ `<external>/17793290034335270529/ssl.h:1831:9`!(c->tlscxx . . $ connection#tls.);
468 if (c->tls_certcxx . . $ connection#tls_cert. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
469 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "no peer certificate");
470 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
471 }
472 } else {
473 c->tls_certcxx . . $ connection#tls_cert. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
474 }
475
476 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
477 }
478
479 int
480 kore_tls_readcxx . . $ kore_tls_read(6a0a663d3dbc77f8).(struct connectioncxx . . $ connection# *c, size_t *bytes)
481 {
482 int r;
483
484 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
485 r = SSL_readcxx . . $ SSL_read(a20f3d5c8f16153d).(c->tlscxx . . $ connection#tls., (c->rnbcxx . . $ connection#rnb.->bufcxx . . $ netbuf#buf. + c->rnbcxx . . $ connection#rnb.->s_offcxx . . $ netbuf#s_off.),
486 (c->rnbcxx . . $ connection#rnb.->b_lencxx . . $ netbuf#b_len. - c->rnbcxx . . $ connection#rnb.->s_offcxx . . $ netbuf#s_off.));
487
488 if (c->tls_renegcxx . . $ connection#tls_reneg. > 1)
489 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
490
491 if (r <= 0) {
492 r = SSL_get_errorcxx . . $ SSL_get_error(ab5421b0c6abcd5d).(c->tlscxx . . $ connection#tls., r);
493 switch (r) {
494 case SSL_ERROR_WANT_READcxx . . $ `<external>/17793290034335270529/ssl.h:1260:9`!:
495 case SSL_ERROR_WANT_WRITEcxx . . $ `<external>/17793290034335270529/ssl.h:1261:9`!:
496 c->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. &= ~KORE_EVENT_READcxx . . $ `include/kore/kore.h:187:9`!;
497 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
498 case SSL_ERROR_ZERO_RETURNcxx . . $ `<external>/17793290034335270529/ssl.h:1265:9`!:
499 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
500 case SSL_ERROR_SYSCALLcxx . . $ `<external>/17793290034335270529/ssl.h:1263:9`!:
501 switch (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) {
502 case EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!:
503 *bytes = 0;
504 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
505 case EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!:
506 c->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. &= ~KORE_EVENT_READcxx . . $ `include/kore/kore.h:187:9`!;
507 c->snbcxx . . $ connection#snb.->flagscxx . . $ netbuf#flags. |= NETBUF_MUST_RESENDcxx . . $ `include/kore/kore.h:117:9`!;
508 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
509 default:
510 break;
511 }
512 /* FALLTHROUGH */
513 default:
514 if (c->flagscxx . . $ connection#flags. & CONN_LOG_TLS_FAILUREcxx . . $ `include/kore/kore.h:195:9`!) {
515 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
516 "SSL_read(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
517 }
518 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
519 }
520 }
521
522 *bytes = (size_t)r;
523
524 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
525 }
526
527 int
528 kore_tls_writecxx . . $ kore_tls_write(6baa9a301ab1221c).(struct connectioncxx . . $ connection# *c, size_t len, size_t *written)
529 {
530 int r;
531
532 if (len > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!)
533 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
534
535 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
536 r = SSL_writecxx . . $ SSL_write(f9d15678b0fd9307).(c->tlscxx . . $ connection#tls., (c->snbcxx . . $ connection#snb.->bufcxx . . $ netbuf#buf. + c->snbcxx . . $ connection#snb.->s_offcxx . . $ netbuf#s_off.), len);
537 if (c->tls_renegcxx . . $ connection#tls_reneg. > 1)
538 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
539
540 if (r <= 0) {
541 r = SSL_get_errorcxx . . $ SSL_get_error(ab5421b0c6abcd5d).(c->tlscxx . . $ connection#tls., r);
542 switch (r) {
543 case SSL_ERROR_WANT_READcxx . . $ `<external>/17793290034335270529/ssl.h:1260:9`!:
544 case SSL_ERROR_WANT_WRITEcxx . . $ `<external>/17793290034335270529/ssl.h:1261:9`!:
545 c->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. &= ~KORE_EVENT_WRITEcxx . . $ `include/kore/kore.h:188:9`!;
546 c->snbcxx . . $ connection#snb.->flagscxx . . $ netbuf#flags. |= NETBUF_MUST_RESENDcxx . . $ `include/kore/kore.h:117:9`!;
547 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
548 case SSL_ERROR_SYSCALLcxx . . $ `<external>/17793290034335270529/ssl.h:1263:9`!:
549 switch (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) {
550 case EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!:
551 *written = 0;
552 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
553 case EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!:
554 c->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. &= ~KORE_EVENT_WRITEcxx . . $ `include/kore/kore.h:188:9`!;
555 c->snbcxx . . $ connection#snb.->flagscxx . . $ netbuf#flags. |= NETBUF_MUST_RESENDcxx . . $ `include/kore/kore.h:117:9`!;
556 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
557 default:
558 break;
559 }
560 /* FALLTHROUGH */
561 default:
562 if (c->flagscxx . . $ connection#flags. & CONN_LOG_TLS_FAILUREcxx . . $ `include/kore/kore.h:195:9`!) {
563 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
564 "SSL_write(): %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
565 }
566 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
567 }
568 }
569
570 *written = (size_t)r;
571
572 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
573 }
574
575 void
576 kore_tls_connection_cleanupcxx . . $ kore_tls_connection_cleanup(8475afdb72fd1ebb).(struct connectioncxx . . $ connection# *c)
577 {
578 if (c->tlscxx . . $ connection#tls. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
579 SSL_shutdowncxx . . $ SSL_shutdown(1fba8404fe30de9a).(c->tlscxx . . $ connection#tls.);
580 SSL_freecxx . . $ SSL_free(8163decced8681d3).(c->tlscxx . . $ connection#tls.);
581 }
582
583 if (c->tls_certcxx . . $ connection#tls_cert. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
584 X509_freecxx . . $ X509_free(6d28345afe631eba).(c->tls_certcxx . . $ connection#tls_cert.);
585
586 if (c->tls_snicxx . . $ connection#tls_sni. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
587 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(c->tls_snicxx . . $ connection#tls_sni.);
588 }
589
590
591 KORE_PRIVATE_KEYcxx . . $ KORE_PRIVATE_KEY# *
592 kore_tls_rsakey_loadcxx . . $ kore_tls_rsakey_load(49e461faa1428cff).(const char *path)
593 {
594 FILEcxx . . $ FILE# *fp;
595 KORE_PRIVATE_KEYcxx . . $ KORE_PRIVATE_KEY# *pkey;
596
597 if (accesscxx . . $ access(cb7559238bb26814).(path, R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!) == -1)
598 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
599
600 if ((fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(path, "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
601 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s(%s): %s", __func__, path, errno_scxx . . $ `include/kore/kore.h:100:9`!);
602
603 if ((pkey = PEM_read_PrivateKeycxx . . $ PEM_read_PrivateKey(e0d8e6cb8d637963).(fp, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
604 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("PEM_read_PrivateKey: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
605
606 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
607
608 return (pkey);
609 }
610
611 KORE_PRIVATE_KEYcxx . . $ KORE_PRIVATE_KEY# *
612 kore_tls_rsakey_generatecxx . . $ kore_tls_rsakey_generate(49e461faa1428cff).(const char *path)
613 {
614 FILEcxx . . $ FILE# *fp;
615 EVP_PKEY_CTXcxx . . $ EVP_PKEY_CTX# *ctx;
616 KORE_PRIVATE_KEYcxx . . $ KORE_PRIVATE_KEY# *pkey;
617
618 if ((ctx = EVP_PKEY_CTX_new_idcxx . . $ EVP_PKEY_CTX_new_id(1d4741483a6a9986).(EVP_PKEY_RSAcxx . . $ `<external>/2497497914425334111/evp.h:63:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
619 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("EVP_PKEY_CTX_new_id: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
620
621 if (EVP_PKEY_keygen_initcxx . . $ EVP_PKEY_keygen_init(ff43b6e280f06fe5).(ctx) <= 0)
622 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("EVP_PKEY_keygen_init: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
623
624 if (EVP_PKEY_CTX_set_rsa_keygen_bitscxx . . $ EVP_PKEY_CTX_set_rsa_keygen_bits(8ec8b356f69793da).(ctx, KORE_RSAKEY_BITScxx . . $ `include/kore/kore.h:80:9`!) <= 0)
625 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("EVP_PKEY_CTX_set_rsa_keygen_bits: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
626
627 pkey = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
628 if (EVP_PKEY_keygencxx . . $ EVP_PKEY_keygen(5d28330bf0d0aa03).(ctx, &pkey) <= 0)
629 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("EVP_PKEY_keygen: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
630
631 if (path != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
632 if ((fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(path, "w")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
633 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("fopen(%s): %s", path, errno_scxx . . $ `include/kore/kore.h:100:9`!);
634
635 if (!PEM_write_PrivateKeycxx . . $ PEM_write_PrivateKey(2bcb0dc1c6398047).(fp, pkey, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!))
636 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("PEM_write_PrivateKey: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
637
638 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
639 }
640
641 return (pkey);
642 }
643
644 KORE_X509_NAMEScxx . . $ KORE_X509_NAMES# *
645 kore_tls_x509_subject_namecxx . . $ kore_tls_x509_subject_name(6f423c880c279b5c).(struct connectioncxx . . $ connection# *c)
646 {
647 X509_NAMEcxx . . $ X509_NAME# *name;
648
649 if ((name = X509_get_subject_namecxx . . $ X509_get_subject_name(38fd56961e51b042).(c->tls_certcxx . . $ connection#tls_cert.)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
650 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "X509_get_subject_name: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
651
652 return (name);
653 }
654
655 KORE_X509_NAMEScxx . . $ KORE_X509_NAMES# *
656 kore_tls_x509_issuer_namecxx . . $ kore_tls_x509_issuer_name(6f423c880c279b5c).(struct connectioncxx . . $ connection# *c)
657 {
658 X509_NAMEcxx . . $ X509_NAME# *name;
659
660 if ((name = X509_get_issuer_namecxx . . $ X509_get_issuer_name(38fd56961e51b042).(c->tls_certcxx . . $ connection#tls_cert.)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
661 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "X509_get_issuer_name: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
662
663 return (name);
664 }
665
666 int
667 kore_tls_x509name_foreachcxx . . $ kore_tls_x509name_foreach(cf8858a95eca5aae).(KORE_X509_NAMEScxx . . $ KORE_X509_NAMES# *name, int flags, void *udata,
668 int (*cb)(void *, int, int, const char *, const void *, size_t, int))
669 {
670 u_int8_tcxx . . $ u_int8_t# *data;
671 ASN1_STRINGcxx . . $ ASN1_STRING# *astr;
672 X509_NAME_ENTRYcxx . . $ X509_NAME_ENTRY# *entry;
673 const char *field;
674 int islast, ret, idx, namelen, nid, len;
675
676 data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
677 ret = KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!;
678
679 if ((namelen = X509_NAME_entry_countcxx . . $ X509_NAME_entry_count(d2bd2e864b6e714b).(name)) == 0)
680 goto cleanup;
681
682 for (idx = 0; idx < namelen; idx++) {
683 if ((entry = X509_NAME_get_entrycxx . . $ X509_NAME_get_entry(51a970611543b81f).(name, idx)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
684 goto cleanup;
685
686 nid = OBJ_obj2nidcxx . . $ OBJ_obj2nid(6d10da3ddaac14e7).(X509_NAME_ENTRY_get_objectcxx . . $ X509_NAME_ENTRY_get_object(eb85c31c45e217b0).(entry));
687 if ((field = OBJ_nid2sncxx . . $ OBJ_nid2sn(f7ae7e56280f18ae).(nid)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
688 goto cleanup;
689
690 switch (nid) {
691 case NID_commonNamecxx . . $ `<external>/18030087176251566933/obj_mac.h:2403:9`!:
692 nid = KORE_X509_NAME_COMMON_NAMEcxx . . $ `include/kore/kore.h:817:9`!;
693 break;
694 default:
695 nid = -1;
696 break;
697 }
698
699 if ((astr = X509_NAME_ENTRY_get_datacxx . . $ X509_NAME_ENTRY_get_data(a7bab23f60bf427e).(entry)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
700 goto cleanup;
701
702 data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
703 if ((len = ASN1_STRING_to_UTF8cxx . . $ ASN1_STRING_to_UTF8(d088b7111258b04b).(&data, astr)) < 0)
704 goto cleanup;
705
706 if (idx != (namelen - 1))
707 islast = 0;
708 else
709 islast = 1;
710
711 if (!cb(udata, islast, nid, field, data, len, flags))
712 goto cleanup;
713
714 OPENSSL_freecxx . . $ `<external>/8711174516434588327/crypto.h:130:9`!(data);
715 data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
716 }
717
718 ret = KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!;
719
720 cleanup:
721 if (data != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
722 OPENSSL_freecxx . . $ `<external>/8711174516434588327/crypto.h:130:9`!(data);
723
724 return (ret);
725 }
726
727 int
728 kore_tls_x509_datacxx . . $ kore_tls_x509_data(b7d795513140487e).(struct connectioncxx . . $ connection# *c, u_int8_tcxx . . $ u_int8_t# **ptr, size_t *olen)
729 {
730 int len;
731 u_int8_tcxx . . $ u_int8_t# *der, *pp;
732
733 if ((len = i2d_X509cxx . . $ i2d_X509(4b03eed01144511c).(c->tls_certcxx . . $ connection#tls_cert., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) <= 0) {
734 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "i2d_X509: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
735 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
736 }
737
738 der = kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(1, len);
739 pp = der;
740
741 if (i2d_X509cxx . . $ i2d_X509(4b03eed01144511c).(c->tls_certcxx . . $ connection#tls_cert., &pp) <= 0) {
742 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(der);
743 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "i2d_X509: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!);
744 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
745 }
746
747 *ptr = der;
748 *olen = len;
749
750 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
751 }
752
753 void
754 kore_tls_seedcxx . . $ kore_tls_seed(8c7266571c1d65cc).(const void *data, size_t len)
755 {
756 RAND_pollcxx . . $ RAND_poll(b126dc7c1de90089).();
757 RAND_seedcxx . . $ RAND_seed(a8b5d42151974564).(data, len);
758 }
759
760 static void
761 tls_info_callbackcxx . . $ tls_info_callback(e8fec3d180844bc0).(const SSLcxx . . $ SSL# *ssl, int flags, int ret)
762 {
763 struct connectioncxx . . $ connection# *c;
764
765 if (flags & SSL_CB_HANDSHAKE_STARTcxx . . $ `<external>/17793290034335270529/ssl.h:1146:9`!) {
766 if ((c = SSL_get_app_datacxx . . $ `<external>/17793290034335270529/ssl.h:1035:9`!(ssl)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
767 fatalcxx . . $ fatal(f6bc48f0426f26ee).("no SSL_get_app_data");
768
769 #if defined(TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!)
770 if (SSL_versioncxx . . $ SSL_version(545d7b13bd4423d0).(ssl) != TLS1_3_VERSIONcxx . . $ `<external>/581042083043620402/prov_ssl.h:27:9`!)
771 #endif
772 c->tls_renegcxx . . $ connection#tls_reneg.++;
773 }
774 }
775
776 static int
777 tls_sni_cbcxx . . $ tls_sni_cb(319e886ac3c8f843).(SSLcxx . . $ SSL# *ssl, int *ad, void *arg)
778 {
779 struct connectioncxx . . $ connection# *c;
780 struct kore_domaincxx . . $ kore_domain# *dom;
781 const char *sname;
782
783 if ((c = SSL_get_ex_datacxx . . $ SSL_get_ex_data(fb64398dc17e8687).(ssl, 0)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
784 fatalcxx . . $ fatal(f6bc48f0426f26ee).("no connection data in %s", __func__);
785
786 sname = SSL_get_servernamecxx . . $ SSL_get_servername(cff1c8c8daa96b55).(ssl, TLSEXT_NAMETYPE_host_namecxx . . $ `<external>/8732523339078948436/tls1.h:171:9`!);
787
788 if (sname != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
789 c->tls_snicxx . . $ connection#tls_sni. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(sname);
790
791 if (sname != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! &&
792 (dom = kore_domain_lookupcxx . . $ kore_domain_lookup(1312d99c9b0d5185).(c->ownercxx . . $ connection#owner.->servercxx . . $ listener#server., sname)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
793 if (dom->tls_ctxcxx . . $ kore_domain#tls_ctx. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
794 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
795 "TLS configuration for %s not complete",
796 dom->domaincxx . . $ kore_domain#domain.);
797 return (SSL_TLSEXT_ERR_NOACKcxx . . $ `<external>/8732523339078948436/tls1.h:340:9`!);
798 }
799
800 SSL_set_SSL_CTXcxx . . $ SSL_set_SSL_CTX(c5fed1161b68215e).(ssl, dom->tls_ctxcxx . . $ kore_domain#tls_ctx.);
801
802 if (dom->cafilecxx . . $ kore_domain#cafile. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
803 SSL_set_verifycxx . . $ SSL_set_verify(a05a4fdf65031ead).(ssl, SSL_VERIFY_PEERcxx . . $ `<external>/17793290034335270529/ssl.h:1178:9`! |
804 SSL_VERIFY_FAIL_IF_NO_PEER_CERTcxx . . $ `<external>/17793290034335270529/ssl.h:1179:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
805 c->flagscxx . . $ connection#flags. |= CONN_LOG_TLS_FAILUREcxx . . $ `include/kore/kore.h:195:9`!;
806 } else {
807 SSL_set_verifycxx . . $ SSL_set_verify(a05a4fdf65031ead).(ssl, SSL_VERIFY_NONEcxx . . $ `<external>/17793290034335270529/ssl.h:1177:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
808 }
809
810 #if defined(KORE_USE_ACME)
811 /*
812 * If ALPN callback was called before SNI was parsed we
813 * must make sure we swap to the correct certificate now.
814 */
815 if (c->flags & CONN_TLS_ALPN_ACME_SEEN)
816 tls_acme_challenge_set_cert(ssl, dom);
817
818 c->flags |= CONN_TLS_SNI_SEEN;
819 #endif
820 return (SSL_TLSEXT_ERR_OKcxx . . $ `<external>/8732523339078948436/tls1.h:337:9`!);
821 }
822
823 return (SSL_TLSEXT_ERR_NOACKcxx . . $ `<external>/8732523339078948436/tls1.h:340:9`!);
824 }
825
826 static int
827 tls_keymgr_rsa_initcxx . . $ tls_keymgr_rsa_init(fbdff9eb75bf4b40).(RSAcxx . . $ RSA# *rsa)
828 {
829 if (rsa != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
•830 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "Set the flags on %p", (void *)rsa);
831 RSA_set_flagscxx . . $ RSA_set_flags(f95d0a34364cd5d4).(rsa, RSA_flagscxx . . $ RSA_flags(e0c3c17b6cc9d160).(rsa) |
832 RSA_FLAG_EXT_PKEYcxx . . $ `<external>/18132502141450429728/rsa.h:75:9`! | RSA_METHOD_FLAG_NO_CHECKcxx . . $ `<external>/18132502141450429728/rsa.h:64:9`!);
833 return (1);
834 }
835
836 return (0);
837 }
838
839 static int
840 tls_keymgr_rsa_privenccxx . . $ tls_keymgr_rsa_privenc(8dc5f0d62264ec93).(int flen, const unsigned char *from, unsigned char *to,
841 RSAcxx . . $ RSA# *rsa, int padding)
842 {
843 int ret;
844 size_t len;
845 struct kore_keyreqcxx . . $ kore_keyreq# *req;
846 struct kore_domaincxx . . $ kore_domain# *dom;
847
848 len = sizeof(*req) + flen;
849 if (len > sizeof(keymgr_bufcxx . . $ keymgr_buf.))
850 fatalcxx . . $ fatal(f6bc48f0426f26ee).("keymgr_buf too small");
851
852 if ((dom = RSA_get_app_datacxx . . $ `<external>/18132502141450429728/rsa.h:209:9`!(rsa)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
853 fatalcxx . . $ fatal(f6bc48f0426f26ee).("RSA key has no domain attached");
854
855 memsetcxx . . $ memset(d1de1e4fb215f1c6).(keymgr_bufcxx . . $ keymgr_buf., 0, sizeof(keymgr_bufcxx . . $ keymgr_buf.));
856
857 req = (struct kore_keyreqcxx . . $ kore_keyreq# *)keymgr_bufcxx . . $ keymgr_buf.;
858
859 if (kore_strlcpycxx . . $ kore_strlcpy(55abe9014c6d0e39).(req->domaincxx . . $ kore_keyreq#domain., dom->domaincxx . . $ kore_domain#domain., sizeof(req->domaincxx . . $ kore_keyreq#domain.)) >=
860 sizeof(req->domaincxx . . $ kore_keyreq#domain.))
861 fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: domain truncated", __func__);
862
863 req->data_lencxx . . $ kore_keyreq#data_len. = flen;
864 req->paddingcxx . . $ kore_keyreq#padding. = padding;
865 memcpycxx . . $ memcpy(cc817cb175f282d).(&req->datacxx . . $ kore_keyreq#data.[0], from, req->data_lencxx . . $ kore_keyreq#data_len.);
866
867 kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!, KORE_MSG_KEYMGR_REQcxx . . $ `include/kore/kore.h:661:9`!, keymgr_bufcxx . . $ keymgr_buf., len);
868 tls_keymgr_await_datacxx . . $ tls_keymgr_await_data(49f6e7a06ebc5aa8).();
869
870 ret = -1;
871 if (keymgr_responsecxx . . $ keymgr_response.) {
872 if (keymgr_buflencxx . . $ keymgr_buflen. < INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`! &&
873 (int)keymgr_buflencxx . . $ keymgr_buflen. == RSA_sizecxx . . $ RSA_size(e0c3c17b6cc9d160).(rsa)) {
874 ret = RSA_sizecxx . . $ RSA_size(e0c3c17b6cc9d160).(rsa);
875 memcpycxx . . $ memcpy(cc817cb175f282d).(to, keymgr_bufcxx . . $ keymgr_buf., RSA_sizecxx . . $ RSA_size(e0c3c17b6cc9d160).(rsa));
876 }
877 }
878
879 keymgr_buflencxx . . $ keymgr_buflen. = 0;
880 keymgr_responsecxx . . $ keymgr_response. = 0;
881 kore_platform_event_allcxx . . $ kore_platform_event_all(6ca3d68f5c5f0975).(workercxx . . $ worker.->msgcxx . . $ kore_worker#msg.[1]->fdcxx . . $ connection#fd., workercxx . . $ worker.->msgcxx . . $ kore_worker#msg.[1]);
882
883 return (ret);
884 }
885
886 static int
887 tls_keymgr_rsa_finishcxx . . $ tls_keymgr_rsa_finish(fbdff9eb75bf4b40).(RSAcxx . . $ RSA# *rsa)
888 {
889 return (1);
890 }
891
892 static void
893 tls_keymgr_await_datacxx . . $ tls_keymgr_await_data(49f6e7a06ebc5aa8).(void)
894 {
895 int ret;
896 struct pollfdcxx . . $ pollfd# pfd[1];
897 u_int64_tcxx . . $ u_int64_t# start, cur;
898 #if !defined(KORE_NO_HTTP)
899 int process_requests;
900 #endif
901
902 /*
903 * We need to wait until the keymgr responds to us, so keep doing
904 * net_recv_flush() until our callback for KORE_MSG_KEYMGR_RESP
905 * tells us that we have obtained the response.
906 *
907 * This means other internal messages can still be delivered by
908 * this worker process to the appropriate callbacks but we do not
909 * drop out until we've either received an answer from the keymgr
910 * or until the timeout has been reached (1 second currently).
911 *
912 * If we end up waiting for the keymgr process we will call
913 * http_process (if not built with NOHTTP=1) to further existing
914 * requests so those do not block too much.
915 *
916 * This means that all incoming data will stop being processed
917 * while existing requests will get processed until we return
918 * from this call.
919 */
920 start = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).();
921 kore_platform_disable_readcxx . . $ kore_platform_disable_read(d4f767463ce0a6b3).(workercxx . . $ worker.->msgcxx . . $ kore_worker#msg.[1]->fdcxx . . $ connection#fd.);
922
923 keymgr_responsecxx . . $ keymgr_response. = 0;
924 memsetcxx . . $ memset(d1de1e4fb215f1c6).(keymgr_bufcxx . . $ keymgr_buf., 0, sizeof(keymgr_bufcxx . . $ keymgr_buf.));
925
926 #if !defined(KORE_NO_HTTP)
927 process_requests = 0;
928 #endif
929
930 for (;;) {
931 #if !defined(KORE_NO_HTTP)
932 if (process_requests) {
933 http_processcxx . . $ http_process(49f6e7a06ebc5aa8).();
934 process_requests = 0;
935 }
936 #endif
937 pfd[0].fdcxx . . $ pollfd#fd. = workercxx . . $ worker.->msgcxx . . $ kore_worker#msg.[1]->fdcxx . . $ connection#fd.;
938 pfd[0].eventscxx . . $ pollfd#events. = POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`!;
939 pfd[0].reventscxx . . $ pollfd#revents. = 0;
940
941 ret = pollcxx . . $ poll(bc0744c797f613bd).(pfd, 1, 100);
942 if (ret == -1) {
943 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!)
944 continue;
945 fatalcxx . . $ fatal(f6bc48f0426f26ee).("poll: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
946 }
947
948 cur = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).();
949 if ((cur - start) > 1000)
950 break;
951 if (ret == 0) {
952 #if !defined(KORE_NO_HTTP)
953 /* No activity on channel, process HTTP requests. */
954 process_requests = 1;
955 #endif
956 continue;
957 }
958
959 if (pfd[0].reventscxx . . $ pollfd#revents. & (POLLERRcxx . . $ `<external>/17223861378049612178/poll.h:47:9`! | POLLHUPcxx . . $ `<external>/17223861378049612178/poll.h:48:9`!))
960 break;
961 if (!(pfd[0].reventscxx . . $ pollfd#revents. & POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`!))
962 break;
963
964 workercxx . . $ worker.->msgcxx . . $ kore_worker#msg.[1]->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. |= KORE_EVENT_READcxx . . $ `include/kore/kore.h:187:9`!;
965 if (!net_recv_flushcxx . . $ net_recv_flush(d3ce87d3c6fa6963).(workercxx . . $ worker.->msgcxx . . $ kore_worker#msg.[1]))
966 break;
967
968 if (keymgr_responsecxx . . $ keymgr_response.)
969 break;
970
971 #if !defined(KORE_NO_HTTP)
972 /* If we've spent 100ms already, process HTTP requests. */
973 if ((cur - start) > 100) {
974 process_requests = 1;
975 }
976 #endif
977 }
978 }
979
980 static void
981 tls_keymgr_msg_responsecxx . . $ tls_keymgr_msg_response(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data)
982 {
983 keymgr_responsecxx . . $ keymgr_response. = 1;
984 keymgr_buflencxx . . $ keymgr_buflen. = msg->lengthcxx . . $ kore_msg#length.;
985
986 if (keymgr_buflencxx . . $ keymgr_buflen. > sizeof(keymgr_bufcxx . . $ keymgr_buf.))
987 return;
988
989 memcpycxx . . $ memcpy(cc817cb175f282d).(keymgr_bufcxx . . $ keymgr_buf., data, keymgr_buflencxx . . $ keymgr_buflen.);
990 }
991
992 static int
993 tls_domain_x509_verifycxx . . $ tls_domain_x509_verify(8bfeb6703ed00cb2).(int ok, X509_STORE_CTXcxx . . $ X509_STORE_CTX# *ctx)
994 {
995 X509cxx . . $ X509# *cert;
996 const char *text;
997 int error, depth;
998
999 error = X509_STORE_CTX_get_errorcxx . . $ X509_STORE_CTX_get_error(fc5f4c3049cd401d).(ctx);
1000 cert = X509_STORE_CTX_get_current_certcxx . . $ X509_STORE_CTX_get_current_cert(2438b0cd0150da2b).(ctx);
1001
1002 if (ok == 0 && cert != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1003 text = X509_verify_cert_error_stringcxx . . $ X509_verify_cert_error_string(595caefabb192cea).(error);
1004 depth = X509_STORE_CTX_get_error_depthcxx . . $ X509_STORE_CTX_get_error_depth(fc5f4c3049cd401d).(ctx);
1005
1006 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "X509 verification error depth:%d - %s",
1007 depth, text);
1008
1009 /* Continue on CRL validity errors. */
1010 switch (error) {
1011 case X509_V_ERR_CRL_HAS_EXPIREDcxx . . $ `<external>/18019146896230236826/x509_vfy.h:324:9`!:
1012 case X509_V_ERR_CRL_NOT_YET_VALIDcxx . . $ `<external>/18019146896230236826/x509_vfy.h:323:9`!:
1013 case X509_V_ERR_UNABLE_TO_GET_CRLcxx . . $ `<external>/18019146896230236826/x509_vfy.h:315:9`!:
1014 ok = 1;
1015 break;
1016 }
1017 }
1018
1019 return (ok);
1020 }
1021
1022 /*
1023 * What follows is basically a reimplementation of
1024 * SSL_CTX_use_certificate_chain_file() from OpenSSL but with our
1025 * BIO set to the pem data that we received.
1026 */
1027 static X509cxx . . $ X509# *
1028 tls_domain_load_certificate_chaincxx . . $ tls_domain_load_certificate_chain(ed4c25dcdeef8a11).(SSL_CTXcxx . . $ SSL_CTX# *ctx, const void *data, size_t len)
1029 {
1030 unsigned long err;
1031 BIOcxx . . $ BIO# *in;
1032 X509cxx . . $ X509# *x, *ca;
1033
1034 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
1035 in = BIO_new_mem_bufcxx . . $ BIO_new_mem_buf(fff3f1d170340e13).(data, len);
1036
1037 if ((x = PEM_read_bio_X509_AUXcxx . . $ PEM_read_bio_X509_AUX(fe349e2f3a4b5e72).(in, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
1038 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
1039
1040 /* refcount for x509 will go up one. */
1041 if (SSL_CTX_use_certificatecxx . . $ SSL_CTX_use_certificate(7e6e14ced7e9e02d).(ctx, x) == 0)
1042 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
1043
1044 SSL_CTX_clear_chain_certscxx . . $ `<external>/17793290034335270529/ssl.h:1447:9`!(ctx);
1045
1046 ERR_clear_errorcxx . . $ ERR_clear_error(49f6e7a06ebc5aa8).();
1047 while ((ca = PEM_read_bio_X509cxx . . $ PEM_read_bio_X509(fe349e2f3a4b5e72).(in, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1048 /* ca its reference count won't be increased. */
1049 if (SSL_CTX_add0_chain_certcxx . . $ `<external>/17793290034335270529/ssl.h:1441:9`!(ctx, ca) == 0)
1050 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
1051 }
1052
1053 err = ERR_peek_last_errorcxx . . $ ERR_peek_last_error(f708dce9a8856c70).();
1054
1055 if (ERR_GET_LIBcxx . . $ ERR_GET_LIB(15f694ef3c96d230).(err) != ERR_LIB_PEMcxx . . $ `<external>/10274939680011219511/err.h:81:9`! ||
1056 ERR_GET_REASONcxx . . $ ERR_GET_REASON(15f694ef3c96d230).(err) != PEM_R_NO_START_LINEcxx . . $ `<external>/4786226268131619264/pemerr.h:44:9`!)
1057 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
1058
1059 BIO_freecxx . . $ BIO_free(835332762aa40168).(in);
1060
1061 return (x);
1062 }
1063
•1064 static EVP_PKEYcxx . . $ EVP_PKEY# *
1065 tls_privsep_private_keycxx . . $ tls_privsep_private_key(20fe4a5192f6b463).(EVP_PKEYcxx . . $ EVP_PKEY# *pub, struct kore_domaincxx . . $ kore_domain# *dom)
1066 {
1067 EVP_PKEYcxx . . $ EVP_PKEY# *pkey;
1068 RSAcxx . . $ RSA# *rsa_new;
1069 const RSAcxx . . $ RSA# *rsa_cur;
1070 const BIGNUMcxx . . $ BIGNUM# *e_cur, *n_cur;
1071 BIGNUMcxx . . $ BIGNUM# *e_new, *n_new;
1072
1073 if ((pkey = EVP_PKEY_newcxx . . $ EVP_PKEY_new(329d513567cbda88).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
1074 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("failed to create new EVP_PKEY data structure");
1075
1076 if ((rsa_new = RSA_newcxx . . $ RSA_new(88f934d4dfdd44e4).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
1077 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("failed to create new RSA data structure");
1078
1079 if ((rsa_cur = EVP_PKEY_get0_RSAcxx . . $ EVP_PKEY_get0_RSA(b6b77da49c324b2b).(pub)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
1080 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("no RSA public key present");
1081
1082 RSA_get0_keycxx . . $ RSA_get0_key(5aaa3ec21136b50f).(rsa_cur, &n_cur, &e_cur, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
1083
1084 if ((n_new = BN_dupcxx . . $ BN_dup(70c12821d1564b33).(n_cur)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
1085 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("BN_dup failed");
1086
1087 if ((e_new = BN_dupcxx . . $ BN_dup(70c12821d1564b33).(e_cur)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
1088 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("BN_dup failed");
1089
1090 RSA_set_app_datacxx . . $ `<external>/18132502141450429728/rsa.h:208:9`!(rsa_new, dom);
1091 RSA_set_methodcxx . . $ RSA_set_method(db72d4a1410e773e).(rsa_new, keymgr_rsa_methcxx . . $ keymgr_rsa_meth.);
1092 RSA_set0_keycxx . . $ RSA_set0_key(528443a14364390d).(rsa_new, n_new, e_new, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
1093
1094 EVP_PKEY_set1_RSAcxx . . $ EVP_PKEY_set1_RSA(cb73477c238009ff).(pkey, rsa_new);
1095
1096 return (pkey);
1097 }
1098
1099 #if defined(KORE_USE_ACME)
1100 static int
1101 tls_acme_alpn(SSL *ssl, const unsigned char **out, unsigned char *outlen,
1102 const unsigned char *in, unsigned int inlen, void *udata)
1103 {
1104 struct connection *c;
1105
1106 if ((c = SSL_get_ex_data(ssl, 0)) == NULL)
1107 fatal("%s: no connection data present", __func__);
1108
1109 if (inlen != sizeof(acme_alpn_name))
1110 return (SSL_TLSEXT_ERR_NOACK);
1111
1112 if (memcmp(acme_alpn_name, in, sizeof(acme_alpn_name)))
1113 return (SSL_TLSEXT_ERR_NOACK);
1114
1115 *out = in + 1;
1116 *outlen = inlen - 1;
1117
1118 c->flags |= CONN_TLS_ALPN_ACME_SEEN;
1119
1120 /*
1121 * If SNI was already done, we can continue, otherwise we mark
1122 * that we saw the right ALPN negotiation on this connection
1123 * and wait for the SNI extension to be parsed.
1124 */
1125 if (c->flags & CONN_TLS_SNI_SEEN) {
1126 /* SNI was seen, we are on the right domain. */
1127 tls_acme_challenge_set_cert(ssl, udata);
1128 }
1129
1130 return (SSL_TLSEXT_ERR_OK);
1131 }
1132
1133 static void
1134 tls_acme_challenge_set_cert(SSL *ssl, struct kore_domain *dom)
1135 {
1136 struct connection *c;
1137 const unsigned char *ptr;
1138 X509 *x509;
1139
1140 if (dom->acme == 0) {
1141 kore_log(LOG_NOTICE, "[%s] ACME not active", dom->domain);
1142 return;
1143 }
1144
1145 if (dom->acme_challenge == 0) {
1146 kore_log(LOG_NOTICE,
1147 "[%s] ACME auth challenge not active", dom->domain);
1148 return;
1149 }
1150
1151 kore_log(LOG_INFO, "[%s] acme-tls/1 challenge requested",
1152 dom->domain);
1153
1154 if ((c = SSL_get_ex_data(ssl, 0)) == NULL)
1155 fatal("%s: no connection data present", __func__);
1156
1157 ptr = dom->acme_cert;
1158 if ((x509 = d2i_X509(NULL, &ptr, dom->acme_cert_len)) == NULL)
1159 fatal("d2i_X509: %s", ssl_errno_s);
1160
1161 if (SSL_use_certificate(ssl, x509) == 0)
1162 fatal("SSL_use_certificate: %s", ssl_errno_s);
1163
1164 SSL_clear_chain_certs(ssl);
1165 SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
1166
1167 c->proto = CONN_PROTO_ACME_ALPN;
1168 }
1169 #endif /* KORE_USE_ACME */