/src/libwebsockets/lib/tls/openssl/openssl-client.c
Line | Count | Source |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "lws_config.h" |
26 | | #ifdef LWS_HAVE_X509_VERIFY_PARAM_set1_host |
27 | | /* Before glibc 2.10, strnlen required _GNU_SOURCE */ |
28 | | #if !defined(_GNU_SOURCE) |
29 | | #define _GNU_SOURCE |
30 | | #endif |
31 | | #endif |
32 | | #include <string.h> |
33 | | |
34 | | #include "private-lib-core.h" |
35 | | #include "private-lib-tls-openssl.h" |
36 | | |
37 | | /* |
38 | | * Care: many openssl apis return 1 for success. These are translated to the |
39 | | * lws convention of 0 for success. |
40 | | */ |
41 | | |
42 | | int lws_openssl_describe_cipher(struct lws *wsi); |
43 | | |
44 | | extern int openssl_websocket_private_data_index, |
45 | | openssl_SSL_CTX_private_data_index; |
46 | | |
47 | | #if !defined(USE_WOLFSSL) |
48 | | |
49 | | #if 0 |
50 | | #if defined(LWS_WITH_TLS_JIT_TRUST) |
51 | | |
52 | | /* |
53 | | * Completion of sync or async JIT trust lookup |
54 | | */ |
55 | | |
56 | | int |
57 | | lws_tls_jit_trust_got_cert_cb(void *got_opaque, const uint8_t *der, |
58 | | size_t der_len) |
59 | | { |
60 | | X509 *x = d2i_X509(NULL, &der, (long)der_len); |
61 | | /** !!! this is not safe for async atm */ |
62 | | struct lws *wsi = (struct lws *)got_opaque; |
63 | | X509_STORE *xs; |
64 | | int ret = 0; |
65 | | |
66 | | if (!x) { |
67 | | lwsl_err("%s: failed\n", __func__); |
68 | | return 1; |
69 | | } |
70 | | |
71 | | xs = SSL_CTX_get_cert_store(SSL_get_SSL_CTX(wsi->tls.ssl)); |
72 | | if (xs) { |
73 | | if (X509_STORE_add_cert(xs, x) != 1) { |
74 | | lwsl_warn("%s: unable to set trusted CA\n", __func__); |
75 | | ret = 1; |
76 | | } else |
77 | | lwsl_notice("%s: added trusted CA to CTX for next time\n", |
78 | | __func__); |
79 | | } else |
80 | | lwsl_warn("%s: couldn't get cert store\n", __func__); |
81 | | |
82 | | X509_free(x); |
83 | | |
84 | | return ret; |
85 | | } |
86 | | #endif |
87 | | #endif |
88 | | |
89 | | static int |
90 | | OpenSSL_client_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
91 | 0 | { |
92 | 0 | SSL *ssl; |
93 | 0 | int n, err = 0; |
94 | 0 | struct lws *wsi; |
95 | 0 | const struct lws_protocols *lp; |
96 | | |
97 | | /* keep old behaviour accepting self-signed server certs */ |
98 | 0 | if (!preverify_ok) { |
99 | 0 | err = X509_STORE_CTX_get_error(x509_ctx); |
100 | |
|
101 | 0 | if (err != X509_V_OK) { |
102 | 0 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
103 | 0 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
104 | 0 | wsi = SSL_get_ex_data(ssl, |
105 | 0 | openssl_websocket_private_data_index); |
106 | 0 | if (!wsi) { |
107 | 0 | lwsl_err("%s: can't get wsi from ssl privdata\n", |
108 | 0 | __func__); |
109 | |
|
110 | 0 | return 0; |
111 | 0 | } |
112 | | |
113 | 0 | if ((err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || |
114 | 0 | err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) && |
115 | 0 | wsi->tls.use_ssl & LCCSCF_ALLOW_SELFSIGNED) { |
116 | 0 | lwsl_notice("accepting self-signed " |
117 | 0 | "certificate (verify_callback)\n"); |
118 | 0 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
119 | 0 | return 1; // ok |
120 | 0 | } else if ((err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY || |
121 | 0 | err == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE) && |
122 | 0 | wsi->tls.use_ssl & LCCSCF_ALLOW_INSECURE) { |
123 | 0 | lwsl_notice("accepting non-trusted certificate\n"); |
124 | 0 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
125 | 0 | return 1; /* ok */ |
126 | 0 | } else if ((err == X509_V_ERR_CERT_NOT_YET_VALID || |
127 | 0 | err == X509_V_ERR_CERT_HAS_EXPIRED) && |
128 | 0 | wsi->tls.use_ssl & LCCSCF_ALLOW_EXPIRED) { |
129 | 0 | if (err == X509_V_ERR_CERT_NOT_YET_VALID) |
130 | 0 | lwsl_notice("accepting not yet valid " |
131 | 0 | "certificate (verify_" |
132 | 0 | "callback)\n"); |
133 | 0 | else if (err == X509_V_ERR_CERT_HAS_EXPIRED) |
134 | 0 | lwsl_notice("accepting expired " |
135 | 0 | "certificate (verify_" |
136 | 0 | "callback)\n"); |
137 | 0 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
138 | 0 | return 1; // ok |
139 | 0 | } |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | 0 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
144 | 0 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
145 | 0 | wsi = SSL_get_ex_data(ssl, openssl_websocket_private_data_index); |
146 | 0 | if (!wsi) { |
147 | 0 | lwsl_err("%s: can't get wsi from ssl privdata\n", __func__); |
148 | |
|
149 | 0 | return 0; |
150 | 0 | } |
151 | | |
152 | | #if defined(LWS_WITH_TLS_JIT_TRUST) |
153 | | if (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) { |
154 | | union lws_tls_cert_info_results ci; |
155 | | STACK_OF(X509) *x509_stack; |
156 | | |
157 | | x509_stack = X509_STORE_CTX_get1_chain(x509_ctx); |
158 | | if (x509_stack) { |
159 | | |
160 | | for (n = 0; n < OPENSSL_sk_num((const OPENSSL_STACK *)x509_stack) && |
161 | | wsi->tls.kid_chain.count != |
162 | | LWS_ARRAY_SIZE(wsi->tls.kid_chain.akid); n++) { |
163 | | X509 *x509 = OPENSSL_sk_value((const OPENSSL_STACK *)x509_stack, n); |
164 | | |
165 | | if (!lws_tls_openssl_cert_info(x509, |
166 | | LWS_TLS_CERT_INFO_SUBJECT_KEY_ID, |
167 | | &ci, 0)) |
168 | | lws_tls_kid_copy(&ci, |
169 | | &wsi->tls.kid_chain.skid[ |
170 | | wsi->tls.kid_chain.count]); |
171 | | |
172 | | if (!lws_tls_openssl_cert_info(x509, |
173 | | LWS_TLS_CERT_INFO_AUTHORITY_KEY_ID, |
174 | | &ci, 0)) |
175 | | lws_tls_kid_copy(&ci, |
176 | | &wsi->tls.kid_chain.akid[ |
177 | | wsi->tls.kid_chain.count]); |
178 | | |
179 | | wsi->tls.kid_chain.count++; |
180 | | } |
181 | | |
182 | | sk_X509_pop_free(x509_stack, X509_free); |
183 | | } |
184 | | |
185 | | lws_tls_jit_trust_sort_kids(wsi, &wsi->tls.kid_chain); |
186 | | } |
187 | | #endif |
188 | 0 | lp = &(lws_get_context_protocol(wsi->a.context, 0)); |
189 | 0 | if (wsi->a.protocol) |
190 | 0 | lp = wsi->a.protocol; |
191 | |
|
192 | 0 | n = lp->callback(wsi, |
193 | 0 | LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION, |
194 | 0 | x509_ctx, ssl, (unsigned int)preverify_ok); |
195 | | |
196 | | /* keep old behaviour if something wrong with server certs */ |
197 | | /* if ssl error is overruled in callback and cert is ok, |
198 | | * X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); must be set and |
199 | | * return value is 0 from callback */ |
200 | 0 | if (!preverify_ok) { |
201 | 0 | int err = X509_STORE_CTX_get_error(x509_ctx); |
202 | |
|
203 | 0 | if (err != X509_V_OK) { |
204 | | /* cert validation error was not handled in callback */ |
205 | 0 | int depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
206 | 0 | const char *msg = X509_verify_cert_error_string(err); |
207 | |
|
208 | 0 | lws_strncpy(wsi->tls.err_helper, msg, |
209 | 0 | sizeof(wsi->tls.err_helper)); |
210 | |
|
211 | 0 | lwsl_err("SSL error: %s (preverify_ok=%d;err=%d;" |
212 | 0 | "depth=%d)\n", msg, preverify_ok, err, depth); |
213 | |
|
214 | 0 | if (err == X509_V_ERR_HOSTNAME_MISMATCH) { |
215 | 0 | const char *expected = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
216 | 0 | char cert_cn[256] = "unknown"; |
217 | 0 | X509 *cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
218 | 0 | if (cert) { |
219 | 0 | X509_NAME *subject = X509_get_subject_name(cert); |
220 | 0 | if (subject) |
221 | 0 | X509_NAME_get_text_by_NID(subject, NID_commonName, cert_cn, sizeof(cert_cn)); |
222 | 0 | } |
223 | 0 | lwsl_err("Hostname mismatch details: Expected='%s', Cert CN='%s'\n", |
224 | 0 | expected ? expected : "unknown", cert_cn); |
225 | 0 | } |
226 | |
|
227 | | #if defined(LWS_WITH_SYS_METRICS) |
228 | | { |
229 | | char buckname[64]; |
230 | | |
231 | | lws_snprintf(buckname, sizeof(buckname), |
232 | | "tls=\"%s\"", msg); |
233 | | lws_metrics_hist_bump_describe_wsi(wsi, |
234 | | lws_metrics_priv_to_pub(wsi->a.context->mth_conn_failures), |
235 | | buckname); |
236 | | } |
237 | | #endif |
238 | |
|
239 | 0 | return preverify_ok; // not ok |
240 | 0 | } |
241 | 0 | } |
242 | | /* |
243 | | * convert callback return code from 0 = OK to verify callback |
244 | | * return value 1 = OK |
245 | | */ |
246 | 0 | return !n; |
247 | 0 | } |
248 | | #endif |
249 | | |
250 | | int |
251 | | lws_ssl_client_bio_create(struct lws *wsi) |
252 | 0 | { |
253 | 0 | char hostname[128], *p; |
254 | 0 | #if (defined(LWS_HAVE_SSL_set_alpn_protos) || defined(OPENSSL_IS_AWSLC)) && \ |
255 | 0 | (defined(LWS_HAVE_SSL_get0_alpn_selected) || defined(OPENSSL_IS_AWSLC)) |
256 | 0 | uint8_t openssl_alpn[40]; |
257 | 0 | const char *alpn_comma = wsi->a.context->tls.alpn_default; |
258 | 0 | int n; |
259 | 0 | #endif |
260 | |
|
261 | 0 | if (wsi->stash) { |
262 | 0 | lws_strncpy(hostname, wsi->stash->cis[CIS_HOST], sizeof(hostname)); |
263 | 0 | #if (defined(LWS_HAVE_SSL_set_alpn_protos) || defined(OPENSSL_IS_AWSLC)) && \ |
264 | 0 | (defined(LWS_HAVE_SSL_get0_alpn_selected) || defined(OPENSSL_IS_AWSLC)) |
265 | 0 | alpn_comma = wsi->stash->cis[CIS_ALPN]; |
266 | 0 | #endif |
267 | 0 | } else { |
268 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
269 | 0 | if (lws_hdr_copy(wsi, hostname, sizeof(hostname), |
270 | 0 | _WSI_TOKEN_CLIENT_HOST) <= 0) |
271 | 0 | #endif |
272 | 0 | { |
273 | 0 | lwsl_err("%s: Unable to get hostname\n", __func__); |
274 | |
|
275 | 0 | return -1; |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | | /* |
280 | | * remove any :port part on the hostname... necessary for network |
281 | | * connection but typical certificates do not contain it |
282 | | */ |
283 | 0 | p = hostname; |
284 | 0 | while (*p) { |
285 | 0 | if (*p == ':') { |
286 | 0 | *p = '\0'; |
287 | 0 | break; |
288 | 0 | } |
289 | 0 | p++; |
290 | 0 | } |
291 | |
|
292 | 0 | wsi->tls.ssl = SSL_new(wsi->a.vhost->tls.ssl_client_ctx); |
293 | 0 | if (!wsi->tls.ssl) { |
294 | 0 | unsigned long err = ERR_get_error(); |
295 | 0 | const char *es = ERR_error_string(LWS_TLS_ERR_CAST(err), NULL); |
296 | 0 | lwsl_err("SSL_new failed: %s (real error %lu)\n", es, err); |
297 | 0 | lws_tls_err_describe_clear(); |
298 | 0 | return -1; |
299 | 0 | } |
300 | | |
301 | 0 | #if defined(LWS_WITH_TLS_SESSIONS) |
302 | 0 | if (!(wsi->a.vhost->options & LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE)) |
303 | 0 | lws_tls_reuse_session(wsi); |
304 | 0 | #endif |
305 | |
|
306 | 0 | #if defined (LWS_HAVE_SSL_SET_INFO_CALLBACK) |
307 | 0 | if (wsi->a.vhost->tls.ssl_info_event_mask) |
308 | 0 | SSL_set_info_callback(wsi->tls.ssl, lws_ssl_info_callback); |
309 | 0 | #endif |
310 | |
|
311 | 0 | #if defined(LWS_HAVE_X509_VERIFY_PARAM_set1_host) |
312 | 0 | if (!(wsi->tls.use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)) { |
313 | 0 | #if !defined(USE_WOLFSSL) |
314 | |
|
315 | 0 | X509_VERIFY_PARAM *param = SSL_get0_param(wsi->tls.ssl); |
316 | | |
317 | | /* Enable automatic hostname checks */ |
318 | 0 | X509_VERIFY_PARAM_set_hostflags(param, |
319 | 0 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
320 | | /* Handle the case where the hostname is an IP address */ |
321 | 0 | if (!X509_VERIFY_PARAM_set1_ip_asc(param, hostname)) |
322 | 0 | X509_VERIFY_PARAM_set1_host(param, hostname, |
323 | 0 | strnlen(hostname, sizeof(hostname))); |
324 | 0 | #endif |
325 | |
|
326 | 0 | } |
327 | | #else |
328 | | if (!(wsi->tls.use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)) { |
329 | | lwsl_err("%s: your tls lib is too old to have " |
330 | | "X509_VERIFY_PARAM_set1_host, failing all client tls\n", |
331 | | __func__); |
332 | | return -1; |
333 | | } |
334 | | #endif |
335 | |
|
336 | 0 | #if !defined(USE_WOLFSSL) |
337 | 0 | #ifndef USE_OLD_CYASSL |
338 | | /* OpenSSL_client_verify_callback will be called @ SSL_connect() */ |
339 | 0 | SSL_set_verify(wsi->tls.ssl, SSL_VERIFY_PEER, |
340 | 0 | OpenSSL_client_verify_callback); |
341 | 0 | #endif |
342 | 0 | #endif |
343 | |
|
344 | 0 | #if !defined(USE_WOLFSSL) |
345 | 0 | SSL_set_mode(wsi->tls.ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
346 | 0 | #endif |
347 | | /* |
348 | | * use server name indication (SNI), if supported, |
349 | | * when establishing connection |
350 | | */ |
351 | | #ifdef USE_WOLFSSL |
352 | | #ifdef USE_OLD_CYASSL |
353 | | #ifdef CYASSL_SNI_HOST_NAME |
354 | | CyaSSL_UseSNI(wsi->tls.ssl, CYASSL_SNI_HOST_NAME, hostname, |
355 | | strlen(hostname)); |
356 | | #endif |
357 | | #else |
358 | | #if defined(WOLFSSL_SNI_HOST_NAME) || defined(HAVE_SNI) |
359 | | wolfSSL_UseSNI(wsi->tls.ssl, WOLFSSL_SNI_HOST_NAME, hostname, |
360 | | (unsigned short)strlen(hostname)); |
361 | | #endif |
362 | | #endif |
363 | | #else |
364 | 0 | #if defined(SSL_CTRL_SET_TLSEXT_HOSTNAME) || defined(LWS_HAVE_SSL_set_tlsext_host_name) |
365 | 0 | SSL_set_tlsext_host_name(wsi->tls.ssl, hostname); |
366 | 0 | #endif |
367 | 0 | #endif |
368 | |
|
369 | | #ifdef USE_WOLFSSL |
370 | | /* |
371 | | * wolfSSL/CyaSSL does certificate verification differently |
372 | | * from OpenSSL. |
373 | | * If we should ignore the certificate, we need to set |
374 | | * this before SSL_new and SSL_connect is called. |
375 | | * Otherwise the connect will simply fail with error code -155 |
376 | | */ |
377 | | #ifdef USE_OLD_CYASSL |
378 | | if (wsi->tls.use_ssl & LCCSCF_ALLOW_SELFSIGNED) |
379 | | CyaSSL_set_verify(wsi->tls.ssl, SSL_VERIFY_NONE, NULL); |
380 | | #else |
381 | | if (wsi->tls.use_ssl & LCCSCF_ALLOW_SELFSIGNED) |
382 | | wolfSSL_set_verify(wsi->tls.ssl, SSL_VERIFY_NONE, NULL); |
383 | | #endif |
384 | | #endif /* USE_WOLFSSL */ |
385 | |
|
386 | 0 | wsi->tls.client_bio = BIO_new_socket((int)(lws_intptr_t)wsi->desc.sockfd, |
387 | 0 | BIO_NOCLOSE); |
388 | 0 | SSL_set_bio(wsi->tls.ssl, wsi->tls.client_bio, wsi->tls.client_bio); |
389 | |
|
390 | | #ifdef USE_WOLFSSL |
391 | | #ifdef USE_OLD_CYASSL |
392 | | CyaSSL_set_using_nonblock(wsi->tls.ssl, 1); |
393 | | #else |
394 | | wolfSSL_set_using_nonblock(wsi->tls.ssl, 1); |
395 | | #endif |
396 | | #else |
397 | 0 | BIO_set_nbio(wsi->tls.client_bio, 1); /* nonblocking */ |
398 | 0 | #endif |
399 | |
|
400 | 0 | #if (defined(LWS_HAVE_SSL_set_alpn_protos) || defined(OPENSSL_IS_AWSLC)) && \ |
401 | 0 | (defined(LWS_HAVE_SSL_get0_alpn_selected) || defined(OPENSSL_IS_AWSLC)) |
402 | 0 | if (wsi->a.vhost->tls.alpn) |
403 | 0 | alpn_comma = wsi->a.vhost->tls.alpn; |
404 | 0 | if (wsi->stash) { |
405 | 0 | alpn_comma = wsi->stash->cis[CIS_ALPN]; |
406 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
407 | 0 | } else { |
408 | 0 | if (lws_hdr_copy(wsi, hostname, sizeof(hostname), |
409 | 0 | _WSI_TOKEN_CLIENT_ALPN) > 0) |
410 | 0 | alpn_comma = hostname; |
411 | 0 | #endif |
412 | 0 | } |
413 | |
|
414 | 0 | lwsl_info("%s client conn using alpn list '%s'\n", wsi->role_ops->name, alpn_comma); |
415 | |
|
416 | 0 | n = lws_alpn_comma_to_openssl(alpn_comma, openssl_alpn, |
417 | 0 | sizeof(openssl_alpn) - 1); |
418 | |
|
419 | 0 | SSL_set_alpn_protos(wsi->tls.ssl, openssl_alpn, (unsigned int)n); |
420 | 0 | #endif |
421 | |
|
422 | 0 | SSL_set_ex_data(wsi->tls.ssl, openssl_websocket_private_data_index, |
423 | 0 | wsi); |
424 | |
|
425 | 0 | if (wsi->sys_tls_client_cert) { |
426 | 0 | lws_system_blob_t *b = lws_system_get_blob(wsi->a.context, |
427 | 0 | LWS_SYSBLOB_TYPE_CLIENT_CERT_DER, |
428 | 0 | wsi->sys_tls_client_cert - 1); |
429 | 0 | const uint8_t *data; |
430 | 0 | size_t size; |
431 | |
|
432 | 0 | if (!b) |
433 | 0 | goto no_client_cert; |
434 | | |
435 | | /* |
436 | | * Set up the per-connection client cert |
437 | | */ |
438 | | |
439 | 0 | size = lws_system_blob_get_size(b); |
440 | 0 | if (!size) |
441 | 0 | goto no_client_cert; |
442 | | |
443 | 0 | if (lws_system_blob_get_single_ptr(b, &data)) |
444 | 0 | goto no_client_cert; |
445 | | |
446 | 0 | if (SSL_use_certificate_ASN1(wsi->tls.ssl, SSL_DATA_CAST(data), |
447 | 0 | SSL_SIZE_T_CAST(size)) != 1) { |
448 | 0 | lwsl_err("%s: use_certificate failed\n", __func__); |
449 | 0 | lws_tls_err_describe_clear(); |
450 | 0 | goto no_client_cert; |
451 | 0 | } |
452 | | |
453 | 0 | b = lws_system_get_blob(wsi->a.context, |
454 | 0 | LWS_SYSBLOB_TYPE_CLIENT_KEY_DER, |
455 | 0 | wsi->sys_tls_client_cert - 1); |
456 | 0 | if (!b) |
457 | 0 | goto no_client_cert; |
458 | | |
459 | 0 | size = lws_system_blob_get_size(b); |
460 | 0 | if (!size) |
461 | 0 | goto no_client_cert; |
462 | | |
463 | 0 | if (lws_system_blob_get_single_ptr(b, &data)) |
464 | 0 | goto no_client_cert; |
465 | | |
466 | 0 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, wsi->tls.ssl, SSL_DATA_CAST(data), |
467 | 0 | SSL_SIZE_T_CAST(size)) != 1 && |
468 | 0 | SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, wsi->tls.ssl, SSL_DATA_CAST(data), |
469 | 0 | SSL_SIZE_T_CAST(size)) != 1) { |
470 | |
|
471 | 0 | lwsl_err("%s: use_privkey failed\n", __func__); |
472 | 0 | lws_tls_err_describe_clear(); |
473 | 0 | goto no_client_cert; |
474 | 0 | } |
475 | | |
476 | 0 | if (SSL_check_private_key(wsi->tls.ssl) != 1) { |
477 | 0 | lwsl_err("Private SSL key doesn't match cert\n"); |
478 | 0 | lws_tls_err_describe_clear(); |
479 | 0 | return 1; |
480 | 0 | } |
481 | | |
482 | 0 | lwsl_notice("%s: set system client cert %u\n", __func__, |
483 | 0 | wsi->sys_tls_client_cert - 1); |
484 | 0 | } |
485 | | |
486 | 0 | return 0; |
487 | | |
488 | 0 | no_client_cert: |
489 | 0 | lwsl_err("%s: unable to set up system client cert %d\n", __func__, |
490 | 0 | wsi->sys_tls_client_cert - 1); |
491 | |
|
492 | 0 | return 1; |
493 | 0 | } |
494 | | |
495 | | #if defined(LWS_WITH_TCP_TLS) |
496 | | enum lws_ssl_capable_status |
497 | | lws_tls_client_connect(struct lws *wsi, char *errbuf, size_t elen) |
498 | 0 | { |
499 | 0 | #if (defined(LWS_HAVE_SSL_set_alpn_protos) || defined(OPENSSL_IS_AWSLC)) && \ |
500 | 0 | (defined(LWS_HAVE_SSL_get0_alpn_selected) || defined(OPENSSL_IS_AWSLC)) |
501 | 0 | const unsigned char *prot; |
502 | 0 | char a[32]; |
503 | 0 | unsigned int len; |
504 | 0 | #endif |
505 | 0 | int m, n, en; |
506 | 0 | unsigned long l; |
507 | 0 | #if defined(LWS_WITH_TLS_SESSIONS) && defined(LWS_HAVE_SSL_SESSION_set_time) |
508 | 0 | SSL_SESSION *sess; |
509 | 0 | #endif |
510 | 0 | errno = 0; |
511 | 0 | ERR_clear_error(); |
512 | 0 | wsi->tls.err_helper[0] = '\0'; |
513 | 0 | n = SSL_connect(wsi->tls.ssl); |
514 | 0 | en = errno; |
515 | |
|
516 | 0 | m = lws_ssl_get_error(wsi, n); |
517 | |
|
518 | 0 | if (m == SSL_ERROR_SYSCALL |
519 | | #if defined(WIN32) |
520 | | && en |
521 | | #endif |
522 | 0 | ) { |
523 | 0 | #if defined(WIN32) || (_LWS_ENABLED_LOGS & LLL_INFO) |
524 | 0 | lwsl_info("%s: n %d, m %d, errno %d\n", __func__, n, m, en); |
525 | 0 | #endif |
526 | 0 | lws_snprintf(errbuf, elen, "connect SYSCALL %d", en); |
527 | 0 | return LWS_SSL_CAPABLE_ERROR; |
528 | 0 | } |
529 | | |
530 | 0 | if (m == SSL_ERROR_SSL) { |
531 | 0 | l = ERR_get_error(); |
532 | 0 | n = lws_snprintf(errbuf, elen, "tls: %s", wsi->tls.err_helper); |
533 | 0 | if (!wsi->tls.err_helper[0]) |
534 | 0 | ERR_error_string_n(LWS_TLS_ERR_CAST(l), errbuf + n, (elen - (unsigned int)n)); |
535 | 0 | return LWS_SSL_CAPABLE_ERROR; |
536 | 0 | } |
537 | | |
538 | 0 | #if defined(LWS_WITH_TLS_SESSIONS) |
539 | 0 | if (SSL_session_reused(wsi->tls.ssl)) { |
540 | 0 | #if defined(LWS_HAVE_SSL_SESSION_set_time) |
541 | 0 | sess = SSL_get_session(wsi->tls.ssl); |
542 | 0 | if (sess) /* should always be true */ |
543 | | #if defined(OPENSSL_IS_BORINGSSL) || defined(LWS_WITH_AWSLC) |
544 | | SSL_SESSION_set_time(sess, (uint64_t)time(NULL)); /* extend session lifetime */ |
545 | | #else |
546 | 0 | SSL_SESSION_set_time(sess, (long)time(NULL)); /* extend session lifetime */ |
547 | 0 | #endif |
548 | 0 | #endif |
549 | 0 | } |
550 | 0 | #endif |
551 | |
|
552 | 0 | if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) |
553 | 0 | return LWS_SSL_CAPABLE_MORE_SERVICE_READ; |
554 | | |
555 | 0 | if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) |
556 | 0 | return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE; |
557 | | |
558 | 0 | if (n == 1 || m == SSL_ERROR_SYSCALL) { |
559 | 0 | #if (defined(LWS_HAVE_SSL_set_alpn_protos) || defined(OPENSSL_IS_AWSLC)) && \ |
560 | 0 | (defined(LWS_HAVE_SSL_get0_alpn_selected) || defined(OPENSSL_IS_AWSLC)) |
561 | 0 | SSL_get0_alpn_selected(wsi->tls.ssl, &prot, &len); |
562 | |
|
563 | 0 | if (len >= sizeof(a)) |
564 | 0 | len = sizeof(a) - 1; |
565 | 0 | memcpy(a, (const char *)prot, len); |
566 | 0 | a[len] = '\0'; |
567 | |
|
568 | 0 | lws_role_call_alpn_negotiated(wsi, (const char *)a); |
569 | 0 | #endif |
570 | | #if defined(LWS_TLS_SYNTHESIZE_CB) |
571 | | lws_sul_schedule(wsi->a.context, wsi->tsi, |
572 | | &wsi->tls.sul_cb_synth, |
573 | | lws_sess_cache_synth_cb, 500 * LWS_US_PER_MS); |
574 | | #endif |
575 | |
|
576 | 0 | lwsl_info("client connect OK\n"); |
577 | 0 | lws_openssl_describe_cipher(wsi); |
578 | 0 | return LWS_SSL_CAPABLE_DONE; |
579 | 0 | } |
580 | | |
581 | 0 | if (!n) /* we don't know what he wants, but he says to retry */ |
582 | 0 | return LWS_SSL_CAPABLE_MORE_SERVICE_READ; |
583 | | |
584 | 0 | lws_snprintf(errbuf, elen, "connect unk %d", m); |
585 | |
|
586 | 0 | return LWS_SSL_CAPABLE_ERROR; |
587 | 0 | } |
588 | | |
589 | | int |
590 | | lws_tls_client_confirm_peer_cert(struct lws *wsi, char *ebuf, size_t ebuf_len) |
591 | 0 | { |
592 | 0 | #if !defined(USE_WOLFSSL) |
593 | 0 | char buf[256]; |
594 | 0 | const char *es, *type = ""; |
595 | 0 | unsigned int avoid = 0; |
596 | 0 | long n; |
597 | |
|
598 | 0 | errno = 0; |
599 | 0 | ERR_clear_error(); |
600 | 0 | n = SSL_get_verify_result(wsi->tls.ssl); |
601 | |
|
602 | 0 | switch (n) { |
603 | 0 | case X509_V_OK: |
604 | 0 | return 0; |
605 | | |
606 | 0 | case X509_V_ERR_HOSTNAME_MISMATCH: |
607 | 0 | type = "tls=hostname"; |
608 | 0 | avoid = LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK; |
609 | 0 | break; |
610 | | |
611 | 0 | case X509_V_ERR_INVALID_CA: |
612 | 0 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
613 | 0 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
614 | 0 | type = "tls=invalidca"; |
615 | 0 | avoid = LCCSCF_ALLOW_SELFSIGNED; |
616 | 0 | break; |
617 | | |
618 | 0 | case X509_V_ERR_CERT_NOT_YET_VALID: |
619 | 0 | type = "tls=notyetvalid"; |
620 | 0 | avoid = LCCSCF_ALLOW_EXPIRED; |
621 | 0 | break; |
622 | | |
623 | 0 | case X509_V_ERR_CERT_HAS_EXPIRED: |
624 | 0 | type = "tls=expired"; |
625 | 0 | avoid = LCCSCF_ALLOW_EXPIRED; |
626 | 0 | break; |
627 | 0 | } |
628 | | |
629 | 0 | lwsl_info("%s: cert problem: %s\n", __func__, type); |
630 | |
|
631 | | #if defined(LWS_WITH_SYS_METRICS) |
632 | | lws_metrics_hist_bump_describe_wsi(wsi, |
633 | | lws_metrics_priv_to_pub(wsi->a.context->mth_conn_failures), type); |
634 | | #endif |
635 | |
|
636 | 0 | if (wsi->tls.use_ssl & avoid) { |
637 | 0 | lwsl_info("%s: allowing anyway\n", __func__); |
638 | |
|
639 | 0 | return 0; |
640 | 0 | } |
641 | | |
642 | 0 | es = ERR_error_string(LWS_TLS_ERR_CAST(n), buf); |
643 | 0 | lws_snprintf(ebuf, ebuf_len, |
644 | 0 | "server's cert didn't look good, %s X509_V_ERR = %ld: %s\n", |
645 | 0 | type, n, es); |
646 | 0 | lwsl_info("%s\n", ebuf); |
647 | 0 | lws_tls_err_describe_clear(); |
648 | |
|
649 | 0 | return -1; |
650 | |
|
651 | | #else /* USE_WOLFSSL */ |
652 | | return 0; |
653 | | #endif |
654 | 0 | } |
655 | | #endif |
656 | | |
657 | | int |
658 | | lws_tls_client_vhost_extra_cert_mem(struct lws_vhost *vh, |
659 | | const uint8_t *der, size_t der_len) |
660 | 0 | { |
661 | 0 | X509_STORE *st; |
662 | | #if defined(USE_WOLFSSL) |
663 | | X509 *x = d2i_X509(NULL, &der, (int)der_len); |
664 | | #else |
665 | 0 | X509 *x = d2i_X509(NULL, &der, (long)der_len); |
666 | 0 | #endif |
667 | 0 | int n; |
668 | |
|
669 | 0 | if (!x) { |
670 | 0 | lwsl_err("%s: Failed to load DER\n", __func__); |
671 | 0 | lws_tls_err_describe_clear(); |
672 | 0 | return 1; |
673 | 0 | } |
674 | | |
675 | 0 | st = SSL_CTX_get_cert_store(vh->tls.ssl_client_ctx); |
676 | 0 | if (!st) { |
677 | 0 | st = X509_STORE_new(); |
678 | 0 | if (!st) { |
679 | 0 | lwsl_err("%s: failed to create cert store\n", __func__); |
680 | 0 | X509_free(x); |
681 | 0 | return 1; |
682 | 0 | } |
683 | 0 | SSL_CTX_set_cert_store(vh->tls.ssl_client_ctx, st); |
684 | 0 | } |
685 | | |
686 | 0 | n = X509_STORE_add_cert(st, x); |
687 | 0 | if (n != 1) |
688 | 0 | lwsl_err("%s: failed to add cert\n", __func__); |
689 | |
|
690 | 0 | X509_free(x); |
691 | |
|
692 | 0 | return n != 1; |
693 | 0 | } |
694 | | |
695 | | int |
696 | | lws_tls_client_create_vhost_context(struct lws_vhost *vh, |
697 | | const struct lws_context_creation_info *info, |
698 | | const char *cipher_list, |
699 | | const char *ca_filepath, |
700 | | const void *ca_mem, |
701 | | unsigned int ca_mem_len, |
702 | | const char *cert_filepath, |
703 | | const void *cert_mem, |
704 | | unsigned int cert_mem_len, |
705 | | const char *private_key_filepath, |
706 | | const void *key_mem, |
707 | | unsigned int key_mem_len |
708 | | ) |
709 | 0 | { |
710 | 0 | struct lws_tls_client_reuse *tcr; |
711 | 0 | unsigned long error; |
712 | 0 | SSL_METHOD *method; |
713 | 0 | EVP_MD_CTX *mdctx; |
714 | 0 | unsigned int len; |
715 | 0 | uint8_t hash[32]; |
716 | 0 | char c; |
717 | 0 | int n; |
718 | | |
719 | | /* basic openssl init already happened in context init */ |
720 | | |
721 | | /* choose the most recent spin of the api */ |
722 | 0 | #if defined(LWS_HAVE_TLS_CLIENT_METHOD) |
723 | 0 | method = (SSL_METHOD *)TLS_client_method(); |
724 | | #elif defined(LWS_HAVE_TLSV1_2_CLIENT_METHOD) |
725 | | method = (SSL_METHOD *)TLSv1_2_client_method(); |
726 | | #else |
727 | | method = (SSL_METHOD *)SSLv23_client_method(); |
728 | | #endif |
729 | |
|
730 | 0 | if (!method) { |
731 | 0 | const char *es; |
732 | 0 | char buf[256]; |
733 | |
|
734 | 0 | error = ERR_peek_error(); |
735 | 0 | es = ERR_error_string(LWS_TLS_ERR_CAST(ERR_get_error()), buf); |
736 | 0 | lwsl_err("problem creating ssl method %lu: %s\n", |
737 | 0 | error, es); |
738 | 0 | return 1; |
739 | 0 | } |
740 | | |
741 | | /* |
742 | | * OpenSSL client contexts are quite expensive, because they bring in |
743 | | * the system certificate bundle for each one. So if you have multiple |
744 | | * vhosts, each with a client context, it can add up to several |
745 | | * megabytes of heap. In the case the client contexts are configured |
746 | | * identically, they could perfectly well have shared just the one. |
747 | | * |
748 | | * For that reason, use a hash to fingerprint the context configuration |
749 | | * and prefer to reuse an existing one with the same fingerprint if |
750 | | * possible. |
751 | | */ |
752 | | |
753 | 0 | mdctx = EVP_MD_CTX_create(); |
754 | 0 | if (!mdctx) |
755 | 0 | return 1; |
756 | | |
757 | 0 | if (EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1) { |
758 | 0 | EVP_MD_CTX_destroy(mdctx); |
759 | |
|
760 | 0 | return 1; |
761 | 0 | } |
762 | | |
763 | 0 | if (info->ssl_client_options_set) |
764 | 0 | EVP_DigestUpdate(mdctx, &info->ssl_client_options_set, |
765 | 0 | sizeof(info->ssl_client_options_set)); |
766 | |
|
767 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x009080df) && !defined(USE_WOLFSSL) |
768 | 0 | if (info->ssl_client_options_clear) |
769 | 0 | EVP_DigestUpdate(mdctx, &info->ssl_client_options_clear, |
770 | 0 | sizeof(info->ssl_client_options_clear)); |
771 | 0 | #endif |
772 | |
|
773 | 0 | if (info->client_tls_ciphers_iana) |
774 | 0 | EVP_DigestUpdate(mdctx, info->client_tls_ciphers_iana, |
775 | 0 | strlen(info->client_tls_ciphers_iana)); |
776 | 0 | else { |
777 | 0 | if (cipher_list) |
778 | 0 | EVP_DigestUpdate(mdctx, cipher_list, strlen(cipher_list)); |
779 | |
|
780 | 0 | #if defined(LWS_HAVE_SSL_CTX_set_ciphersuites) |
781 | 0 | if (info->client_tls_1_3_plus_cipher_list) |
782 | 0 | EVP_DigestUpdate(mdctx, info->client_tls_1_3_plus_cipher_list, |
783 | 0 | strlen(info->client_tls_1_3_plus_cipher_list)); |
784 | 0 | #endif |
785 | 0 | } |
786 | |
|
787 | 0 | if (!lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS)) { |
788 | 0 | c = 1; |
789 | 0 | EVP_DigestUpdate(mdctx, &c, 1); |
790 | 0 | } |
791 | |
|
792 | 0 | if (ca_filepath) |
793 | 0 | EVP_DigestUpdate(mdctx, ca_filepath, strlen(ca_filepath)); |
794 | |
|
795 | 0 | if (cert_filepath) |
796 | 0 | EVP_DigestUpdate(mdctx, cert_filepath, strlen(cert_filepath)); |
797 | |
|
798 | 0 | if (private_key_filepath) |
799 | 0 | EVP_DigestUpdate(mdctx, private_key_filepath, |
800 | 0 | strlen(private_key_filepath)); |
801 | 0 | if (ca_mem && ca_mem_len) |
802 | 0 | EVP_DigestUpdate(mdctx, ca_mem, ca_mem_len); |
803 | |
|
804 | 0 | if (cert_mem && cert_mem_len) |
805 | 0 | EVP_DigestUpdate(mdctx, cert_mem, cert_mem_len); |
806 | |
|
807 | 0 | len = sizeof(hash); |
808 | 0 | EVP_DigestFinal_ex(mdctx, hash, &len); |
809 | 0 | EVP_MD_CTX_destroy(mdctx); |
810 | | |
811 | | /* look for existing client context with same config already */ |
812 | |
|
813 | 0 | lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp, |
814 | 0 | lws_dll2_get_head(&vh->context->tls.cc_owner)) { |
815 | 0 | tcr = lws_container_of(p, struct lws_tls_client_reuse, cc_list); |
816 | |
|
817 | 0 | if (!memcmp(hash, tcr->hash, len)) { |
818 | | |
819 | | /* it's a match */ |
820 | |
|
821 | 0 | tcr->refcount++; |
822 | 0 | vh->tls.ssl_client_ctx = tcr->ssl_client_ctx; |
823 | 0 | vh->tls.tcr = tcr; |
824 | |
|
825 | 0 | lwsl_info("%s: vh %s: reusing client ctx %d: use %d\n", |
826 | 0 | __func__, vh->name, tcr->index, |
827 | 0 | tcr->refcount); |
828 | |
|
829 | 0 | return 0; |
830 | 0 | } |
831 | 0 | } lws_end_foreach_dll_safe(p, tp); |
832 | | |
833 | | /* no existing one the same... create new client SSL_CTX */ |
834 | |
|
835 | 0 | errno = 0; |
836 | 0 | ERR_clear_error(); |
837 | 0 | vh->tls.ssl_client_ctx = SSL_CTX_new(method); |
838 | 0 | if (!vh->tls.ssl_client_ctx) { |
839 | 0 | const char *es; |
840 | 0 | char buf[256]; |
841 | |
|
842 | 0 | error = ERR_peek_error(); |
843 | 0 | es = ERR_error_string(LWS_TLS_ERR_CAST(ERR_get_error()), buf); |
844 | 0 | lwsl_err("problem creating ssl context %lu: %s\n", |
845 | 0 | error, es); |
846 | 0 | return 1; |
847 | 0 | } |
848 | | |
849 | 0 | SSL_CTX_set_ex_data(vh->tls.ssl_client_ctx, |
850 | 0 | openssl_SSL_CTX_private_data_index, |
851 | 0 | (char *)vh->context); |
852 | |
|
853 | 0 | lws_plat_vhost_tls_client_ctx_init(vh); |
854 | |
|
855 | 0 | tcr = lws_zalloc(sizeof(*tcr), "client ctx tcr"); |
856 | 0 | if (!tcr) { |
857 | 0 | SSL_CTX_free(vh->tls.ssl_client_ctx); |
858 | 0 | return 1; |
859 | 0 | } |
860 | | |
861 | 0 | tcr->ssl_client_ctx = vh->tls.ssl_client_ctx; |
862 | 0 | tcr->refcount = 1; |
863 | 0 | memcpy(tcr->hash, hash, len); |
864 | 0 | tcr->index = vh->context->tls.count_client_contexts++; |
865 | 0 | lws_dll2_add_head(&tcr->cc_list, &vh->context->tls.cc_owner); |
866 | |
|
867 | 0 | lwsl_info("%s: vh %s: created new client ctx %d\n", __func__, |
868 | 0 | vh->name, tcr->index); |
869 | | |
870 | | /* bind the tcr to the client context */ |
871 | |
|
872 | 0 | vh->tls.tcr = tcr; |
873 | |
|
874 | 0 | #if defined(LWS_WITH_TLS_KEYLOG) && \ |
875 | 0 | defined(LWS_WITH_TLS) && defined(LWS_WITH_CLIENT) |
876 | 0 | if (vh->context->keylog_file[0]) |
877 | 0 | SSL_CTX_set_keylog_callback(vh->tls.ssl_client_ctx, lws_klog_dump); |
878 | 0 | #endif |
879 | |
|
880 | 0 | #if defined(LWS_WITH_TLS_SESSIONS) |
881 | 0 | vh->tls_session_cache_max = info->tls_session_cache_max ? |
882 | 0 | info->tls_session_cache_max : 10; |
883 | 0 | lws_tls_session_cache(vh, info->tls_session_timeout); |
884 | 0 | #endif |
885 | |
|
886 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
887 | 0 | SSL_CTX_set_options(vh->tls.ssl_client_ctx, SSL_OP_NO_COMPRESSION); |
888 | 0 | #endif |
889 | |
|
890 | 0 | SSL_CTX_set_options(vh->tls.ssl_client_ctx, |
891 | 0 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
892 | |
|
893 | 0 | SSL_CTX_set_mode(vh->tls.ssl_client_ctx, |
894 | 0 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
895 | 0 | SSL_MODE_RELEASE_BUFFERS); |
896 | |
|
897 | 0 | SSL_OPT_TYPE ssl_client_options_set_value = (SSL_OPT_TYPE) info->ssl_client_options_set; |
898 | |
|
899 | 0 | if (info->ssl_client_options_set) |
900 | 0 | SSL_CTX_set_options(vh->tls.ssl_client_ctx, ssl_client_options_set_value); |
901 | |
|
902 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x009080df) && !defined(USE_WOLFSSL) |
903 | | /* SSL_clear_options introduced in 0.9.8m */ |
904 | 0 | SSL_OPT_TYPE ssl_client_options_clear_value = (SSL_OPT_TYPE) info->ssl_client_options_clear; |
905 | |
|
906 | 0 | if (info->ssl_client_options_clear) |
907 | 0 | SSL_CTX_clear_options(vh->tls.ssl_client_ctx, ssl_client_options_clear_value); |
908 | 0 | #endif |
909 | |
|
910 | 0 | if (info->client_tls_ciphers_iana) { |
911 | 0 | char *p = lws_strdup(info->client_tls_ciphers_iana); |
912 | 0 | if (p) { |
913 | 0 | char *q = p; |
914 | 0 | while (*q) { |
915 | 0 | if (*q == ',') |
916 | 0 | *q = ':'; |
917 | 0 | q++; |
918 | 0 | } |
919 | 0 | SSL_CTX_set_cipher_list(vh->tls.ssl_client_ctx, p); |
920 | 0 | #if defined(LWS_HAVE_SSL_CTX_set_ciphersuites) |
921 | 0 | SSL_CTX_set_ciphersuites(vh->tls.ssl_client_ctx, p); |
922 | 0 | #endif |
923 | 0 | lws_free(p); |
924 | 0 | } |
925 | 0 | } else { |
926 | 0 | if (cipher_list) |
927 | 0 | SSL_CTX_set_cipher_list(vh->tls.ssl_client_ctx, cipher_list); |
928 | |
|
929 | 0 | #if defined(LWS_HAVE_SSL_CTX_set_ciphersuites) |
930 | 0 | if (info->client_tls_1_3_plus_cipher_list) |
931 | 0 | SSL_CTX_set_ciphersuites(vh->tls.ssl_client_ctx, |
932 | 0 | info->client_tls_1_3_plus_cipher_list); |
933 | 0 | #endif |
934 | 0 | } |
935 | |
|
936 | 0 | #ifdef LWS_SSL_CLIENT_USE_OS_CA_CERTS |
937 | 0 | if (!lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS)) |
938 | | /* loads OS default CA certs */ |
939 | 0 | SSL_CTX_set_default_verify_paths(vh->tls.ssl_client_ctx); |
940 | 0 | #endif |
941 | | |
942 | | /* openssl init for cert verification (for client sockets) */ |
943 | 0 | if (!ca_filepath && (!ca_mem || !ca_mem_len)) { |
944 | | #if defined(LWS_HAVE_SSL_CTX_load_verify_dir) |
945 | | if (!SSL_CTX_load_verify_dir( |
946 | | vh->tls.ssl_client_ctx, LWS_OPENSSL_CLIENT_CERTS)) |
947 | | #else |
948 | 0 | if (!SSL_CTX_load_verify_locations( |
949 | 0 | vh->tls.ssl_client_ctx, NULL, LWS_OPENSSL_CLIENT_CERTS)) |
950 | 0 | #endif |
951 | 0 | lwsl_err("Unable to load SSL Client certs from %s " |
952 | 0 | "(set by LWS_OPENSSL_CLIENT_CERTS) -- " |
953 | 0 | "client ssl isn't going to work\n", |
954 | 0 | LWS_OPENSSL_CLIENT_CERTS); |
955 | 0 | } else if (ca_filepath) { |
956 | | #if defined(LWS_HAVE_SSL_CTX_load_verify_file) |
957 | | if (!SSL_CTX_load_verify_file( |
958 | | vh->tls.ssl_client_ctx, ca_filepath)) { |
959 | | #else |
960 | 0 | if (!SSL_CTX_load_verify_locations( |
961 | 0 | vh->tls.ssl_client_ctx, ca_filepath, NULL)) { |
962 | 0 | #endif |
963 | 0 | lwsl_err( |
964 | 0 | "Unable to load SSL Client certs " |
965 | 0 | "file from %s -- client ssl isn't " |
966 | 0 | "going to work\n", ca_filepath); |
967 | 0 | lws_tls_err_describe_clear(); |
968 | 0 | } |
969 | 0 | else |
970 | 0 | lwsl_info("loaded ssl_ca_filepath\n"); |
971 | 0 | } else { |
972 | |
|
973 | 0 | if (lws_tls_client_vhost_ca_mem_parse(vh, ca_mem, ca_mem_len)) { |
974 | 0 | lwsl_err("%s: Unable to load x.509 ca_mem\n", __func__); |
975 | 0 | return 1; |
976 | 0 | } |
977 | 0 | } |
978 | | |
979 | | /* |
980 | | * callback allowing user code to load extra verification certs |
981 | | * helping the client to verify server identity |
982 | | */ |
983 | | |
984 | | /* support for client-side certificate authentication */ |
985 | | |
986 | 0 | if (cert_filepath) { |
987 | 0 | if (lws_tls_use_any_upgrade_check_extant(cert_filepath) != |
988 | 0 | LWS_TLS_EXTANT_YES && |
989 | 0 | (info->options & LWS_SERVER_OPTION_IGNORE_MISSING_CERT)) |
990 | 0 | return 0; |
991 | | |
992 | 0 | lwsl_notice("%s: doing cert filepath %s\n", __func__, |
993 | 0 | cert_filepath); |
994 | 0 | n = SSL_CTX_use_certificate_chain_file(vh->tls.ssl_client_ctx, |
995 | 0 | cert_filepath); |
996 | 0 | if (n < 1) { |
997 | 0 | lwsl_err("problem %d getting cert '%s'\n", n, |
998 | 0 | cert_filepath); |
999 | 0 | lws_tls_err_describe_clear(); |
1000 | 0 | return 1; |
1001 | 0 | } |
1002 | 0 | lwsl_info("Loaded client cert %s\n", cert_filepath); |
1003 | |
|
1004 | 0 | } else if (cert_mem && cert_mem_len) { |
1005 | 0 | lws_filepos_t flen; |
1006 | 0 | uint8_t *p; |
1007 | |
|
1008 | 0 | if (lws_tls_alloc_pem_to_der_file(vh->context, NULL, cert_mem, |
1009 | 0 | cert_mem_len, &p, &flen)) { |
1010 | 0 | lwsl_err("%s: couldn't read cert file\n", __func__); |
1011 | |
|
1012 | 0 | return 1; |
1013 | 0 | } |
1014 | | |
1015 | 0 | n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx, SSL_SIZE_T_CAST(flen), p); |
1016 | |
|
1017 | 0 | if (n < 1) { |
1018 | 0 | lwsl_err("%s: problem interpreting client cert\n", __func__); |
1019 | 0 | lws_tls_err_describe_clear(); |
1020 | 0 | } |
1021 | |
|
1022 | 0 | lws_free_set_NULL(p); |
1023 | |
|
1024 | 0 | if (n != 1) |
1025 | 0 | return 1; |
1026 | |
|
1027 | 0 | } |
1028 | 0 | if (private_key_filepath) { |
1029 | 0 | lwsl_info("%s: using private key filepath\n", __func__); |
1030 | 0 | lws_ssl_bind_passphrase(vh->tls.ssl_client_ctx, 1, info); |
1031 | | /* set the private key from KeyFile */ |
1032 | 0 | if (SSL_CTX_use_PrivateKey_file(vh->tls.ssl_client_ctx, |
1033 | 0 | private_key_filepath, SSL_FILETYPE_PEM) != 1) { |
1034 | 0 | lwsl_err("use_PrivateKey_file '%s'\n", |
1035 | 0 | private_key_filepath); |
1036 | 0 | lws_tls_err_describe_clear(); |
1037 | 0 | return 1; |
1038 | 0 | } |
1039 | 0 | lwsl_info("Loaded client cert private key %s\n", |
1040 | 0 | private_key_filepath); |
1041 | | |
1042 | | /* verify private key */ |
1043 | 0 | if (!SSL_CTX_check_private_key(vh->tls.ssl_client_ctx)) { |
1044 | 0 | lwsl_err("Private SSL key doesn't match cert\n"); |
1045 | 0 | return 1; |
1046 | 0 | } |
1047 | 0 | } |
1048 | 0 | else if (key_mem && key_mem_len) { |
1049 | |
|
1050 | 0 | lws_filepos_t flen; |
1051 | 0 | uint8_t *p; |
1052 | |
|
1053 | 0 | if (lws_tls_alloc_pem_to_der_file(vh->context, NULL, key_mem, |
1054 | 0 | key_mem_len, &p, &flen)) { |
1055 | 0 | lwsl_err("%s: couldn't use mem cert\n", __func__); |
1056 | |
|
1057 | 0 | return 1; |
1058 | 0 | } |
1059 | | |
1060 | 0 | n = SSL_CTX_use_PrivateKey_ASN1(EVP_PKEY_RSA, vh->tls.ssl_client_ctx, p, |
1061 | | #if defined(LWS_WITH_BORINGSSL) || defined(LWS_WITH_AWSLC) |
1062 | | (size_t) |
1063 | | #else |
1064 | 0 | (long)(lws_intptr_t) |
1065 | 0 | #endif |
1066 | 0 | flen); |
1067 | 0 | if (n != 1) |
1068 | 0 | n = SSL_CTX_use_PrivateKey_ASN1(EVP_PKEY_EC, |
1069 | 0 | vh->tls.ssl_client_ctx, p, |
1070 | | #if defined(LWS_WITH_BORINGSSL) || defined(LWS_WITH_AWSLC) |
1071 | | (size_t) |
1072 | | #else |
1073 | 0 | (long)(lws_intptr_t) |
1074 | 0 | #endif |
1075 | 0 | flen); |
1076 | |
|
1077 | 0 | lws_free_set_NULL(p); |
1078 | |
|
1079 | 0 | if (n != 1) { |
1080 | 0 | lwsl_err("%s: unable to use key_mem\n", __func__); |
1081 | |
|
1082 | 0 | return 1; |
1083 | 0 | } |
1084 | 0 | } |
1085 | | |
1086 | | #if defined(LWS_ROLE_QUIC) && !defined(LWS_WITH_MBEDTLS) && !defined(LWS_WITH_WOLFSSL) && !defined(LWS_WITH_SCHANNEL) && !defined(LWS_WITH_GNUTLS) && !defined(LWS_WITH_BEARSSL) |
1087 | | lws_tls_quic_vhost_init(vh->tls.ssl_client_ctx); |
1088 | | #endif |
1089 | | |
1090 | 0 | return 0; |
1091 | 0 | } |
1092 | | |
1093 | | |