ssl.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* SSL-related stuff for slapd */
  13. #include <stdio.h>
  14. #include <sys/param.h>
  15. #include <ssl.h>
  16. #include <nss.h>
  17. #include <key.h>
  18. #include <sslproto.h>
  19. #include "secmod.h"
  20. #include <string.h>
  21. #include <errno.h>
  22. #define NEED_TOK_PBE /* defines tokPBE and ptokPBE - see slap.h */
  23. #include "slap.h"
  24. #include "svrcore.h"
  25. #include "fe.h"
  26. #include "certdb.h"
  27. #if !defined(USE_OPENLDAP)
  28. #include "ldap_ssl.h"
  29. #endif
  30. /* For IRIX... */
  31. #ifndef MAXPATHLEN
  32. #define MAXPATHLEN 1024
  33. #endif
  34. #if NSS_VMAJOR * 100 + NSS_VMINOR >= 315
  35. /* TLS1.2 is defined in RFC5246. */
  36. #define NSS_TLS12 1
  37. #elif NSS_VMAJOR * 100 + NSS_VMINOR >= 314
  38. /* TLS1.1 is defined in RFC4346. */
  39. #define NSS_TLS11 1
  40. #else
  41. #define NSS_TLS10 1
  42. #endif
  43. /******************************************************************************
  44. * Default SSL Version Rule
  45. * Old SSL version attributes:
  46. * nsSSL3: off -- nsSSL3 == SSL_LIBRARY_VERSION_3_0
  47. * nsTLS1: on -- nsTLS1 == SSL_LIBRARY_VERSION_TLS_1_0 and greater
  48. * Note: TLS1.0 is defined in RFC2246, which is close to SSL 3.0.
  49. * New SSL version attributes:
  50. * sslVersionMin: TLS1.0
  51. * sslVersionMax: max ssl version supported by NSS
  52. ******************************************************************************/
  53. #define DEFVERSION "TLS1.0"
  54. #define CURRENT_DEFAULT_SSL_VERSION SSL_LIBRARY_VERSION_TLS_1_0
  55. extern char* slapd_SSL3ciphers;
  56. extern symbol_t supported_ciphers[];
  57. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  58. static SSLVersionRange enabledNSSVersions;
  59. static SSLVersionRange slapdNSSVersions;
  60. #endif
  61. /* dongle_file_name is set in slapd_nss_init when we set the path for the
  62. key, cert, and secmod files - the dongle file must be in the same directory
  63. and use the same naming scheme
  64. */
  65. static char* dongle_file_name = NULL;
  66. static int _security_library_initialized = 0;
  67. static int _ssl_listener_initialized = 0;
  68. static int _nss_initialized = 0;
  69. /* Our name for the internal token, must match PKCS-11 config data below */
  70. static char *internalTokenName = "Internal (Software) Token";
  71. static int stimeout;
  72. static char *ciphers = NULL;
  73. static char * configDN = "cn=encryption,cn=config";
  74. /* Copied from libadmin/libadmin.h public/nsapi.h */
  75. #define SERVER_KEY_NAME "Server-Key"
  76. #define MAGNUS_ERROR_LEN 1024
  77. #define LOG_WARN 0
  78. #define LOG_FAILURE 3
  79. #define FILE_PATHSEP '/'
  80. /* ----------------------- Multiple cipher support ------------------------ */
  81. /* cipher set flags */
  82. #define CIPHER_SET_NONE 0x0
  83. #define CIPHER_SET_ALL 0x1
  84. #define CIPHER_SET_DEFAULT 0x2
  85. #define CIPHER_SET_DEFAULTWEAKCIPHER 0x10 /* allowWeakCipher is not set in cn=encryption */
  86. #define CIPHER_SET_ALLOWWEAKCIPHER 0x20 /* allowWeakCipher is on */
  87. #define CIPHER_SET_DISALLOWWEAKCIPHER 0x40 /* allowWeakCipher is off */
  88. #define CIPHER_SET_ISDEFAULT(flag) \
  89. (((flag)&CIPHER_SET_DEFAULT) ? PR_TRUE : PR_FALSE)
  90. #define CIPHER_SET_ISALL(flag) \
  91. (((flag)&CIPHER_SET_ALL) ? PR_TRUE : PR_FALSE)
  92. #define ALLOWWEAK_ISDEFAULT(flag) \
  93. (((flag)&CIPHER_SET_DEFAULTWEAKCIPHER) ? PR_TRUE : PR_FALSE)
  94. #define ALLOWWEAK_ISON(flag) \
  95. (((flag)&CIPHER_SET_ALLOWWEAKCIPHER) ? PR_TRUE : PR_FALSE)
  96. #define ALLOWWEAK_ISOFF(flag) \
  97. (((flag)&CIPHER_SET_DISALLOWWEAKCIPHER) ? PR_TRUE : PR_FALSE)
  98. /*
  99. * If ISALL or ISDEFAULT, allowWeakCipher is true only if CIPHER_SET_ALLOWWEAKCIPHER.
  100. * Otherwise (user specified cipher list), allowWeakCipher is true
  101. * if CIPHER_SET_ALLOWWEAKCIPHER or CIPHER_SET_DEFAULTWEAKCIPHER.
  102. */
  103. #define CIPHER_SET_ALLOWSWEAKCIPHER(flag) \
  104. ((CIPHER_SET_ISDEFAULT(flag)|CIPHER_SET_ISALL(flag)) ? \
  105. (ALLOWWEAK_ISON(flag) ? PR_TRUE : PR_FALSE) : \
  106. (!ALLOWWEAK_ISOFF(flag) ? PR_TRUE : PR_FALSE))
  107. #define CIPHER_SET_DISABLE_ALLOWSWEAKCIPHER(flag) \
  108. ((flag)&~CIPHER_SET_ALLOWWEAKCIPHER)
  109. /* flags */
  110. #define CIPHER_IS_DEFAULT 0x1
  111. #define CIPHER_MUST_BE_DISABLED 0x2
  112. #define CIPHER_IS_WEAK 0x4
  113. #define CIPHER_IS_DEPRECATED 0x8
  114. static char **cipher_names = NULL;
  115. static char **enabled_cipher_names = NULL;
  116. typedef struct {
  117. char *name;
  118. int num;
  119. int flags;
  120. } cipherstruct;
  121. static cipherstruct *_conf_ciphers = NULL;
  122. static void _conf_init_ciphers();
  123. /*
  124. * This lookup table is for supporting the old cipher name.
  125. * Once swtiching to the NSS cipherSuiteName is done,
  126. * this lookup_cipher table can be removed.
  127. */
  128. typedef struct {
  129. char *alias;
  130. char *name;
  131. } lookup_cipher;
  132. static lookup_cipher _lookup_cipher[] = {
  133. {"rc4", "SSL_CK_RC4_128_WITH_MD5"},
  134. {"rc4export", "SSL_CK_RC4_128_EXPORT40_WITH_MD5"},
  135. {"rc2", "SSL_CK_RC2_128_CBC_WITH_MD5"},
  136. {"rc2export", "SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5"},
  137. /*{"idea", "SSL_EN_IDEA_128_CBC_WITH_MD5"}, */
  138. {"des", "SSL_CK_DES_64_CBC_WITH_MD5"},
  139. {"desede3", "SSL_CK_DES_192_EDE3_CBC_WITH_MD5"},
  140. {"rsa_rc4_128_md5", "TLS_RSA_WITH_RC4_128_MD5"},
  141. {"rsa_rc4_128_sha", "TLS_RSA_WITH_RC4_128_SHA"},
  142. {"rsa_3des_sha", "TLS_RSA_WITH_3DES_EDE_CBC_SHA"},
  143. {"tls_rsa_3des_sha", "TLS_RSA_WITH_3DES_EDE_CBC_SHA"},
  144. {"rsa_fips_3des_sha", "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA"},
  145. {"fips_3des_sha", "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA"},
  146. {"rsa_des_sha", "TLS_RSA_WITH_DES_CBC_SHA"},
  147. {"rsa_fips_des_sha", "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
  148. {"fips_des_sha", "SSL_RSA_FIPS_WITH_DES_CBC_SHA"}, /* ditto */
  149. {"rsa_rc4_40_md5", "TLS_RSA_EXPORT_WITH_RC4_40_MD5"},
  150. {"tls_rsa_rc4_40_md5", "TLS_RSA_EXPORT_WITH_RC4_40_MD5"},
  151. {"rsa_rc2_40_md5", "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5"},
  152. {"tls_rsa_rc2_40_md5", "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5"},
  153. {"rsa_null_md5", "TLS_RSA_WITH_NULL_MD5"}, /* disabled by default */
  154. {"rsa_null_sha", "TLS_RSA_WITH_NULL_SHA"}, /* disabled by default */
  155. {"tls_rsa_export1024_with_rc4_56_sha", "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA"},
  156. {"rsa_rc4_56_sha", "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA"}, /* ditto */
  157. {"tls_rsa_export1024_with_des_cbc_sha", "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA"},
  158. {"rsa_des_56_sha", "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA"}, /* ditto */
  159. {"fortezza", ""}, /* deprecated */
  160. {"fortezza_rc4_128_sha", ""}, /* deprecated */
  161. {"fortezza_null", ""}, /* deprecated */
  162. /*{"dhe_dss_40_sha", SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, 0}, */
  163. {"dhe_dss_des_sha", "TLS_DHE_DSS_WITH_DES_CBC_SHA"},
  164. {"dhe_dss_3des_sha", "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA"},
  165. {"dhe_rsa_40_sha", "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA"},
  166. {"dhe_rsa_des_sha", "TLS_DHE_RSA_WITH_DES_CBC_SHA"},
  167. {"dhe_rsa_3des_sha", "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA"},
  168. {"tls_rsa_aes_128_sha", "TLS_RSA_WITH_AES_128_CBC_SHA"},
  169. {"rsa_aes_128_sha", "TLS_RSA_WITH_AES_128_CBC_SHA"}, /* ditto */
  170. {"tls_dh_dss_aes_128_sha", ""}, /* deprecated */
  171. {"tls_dh_rsa_aes_128_sha", ""}, /* deprecated */
  172. {"tls_dhe_dss_aes_128_sha", "TLS_DHE_DSS_WITH_AES_128_CBC_SHA"},
  173. {"tls_dhe_rsa_aes_128_sha", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"},
  174. {"tls_rsa_aes_256_sha", "TLS_RSA_WITH_AES_256_CBC_SHA"},
  175. {"rsa_aes_256_sha", "TLS_RSA_WITH_AES_256_CBC_SHA"}, /* ditto */
  176. {"tls_dss_aes_256_sha", ""}, /* deprecated */
  177. {"tls_rsa_aes_256_sha", ""}, /* deprecated */
  178. {"tls_dhe_dss_aes_256_sha", "TLS_DHE_DSS_WITH_AES_256_CBC_SHA"},
  179. {"tls_dhe_rsa_aes_256_sha", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
  180. /*{"tls_dhe_dss_1024_des_sha", ""}, */
  181. {"tls_dhe_dss_1024_rc4_sha", "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA"},
  182. {"tls_dhe_dss_rc4_128_sha", "TLS_DHE_DSS_WITH_RC4_128_SHA"},
  183. #if defined(NSS_TLS12)
  184. /* New in NSS 3.15 */
  185. {"tls_rsa_aes_128_gcm_sha", "TLS_RSA_WITH_AES_128_GCM_SHA256"},
  186. {"tls_dhe_rsa_aes_128_gcm_sha", "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256"},
  187. {"tls_dhe_dss_aes_128_gcm_sha", NULL}, /* not available */
  188. #endif
  189. {NULL, NULL}
  190. };
  191. /* E.g., "SSL3", "TLS1.2", "Unknown SSL version: 0x0" */
  192. #define VERSION_STR_LENGTH 64
  193. /* Supported SSL versions */
  194. /* nsSSL2: on -- we don't allow this any more. */
  195. PRBool enableSSL2 = PR_FALSE;
  196. /*
  197. * nsSSL3: on -- disable SSLv3 by default.
  198. * Corresonding to SSL_LIBRARY_VERSION_3_0
  199. */
  200. PRBool enableSSL3 = PR_FALSE;
  201. /*
  202. * nsTLS1: on -- enable TLS1 by default.
  203. * Corresonding to SSL_LIBRARY_VERSION_TLS_1_0 and greater.
  204. */
  205. PRBool enableTLS1 = PR_TRUE;
  206. static void
  207. slapd_SSL_report(int degree, char *fmt, va_list args)
  208. {
  209. char buf[2048];
  210. PR_vsnprintf( buf, sizeof(buf), fmt, args );
  211. LDAPDebug( LDAP_DEBUG_ANY, "SSL %s: %s\n",
  212. (degree == LOG_FAILURE) ? "failure" : "alert",
  213. buf, 0 );
  214. }
  215. void
  216. slapd_SSL_error(char *fmt, ...)
  217. {
  218. va_list args;
  219. va_start(args, fmt);
  220. slapd_SSL_report(LOG_FAILURE, fmt, args);
  221. va_end(args);
  222. }
  223. void
  224. slapd_SSL_warn(char *fmt, ...)
  225. {
  226. va_list args;
  227. va_start(args, fmt);
  228. slapd_SSL_report(LOG_WARN, fmt, args);
  229. va_end(args);
  230. }
  231. char **
  232. getSupportedCiphers()
  233. {
  234. SSLCipherSuiteInfo info;
  235. char *sep = "::";
  236. int number_of_ciphers = SSL_NumImplementedCiphers;
  237. int i;
  238. int idx = 0;
  239. PRBool isFIPS = slapd_pk11_isFIPS();
  240. _conf_init_ciphers();
  241. if ((cipher_names == NULL) && (_conf_ciphers)) {
  242. cipher_names = (char **)slapi_ch_calloc((number_of_ciphers + 1), sizeof(char *));
  243. for (i = 0 ; _conf_ciphers[i].name != NULL; i++ ) {
  244. SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[i].num,&info,sizeof(info));
  245. /* only support FIPS approved ciphers in FIPS mode */
  246. if (!isFIPS || info.isFIPS) {
  247. cipher_names[idx++] = PR_smprintf("%s%s%s%s%s%s%d",
  248. _conf_ciphers[i].name,sep,
  249. info.symCipherName,sep,
  250. info.macAlgorithmName,sep,
  251. info.symKeyBits);
  252. }
  253. }
  254. cipher_names[idx] = NULL;
  255. }
  256. return cipher_names;
  257. }
  258. char **
  259. getEnabledCiphers()
  260. {
  261. SSLCipherSuiteInfo info;
  262. char *sep = "::";
  263. int number_of_ciphers = 0;
  264. int x;
  265. int idx = 0;
  266. PRBool enabled;
  267. /* We have to wait until the SSL initialization is done. */
  268. if (!slapd_ssl_listener_is_initialized()) {
  269. return NULL;
  270. }
  271. if ((enabled_cipher_names == NULL) && _conf_ciphers) {
  272. for (x = 0; _conf_ciphers[x].name; x++) {
  273. SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &enabled);
  274. if (enabled) {
  275. number_of_ciphers++;
  276. }
  277. }
  278. enabled_cipher_names = (char **)slapi_ch_calloc((number_of_ciphers + 1), sizeof(char *));
  279. for (x = 0; _conf_ciphers[x].name; x++) {
  280. SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &enabled);
  281. if (enabled) {
  282. SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[x].num,&info,sizeof(info));
  283. enabled_cipher_names[idx++] = PR_smprintf("%s%s%s%s%s%s%d",
  284. _conf_ciphers[x].name,sep,
  285. info.symCipherName,sep,
  286. info.macAlgorithmName,sep,
  287. info.symKeyBits);
  288. }
  289. }
  290. }
  291. return enabled_cipher_names;
  292. }
  293. static PRBool
  294. cipher_check_fips(int idx, char ***suplist, char ***unsuplist)
  295. {
  296. PRBool rc = PR_TRUE;
  297. if (_conf_ciphers && slapd_pk11_isFIPS()) {
  298. SSLCipherSuiteInfo info;
  299. if (SECFailure == SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[idx].num,
  300. &info, sizeof info)) {
  301. PRErrorCode errorCode = PR_GetError();
  302. if (slapi_is_loglevel_set(SLAPI_LOG_CONFIG)) {
  303. slapd_SSL_warn("Security Initialization: no information for cipher suite [%s] "
  304. "error %d - %s", _conf_ciphers[idx].name,
  305. errorCode, slapd_pr_strerror(errorCode));
  306. }
  307. rc = PR_FALSE;
  308. }
  309. if (rc && !info.isFIPS) {
  310. if (slapi_is_loglevel_set(SLAPI_LOG_CONFIG)) {
  311. slapd_SSL_warn("Security Initialization: FIPS mode is enabled but "
  312. "cipher suite [%s] is not approved for FIPS - "
  313. "the cipher suite will be disabled - if "
  314. "you want to use this cipher suite, you must use modutil to "
  315. "disable FIPS in the internal token.",
  316. _conf_ciphers[idx].name);
  317. }
  318. rc = PR_FALSE;
  319. }
  320. if (!rc && unsuplist && !charray_inlist(*unsuplist, _conf_ciphers[idx].name)) {
  321. charray_add(unsuplist, _conf_ciphers[idx].name);
  322. }
  323. if (rc && suplist && !charray_inlist(*suplist, _conf_ciphers[idx].name)) {
  324. charray_add(suplist, _conf_ciphers[idx].name);
  325. }
  326. }
  327. return rc;
  328. }
  329. int
  330. getSSLVersionInfo(int *ssl2, int *ssl3, int *tls1)
  331. {
  332. if (!slapd_ssl_listener_is_initialized()) {
  333. return -1;
  334. }
  335. *ssl2 = enableSSL2;
  336. *ssl3 = enableSSL3;
  337. *tls1 = enableTLS1;
  338. return 0;
  339. }
  340. int
  341. getSSLVersionRange(char **min, char **max)
  342. {
  343. if (!slapd_ssl_listener_is_initialized()) {
  344. return -1;
  345. }
  346. if ((NULL == min) || (NULL == max)) {
  347. return -1;
  348. }
  349. #if defined(NSS_TLS10)
  350. return -1; /* not supported */
  351. #else /* NSS_TLS11 or newer */
  352. *min = slapi_getSSLVersion_str(slapdNSSVersions.min, NULL, 0);
  353. *max = slapi_getSSLVersion_str(slapdNSSVersions.max, NULL, 0);
  354. return 0;
  355. #endif
  356. }
  357. static void
  358. _conf_init_ciphers()
  359. {
  360. int x;
  361. SECStatus rc;
  362. SSLCipherSuiteInfo info;
  363. const PRUint16 *implementedCiphers = SSL_GetImplementedCiphers();
  364. /* Initialize _conf_ciphers */
  365. if (_conf_ciphers) {
  366. return;
  367. }
  368. _conf_ciphers = (cipherstruct *)slapi_ch_calloc(SSL_NumImplementedCiphers + 1, sizeof(cipherstruct));
  369. for (x = 0; implementedCiphers && (x < SSL_NumImplementedCiphers); x++) {
  370. rc = SSL_GetCipherSuiteInfo(implementedCiphers[x], &info, sizeof info);
  371. if (SECFailure == rc) {
  372. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  373. "Warning: failed to get the cipher suite info of cipher ID %d\n",
  374. implementedCiphers[x]);
  375. continue;
  376. }
  377. if (!_conf_ciphers[x].num) { /* initialize each cipher */
  378. _conf_ciphers[x].name = slapi_ch_strdup(info.cipherSuiteName);
  379. _conf_ciphers[x].num = implementedCiphers[x];
  380. if (info.symCipher == ssl_calg_null) {
  381. _conf_ciphers[x].flags |= CIPHER_MUST_BE_DISABLED;
  382. } else {
  383. _conf_ciphers[x].flags |= info.isExportable?CIPHER_IS_WEAK:
  384. (info.symCipher < ssl_calg_3des)?CIPHER_IS_WEAK:
  385. (info.effectiveKeyBits < 128)?CIPHER_IS_WEAK:0;
  386. }
  387. }
  388. }
  389. return;
  390. }
  391. /*
  392. * flag: CIPHER_SET_ALL -- enable all
  393. * CIPHER_SET_NONE -- disable all
  394. * CIPHER_SET_DEFAULT -- set default ciphers
  395. * CIPHER_SET_ALLOW_WEAKCIPHER -- allow weak ciphers (can be or'ed with the ather CIPHER_SET flags)
  396. */
  397. static void
  398. _conf_setallciphers(int flag, char ***suplist, char ***unsuplist)
  399. {
  400. int x;
  401. SECStatus rc;
  402. PRBool setdefault = CIPHER_SET_ISDEFAULT(flag);
  403. PRBool enabled = CIPHER_SET_ISALL(flag);
  404. PRBool allowweakcipher = CIPHER_SET_ALLOWSWEAKCIPHER(flag);
  405. PRBool setme = PR_FALSE;
  406. const PRUint16 *implementedCiphers = SSL_GetImplementedCiphers();
  407. _conf_init_ciphers();
  408. for (x = 0; implementedCiphers && (x < SSL_NumImplementedCiphers); x++) {
  409. if (_conf_ciphers[x].flags & CIPHER_IS_DEFAULT) {
  410. /* certainly, not the first time. */
  411. setme = PR_TRUE;
  412. } else if (setdefault) {
  413. /*
  414. * SSL_CipherPrefGetDefault
  415. * If the application has not previously set the default preference,
  416. * SSL_CipherPrefGetDefault returns the factory setting.
  417. */
  418. rc = SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &setme);
  419. if (SECFailure == rc) {
  420. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  421. "Warning: failed to get the default state of cipher %s\n",
  422. _conf_ciphers[x].name);
  423. continue;
  424. }
  425. if (!allowweakcipher && (_conf_ciphers[x].flags & CIPHER_IS_WEAK)) {
  426. setme = PR_FALSE;
  427. }
  428. _conf_ciphers[x].flags |= setme?CIPHER_IS_DEFAULT:0;
  429. } else if (enabled && !(_conf_ciphers[x].flags & CIPHER_MUST_BE_DISABLED)) {
  430. if (!allowweakcipher && (_conf_ciphers[x].flags & CIPHER_IS_WEAK)) {
  431. setme = PR_FALSE;
  432. } else {
  433. setme = PR_TRUE;
  434. }
  435. } else {
  436. setme = PR_FALSE;
  437. }
  438. if (setme) {
  439. setme = cipher_check_fips(x, suplist, unsuplist);
  440. }
  441. SSL_CipherPrefSetDefault(_conf_ciphers[x].num, setme);
  442. }
  443. }
  444. static char *
  445. charray2str(char **ary, const char *delim)
  446. {
  447. char *str = NULL;
  448. while (ary && *ary) {
  449. if (str) {
  450. str = PR_sprintf_append(str, "%s%s", delim, *ary++);
  451. } else {
  452. str = PR_smprintf("%s", *ary++);
  453. }
  454. }
  455. return str;
  456. }
  457. void
  458. _conf_dumpciphers()
  459. {
  460. int x;
  461. PRBool enabled;
  462. /* {"SSL3","rc4", SSL_EN_RC4_128_WITH_MD5}, */
  463. slapd_SSL_warn("Configured NSS Ciphers");
  464. for (x = 0; _conf_ciphers[x].name; x++) {
  465. SSL_CipherPrefGetDefault(_conf_ciphers[x].num, &enabled);
  466. if (enabled) {
  467. slapd_SSL_warn("\t%s: enabled%s%s%s", _conf_ciphers[x].name,
  468. (_conf_ciphers[x].flags&CIPHER_IS_WEAK)?", (WEAK CIPHER)":"",
  469. (_conf_ciphers[x].flags&CIPHER_IS_DEPRECATED)?", (DEPRECATED)":"",
  470. (_conf_ciphers[x].flags&CIPHER_MUST_BE_DISABLED)?", (MUST BE DISABLED)":"");
  471. } else if (slapi_is_loglevel_set(SLAPI_LOG_CONFIG)) {
  472. slapd_SSL_warn("\t%s: disabled%s%s%s", _conf_ciphers[x].name,
  473. (_conf_ciphers[x].flags&CIPHER_IS_WEAK)?", (WEAK CIPHER)":"",
  474. (_conf_ciphers[x].flags&CIPHER_IS_DEPRECATED)?", (DEPRECATED)":"",
  475. (_conf_ciphers[x].flags&CIPHER_MUST_BE_DISABLED)?", (MUST BE DISABLED)":"");
  476. }
  477. }
  478. }
  479. char *
  480. _conf_setciphers(char *ciphers, int flags)
  481. {
  482. char *t, err[MAGNUS_ERROR_LEN];
  483. int x, i, active;
  484. char *raw = ciphers;
  485. char **suplist = NULL;
  486. char **unsuplist = NULL;
  487. PRBool enabledOne = PR_FALSE;
  488. /* #47838: harden the list of ciphers available by default */
  489. /* Default is to activate all of them ==> none of them*/
  490. if (!ciphers || (ciphers[0] == '\0') || !PL_strcasecmp(ciphers, "default")) {
  491. _conf_setallciphers((CIPHER_SET_DEFAULT|flags), NULL, NULL);
  492. slapd_SSL_warn("Security Initialization: Enabling default cipher set.");
  493. _conf_dumpciphers();
  494. return NULL;
  495. }
  496. if (PL_strcasestr(ciphers, "+all")) {
  497. /*
  498. * Enable all the ciphers if "+all" and the following while loop would
  499. * disable the user disabled ones. This is needed because we added a new
  500. * set of ciphers in the table. Right now there is no support for this
  501. * from the console
  502. */
  503. _conf_setallciphers((CIPHER_SET_ALL|flags), &suplist, NULL);
  504. enabledOne = PR_TRUE;
  505. } else {
  506. /* If "+all" is not in nsSSL3Ciphers value, disable all first,
  507. * then enable specified ciphers. */
  508. _conf_setallciphers(CIPHER_SET_NONE /* disabled */, NULL, NULL);
  509. }
  510. t = ciphers;
  511. while(t) {
  512. while((*ciphers) && (isspace(*ciphers))) ++ciphers;
  513. switch(*ciphers++) {
  514. case '+':
  515. active = 1; break;
  516. case '-':
  517. active = 0; break;
  518. default:
  519. PR_snprintf(err, sizeof(err), "invalid ciphers <%s>: format is "
  520. "+cipher1,-cipher2...", raw);
  521. return slapi_ch_strdup(err);
  522. }
  523. if( (t = strchr(ciphers, ',')) )
  524. *t++ = '\0';
  525. if (strcasecmp(ciphers, "all")) { /* if not all */
  526. PRBool enabled = active ? PR_TRUE : PR_FALSE;
  527. int lookup = 1;
  528. for (x = 0; _conf_ciphers[x].name; x++) {
  529. if (!PL_strcasecmp(ciphers, _conf_ciphers[x].name)) {
  530. if (_conf_ciphers[x].flags & CIPHER_IS_WEAK) {
  531. if (active && CIPHER_SET_ALLOWSWEAKCIPHER(flags)) {
  532. slapd_SSL_warn("Cipher %s is weak. It is enabled since allowWeakCipher is \"on\" "
  533. "(default setting for the backward compatibility). "
  534. "We strongly recommend to set it to \"off\". "
  535. "Please replace the value of allowWeakCipher with \"off\" in "
  536. "the encryption config entry cn=encryption,cn=config and "
  537. "restart the server.", ciphers);
  538. } else {
  539. /* if the cipher is weak and we don't allow weak cipher,
  540. disable it. */
  541. enabled = PR_FALSE;
  542. }
  543. }
  544. if (enabled) {
  545. /* if the cipher is not weak or we allow weak cipher,
  546. check fips. */
  547. enabled = cipher_check_fips(x, NULL, &unsuplist);
  548. }
  549. if (enabled) {
  550. enabledOne = PR_TRUE; /* At least one active cipher is set. */
  551. }
  552. SSL_CipherPrefSetDefault(_conf_ciphers[x].num, enabled);
  553. lookup = 0;
  554. break;
  555. }
  556. }
  557. if (lookup) { /* lookup with old cipher name and get NSS cipherSuiteName */
  558. for (i = 0; _lookup_cipher[i].alias; i++) {
  559. if (!PL_strcasecmp(ciphers, _lookup_cipher[i].alias)) {
  560. if (!_lookup_cipher[i].name[0]) {
  561. slapd_SSL_warn("Cipher suite %s is not available in NSS %d.%d. Ignoring %s",
  562. ciphers, NSS_VMAJOR, NSS_VMINOR, ciphers);
  563. continue;
  564. }
  565. for (x = 0; _conf_ciphers[x].name; x++) {
  566. if (!PL_strcasecmp(_lookup_cipher[i].name, _conf_ciphers[x].name)) {
  567. if (enabled) {
  568. if (_conf_ciphers[x].flags & CIPHER_IS_WEAK) {
  569. if (active && CIPHER_SET_ALLOWSWEAKCIPHER(flags)) {
  570. slapd_SSL_warn("Cipher %s is weak. "
  571. "It is enabled since allowWeakCipher is \"on\" "
  572. "(default setting for the backward compatibility). "
  573. "We strongly recommend to set it to \"off\". "
  574. "Please replace the value of allowWeakCipher with \"off\" in "
  575. "the encryption config entry cn=encryption,cn=config and "
  576. "restart the server.", ciphers);
  577. } else {
  578. /* if the cipher is weak and we don't allow weak cipher,
  579. disable it. */
  580. enabled = PR_FALSE;
  581. }
  582. }
  583. if (enabled) {
  584. /* if the cipher is not weak or we allow weak cipher,
  585. check fips. */
  586. enabled = cipher_check_fips(x, NULL, &unsuplist);
  587. }
  588. }
  589. if (enabled) {
  590. enabledOne = PR_TRUE; /* At least one active cipher is set. */
  591. }
  592. SSL_CipherPrefSetDefault(_conf_ciphers[x].num, enabled);
  593. break;
  594. }
  595. }
  596. break;
  597. }
  598. }
  599. }
  600. if (!lookup && !_conf_ciphers[x].name) { /* If lookup, it's already reported. */
  601. slapd_SSL_warn("Cipher suite %s is not available in NSS %d.%d. Ignoring %s",
  602. ciphers, NSS_VMAJOR, NSS_VMINOR, ciphers);
  603. }
  604. }
  605. if(t) {
  606. ciphers = t;
  607. }
  608. }
  609. if (unsuplist && *unsuplist) {
  610. char *strsup = charray2str(suplist, ",");
  611. char *strunsup = charray2str(unsuplist, ",");
  612. slapd_SSL_warn("Security Initialization: FIPS mode is enabled - only the following "
  613. "cipher suites are approved for FIPS: [%s] - "
  614. "the specified cipher suites [%s] are disabled - if "
  615. "you want to use these unsupported cipher suites, you must use modutil to "
  616. "disable FIPS in the internal token.",
  617. strsup ? strsup : "(none)", strunsup ? strunsup : "(none)");
  618. slapi_ch_free_string(&strsup);
  619. slapi_ch_free_string(&strunsup);
  620. }
  621. slapi_ch_free((void **)&suplist); /* strings inside are static */
  622. slapi_ch_free((void **)&unsuplist); /* strings inside are static */
  623. if (!enabledOne) {
  624. char *nocipher = PR_smprintf("No active cipher suite is available.");
  625. return nocipher;
  626. }
  627. _conf_dumpciphers();
  628. return NULL;
  629. }
  630. /* SSL Policy stuff */
  631. /*
  632. * SSLPLCY_Install
  633. *
  634. * Call the SSL_CipherPolicySet function for each ciphersuite.
  635. */
  636. PRStatus
  637. SSLPLCY_Install(void)
  638. {
  639. SECStatus s = 0;
  640. s = NSS_SetDomesticPolicy();
  641. return s?PR_FAILURE:PR_SUCCESS;
  642. }
  643. /**
  644. * Get a particular entry
  645. */
  646. static Slapi_Entry *
  647. getConfigEntry( const char *dn, Slapi_Entry **e2 ) {
  648. Slapi_DN sdn;
  649. slapi_sdn_init_dn_byref( &sdn, dn );
  650. slapi_search_internal_get_entry( &sdn, NULL, e2,
  651. plugin_get_default_component_id());
  652. slapi_sdn_done( &sdn );
  653. return *e2;
  654. }
  655. /**
  656. * Free an entry
  657. */
  658. static void
  659. freeConfigEntry( Slapi_Entry ** e ) {
  660. if ( (e != NULL) && (*e != NULL) ) {
  661. slapi_entry_free( *e );
  662. *e = NULL;
  663. }
  664. }
  665. /**
  666. * Get a list of child DNs
  667. */
  668. static char **
  669. getChildren( char *dn ) {
  670. Slapi_PBlock *new_pb = NULL;
  671. Slapi_Entry **e;
  672. int search_result = 1;
  673. int nEntries = 0;
  674. char **list = NULL;
  675. new_pb = slapi_search_internal ( dn, LDAP_SCOPE_ONELEVEL,
  676. "(objectclass=nsEncryptionModule)",
  677. NULL, NULL, 0);
  678. slapi_pblock_get( new_pb, SLAPI_NENTRIES, &nEntries);
  679. if ( nEntries > 0 ) {
  680. slapi_pblock_get( new_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
  681. slapi_pblock_get( new_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &e);
  682. if ( e != NULL ) {
  683. int i;
  684. list = (char **)slapi_ch_malloc( sizeof(*list) * (nEntries + 1));
  685. for ( i = 0; e[i] != NULL; i++ ) {
  686. list[i] = slapi_ch_strdup(slapi_entry_get_dn(e[i]));
  687. }
  688. list[nEntries] = NULL;
  689. }
  690. }
  691. slapi_free_search_results_internal(new_pb);
  692. slapi_pblock_destroy(new_pb );
  693. return list;
  694. }
  695. /**
  696. * Free a list of child DNs
  697. */
  698. static void
  699. freeChildren( char **list ) {
  700. if ( list != NULL ) {
  701. int i;
  702. for ( i = 0; list[i] != NULL; i++ ) {
  703. slapi_ch_free( (void **)(&list[i]) );
  704. }
  705. slapi_ch_free( (void **)(&list) );
  706. }
  707. }
  708. /* Logs a warning and returns 1 if cert file doesn't exist. You
  709. * can skip the warning log message by setting no_log to 1.*/
  710. static int
  711. warn_if_no_cert_file(const char *dir, int no_log)
  712. {
  713. int ret = 0;
  714. char *filename = slapi_ch_smprintf("%s/cert8.db", dir);
  715. PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
  716. if (PR_SUCCESS != status) {
  717. slapi_ch_free_string(&filename);
  718. filename = slapi_ch_smprintf("%s/cert7.db", dir);
  719. status = PR_Access(filename, PR_ACCESS_READ_OK);
  720. if (PR_SUCCESS != status) {
  721. ret = 1;
  722. if (!no_log) {
  723. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  724. "Warning: certificate DB file cert8.db nor cert7.db exists in [%s] - "
  725. "SSL initialization will likely fail\n", dir);
  726. }
  727. }
  728. }
  729. slapi_ch_free_string(&filename);
  730. return ret;
  731. }
  732. /* Logs a warning and returns 1 if key file doesn't exist. You
  733. * can skip the warning log message by setting no_log to 1.*/
  734. static int
  735. warn_if_no_key_file(const char *dir, int no_log)
  736. {
  737. int ret = 0;
  738. char *filename = slapi_ch_smprintf("%s/key3.db", dir);
  739. PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
  740. if (PR_SUCCESS != status) {
  741. ret = 1;
  742. if (!no_log) {
  743. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  744. "Warning: key DB file %s does not exist - SSL initialization will "
  745. "likely fail\n", filename);
  746. }
  747. }
  748. slapi_ch_free_string(&filename);
  749. return ret;
  750. }
  751. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  752. /*
  753. * If non NULL buf and positive bufsize is given,
  754. * the memory is used to store the version string.
  755. * Otherwise, the memory for the string is allocated.
  756. * The latter case, caller is responsible to free it.
  757. */
  758. char *
  759. slapi_getSSLVersion_str(PRUint16 vnum, char *buf, size_t bufsize)
  760. {
  761. char *vstr = buf;
  762. if (vnum >= SSL_LIBRARY_VERSION_3_0) {
  763. if (vnum == SSL_LIBRARY_VERSION_3_0) { /* SSL3 */
  764. if (buf && bufsize) {
  765. PR_snprintf(buf, bufsize, "SSL3");
  766. } else {
  767. vstr = slapi_ch_smprintf("SSL3");
  768. }
  769. } else { /* TLS v X.Y */
  770. const char *TLSFMT = "TLS%d.%d";
  771. int minor_offset = 0; /* e.g. 0x0401 -> TLS v 2.1, not 2.0 */
  772. if ((vnum & SSL_LIBRARY_VERSION_3_0) == SSL_LIBRARY_VERSION_3_0) {
  773. minor_offset = 1; /* e.g. 0x0301 -> TLS v 1.0, not 1.1 */
  774. }
  775. if (buf && bufsize) {
  776. PR_snprintf(buf, bufsize, TLSFMT, (vnum >> 8) - 2, (vnum & 0xff) - minor_offset);
  777. } else {
  778. vstr = slapi_ch_smprintf(TLSFMT, (vnum >> 8) - 2, (vnum & 0xff) - minor_offset);
  779. }
  780. }
  781. } else if (vnum == SSL_LIBRARY_VERSION_2) { /* SSL2 */
  782. if (buf && bufsize) {
  783. PR_snprintf(buf, bufsize, "SSL2");
  784. } else {
  785. vstr = slapi_ch_smprintf("SSL2");
  786. }
  787. } else {
  788. if (buf && bufsize) {
  789. PR_snprintf(buf, bufsize, "Unknown SSL version: 0x%x", vnum);
  790. } else {
  791. vstr = slapi_ch_smprintf("Unknown SSL version: 0x%x", vnum);
  792. }
  793. }
  794. return vstr;
  795. }
  796. #define SSLVGreater(x, y) (((x) > (y)) ? (x) : (y))
  797. /*
  798. * Check the SSLVersionRange and the old style config params (nsSSL3, nsTLS1) .
  799. * If there are conflicts, choose the secure setting.
  800. */
  801. static void
  802. restrict_SSLVersionRange(void)
  803. {
  804. char mymin[VERSION_STR_LENGTH], mymax[VERSION_STR_LENGTH];
  805. char emin[VERSION_STR_LENGTH], emax[VERSION_STR_LENGTH];
  806. (void) slapi_getSSLVersion_str(slapdNSSVersions.min, mymin, sizeof(mymin));
  807. (void) slapi_getSSLVersion_str(slapdNSSVersions.max, mymax, sizeof(mymax));
  808. (void) slapi_getSSLVersion_str(enabledNSSVersions.max, emax, sizeof(emax));
  809. (void) slapi_getSSLVersion_str(enabledNSSVersions.min, emin, sizeof(emin));
  810. if (slapdNSSVersions.min > slapdNSSVersions.max) {
  811. slapd_SSL_warn("Invalid configured SSL range: min: %s, max: %s; "
  812. "Resetting the max to the supported max SSL version: %s.",
  813. mymin, mymax, emax);
  814. slapdNSSVersions.max = enabledNSSVersions.max;
  815. }
  816. if (enableSSL3) {
  817. slapd_SSL_warn("Found unsecure configuration: nsSSL3: on; "
  818. "We strongly recommend to disable nsSSL3 in %s.", configDN);
  819. if (enableTLS1) {
  820. if (slapdNSSVersions.min >= CURRENT_DEFAULT_SSL_VERSION) {
  821. slapd_SSL_warn("Configured range: min: %s, max: %s; "
  822. "but both nsSSL3 and nsTLS1 are on. "
  823. "Respect the supported range.",
  824. mymin, mymax);
  825. enableSSL3 = PR_FALSE;
  826. }
  827. if (slapdNSSVersions.max < CURRENT_DEFAULT_SSL_VERSION) {
  828. slapd_SSL_warn("Configured range: min: %s, max: %s; "
  829. "but both nsSSL3 and nsTLS1 are on. "
  830. "Resetting the max to the supported max SSL version: %s.",
  831. mymin, mymax, emax);
  832. slapdNSSVersions.max = enabledNSSVersions.max;
  833. }
  834. } else {
  835. /* nsTLS1 is explicitly set to off. */
  836. if (enabledNSSVersions.min >= CURRENT_DEFAULT_SSL_VERSION) {
  837. slapd_SSL_warn("Supported range: min: %s, max: %s; "
  838. "but nsSSL3 is on and nsTLS1 is off. "
  839. "Respect the supported range.",
  840. emin, emax);
  841. slapdNSSVersions.min = SSLVGreater(slapdNSSVersions.min, enabledNSSVersions.min);
  842. enableSSL3 = PR_FALSE;
  843. enableTLS1 = PR_TRUE;
  844. } else if (slapdNSSVersions.min >= CURRENT_DEFAULT_SSL_VERSION) {
  845. slapd_SSL_warn("Configured range: min: %s, max: %s; "
  846. "but nsSSL3 is on and nsTLS1 is off. "
  847. "Respect the configured range.",
  848. mymin, mymax);
  849. enableSSL3 = PR_FALSE;
  850. enableTLS1 = PR_TRUE;
  851. } else if (slapdNSSVersions.min < CURRENT_DEFAULT_SSL_VERSION) {
  852. slapd_SSL_warn("Too low configured range: min: %s, max: %s; "
  853. "We strongly recommend to set sslVersionMin higher than %s.",
  854. mymin, mymax, DEFVERSION);
  855. } else {
  856. /*
  857. * slapdNSSVersions.min < SSL_LIBRARY_VERSION_TLS_1_0 &&
  858. * slapdNSSVersions.max >= SSL_LIBRARY_VERSION_TLS_1_1
  859. */
  860. slapd_SSL_warn("Configured range: min: %s, max: %s; "
  861. "but nsSSL3 is on and nsTLS1 is off. "
  862. "Respect the configured range.",
  863. mymin, mymax);
  864. enableTLS1 = PR_TRUE;
  865. }
  866. }
  867. } else {
  868. if (enableTLS1) {
  869. if (enabledNSSVersions.max < CURRENT_DEFAULT_SSL_VERSION) {
  870. /* TLS1 is on, but TLS1 is not supported by NSS. */
  871. slapd_SSL_warn("Supported range: min: %s, max: %s; "
  872. "Setting the version range based upon the supported range.",
  873. emin, emax);
  874. slapdNSSVersions.max = enabledNSSVersions.max;
  875. slapdNSSVersions.min = enabledNSSVersions.min;
  876. enableSSL3 = PR_TRUE;
  877. enableTLS1 = PR_FALSE;
  878. } else if ((slapdNSSVersions.max < CURRENT_DEFAULT_SSL_VERSION) ||
  879. (slapdNSSVersions.min < CURRENT_DEFAULT_SSL_VERSION)) {
  880. slapdNSSVersions.max = enabledNSSVersions.max;
  881. slapdNSSVersions.min = SSLVGreater(CURRENT_DEFAULT_SSL_VERSION, enabledNSSVersions.min);
  882. slapd_SSL_warn("nsTLS1 is on, but the version range is lower than \"%s\"; "
  883. "Configuring the version range as default min: %s, max: %s.",
  884. DEFVERSION, DEFVERSION, emax);
  885. } else {
  886. /*
  887. * slapdNSSVersions.min >= SSL_LIBRARY_VERSION_TLS_1_0 &&
  888. * slapdNSSVersions.max >= SSL_LIBRARY_VERSION_TLS_1_0
  889. */
  890. ;
  891. }
  892. } else {
  893. slapd_SSL_warn("Supported range: min: %s, max: %s; "
  894. "Respect the configured range.",
  895. emin, emax);
  896. /* nsTLS1 is explicitly set to off. */
  897. if (slapdNSSVersions.min >= CURRENT_DEFAULT_SSL_VERSION) {
  898. enableTLS1 = PR_TRUE;
  899. } else if (slapdNSSVersions.max < CURRENT_DEFAULT_SSL_VERSION) {
  900. enableSSL3 = PR_TRUE;
  901. } else {
  902. /*
  903. * slapdNSSVersions.min < SSL_LIBRARY_VERSION_TLS_1_0 &&
  904. * slapdNSSVersions.max >= SSL_LIBRARY_VERSION_TLS_1_0
  905. */
  906. enableSSL3 = PR_TRUE;
  907. enableTLS1 = PR_TRUE;
  908. }
  909. }
  910. }
  911. }
  912. #endif
  913. /*
  914. * slapd_nss_init() is always called from main(), even if we do not
  915. * plan to listen on a secure port. If config_available is 0, the
  916. * config. entries from dse.ldif are NOT available (used only when
  917. * running in referral mode).
  918. * As of DS6.1, the init_ssl flag passed is ignored.
  919. *
  920. * richm 20070126 - By default now we put the key/cert db files
  921. * in an instance specific directory (the certdir directory) so
  922. * we do not need a prefix any more.
  923. */
  924. int
  925. slapd_nss_init(int init_ssl, int config_available)
  926. {
  927. SECStatus secStatus;
  928. PRErrorCode errorCode;
  929. int rv = 0;
  930. int len = 0;
  931. int create_certdb = 0;
  932. PRUint32 nssFlags = 0;
  933. char *certdir;
  934. char *certdb_file_name = NULL;
  935. char *keydb_file_name = NULL;
  936. char *secmoddb_file_name = NULL;
  937. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  938. char emin[VERSION_STR_LENGTH], emax[VERSION_STR_LENGTH];
  939. /* Get the range of the supported SSL version */
  940. SSL_VersionRangeGetSupported(ssl_variant_stream, &enabledNSSVersions);
  941. (void) slapi_getSSLVersion_str(enabledNSSVersions.min, emin, sizeof(emin));
  942. (void) slapi_getSSLVersion_str(enabledNSSVersions.max, emax, sizeof(emax));
  943. slapi_log_error(SLAPI_LOG_CONFIG, "SSL Initialization",
  944. "supported range by NSS: min: %s, max: %s\n",
  945. emin, emax);
  946. #endif
  947. /* set in slapd_bootstrap_config,
  948. thus certdir is available even if config_available is false */
  949. certdir = config_get_certdir();
  950. /* make sure path does not end in the path separator character */
  951. len = strlen(certdir);
  952. if (certdir[len-1] == '/' || certdir[len-1] == '\\') {
  953. certdir[len-1] = '\0';
  954. }
  955. /* If the server is configured to use SSL, we must have a key and cert db */
  956. if (config_get_security()) {
  957. warn_if_no_cert_file(certdir, 0);
  958. warn_if_no_key_file(certdir, 0);
  959. } else { /* otherwise, NSS will create empty databases */
  960. /* we open the key/cert db in rw mode, so make sure the directory
  961. is writable */
  962. if (PR_SUCCESS != PR_Access(certdir, PR_ACCESS_WRITE_OK)) {
  963. char *serveruser = "unknown";
  964. serveruser = config_get_localuser();
  965. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  966. "Warning: The key/cert database directory [%s] is not writable by "
  967. "the server uid [%s]: initialization likely to fail.\n",
  968. certdir, serveruser);
  969. slapi_ch_free_string(&serveruser);
  970. }
  971. }
  972. /* Check if we have a certdb already. If not, set a flag that we are
  973. * going to create one so we can set the appropriate permissions on it. */
  974. if (warn_if_no_cert_file(certdir, 1) || warn_if_no_key_file(certdir, 1)) {
  975. create_certdb = 1;
  976. }
  977. /******** Initialise NSS *********/
  978. nssFlags &= (~NSS_INIT_READONLY);
  979. slapd_pk11_configurePKCS11(NULL, NULL, tokPBE, ptokPBE, NULL, NULL, NULL, NULL, 0, 0 );
  980. secStatus = NSS_Initialize(certdir, NULL, NULL, "secmod.db", nssFlags);
  981. dongle_file_name = PR_smprintf("%s/pin.txt", certdir);
  982. if (secStatus != SECSuccess) {
  983. errorCode = PR_GetError();
  984. slapd_SSL_warn("Security Initialization: NSS initialization failed ("
  985. SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
  986. "certdir: %s",
  987. errorCode, slapd_pr_strerror(errorCode), certdir);
  988. rv = -1;
  989. }
  990. if(SSLPLCY_Install() != PR_SUCCESS) {
  991. errorCode = PR_GetError();
  992. slapd_SSL_warn("Security Initialization: Unable to set SSL export policy ("
  993. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  994. errorCode, slapd_pr_strerror(errorCode));
  995. return -1;
  996. }
  997. /* NSS creates the certificate db files with a mode of 600. There
  998. * is no way to pass in a mode to use for creation to NSS, so we
  999. * need to modify it after creation. We need to allow read and
  1000. * write permission to the group so the certs can be managed via
  1001. * the console/adminserver. */
  1002. if (create_certdb) {
  1003. certdb_file_name = slapi_ch_smprintf("%s/cert8.db", certdir);
  1004. keydb_file_name = slapi_ch_smprintf("%s/key3.db", certdir);
  1005. secmoddb_file_name = slapi_ch_smprintf("%s/secmod.db", certdir);
  1006. if(chmod(certdb_file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )){
  1007. LDAPDebug(LDAP_DEBUG_ANY, "slapd_nss_init: chmod failed for file %s error (%d) %s.\n",
  1008. certdb_file_name, errno, slapd_system_strerror(errno));
  1009. }
  1010. if(chmod(keydb_file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )){
  1011. LDAPDebug(LDAP_DEBUG_ANY, "slapd_nss_init: chmod failed for file %s error (%d) %s.\n",
  1012. keydb_file_name, errno, slapd_system_strerror(errno));
  1013. }
  1014. if(chmod(secmoddb_file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )){
  1015. LDAPDebug(LDAP_DEBUG_ANY, "slapd_nss_init: chmod failed for file %s error (%d) %s.\n",
  1016. secmoddb_file_name, errno, slapd_system_strerror(errno));
  1017. }
  1018. }
  1019. /****** end of NSS Initialization ******/
  1020. _nss_initialized = 1;
  1021. slapi_ch_free_string(&certdb_file_name);
  1022. slapi_ch_free_string(&keydb_file_name);
  1023. slapi_ch_free_string(&secmoddb_file_name);
  1024. slapi_ch_free_string(&certdir);
  1025. return rv;
  1026. }
  1027. static int
  1028. svrcore_setup()
  1029. {
  1030. PRErrorCode errorCode;
  1031. int rv = 0;
  1032. SVRCOREStdPinObj *StdPinObj;
  1033. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  1034. if (StdPinObj) {
  1035. return 0; /* already registered */
  1036. }
  1037. if ( SVRCORE_CreateStdPinObj(&StdPinObj, dongle_file_name, PR_TRUE) != SVRCORE_Success) {
  1038. errorCode = PR_GetError();
  1039. slapd_SSL_warn("Security Initialization: Unable to create PinObj ("
  1040. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1041. errorCode, slapd_pr_strerror(errorCode));
  1042. return -1;
  1043. }
  1044. SVRCORE_RegisterPinObj((SVRCOREPinObj *)StdPinObj);
  1045. return rv;
  1046. }
  1047. /*
  1048. * slapd_ssl_init() is called from main() if we plan to listen
  1049. * on a secure port.
  1050. */
  1051. int
  1052. slapd_ssl_init()
  1053. {
  1054. PRErrorCode errorCode;
  1055. char ** family_list;
  1056. char *val = NULL;
  1057. PK11SlotInfo *slot;
  1058. Slapi_Entry *entry = NULL;
  1059. /* Get general information */
  1060. getConfigEntry( configDN, &entry );
  1061. val = slapi_entry_attr_get_charptr( entry, "nssslSessionTimeout" );
  1062. ciphers = slapi_entry_attr_get_charptr( entry, "nsssl3ciphers" );
  1063. /* We are currently using the value of sslSessionTimeout
  1064. for ssl3SessionTimeout, see SSL_ConfigServerSessionIDCache() */
  1065. /* Note from Tom Weinstein on the meaning of the timeout:
  1066. Timeouts are in seconds. '0' means use the default, which is
  1067. 24hrs for SSL3 and 100 seconds for SSL2.
  1068. */
  1069. if(!val) {
  1070. errorCode = PR_GetError();
  1071. slapd_SSL_warn("Security Initialization: Failed to retrieve SSL "
  1072. "configuration information ("
  1073. SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
  1074. "nssslSessionTimeout: %s ",
  1075. errorCode, slapd_pr_strerror(errorCode),
  1076. (val ? "found" : "not found"));
  1077. slapi_ch_free((void **) &val);
  1078. slapi_ch_free((void **) &ciphers);
  1079. freeConfigEntry( &entry );
  1080. return -1;
  1081. }
  1082. stimeout = atoi(val);
  1083. slapi_ch_free((void **) &val);
  1084. if (svrcore_setup()) {
  1085. freeConfigEntry( &entry );
  1086. return -1;
  1087. }
  1088. if ((family_list = getChildren(configDN))) {
  1089. char **family;
  1090. char *token;
  1091. char *activation;
  1092. for (family = family_list; *family; family++) {
  1093. token = NULL;
  1094. activation = NULL;
  1095. freeConfigEntry( &entry );
  1096. getConfigEntry( *family, &entry );
  1097. if ( entry == NULL ) {
  1098. continue;
  1099. }
  1100. activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
  1101. if((!activation) || (!PL_strcasecmp(activation, "off"))) {
  1102. /* this family was turned off, goto next */
  1103. slapi_ch_free((void **) &activation);
  1104. continue;
  1105. }
  1106. slapi_ch_free((void **) &activation);
  1107. token = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  1108. if ( token ) {
  1109. if (!PL_strcasecmp(token, "internal") ||
  1110. !PL_strcasecmp(token, "internal (software)")) {
  1111. slot = slapd_pk11_getInternalKeySlot();
  1112. } else {
  1113. slot = slapd_pk11_findSlotByName(token);
  1114. }
  1115. } else {
  1116. errorCode = PR_GetError();
  1117. slapd_SSL_warn("Security Initialization: Unable to get token ("
  1118. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1119. errorCode, slapd_pr_strerror(errorCode));
  1120. freeChildren(family_list);
  1121. freeConfigEntry( &entry );
  1122. return -1;
  1123. }
  1124. slapi_ch_free((void **) &token);
  1125. if (!slot) {
  1126. errorCode = PR_GetError();
  1127. slapd_SSL_warn("Security Initialization: Unable to find slot ("
  1128. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1129. errorCode, slapd_pr_strerror(errorCode));
  1130. freeChildren(family_list);
  1131. freeConfigEntry( &entry );
  1132. return -1;
  1133. }
  1134. /* authenticate */
  1135. if (slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess) {
  1136. errorCode = PR_GetError();
  1137. slapd_SSL_warn("Security Initialization: Unable to authenticate ("
  1138. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1139. errorCode, slapd_pr_strerror(errorCode));
  1140. freeChildren(family_list);
  1141. freeConfigEntry( &entry );
  1142. return -1;
  1143. }
  1144. }
  1145. freeChildren( family_list );
  1146. freeConfigEntry( &entry );
  1147. }
  1148. freeConfigEntry( &entry );
  1149. /* Introduce a way of knowing whether slapd_ssl_init has
  1150. * already been executed. */
  1151. _security_library_initialized = 1;
  1152. return 0;
  1153. }
  1154. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1155. /*
  1156. * val: sslVersionMin/Max value set in cn=encription,cn=config (INPUT)
  1157. * rval: Corresponding value to set SSLVersionRange (OUTPUT)
  1158. * ismin: True if val is sslVersionMin value
  1159. */
  1160. #define SSLSTR "ssl"
  1161. #define SSLLEN (sizeof(SSLSTR) - 1)
  1162. #define TLSSTR "tls"
  1163. #define TLSLEN (sizeof(TLSSTR) - 1)
  1164. static int
  1165. set_NSS_version(char *val, PRUint16 *rval, int ismin)
  1166. {
  1167. char *vp, *endp;
  1168. int vnum;
  1169. char emin[VERSION_STR_LENGTH], emax[VERSION_STR_LENGTH];
  1170. if (NULL == rval) {
  1171. return 1;
  1172. }
  1173. (void) slapi_getSSLVersion_str(enabledNSSVersions.min, emin, sizeof(emin));
  1174. (void) slapi_getSSLVersion_str(enabledNSSVersions.max, emax, sizeof(emax));
  1175. if (!strncasecmp(val, SSLSTR, SSLLEN)) { /* ssl# */
  1176. vp = val + SSLLEN;
  1177. vnum = strtol(vp, &endp, 10);
  1178. if (2 == vnum) {
  1179. if (ismin) {
  1180. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_2) {
  1181. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1182. "\"%s\" is lower than the supported version; "
  1183. "the default value \"%s\" is used.",
  1184. val, emin);
  1185. (*rval) = enabledNSSVersions.min;
  1186. } else {
  1187. (*rval) = SSL_LIBRARY_VERSION_2;
  1188. }
  1189. } else {
  1190. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_2) {
  1191. /* never happens */
  1192. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1193. "\"%s\" is higher than the supported version; "
  1194. "the default value \"%s\" is used.",
  1195. val, emax);
  1196. (*rval) = enabledNSSVersions.max;
  1197. } else {
  1198. (*rval) = SSL_LIBRARY_VERSION_2;
  1199. }
  1200. }
  1201. } else if (3 == vnum) {
  1202. if (ismin) {
  1203. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_3_0) {
  1204. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1205. "\"%s\" is lower than the supported version; "
  1206. "the default value \"%s\" is used.",
  1207. val, emin);
  1208. (*rval) = enabledNSSVersions.min;
  1209. } else {
  1210. (*rval) = SSL_LIBRARY_VERSION_3_0;
  1211. }
  1212. } else {
  1213. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_3_0) {
  1214. /* never happens */
  1215. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1216. "\"%s\" is higher than the supported version; "
  1217. "the default value \"%s\" is used.",
  1218. val, emax);
  1219. (*rval) = enabledNSSVersions.max;
  1220. } else {
  1221. (*rval) = SSL_LIBRARY_VERSION_3_0;
  1222. }
  1223. }
  1224. } else {
  1225. if (ismin) {
  1226. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1227. "\"%s\" is invalid; the default value \"%s\" is used.",
  1228. val, emin);
  1229. (*rval) = enabledNSSVersions.min;
  1230. } else {
  1231. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1232. "\"%s\" is invalid; the default value \"%s\" is used.",
  1233. val, emax);
  1234. (*rval) = enabledNSSVersions.max;
  1235. }
  1236. }
  1237. } else if (!strncasecmp(val, TLSSTR, TLSLEN)) { /* tls# */
  1238. float tlsv;
  1239. vp = val + TLSLEN;
  1240. sscanf(vp, "%4f", &tlsv);
  1241. if (tlsv < 1.1) { /* TLS1.0 */
  1242. if (ismin) {
  1243. if (enabledNSSVersions.min > CURRENT_DEFAULT_SSL_VERSION) {
  1244. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1245. "\"%s\" is lower than the supported version; "
  1246. "the default value \"%s\" is used.",
  1247. val, emin);
  1248. (*rval) = enabledNSSVersions.min;
  1249. } else {
  1250. (*rval) = CURRENT_DEFAULT_SSL_VERSION;
  1251. }
  1252. } else {
  1253. if (enabledNSSVersions.max < CURRENT_DEFAULT_SSL_VERSION) {
  1254. /* never happens */
  1255. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1256. "\"%s\" is higher than the supported version; "
  1257. "the default value \"%s\" is used.",
  1258. val, emax);
  1259. (*rval) = enabledNSSVersions.max;
  1260. } else {
  1261. (*rval) = CURRENT_DEFAULT_SSL_VERSION;
  1262. }
  1263. }
  1264. } else if (tlsv < 1.2) { /* TLS1.1 */
  1265. if (ismin) {
  1266. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_TLS_1_1) {
  1267. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1268. "\"%s\" is lower than the supported version; "
  1269. "the default value \"%s\" is used.",
  1270. val, emin);
  1271. (*rval) = enabledNSSVersions.min;
  1272. } else {
  1273. (*rval) = SSL_LIBRARY_VERSION_TLS_1_1;
  1274. }
  1275. } else {
  1276. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_TLS_1_1) {
  1277. /* never happens */
  1278. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1279. "\"%s\" is higher than the supported version; "
  1280. "the default value \"%s\" is used.",
  1281. val, emax);
  1282. (*rval) = enabledNSSVersions.max;
  1283. } else {
  1284. (*rval) = SSL_LIBRARY_VERSION_TLS_1_1;
  1285. }
  1286. }
  1287. } else if (tlsv < 1.3) { /* TLS1.2 */
  1288. #if defined(NSS_TLS12)
  1289. if (ismin) {
  1290. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_TLS_1_2) {
  1291. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1292. "\"%s\" is lower than the supported version; "
  1293. "the default value \"%s\" is used.",
  1294. val, emin);
  1295. (*rval) = enabledNSSVersions.min;
  1296. } else {
  1297. (*rval) = SSL_LIBRARY_VERSION_TLS_1_2;
  1298. }
  1299. } else {
  1300. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_TLS_1_2) {
  1301. /* never happens */
  1302. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1303. "\"%s\" is higher than the supported version; "
  1304. "the default value \"%s\" is used.",
  1305. val, emax);
  1306. (*rval) = enabledNSSVersions.max;
  1307. } else {
  1308. (*rval) = SSL_LIBRARY_VERSION_TLS_1_2;
  1309. }
  1310. }
  1311. #endif
  1312. } else { /* Specified TLS is newer than supported */
  1313. if (ismin) {
  1314. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1315. "\"%s\" is out of the range of the supported version; "
  1316. "the default value \"%s\" is used.",
  1317. val, emin);
  1318. (*rval) = enabledNSSVersions.min;
  1319. } else {
  1320. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1321. "\"%s\" is out of the range of the supported version; "
  1322. "the default value \"%s\" is used.",
  1323. val, emax);
  1324. (*rval) = enabledNSSVersions.max;
  1325. }
  1326. }
  1327. } else {
  1328. if (ismin) {
  1329. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1330. "\"%s\" is invalid; the default value \"%s\" is used.",
  1331. val, emin);
  1332. (*rval) = enabledNSSVersions.min;
  1333. } else {
  1334. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1335. "\"%s\" is invalid; the default value \"%s\" is used.",
  1336. val, emax);
  1337. (*rval) = enabledNSSVersions.max;
  1338. }
  1339. }
  1340. return 0;
  1341. }
  1342. #undef SSLSTR
  1343. #undef SSLLEN
  1344. #undef TLSSTR
  1345. #undef TLSLEN
  1346. #endif
  1347. int
  1348. slapd_ssl_init2(PRFileDesc **fd, int startTLS)
  1349. {
  1350. PRFileDesc *pr_sock, *sock = (*fd);
  1351. PRErrorCode errorCode;
  1352. SECStatus rv = SECFailure;
  1353. char ** family_list;
  1354. CERTCertificate *cert = NULL;
  1355. SECKEYPrivateKey *key = NULL;
  1356. char errorbuf[BUFSIZ];
  1357. char *val = NULL;
  1358. char *default_val = NULL;
  1359. int nFamilies = 0;
  1360. SECStatus sslStatus;
  1361. int slapd_SSLclientAuth;
  1362. char* tmpDir;
  1363. Slapi_Entry *e = NULL;
  1364. PRBool fipsMode = PR_FALSE;
  1365. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1366. PRUint16 NSSVersionMin = enabledNSSVersions.min;
  1367. PRUint16 NSSVersionMax = enabledNSSVersions.max;
  1368. char mymin[VERSION_STR_LENGTH], mymax[VERSION_STR_LENGTH];
  1369. char newmax[VERSION_STR_LENGTH];
  1370. #endif
  1371. char cipher_string[1024];
  1372. int allowweakcipher = CIPHER_SET_DEFAULTWEAKCIPHER;
  1373. /* turn off the PKCS11 pin interactive mode */
  1374. SVRCOREStdPinObj *StdPinObj;
  1375. if (svrcore_setup()) {
  1376. return 1;
  1377. }
  1378. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  1379. SVRCORE_SetStdPinInteractive(StdPinObj, PR_FALSE);
  1380. errorbuf[0] = '\0';
  1381. /*
  1382. * Cipher preferences must be set before any sslSocket is created
  1383. * for such sockets to take preferences into account.
  1384. */
  1385. getConfigEntry(configDN, &e);
  1386. if (e == NULL) {
  1387. slapd_SSL_warn("Security Initialization: Failed get config entry %s", configDN);
  1388. return 1;
  1389. }
  1390. val = slapi_entry_attr_get_charptr(e, "allowWeakCipher");
  1391. if (val) {
  1392. if (!PL_strcasecmp(val, "off") || !PL_strcasecmp(val, "false") ||
  1393. !PL_strcmp(val, "0") || !PL_strcasecmp(val, "no")) {
  1394. allowweakcipher = CIPHER_SET_DISALLOWWEAKCIPHER;
  1395. } else if (!PL_strcasecmp(val, "on") || !PL_strcasecmp(val, "true") ||
  1396. !PL_strcmp(val, "1") || !PL_strcasecmp(val, "yes")) {
  1397. allowweakcipher = CIPHER_SET_ALLOWWEAKCIPHER;
  1398. } else {
  1399. slapd_SSL_warn("The value of allowWeakCipher \"%s\" in %s is invalid.",
  1400. "Ignoring it and set it to default.", val, configDN);
  1401. }
  1402. }
  1403. slapi_ch_free((void **) &val);
  1404. /* Set SSL cipher preferences */
  1405. *cipher_string = 0;
  1406. if(ciphers && (*ciphers) && PL_strcmp(ciphers, "blank"))
  1407. PL_strncpyz(cipher_string, ciphers, sizeof(cipher_string));
  1408. slapi_ch_free((void **) &ciphers);
  1409. if ( NULL != (val = _conf_setciphers(cipher_string, allowweakcipher)) ) {
  1410. errorCode = PR_GetError();
  1411. slapd_SSL_warn("Security Initialization: Failed to set SSL cipher "
  1412. "preference information: %s (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1413. val, errorCode, slapd_pr_strerror(errorCode));
  1414. slapi_ch_free((void **) &val);
  1415. }
  1416. freeConfigEntry(&e);
  1417. /* Import pr fd into SSL */
  1418. pr_sock = SSL_ImportFD( NULL, sock );
  1419. if( pr_sock == (PRFileDesc *)NULL ) {
  1420. errorCode = PR_GetError();
  1421. slapd_SSL_warn("Security Initialization: Failed to import NSPR "
  1422. "fd into SSL (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1423. errorCode, slapd_pr_strerror(errorCode));
  1424. return 1;
  1425. }
  1426. (*fd) = pr_sock;
  1427. /* Step / Three.6 /
  1428. * - If in FIPS mode, authenticate to the token before
  1429. * doing anything else
  1430. */
  1431. {
  1432. PK11SlotInfo *slot = slapd_pk11_getInternalSlot();
  1433. if (!slot) {
  1434. errorCode = PR_GetError();
  1435. slapd_SSL_warn("Security Initialization: Unable to get internal slot ("
  1436. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1437. errorCode, slapd_pr_strerror(errorCode));
  1438. return -1;
  1439. }
  1440. if(slapd_pk11_isFIPS()) {
  1441. if(slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess) {
  1442. errorCode = PR_GetError();
  1443. slapd_SSL_warn("Security Initialization: Unable to authenticate ("
  1444. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1445. errorCode, slapd_pr_strerror(errorCode));
  1446. return -1;
  1447. }
  1448. fipsMode = PR_TRUE;
  1449. /* FIPS does not like to use SSLv3 */
  1450. enableSSL3 = PR_FALSE;
  1451. }
  1452. slapd_pk11_setSlotPWValues(slot, 0, 0);
  1453. }
  1454. /*
  1455. * Now, get the complete list of cipher families. Each family
  1456. * has a token name and personality name which we'll use to find
  1457. * appropriate keys and certs, and call SSL_ConfigSecureServer
  1458. * with.
  1459. */
  1460. if((family_list = getChildren(configDN))) {
  1461. char **family;
  1462. char cert_name[1024];
  1463. char *token;
  1464. char *personality;
  1465. char *activation;
  1466. for (family = family_list; *family; family++) {
  1467. token = NULL;
  1468. personality = NULL;
  1469. activation = NULL;
  1470. getConfigEntry( *family, &e );
  1471. if ( e == NULL ) {
  1472. continue;
  1473. }
  1474. activation = slapi_entry_attr_get_charptr( e, "nssslactivation" );
  1475. if((!activation) || (!PL_strcasecmp(activation, "off"))) {
  1476. /* this family was turned off, goto next */
  1477. slapi_ch_free((void **) &activation);
  1478. freeConfigEntry( &e );
  1479. continue;
  1480. }
  1481. slapi_ch_free((void **) &activation);
  1482. token = slapi_entry_attr_get_charptr( e, "nsssltoken" );
  1483. personality = slapi_entry_attr_get_charptr( e, "nssslpersonalityssl" );
  1484. if( token && personality ) {
  1485. if( !PL_strcasecmp(token, "internal") ||
  1486. !PL_strcasecmp(token, "internal (software)") )
  1487. PL_strncpyz(cert_name, personality, sizeof(cert_name));
  1488. else
  1489. /* external PKCS #11 token - attach token name */
  1490. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1491. }
  1492. else {
  1493. errorCode = PR_GetError();
  1494. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1495. "family information. Missing nsssltoken or"
  1496. "nssslpersonalityssl in %s ("
  1497. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1498. *family, errorCode, slapd_pr_strerror(errorCode));
  1499. slapi_ch_free((void **) &token);
  1500. slapi_ch_free((void **) &personality);
  1501. freeConfigEntry( &e );
  1502. continue;
  1503. }
  1504. slapi_ch_free((void **) &token);
  1505. /* Step Four -- Locate the server certificate */
  1506. cert = slapd_pk11_findCertFromNickname(cert_name, NULL);
  1507. if (cert == NULL) {
  1508. errorCode = PR_GetError();
  1509. slapd_SSL_warn("Security Initialization: Can't find "
  1510. "certificate (%s) for family %s ("
  1511. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1512. cert_name, *family,
  1513. errorCode, slapd_pr_strerror(errorCode));
  1514. }
  1515. /* Step Five -- Get the private key from cert */
  1516. if( cert != NULL )
  1517. key = slapd_pk11_findKeyByAnyCert(cert, NULL);
  1518. if (key == NULL) {
  1519. errorCode = PR_GetError();
  1520. slapd_SSL_warn("Security Initialization: Unable to retrieve "
  1521. "private key for cert %s of family %s ("
  1522. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1523. cert_name, *family,
  1524. errorCode, slapd_pr_strerror(errorCode));
  1525. slapi_ch_free((void **) &personality);
  1526. CERT_DestroyCertificate(cert);
  1527. cert = NULL;
  1528. freeConfigEntry( &e );
  1529. continue;
  1530. }
  1531. /* Step Six -- Configure Secure Server Mode */
  1532. if(pr_sock) {
  1533. SECCertificateUsage returnedUsages;
  1534. if (config_get_validate_cert_switch() == SLAPD_VALIDATE_CERT_OFF) {
  1535. /* If we're set to ignore certificate verification issues,
  1536. * just skip performing verification. */
  1537. rv = SECSuccess;
  1538. } else {
  1539. /* Check if the certificate is valid. */
  1540. rv = CERT_VerifyCertificateNow(
  1541. CERT_GetDefaultCertDB(), cert, PR_TRUE,
  1542. certificateUsageSSLServer,
  1543. SSL_RevealPinArg(pr_sock),
  1544. &returnedUsages);
  1545. if (rv != SECSuccess) {
  1546. /* Log warning */
  1547. errorCode = PR_GetError();
  1548. slapd_SSL_warn("CERT_VerifyCertificateNow: "
  1549. "verify certificate failed "
  1550. "for cert %s of family %s ("
  1551. SLAPI_COMPONENT_NAME_NSPR
  1552. " error %d - %s)",
  1553. cert_name, *family, errorCode,
  1554. slapd_pr_strerror(errorCode));
  1555. /* If we're set to only warn, go ahead and
  1556. * override rv to allow us to start up. */
  1557. if (config_get_validate_cert_switch() == SLAPD_VALIDATE_CERT_WARN) {
  1558. rv = SECSuccess;
  1559. }
  1560. }
  1561. }
  1562. if (SECSuccess == rv) {
  1563. if( slapd_pk11_fortezzaHasKEA(cert) == PR_TRUE ) {
  1564. rv = SSL_ConfigSecureServer(*fd, cert, key, kt_fortezza);
  1565. }
  1566. else {
  1567. rv = SSL_ConfigSecureServer(*fd, cert, key, kt_rsa);
  1568. }
  1569. if (SECSuccess != rv) {
  1570. errorCode = PR_GetError();
  1571. slapd_SSL_warn("ConfigSecureServer: "
  1572. "Server key/certificate is "
  1573. "bad for cert %s of family %s ("
  1574. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1575. cert_name, *family, errorCode,
  1576. slapd_pr_strerror(errorCode));
  1577. }
  1578. }
  1579. }
  1580. if (cert) {
  1581. CERT_DestroyCertificate(cert);
  1582. cert = NULL;
  1583. }
  1584. if (key) {
  1585. slapd_pk11_DestroyPrivateKey(key);
  1586. key = NULL;
  1587. }
  1588. slapi_ch_free((void **) &personality);
  1589. if (SECSuccess != rv) {
  1590. freeConfigEntry( &e );
  1591. continue;
  1592. }
  1593. nFamilies++;
  1594. freeConfigEntry( &e );
  1595. }
  1596. freeChildren( family_list );
  1597. }
  1598. if ( !nFamilies ) {
  1599. slapd_SSL_error("None of the cipher are valid");
  1600. return -1;
  1601. }
  1602. /* Step Seven -- Configure Server Session ID Cache */
  1603. tmpDir = slapd_get_tmp_dir();
  1604. slapi_log_error(SLAPI_LOG_TRACE,
  1605. "slapd_ssl_init2", "tmp dir = %s\n", tmpDir);
  1606. rv = SSL_ConfigServerSessionIDCache(0, stimeout, stimeout, tmpDir);
  1607. slapi_ch_free_string(&tmpDir);
  1608. if (rv) {
  1609. errorCode = PR_GetError();
  1610. if (errorCode == ENOSPC) {
  1611. slapd_SSL_error("Config of server nonce cache failed, "
  1612. "out of disk space! Make more room in /tmp "
  1613. "and try again. (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1614. errorCode, slapd_pr_strerror(errorCode));
  1615. } else {
  1616. slapd_SSL_error("Config of server nonce cache failed (error %d - %s)",
  1617. errorCode, slapd_pr_strerror(errorCode));
  1618. }
  1619. return rv;
  1620. }
  1621. sslStatus = SSL_OptionSet(pr_sock, SSL_SECURITY, PR_TRUE);
  1622. if (sslStatus != SECSuccess) {
  1623. errorCode = PR_GetError();
  1624. slapd_SSL_warn("Security Initialization: Failed to enable security "
  1625. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1626. errorCode, slapd_pr_strerror(errorCode));
  1627. return -1;
  1628. }
  1629. /* Explicitly disabling SSL2 - NGK */
  1630. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL2, enableSSL2);
  1631. if (sslStatus != SECSuccess) {
  1632. errorCode = PR_GetError();
  1633. slapd_SSL_warn("Security Initialization: Failed to %s SSLv2 "
  1634. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1635. enableSSL2 ? "enable" : "disable",
  1636. errorCode, slapd_pr_strerror(errorCode));
  1637. return -1;
  1638. }
  1639. /* Retrieve the SSL Client Authentication status from cn=config */
  1640. /* Set a default value if no value found */
  1641. getConfigEntry( configDN, &e );
  1642. val = NULL;
  1643. if ( e != NULL ) {
  1644. val = slapi_entry_attr_get_charptr( e, "nssslclientauth" );
  1645. }
  1646. if( !val ) {
  1647. errorCode = PR_GetError();
  1648. slapd_SSL_warn("Security Initialization: Cannot get SSL Client "
  1649. "Authentication status. No nsslclientauth in %s ("
  1650. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1651. configDN, errorCode, slapd_pr_strerror(errorCode));
  1652. switch( SLAPD_SSLCLIENTAUTH_DEFAULT ) {
  1653. case SLAPD_SSLCLIENTAUTH_OFF:
  1654. default_val = "off";
  1655. break;
  1656. case SLAPD_SSLCLIENTAUTH_ALLOWED:
  1657. default_val = "allowed";
  1658. break;
  1659. case SLAPD_SSLCLIENTAUTH_REQUIRED:
  1660. default_val = "required";
  1661. break;
  1662. default:
  1663. default_val = "allowed";
  1664. break;
  1665. }
  1666. val = default_val;
  1667. }
  1668. if( config_set_SSLclientAuth( "nssslclientauth", val, errorbuf,
  1669. CONFIG_APPLY ) != LDAP_SUCCESS ) {
  1670. errorCode = PR_GetError();
  1671. slapd_SSL_warn("Security Initialization: Cannot set SSL Client "
  1672. "Authentication status to \"%s\", error (%s). "
  1673. "Supported values are \"off\", \"allowed\" "
  1674. "and \"required\". (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1675. val, errorbuf, errorCode, slapd_pr_strerror(errorCode));
  1676. }
  1677. if (val != default_val) {
  1678. slapi_ch_free_string(&val);
  1679. }
  1680. if ( e != NULL ) {
  1681. val = slapi_entry_attr_get_charptr( e, "nsSSL3" );
  1682. if ( val ) {
  1683. if ( !PL_strcasecmp( val, "off" ) ) {
  1684. enableSSL3 = PR_FALSE;
  1685. } else if ( !PL_strcasecmp( val, "on" ) ) {
  1686. enableSSL3 = PR_TRUE;
  1687. } else {
  1688. enableSSL3 = slapi_entry_attr_get_bool( e, "nsSSL3" );
  1689. }
  1690. if ( fipsMode && enableSSL3 ) {
  1691. slapd_SSL_warn("Security Initialization: FIPS mode is enabled and "
  1692. "nsSSL3 explicitly set to on - SSLv3 is not approved "
  1693. "for use in FIPS mode - SSLv3 will be disabled - if "
  1694. "you want to use SSLv3, you must use modutil to "
  1695. "disable FIPS in the internal token.");
  1696. enableSSL3 = PR_FALSE;
  1697. }
  1698. }
  1699. slapi_ch_free_string( &val );
  1700. val = slapi_entry_attr_get_charptr( e, "nsTLS1" );
  1701. if ( val ) {
  1702. if ( !PL_strcasecmp( val, "off" ) ) {
  1703. enableTLS1 = PR_FALSE;
  1704. } else if ( !PL_strcasecmp( val, "on" ) ) {
  1705. enableTLS1 = PR_TRUE;
  1706. } else {
  1707. enableTLS1 = slapi_entry_attr_get_bool( e, "nsTLS1" );
  1708. }
  1709. } else if (enabledNSSVersions.max >= CURRENT_DEFAULT_SSL_VERSION) {
  1710. enableTLS1 = PR_TRUE; /* If available, enable TLS1 */
  1711. }
  1712. slapi_ch_free_string( &val );
  1713. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1714. val = slapi_entry_attr_get_charptr( e, "sslVersionMin" );
  1715. if ( val ) {
  1716. (void)set_NSS_version(val, &NSSVersionMin, 1);
  1717. }
  1718. slapi_ch_free_string( &val );
  1719. val = slapi_entry_attr_get_charptr( e, "sslVersionMax" );
  1720. if ( val ) {
  1721. (void)set_NSS_version(val, &NSSVersionMax, 0);
  1722. }
  1723. slapi_ch_free_string( &val );
  1724. if (NSSVersionMin > NSSVersionMax) {
  1725. (void) slapi_getSSLVersion_str(NSSVersionMin, mymin, sizeof(mymin));
  1726. (void) slapi_getSSLVersion_str(NSSVersionMax, mymax, sizeof(mymax));
  1727. slapd_SSL_warn("The min value of NSS version range \"%s\" is greater than the max value \"%s\".",
  1728. mymin, mymax);
  1729. (void) slapi_getSSLVersion_str(enabledNSSVersions.max, newmax, sizeof(newmax));
  1730. slapd_SSL_warn("Reset the max \"%s\" to supported max \"%s\".",
  1731. mymax, newmax);
  1732. NSSVersionMax = enabledNSSVersions.max;
  1733. }
  1734. #endif
  1735. }
  1736. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1737. if (NSSVersionMin > 0) {
  1738. /* Use new NSS API SSL_VersionRangeSet (NSS3.14 or newer) */
  1739. slapdNSSVersions.min = NSSVersionMin;
  1740. slapdNSSVersions.max = NSSVersionMax;
  1741. restrict_SSLVersionRange();
  1742. (void) slapi_getSSLVersion_str(slapdNSSVersions.min, mymin, sizeof(mymin));
  1743. (void) slapi_getSSLVersion_str(slapdNSSVersions.max, mymax, sizeof(mymax));
  1744. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  1745. "Configured SSL version range: min: %s, max: %s\n",
  1746. mymin, mymax);
  1747. sslStatus = SSL_VersionRangeSet(pr_sock, &slapdNSSVersions);
  1748. if (sslStatus == SECSuccess) {
  1749. /* Set the restricted value to the cn=encryption entry */
  1750. } else {
  1751. slapd_SSL_error("SSL Initialization 2: "
  1752. "Failed to set SSL range: min: %s, max: %s\n",
  1753. mymin, mymax);
  1754. }
  1755. } else {
  1756. #endif
  1757. /* deprecated code */
  1758. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL3, enableSSL3);
  1759. if (sslStatus != SECSuccess) {
  1760. errorCode = PR_GetError();
  1761. slapd_SSL_warn("Security Initialization: Failed to %s SSLv3 "
  1762. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1763. enableSSL3 ? "enable" : "disable",
  1764. errorCode, slapd_pr_strerror(errorCode));
  1765. }
  1766. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_TLS, enableTLS1);
  1767. if (sslStatus != SECSuccess) {
  1768. errorCode = PR_GetError();
  1769. slapd_SSL_warn("Security Initialization: Failed to %s TLSv1 "
  1770. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1771. enableTLS1 ? "enable" : "disable",
  1772. errorCode, slapd_pr_strerror(errorCode));
  1773. }
  1774. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1775. }
  1776. #endif
  1777. freeConfigEntry( &e );
  1778. if(( slapd_SSLclientAuth = config_get_SSLclientAuth()) != SLAPD_SSLCLIENTAUTH_OFF ) {
  1779. int err;
  1780. switch (slapd_SSLclientAuth) {
  1781. case SLAPD_SSLCLIENTAUTH_ALLOWED:
  1782. #ifdef SSL_REQUIRE_CERTIFICATE /* new feature */
  1783. if ((err = SSL_OptionSet (pr_sock, SSL_REQUIRE_CERTIFICATE, PR_FALSE)) < 0) {
  1784. PRErrorCode prerr = PR_GetError();
  1785. LDAPDebug (LDAP_DEBUG_ANY,
  1786. "SSL_OptionSet(SSL_REQUIRE_CERTIFICATE,PR_FALSE) %d "
  1787. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1788. err, prerr, slapd_pr_strerror(prerr));
  1789. }
  1790. #endif
  1791. /* Give the client a clear opportunity to send her certificate: */
  1792. case SLAPD_SSLCLIENTAUTH_REQUIRED:
  1793. if ((err = SSL_OptionSet (pr_sock, SSL_REQUEST_CERTIFICATE, PR_TRUE)) < 0) {
  1794. PRErrorCode prerr = PR_GetError();
  1795. LDAPDebug (LDAP_DEBUG_ANY,
  1796. "SSL_OptionSet(SSL_REQUEST_CERTIFICATE,PR_TRUE) %d "
  1797. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1798. err, prerr, slapd_pr_strerror(prerr));
  1799. }
  1800. default: break;
  1801. }
  1802. }
  1803. /* Introduce a way of knowing whether slapd_ssl_init2 has
  1804. * already been executed.
  1805. * The cases in which slapd_ssl_init2 is executed during an
  1806. * Start TLS operation are not taken into account, for it is
  1807. * the fact of being executed by the server's SSL listener socket
  1808. * that matters. */
  1809. if (!startTLS)
  1810. _ssl_listener_initialized = 1; /* --ugaston */
  1811. return 0;
  1812. }
  1813. /* richm 20020227
  1814. To do LDAP client SSL init, we need to do
  1815. static void
  1816. ldapssl_basic_init( void )
  1817. {
  1818. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  1819. PR_SetConcurrency( 4 );
  1820. }
  1821. NSS_Init(certdbpath);
  1822. SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE);
  1823. SSL_OptionSetDefault(SSL_ENABLE_SSL3, PR_TRUE);
  1824. s = NSS_SetDomesticPolicy();
  1825. We already do pr_init, we don't need pr_setconcurrency, we already do nss_init and the rest
  1826. */
  1827. int
  1828. slapd_SSL_client_auth (LDAP* ld)
  1829. {
  1830. int rc = 0;
  1831. PRErrorCode errorCode;
  1832. char* pw = NULL;
  1833. char ** family_list;
  1834. Slapi_Entry *entry = NULL;
  1835. char cert_name[1024];
  1836. char *token = NULL;
  1837. SVRCOREStdPinObj *StdPinObj;
  1838. SVRCOREError err = SVRCORE_Success;
  1839. if((family_list = getChildren(configDN))) {
  1840. char **family;
  1841. char *personality = NULL;
  1842. char *activation = NULL;
  1843. char *cipher = NULL;
  1844. for (family = family_list; *family; family++) {
  1845. getConfigEntry( *family, &entry );
  1846. if ( entry == NULL ) {
  1847. continue;
  1848. }
  1849. activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
  1850. if((!activation) || (!PL_strcasecmp(activation, "off"))) {
  1851. /* this family was turned off, goto next */
  1852. slapi_ch_free((void **) &activation);
  1853. freeConfigEntry( &entry );
  1854. continue;
  1855. }
  1856. slapi_ch_free((void **) &activation);
  1857. personality = slapi_entry_attr_get_charptr( entry, "nssslpersonalityssl" );
  1858. cipher = slapi_entry_attr_get_charptr( entry, "cn" );
  1859. if ( cipher && !PL_strcasecmp(cipher, "RSA" )) {
  1860. char *ssltoken;
  1861. /* If there already is a token name, use it */
  1862. if (token) {
  1863. slapi_ch_free((void **) &personality);
  1864. slapi_ch_free((void **) &cipher);
  1865. freeConfigEntry( &entry );
  1866. continue;
  1867. }
  1868. ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  1869. if( ssltoken && personality ) {
  1870. if( !PL_strcasecmp(ssltoken, "internal") ||
  1871. !PL_strcasecmp(ssltoken, "internal (software)") ) {
  1872. /* Translate config internal name to more
  1873. * readable form. Certificate name is just
  1874. * the personality for internal tokens.
  1875. */
  1876. token = slapi_ch_strdup(internalTokenName);
  1877. #if defined(USE_OPENLDAP)
  1878. /* openldap needs tokenname:certnick */
  1879. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1880. #else
  1881. PL_strncpyz(cert_name, personality, sizeof(cert_name));
  1882. #endif
  1883. slapi_ch_free((void **) &ssltoken);
  1884. } else {
  1885. /* external PKCS #11 token - attach token name */
  1886. /*ssltoken was already dupped and we don't need it anymore*/
  1887. token = ssltoken;
  1888. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1889. }
  1890. } else {
  1891. errorCode = PR_GetError();
  1892. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1893. "family information. Missing nsssltoken or"
  1894. "nssslpersonalityssl in %s ("
  1895. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1896. *family, errorCode, slapd_pr_strerror(errorCode));
  1897. slapi_ch_free((void **) &ssltoken);
  1898. slapi_ch_free((void **) &personality);
  1899. slapi_ch_free((void **) &cipher);
  1900. freeConfigEntry( &entry );
  1901. continue;
  1902. }
  1903. } else { /* external PKCS #11 cipher */
  1904. char *ssltoken;
  1905. ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  1906. if( token && personality ) {
  1907. /* free the old token and remember the new one */
  1908. if (token) slapi_ch_free((void **)&token);
  1909. token = ssltoken; /*ssltoken was already dupped and we don't need it anymore*/
  1910. /* external PKCS #11 token - attach token name */
  1911. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1912. } else {
  1913. errorCode = PR_GetError();
  1914. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1915. "family information. Missing nsssltoken or"
  1916. "nssslpersonalityssl in %s ("
  1917. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1918. *family, errorCode, slapd_pr_strerror(errorCode));
  1919. slapi_ch_free((void **) &ssltoken);
  1920. slapi_ch_free((void **) &personality);
  1921. slapi_ch_free((void **) &cipher);
  1922. freeConfigEntry( &entry );
  1923. continue;
  1924. }
  1925. }
  1926. slapi_ch_free((void **) &personality);
  1927. slapi_ch_free((void **) &cipher);
  1928. freeConfigEntry( &entry );
  1929. } /* end of for */
  1930. freeChildren( family_list );
  1931. }
  1932. /* Free config data */
  1933. if (!svrcore_setup()) {
  1934. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  1935. err = SVRCORE_StdPinGetPin( &pw, StdPinObj, token );
  1936. if ( err != SVRCORE_Success || pw == NULL) {
  1937. errorCode = PR_GetError();
  1938. slapd_SSL_warn("SSL client authentication cannot be used "
  1939. "(no password). (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1940. errorCode, slapd_pr_strerror(errorCode));
  1941. } else {
  1942. #if defined(USE_OPENLDAP)
  1943. rc = ldap_set_option(ld, LDAP_OPT_X_TLS_KEYFILE, SERVER_KEY_NAME);
  1944. if (rc) {
  1945. slapd_SSL_warn("SSL client authentication cannot be used "
  1946. "unable to set the key to use to %s", SERVER_KEY_NAME);
  1947. }
  1948. rc = ldap_set_option(ld, LDAP_OPT_X_TLS_CERTFILE, cert_name);
  1949. if (rc) {
  1950. slapd_SSL_warn("SSL client authentication cannot be used "
  1951. "unable to set the cert to use to %s", cert_name);
  1952. }
  1953. /*
  1954. * not sure what else needs to be done for client auth - don't
  1955. * currently have a way to pass in the password to use to unlock
  1956. * the keydb - nor a way to disable caching
  1957. */
  1958. #else /* !USE_OPENLDAP */
  1959. rc = ldapssl_enable_clientauth (ld, SERVER_KEY_NAME, pw, cert_name);
  1960. if (rc != 0) {
  1961. errorCode = PR_GetError();
  1962. slapd_SSL_warn("ldapssl_enable_clientauth(%s, %s) %i ("
  1963. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1964. SERVER_KEY_NAME, cert_name, rc,
  1965. errorCode, slapd_pr_strerror(errorCode));
  1966. } else {
  1967. /*
  1968. * We cannot allow NSS to cache outgoing client auth connections -
  1969. * each client auth connection must have it's own non-shared SSL
  1970. * connection to the peer so that it will go through the
  1971. * entire handshake protocol every time including the use of its
  1972. * own unique client cert - see bug 605457
  1973. */
  1974. ldapssl_set_option(ld, SSL_NO_CACHE, PR_TRUE);
  1975. }
  1976. #endif
  1977. }
  1978. }
  1979. slapi_ch_free_string(&token);
  1980. slapi_ch_free_string(&pw);
  1981. LDAPDebug (LDAP_DEBUG_TRACE, "slapd_SSL_client_auth() %i\n", rc, 0, 0);
  1982. return rc;
  1983. }
  1984. /* Function for keeping track of the SSL initialization status:
  1985. * - returns 1: when slapd_ssl_init has been executed
  1986. */
  1987. int
  1988. slapd_security_library_is_initialized()
  1989. {
  1990. return _security_library_initialized;
  1991. }
  1992. /* Function for keeping track of the SSL listener socket initialization status:
  1993. * - returns 1: when slapd_ssl_init2 has been executed
  1994. */
  1995. int
  1996. slapd_ssl_listener_is_initialized()
  1997. {
  1998. return _ssl_listener_initialized;
  1999. }
  2000. int
  2001. slapd_nss_is_initialized()
  2002. {
  2003. return _nss_initialized;
  2004. }
  2005. /* memory to store tmpdir is allocated and returned; caller should free it. */
  2006. char* slapd_get_tmp_dir()
  2007. {
  2008. static char tmp[MAXPATHLEN];
  2009. char* tmpdir = NULL;;
  2010. tmp[0] = '\0';
  2011. if((tmpdir = config_get_tmpdir()) == NULL)
  2012. {
  2013. slapi_log_error(
  2014. SLAPI_LOG_FATAL,
  2015. "slapd_get_tmp_dir",
  2016. "config_get_tmpdir returns NULL Setting tmp dir to default\n");
  2017. strcpy(tmp, "/tmp");
  2018. return slapi_ch_strdup(tmp);
  2019. }
  2020. if(mkdir(tmpdir, 00770) == -1)
  2021. {
  2022. if (errno == EEXIST) {
  2023. slapi_log_error(
  2024. SLAPI_LOG_TRACE,
  2025. "slapd_get_tmp_dir",
  2026. "mkdir(%s, 00770) - already exists\n",
  2027. tmpdir);
  2028. } else {
  2029. slapi_log_error(
  2030. SLAPI_LOG_FATAL,
  2031. "slapd_get_tmp_dir",
  2032. "mkdir(%s, 00770) Error: %s\n",
  2033. tmpdir, strerror(errno));
  2034. }
  2035. }
  2036. return ( tmpdir );
  2037. }
  2038. SECKEYPrivateKey *
  2039. slapd_get_unlocked_key_for_cert(CERTCertificate *cert, void *pin_arg)
  2040. {
  2041. SECKEYPrivateKey *key = NULL;
  2042. PK11SlotListElement *sle;
  2043. PK11SlotList *slotlist = PK11_GetAllSlotsForCert(cert, NULL);
  2044. const char *certsubject = cert->subjectName ? cert->subjectName : "unknown cert";
  2045. if (!slotlist) {
  2046. PRErrorCode errcode = PR_GetError();
  2047. slapi_log_error(SLAPI_LOG_FATAL, "slapd_get_unlocked_key_for_cert",
  2048. "Error: cannot get slot list for certificate [%s] (%d: %s)\n",
  2049. certsubject, errcode, slapd_pr_strerror(errcode));
  2050. return key;
  2051. }
  2052. for (sle = slotlist->head; sle; sle = sle->next) {
  2053. PK11SlotInfo *slot = sle->slot;
  2054. const char *slotname = (slot && PK11_GetSlotName(slot)) ? PK11_GetSlotName(slot) : "unknown slot";
  2055. const char *tokenname = (slot && PK11_GetTokenName(slot)) ? PK11_GetTokenName(slot) : "unknown token";
  2056. if (!slot) {
  2057. slapi_log_error(SLAPI_LOG_TRACE, "slapd_get_unlocked_key_for_cert",
  2058. "Missing slot for slot list element for certificate [%s]\n",
  2059. certsubject);
  2060. } else if (!PK11_NeedLogin(slot) || PK11_IsLoggedIn(slot, pin_arg)) {
  2061. key = PK11_FindKeyByDERCert(slot, cert, pin_arg);
  2062. slapi_log_error(SLAPI_LOG_TRACE, "slapd_get_unlocked_key_for_cert",
  2063. "Found unlocked slot [%s] token [%s] for certificate [%s]\n",
  2064. slotname, tokenname, certsubject);
  2065. break;
  2066. } else {
  2067. slapi_log_error(SLAPI_LOG_TRACE, "slapd_get_unlocked_key_for_cert",
  2068. "Skipping locked slot [%s] token [%s] for certificate [%s]\n",
  2069. slotname, tokenname, certsubject);
  2070. }
  2071. }
  2072. if (!key) {
  2073. slapi_log_error(SLAPI_LOG_FATAL, "slapd_get_unlocked_key_for_cert",
  2074. "Error: could not find any unlocked slots for certificate [%s]. "
  2075. "Please review your TLS/SSL configuration. The following slots were found:\n",
  2076. certsubject);
  2077. for (sle = slotlist->head; sle; sle = sle->next) {
  2078. PK11SlotInfo *slot = sle->slot;
  2079. const char *slotname = (slot && PK11_GetSlotName(slot)) ? PK11_GetSlotName(slot) : "unknown slot";
  2080. const char *tokenname = (slot && PK11_GetTokenName(slot)) ? PK11_GetTokenName(slot) : "unknown token";
  2081. slapi_log_error(SLAPI_LOG_FATAL, "slapd_get_unlocked_key_for_cert",
  2082. "Slot [%s] token [%s] was locked.\n",
  2083. slotname, tokenname);
  2084. }
  2085. }
  2086. PK11_FreeSlotList(slotlist);
  2087. return key;
  2088. }