-
Notifications
You must be signed in to change notification settings - Fork 1
/
openssl.c
2284 lines (2058 loc) · 60.1 KB
/
openssl.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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/openssl.c,v $
*
* Purpose : File with TLS/SSL extension. Contains methods for
* creating, using and closing TLS/SSL connections
* using OpenSSL (or LibreSSL).
*
* Copyright : Written by and Copyright (c) 2020 Maxim Antonov <[email protected]>
* Copyright (C) 2017 Vaclav Svec. FIT CVUT.
* Copyright (C) 2018-2022 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 <unistd.h>
#include <openssl/bn.h>
#include <openssl/opensslv.h>
#include <openssl/pem.h>
#include <openssl/md5.h>
#include <openssl/x509v3.h>
#include "config.h"
#include "project.h"
#include "miscutil.h"
#include "errlog.h"
#include "encode.h"
#include "jcc.h"
#include "ssl.h"
#include "ssl_common.h"
/*
* Macros for openssl.c
*/
#define CERTIFICATE_BASIC_CONSTRAINTS "CA:FALSE"
#define CERTIFICATE_SUBJECT_KEY "hash"
#define CERTIFICATE_AUTHORITY_KEY "keyid:always"
#define CERTIFICATE_ALT_NAME_PREFIX "DNS:"
#define CERTIFICATE_VERSION 2
#define VALID_DATETIME_FMT "%y%m%d%H%M%SZ"
#define VALID_DATETIME_BUFLEN 16
static int generate_host_certificate(struct client_state *csp);
static void free_client_ssl_structures(struct client_state *csp);
static void free_server_ssl_structures(struct client_state *csp);
static int ssl_store_cert(struct client_state *csp, X509 *crt);
static void log_ssl_errors(int debuglevel, const char* fmt, ...) __attribute__((format(printf, 2, 3)));
static int ssl_inited = 0;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define X509_set1_notBefore X509_set_notBefore
#define X509_set1_notAfter X509_set_notAfter
#define X509_get0_serialNumber X509_get_serialNumber
#define X509_get0_notBefore X509_get_notBefore
#define X509_get0_notAfter X509_get_notAfter
#endif
/*********************************************************************
*
* Function : openssl_init
*
* Description : Initializes OpenSSL library once
*
* Parameters : N/A
*
* Returns : N/A
*
*********************************************************************/
static void openssl_init(void)
{
if (ssl_inited == 0)
{
privoxy_mutex_lock(&ssl_init_mutex);
if (ssl_inited == 0)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_library_init();
#else
OPENSSL_init_ssl(0, NULL);
#endif
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
ssl_inited = 1;
}
privoxy_mutex_unlock(&ssl_init_mutex);
}
}
/*********************************************************************
*
* Function : is_ssl_pending
*
* Description : Tests if there are some waiting data on ssl connection.
* Only considers data that has actually been received
* locally and ignores data that is still on the fly
* or has not yet been sent by the remote end.
*
* Parameters :
* 1 : ssl_attr = SSL context to test
*
* Returns : 0 => No data are pending
* >0 => Pending data length
*
*********************************************************************/
extern size_t is_ssl_pending(struct ssl_attr *ssl_attr)
{
BIO *bio = ssl_attr->openssl_attr.bio;
if (bio == NULL)
{
return 0;
}
return (size_t)BIO_pending(bio);
}
/*********************************************************************
*
* Function : ssl_send_data
*
* Description : Sends the content of buf (for n bytes) to given SSL
* connection context.
*
* 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
*
* Returns : Length of sent data or negative value on error.
*
*********************************************************************/
extern int ssl_send_data(struct ssl_attr *ssl_attr, const unsigned char *buf, size_t len)
{
BIO *bio = ssl_attr->openssl_attr.bio;
SSL *ssl;
int ret = 0;
int pos = 0; /* Position of unsent part in buffer */
int fd = -1;
if (len == 0)
{
return 0;
}
if (BIO_get_ssl(bio, &ssl) == 1)
{
fd = SSL_get_fd(ssl);
}
while (pos < len)
{
int send_len = (int)len - pos;
log_error(LOG_LEVEL_WRITING, "TLS on socket %d: %N",
fd, send_len, buf+pos);
/*
* Sending one part of the buffer
*/
while ((ret = BIO_write(bio,
(const unsigned char *)(buf + pos),
send_len)) <= 0)
{
if (!BIO_should_retry(bio))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"Sending data on socket %d over TLS/SSL failed", fd);
return -1;
}
}
/* Adding count of sent bytes to position in buffer */
pos = pos + ret;
}
return (int)len;
}
/*********************************************************************
*
* Function : ssl_recv_data
*
* Description : Receives data from given SSL context and puts
* it into buffer.
*
* Parameters :
* 1 : ssl_attr = SSL context to receive data from
* 2 : buf = Pointer to buffer where data will be written
* 3 : max_length = Maximum number of bytes to read
*
* Returns : Number of bytes read, 0 for EOF, or -1
* on error.
*
*********************************************************************/
extern int ssl_recv_data(struct ssl_attr *ssl_attr, unsigned char *buf, size_t max_length)
{
BIO *bio = ssl_attr->openssl_attr.bio;
SSL *ssl;
int ret = 0;
int fd = -1;
memset(buf, 0, max_length);
/*
* Receiving data from SSL context into buffer
*/
do
{
ret = BIO_read(bio, buf, (int)max_length);
} while (ret <= 0 && BIO_should_retry(bio));
if (BIO_get_ssl(bio, &ssl) == 1)
{
fd = SSL_get_fd(ssl);
}
if (ret < 0)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"Receiving data on socket %d over TLS/SSL failed", fd);
return -1;
}
log_error(LOG_LEVEL_RECEIVED, "TLS from socket %d: %N",
fd, ret, buf);
return ret;
}
/*********************************************************************
*
* Function : ssl_store_cert
*
* Description : This function is called once for each certificate in the
* server's certificate trusted chain and prepares
* information about the certificate. The information can
* be used to inform the user about invalid certificates.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : crt = certificate from trusted chain
*
* Returns : 0 on success and negative value on error
*
*********************************************************************/
static int ssl_store_cert(struct client_state *csp, X509 *crt)
{
long len;
struct certs_chain *last = &(csp->server_certs_chain);
int ret = 0;
BIO *bio = BIO_new(BIO_s_mem());
EVP_PKEY *pkey = NULL;
char *bio_mem_data = NULL;
char *encoded_text;
long l;
const ASN1_INTEGER *bs;
#if OPENSSL_VERSION_NUMBER > 0x10100000L
const X509_ALGOR *tsig_alg;
#endif
int loc;
if (!bio)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new() failed");
return -1;
}
/*
* Searching for last item in certificates linked list
*/
while (last->next != NULL)
{
last = last->next;
}
/*
* Preparing next item in linked list for next certificate
*/
last->next = malloc_or_die(sizeof(struct certs_chain));
last->next->next = NULL;
memset(last->next->info_buf, 0, sizeof(last->next->info_buf));
last->next->file_buf = NULL;
/*
* Saving certificate file into buffer
*/
if (!PEM_write_bio_X509(bio, crt))
{
log_ssl_errors(LOG_LEVEL_ERROR, "PEM_write_bio_X509() failed");
ret = -1;
goto exit;
}
len = BIO_get_mem_data(bio, &bio_mem_data);
last->file_buf = malloc((size_t)len + 1);
if (last->file_buf == NULL)
{
log_error(LOG_LEVEL_ERROR,
"Failed to allocate %lu bytes to store the X509 PEM certificate",
len + 1);
ret = -1;
goto exit;
}
strncpy(last->file_buf, bio_mem_data, (size_t)len);
last->file_buf[len] = '\0';
BIO_free(bio);
bio = BIO_new(BIO_s_mem());
if (!bio)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new() failed");
ret = -1;
goto exit;
}
/*
* Saving certificate information into buffer
*/
l = X509_get_version(crt);
if (l >= 0 && l <= 2)
{
if (BIO_printf(bio, "cert. version : %ld\n", l + 1) <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
ret = -1;
goto exit;
}
}
else
{
if (BIO_printf(bio, "cert. version : Unknown (%ld)\n", l) <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
ret = -1;
goto exit;
}
}
if (BIO_puts(bio, "serial number : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
ret = -1;
goto exit;
}
bs = X509_get0_serialNumber(crt);
if (bs->length <= (int)sizeof(long))
{
ERR_set_mark();
l = ASN1_INTEGER_get(bs);
ERR_pop_to_mark();
}
else
{
l = -1;
}
if (l != -1)
{
unsigned long ul;
const char *neg;
if (bs->type == V_ASN1_NEG_INTEGER)
{
ul = 0 - (unsigned long)l;
neg = "-";
}
else
{
ul = (unsigned long)l;
neg = "";
}
if (BIO_printf(bio, "%s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
ret = -1;
goto exit;
}
}
else
{
int i;
if (bs->type == V_ASN1_NEG_INTEGER)
{
if (BIO_puts(bio, " (Negative)") < 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
ret = -1;
goto exit;
}
}
for (i = 0; i < bs->length; i++)
{
if (BIO_printf(bio, "%02x%c", bs->data[i],
((i + 1 == bs->length) ? '\n' : ':')) <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
ret = -1;
goto exit;
}
}
}
if (BIO_puts(bio, "issuer name : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issuer failed");
ret = -1;
goto exit;
}
if (X509_NAME_print_ex(bio, X509_get_issuer_name(crt), 0, 0) < 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for issuer failed");
ret = -1;
goto exit;
}
if (BIO_puts(bio, "\nsubject name : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for subject failed");
ret = -1;
goto exit;
}
if (X509_NAME_print_ex(bio, X509_get_subject_name(crt), 0, 0) < 0) {
log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for subject failed");
ret = -1;
goto exit;
}
if (BIO_puts(bio, "\nissued on : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issued on failed");
ret = -1;
goto exit;
}
if (!ASN1_TIME_print(bio, X509_get0_notBefore(crt)))
{
log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for issued on failed");
ret = -1;
goto exit;
}
if (BIO_puts(bio, "\nexpires on : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for expires on failed");
ret = -1;
goto exit;
}
if (!ASN1_TIME_print(bio, X509_get0_notAfter(crt)))
{
log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for expires on failed");
ret = -1;
goto exit;
}
#if OPENSSL_VERSION_NUMBER > 0x10100000L
if (BIO_puts(bio, "\nsigned using : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for signed using failed");
ret = -1;
goto exit;
}
tsig_alg = X509_get0_tbs_sigalg(crt);
if (!i2a_ASN1_OBJECT(bio, tsig_alg->algorithm))
{
log_ssl_errors(LOG_LEVEL_ERROR, "i2a_ASN1_OBJECT() for signed using failed");
ret = -1;
goto exit;
}
#endif
pkey = X509_get_pubkey(crt);
if (!pkey)
{
log_ssl_errors(LOG_LEVEL_ERROR, "X509_get_pubkey() failed");
ret = -1;
goto exit;
}
#define BC "18"
switch (EVP_PKEY_base_id(pkey))
{
case EVP_PKEY_RSA:
ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "RSA key size", EVP_PKEY_bits(pkey));
break;
case EVP_PKEY_DSA:
ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "DSA key size", EVP_PKEY_bits(pkey));
break;
case EVP_PKEY_EC:
ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "EC key size", EVP_PKEY_bits(pkey));
break;
default:
ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "non-RSA/DSA/EC key size",
EVP_PKEY_bits(pkey));
break;
}
if (ret <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key size failed");
ret = -1;
goto exit;
}
loc = X509_get_ext_by_NID(crt, NID_basic_constraints, -1);
if (loc != -1)
{
X509_EXTENSION *ex = X509_get_ext(crt, loc);
if (BIO_puts(bio, "\nbasic constraints : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"BIO_printf() for basic constraints failed");
ret = -1;
goto exit;
}
if (!X509V3_EXT_print(bio, ex, 0, 0))
{
if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"ASN1_STRING_print_ex() for basic constraints failed");
ret = -1;
goto exit;
}
}
}
loc = X509_get_ext_by_NID(crt, NID_subject_alt_name, -1);
if (loc != -1)
{
X509_EXTENSION *ex = X509_get_ext(crt, loc);
if (BIO_puts(bio, "\nsubject alt name : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for alt name failed");
ret = -1;
goto exit;
}
if (!X509V3_EXT_print(bio, ex, 0, 0))
{
if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
ASN1_STRFLGS_RFC2253))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"ASN1_STRING_print_ex() for alt name failed");
ret = -1;
goto exit;
}
}
}
loc = X509_get_ext_by_NID(crt, NID_netscape_cert_type, -1);
if (loc != -1)
{
X509_EXTENSION *ex = X509_get_ext(crt, loc);
if (BIO_puts(bio, "\ncert. type : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for cert type failed");
ret = -1;
goto exit;
}
if (!X509V3_EXT_print(bio, ex, 0, 0))
{
if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
ASN1_STRFLGS_RFC2253))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"ASN1_STRING_print_ex() for cert type failed");
ret = -1;
goto exit;
}
}
}
loc = X509_get_ext_by_NID(crt, NID_key_usage, -1);
if (loc != -1)
{
X509_EXTENSION *ex = X509_get_ext(crt, loc);
if (BIO_puts(bio, "\nkey usage : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key usage failed");
ret = -1;
goto exit;
}
if (!X509V3_EXT_print(bio, ex, 0, 0))
{
if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
ASN1_STRFLGS_RFC2253))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"ASN1_STRING_print_ex() for key usage failed");
ret = -1;
goto exit;
}
}
}
loc = X509_get_ext_by_NID(crt, NID_ext_key_usage, -1);
if (loc != -1) {
X509_EXTENSION *ex = X509_get_ext(crt, loc);
if (BIO_puts(bio, "\next key usage : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"BIO_printf() for ext key usage failed");
ret = -1;
goto exit;
}
if (!X509V3_EXT_print(bio, ex, 0, 0))
{
if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
ASN1_STRFLGS_RFC2253))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"ASN1_STRING_print_ex() for ext key usage failed");
ret = -1;
goto exit;
}
}
}
loc = X509_get_ext_by_NID(crt, NID_certificate_policies, -1);
if (loc != -1)
{
X509_EXTENSION *ex = X509_get_ext(crt, loc);
if (BIO_puts(bio, "\ncertificate policies : ") <= 0)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for certificate policies failed");
ret = -1;
goto exit;
}
if (!X509V3_EXT_print(bio, ex, 0, 0))
{
if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
ASN1_STRFLGS_RFC2253))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"ASN1_STRING_print_ex() for certificate policies failed");
ret = -1;
goto exit;
}
}
}
/* make valgrind happy */
static const char zero = 0;
BIO_write(bio, &zero, 1);
len = BIO_get_mem_data(bio, &bio_mem_data);
if (len <= 0)
{
log_error(LOG_LEVEL_ERROR, "BIO_get_mem_data() returned %ld "
"while gathering certificate information", len);
ret = -1;
goto exit;
}
encoded_text = html_encode(bio_mem_data);
if (encoded_text == NULL)
{
log_error(LOG_LEVEL_ERROR,
"Failed to HTML-encode the certificate information");
ret = -1;
goto exit;
}
strlcpy(last->info_buf, encoded_text, sizeof(last->info_buf));
freez(encoded_text);
ret = 0;
exit:
if (bio)
{
BIO_free(bio);
}
if (pkey)
{
EVP_PKEY_free(pkey);
}
return ret;
}
/*********************************************************************
*
* Function : host_to_hash
*
* Description : Creates MD5 hash from host name. Host name is loaded
* from structure csp and saved again into it.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : -1 => Error while creating hash
* 0 => Hash created successfully
*
*********************************************************************/
static int host_to_hash(struct client_state *csp)
{
int ret = 0;
memset(csp->http->hash_of_host, 0, sizeof(csp->http->hash_of_host));
MD5((unsigned char *)csp->http->host, strlen(csp->http->host),
csp->http->hash_of_host);
/* Converting hash into string with hex */
size_t i = 0;
for (; i < 16; i++)
{
if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
csp->http->hash_of_host[i])) < 0)
{
log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
return -1;
}
}
return 0;
}
/*********************************************************************
*
* Function : create_client_ssl_connection
*
* Description : Creates TLS/SSL secured connection with client
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : 0 on success, negative value if connection wasn't created
* successfully.
*
*********************************************************************/
extern int create_client_ssl_connection(struct client_state *csp)
{
struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
/* Paths to certificates file and key file */
char *key_file = NULL;
char *cert_file = NULL;
int ret = 0;
SSL *ssl;
/*
* Initializing OpenSSL structures for TLS/SSL connection
*/
openssl_init();
/*
* Preparing hash of host for creating certificates
*/
ret = host_to_hash(csp);
if (ret != 0)
{
log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
ret = -1;
goto exit;
}
/*
* Preparing paths to certificates files and key file
*/
cert_file = make_certs_path(csp->config->certificate_directory,
(const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
key_file = make_certs_path(csp->config->certificate_directory,
(const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
if (cert_file == NULL || key_file == NULL)
{
ret = -1;
goto exit;
}
/*
* Generating certificate for requested host. Mutex to prevent
* certificate and key inconsistence must be locked.
*/
privoxy_mutex_lock(&certificate_mutex);
ret = generate_host_certificate(csp);
if (ret < 0)
{
log_error(LOG_LEVEL_ERROR,
"generate_host_certificate failed: %d", ret);
privoxy_mutex_unlock(&certificate_mutex);
ret = -1;
goto exit;
}
privoxy_mutex_unlock(&certificate_mutex);
if (!(ssl_attr->openssl_attr.ctx = SSL_CTX_new(SSLv23_server_method())))
{
log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create SSL context");
ret = -1;
goto exit;
}
/* Set the key and cert */
if (SSL_CTX_use_certificate_file(ssl_attr->openssl_attr.ctx,
cert_file, SSL_FILETYPE_PEM) != 1)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"Loading webpage certificate %s failed", cert_file);
ret = -1;
goto exit;
}
if (SSL_CTX_use_PrivateKey_file(ssl_attr->openssl_attr.ctx,
key_file, SSL_FILETYPE_PEM) != 1)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"Loading webpage certificate private key %s failed", key_file);
ret = -1;
goto exit;
}
SSL_CTX_set_options(ssl_attr->openssl_attr.ctx, SSL_OP_NO_SSLv3);
if (!(ssl_attr->openssl_attr.bio = BIO_new_ssl(ssl_attr->openssl_attr.ctx, 0)))
{
log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
ret = -1;
goto exit;
}
if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1)
{
log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
ret = -1;
goto exit;
}
if (!SSL_set_fd(ssl, csp->cfd))
{
log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
ret = -1;
goto exit;
}
if (csp->config->cipher_list != NULL)
{
if (!SSL_set_cipher_list(ssl, csp->config->cipher_list))
{
log_ssl_errors(LOG_LEVEL_ERROR,
"Setting the cipher list '%s' for the client connection failed",
csp->config->cipher_list);
ret = -1;
goto exit;
}
}
/*
* Handshake with client
*/
log_error(LOG_LEVEL_CONNECT,
"Performing the TLS/SSL handshake with client. Hash of host: %s",
csp->http->hash_of_host_hex);
if (BIO_do_handshake(ssl_attr->openssl_attr.bio) != 1)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"The TLS/SSL handshake with the client failed");
ret = -1;
goto exit;
}
log_error(LOG_LEVEL_CONNECT, "Client successfully connected over %s (%s).",
SSL_get_version(ssl), SSL_get_cipher_name(ssl));
csp->ssl_with_client_is_opened = 1;
ret = 0;
exit:
/*
* Freeing allocated paths to files
*/
freez(cert_file);
freez(key_file);
/* Freeing structures if connection wasn't created successfully */
if (ret < 0)
{
free_client_ssl_structures(csp);
}
return ret;
}
/*********************************************************************
*
* Function : close_client_ssl_connection
*
* Description : Closes TLS/SSL connection with client. This function
* checks if this connection is already created.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : N/A
*
*********************************************************************/
extern void close_client_ssl_connection(struct client_state *csp)
{
struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
SSL *ssl;
if (csp->ssl_with_client_is_opened == 0)
{
return;
}
/*
* Notifying the peer that the connection is being closed.
*/
BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1)
{
log_ssl_errors(LOG_LEVEL_ERROR,
"BIO_get_ssl() failed in close_client_ssl_connection()");
}
else
{
/*
* Pretend we received a shutdown alert so
* the BIO_free_all() call later on returns
* quickly.
*/
SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN);
}
free_client_ssl_structures(csp);
csp->ssl_with_client_is_opened = 0;
}
/*********************************************************************
*
* Function : free_client_ssl_structures
*
* Description : Frees structures used for SSL communication with
* client.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : N/A
*
*********************************************************************/
static void free_client_ssl_structures(struct client_state *csp)
{
struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
if (ssl_attr->openssl_attr.bio)
{
BIO_free_all(ssl_attr->openssl_attr.bio);
}
if (ssl_attr->openssl_attr.ctx)
{
SSL_CTX_free(ssl_attr->openssl_attr.ctx);
}
}
/*********************************************************************
*
* Function : close_server_ssl_connection
*
* Description : Closes TLS/SSL connection with server. This function
* checks if this connection is already opened.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
* Returns : N/A
*
*********************************************************************/
extern void close_server_ssl_connection(struct client_state *csp)
{
struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
SSL *ssl;
if (csp->ssl_with_server_is_opened == 0)
{
return;
}
/*
* Notifying the peer that the connection is being closed.