-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathssl.cc
1560 lines (1373 loc) · 62.3 KB
/
ssl.cc
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
/* SSL implementation for redis
*
* -----------------------------------------------------------------------------
*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stddef.h>
#include <string.h>
#include "ssl.h"
#include <stdlib.h>
#include <inttypes.h>
ssl_t g_ssl_config;
/* Return the file proc for a file descriptor and given event
* (AE_READABLE or AE_WRITABLE) or null if one does not exist. */
aeFileProc* aeGetFileProc(aeEventLoop *eventLoop, int fd, int event) {
if (fd >= eventLoop->setsize) return NULL;
aeFileEvent *fe = &eventLoop->events[fd];
if (event == AE_READABLE) {
return fe->rfileProc;
} else if (event == AE_WRITABLE) {
return fe->wfileProc;
}
return NULL;
}
/* Return the client data associated with a file descriptor,
* or null if it does not exist. */
void* aeGetClientData(aeEventLoop *eventLoop, int fd) {
if (fd >= eventLoop->setsize) return NULL;
aeFileEvent *fe = &eventLoop->events[fd];
return fe->clientData;
}
/* =============================================================================
* SSL independent helper functions
* ==========================================================================*/
/**
* Converts SSL performance mode string to corresponding integer constant.
*/
int getSslPerformanceModeByName(char *name) {
if (!strcasecmp(name, "low-latency")) return SSL_PERFORMANCE_MODE_LOW_LATENCY;
else if (!strcasecmp(name, "high-throughput")) return SSL_PERFORMANCE_MODE_HIGH_THROUGHPUT;
else return -1;
}
/**
* Converts SSL performance mode integer to corresponding str
*/
const char *getSslPerformanceModeStr(int mode) {
if (mode == SSL_PERFORMANCE_MODE_LOW_LATENCY) return "low-latency";
else if (mode == SSL_PERFORMANCE_MODE_HIGH_THROUGHPUT) return "high-throughput";
else return "invalid input";
}
/**
* Initialize default values for SSL related global variables. It should be
* invoked at Redis startup to provide sane default values to SSL related
* variables
*/
void initSslConfigDefaults(ssl_t *ssl_config) {
ssl_config->enable_ssl = SSL_ENABLE_DEFAULT;
#ifdef BUILD_SSL
ssl_config->server_ssl_config = NULL;
ssl_config->cert_chain_and_key = NULL;
ssl_config->server_ssl_config_old = NULL;
ssl_config->cert_chain_and_key_old = NULL;
ssl_config->client_ssl_config = NULL;
#endif
ssl_config->ssl_certificate = NULL;
ssl_config->ssl_certificate_file = NULL;
ssl_config->ssl_certificate_private_key = NULL;
ssl_config->ssl_certificate_private_key_file = NULL;
ssl_config->ssl_dh_params = NULL;
ssl_config->ssl_dh_params_file = NULL;
ssl_config->ssl_cipher_prefs = SSL_CIPHER_PREFS_DEFAULT;
ssl_config->ssl_performance_mode = SSL_PERFORMANCE_MODE_DEFAULT;
ssl_config->root_ca_certs_path = NULL;
ssl_config->sslconn_with_cached_data = NULL;
ssl_config->repeated_reads_task_id = AE_ERR;
ssl_config->total_repeated_reads = 0;
ssl_config->max_repeated_read_list_length = 0;
ssl_config->expected_hostname = NULL;
ssl_config->certificate_not_after_date = NULL;
ssl_config->certificate_not_before_date = NULL;
ssl_config->connections_to_current_certificate = 0;
ssl_config->connections_to_previous_certificate = 0;
ssl_config->certificate_serial = 0;
}
/**
* This is a handler that does nothing, and is only used for non-ssl compilation
*/
void noopHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
(void)(el); (void)(fd); (void)(privdata); (void)(mask);
}
#ifdef BUILD_SSL
/* We need OpenSSL for certificate information only */
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
/* =============================================================================
* Private helper function prototypes
* ==========================================================================*/
static ssl_connection * getSslConnectionForFd(int fd);
/* SSL IO primitive functions */
static ssize_t sslRecv(int fd, void *buffer, size_t nbytes, s2n_blocked_status * blocked);
ssize_t sslRead(int fd, void *buffer, size_t nbytes);
ssize_t sslWrite(int fd, const void *buffer, size_t nbytes);
int sslClose(int fd);
void sslPing(int fd);
const char *sslStrerror(int err);
/* Functions for normal SSL negotiations */
static SslNegotiationStatus
sslNegotiate(aeEventLoop *el, int fd, void *privdata, aeFileProc *post_handshake_handler,
int post_handshake_handler_mask, aeFileProc *sourceProcedure, const char *sourceProcedureName);
static int updateEventHandlerForSslHandshake(s2n_blocked_status blocked, aeEventLoop *el, int fd, void *privdata,
aeFileProc *sourceProc);
static struct s2n_config *
initSslConfigForServer(const char *certificate, struct s2n_cert_chain_and_key *chain_and_key, const char *dhParams,
const char *cipherPrefs);
static struct s2n_config *
initSslConfigForClient(const char *cipher_prefs,
const char *certificate, const char *rootCACertificatesPath);
static struct s2n_config *
initSslConfig(int is_server, const char *certificate, struct s2n_cert_chain_and_key *chain_and_key, const char *dh_params,
const char *cipher_prefs, const char *rootCACertificatesPath);
uint8_t s2nVerifyHost(const char *hostName, size_t length, void *data);
/* Functions for SSL connections */
static void cleanupSslConnection(ssl_connection *conn, int fd, int shutdown);
static int shutdownSslConnection(ssl_connection *conn);
static int freeSslConnection(ssl_connection *conn);
/* Functions used for reading and parsing X509 certificates */
static int getCnameFromCertificate(const char *certificate, char *subject_name);
static int updateServerCertificateInformation(const char *certificate, char *not_before_date, char *not_after_date, long *serial);
static int convertASN1TimeToString(ASN1_TIME *timePointer, char* outputBuffer, size_t length);
static X509 *getX509FromCertificate(const char *certificate);
static void updateClientsUsingOldCertificate(void);
/* Functions for handling SSL negotiation after a socket BGSave */
static void waitForSlaveToLoadRdbAfterRdbTransfer(aeEventLoop *el, int fd, void *privdata, int mask);
static void sslNegotiateWithSlaveAfterSocketRdbTransfer(aeEventLoop *el, int fd, void *privdata, int mask);
static void sslNegotiateWithMasterAfterSocketRdbLoad(aeEventLoop *el, int fd, void *privdata, int mask);
static SslNegotiationStatus sslNegotiateWithoutPostHandshakeHandler(aeEventLoop *el, int fd, void *privdata, aeFileProc *sourceProcedure,
char *sourceProcedureName);
/* Functions for processing repeated reads */
static int processRepeatedReads(struct aeEventLoop *eventLoop, long long id, void *clientData);
static void addRepeatedRead(ssl_connection *conn);
static void removeRepeatedRead(ssl_connection *conn);
/* =============================================================================
* SSL configuration management
* ==========================================================================*/
/**
* Perform the same verification as open source s2n uses except don't use the connection name
* since it doesn't have the right endpoint in some cases for cluster bus.
*/
uint8_t s2nVerifyHost(const char *hostName, size_t length, void *data) {
UNUSED(data);
/* if present, match server_name of the connection using rules
* outlined in RFC6125 6.4. */
if (g_ssl_config.expected_hostname == NULL) {
return 0;
}
/* complete match */
if (strlen(g_ssl_config.expected_hostname) == length &&
strncasecmp(g_ssl_config.expected_hostname, hostName, length) == 0) {
return 1;
}
/* match 1 level of wildcard */
if (length > 2 && hostName[0] == '*' && hostName[1] == '.') {
const char *suffix = strchr(g_ssl_config.expected_hostname, '.');
if (suffix == NULL) {
return 0;
}
if (strlen(suffix) == length - 1 &&
strncasecmp(suffix, hostName + 1, length - 1) == 0) {
return 1;
}
}
return 0;
}
/**
* Initializes any global level resource required for SSL. This method
* should be invoked at startup time
*/
void initSsl(ssl_t *ssl) {
if (!isSSLEnabled()) return;
serverLog(LL_NOTICE, "Initializing SSL configuration");
setenv("S2N_ENABLE_CLIENT_MODE", "1", 1);
/* MLOCK is used to keep memory from being moved to SWAP. However, S2N can
* run into kernel limits for the number distinct mapped ranges
* associated to a process when a large number of clients are connected.
* Failed mlock calls will not free memory, so pages will not get unmapped
* until the engine is rebooted. In order to avoid this, we are
* unconditionally disabling MLOCK. */
setenv("S2N_DONT_MLOCK", "1", 1);
if (s2n_init() < 0) {
serverLog(LL_WARNING, "Error running s2n_init(): '%s'. Exiting",
s2n_strerror(s2n_errno, "EN"));
serverAssert(0);
}
/* Initialize Cert and chain structure */
ssl->cert_chain_and_key = s2n_cert_chain_and_key_new();
if (s2n_cert_chain_and_key_load_pem(ssl->cert_chain_and_key,
ssl->ssl_certificate, ssl->ssl_certificate_private_key) < 0) {
serverLog(LL_WARNING, "Error initializing server SSL configuration");
serverAssert(0);
}
/* Initialize configuration for Redis to act as a
* Server (client connections and cluster bus server) */
ssl->server_ssl_config = initSslConfigForServer(ssl->ssl_certificate,
ssl->cert_chain_and_key, ssl->ssl_dh_params, ssl->ssl_cipher_prefs);
if (!ssl->server_ssl_config) {
serverLog(LL_WARNING, "Error initializing server SSL configuration");
serverAssert(0);
}
/* Initialize configuration for Redis to act as a
* Client (replication connections and cluster bus clients) */
ssl->client_ssl_config = initSslConfigForClient(ssl->ssl_cipher_prefs,
ssl->ssl_certificate, ssl->root_ca_certs_path);
if (!ssl->client_ssl_config) {
serverLog(LL_WARNING, "Error initializing client SSL configuration");
serverAssert(0);
}
/* The expected hostname from the certificate to use as part of hostname validation */
ssl->expected_hostname = (char*)malloc(CERT_CNAME_MAX_LENGTH);
if (getCnameFromCertificate(ssl->ssl_certificate, ssl->expected_hostname) == C_ERR) {
serverLog(LL_WARNING, "Error while discovering expected hostname from certificate file");
serverAssert(0);
}
/* Allocate space for not before and not after dates */
ssl->certificate_not_after_date = (char*)malloc(CERT_DATE_MAX_LENGTH);
ssl->certificate_not_before_date = (char*)malloc(CERT_DATE_MAX_LENGTH);
if (updateServerCertificateInformation(ssl->ssl_certificate,
ssl->certificate_not_before_date, ssl->certificate_not_after_date,
&g_ssl_config.certificate_serial) == C_ERR) {
serverLog(LL_WARNING, "Error while discovering not_after and not_before from certificate file");
serverAssert(0);
}
ssl->sslconn_with_cached_data = listCreate();
}
static struct s2n_config *
initSslConfigForServer(const char *certificate, struct s2n_cert_chain_and_key *chain_and_key, const char *dhParams,
const char *cipherPrefs) {
return initSslConfig(1, certificate, chain_and_key, dhParams, cipherPrefs, NULL);
}
static struct s2n_config *
initSslConfigForClient(const char *cipher_prefs,
const char *certificate, const char *rootCACertificatesPath) {
return initSslConfig(0, certificate, NULL, NULL, cipher_prefs, rootCACertificatesPath);
}
static struct s2n_config *
initSslConfig(int is_server, const char *certificate, struct s2n_cert_chain_and_key *chain_and_key, const char *dh_params,
const char *cipher_prefs, const char *rootCACertificatesPath) {
serverLog(LL_DEBUG, "Initializing %s SSL configuration", is_server ? "Server" : "Client");
struct s2n_config *ssl_config = s2n_config_new();
if (!ssl_config) {
serverLog(LL_WARNING, "Error getting new s2n config: '%s'.", s2n_strerror(s2n_errno, "EN"));
return NULL;
}
if (is_server && s2n_config_add_cert_chain_and_key_to_store(ssl_config,
chain_and_key) < 0) {
serverLog(LL_WARNING, "Error adding certificate/key to s2n config: '%s'.",
s2n_strerror(s2n_errno, "EN"));
goto config_error;
}
if (is_server && s2n_config_add_dhparams(ssl_config, dh_params) < 0) {
serverLog(LL_WARNING, "Error adding DH parameters to s2n config: '%s'.",
s2n_strerror(s2n_errno, "EN"));
goto config_error;
}
/* Load the root ca certificate */
if (!is_server && s2n_config_set_verification_ca_location(ssl_config,
NULL, rootCACertificatesPath) < 0) {
serverLog(LL_WARNING, "Error while loading CA certificates into s2n: '%s'.", s2n_strerror(s2n_errno, "EN"));
goto config_error;
}
/**
* Load the intermediate nodes from the provided certificate file, this will also load the leaf nodes
* but they will be unused.
*/
if (!is_server && s2n_config_add_pem_to_trust_store(ssl_config,
certificate) < 0) {
serverLog(LL_WARNING, "Error while loading SSL certificate into s2n: '%s'.", s2n_strerror(s2n_errno, "EN"));
goto config_error;
}
if (!is_server && s2n_config_set_verify_host_callback(ssl_config,
s2nVerifyHost, NULL) < 0) {
serverLog(LL_WARNING, "Error while setting host verify callback: '%s'.", s2n_strerror(s2n_errno, "EN"));
goto config_error;
}
if (s2n_config_set_cipher_preferences(ssl_config, cipher_prefs) < 0) {
serverLog(LL_WARNING, "Error setting cipher prefs on s2n config: '%s'.",
s2n_strerror(s2n_errno, "EN"));
goto config_error;
}
return ssl_config;
config_error:
if (s2n_config_free(ssl_config) < 0)
serverLog(LL_WARNING, "Error freeing server SSL configuration");
return NULL;
}
/**
* Cleans any global level resources used by SSL. This method
* should be invoked at shutdown time
*/
void cleanupSsl(ssl_t *ssl) {
if (!isSSLEnabled()) return;
if (s2n_cleanup() < 0)
serverLog(LL_WARNING, "Error cleaning up SSL resources: %s", s2n_strerror(s2n_errno, "EN"));
if (s2n_config_free(ssl->server_ssl_config) < 0)
serverLog(LL_WARNING, "Error freeing server SSL config: %s", s2n_strerror(s2n_errno, "EN"));
if (s2n_config_free(ssl->client_ssl_config) < 0)
serverLog(LL_WARNING, "Error freeing client SSL config: %s", s2n_strerror(s2n_errno, "EN"));
if (s2n_cert_chain_and_key_free(ssl->cert_chain_and_key) < 0)
serverLog(LL_WARNING, "Error freeing the server chain and key: %s", s2n_strerror(s2n_errno, "EN"));
if (ssl->server_ssl_config_old) {
if (s2n_config_free(ssl->server_ssl_config_old) < 0)
serverLog(LL_WARNING, "Error freeing the old server SSL config: %s", s2n_strerror(s2n_errno, "EN"));
if (s2n_cert_chain_and_key_free(ssl->cert_chain_and_key_old) < 0)
serverLog(LL_WARNING, "Error freeing the old server cert chain and key: %s", s2n_strerror(s2n_errno, "EN"));
}
listRelease(ssl->sslconn_with_cached_data);
free(ssl->expected_hostname);
free(ssl->certificate_not_after_date);
free(ssl->certificate_not_before_date);
}
#ifdef UNSUPPORTED
/**
* Disconnect any clients that are still using old certificate and mark all
* of the connections as using the older connection so that the count of
* connections is accurate.
*/
static void updateClientsUsingOldCertificate(void) {
if (!isSSLEnabled()) return;
listIter li;
listRewind(server.clients, &li);
listNode *ln;
client *client;
if (g_ssl_config.server_ssl_config_old != NULL) {
serverLog(LL_VERBOSE, "Disconnecting clients using very old certificates");
unsigned int clients_disconnected = 0;
while ((ln = listNext(&li)) != NULL) {
client = listNodeValue(ln);
ssl_connection *ssl_conn = getSslConnectionForFd(client->fd);
if (ssl_conn->connection_flags & OLD_CERTIFICATE_FLAG) {
if (server.current_client == client) {
client->flags |= CLIENT_CLOSE_AFTER_REPLY;
} else {
freeClient(client);
}
clients_disconnected++;
}else{
/* Mark the connection as connected to the old certificate */
ssl_conn->connection_flags |= OLD_CERTIFICATE_FLAG;
}
}
serverLog(LL_WARNING, "Disconnected %d clients using very old certificate", clients_disconnected);
} else {
/* If there is no old config, just update the connection properties */
while ((ln = listNext(&li)) != NULL) {
client = listNodeValue(ln);
ssl_connection *ssl_conn = getSslConnectionForFd(client->fd);
ssl_conn->connection_flags |= OLD_CERTIFICATE_FLAG;
}
}
}
/**
* Update the certificate/private key pair used by SSL. This method can be used to
* renew the expiring certificate without bouncing Redis
*/
int renewCertificate(char *new_certificate, char *new_private_key,
char *new_certificate_filename, char *new_private_key_filename) {
serverLog(LL_NOTICE, "Initializing SSL configuration for new certificate");
struct s2n_cert_chain_and_key *new_chain_and_key = NULL;
struct s2n_config *new_config = NULL;
/* Initialize Cert and chain structure */
new_chain_and_key = s2n_cert_chain_and_key_new();
if (s2n_cert_chain_and_key_load_pem(new_chain_and_key,
new_certificate, new_private_key) < 0) {
serverLog(LL_WARNING, "Error initializing SSL key and chain");
goto renew_error;
}
new_config = initSslConfigForServer(new_certificate, new_chain_and_key,
g_ssl_config.ssl_dh_params, g_ssl_config.ssl_cipher_prefs);
if (new_config == NULL) {
serverLog(LL_DEBUG, "Error creating SSL configuration using new certificate");
goto renew_error;
}
char *newNotBeforeDate = malloc(CERT_DATE_MAX_LENGTH);
char *newNotAfterDate = malloc(CERT_DATE_MAX_LENGTH);
long newSerial = 0;
/* Update the not before and not after date provided in info */
if (updateServerCertificateInformation(new_certificate, newNotBeforeDate,
newNotAfterDate, &newSerial) != C_OK) {
serverLog(LL_DEBUG, "Failed to read not_before and not_after date from new certificate");
free(newNotBeforeDate);
free(newNotAfterDate);
goto renew_error;
}
/* After we have validated that new cert is valid, disconnect any
* clients using the oldest certificate. We don't want to have more that
* 2 certificates in use at a time. We proactively disconnect any
*clients using oldest certificate to stay within 2 certificate limit */
updateClientsUsingOldCertificate();
if (g_ssl_config.server_ssl_config_old) {
/* Now that no client are using the old config, free it */
if (s2n_config_free(g_ssl_config.server_ssl_config_old) < 0)
serverLog(LL_WARNING, "Error freeing the old server SSL config: %s", s2n_strerror(s2n_errno, "EN"));
g_ssl_config.server_ssl_config_old = g_ssl_config.server_ssl_config;
if (s2n_cert_chain_and_key_free(g_ssl_config.cert_chain_and_key_old) < 0)
serverLog(LL_WARNING, "Error freeing the old SSL cert chain and key: %s", s2n_strerror(s2n_errno, "EN"));
g_ssl_config.cert_chain_and_key_old = g_ssl_config.cert_chain_and_key;
}
/* start using new configuration. Any new connections
* will start using new certificate from this point onwards */
g_ssl_config.server_ssl_config = new_config;
g_ssl_config.cert_chain_and_key = new_chain_and_key;
/*free the memory used by old stuff */
free((void *) g_ssl_config.ssl_certificate);
free((void *) g_ssl_config.ssl_certificate_file);
free((void *) g_ssl_config.ssl_certificate_private_key);
free((void *) g_ssl_config.ssl_certificate_private_key_file);
free((void *) g_ssl_config.certificate_not_before_date);
free((void *) g_ssl_config.certificate_not_after_date);
/*save the references to the new stuff */
g_ssl_config.ssl_certificate = new_certificate;
g_ssl_config.ssl_certificate_file = new_certificate_filename;
g_ssl_config.ssl_certificate_private_key = new_private_key;
g_ssl_config.ssl_certificate_private_key_file = new_private_key_filename;
g_ssl_config.certificate_not_before_date = newNotBeforeDate;
g_ssl_config.certificate_not_after_date = newNotAfterDate;
g_ssl_config.certificate_serial = newSerial;
/* Update the connection count for redis info */
g_ssl_config.connections_to_previous_certificate = g_ssl_config.connections_to_current_certificate;
g_ssl_config.connections_to_current_certificate = 0;
serverLog(LL_NOTICE, "Successfully renewed SSL certificate");
return C_OK;
renew_error:
if (new_chain_and_key && s2n_cert_chain_and_key_free(new_chain_and_key) < 0)
serverLog(LL_WARNING, "Error freeing the new server SSL chain and key on renew: %s", s2n_strerror(s2n_errno, "EN"));
if (new_config && s2n_config_free(new_config) < 0)
serverLog(LL_WARNING, "Error freeing the new server SSL config on renew: %s", s2n_strerror(s2n_errno, "EN"));
return C_ERR;
}
#endif
/**
* Return an x509 object from a certificate string.
*/
X509 *getX509FromCertificate(const char *certificate) {
BIO *bio = NULL;
/* Create a read-only BIO backed by the supplied memory buffer */
bio = BIO_new_mem_buf((void *) certificate, -1);
if (!bio) {
serverLog(LL_WARNING, "Error allocating BIO buffer");
return NULL;
}
X509 *x509_cert = NULL;
/*Read a certificate in PEM format from a BIO */
if (!(x509_cert = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
BIO_free(bio);
serverLog(LL_DEBUG, "Error converting certificate from PEM to X509 format");
return NULL;
}
/* Cleanup. bio is no longer needed */
BIO_free(bio);
return x509_cert;
}
/**
* Extract the Cname from a certificate to be used later in hostname validation. We need this
* because we want to verify the hostname we are connecting to even when we are using the IP address.
*/
static int getCnameFromCertificate(const char *certificate, char *subject_name) {
X509 *x509_cert = getX509FromCertificate(certificate);
if (x509_cert == NULL) {
return C_ERR;
}
if (X509_NAME_get_text_by_NID(X509_get_subject_name(x509_cert), NID_commonName, subject_name,
CERT_CNAME_MAX_LENGTH) == -1) {
X509_free(x509_cert);
serverLog(LL_DEBUG, "Could not find a CN entry in certificate");
return C_ERR;
}
X509_free(x509_cert);
serverLog(LL_DEBUG, "Successfully extracted subject name from certificate. Subject Name: %s", subject_name);
return C_OK;
}
/**
* Convert an ANSI string to a C String and write it to the output buffer.
*/
int convertASN1TimeToString(ASN1_TIME *timePointer, char* outputBuffer, size_t length) {
BIO *buffer = BIO_new(BIO_s_mem());
if (ASN1_TIME_print(buffer, timePointer) <= 0) {
BIO_free(buffer);
return C_ERR;
}
if (BIO_gets(buffer, outputBuffer, length) <= 0) {
BIO_free(buffer);
return C_ERR;
}
BIO_free(buffer);
return C_OK;
}
/**
* Read the provided certificate file and populate the not_after and not_before dates. The values returned
* are not guaranteed to be right unless C_OK is returned.
*/
int updateServerCertificateInformation(const char *certificate, char *not_before_date, char *not_after_date, long *serial) {
X509 *x509_cert = getX509FromCertificate(certificate);
if (x509_cert == NULL) {
return C_ERR;
}
if (convertASN1TimeToString(X509_get_notBefore(x509_cert), not_before_date, CERT_DATE_MAX_LENGTH) == C_ERR) {
serverLog(LL_DEBUG, "Failed to extract not before date from certificate.");
X509_free(x509_cert);
return C_ERR;
}
serverLog(LL_DEBUG, "Successfully extracted not before date: %s from certificate.", not_before_date);
if (convertASN1TimeToString(X509_get_notAfter(x509_cert), not_after_date, CERT_DATE_MAX_LENGTH) == C_ERR) {
serverLog(LL_DEBUG, "Failed to extract not after date from provided certificate.");
X509_free(x509_cert);
return C_ERR;
}
serverLog(LL_DEBUG, "Successfully extracted not after date: %s from certificate.", not_after_date);
long newSerial = ASN1_INTEGER_get(X509_get_serialNumber(x509_cert));
if (newSerial == 0) {
serverLog(LL_DEBUG, "Failed to extract not before date from provided certificate.");
X509_free(x509_cert);
return C_ERR;
}
*serial = newSerial;
serverLog(LL_DEBUG, "Successfully extracted serial: %lx from certificate.", newSerial);
X509_free(x509_cert);
return C_OK;
}
/* =============================================================================
* SSL IO primitive functions
* ==========================================================================*/
/*
* SSL compatible wrapper around recv that is used as an abstraction for sslRead.
*/
static ssize_t sslRecv(int fd, void *buffer, size_t nbytes, s2n_blocked_status * blocked) {
s2n_errno = S2N_ERR_T_OK;
errno = 0;
ssl_connection *ssl_conn = fd_to_sslconn(fd);
ssize_t bytesread = s2n_recv(ssl_conn->s2nconn, buffer, nbytes, blocked);
if (bytesread < 0 && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED) {
/* No data was returned because the socket did not have a full frame.
* We can only continue when the socket is readable again. set errno as
* well in case IO blocked. This is so that calling code treats
* it like regular blocking IO and does not has to do any special
* logic for SSL based IO */
errno = EAGAIN;
}
return bytesread;
}
/**
* SSL compatible read IO method. This method sets errno
* so that is compatible with a normal read syscall.
*/
ssize_t sslRead(int fd, void *buffer, size_t nbytes) {
s2n_blocked_status blocked;
ssize_t bytesread = sslRecv(fd, buffer, nbytes, &blocked);
ssl_connection *ssl_conn = fd_to_sslconn(fd);
if (bytesread > 0 && blocked == S2N_BLOCKED_ON_READ) {
/* Data was returned, but we didn't consume an entire frame,
* so signal that we need to repeat the event handler. */
addRepeatedRead(ssl_conn);
} else {
/* Either the entire frame was consumed, or nothing was
* returned because we were blocked on a socket read. */
removeRepeatedRead(ssl_conn);
}
return bytesread;
}
/**
* Send a newline ping on a socket used for other purposes. This is necessary
* instead of using sslWrite for a ping when SSL is enabled because S2N
* assumes that a single stream of data is sent. If a newline byte is sent
* as its own SSL frame, it is no longer atomic, and can be partially sent.
* S2N assumes the caller will always retry the call until success, whereas
* Redis just performs best-effort pings. Therefore we hijack the sending
* process and ensure that pings are fully flushed when sent.
*
* While negotiation is in progress sending data here will cause the
* negotiation to break, so that needs to be handled by the caller.
*/
void sslPing(int fd) {
ssize_t byteswritten = sslWrite(fd, "\n", 1);
if (byteswritten < 0 && errno == EAGAIN) {
/* A newline ping request is in progress. We need to make sure
* this request succeeds before we issue another independent
* request. */
ssl_connection *ssl_conn = getSslConnectionForFd(fd);
ssl_conn->connection_flags |= NEWLINE_PING_IN_PROGRESS_FLAG;
}
}
/**
* SSL compatible write IO method. This method sets errno
* so that is compatible with a normal write syscall.
*/
ssize_t sslWrite(int fd, const void *buffer, size_t nbytes) {
s2n_errno = S2N_ERR_T_OK;
errno = 0;
ssl_connection *ssl_conn = getSslConnectionForFd(fd);
s2n_blocked_status blocked;
if (ssl_conn->connection_flags & NEWLINE_PING_IN_PROGRESS_FLAG) {
/* We previously called sslPing and it didn't fully complete the request!
* We need to flush out that request before continuing since s2n is stateful. */
ssize_t r = s2n_send(ssl_conn->s2nconn, "\n", 1, &blocked);
if (r < 0) {
/* Still didn't succeed */
if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED) {
errno = EAGAIN;
}
return r;
}
/* Success! Continue to our actual request. */
ssl_conn->connection_flags &= ~NEWLINE_PING_IN_PROGRESS_FLAG;
}
ssize_t r = s2n_send(ssl_conn->s2nconn, buffer, nbytes, &blocked);
/* Set errno as well in case IO blocked. This is so that calling code treats
* it like regular blocking IO and does not has to do any special logic for SSL based IO */
if (r < 0 && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED) errno = EAGAIN;
return r;
}
/**
* SSL compatible close IO method.
*/
int sslClose(int fd) {
++fInSsl;
cleanupSslConnectionForFd(fd);
int rval = close(fd);
--fInSsl;
return rval;
}
/**
* SSL compatible IO error string method. It checks the last s2n_errno and
* prints out its corresponding error, otherwise it prints the error
* associated with the errno that is passed in.
*/
const char *sslStrerror(int err) {
if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_IO) {
/* S2N_ERR_T_IO => underlying I/O operation failed, check system errno
* therefore in this case, returning System IO error string */
return (strerror)(err);
} else {
return s2n_strerror(s2n_errno, "EN");
}
}
/* =============================================================================
* SSL connection management
* ==========================================================================*/
/* Convenience function to fetch an sslConnection from a fd and make sure it exists */
ssl_connection *getSslConnectionForFd(int fd) {
serverAssert(isSSLFd(fd));
return fd_to_sslconn(fd);
}
/**
* Creates and initializes an SSL connection. It performs following critical functions on a connection
* so that it is usable by Redis
* - create a new connection in Server or Client mode
* - Associates appropriate configuration with the connection
* - Associates appropriate socket file descriptor with the connection
* - Set a performance mode on the connection
* - Create an entry for Socket FD to SSL connection mapping
*/
ssl_connection *
initSslConnection(sslMode mode, int fd, int ssl_performance_mode, char *masterhost) {
s2n_mode connection_mode;
struct s2n_config *config;
if (mode == SSL_SERVER) {
connection_mode = S2N_SERVER;
config = g_ssl_config.server_ssl_config;
} else if (mode == SSL_CLIENT) {
connection_mode = S2N_CLIENT;
config = g_ssl_config.client_ssl_config;
} else {
return NULL;
}
ssl_connection *sslconn = (ssl_connection*)malloc(sizeof(ssl_connection));
if (!sslconn) {
serverLog(LL_WARNING, "Error creating new ssl connection.");
return NULL;
}
sslconn->connection_flags = 0;
sslconn->fd = fd;
sslconn->cached_data_node = NULL;
/* create a new connection in Server or Client mode */
sslconn->s2nconn = s2n_connection_new(connection_mode);
if (!sslconn->s2nconn) {
serverLog(LL_WARNING, "Error creating new s2n connection. Error: '%s'", s2n_strerror(s2n_errno, "EN"));
goto error;
}
/* Associates appropriate configuration with the connection */
if (s2n_connection_set_config(sslconn->s2nconn, config) < 0) {
serverLog(LL_WARNING, "Error setting configuration on s2n connection. Error: '%s'",
s2n_strerror(s2n_errno, "EN"));
goto error;
}
/* Associates appropriate socket file descriptor with the connection */
if (s2n_connection_set_fd(sslconn->s2nconn, fd) < 0) {
serverLog(LL_WARNING, "Error setting socket file descriptor: %d on s2n connection. Error:'%s'", fd,
s2n_strerror(s2n_errno, "EN"));
goto error;
}
/* disable blinding. Blinding could lead to Redis sleeping upto to 10s which is not desirable in a
* single threaded application */
if (s2n_connection_set_blinding(sslconn->s2nconn, S2N_SELF_SERVICE_BLINDING) < 0) {
serverLog(LL_WARNING, "Error setting blinding mode: S2N_SELF_SERVICE_BLINDING on s2n connection. Error:'%s'",
s2n_strerror(s2n_errno, "EN"));
goto error;
}
/* Set a performance mode on the connection */
switch (ssl_performance_mode) {
case SSL_PERFORMANCE_MODE_HIGH_THROUGHPUT:
if (s2n_connection_prefer_throughput(sslconn->s2nconn) < 0) {
serverLog(LL_WARNING, "Error setting performance mode of high throughput on SSL connection");
goto error;
}
break;
case SSL_PERFORMANCE_MODE_LOW_LATENCY:
if (s2n_connection_prefer_low_latency(sslconn->s2nconn) < 0) {
serverLog(LL_WARNING, "Error setting performance mode of low latency on SSL connection");
goto error;
}
break;
default:
serverLog(LL_DEBUG, "Invalid SSL performance mode: %d", ssl_performance_mode);
goto error;
}
/*Set master host on the ssl connection */
if (connection_mode == S2N_CLIENT && masterhost != NULL && s2n_set_server_name(sslconn->s2nconn, masterhost) < 0) {
serverLog(LL_WARNING, "Error setting server name on s2n connection: '%s'", s2n_strerror(s2n_errno, "EN"));
goto error;
}
/* Create an entry for Socket FD to SSL connection mapping */
set_sslconn(fd, sslconn);
serverLog(LL_DEBUG, "SSL Connection setup successfully for fd %d", fd);
return sslconn;
error:
freeSslConnection(sslconn);
return NULL;
}
/**
* Performs SSL related setup for a client. It includes creating and initializing an SSL connection,
* and registering an event handler for SSL negotiation
*/
int setupSslOnClient(client *c, int fd, int ssl_performance_mode) {
struct ssl_connection *ssl_conn = initSslConnection(SSL_SERVER, fd, ssl_performance_mode, NULL);
if (!ssl_conn) {
serverLog(LL_WARNING, "Error getting new s2n connection for client with fd: %d, Error: '%s'", fd,
s2n_strerror(s2n_errno, "EN"));
return C_ERR;
}
/* Increment the number of connections associated with the latest certificate */
g_ssl_config.connections_to_current_certificate++;
ssl_conn->connection_flags |= CLIENT_CONNECTION_FLAG;
if (aeCreateFileEvent(server.el, fd, AE_READABLE | AE_WRITABLE,
sslNegotiateWithClient, c) == AE_ERR) {
cleanupSslConnectionForFd(fd);
return C_ERR;
}
return C_OK;
}
/**
* shuts down the SSL connection. It effectively sends
* a SHUTDOWN tls alert to the peer (as a SSL best practice before
* we close socket)
*/
int shutdownSslConnection(ssl_connection *conn) {
serverLog(LL_DEBUG, "Shutting down SSL conn");
if (conn != NULL && conn->s2nconn != NULL) {
s2n_blocked_status blocked;
s2n_shutdown(conn->s2nconn, &blocked);
}
return C_OK;
}
/**
* This method should be used for Cleanup a connection. It shuts down the
* SSL connection (sends a SHUTDOWN TLS alert) for secure shutdown, frees
* the memory consumed by connection and deletes the mapping from Socket FD
* to this connection
*/
void cleanupSslConnectionForFd(int fd) {
cleanupSslConnection(getSslConnectionForFd(fd), fd, 1);
}
/**
* This method should be used for cleaning up an SSL connection when shutdown
* is not desired. This is currently used when re-negotiating an existing connection
* so there are no race conditions with ssl alerts and negotiating.
*/
void cleanupSslConnectionForFdWithoutShutdown(int fd) {
cleanupSslConnection(getSslConnectionForFd(fd), fd, 0);
}
/**
* This method should be used to cleanup a connection. It will shutdown the
* SSL connection (sends a SHUTDOWN TLS alert) for secure shutdown, free
* the memory consumed by connection and delete the mapping from Socket FD
* to this connection
*/
void cleanupSslConnection(struct ssl_connection *conn, int fd, int shutdown) {
serverLog(LL_DEBUG, "Cleaning up SSL conn for socket fd: %d", fd);
if (conn->connection_flags & CLIENT_CONNECTION_FLAG) {
if (conn->connection_flags & OLD_CERTIFICATE_FLAG) {
g_ssl_config.connections_to_previous_certificate--;
} else {
g_ssl_config.connections_to_current_certificate--;
}
}
/* Don't shutdown if we haven't even initialized anything */
if (shutdown && s2n_connection_get_client_hello(conn->s2nconn) != NULL) {
shutdownSslConnection(conn);
}
freeSslConnection(conn);
serverLog(LL_DEBUG, "Deleting fd: %d from fd_to_sslconn map", fd);
set_sslconn(fd, NULL);
}
/**
* Frees the memory used by ssl connection. Returns C_ERR
* if the underlying S2N connection could not be freed successfully
* but always frees application memory.
*/
int freeSslConnection(ssl_connection *conn) {
serverLog(LL_DEBUG, "Freeing up SSL conn");
int ret = C_OK;
if (conn != NULL) {
if (conn->s2nconn != NULL) {
/*
* Just doing s2n_connection_free is not sufficient in production.
* s2n_connection_wipe calls s2n_connection_wipe_io which frees
* some memory allocated. Just doing s2n_connection_free
* was causing a memory leak reported by valgrind and after a while, redis
* would stop accepting new connections
*/
if (s2n_connection_wipe(conn->s2nconn) < 0) {
serverLog(LL_WARNING, "Error wiping connection: '%s'", s2n_strerror(s2n_errno, "EN"));
}
if (s2n_connection_free(conn->s2nconn) < 0) {
serverLog(LL_WARNING, "Error freeing connection: '%s'", s2n_strerror(s2n_errno, "EN"));
ret = C_ERR;
}
}
if (conn->cached_data_node != NULL) {
removeRepeatedRead(conn);
}
free(conn);
}
return ret;
}