-
Notifications
You must be signed in to change notification settings - Fork 271
/
ssl-proxy-openssl.c
1425 lines (1216 loc) · 36.2 KB
/
ssl-proxy-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
/* Copyright (c) 2002-2016 Dovecot authors, see the included COPYING file */
#include "login-common.h"
#include "array.h"
#include "ioloop.h"
#include "net.h"
#include "ostream.h"
#include "read-full.h"
#include "safe-memset.h"
#include "hash.h"
#include "llist.h"
#include "master-interface.h"
#include "master-service-ssl-settings.h"
#include "client-common.h"
#include "ssl-proxy.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef HAVE_OPENSSL
#include "iostream-openssl.h"
#include <openssl/crypto.h>
#include <openssl/engine.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#if !defined(OPENSSL_NO_ECDH) && OPENSSL_VERSION_NUMBER >= 0x10000000L
# define HAVE_ECDH
#endif
/* Check every 30 minutes if parameters file has been updated */
#define SSL_PARAMFILE_CHECK_INTERVAL (60*30)
#define SSL_PARAMETERS_PATH "ssl-params"
#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME /* FIXME: this may be unnecessary.. */
# undef HAVE_SSL_GET_SERVERNAME
#endif
enum ssl_io_action {
SSL_ADD_INPUT,
SSL_REMOVE_INPUT,
SSL_ADD_OUTPUT,
SSL_REMOVE_OUTPUT
};
struct ssl_proxy {
int refcount;
struct ssl_proxy *prev, *next;
SSL *ssl;
struct client *client;
struct ip_addr ip;
const struct login_settings *login_set;
const struct master_service_ssl_settings *ssl_set;
pool_t set_pool;
int fd_ssl, fd_plain;
struct io *io_ssl_read, *io_ssl_write, *io_plain_read, *io_plain_write;
unsigned char plainout_buf[1024];
unsigned int plainout_size;
unsigned char sslout_buf[1024];
unsigned int sslout_size;
ssl_handshake_callback_t *handshake_callback;
void *handshake_context;
const char *cert_error;
char *last_error;
unsigned int handshaked:1;
unsigned int destroyed:1;
unsigned int cert_received:1;
unsigned int cert_broken:1;
unsigned int client_proxy:1;
unsigned int flushing:1;
unsigned int failed:1;
};
struct ssl_parameters {
const char *path;
time_t last_refresh;
int fd;
DH *dh_512, *dh_default;
};
struct ssl_server_context {
SSL_CTX *ctx;
pool_t pool;
const char *cert;
const char *key;
const char *ca;
const char *cipher_list;
const char *protocols;
bool verify_client_cert;
bool prefer_server_ciphers;
bool compression;
bool tickets;
};
static int extdata_index;
static HASH_TABLE(struct ssl_server_context *,
struct ssl_server_context *) ssl_servers;
static SSL_CTX *ssl_client_ctx;
static unsigned int ssl_proxy_count;
static struct ssl_proxy *ssl_proxies;
static struct ssl_parameters ssl_params;
static int ssl_username_nid;
static ENGINE *ssl_engine;
static void plain_read(struct ssl_proxy *proxy);
static void ssl_read(struct ssl_proxy *proxy);
static void ssl_write(struct ssl_proxy *proxy);
static void ssl_step(struct ssl_proxy *proxy);
static void ssl_proxy_unref(struct ssl_proxy *proxy);
static struct ssl_server_context *
ssl_server_context_init(const struct login_settings *login_set,
const struct master_service_ssl_settings *ssl_set);
static void ssl_server_context_deinit(struct ssl_server_context **_ctx);
static void ssl_proxy_ctx_set_crypto_params(SSL_CTX *ssl_ctx,
const struct master_service_ssl_settings *set);
#if defined(HAVE_ECDH) && !defined(SSL_CTRL_SET_ECDH_AUTO)
static int ssl_proxy_ctx_get_pkey_ec_curve_name(const struct master_service_ssl_settings *set);
#endif
static void ssl_proxy_destroy_failed(struct ssl_proxy *proxy)
{
proxy->failed = TRUE;
ssl_proxy_destroy(proxy);
}
static unsigned int ssl_server_context_hash(const struct ssl_server_context *ctx)
{
unsigned int i, g, h = 0;
/* checking for different certs is typically good enough,
and it should be enough to check only the first few bytes. */
for (i = 0; i < 16 && ctx->cert[i] != '\0'; i++) {
h = (h << 4) + ctx->cert[i];
if ((g = h & 0xf0000000UL)) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
static int ssl_server_context_cmp(const struct ssl_server_context *ctx1,
const struct ssl_server_context *ctx2)
{
if (strcmp(ctx1->cert, ctx2->cert) != 0)
return 1;
if (strcmp(ctx1->key, ctx2->key) != 0)
return 1;
if (null_strcmp(ctx1->ca, ctx2->ca) != 0)
return 1;
if (null_strcmp(ctx1->cipher_list, ctx2->cipher_list) != 0)
return 1;
if (null_strcmp(ctx1->protocols, ctx2->protocols) != 0)
return 1;
return ctx1->verify_client_cert == ctx2->verify_client_cert ? 0 : 1;
}
static void ssl_params_corrupted(const char *reason)
{
i_fatal("Corrupted SSL ssl-parameters.dat in state_dir: %s", reason);
}
static void read_next(struct ssl_parameters *params, void *data, size_t size)
{
int ret;
if ((ret = read_full(params->fd, data, size)) < 0)
i_fatal("read(%s) failed: %m", params->path);
if (ret == 0)
ssl_params_corrupted("Truncated file");
}
static bool read_dh_parameters_next(struct ssl_parameters *params)
{
unsigned char *buf;
const unsigned char *cbuf;
unsigned int len;
int bits;
/* read bit size. 0 ends the DH parameters list. */
read_next(params, &bits, sizeof(bits));
if (bits == 0)
return FALSE;
/* read data size. */
read_next(params, &len, sizeof(len));
if (len > 1024*100) /* should be enough? */
ssl_params_corrupted("File too large");
buf = i_malloc(len);
read_next(params, buf, len);
cbuf = buf;
switch (bits) {
case 512:
if (params->dh_512 != NULL)
ssl_params_corrupted("Duplicate 512bit parameters");
params->dh_512 = d2i_DHparams(NULL, &cbuf, len);
break;
default:
if (params->dh_default != NULL)
ssl_params_corrupted("Duplicate default parameters");
params->dh_default = d2i_DHparams(NULL, &cbuf, len);
break;
}
i_free(buf);
return TRUE;
}
static void ssl_free_parameters(struct ssl_parameters *params)
{
if (params->dh_512 != NULL) {
DH_free(params->dh_512);
params->dh_512 = NULL;
}
if (params->dh_default != NULL) {
DH_free(params->dh_default);
params->dh_default = NULL;
}
}
static void ssl_refresh_parameters(struct ssl_parameters *params)
{
char c;
int ret;
if (params->last_refresh > ioloop_time - SSL_PARAMFILE_CHECK_INTERVAL)
return;
params->last_refresh = ioloop_time;
params->fd = net_connect_unix(params->path);
if (params->fd == -1) {
i_error("connect(%s) failed: %m", params->path);
return;
}
net_set_nonblock(params->fd, FALSE);
ssl_free_parameters(params);
while (read_dh_parameters_next(params)) ;
if ((ret = read_full(params->fd, &c, 1)) < 0)
i_fatal("read(%s) failed: %m", params->path);
else if (ret != 0) {
/* more data than expected */
ssl_params_corrupted("More data than expected");
}
if (close(params->fd) < 0)
i_error("close(%s) failed: %m", params->path);
params->fd = -1;
}
static void ssl_set_io(struct ssl_proxy *proxy, enum ssl_io_action action)
{
switch (action) {
case SSL_ADD_INPUT:
if (proxy->io_ssl_read != NULL)
break;
proxy->io_ssl_read = io_add(proxy->fd_ssl, IO_READ,
ssl_step, proxy);
break;
case SSL_REMOVE_INPUT:
if (proxy->io_ssl_read != NULL)
io_remove(&proxy->io_ssl_read);
break;
case SSL_ADD_OUTPUT:
if (proxy->io_ssl_write != NULL)
break;
proxy->io_ssl_write = io_add(proxy->fd_ssl, IO_WRITE,
ssl_step, proxy);
break;
case SSL_REMOVE_OUTPUT:
if (proxy->io_ssl_write != NULL)
io_remove(&proxy->io_ssl_write);
break;
}
}
static void plain_block_input(struct ssl_proxy *proxy, bool block)
{
if (block) {
if (proxy->io_plain_read != NULL)
io_remove(&proxy->io_plain_read);
} else {
if (proxy->io_plain_read == NULL) {
proxy->io_plain_read = io_add(proxy->fd_plain, IO_READ,
plain_read, proxy);
}
}
}
static void plain_read(struct ssl_proxy *proxy)
{
ssize_t ret;
bool corked = FALSE;
if (proxy->sslout_size == sizeof(proxy->sslout_buf)) {
/* buffer full, block input until it's written */
plain_block_input(proxy, TRUE);
return;
}
proxy->refcount++;
while (proxy->sslout_size < sizeof(proxy->sslout_buf) &&
!proxy->destroyed) {
ret = net_receive(proxy->fd_plain,
proxy->sslout_buf + proxy->sslout_size,
sizeof(proxy->sslout_buf) -
proxy->sslout_size);
if (ret <= 0) {
if (ret < 0)
ssl_proxy_destroy(proxy);
break;
} else {
proxy->sslout_size += ret;
if (!corked) {
if (net_set_cork(proxy->fd_ssl, TRUE) == 0)
corked = TRUE;
}
ssl_write(proxy);
}
}
if (corked)
(void)net_set_cork(proxy->fd_ssl, FALSE);
ssl_proxy_unref(proxy);
}
static void plain_write(struct ssl_proxy *proxy)
{
ssize_t ret;
proxy->refcount++;
ret = net_transmit(proxy->fd_plain, proxy->plainout_buf,
proxy->plainout_size);
if (ret < 0)
ssl_proxy_destroy(proxy);
else {
proxy->plainout_size -= ret;
memmove(proxy->plainout_buf, proxy->plainout_buf + ret,
proxy->plainout_size);
if (proxy->plainout_size > 0) {
if (proxy->io_plain_write == NULL) {
proxy->io_plain_write =
io_add(proxy->fd_plain, IO_WRITE,
plain_write, proxy);
}
} else {
if (proxy->io_plain_write != NULL)
io_remove(&proxy->io_plain_write);
}
ssl_set_io(proxy, SSL_ADD_INPUT);
if (SSL_pending(proxy->ssl) > 0)
ssl_read(proxy);
}
ssl_proxy_unref(proxy);
}
static void ssl_handle_error(struct ssl_proxy *proxy, int ret,
const char *func_name)
{
const char *errstr = NULL;
int err;
proxy->refcount++;
i_free_and_null(proxy->last_error);
err = SSL_get_error(proxy->ssl, ret);
switch (err) {
case SSL_ERROR_WANT_READ:
ssl_set_io(proxy, SSL_ADD_INPUT);
break;
case SSL_ERROR_WANT_WRITE:
ssl_set_io(proxy, SSL_ADD_OUTPUT);
break;
case SSL_ERROR_SYSCALL:
/* eat up the error queue */
if (ERR_peek_error() != 0)
errstr = openssl_iostream_error();
else if (ret != 0)
errstr = strerror(errno);
else {
/* EOF. */
errstr = "Disconnected";
break;
}
errstr = t_strdup_printf("%s syscall failed: %s",
func_name, errstr);
break;
case SSL_ERROR_ZERO_RETURN:
/* clean connection closing */
ssl_proxy_destroy(proxy);
break;
case SSL_ERROR_SSL:
if (ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE) {
i_error("OpenSSL malloc() failed. "
"You may need to increase service %s { vsz_limit }",
login_binary->process_name);
}
errstr = t_strdup_printf("%s failed: %s",
func_name, openssl_iostream_error());
break;
default:
errstr = t_strdup_printf("%s failed: unknown failure %d (%s)",
func_name, err, openssl_iostream_error());
break;
}
if (errstr != NULL) {
if (proxy->ssl_set->verbose_ssl)
i_debug("SSL error: %s", errstr);
proxy->last_error = i_strdup(errstr);
ssl_proxy_destroy_failed(proxy);
}
ssl_proxy_unref(proxy);
}
static void ssl_handshake(struct ssl_proxy *proxy)
{
int ret;
if (proxy->client_proxy) {
ret = SSL_connect(proxy->ssl);
if (ret != 1) {
ssl_handle_error(proxy, ret, "SSL_connect()");
return;
}
} else {
ret = SSL_accept(proxy->ssl);
if (ret != 1) {
ssl_handle_error(proxy, ret, "SSL_accept()");
return;
}
}
i_free_and_null(proxy->last_error);
proxy->handshaked = TRUE;
ssl_set_io(proxy, SSL_ADD_INPUT);
plain_block_input(proxy, FALSE);
if (proxy->handshake_callback != NULL) {
if (proxy->handshake_callback(proxy->handshake_context) < 0)
ssl_proxy_destroy_failed(proxy);
}
}
static void ssl_read(struct ssl_proxy *proxy)
{
int ret;
while (proxy->plainout_size < sizeof(proxy->plainout_buf) &&
!proxy->destroyed) {
ret = SSL_read(proxy->ssl,
proxy->plainout_buf + proxy->plainout_size,
sizeof(proxy->plainout_buf) -
proxy->plainout_size);
if (ret <= 0) {
ssl_handle_error(proxy, ret, "SSL_read()");
break;
} else {
i_free_and_null(proxy->last_error);
proxy->plainout_size += ret;
plain_write(proxy);
}
}
}
static void ssl_write(struct ssl_proxy *proxy)
{
int ret;
ret = SSL_write(proxy->ssl, proxy->sslout_buf, proxy->sslout_size);
if (ret <= 0)
ssl_handle_error(proxy, ret, "SSL_write()");
else {
i_free_and_null(proxy->last_error);
proxy->sslout_size -= ret;
memmove(proxy->sslout_buf, proxy->sslout_buf + ret,
proxy->sslout_size);
ssl_set_io(proxy, proxy->sslout_size > 0 ?
SSL_ADD_OUTPUT : SSL_REMOVE_OUTPUT);
plain_block_input(proxy, FALSE);
}
}
static void ssl_step(struct ssl_proxy *proxy)
{
proxy->refcount++;
if (!proxy->handshaked) {
ssl_set_io(proxy, SSL_REMOVE_OUTPUT);
ssl_handshake(proxy);
}
if (proxy->handshaked) {
if (proxy->plainout_size == sizeof(proxy->plainout_buf))
ssl_set_io(proxy, SSL_REMOVE_INPUT);
else
ssl_read(proxy);
if (proxy->sslout_size == 0)
ssl_set_io(proxy, SSL_REMOVE_OUTPUT);
else {
(void)net_set_cork(proxy->fd_ssl, TRUE);
ssl_write(proxy);
(void)net_set_cork(proxy->fd_ssl, FALSE);
}
}
ssl_proxy_unref(proxy);
}
static int
ssl_proxy_alloc_common(SSL_CTX *ssl_ctx, int fd, const struct ip_addr *ip,
pool_t set_pool, const struct login_settings *login_set,
const struct master_service_ssl_settings *ssl_set,
struct ssl_proxy **proxy_r)
{
struct ssl_proxy *proxy;
SSL *ssl;
int sfd[2];
i_assert(fd != -1);
*proxy_r = NULL;
if (!ssl_initialized) {
i_error("SSL support not enabled in configuration");
return -1;
}
ssl_refresh_parameters(&ssl_params);
ssl = SSL_new(ssl_ctx);
if (ssl == NULL) {
i_error("SSL_new() failed: %s", openssl_iostream_error());
return -1;
}
if (SSL_set_fd(ssl, fd) != 1) {
i_error("SSL_set_fd() failed: %s", openssl_iostream_error());
SSL_free(ssl);
return -1;
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0) {
i_error("socketpair() failed: %m");
SSL_free(ssl);
return -1;
}
net_set_nonblock(sfd[0], TRUE);
net_set_nonblock(sfd[1], TRUE);
net_set_nonblock(fd, TRUE);
proxy = i_new(struct ssl_proxy, 1);
proxy->refcount = 2;
proxy->ssl = ssl;
proxy->login_set = login_set;
proxy->ssl_set = ssl_set;
proxy->fd_ssl = fd;
proxy->fd_plain = sfd[0];
proxy->ip = *ip;
proxy->set_pool = set_pool;
pool_ref(set_pool);
SSL_set_ex_data(ssl, extdata_index, proxy);
ssl_proxy_count++;
DLLIST_PREPEND(&ssl_proxies, proxy);
*proxy_r = proxy;
return sfd[1];
}
static struct ssl_server_context *
ssl_server_context_get(const struct login_settings *login_set,
const struct master_service_ssl_settings *set)
{
struct ssl_server_context *ctx, lookup_ctx;
memset(&lookup_ctx, 0, sizeof(lookup_ctx));
lookup_ctx.cert = set->ssl_cert;
lookup_ctx.key = set->ssl_key;
lookup_ctx.ca = set->ssl_ca;
lookup_ctx.cipher_list = set->ssl_cipher_list;
lookup_ctx.protocols = set->ssl_protocols;
lookup_ctx.verify_client_cert = set->ssl_verify_client_cert ||
login_set->auth_ssl_require_client_cert ||
login_set->auth_ssl_username_from_cert;
lookup_ctx.prefer_server_ciphers = set->ssl_prefer_server_ciphers;
lookup_ctx.compression = set->parsed_opts.compression;
lookup_ctx.tickets = set->parsed_opts.tickets;
ctx = hash_table_lookup(ssl_servers, &lookup_ctx);
if (ctx == NULL)
ctx = ssl_server_context_init(login_set, set);
return ctx;
}
int ssl_proxy_alloc(int fd, const struct ip_addr *ip, pool_t set_pool,
const struct login_settings *login_set,
const struct master_service_ssl_settings *ssl_set,
struct ssl_proxy **proxy_r)
{
struct ssl_server_context *ctx;
ctx = ssl_server_context_get(login_set, ssl_set);
return ssl_proxy_alloc_common(ctx->ctx, fd, ip,
set_pool, login_set, ssl_set, proxy_r);
}
int ssl_proxy_client_alloc(int fd, struct ip_addr *ip, pool_t set_pool,
const struct login_settings *login_set,
const struct master_service_ssl_settings *ssl_set,
ssl_handshake_callback_t *callback, void *context,
struct ssl_proxy **proxy_r)
{
int ret;
ret = ssl_proxy_alloc_common(ssl_client_ctx, fd, ip,
set_pool, login_set, ssl_set, proxy_r);
if (ret < 0)
return -1;
(*proxy_r)->handshake_callback = callback;
(*proxy_r)->handshake_context = context;
(*proxy_r)->client_proxy = TRUE;
return ret;
}
void ssl_proxy_start(struct ssl_proxy *proxy)
{
ssl_step(proxy);
}
void ssl_proxy_set_client(struct ssl_proxy *proxy, struct client *client)
{
i_assert(proxy->client == NULL);
client_ref(client);
proxy->client = client;
}
bool ssl_proxy_has_valid_client_cert(const struct ssl_proxy *proxy)
{
return proxy->cert_received && !proxy->cert_broken;
}
bool ssl_proxy_has_broken_client_cert(struct ssl_proxy *proxy)
{
return proxy->cert_received && proxy->cert_broken;
}
int ssl_proxy_cert_match_name(struct ssl_proxy *proxy, const char *verify_name)
{
return openssl_cert_match_name(proxy->ssl, verify_name);
}
const char *ssl_proxy_get_peer_name(struct ssl_proxy *proxy)
{
X509 *x509;
char *name;
int len;
if (!ssl_proxy_has_valid_client_cert(proxy))
return NULL;
x509 = SSL_get_peer_certificate(proxy->ssl);
if (x509 == NULL)
return NULL; /* we should have had it.. */
len = X509_NAME_get_text_by_NID(X509_get_subject_name(x509),
ssl_username_nid, NULL, 0);
if (len < 0)
name = "";
else {
name = t_malloc(len + 1);
if (X509_NAME_get_text_by_NID(X509_get_subject_name(x509),
ssl_username_nid, name, len + 1) < 0)
name = "";
else if (strlen(name) != (size_t)len) {
/* NUL characters in name. Someone's trying to fake
being another user? Don't allow it. */
name = "";
}
}
X509_free(x509);
return *name == '\0' ? NULL : name;
}
bool ssl_proxy_is_handshaked(const struct ssl_proxy *proxy)
{
return proxy->handshaked;
}
const char *ssl_proxy_get_last_error(const struct ssl_proxy *proxy)
{
return proxy->last_error;
}
const char *ssl_proxy_get_security_string(struct ssl_proxy *proxy)
{
const SSL_CIPHER *cipher;
int bits, alg_bits;
const char *comp_str;
if (!proxy->handshaked)
return "";
cipher = SSL_get_current_cipher(proxy->ssl);
bits = SSL_CIPHER_get_bits(cipher, &alg_bits);
comp_str = ssl_proxy_get_compression(proxy);
comp_str = comp_str == NULL ? "" : t_strconcat(" ", comp_str, NULL);
return t_strdup_printf("%s with cipher %s (%d/%d bits)%s",
SSL_get_version(proxy->ssl),
SSL_CIPHER_get_name(cipher),
bits, alg_bits, comp_str);
}
const char *ssl_proxy_get_compression(struct ssl_proxy *proxy ATTR_UNUSED)
{
#if defined(HAVE_SSL_COMPRESSION) && !defined(OPENSSL_NO_COMP)
const COMP_METHOD *comp;
comp = SSL_get_current_compression(proxy->ssl);
return comp == NULL ? NULL : SSL_COMP_get_name(comp);
#else
return NULL;
#endif
}
const char *ssl_proxy_get_cert_error(struct ssl_proxy *proxy)
{
return proxy->cert_error != NULL ? proxy->cert_error :
"(Unknown error)";
}
void ssl_proxy_free(struct ssl_proxy **_proxy)
{
struct ssl_proxy *proxy = *_proxy;
*_proxy = NULL;
ssl_proxy_unref(proxy);
}
static void ssl_proxy_unref(struct ssl_proxy *proxy)
{
if (--proxy->refcount > 0)
return;
i_assert(proxy->refcount == 0);
SSL_free(proxy->ssl);
pool_unref(&proxy->set_pool);
i_free(proxy->last_error);
i_free(proxy);
}
static void ssl_proxy_flush(struct ssl_proxy *proxy)
{
/* this is pretty kludgy. mainly this is just for flushing the final
LOGOUT command output. */
plain_read(proxy);
ssl_step(proxy);
}
void ssl_proxy_destroy(struct ssl_proxy *proxy)
{
if (proxy->destroyed || proxy->flushing)
return;
proxy->flushing = TRUE;
if (!proxy->failed && proxy->handshaked)
ssl_proxy_flush(proxy);
proxy->destroyed = TRUE;
ssl_proxy_count--;
DLLIST_REMOVE(&ssl_proxies, proxy);
if (proxy->io_ssl_read != NULL)
io_remove(&proxy->io_ssl_read);
if (proxy->io_ssl_write != NULL)
io_remove(&proxy->io_ssl_write);
if (proxy->io_plain_read != NULL)
io_remove(&proxy->io_plain_read);
if (proxy->io_plain_write != NULL)
io_remove(&proxy->io_plain_write);
if (SSL_shutdown(proxy->ssl) != 1) {
/* if bidirectional shutdown fails we need to clear
the error queue. */
openssl_iostream_clear_errors();
}
net_disconnect(proxy->fd_ssl);
net_disconnect(proxy->fd_plain);
if (proxy->client != NULL)
client_unref(&proxy->client);
ssl_proxy_unref(proxy);
}
static RSA *ssl_gen_rsa_key(SSL *ssl ATTR_UNUSED,
int is_export ATTR_UNUSED, int keylength)
{
RSA *rsa = RSA_new();
BIGNUM *e = BN_new();
BN_set_word(e, RSA_F4);
RSA_generate_key_ex(rsa, keylength, e, NULL);
BN_free(e);
return rsa;
}
static DH *ssl_tmp_dh_callback(SSL *ssl ATTR_UNUSED,
int is_export, int keylength)
{
if (is_export && keylength == 512 && ssl_params.dh_512 != NULL)
return ssl_params.dh_512;
return ssl_params.dh_default;
}
static void ssl_info_callback(const SSL *ssl, int where, int ret)
{
struct ssl_proxy *proxy;
proxy = SSL_get_ex_data(ssl, extdata_index);
if (!proxy->ssl_set->verbose_ssl)
return;
if ((where & SSL_CB_ALERT) != 0) {
switch (ret & 0xff) {
case SSL_AD_CLOSE_NOTIFY:
i_debug("SSL alert: %s [%s]",
SSL_alert_desc_string_long(ret),
net_ip2addr(&proxy->ip));
break;
default:
i_warning("SSL alert: where=0x%x, ret=%d: %s %s [%s]",
where, ret, SSL_alert_type_string_long(ret),
SSL_alert_desc_string_long(ret),
net_ip2addr(&proxy->ip));
break;
}
} else if (ret == 0) {
i_warning("SSL failed: where=0x%x: %s [%s]",
where, SSL_state_string_long(ssl),
net_ip2addr(&proxy->ip));
} else {
i_debug("SSL: where=0x%x, ret=%d: %s [%s]",
where, ret, SSL_state_string_long(ssl),
net_ip2addr(&proxy->ip));
}
}
static int ssl_verify_client_cert(int preverify_ok, X509_STORE_CTX *ctx)
{
SSL *ssl;
struct ssl_proxy *proxy;
int ctxerr;
char buf[1024];
X509_NAME *subject;
ssl = X509_STORE_CTX_get_ex_data(ctx,
SSL_get_ex_data_X509_STORE_CTX_idx());
proxy = SSL_get_ex_data(ssl, extdata_index);
proxy->cert_received = TRUE;
ctxerr = X509_STORE_CTX_get_error(ctx);
if (proxy->client_proxy && !proxy->login_set->ssl_require_crl &&
(ctxerr == X509_V_ERR_UNABLE_TO_GET_CRL ||
ctxerr == X509_V_ERR_CRL_HAS_EXPIRED)) {
/* no CRL given with the CA list. don't worry about it. */
preverify_ok = 1;
}
if (!preverify_ok)
proxy->cert_broken = TRUE;
subject = X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx));
(void)X509_NAME_oneline(subject, buf, sizeof(buf));
buf[sizeof(buf)-1] = '\0'; /* just in case.. */
ctxerr = X509_STORE_CTX_get_error(ctx);
if (proxy->cert_error == NULL) {
proxy->cert_error = p_strdup_printf(proxy->client->pool, "%s: %s",
X509_verify_cert_error_string(ctxerr), buf);
}
if (proxy->ssl_set->verbose_ssl ||
(proxy->login_set->auth_verbose && !preverify_ok)) {
if (preverify_ok) {
client_log(proxy->client, t_strdup_printf(
"Valid certificate: %s", buf));
} else {
client_log(proxy->client, t_strdup_printf(
"Invalid certificate: %s: %s",
X509_verify_cert_error_string(ctxerr), buf));
}
}
/* Return success anyway, because if ssl_require_client_cert=no we
could still allow authentication. */
return 1;
}
static int
pem_password_callback(char *buf, int size, int rwflag ATTR_UNUSED,
void *userdata)
{
if (userdata == NULL) {
i_error("SSL private key file is password protected, "
"but password isn't given");
return 0;
}
if (i_strocpy(buf, userdata, size) < 0)
return 0;
return strlen(buf);
}
unsigned int ssl_proxy_get_count(void)
{
return ssl_proxy_count;
}
static void load_ca(X509_STORE *store, const char *ca,
STACK_OF(X509_NAME) **xnames_r)
{
/* mostly just copy&pasted from X509_load_cert_crl_file() */
STACK_OF(X509_INFO) *inf;
X509_INFO *itmp;
X509_NAME *xname;
BIO *bio;
int i;
bio = BIO_new_mem_buf(t_strdup_noconst(ca), strlen(ca));
if (bio == NULL)
i_fatal("BIO_new_mem_buf() failed");
inf = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
if (inf == NULL)
i_fatal("Couldn't parse ssl_ca: %s", openssl_iostream_error());
BIO_free(bio);
if (xnames_r != NULL) {
*xnames_r = sk_X509_NAME_new_null();
if (*xnames_r == NULL)
i_fatal_status(FATAL_OUTOFMEM, "sk_X509_NAME_new_null() failed");
}
for(i = 0; i < sk_X509_INFO_num(inf); i++) {
itmp = sk_X509_INFO_value(inf, i);
if(itmp->x509) {
X509_STORE_add_cert(store, itmp->x509);
xname = X509_get_subject_name(itmp->x509);
if (xname != NULL && xnames_r != NULL) {
xname = X509_NAME_dup(xname);
if (xname == NULL)
i_fatal_status(FATAL_OUTOFMEM, "X509_NAME_dup() failed");
sk_X509_NAME_push(*xnames_r, xname);
}
}
if(itmp->crl)
X509_STORE_add_crl(store, itmp->crl);
}
sk_X509_INFO_pop_free(inf, X509_INFO_free);
}
static STACK_OF(X509_NAME) *
ssl_proxy_ctx_init(SSL_CTX *ssl_ctx, const struct master_service_ssl_settings *set,
bool load_xnames)
{
X509_STORE *store;