1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2021 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 "private-lib-core.h"
26 #include "private-lib-tls-mbedtls.h"
27
28 #if defined(LWS_WITH_TLS_JIT_TRUST)
29
30 /*
31 * We get called for each peer certificate that was provided in turn.
32 *
33 * Our job is just to collect the AKID and SKIDs into ssl->kid_chain, and walk
34 * later at verification result time if it failed.
35 *
36 * None of these should be trusted, even if a misconfigured server sends us
37 * his root CA.
38 */
39
40 static int
lws_mbedtls_client_verify_callback(SSL * ssl,mbedtls_x509_crt * x509)41 lws_mbedtls_client_verify_callback(SSL *ssl, mbedtls_x509_crt *x509)
42 {
43 union lws_tls_cert_info_results ci;
44
45 /* we reached the max we can hold? */
46
47 if (ssl->kid_chain.count == LWS_ARRAY_SIZE(ssl->kid_chain.akid))
48 return 0;
49
50 /* if not, stash the SKID and AKID into the next kid slot */
51
52 if (!lws_tls_mbedtls_cert_info(x509, LWS_TLS_CERT_INFO_SUBJECT_KEY_ID,
53 &ci, 0))
54 lws_tls_kid_copy(&ci,
55 &ssl->kid_chain.skid[ssl->kid_chain.count]);
56
57 if (!lws_tls_mbedtls_cert_info(x509, LWS_TLS_CERT_INFO_AUTHORITY_KEY_ID,
58 &ci, 0))
59 lws_tls_kid_copy(&ci,
60 &ssl->kid_chain.akid[ssl->kid_chain.count]);
61
62 ssl->kid_chain.count++;
63
64 // lwsl_notice("%s: %u\n", __func__, ssl->kid_chain.count);
65
66 return 0;
67 }
68
69 #endif
70
71 int
lws_ssl_client_bio_create(struct lws * wsi)72 lws_ssl_client_bio_create(struct lws *wsi)
73 {
74 char hostname[128], *p;
75 const char *alpn_comma = wsi->a.context->tls.alpn_default;
76 struct alpn_ctx protos;
77 int fl = SSL_VERIFY_PEER;
78
79 if (wsi->stash)
80 lws_strncpy(hostname, wsi->stash->cis[CIS_HOST], sizeof(hostname));
81 else
82 if (lws_hdr_copy(wsi, hostname, sizeof(hostname),
83 _WSI_TOKEN_CLIENT_HOST) <= 0) {
84 lwsl_err("%s: Unable to get hostname\n", __func__);
85
86 return -1;
87 }
88
89 /*
90 * remove any :port part on the hostname... necessary for network
91 * connection but typical certificates do not contain it
92 */
93 p = hostname;
94 while (*p) {
95 if (*p == ':') {
96 *p = '\0';
97 break;
98 }
99 p++;
100 }
101
102 wsi->tls.ssl = SSL_new(wsi->a.vhost->tls.ssl_client_ctx);
103 if (!wsi->tls.ssl) {
104 lwsl_info("%s: SSL_new() failed\n", __func__);
105 return -1;
106 }
107
108 #if defined(LWS_WITH_TLS_SESSIONS)
109 if (!(wsi->a.vhost->options & LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE))
110 lws_tls_reuse_session(wsi);
111 #endif
112
113 if (wsi->a.vhost->tls.ssl_info_event_mask)
114 SSL_set_info_callback(wsi->tls.ssl, lws_ssl_info_callback);
115
116 if (!(wsi->tls.use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)) {
117 X509_VERIFY_PARAM *param = SSL_get0_param(wsi->tls.ssl);
118 /* Enable automatic hostname checks */
119 // X509_VERIFY_PARAM_set_hostflags(param,
120 // X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
121 lwsl_info("%s: setting hostname %s\n", __func__, hostname);
122 if (X509_VERIFY_PARAM_set1_host(param, hostname, 0) != 1)
123 return -1;
124 }
125
126 if (wsi->a.vhost->tls.alpn)
127 alpn_comma = wsi->a.vhost->tls.alpn;
128
129 if (wsi->stash) {
130 lws_strncpy(hostname, wsi->stash->cis[CIS_HOST],
131 sizeof(hostname));
132 if (wsi->stash->cis[CIS_ALPN])
133 alpn_comma = wsi->stash->cis[CIS_ALPN];
134 } else {
135 if (lws_hdr_copy(wsi, hostname, sizeof(hostname),
136 _WSI_TOKEN_CLIENT_ALPN) > 0)
137 alpn_comma = hostname;
138 }
139
140 lwsl_info("%s: %s: client conn sending ALPN list '%s'\n",
141 __func__, lws_wsi_tag(wsi), alpn_comma);
142
143 protos.len = (uint8_t)lws_alpn_comma_to_openssl(alpn_comma, protos.data,
144 sizeof(protos.data) - 1);
145
146 /* with mbedtls, protos is not pointed to after exit from this call */
147 SSL_set_alpn_select_cb(wsi->tls.ssl, &protos);
148
149 if (wsi->flags & LCCSCF_ALLOW_SELFSIGNED) {
150 lwsl_notice("%s: allowing selfsigned\n", __func__);
151 fl = SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
152 }
153
154 if (wsi->flags & LCCSCF_ALLOW_INSECURE)
155 fl = SSL_VERIFY_NONE;
156
157 /*
158 * use server name indication (SNI), if supported,
159 * when establishing connection
160 */
161 #if defined(LWS_WITH_TLS_JIT_TRUST)
162 SSL_set_verify(wsi->tls.ssl, SSL_VERIFY_PEER,
163 lws_mbedtls_client_verify_callback);
164 (void)fl;
165 #else
166 SSL_set_verify(wsi->tls.ssl, fl, NULL);
167 #endif
168
169 SSL_set_fd(wsi->tls.ssl, (int)wsi->desc.sockfd);
170
171 if (wsi->sys_tls_client_cert) {
172 lws_system_blob_t *b = lws_system_get_blob(wsi->a.context,
173 LWS_SYSBLOB_TYPE_CLIENT_CERT_DER,
174 wsi->sys_tls_client_cert - 1);
175 const uint8_t *pem_data = NULL;
176 uint8_t *data = NULL;
177 lws_filepos_t flen;
178 size_t size;
179 int err = 0;
180
181 if (!b)
182 goto no_client_cert;
183
184 /*
185 * Set up the per-connection client cert
186 */
187
188 size = lws_system_blob_get_size(b);
189 if (!size)
190 goto no_client_cert;
191
192 if (lws_system_blob_get_single_ptr(b, &pem_data))
193 goto no_client_cert;
194
195 if (lws_tls_alloc_pem_to_der_file(wsi->a.context, NULL,
196 (const char *)pem_data, size,
197 &data, &flen))
198 goto no_client_cert;
199 size = (size_t) flen;
200
201 err = SSL_use_certificate_ASN1(wsi->tls.ssl, data, (int)size);
202 lws_free_set_NULL(data);
203 if (err != 1)
204 goto no_client_cert;
205
206 b = lws_system_get_blob(wsi->a.context,
207 LWS_SYSBLOB_TYPE_CLIENT_KEY_DER,
208 wsi->sys_tls_client_cert - 1);
209 if (!b)
210 goto no_client_cert;
211 size = lws_system_blob_get_size(b);
212 if (!size)
213 goto no_client_cert;
214
215 if (lws_system_blob_get_single_ptr(b, &pem_data))
216 goto no_client_cert;
217
218 if (lws_tls_alloc_pem_to_der_file(wsi->a.context, NULL,
219 (const char *)pem_data, size,
220 &data, &flen))
221 goto no_client_cert;
222 size = (size_t) flen;
223
224 err = SSL_use_PrivateKey_ASN1(0, wsi->tls.ssl, data, (int)size);
225 lws_free_set_NULL(data);
226 if (err != 1)
227 goto no_client_cert;
228
229 /* no wrapper api for check key */
230
231 lwsl_notice("%s: set system client cert %u\n", __func__,
232 wsi->sys_tls_client_cert - 1);
233 }
234
235 return 0;
236
237 no_client_cert:
238 lwsl_err("%s: unable to set up system client cert %d\n", __func__,
239 wsi->sys_tls_client_cert - 1);
240
241 return 1;
242 }
243
ERR_get_error(void)244 int ERR_get_error(void)
245 {
246 return 0;
247 }
248
249 enum lws_ssl_capable_status
lws_tls_client_connect(struct lws * wsi,char * errbuf,size_t elen)250 lws_tls_client_connect(struct lws *wsi, char *errbuf, size_t elen)
251 {
252 int m, n = SSL_connect(wsi->tls.ssl), en;
253
254 if (n == 1) {
255 lws_tls_server_conn_alpn(wsi);
256 #if defined(LWS_WITH_TLS_SESSIONS)
257 lws_tls_session_new_mbedtls(wsi);
258 #endif
259 lwsl_info("%s: client connect OK\n", __func__);
260 return LWS_SSL_CAPABLE_DONE;
261 }
262
263 en = (int)LWS_ERRNO;
264 m = SSL_get_error(wsi->tls.ssl, n);
265
266 if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl))
267 return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
268
269 if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl))
270 return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
271
272 if (!n) /* we don't know what he wants, but he says to retry */
273 return LWS_SSL_CAPABLE_MORE_SERVICE;
274
275 if (m == SSL_ERROR_SYSCALL && !en)
276 return LWS_SSL_CAPABLE_MORE_SERVICE;
277
278 lws_snprintf(errbuf, elen, "mbedtls connect %d %d %d", n, m, en);
279
280 return LWS_SSL_CAPABLE_ERROR;
281 }
282
283 int
lws_tls_client_confirm_peer_cert(struct lws * wsi,char * ebuf,size_t ebuf_len)284 lws_tls_client_confirm_peer_cert(struct lws *wsi, char *ebuf, size_t ebuf_len)
285 {
286 int n;
287 unsigned int avoid = 0;
288 X509 *peer = SSL_get_peer_certificate(wsi->tls.ssl);
289 struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
290 const char *type = "";
291 char *sb = (char *)&pt->serv_buf[0];
292
293 if (!peer) {
294 #if defined(LWS_WITH_SYS_METRICS)
295 lws_metrics_hist_bump_describe_wsi(wsi, lws_metrics_priv_to_pub(
296 wsi->a.context->mth_conn_failures),
297 "tls=\"nocert\"");
298 #endif
299 lwsl_info("peer did not provide cert\n");
300 lws_snprintf(ebuf, ebuf_len, "no peer cert");
301
302 return -1;
303 }
304
305 n = (int)SSL_get_verify_result(wsi->tls.ssl);
306 lwsl_debug("get_verify says %d\n", n);
307
308 switch (n) {
309 case X509_V_OK:
310 return 0;
311
312 case X509_V_ERR_HOSTNAME_MISMATCH:
313 type = "hostname";
314 avoid = LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
315 break;
316
317 case X509_V_ERR_INVALID_CA:
318 type = "invalidca";
319 avoid = LCCSCF_ALLOW_SELFSIGNED;
320 break;
321
322 case X509_V_ERR_CERT_NOT_YET_VALID:
323 type = "notyetvalid";
324 avoid = LCCSCF_ALLOW_EXPIRED;
325 break;
326
327 case X509_V_ERR_CERT_HAS_EXPIRED:
328 type = "expired";
329 avoid = LCCSCF_ALLOW_EXPIRED;
330 break;
331 }
332
333 lwsl_info("%s: cert problem: %s\n", __func__, type);
334 #if defined(LWS_WITH_SYS_METRICS)
335 {
336 char buckname[64];
337 lws_snprintf(buckname, sizeof(buckname), "tls=\"%s\"", type);
338 lws_metrics_hist_bump_describe_wsi(wsi,
339 lws_metrics_priv_to_pub(wsi->a.context->mth_conn_failures),
340 buckname);
341 }
342 #endif
343 if (wsi->tls.use_ssl & avoid) {
344 lwsl_info("%s: allowing anyway\n", __func__);
345
346 return 0;
347 }
348
349 #if defined(LWS_WITH_TLS_JIT_TRUST)
350 if (n == X509_V_ERR_INVALID_CA)
351 lws_tls_jit_trust_sort_kids(wsi, &wsi->tls.ssl->kid_chain);
352 #endif
353 lws_snprintf(ebuf, ebuf_len,
354 "server's cert didn't look good, %s (use_ssl 0x%x) X509_V_ERR = %d: %s\n",
355 type, (unsigned int)wsi->tls.use_ssl, n,
356 ERR_error_string((unsigned long)n, sb));
357
358 lwsl_info("%s\n", ebuf);
359
360 lws_tls_err_describe_clear();
361
362 return -1;
363 }
364
365 int
lws_tls_client_create_vhost_context(struct lws_vhost * vh,const struct lws_context_creation_info * info,const char * cipher_list,const char * ca_filepath,const void * ca_mem,unsigned int ca_mem_len,const char * cert_filepath,const void * cert_mem,unsigned int cert_mem_len,const char * private_key_filepath,const void * key_mem,unsigned int key_mem_len)366 lws_tls_client_create_vhost_context(struct lws_vhost *vh,
367 const struct lws_context_creation_info *info,
368 const char *cipher_list,
369 const char *ca_filepath,
370 const void *ca_mem,
371 unsigned int ca_mem_len,
372 const char *cert_filepath,
373 const void *cert_mem,
374 unsigned int cert_mem_len,
375 const char *private_key_filepath,
376 const void *key_mem,
377 unsigned int key_mem_len
378 )
379 {
380 X509 *d2i_X509(X509 **cert, const unsigned char *buffer, long len);
381 SSL_METHOD *method = (SSL_METHOD *)TLS_client_method();
382 unsigned long error;
383 int n;
384
385 #if defined(LWS_WITH_TLS_SESSIONS)
386 vh->tls_session_cache_max = info->tls_session_cache_max ?
387 info->tls_session_cache_max : 10;
388 lws_tls_session_cache(vh, info->tls_session_timeout);
389 #endif
390
391 if (!method) {
392 error = (unsigned long)ERR_get_error();
393 lwsl_err("problem creating ssl method %lu: %s\n",
394 error, ERR_error_string(error,
395 (char *)vh->context->pt[0].serv_buf));
396 return 1;
397 }
398 /* create context */
399 vh->tls.ssl_client_ctx = SSL_CTX_new(method, &vh->context->mcdc);
400 if (!vh->tls.ssl_client_ctx) {
401 error = (unsigned long)ERR_get_error();
402 lwsl_err("problem creating ssl context %lu: %s\n",
403 error, ERR_error_string(error,
404 (char *)vh->context->pt[0].serv_buf));
405 return 1;
406 }
407
408 if (!ca_filepath && (!ca_mem || !ca_mem_len))
409 return 0;
410
411 if (ca_filepath) {
412 #if !defined(LWS_PLAT_OPTEE)
413 uint8_t *buf;
414 lws_filepos_t len;
415
416 if (alloc_file(vh->context, ca_filepath, &buf, &len)) {
417 lwsl_err("Load CA cert file %s failed\n", ca_filepath);
418 return 1;
419 }
420 vh->tls.x509_client_CA = d2i_X509(NULL, buf, (long)len);
421 free(buf);
422
423 lwsl_info("Loading vh %s client CA for verification %s\n", vh->name, ca_filepath);
424 #endif
425 } else {
426 vh->tls.x509_client_CA = d2i_X509(NULL, (uint8_t*)ca_mem, (long)ca_mem_len);
427 lwsl_info("%s: using mem client CA cert %d\n",
428 __func__, ca_mem_len);
429 }
430
431 if (!vh->tls.x509_client_CA) {
432 lwsl_err("client CA: x509 parse failed\n");
433 return 1;
434 }
435
436 if (!vh->tls.ssl_ctx)
437 SSL_CTX_add_client_CA(vh->tls.ssl_client_ctx, vh->tls.x509_client_CA);
438 else
439 SSL_CTX_add_client_CA(vh->tls.ssl_ctx, vh->tls.x509_client_CA);
440
441 /* support for client-side certificate authentication */
442 if (cert_filepath) {
443 #if !defined(LWS_PLAT_OPTEE)
444 uint8_t *buf;
445 lws_filepos_t amount;
446
447 if (lws_tls_use_any_upgrade_check_extant(cert_filepath) !=
448 LWS_TLS_EXTANT_YES &&
449 (info->options & LWS_SERVER_OPTION_IGNORE_MISSING_CERT))
450 return 0;
451
452 lwsl_notice("%s: doing cert filepath %s\n", __func__,
453 cert_filepath);
454
455 if (alloc_file(vh->context, cert_filepath, &buf, &amount))
456 return 1;
457
458 buf[amount++] = '\0';
459
460 n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx,
461 (int)amount, buf);
462 lws_free(buf);
463 if (n < 1) {
464 lwsl_err("problem %d getting cert '%s'\n", n,
465 cert_filepath);
466 lws_tls_err_describe_clear();
467 return 1;
468 }
469
470 lwsl_info("Loaded client cert %s\n", cert_filepath);
471 #endif
472 } else if (cert_mem && cert_mem_len) {
473 /* lwsl_hexdump_notice(cert_mem, cert_mem_len - 1); */
474 n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx,
475 (int)cert_mem_len, cert_mem);
476 if (n < 1) {
477 lwsl_err("%s: (mbedtls) problem interpreting client cert\n",
478 __func__);
479 lws_tls_err_describe_clear();
480 return 1;
481 }
482 lwsl_info("%s: using mem client cert %d\n",
483 __func__, cert_mem_len);
484 }
485
486 if (private_key_filepath) {
487 #if !defined(LWS_PLAT_OPTEE)
488
489 uint8_t *buf;
490 lws_filepos_t amount;
491
492 lwsl_notice("%s: doing private key filepath %s\n", __func__,
493 private_key_filepath);
494 if (alloc_file(vh->context, private_key_filepath, &buf, &amount))
495 return 1;
496
497 buf[amount++] = '\0';
498
499 n = SSL_CTX_use_PrivateKey_ASN1(0, vh->tls.ssl_client_ctx,
500 buf, (long)amount);
501
502 lws_free(buf);
503 if (n < 1) {
504 lwsl_err("problem %d getting private key '%s'\n", n,
505 private_key_filepath);
506 lws_tls_err_describe_clear();
507 return 1;
508 }
509
510 lwsl_notice("Loaded private key %s\n", private_key_filepath);
511 #endif
512 } else if (key_mem && key_mem_len) {
513 /* lwsl_hexdump_notice(cert_mem, cert_mem_len - 1); */
514 n = SSL_CTX_use_PrivateKey_ASN1(0, vh->tls.ssl_client_ctx,
515 key_mem, (long)key_mem_len - 1);
516
517 if (n < 1) {
518 lwsl_err("%s: (mbedtls) problem interpreting private key\n",
519 __func__);
520 lws_tls_err_describe_clear();
521 return 1;
522 }
523 lwsl_info("%s: using mem private key %d\n",
524 __func__, key_mem_len);
525
526 }
527 return 0;
528 }
529
530 int
lws_tls_client_vhost_extra_cert_mem(struct lws_vhost * vh,const uint8_t * der,size_t der_len)531 lws_tls_client_vhost_extra_cert_mem(struct lws_vhost *vh,
532 const uint8_t *der, size_t der_len)
533 {
534 if (SSL_CTX_add_client_CA_ASN1(vh->tls.ssl_client_ctx, (int)der_len, der) != 1) {
535 lwsl_err("%s: failed\n", __func__);
536 return 1;
537 }
538
539 return 0;
540 }
541
542