-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathssl.c
3062 lines (2789 loc) · 112 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
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* SSL-related stuff for slapd */
#include <stdio.h>
#include <libgen.h>
#include <sys/param.h>
#include <ssl.h>
#include <nss.h>
#include <keyhi.h>
#include <sslproto.h>
#include "secmod.h"
#include <string.h>
#include <errno.h>
#define NEED_TOK_PBE /* defines tokPBE and ptokPBE - see slap.h */
#include "slap.h"
#include <unistd.h>
#include "svrcore.h"
#include "fe.h"
#include "certdb.h"
/* For IRIX... */
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
/******************************************************************************
* Default SSL Version Rule
* Old SSL version attributes:
* nsSSL3: off -- nsSSL3 == SSL_LIBRARY_VERSION_3_0
* nsTLS1: on -- nsTLS1 == SSL_LIBRARY_VERSION_TLS_1_2 and greater
* Note: TLS1.0 is defined in RFC2246, which is close to SSL 3.0.
* New SSL version attributes:
* sslVersionMin: TLS1.2
* sslVersionMax: max ssl version supported by NSS
******************************************************************************/
#define DEFVERSION "TLS1.2"
extern char *slapd_SSL3ciphers;
extern symbol_t supported_ciphers[];
static SSLVersionRange defaultNSSVersions;
static SSLVersionRange supportedNSSVersions;
static SSLVersionRange slapdNSSVersions;
/* dongle_file_name is set in slapd_nss_init when we set the path for the
key, cert, and secmod files - the dongle file must be in the same directory
and use the same naming scheme
*/
static char *dongle_file_name = NULL;
static int _security_library_initialized = 0;
static int _ssl_listener_initialized = 0;
static int _nss_initialized = 0;
/* Our name for the internal token, must match PKCS-11 config data below */
static char *internalTokenName = "Internal (Software) Token";
static int stimeout;
static char *ciphers = NULL;
static char *configDN = "cn=encryption,cn=config";
/* The paths of extracted key and certs if any. */
static char *key_extract_file = NULL;
static char *cert_extract_file = NULL;
/* Copied from libadmin/libadmin.h public/nsapi.h */
#define SERVER_KEY_NAME "Server-Key"
#define MAGNUS_ERROR_LEN 1024
#define LOG_WARN 0
#define LOG_FAILURE 3
#define LOG_MSG 4
#define FILE_PATHSEP '/'
/* ----------------------- Multiple cipher support ------------------------ */
/* cipher set flags */
#define CIPHER_SET_NONE 0x0
#define CIPHER_SET_ALL 0x1
#define CIPHER_SET_DEFAULT 0x2
#define CIPHER_SET_DEFAULTWEAKCIPHER 0x10 /* allowWeakCipher is not set in cn=encryption */
#define CIPHER_SET_ALLOWWEAKCIPHER 0x20 /* allowWeakCipher is on */
#define CIPHER_SET_DISALLOWWEAKCIPHER 0x40 /* allowWeakCipher is off */
#define CIPHER_SET_DEFAULTWEAKDHPARAM 0x100 /* allowWeakDhParam is not set in cn=encryption */
#define CIPHER_SET_ALLOWWEAKDHPARAM 0x200 /* allowWeakDhParam is on */
#define CIPHER_SET_DISALLOWWEAKDHPARAM 0x400 /* allowWeakDhParam is off */
#define CIPHER_SET_ISDEFAULT(flag) \
(((flag)&CIPHER_SET_DEFAULT) ? PR_TRUE : PR_FALSE)
#define CIPHER_SET_ISALL(flag) \
(((flag)&CIPHER_SET_ALL) ? PR_TRUE : PR_FALSE)
#define ALLOWWEAK_ISDEFAULT(flag) \
(((flag)&CIPHER_SET_DEFAULTWEAKCIPHER) ? PR_TRUE : PR_FALSE)
#define ALLOWWEAK_ISON(flag) \
(((flag)&CIPHER_SET_ALLOWWEAKCIPHER) ? PR_TRUE : PR_FALSE)
#define ALLOWWEAK_ISOFF(flag) \
(((flag)&CIPHER_SET_DISALLOWWEAKCIPHER) ? PR_TRUE : PR_FALSE)
/*
* If ISALL or ISDEFAULT, allowWeakCipher is true only if CIPHER_SET_ALLOWWEAKCIPHER.
* Otherwise (user specified cipher list), allowWeakCipher is true
* if CIPHER_SET_ALLOWWEAKCIPHER or CIPHER_SET_DEFAULTWEAKCIPHER.
*/
#define CIPHER_SET_ALLOWSWEAKCIPHER(flag) \
((CIPHER_SET_ISDEFAULT(flag) | CIPHER_SET_ISALL(flag)) ? (ALLOWWEAK_ISON(flag) ? PR_TRUE : PR_FALSE) : (!ALLOWWEAK_ISOFF(flag) ? PR_TRUE : PR_FALSE))
#define CIPHER_SET_DISABLE_ALLOWSWEAKCIPHER(flag) \
((flag) & ~CIPHER_SET_ALLOWWEAKCIPHER)
/* flags */
#define CIPHER_IS_DEFAULT 0x1
#define CIPHER_MUST_BE_DISABLED 0x2
#define CIPHER_IS_WEAK 0x4
#define CIPHER_IS_DEPRECATED 0x8
static int allowweakdhparam = CIPHER_SET_DEFAULTWEAKDHPARAM;
static char **cipher_names = NULL;
static char **enabled_cipher_names = NULL;
typedef struct
{
char *name;
int num;
int flags;
} cipherstruct;
static cipherstruct *_conf_ciphers = NULL;
static void _conf_init_ciphers(void);
/* E.g., "SSL3", "TLS1.2", "Unknown SSL version: 0x0" */
#define VERSION_STR_LENGTH 64
/* Supported SSL versions */
/* nsSSL2: on -- we don't allow this any more. */
PRBool enableSSL2 = PR_FALSE;
/*
* nsSSL3: on -- disable SSLv3 by default.
* Corresonding to SSL_LIBRARY_VERSION_3_0
*/
PRBool enableSSL3 = PR_FALSE;
/*
* nsTLS1: on -- enable TLS1 by default.
* Corresonding to SSL_LIBRARY_VERSION_TLS_1_2 and greater.
*/
PRBool enableTLS1 = PR_TRUE;
/*
* OpenLDAP client library with OpenSSL (ticket 47536)
*/
#define PEMEXT ".pem"
/* CA cert pem file */
static char *CACertPemFile = NULL;
/* helper functions for openldap update. */
static int slapd_extract_cert(Slapi_Entry *entry, int isCA);
static int slapd_extract_key(Slapi_Entry *entry, char *token, PK11SlotInfo *slot);
static void entrySetValue(Slapi_DN *sdn, char *type, char *value);
static char *gen_pem_path(char *filename);
static void
slapd_SSL_report(int degree, char *fmt, va_list args)
{
char buf[2048];
char *msg = NULL;
int sev;
if (degree == LOG_FAILURE) {
sev = SLAPI_LOG_ERR;
msg = "failure";
} else if (degree == LOG_WARN) {
sev = SLAPI_LOG_WARNING;
msg = "alert";
} else {
sev = SLAPI_LOG_INFO;
msg = "info";
}
PR_vsnprintf(buf, sizeof(buf), fmt, args);
slapi_log_err(sev, "Security Initialization", "SSL %s: %s\n", msg, buf);
}
void
slapd_SSL_error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
slapd_SSL_report(LOG_FAILURE, fmt, args);
va_end(args);
}
void
slapd_SSL_warn(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
slapd_SSL_report(LOG_WARN, fmt, args);
va_end(args);
}
void
slapd_SSL_info(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
slapd_SSL_report(LOG_MSG, fmt, args);
va_end(args);
}
char **
getSupportedCiphers(void)
{
SSLCipherSuiteInfo info;
char *sep = "::";
int number_of_ciphers = SSL_NumImplementedCiphers;
int idx = 0;
PRBool isFIPS = slapd_pk11_isFIPS();
_conf_init_ciphers();
if ((cipher_names == NULL) && (_conf_ciphers)) {
cipher_names = (char **)slapi_ch_calloc((number_of_ciphers + 1), sizeof(char *));
for (size_t i = 0; _conf_ciphers[i].name != NULL; i++) {
SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[i].num, &info, sizeof(info));
/* only support FIPS approved ciphers in FIPS mode */
if (!isFIPS || info.isFIPS) {
cipher_names[idx++] = slapi_ch_smprintf("%s%s%s%s%s%s%d",
_conf_ciphers[i].name, sep,
info.symCipherName, sep,
info.macAlgorithmName, sep,
info.symKeyBits);
}
}
cipher_names[idx] = NULL;
}
return cipher_names;
}
int
get_allow_weak_dh_param(Slapi_Entry *e)
{
/* Check if the user wants weak params */
int allow = CIPHER_SET_DEFAULTWEAKDHPARAM;
char *val;
val = slapi_entry_attr_get_charptr(e, "allowWeakDHParam");
if (val) {
if (!PL_strcasecmp(val, "off") || !PL_strcasecmp(val, "false") ||
!PL_strcmp(val, "0") || !PL_strcasecmp(val, "no")) {
allow = CIPHER_SET_DISALLOWWEAKDHPARAM;
} else if (!PL_strcasecmp(val, "on") || !PL_strcasecmp(val, "true") ||
!PL_strcmp(val, "1") || !PL_strcasecmp(val, "yes")) {
allow = CIPHER_SET_ALLOWWEAKDHPARAM;
slapd_SSL_warn("The value of allowWeakDHParam is set to %s. THIS EXPOSES YOU TO CVE-2015-4000.", val);
} else {
slapd_SSL_warn("The value of allowWeakDHParam \"%s\" is invalid.",
"Ignoring it and set it to default.", val);
}
}
slapi_ch_free((void **)&val);
return allow;
}
char **
getEnabledCiphers(void)
{
SSLCipherSuiteInfo info;
char *sep = "::";
int number_of_ciphers = 0;
int idx = 0;
PRBool enabled;
/* We have to wait until the SSL initialization is done. */
if (!slapd_ssl_listener_is_initialized()) {
return NULL;
}
if ((enabled_cipher_names == NULL) && _conf_ciphers) {
for (size_t x = 0; _conf_ciphers[x].name; x++) {
SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &enabled);
if (enabled) {
number_of_ciphers++;
}
}
enabled_cipher_names = (char **)slapi_ch_calloc((number_of_ciphers + 1), sizeof(char *));
for (size_t x = 0; _conf_ciphers[x].name; x++) {
SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &enabled);
if (enabled) {
SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[x].num, &info, sizeof(info));
enabled_cipher_names[idx++] = slapi_ch_smprintf("%s%s%s%s%s%s%d",
_conf_ciphers[x].name, sep,
info.symCipherName, sep,
info.macAlgorithmName, sep,
info.symKeyBits);
}
}
}
return enabled_cipher_names;
}
static PRBool
cipher_check_fips(int idx, char ***suplist, char ***unsuplist)
{
PRBool rc = PR_TRUE;
if (_conf_ciphers && slapd_pk11_isFIPS()) {
SSLCipherSuiteInfo info;
if (SECFailure == SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[idx].num,
&info, sizeof info)) {
PRErrorCode errorCode = PR_GetError();
if (slapi_is_loglevel_set(SLAPI_LOG_CONFIG)) {
slapd_SSL_warn("No information for cipher suite [%s] "
"error %d - %s",
_conf_ciphers[idx].name,
errorCode, slapd_pr_strerror(errorCode));
}
rc = PR_FALSE;
}
if (rc && !info.isFIPS) {
if (slapi_is_loglevel_set(SLAPI_LOG_CONFIG)) {
slapd_SSL_warn("FIPS mode is enabled but "
"cipher suite [%s] is not approved for FIPS - "
"the cipher suite will be disabled - if "
"you want to use this cipher suite, you must use modutil to "
"disable FIPS in the internal token.",
_conf_ciphers[idx].name);
}
rc = PR_FALSE;
}
if (!rc && unsuplist && !charray_inlist(*unsuplist, _conf_ciphers[idx].name)) {
charray_add(unsuplist, _conf_ciphers[idx].name);
}
if (rc && suplist && !charray_inlist(*suplist, _conf_ciphers[idx].name)) {
charray_add(suplist, _conf_ciphers[idx].name);
}
}
return rc;
}
int
getSSLVersionInfo(int *ssl2, int *ssl3, int *tls1)
{
if (!slapd_ssl_listener_is_initialized()) {
return -1;
}
*ssl2 = enableSSL2;
*ssl3 = enableSSL3;
*tls1 = enableTLS1;
return 0;
}
int
getSSLVersionRange(char **min, char **max)
{
if (!min && !max) {
return -1;
}
if (!slapd_ssl_listener_is_initialized()) {
/*
* We have not initialized NSS yet, so we will set the default for
* now. Then it will get adjusted to NSS's default min and max once
* we complete the security initialization in slapd_ssl_init2()
*/
if (min) {
*min = slapi_getSSLVersion_str(LDAP_OPT_X_TLS_PROTOCOL_TLS1_2, NULL, 0);
}
if (max) {
*max = slapi_getSSLVersion_str(LDAP_OPT_X_TLS_PROTOCOL_TLS1_2, NULL, 0);
}
return -1;
}
if (min) {
*min = slapi_getSSLVersion_str(slapdNSSVersions.min, NULL, 0);
}
if (max) {
*max = slapi_getSSLVersion_str(slapdNSSVersions.max, NULL, 0);
}
return 0;
}
void
getSSLVersionRangeOL(int *min, int *max)
{
/* default range values */
if (min) {
*min = LDAP_OPT_X_TLS_PROTOCOL_TLS1_2;
}
if (max) {
*max = LDAP_OPT_X_TLS_PROTOCOL_TLS1_2;
}
if (!slapd_ssl_listener_is_initialized()) {
return;
}
if (min) {
switch (slapdNSSVersions.min) {
case SSL_LIBRARY_VERSION_3_0:
*min = LDAP_OPT_X_TLS_PROTOCOL_SSL3;
break;
case SSL_LIBRARY_VERSION_TLS_1_0:
*min = LDAP_OPT_X_TLS_PROTOCOL_TLS1_0;
break;
case SSL_LIBRARY_VERSION_TLS_1_1:
*min = LDAP_OPT_X_TLS_PROTOCOL_TLS1_1;
break;
case SSL_LIBRARY_VERSION_TLS_1_2:
*min = LDAP_OPT_X_TLS_PROTOCOL_TLS1_2;
break;
default:
if (slapdNSSVersions.min > SSL_LIBRARY_VERSION_TLS_1_2) {
*min = LDAP_OPT_X_TLS_PROTOCOL_TLS1_2 + 1;
} else {
*min = LDAP_OPT_X_TLS_PROTOCOL_SSL3;
}
break;
}
}
if (max) {
switch (slapdNSSVersions.max) {
case SSL_LIBRARY_VERSION_3_0:
*max = LDAP_OPT_X_TLS_PROTOCOL_SSL3;
break;
case SSL_LIBRARY_VERSION_TLS_1_0:
*max = LDAP_OPT_X_TLS_PROTOCOL_TLS1_0;
break;
case SSL_LIBRARY_VERSION_TLS_1_1:
*max = LDAP_OPT_X_TLS_PROTOCOL_TLS1_1;
break;
case SSL_LIBRARY_VERSION_TLS_1_2:
*max = LDAP_OPT_X_TLS_PROTOCOL_TLS1_2;
break;
default:
if (slapdNSSVersions.max > SSL_LIBRARY_VERSION_TLS_1_2) {
*max = LDAP_OPT_X_TLS_PROTOCOL_TLS1_2 + 1;
} else {
*max = LDAP_OPT_X_TLS_PROTOCOL_SSL3;
}
break;
}
}
return;
}
static void
_conf_init_ciphers(void)
{
SECStatus rc;
SSLCipherSuiteInfo info;
const PRUint16 *implementedCiphers = SSL_GetImplementedCiphers();
/* Initialize _conf_ciphers */
if (_conf_ciphers) {
return;
}
_conf_ciphers = (cipherstruct *)slapi_ch_calloc(SSL_NumImplementedCiphers + 1, sizeof(cipherstruct));
for (size_t x = 0; implementedCiphers && (x < SSL_NumImplementedCiphers); x++) {
rc = SSL_GetCipherSuiteInfo(implementedCiphers[x], &info, sizeof info);
if (SECFailure == rc) {
slapi_log_err(SLAPI_LOG_ERR, "Security Initialization",
"_conf_init_ciphers - Failed to get the cipher suite info of cipher ID %d\n",
implementedCiphers[x]);
continue;
}
if (!_conf_ciphers[x].num) { /* initialize each cipher */
_conf_ciphers[x].name = slapi_ch_strdup(info.cipherSuiteName);
_conf_ciphers[x].num = implementedCiphers[x];
if (info.symCipher == ssl_calg_null) {
_conf_ciphers[x].flags |= CIPHER_MUST_BE_DISABLED;
} else {
_conf_ciphers[x].flags |= info.isExportable ? CIPHER_IS_WEAK : (info.symCipher < ssl_calg_3des) ? CIPHER_IS_WEAK : (info.effectiveKeyBits < 128) ? CIPHER_IS_WEAK : 0;
}
}
}
return;
}
/*
* flag: CIPHER_SET_ALL -- enable all
* CIPHER_SET_NONE -- disable all
* CIPHER_SET_DEFAULT -- set default ciphers
* CIPHER_SET_ALLOW_WEAKCIPHER -- allow weak ciphers (can be or'ed with the ather CIPHER_SET flags)
*/
static void
_conf_setallciphers(int flag, char ***suplist, char ***unsuplist)
{
SECStatus rc;
PRBool setdefault = CIPHER_SET_ISDEFAULT(flag);
PRBool enabled = CIPHER_SET_ISALL(flag);
PRBool allowweakcipher = CIPHER_SET_ALLOWSWEAKCIPHER(flag);
PRBool setme = PR_FALSE;
const PRUint16 *implementedCiphers = SSL_GetImplementedCiphers();
_conf_init_ciphers();
for (size_t x = 0; implementedCiphers && (x < SSL_NumImplementedCiphers); x++) {
if (_conf_ciphers[x].flags & CIPHER_IS_DEFAULT) {
/* certainly, not the first time. */
setme = PR_TRUE;
} else if (setdefault) {
/*
* SSL_CipherPrefGetDefault
* If the application has not previously set the default preference,
* SSL_CipherPrefGetDefault returns the factory setting.
*/
rc = SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &setme);
if (SECFailure == rc) {
slapi_log_err(SLAPI_LOG_ERR, "Security Initialization",
"_conf_setallciphers - Failed to get the default state of cipher %s\n",
_conf_ciphers[x].name);
continue;
}
if (!allowweakcipher && (_conf_ciphers[x].flags & CIPHER_IS_WEAK)) {
setme = PR_FALSE;
}
_conf_ciphers[x].flags |= setme ? CIPHER_IS_DEFAULT : 0;
} else if (enabled && !(_conf_ciphers[x].flags & CIPHER_MUST_BE_DISABLED)) {
if (!allowweakcipher && (_conf_ciphers[x].flags & CIPHER_IS_WEAK)) {
setme = PR_FALSE;
} else {
setme = PR_TRUE;
}
} else {
setme = PR_FALSE;
}
if (setme) {
setme = cipher_check_fips(x, suplist, unsuplist);
}
SSL_CipherPrefSetDefault(_conf_ciphers[x].num, setme);
}
}
static char *
charray2str(char **ary, const char *delim)
{
char *str = NULL;
while (ary && *ary) {
if (str) {
str = PR_sprintf_append(str, "%s%s", delim, *ary++);
} else {
str = slapi_ch_smprintf("%s", *ary++);
}
}
return str;
}
void
_conf_dumpciphers(void)
{
PRBool enabled;
/* {"SSL3","rc4", SSL_EN_RC4_128_WITH_MD5}, */
slapd_SSL_info("Configured NSS Ciphers");
for (size_t x = 0; _conf_ciphers[x].name; x++) {
SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &enabled);
if (enabled) {
slapd_SSL_info("\t%s: enabled%s%s%s", _conf_ciphers[x].name,
(_conf_ciphers[x].flags & CIPHER_IS_WEAK) ? ", (WEAK CIPHER)" : "",
(_conf_ciphers[x].flags & CIPHER_IS_DEPRECATED) ? ", (DEPRECATED)" : "",
(_conf_ciphers[x].flags & CIPHER_MUST_BE_DISABLED) ? ", (MUST BE DISABLED)" : "");
} else if (slapi_is_loglevel_set(SLAPI_LOG_CONFIG)) {
slapd_SSL_info("\t%s: disabled%s%s%s", _conf_ciphers[x].name,
(_conf_ciphers[x].flags & CIPHER_IS_WEAK) ? ", (WEAK CIPHER)" : "",
(_conf_ciphers[x].flags & CIPHER_IS_DEPRECATED) ? ", (DEPRECATED)" : "",
(_conf_ciphers[x].flags & CIPHER_MUST_BE_DISABLED) ? ", (MUST BE DISABLED)" : "");
}
}
}
char *
_conf_setciphers(char *setciphers, int flags)
{
char *t, err[MAGNUS_ERROR_LEN];
int active;
size_t x = 0;
char *raw = setciphers;
char **suplist = NULL;
char **unsuplist = NULL;
PRBool enabledOne = PR_FALSE;
/* #47838: harden the list of ciphers available by default */
/* Default is to activate all of them ==> none of them*/
if (!setciphers || (setciphers[0] == '\0') || !PL_strcasecmp(setciphers, "default")) {
_conf_setallciphers((CIPHER_SET_DEFAULT | flags), NULL, NULL);
slapd_SSL_info("Enabling default cipher set.");
_conf_dumpciphers();
return NULL;
}
if (PL_strcasestr(setciphers, "+all")) {
/*
* Enable all the ciphers if "+all" and the following while loop would
* disable the user disabled ones. This is needed because we added a new
* set of ciphers in the table. Right now there is no support for this
* from the console
*/
_conf_setallciphers((CIPHER_SET_ALL | flags), &suplist, NULL);
enabledOne = PR_TRUE;
} else {
/* If "+all" is not in nsSSL3Ciphers value, disable all first,
* then enable specified ciphers. */
_conf_setallciphers(CIPHER_SET_NONE /* disabled */, NULL, NULL);
}
t = setciphers;
while (t) {
while ((*setciphers) && (isspace(*setciphers)))
++setciphers;
switch (*setciphers++) {
case '+':
active = 1;
break;
case '-':
active = 0;
break;
default:
if (strlen(raw) > MAGNUS_ERROR_LEN) {
PR_snprintf(err, sizeof(err) - 3, "%s...", raw);
return slapi_ch_smprintf("invalid ciphers <%s>: format is +cipher1,-cipher2...", err);
} else {
return slapi_ch_smprintf("invalid ciphers <%s>: format is +cipher1,-cipher2...", raw);
}
}
if ((t = strchr(setciphers, ',')))
*t++ = '\0';
if (strcasecmp(setciphers, "all")) { /* if not all */
PRBool enabled = active ? PR_TRUE : PR_FALSE;
for (x = 0; _conf_ciphers[x].name; x++) {
if (!PL_strcasecmp(setciphers, _conf_ciphers[x].name)) {
if (_conf_ciphers[x].flags & CIPHER_IS_WEAK) {
if (active && CIPHER_SET_ALLOWSWEAKCIPHER(flags)) {
slapd_SSL_warn("Cipher %s is weak. It is enabled since allowWeakCipher is \"on\" "
"(default setting for the backward compatibility). "
"We strongly recommend to set it to \"off\". "
"Please replace the value of allowWeakCipher with \"off\" in "
"the encryption config entry cn=encryption,cn=config and "
"restart the server.",
setciphers);
} else {
/* if the cipher is weak and we don't allow weak cipher,
disable it. */
enabled = PR_FALSE;
}
}
if (enabled) {
/* if the cipher is not weak or we allow weak cipher,
check fips. */
enabled = cipher_check_fips(x, NULL, &unsuplist);
}
if (enabled) {
enabledOne = PR_TRUE; /* At least one active cipher is set. */
}
SSL_CipherPrefSetDefault(_conf_ciphers[x].num, enabled);
break;
}
}
if (!_conf_ciphers[x].name) {
slapd_SSL_warn("Cipher suite %s is not available in NSS %d.%d. Ignoring %s",
setciphers, NSS_VMAJOR, NSS_VMINOR, setciphers);
}
}
if (t) {
setciphers = t;
}
}
if (unsuplist && *unsuplist) {
char *strsup = charray2str(suplist, ",");
char *strunsup = charray2str(unsuplist, ",");
slapd_SSL_warn("FIPS mode is enabled - only the following "
"cipher suites are approved for FIPS: [%s] - "
"the specified cipher suites [%s] are disabled - if "
"you want to use these unsupported cipher suites, you must use modutil to "
"disable FIPS in the internal token.",
strsup ? strsup : "(none)", strunsup ? strunsup : "(none)");
slapi_ch_free_string(&strsup);
slapi_ch_free_string(&strunsup);
}
slapi_ch_free((void **)&suplist); /* strings inside are static */
slapi_ch_free((void **)&unsuplist); /* strings inside are static */
if (!enabledOne) {
char *nocipher = slapi_ch_smprintf("No active cipher suite is available.");
return nocipher;
}
_conf_dumpciphers();
return NULL;
}
/* SSL Policy stuff */
/*
* SSLPLCY_Install
*
* Call the SSL_CipherPolicySet function for each ciphersuite.
*/
PRStatus
SSLPLCY_Install(void)
{
SECStatus s = 0;
s = NSS_SetDomesticPolicy();
return s ? PR_FAILURE : PR_SUCCESS;
}
/**
* Get a particular entry
*/
static Slapi_Entry *
getConfigEntry(const char *dn, Slapi_Entry **e2)
{
Slapi_DN sdn;
slapi_sdn_init_dn_byref(&sdn, dn);
slapi_search_internal_get_entry(&sdn, NULL, e2,
plugin_get_default_component_id());
slapi_sdn_done(&sdn);
return *e2;
}
/**
* Free an entry
*/
static void
freeConfigEntry(Slapi_Entry **e)
{
if ((e != NULL) && (*e != NULL)) {
slapi_entry_free(*e);
*e = NULL;
}
}
/**
* Get a list of child DNs
*/
static char **
getChildren(char *dn)
{
Slapi_PBlock *new_pb = NULL;
Slapi_Entry **e;
int search_result = 1;
int nEntries = 0;
char **list = NULL;
new_pb = slapi_search_internal(dn, LDAP_SCOPE_ONELEVEL,
"(objectclass=nsEncryptionModule)",
NULL, NULL, 0);
slapi_pblock_get(new_pb, SLAPI_NENTRIES, &nEntries);
if (nEntries > 0) {
slapi_pblock_get(new_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
slapi_pblock_get(new_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &e);
if (e != NULL) {
list = (char **)slapi_ch_malloc(sizeof(*list) * (nEntries + 1));
for (size_t i = 0; e[i] != NULL; i++) {
list[i] = slapi_ch_strdup(slapi_entry_get_dn(e[i]));
}
list[nEntries] = NULL;
}
}
slapi_free_search_results_internal(new_pb);
slapi_pblock_destroy(new_pb);
return list;
}
/**
* Free a list of child DNs
*/
static void
freeChildren(char **list)
{
if (list != NULL) {
for (size_t i = 0; list[i] != NULL; i++) {
slapi_ch_free((void **)(&list[i]));
}
slapi_ch_free((void **)(&list));
}
}
static void
entrySetValue(Slapi_DN *sdn, char *type, char *value)
{
Slapi_PBlock *mypb = slapi_pblock_new();
LDAPMod attr;
LDAPMod *mods[2];
char *values[2];
values[0] = value;
values[1] = NULL;
/* modify the attribute */
attr.mod_type = type;
attr.mod_op = LDAP_MOD_REPLACE;
attr.mod_values = values;
mods[0] = &attr;
mods[1] = NULL;
slapi_modify_internal_set_pb_ext(mypb, sdn, mods, NULL, NULL, (void *)plugin_get_default_component_id(), 0);
slapi_modify_internal_pb(mypb);
slapi_pblock_destroy(mypb);
}
/* Logs a warning and returns 1 if cert file doesn't exist. You
* can skip the warning log message by setting no_log to 1.*/
static int
warn_if_no_cert_file(const char *dir, int no_log)
{
int ret = 0;
char *filename = slapi_ch_smprintf("%s/cert8.db", dir);
PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
if (PR_SUCCESS != status) {
slapi_ch_free_string(&filename);
filename = slapi_ch_smprintf("%s/cert9.db", dir);
status = PR_Access(filename, PR_ACCESS_READ_OK);
if (PR_SUCCESS != status) {
ret = 1;
if (!no_log) {
slapi_log_err(SLAPI_LOG_CRIT, "Security Initialization",
"warn_if_no_cert_file - Certificate DB file cert8.db nor cert9.db exists in [%s] - SSL initialization will likely fail\n", dir);
}
}
}
slapi_ch_free_string(&filename);
return ret;
}
/* Logs a warning and returns 1 if key file doesn't exist. You
* can skip the warning log message by setting no_log to 1.*/
static int
warn_if_no_key_file(const char *dir, int no_log)
{
int ret = 0;
char *filename = slapi_ch_smprintf("%s/key3.db", dir);
PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
if (PR_SUCCESS != status) {
slapi_ch_free_string(&filename);
filename = slapi_ch_smprintf("%s/key4.db", dir);
status = PR_Access(filename, PR_ACCESS_READ_OK);
if (PR_SUCCESS != status) {
ret = 1;
if (!no_log) {
slapi_log_err(SLAPI_LOG_CRIT, "Security Initialization",
"warn_if_no_key_file - Key DB file key3.db nor key4.db exists in [%s] - SSL initialization will likely fail\n", dir);
}
}
}
slapi_ch_free_string(&filename);
return ret;
}
/*
* If non NULL buf and positive bufsize is given,
* the memory is used to store the version string.
* Otherwise, the memory for the string is allocated.
* The latter case, caller is responsible to free it.
*/
char *
slapi_getSSLVersion_str(PRUint16 vnum, char *buf, size_t bufsize)
{
char *vstr = buf;
if (vnum >= SSL_LIBRARY_VERSION_3_0) {
if (vnum == SSL_LIBRARY_VERSION_3_0) { /* SSL3 */
if (buf && bufsize) {
PR_snprintf(buf, bufsize, "SSL3");
} else {
vstr = slapi_ch_smprintf("SSL3");
}
} else { /* TLS v X.Y */
const char *TLSFMT = "TLS%d.%d";
int minor_offset = 0; /* e.g. 0x0401 -> TLS v 2.1, not 2.0 */
if ((vnum & SSL_LIBRARY_VERSION_3_0) == SSL_LIBRARY_VERSION_3_0) {
minor_offset = 1; /* e.g. 0x0301 -> TLS v 1.0, not 1.1 */
}
if (buf && bufsize) {
PR_snprintf(buf, bufsize, TLSFMT, (vnum >> 8) - 2, (vnum & 0xff) - minor_offset);
} else {
vstr = slapi_ch_smprintf(TLSFMT, (vnum >> 8) - 2, (vnum & 0xff) - minor_offset);
}
}
} else if (vnum == SSL_LIBRARY_VERSION_2) { /* SSL2 */
if (buf && bufsize) {
PR_snprintf(buf, bufsize, "SSL2");
} else {
vstr = slapi_ch_smprintf("SSL2");
}
} else {
if (buf && bufsize) {
PR_snprintf(buf, bufsize, "Unknown SSL version: 0x%x", vnum);
} else {
vstr = slapi_ch_smprintf("Unknown SSL version: 0x%x", vnum);
}
}
return vstr;
}
#define SSLVGreater(x, y) (((x) > (y)) ? (x) : (y))
/* This routine returns the absolute path where to extract the pem files
* (certificate/key) in case nsslapd-private-certdir is private namespace.
* If absolute patch does not exist, it creates it with right 0777
*
* If the configuration parameter is not defined it returns NULL
*
* If the configuration parameter (nsslapd-private-certdir) is defined and
* valid (under /tmp namespace) it returns it.
*
* If /tmp is not a private namespace or configaration parameter is invalid
* it returns NULL
*
* If returned value is not NULL, caller must free it (slapi_ch_free_string)
*/
char *
check_private_certdir()
{
FILE *f;
char sline[1024];
const char *private_namespace_root = "/systemd-private";
const char *private_mountpoint = "/tmp"; /* Using systemd PrivateTmp=Yes, "/tmp" is expected to be private */
int mountid, parentid, major, minor;
char root[256] = {0}; /* path which forms the root of the mount */
char mountpoint[256] = {0}; /* path of the mountpoint relative to process root directory */
char rest[256] = {0};
PRBool tmp_private = PR_FALSE;
char *path_certdir;
char *certdir;
char *bname = NULL;
/* Check if /tmp is a private namespace */
f = fopen("/proc/self/mountinfo", "r");
if (f == NULL) {
return NULL;
}
while (fgets(sline, sizeof(sline), f)) {
sscanf(sline,"%d %d %d:%d %s %s %s\n",
&mountid, &parentid, &major, &minor, (char *)&root, (char *)&mountpoint, (char *)&rest);
if ((strncmp(mountpoint, private_mountpoint, strlen(private_mountpoint)) == 0) && /* mountpoint=/tmp */
strstr(root, private_namespace_root)) { /* root=...systemd-private... */
tmp_private = PR_TRUE;
break;
}
}
fclose(f);
if (!tmp_private) {
/* tmp is not a private name space */
if (_security_library_initialized == 0) {
/* only alert about this the first time around */
slapi_log_err(SLAPI_LOG_WARNING, "Security Initialization",
"%s is not a private namespace. pem files not exported there\n",
private_mountpoint);
}
return NULL;
}
/* Create the subdirectory under the private tmp namespace
* e.g.: /tmp/slapd-standalone1
*/
certdir = config_get_certdir();
if (certdir == NULL) {
/* config does not define a certdir path, extract pem
* under private_mountpoint
*/
bname = "";
} else {
bname = basename(certdir);
}
path_certdir = slapi_ch_smprintf("%s/%s", private_mountpoint, bname);
slapi_ch_free_string(&certdir);
if (mkdir_p(path_certdir, 0777)) {
slapi_log_err(SLAPI_LOG_WARNING, "Security Initialization",
"check_private_certdir - Fail to create %s\n",
path_certdir);
slapi_ch_free_string(&path_certdir);
return NULL;
}
return path_certdir;