ssl.c 79 KB

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