-
Notifications
You must be signed in to change notification settings - Fork 62
/
ssl.c
1031 lines (795 loc) · 25 KB
/
ssl.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
/*
* Copyright (C) 2011 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <config.h>
#ifdef HAVE_OPENSSL
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/bio.h>
#include "monitor.h"
#include "net.h"
#include "ssl.h"
/* -------------------------------------------------------------- Prototypes */
#define SSLERROR ERR_error_string(ERR_get_error(),NULL)
static int unsigned long ssl_thread_id();
static void ssl_mutex_lock(int, int n, const char *, int );
static int verify_init(ssl_server_connection *);
static int verify_callback(int, X509_STORE_CTX *);
static int check_preverify(X509_STORE_CTX *);
static void cleanup_ssl_socket(ssl_connection *);
static void cleanup_ssl_server_socket(ssl_server_connection *);
static int handle_error(int, ssl_connection *);
static int update_ssl_cert_data(ssl_connection *);
static ssl_server_connection *new_ssl_server_connection(char *, char *);
static int start_ssl();
static int allow_self_certification = FALSE;
static int ssl_initialized = FALSE;
static pthread_mutex_t ssl_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t *ssl_mutex_table;
/* ------------------------------------------------------------- Definitions */
/**
* Number of random bytes to obtain
*/
#define RANDOM_BYTES 1024
/**
* The PRIMARY random device selected for seeding the PRNG. We use a
* non-blocking pseudo random device, to generate pseudo entropy.
*/
#define URANDOM_DEVICE "/dev/urandom"
/**
* If a non-blocking device is not found on the system a blocking
* entropy producer is tried instead.
*/
#define RANDOM_DEVICE "/dev/random"
/**
* SSL Socket methods.
*
* @author Christian Hopp <chopp@iei.tu-clausthal.de>
* @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
* @author Martin Pala, <martinp@tildeslash.com>
*
* @file
*/
/**
* For some of the code I was enlightened by:
*
* An Introduction to OpenSSL Programming, Part I of II
*
* by Eric Rescorla
* Linux Journal 9/2001
* http://www.linuxjournal.com/article.php?sid=4822
*/
/* ------------------------------------------------------------------ Public */
/**
* Embeds a socket in a ssl connection.
* @param socket the socket to be used.
* @return The ssl connection or NULL if an error occured.
*/
int embed_ssl_socket(ssl_connection *ssl, int socket) {
int ssl_error;
time_t ssl_time;
if (!ssl)
return FALSE;
if (!ssl_initialized)
start_ssl();
if (socket >= 0) {
ssl->socket = socket;
} else {
LogError("%s: Socket error!\n", prog);
goto sslerror;
}
if ((ssl->handler = SSL_new (ssl->ctx)) == NULL) {
LogError("%s: Cannot initialize the SSL handler -- %s\n", prog, SSLERROR);
goto sslerror;
}
set_noblock(ssl->socket);
if ((ssl->socket_bio = BIO_new_socket(ssl->socket, BIO_NOCLOSE)) == NULL) {
LogError("%s: Cannot generate IO buffer -- %s\n", prog, SSLERROR);
goto sslerror;
}
SSL_set_bio(ssl->handler, ssl->socket_bio, ssl->socket_bio);
ssl_time = time(NULL);
while ((ssl_error = SSL_connect (ssl->handler)) < 0) {
if ((time(NULL) - ssl_time) > SSL_TIMEOUT) {
LogError("%s: SSL service timeout!\n", prog);
goto sslerror;
}
if (!handle_error(ssl_error, ssl))
goto sslerror;
if (!BIO_should_retry(ssl->socket_bio))
goto sslerror;
}
ssl->cipher = (char *) SSL_get_cipher(ssl->handler);
if (! update_ssl_cert_data(ssl)) {
LogError("%s: Cannot get the SSL server certificate!\n", prog);
goto sslerror;
}
return TRUE;
sslerror:
cleanup_ssl_socket(ssl);
return FALSE;
}
/**
* Compare certificate with given md5 sum
* @param ssl reference to ssl connection
* @param md5sum string of the md5sum to test against
* @return TRUE, if sums do not match FALSE
*/
int check_ssl_md5sum(ssl_connection *ssl, char *md5sum) {
unsigned int i = 0;
ASSERT(md5sum);
while ((i < ssl->cert_md5_len) && (md5sum[2*i] != '\0') && (md5sum[2*i+1] != '\0')) {
unsigned char c = (md5sum[2*i] > 57 ? md5sum[2*i] - 87 : md5sum[2*i] - 48) * 0x10+ (md5sum[2*i+1] > 57 ? md5sum[2*i+1] - 87 : md5sum[2*i+1] - 48);
if (c != ssl->cert_md5[i])
return FALSE;
i++;
}
return TRUE;
}
/**
* Closes a ssl connection (ssl socket + net socket)
* @param ssl ssl connection
* @return TRUE, or FALSE if an error has occured.
*/
int close_ssl_socket(ssl_connection *ssl) {
int rv;
if (!ssl)
return FALSE;
if (! (rv = SSL_shutdown(ssl->handler))) {
shutdown(ssl->socket, 1);
rv = SSL_shutdown(ssl->handler);
}
close_socket(ssl->socket);
cleanup_ssl_socket(ssl);
return (rv > 0) ? TRUE : FALSE;
}
/**
* Garbage collection for non-reusable parts a ssl connection
* @param ssl ssl connection
*/
void delete_ssl_socket(ssl_connection *ssl) {
if (!ssl)
return;
cleanup_ssl_socket(ssl);
if (ssl->ctx && !ssl->accepted)
SSL_CTX_free(ssl->ctx);
ssl->ctx = NULL;
FREE(ssl);
}
/**
* Initializes a ssl connection for server use.
* @param pemfilename Filename for the key/cert file
* @return An ssl connection, or NULL if an error occured.
*/
ssl_server_connection *init_ssl_server(char *pemfile, char *clientpemfile) {
SSL_METHOD *server_method = NULL;
ssl_server_connection *ssl_server;
ASSERT(pemfile);
if (!ssl_initialized)
start_ssl();
ssl_server = new_ssl_server_connection(pemfile, clientpemfile);
#ifdef OPENSSL_FIPS
if (FIPS_mode())
server_method = TLSv1_server_method();
else
server_method = SSLv23_server_method();
#else
server_method = SSLv23_server_method();
#endif
if (!(ssl_server->method = server_method)) {
LogError("%s: Cannot initialize the SSL method -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (!(ssl_server->ctx = SSL_CTX_new(ssl_server->method))) {
LogError("%s: Cannot initialize SSL server certificate handler -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (SSL_CTX_use_certificate_chain_file(ssl_server->ctx, pemfile) != 1) {
LogError("%s: Cannot initialize SSL server certificate -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (SSL_CTX_use_PrivateKey_file(ssl_server->ctx, pemfile, SSL_FILETYPE_PEM) != 1) {
LogError("%s: Cannot initialize SSL server private key -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (SSL_CTX_check_private_key(ssl_server->ctx) != 1) {
LogError("%s: The private key doesn't match the certificate public key -- %s\n", prog, SSLERROR);
goto sslerror;
}
/* Disable session cache */
SSL_CTX_set_session_cache_mode(ssl_server->ctx, SSL_SESS_CACHE_OFF);
/*
* We need this to force transmission of client certs
*/
if (!verify_init(ssl_server)) {
LogError("%s: Verification engine was not properly initialized -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (ssl_server->clientpemfile) {
STACK_OF(X509_NAME) *stack = SSL_CTX_get_client_CA_list(ssl_server->ctx);
LogInfo("%s: Found %d client certificates\n", prog, sk_X509_NAME_num(stack));
}
return ssl_server;
sslerror:
delete_ssl_server_socket(ssl_server);
return NULL;
}
/**
* Deletes a SSL server connection.
* @param ssl_server data for ssl server connection
*/
void delete_ssl_server_socket(ssl_server_connection *ssl_server) {
if (!ssl_server)
return;
cleanup_ssl_server_socket(ssl_server);
if (ssl_server->ctx)
SSL_CTX_free(ssl_server->ctx);
FREE(ssl_server);
}
/**
* Inserts an SSL connection in the connection list of a server.
* @param ssl_server data for ssl server connection
* @return new SSL connection for the connection, or NULL if failed
*/
ssl_connection *insert_accepted_ssl_socket(ssl_server_connection *ssl_server) {
ssl_connection *ssl;
ASSERT(ssl_server);
if (!ssl_initialized)
start_ssl();
NEW(ssl);
ssl->method = NULL;
ssl->handler = NULL;
ssl->cert = NULL;
ssl->cipher = NULL;
ssl->socket = 0;
ssl->next = NULL;
ssl->accepted = FALSE;
ssl->cert_md5= NULL;
ssl->cert_md5_len = 0;
ssl->clientpemfile = NULL;
if (ssl_server->clientpemfile != NULL)
ssl->clientpemfile = xstrdup(ssl_server->clientpemfile);
LOCK(ssl_mutex);
ssl->prev = NULL;
ssl->next = ssl_server->ssl_conn_list;
if ( ssl->next != NULL )
ssl->next->prev = ssl;
END_LOCK;
ssl_server->ssl_conn_list = ssl;
ssl->ctx = ssl_server->ctx;
ssl->accepted = TRUE;
return ssl;
}
/**
* Closes an accepted SSL server connection and deletes it form the
* connection list.
* @param ssl_server data for ssl server connection
* @param ssl data the connection to be deleted
*/
void close_accepted_ssl_socket(ssl_server_connection *ssl_server, ssl_connection *ssl) {
if (!ssl || !ssl_server)
return;
close_socket(ssl->socket);
LOCK(ssl_mutex);
if (ssl->prev == NULL)
ssl_server->ssl_conn_list = ssl->next;
else
ssl->prev->next = ssl->next;
END_LOCK;
delete_ssl_socket(ssl);
}
/**
* Embeds an accepted server socket in an existing ssl connection.
* @param ssl ssl connection
* @param socket the socket to be used.
* @return TRUE, or FALSE if an error has occured.
*/
int embed_accepted_ssl_socket(ssl_connection *ssl, int socket) {
int ssl_error;
time_t ssl_time;
ASSERT(ssl);
ssl->socket = socket;
if (!ssl_initialized)
start_ssl();
if (!(ssl->handler = SSL_new(ssl->ctx))) {
LogError("%s: Cannot initialize the SSL handler -- %s\n", prog, SSLERROR);
return FALSE;
}
if (socket < 0) {
LogError("%s: Socket error!\n", prog);
return FALSE;
}
set_noblock(ssl->socket);
if (!(ssl->socket_bio = BIO_new_socket(ssl->socket, BIO_NOCLOSE))) {
LogError("%s: Cannot generate IO buffer -- %s\n", prog, SSLERROR);
return FALSE;
}
SSL_set_bio(ssl->handler, ssl->socket_bio, ssl->socket_bio);
ssl_time = time(NULL);
while ((ssl_error = SSL_accept(ssl->handler)) < 0) {
if ((time(NULL) - ssl_time) > SSL_TIMEOUT) {
LogError("%s: SSL service timeout!\n", prog);
return FALSE;
}
if (!handle_error(ssl_error, ssl))
return FALSE;
if (!BIO_should_retry(ssl->socket_bio))
return FALSE;
}
ssl->cipher = (char *)SSL_get_cipher(ssl->handler);
if (!update_ssl_cert_data(ssl) && ssl->clientpemfile) {
LogError("%s: The client did not supply a required client certificate!\n",
prog);
return FALSE;
}
if (SSL_get_verify_result(ssl->handler) > 0) {
LogError("%s: Verification of the certificate has failed!\n", prog);
return FALSE;
}
return TRUE;
}
/**
* Send data package though the ssl connection
* @param ssl ssl connection
* @param buffer array containg the data
* @param len size of the data container
* @param timeout Seconds to wait for data to be written
* @return number of bytes transmitted, -1 in case of an error
*/
int send_ssl_socket(ssl_connection *ssl, void *buffer, int len, int timeout) {
int n = 0;
ASSERT(ssl);
do {
n = SSL_write(ssl->handler, buffer, len);
} while (n <= 0 && BIO_should_retry(ssl->socket_bio) && can_write(ssl->socket, timeout));
return (n > 0) ? n : -1;
}
/**
* Receive data package though the ssl connection
* @param ssl ssl connection
* @param buffer array to hold the data
* @param len size of the data container
* @param timeout Seconds to wait for data to be available
* @return number of bytes transmitted, -1 in case of an error
*/
int recv_ssl_socket(ssl_connection *ssl, void *buffer, int len, int timeout) {
int n = 0;
ASSERT(ssl);
do {
n = SSL_read(ssl->handler, buffer, len);
} while (n < 0 && BIO_should_retry(ssl->socket_bio) && can_read(ssl->socket, timeout));
return (n >= 0) ? n : -1;
}
/**
* Stop SSL support library
* @return TRUE, or FALSE if an error has occured.
*/
void stop_ssl() {
if (ssl_initialized) {
int i;
ssl_initialized = FALSE;
ERR_free_strings();
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
for (i = 0; i < CRYPTO_num_locks(); i++)
assert(pthread_mutex_destroy(&ssl_mutex_table[i]) == 0);
FREE(ssl_mutex_table);
RAND_cleanup();
}
}
/**
* Configures the ssl engine
*/
void config_ssl(int conf_allow_self_cert) {
allow_self_certification = conf_allow_self_cert;
}
/**
* Generate a new ssl connection
* @return ssl connection container
*/
ssl_connection *new_ssl_connection(char *clientpemfile, int sslversion) {
ssl_connection *ssl;
if (!ssl_initialized)
start_ssl();
NEW(ssl);
ssl->socket_bio = NULL;
ssl->handler = NULL;
ssl->cert = NULL;
ssl->cipher = NULL;
ssl->socket = 0;
ssl->next = NULL;
ssl->accepted = FALSE;
ssl->cert_md5 = NULL;
ssl->cert_md5_len = 0;
ssl->clientpemfile = clientpemfile ? xstrdup(clientpemfile) : NULL;
switch (sslversion) {
case SSL_VERSION_AUTO:
#ifdef OPENSSL_FIPS
if (FIPS_mode()) {
ssl->method = TLSv1_client_method();
} else {
#endif
ssl->method = SSLv23_client_method();
#ifdef OPENSSL_FIPS
}
#endif
break;
case SSL_VERSION_SSLV2:
#ifdef OPENSSL_FIPS
if (FIPS_mode()) {
LogError("SSLv2 is not allowed in FIPS mode - use TLSv1");
goto sslerror;
} else {
#endif
ssl->method = SSLv2_client_method();
#ifdef OPENSSL_FIPS
}
#endif
break;
case SSL_VERSION_SSLV3:
#ifdef OPENSSL_FIPS
if (FIPS_mode()) {
LogError("SSLv3 is not allowed in FIPS mode - use TLSv1");
goto sslerror;
} else {
#endif
ssl->method = SSLv3_client_method();
#ifdef OPENSSL_FIPS
}
#endif
break;
case SSL_VERSION_TLS:
ssl->method = TLSv1_client_method();
break;
default:
LogError("%s: Unknown SSL version!\n", prog);
goto sslerror;
}
if (!ssl->method) {
LogError("%s: Cannot initialize SSL method -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (!(ssl->ctx = SSL_CTX_new(ssl->method))) {
LogError("%s: Cannot initialize SSL server certificate handler -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (ssl->clientpemfile) {
if (SSL_CTX_use_certificate_chain_file(ssl->ctx, ssl->clientpemfile) <= 0) {
LogError("%s: Cannot initialize SSL server certificate -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (SSL_CTX_use_PrivateKey_file(ssl->ctx, ssl->clientpemfile, SSL_FILETYPE_PEM) <= 0) {
LogError("%s: Cannot initialize SSL server private key -- %s\n", prog, SSLERROR);
goto sslerror;
}
if (!SSL_CTX_check_private_key(ssl->ctx)) {
LogError("%s: Private key does not match the certificate public key -- %s\n", prog, SSLERROR);
goto sslerror;
}
}
return ssl;
sslerror:
delete_ssl_socket(ssl);
return NULL;
}
/* ----------------------------------------------------------------- Private */
/**
* Init verification of transmitted client certs
*/
static int verify_init(ssl_server_connection *ssl_server) {
struct stat stat_buf;
if (!ssl_server->clientpemfile) {
SSL_CTX_set_verify(ssl_server->ctx, SSL_VERIFY_NONE, NULL);
return TRUE;
}
if (stat(ssl_server->clientpemfile, &stat_buf) == -1) {
LogError("%s: Cannot stat the SSL pem path '%s' -- %s\n", prog, Run.httpsslclientpem, STRERROR);
return FALSE;
}
if (S_ISDIR(stat_buf.st_mode)) {
if (!SSL_CTX_load_verify_locations(ssl_server->ctx, NULL , ssl_server->clientpemfile)) {
LogError("%s: Error setting verify directory to %s -- %s\n", prog, Run.httpsslclientpem, SSLERROR);
return FALSE;
}
LogInfo("%s: Loaded SSL client pem directory '%s'\n", prog, ssl_server->clientpemfile);
/* Monit's server cert for cli support */
if (!SSL_CTX_load_verify_locations(ssl_server->ctx, ssl_server->pemfile, NULL)) {
LogError("%s: Error loading verify certificates from %s -- %s\n", prog, ssl_server->pemfile, SSLERROR);
return FALSE;
}
LogInfo("%s: Loaded monit's SSL pem server file '%s'\n", prog, ssl_server->pemfile);
} else if (S_ISREG(stat_buf.st_mode)) {
if (!SSL_CTX_load_verify_locations(ssl_server->ctx, ssl_server->clientpemfile, NULL)) {
LogError("%s: Error loading verify certificates from %s -- %s\n", prog, Run.httpsslclientpem, SSLERROR);
return FALSE;
}
LogInfo("%s: Loaded SSL pem client file '%s'\n", prog, ssl_server->clientpemfile);
/* Monits server cert for cli support ! */
if (!SSL_CTX_load_verify_locations(ssl_server->ctx, ssl_server->pemfile, NULL)) {
LogError("%s: Error loading verify certificates from %s -- %s\n", prog, ssl_server->pemfile, SSLERROR);
return FALSE;
}
LogInfo("%s: Loaded monit's SSL pem server file '%s'\n", prog, ssl_server->pemfile);
SSL_CTX_set_client_CA_list(ssl_server->ctx, SSL_load_client_CA_file(ssl_server->clientpemfile));
} else {
LogError("%s: SSL client pem path is no file or directory %s\n", prog, ssl_server->clientpemfile);
return FALSE;
}
SSL_CTX_set_verify(ssl_server->ctx, SSL_VERIFY_PEER, verify_callback);
return TRUE;
}
/**
* Check the transmitted client certs and a compare with client cert database
*/
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) {
char subject[STRLEN];
X509_OBJECT found_cert;
X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), subject, STRLEN-1);
if (!preverify_ok && !check_preverify(ctx))
return 0;
if (ctx->error_depth == 0 && X509_STORE_get_by_subject(ctx, X509_LU_X509, X509_get_subject_name(ctx->current_cert), &found_cert) != 1) {
LogError("%s: SSL connection rejected. No matching certificate found -- %s\n", prog, SSLERROR);
return 0;
}
return 1;
}
/**
* Analyse errors found before actual verification
* @return TRUE if successful
*/
static int check_preverify(X509_STORE_CTX *ctx) {
if ((ctx->error != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) && (ctx->error != X509_V_ERR_INVALID_PURPOSE)) {
/* Remote site specified a certificate, but it's not correct */
LogError("%s: SSL connection rejected because certificate verification has failed -- error %i\n", prog, ctx->error);
/* Reject connection */
return FALSE;
}
if (allow_self_certification && (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)) {
/* Let's accept self signed certs for the moment! */
LogInfo("%s: SSL connection accepted with self signed certificate!\n", prog);
ctx->error = 0;
return TRUE;
}
/* Reject connection */
LogError("%s: SSL connection rejected because certificate verification has failed -- error %i!\n", prog, ctx->error);
return FALSE;
}
/**
* Helper function for the SSL threadding support
* @return current thread number
*/
static int unsigned long ssl_thread_id() {
return ((unsigned long) pthread_self());
}
/**
* Helper function for the SSL threadding support
*/
static void ssl_mutex_lock(int mode, int n, const char *file, int line) {
if (mode & CRYPTO_LOCK)
assert(pthread_mutex_lock( & ssl_mutex_table[n]) == 0);
else
assert(pthread_mutex_unlock( & ssl_mutex_table[n]) == 0);
}
/**
* Handle errors during read, write, connect and accept
* @return TRUE if non fatal, FALSE if non fatal and retry
*/
static int handle_error(int code, ssl_connection *ssl) {
int ssl_error = SSL_get_error(ssl->handler, code);
switch (ssl_error) {
case SSL_ERROR_WANT_READ:
if (can_read(ssl->socket, SSL_TIMEOUT))
return TRUE;
LogError("%s: Openssl read timeout error!\n", prog);
break;
case SSL_ERROR_WANT_WRITE:
if (can_read(ssl->socket, SSL_TIMEOUT))
return TRUE;
LogError("%s: Openssl write timeout error!\n", prog);
break;
case SSL_ERROR_SYSCALL:
LogError("%s: Openssl syscall error: %s!\n", prog, STRERROR);
break;
case SSL_ERROR_SSL:
LogError("%s: Openssl engine error: %s\n", prog, SSLERROR);
break;
default:
LogError("%s: Openssl error!\n", prog);
break;
}
return FALSE;
}
/**
* Garbage collection for non reusable parts of the ssl connection
* @param ssl ssl connection
*/
static void cleanup_ssl_socket(ssl_connection *ssl) {
if (!ssl)
return;
if (ssl->cert) {
X509_free(ssl->cert);
ssl->cert = NULL;
}
if (ssl->handler) {
SSL_free(ssl->handler);
ssl->handler = NULL;
}
if (ssl->socket_bio) {
/* no BIO_free(ssl->socket_bio); necessary, because BIO is freed by ssl->handler */
ssl->socket_bio = NULL;
}
FREE(ssl->cert_issuer);
FREE(ssl->cert_subject);
FREE(ssl->cert_md5);
FREE(ssl->clientpemfile);
}
/**
* Garbage collection for a SSL server connection.
* @param ssl_server data for ssl server connection
*/
static void cleanup_ssl_server_socket(ssl_server_connection *ssl_server) {
if (!ssl_server)
return;
FREE(ssl_server->pemfile);
FREE(ssl_server->clientpemfile);
while (ssl_server->ssl_conn_list) {
ssl_connection *ssl = ssl_server->ssl_conn_list;
ssl_server->ssl_conn_list = ssl_server->ssl_conn_list->next;
close_accepted_ssl_socket(ssl_server, ssl);
}
}
/**
* Updates some data in the ssl connection
* @param ssl reference to ssl connection
* @return TRUE, if not successful FALSE
*/
static int update_ssl_cert_data(ssl_connection *ssl) {
unsigned char md5[EVP_MAX_MD_SIZE];
ASSERT(ssl);
if (!(ssl->cert = SSL_get_peer_certificate(ssl->handler)))
return FALSE;
#ifdef OPENSSL_FIPS
if (!FIPS_mode()) {
/* In FIPS-140 mode, MD5 is unavailable. */
#endif
ssl->cert_issuer = X509_NAME_oneline (X509_get_issuer_name(ssl->cert), 0, 0);
ssl->cert_subject = X509_NAME_oneline (X509_get_subject_name(ssl->cert), 0, 0);
X509_digest(ssl->cert, EVP_md5(), md5, &ssl->cert_md5_len);
ssl->cert_md5= (unsigned char *)xstrdup((char *)md5);
#ifdef OPENSSL_FIPS
}
#endif
return TRUE;
}
/**
* Generate a new ssl server connection
* @return ssl server connection container
*/
static ssl_server_connection *new_ssl_server_connection(char * pemfile, char * clientpemfile) {
ssl_server_connection *ssl_server;
ASSERT(pemfile);
NEW(ssl_server);
ssl_server->ctx = NULL;
ssl_server->method = NULL;
ssl_server->server_socket = 0;
ssl_server->ssl_conn_list = NULL;
ssl_server->pemfile = xstrdup(pemfile);
ssl_server->clientpemfile = clientpemfile ? xstrdup(clientpemfile) : NULL;
return ssl_server;
}
#ifdef OPENSSL_FIPS
/**
* Enable FIPS mode, if it isn't enabled yet.
*/
void enable_fips_mode() {
if (!FIPS_mode()) {
ASSERT(FIPS_mode_set(1));
LogInfo("FIPS-140 mode is enabled\n");
}
}
#endif
/**
* Start SSL support library. It has to be run before the SSL support
* can be used.
* @return TRUE, or FALSE if an error has occured.
*/
static int start_ssl() {
if (! ssl_initialized) {
int i;