ssl.c 83 KB

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