ssl.c 89 KB

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