-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssl_common.c
738 lines (644 loc) · 21.4 KB
/
ssl_common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/ssl_common.c,v $
*
* Purpose : File with TLS/SSL extension. Contains methods for
* creating, using and closing TLS/SSL connections that do
* not depend on particular TLS/SSL library.
*
* Copyright : Written by and Copyright (c) 2017 Vaclav Svec. FIT CVUT.
* Copyright (C) 2018-2021 by Fabian Keil <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*********************************************************************/
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "config.h"
#include "project.h"
#include "miscutil.h"
#include "errlog.h"
#include "ssl.h"
#include "ssl_common.h"
/*
* Macros for ssl_common.c
*/
#define CERT_SERIAL_NUM_LENGTH 4 /* Bytes of hash to be used for creating serial number of certificate. Min=2 and max=16 */
/*********************************************************************
*
* Function : client_use_ssl
*
* Description : Tests if client in current client state structure
* should use SSL connection or standard connection.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : If client should use TLS/SSL connection, 1 is returned.
* Otherwise 0 is returned.
*
*********************************************************************/
extern int client_use_ssl(const struct client_state *csp)
{
return csp->http->client_ssl;
}
/*********************************************************************
*
* Function : server_use_ssl
*
* Description : Tests if server in current client state structure
* should use SSL connection or standard connection.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : If server should use TLS/SSL connection, 1 is returned.
* Otherwise 0 is returned.
*
*********************************************************************/
extern int server_use_ssl(const struct client_state *csp)
{
return csp->http->server_ssl;
}
/*********************************************************************
*
* Function : ssl_send_data_delayed
*
* Description : Sends the contents of buf (for n bytes) to given SSL
* connection, optionally delaying the operation.
*
* Parameters :
* 1 : ssl_attr = SSL context to send data to
* 2 : buf = Pointer to data to be sent
* 3 : len = Length of data to be sent to the SSL context
* 4 : delay = Delay in milliseconds.
*
* Returns : 0 on success (entire buffer sent).
* nonzero on error.
*
*********************************************************************/
extern int ssl_send_data_delayed(struct ssl_attr* ssl_attr,
const unsigned char *buf, size_t len,
unsigned int delay)
{
size_t i = 0;
if (delay == 0)
{
if (ssl_send_data(ssl_attr, buf, len) < 0)
{
return -1;
}
else
{
return 0;
}
}
while (i < len)
{
size_t write_length;
enum { MAX_WRITE_LENGTH = 10 };
if ((i + MAX_WRITE_LENGTH) > len)
{
write_length = len - i;
}
else
{
write_length = MAX_WRITE_LENGTH;
}
privoxy_millisleep(delay);
if (ssl_send_data(ssl_attr, buf + i, write_length) < 0)
{
return -1;
}
i += write_length;
}
return 0;
}
/*********************************************************************
*
* Function : ssl_flush_socket
*
* Description : Send any pending "buffered" content with given
* SSL connection. Alternative to function flush_socket.
*
* Parameters :
* 1 : ssl_attr = SSL context to send buffer to
* 2 : iob = The I/O buffer to flush, usually csp->iob.
*
* Returns : On success, the number of bytes send are returned (zero
* indicates nothing was sent). On error, -1 is returned.
*
*********************************************************************/
extern long ssl_flush_socket(struct ssl_attr *ssl_attr, struct iob *iob)
{
/* Computing length of buffer part to send */
long len = iob->eod - iob->cur;
if (len <= 0)
{
return(0);
}
/* Sending data to given SSl context */
if (ssl_send_data(ssl_attr, (const unsigned char *)iob->cur, (size_t)len) < 0)
{
return -1;
}
iob->eod = iob->cur = iob->buf;
return(len);
}
/*********************************************************************
*
* Function : close_client_and_server_ssl_connections
*
* Description : Checks if client or server should use secured
* connection over SSL and if so, closes all of them.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : N/A
*
*********************************************************************/
extern void close_client_and_server_ssl_connections(struct client_state *csp)
{
if (client_use_ssl(csp) == 1)
{
close_client_ssl_connection(csp);
}
if (server_use_ssl(csp) == 1)
{
close_server_ssl_connection(csp);
}
}
/*********************************************************************
*
* Function : tunnel_established_successfully
*
* Description : Check if parent proxy server response contains
* information about successfully created connection with
* destination server. (HTTP/... 2xx ...)
*
* Parameters :
* 1 : server_response = Buffer with parent proxy server response
* 2 : response_len = Length of server_response
*
* Returns : 1 => Connection created successfully
* 0 => Connection wasn't created successfully
*
*********************************************************************/
extern int tunnel_established_successfully(const char *server_response,
unsigned int response_len)
{
unsigned int pos = 0;
if (server_response == NULL)
{
return 0;
}
/* Tests if "HTTP/" string is at the begin of received response */
if (strncmp(server_response, "HTTP/", 5) != 0)
{
return 0;
}
for (pos = 0; pos < response_len; pos++)
{
if (server_response[pos] == ' ')
{
break;
}
}
/*
* response_len -3 because of buffer end, response structure and 200 code.
* There must be at least 3 chars after space.
* End of buffer: ... 2xx'\0'
* pos = |
*/
if (pos >= (response_len - 3))
{
return 0;
}
/* Test HTTP status code */
if (server_response[pos + 1] != '2')
{
return 0;
}
return 1;
}
/*********************************************************************
*
* Function : free_certificate_chain
*
* Description : Frees certificates linked list. This linked list is
* used to save information about certificates in
* trusted chain.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : N/A
*
*********************************************************************/
extern void free_certificate_chain(struct client_state *csp)
{
struct certs_chain *cert = csp->server_certs_chain.next;
/* Cleaning buffers */
memset(csp->server_certs_chain.info_buf, 0,
sizeof(csp->server_certs_chain.info_buf));
freez(csp->server_certs_chain.file_buf);
csp->server_certs_chain.next = NULL;
/* Freeing memory in whole linked list */
while (cert != NULL)
{
struct certs_chain *cert_for_free = cert;
cert = cert->next;
/* Cleaning buffers */
memset(cert_for_free->info_buf, 0, sizeof(cert_for_free->info_buf));
freez(cert_for_free->file_buf);
freez(cert_for_free);
}
}
/*********************************************************************
*
* Function : ssl_send_certificate_error
*
* Description : Sends info about invalid server certificate to client.
* Sent message is including all trusted chain certificates,
* that can be downloaded in web browser.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : N/A
*
*********************************************************************/
extern void ssl_send_certificate_error(struct client_state *csp)
{
struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
size_t message_len = 0;
int ret = 0;
struct certs_chain *cert = NULL;
const size_t head_length = 63;
/* Header of message with certificate information */
const char message_begin[] =
"HTTP/1.1 403 Certificate validation failed\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
"<!DOCTYPE html>\n"
"<html><head><title>Server certificate verification failed</title></head>\n"
"<body><h1>Server certificate verification failed</h1>\n"
"<p><a href=\"https://" CGI_SITE_2_HOST "/\">Privoxy</a> was unable "
"to securely connect to the destination server.</p>"
"<p>Reason: ";
const char message_end[] = "</body></html>\n";
char reason[INVALID_CERT_INFO_BUF_SIZE];
memset(reason, 0, sizeof(reason));
/* Get verification message from verification return code */
ssl_crt_verify_info(reason, sizeof(reason), csp);
/*
* Computing total length of message with all certificates inside
*/
message_len = strlen(message_begin) + strlen(message_end)
+ strlen(reason) + strlen("</p>") + 1;
cert = &(csp->server_certs_chain);
while (cert->next != NULL)
{
size_t base64_len;
if (cert->file_buf != NULL)
{
base64_len = 4 * ((strlen(cert->file_buf) + 2) / 3) + 1;
message_len += strlen(cert->info_buf) + strlen("<pre></pre>\n")
+ base64_len + strlen("<a href=\"data:application"
"/x-x509-ca-cert;base64,\">Download certificate</a>");
}
else
{
log_error(LOG_LEVEL_ERROR,
"Incomplete certificate information for %s.",
csp->http->hostport);
}
cert = cert->next;
}
/*
* Joining all blocks in one long message
*/
char message[message_len];
memset(message, 0, message_len);
strlcpy(message, message_begin, message_len);
strlcat(message, reason , message_len);
strlcat(message, "</p>" , message_len);
cert = &(csp->server_certs_chain);
while (cert->next != NULL)
{
if (cert->file_buf != NULL)
{
/* +1 for terminating null */
size_t base64_len = base64_len = 4 * ((strlen(cert->file_buf) + 2) / 3) + 1;
size_t olen = 0;
char base64_buf[base64_len];
memset(base64_buf, 0, base64_len);
/* Encoding certificate into base64 code */
ret = ssl_base64_encode((unsigned char*)base64_buf,
base64_len, &olen, (const unsigned char*)cert->file_buf,
strlen(cert->file_buf));
if (ret != 0)
{
log_error(LOG_LEVEL_ERROR,
"Encoding to base64 failed, buffer is to small");
}
strlcat(message, "<pre>", message_len);
strlcat(message, cert->info_buf, message_len);
strlcat(message, "</pre>\n", message_len);
if (ret == 0)
{
strlcat(message, "<a href=\"data:application/x-x509-ca-cert;base64,",
message_len);
strlcat(message, base64_buf, message_len);
strlcat(message, "\">Download certificate</a>", message_len);
}
}
cert = cert->next;
}
strlcat(message, message_end, message_len);
if (0 == strcmpic(csp->http->gpc, "HEAD"))
{
/* Cut off body */
char *header_end = strstr(message, "\r\n\r\n");
if (header_end != NULL)
{
header_end[3] = '\0';
}
}
/*
* Sending final message to client
*/
(void)ssl_send_data(ssl_attr, (const unsigned char *)message, strlen(message));
free_certificate_chain(csp);
log_error(LOG_LEVEL_CRUNCH, "Certificate error: %s: https://%s%s",
reason, csp->http->hostport, csp->http->path);
log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" 403 %lu",
csp->ip_addr_str, csp->http->gpc, csp->http->hostport, csp->http->path,
csp->http->version, message_len-head_length);
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
#endif
}
/*********************************************************************
*
* Function : file_exists
*
* Description : Tests if file exists and is readable.
*
* Parameters :
* 1 : path = Path to tested file.
*
* Returns : 1 => File exists and is readable.
* 0 => File doesn't exist or is not readable.
*
*********************************************************************/
extern int file_exists(const char *path)
{
FILE *f;
if ((f = fopen(path, "r")) != NULL)
{
fclose(f);
return 1;
}
return 0;
}
/*********************************************************************
*
* Function : make_certs_path
*
* Description : Creates path to file from three pieces. This function
* takes parameters and puts them in one new mallocated
* char * in correct order. Returned variable must be freed
* by caller. This function is mainly used for creating
* paths of certificates and keys files.
*
* Parameters :
* 1 : conf_dir = Name/path of directory where is the file.
* '.' can be used for current directory.
* 2 : file_name = Name of file in conf_dir without suffix.
* 3 : suffix = Suffix of given file_name.
*
* Returns : path => Path was built up successfully
* NULL => Path can't be built up
*
*********************************************************************/
extern char *make_certs_path(const char *conf_dir, const char *file_name,
const char *suffix)
{
/* Test if all given parameters are valid */
if (conf_dir == NULL || *conf_dir == '\0' || file_name == NULL ||
*file_name == '\0' || suffix == NULL || *suffix == '\0')
{
log_error(LOG_LEVEL_ERROR,
"make_certs_path failed: bad input parameters");
return NULL;
}
char *path = NULL;
size_t path_size = strlen(conf_dir)
+ strlen(file_name) + strlen(suffix) + 2;
/* Setting delimiter and editing path length */
#if defined(_WIN32)
char delim[] = "\\";
path_size += 1;
#else /* ifndef _WIN32 */
char delim[] = "/";
#endif /* ifndef _WIN32 */
/*
* Building up path from many parts
*/
#if defined(unix)
if (*conf_dir != '/' && basedir && *basedir)
{
/*
* Replacing conf_dir with basedir. This new variable contains
* absolute path to cwd.
*/
path_size += strlen(basedir) + 2;
path = zalloc_or_die(path_size);
strlcpy(path, basedir, path_size);
strlcat(path, delim, path_size);
strlcat(path, conf_dir, path_size);
strlcat(path, delim, path_size);
strlcat(path, file_name, path_size);
strlcat(path, suffix, path_size);
}
else
#endif /* defined unix */
{
path = zalloc_or_die(path_size);
strlcpy(path, conf_dir, path_size);
strlcat(path, delim, path_size);
strlcat(path, file_name, path_size);
strlcat(path, suffix, path_size);
}
return path;
}
/*********************************************************************
*
* Function : get_certificate_serial
*
* Description : Computes serial number for new certificate from host
* name hash. This hash must be already saved in csp
* structure.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : Serial number for new certificate
*
*********************************************************************/
extern unsigned long get_certificate_serial(struct client_state *csp)
{
unsigned long exp = 1;
unsigned long serial = 0;
int i = CERT_SERIAL_NUM_LENGTH;
for (; i >= 0; i--)
{
serial += exp * (unsigned)csp->http->hash_of_host[i];
exp *= 256;
}
return serial;
}
/*********************************************************************
*
* Function : generate_certificate_valid_date
*
* Description : Turns a time_t into the format expected by mbedTLS.
*
* Parameters :
* 1 : time_spec = The timestamp to convert
* 2 : buffer = The buffer to write the date to
* 3 : buffer_size = The size of the buffer
* 4 : fmt = format
*
* Returns : 0 => The conversion worked
* 1 => The conversion failed
*
*********************************************************************/
static int generate_certificate_valid_date(time_t time_spec, char *buffer,
size_t buffer_size, const char *fmt)
{
struct tm valid_date;
struct tm *timeptr;
size_t ret;
timeptr = privoxy_gmtime_r(&time_spec, &valid_date);
if (NULL == timeptr)
{
return 1;
}
ret = strftime(buffer, buffer_size, fmt, timeptr);
if (ret <= 0)
{
return 1;
}
return 0;
}
/*********************************************************************
*
* Function : get_certificate_valid_from_date
*
* Description : Generates a "valid from" date in the format
* expected by mbedTLS.
*
* Parameters :
* 1 : buffer = The buffer to write the date to
* 2 : buffer_size = The size of the buffer
* 3 : fmt = format
*
* Returns : 0 => The generation worked
* 1 => The generation failed
*
*********************************************************************/
extern int get_certificate_valid_from_date(char *buffer, size_t buffer_size, const char *fmt)
{
time_t time_spec;
time_spec = time(NULL);
/* 1 month in the past */
time_spec -= 30 * 24 * 60 * 60;
return generate_certificate_valid_date(time_spec, buffer, buffer_size, fmt);
}
/*********************************************************************
*
* Function : get_certificate_valid_to_date
*
* Description : Generates a "valid to" date in the format
* expected by mbedTLS.
*
* Parameters :
* 1 : buffer = The buffer to write the date to
* 2 : buffer_size = The size of the buffer
* 3 : fmt = format
*
* Returns : 0 => The generation worked
* 1 => The generation failed
*
*********************************************************************/
extern int get_certificate_valid_to_date(char *buffer, size_t buffer_size, const char *fmt)
{
time_t time_spec;
time_spec = time(NULL);
/* Three months in the future */
time_spec += 90 * 24 * 60 * 60;
return generate_certificate_valid_date(time_spec, buffer, buffer_size, fmt);
}
/*********************************************************************
*
* Function : enforce_sane_certificate_state
*
* Description : Makes sure the certificate state is sane.
*
* Parameters :
* 1 : certificate = Path to the potentionally existing certifcate.
* 2 : key = Path to the potentionally existing key.
*
* Returns : -1 => Error
* 0 => Certificate state is sane
*
*********************************************************************/
extern int enforce_sane_certificate_state(const char *certificate, const char *key)
{
const int certificate_exists = file_exists(certificate);
const int key_exists = file_exists(key);
if (!certificate_exists && key_exists)
{
log_error(LOG_LEVEL_ERROR,
"A website key already exists but there's no matching certificate. "
"Removing %s before creating a new key and certificate.", key);
if (unlink(key))
{
log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E", key);
return -1;
}
}
if (certificate_exists && !key_exists)
{
log_error(LOG_LEVEL_ERROR,
"A certificate exists but there's no matching key. "
"Removing %s before creating a new key and certificate.", certificate);
if (unlink(certificate))
{
log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E", certificate);
return -1;
}
}
return 0;
}