ssl.c 72 KB

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