ssl.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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 <sys/param.h>
  54. #include <ssl.h>
  55. #include <nss.h>
  56. #include <key.h>
  57. #include <sslproto.h>
  58. #include "secmod.h"
  59. #include <string.h>
  60. #include <errno.h>
  61. #define NEED_TOK_DES /* defines tokDes and ptokDes - see slap.h */
  62. #include "slap.h"
  63. #include "svrcore.h"
  64. #include "fe.h"
  65. #include <ldap_ssl.h> /* ldapssl_client_init */
  66. #include "certdb.h"
  67. /* For IRIX... */
  68. #ifndef MAXPATHLEN
  69. #define MAXPATHLEN 1024
  70. #endif
  71. extern char* slapd_SSL3ciphers;
  72. extern symbol_t supported_ciphers[];
  73. /* dongle_file_name is set in slapd_nss_init when we set the path for the
  74. key, cert, and secmod files - the dongle file must be in the same directory
  75. and use the same naming scheme
  76. */
  77. static char* dongle_file_name = NULL;
  78. static int _security_library_initialized = 0;
  79. static int _ssl_listener_initialized = 0;
  80. /* Our name for the internal token, must match PKCS-11 config data below */
  81. static char *internalTokenName = "Internal (Software) Token";
  82. static int stimeout;
  83. static char *ciphers = NULL;
  84. static char * configDN = "cn=encryption,cn=config";
  85. /* Copied from libadmin/libadmin.h public/nsapi.h */
  86. #define SERVER_KEY_NAME "Server-Key"
  87. #define MAGNUS_ERROR_LEN 1024
  88. #define LOG_WARN 0
  89. #define LOG_FAILURE 3
  90. #define FILE_PATHSEP '/'
  91. /* ----------------------- Multiple cipher support ------------------------ */
  92. static char **cipher_names = NULL;
  93. typedef struct {
  94. char *version;
  95. char *name;
  96. int num;
  97. } cipherstruct;
  98. static cipherstruct _conf_ciphers[] = {
  99. {"SSL3","rc4", SSL_EN_RC4_128_WITH_MD5},
  100. {"SSL3","rc4export", SSL_EN_RC4_128_EXPORT40_WITH_MD5},
  101. {"SSL3","rc2", SSL_EN_RC2_128_CBC_WITH_MD5},
  102. {"SSL3","rc2export", SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5},
  103. /*{"idea", SSL_EN_IDEA_128_CBC_WITH_MD5}, */
  104. {"SSL3","des", SSL_EN_DES_64_CBC_WITH_MD5},
  105. {"SSL3","desede3", SSL_EN_DES_192_EDE3_CBC_WITH_MD5},
  106. {"SSL3","rsa_rc4_128_md5", SSL_RSA_WITH_RC4_128_MD5},
  107. {"SSL3","rsa_3des_sha", SSL_RSA_WITH_3DES_EDE_CBC_SHA},
  108. {"SSL3","rsa_des_sha", SSL_RSA_WITH_DES_CBC_SHA},
  109. {"SSL3","rsa_fips_3des_sha", SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA},
  110. {"SSL3","rsa_fips_des_sha", SSL_RSA_FIPS_WITH_DES_CBC_SHA},
  111. {"SSL3","rsa_rc4_40_md5", SSL_RSA_EXPORT_WITH_RC4_40_MD5},
  112. {"SSL3","rsa_rc2_40_md5", SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5},
  113. {"SSL3","rsa_null_md5", SSL_RSA_WITH_NULL_MD5},
  114. {"TLS","tls_rsa_export1024_with_rc4_56_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA},
  115. {"TLS","tls_rsa_export1024_with_des_cbc_sha", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA},
  116. {"SSL3","fortezza", SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA},
  117. {"SSL3","fortezza_rc4_128_sha", SSL_FORTEZZA_DMS_WITH_RC4_128_SHA},
  118. {"SSL3","fortezza_null", SSL_FORTEZZA_DMS_WITH_NULL_SHA},
  119. /*{"SSL3","dhe_dss_40_sha", SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA}, */
  120. {"SSL3","dhe_dss_des_sha", SSL_DHE_DSS_WITH_DES_CBC_SHA},
  121. {"SSL3","dhe_dss_3des_sha", SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA},
  122. /*{"SSL3","dhe_rsa_40_sha", SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA}, */
  123. {"SSL3","dhe_rsa_des_sha", SSL_DHE_RSA_WITH_DES_CBC_SHA},
  124. {"SSL3","dhe_rsa_3des_sha", SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA},
  125. {"TLS","tls_rsa_aes_128_sha", TLS_RSA_WITH_AES_128_CBC_SHA},
  126. {"TLS","tls_dhe_dss_aes_128_sha", TLS_DHE_DSS_WITH_AES_128_CBC_SHA},
  127. {"TLS","tls_dhe_rsa_aes_128_sha", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
  128. {"TLS","tls_rsa_aes_256_sha", TLS_RSA_WITH_AES_256_CBC_SHA},
  129. {"TLS","tls_dhe_dss_aes_256_sha", TLS_DHE_DSS_WITH_AES_256_CBC_SHA},
  130. {"TLS","tls_dhe_rsa_aes_256_sha", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
  131. /*{"TLS","tls_dhe_dss_1024_des_sha", TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA}, */
  132. {"TLS","tls_dhe_dss_1024_rc4_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA},
  133. {"TLS","tls_dhe_dss_rc4_128_sha", TLS_DHE_DSS_WITH_RC4_128_SHA},
  134. {NULL, NULL, 0}
  135. };
  136. char ** getSupportedCiphers()
  137. {
  138. SSLCipherSuiteInfo info;
  139. char *sep = "::";
  140. int number_of_ciphers = sizeof (_conf_ciphers) /sizeof(cipherstruct);
  141. int i;
  142. if (cipher_names == NULL ) {
  143. cipher_names = (char **) slapi_ch_calloc ((number_of_ciphers +1 ) , sizeof(char *));
  144. for (i = 0 ; _conf_ciphers[i].name != NULL; i++ ) {
  145. SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[i].num,&info,sizeof(info));
  146. cipher_names[i] = PR_smprintf("%s%s%s%s%s%s%s%s%d\0",_conf_ciphers[i].version,sep,_conf_ciphers[i].name,sep,info.symCipherName,sep,info.macAlgorithmName,sep,info.symKeyBits);
  147. }
  148. cipher_names[i] = NULL;
  149. }
  150. return cipher_names;
  151. }
  152. void
  153. _conf_setallciphers(int active)
  154. {
  155. int x;
  156. /* MLM - change: Because null_md5 is NOT encrypted at all, force
  157. * them to activate it by name. */
  158. for(x = 0; _conf_ciphers[x].name; x++) {
  159. if(active && !strcmp(_conf_ciphers[x].name, "rsa_null_md5")) {
  160. continue;
  161. }
  162. SSL_CipherPrefSetDefault(_conf_ciphers[x].num, active ? PR_TRUE : PR_FALSE);
  163. }
  164. }
  165. char *
  166. _conf_setciphers(char *ciphers)
  167. {
  168. char *t, err[MAGNUS_ERROR_LEN];
  169. int x, active;
  170. char *raw = ciphers;
  171. /* Default is to activate all of them */
  172. if(!ciphers || ciphers[0] == '\0') {
  173. _conf_setallciphers(1);
  174. return NULL;
  175. }
  176. /* Enable all the ciphers by default and the following while loop would disable the user disabled ones This is needed becuase we added a new set of ciphers in the table . Right now there is no support for this from the console */
  177. _conf_setallciphers(1);
  178. t = ciphers;
  179. while(t) {
  180. while((*ciphers) && (isspace(*ciphers))) ++ciphers;
  181. switch(*ciphers++) {
  182. case '+':
  183. active = 1; break;
  184. case '-':
  185. active = 0; break;
  186. default:
  187. PR_snprintf(err, sizeof(err), "invalid ciphers <%s>: format is "
  188. "+cipher1,-cipher2...", raw);
  189. return slapi_ch_strdup(err);
  190. }
  191. if( (t = strchr(ciphers, ',')) )
  192. *t++ = '\0';
  193. if(!strcasecmp(ciphers, "all"))
  194. _conf_setallciphers(active);
  195. else {
  196. for(x = 0; _conf_ciphers[x].name; x++) {
  197. if(!strcasecmp(ciphers, _conf_ciphers[x].name)) {
  198. SSL_CipherPrefSetDefault(_conf_ciphers[x].num, active ? PR_TRUE : PR_FALSE);
  199. break;
  200. }
  201. }
  202. if(!_conf_ciphers[x].name) {
  203. PR_snprintf(err, sizeof(err), "unknown cipher %s", ciphers);
  204. return slapi_ch_strdup(err);
  205. }
  206. }
  207. if(t)
  208. ciphers = t;
  209. }
  210. return NULL;
  211. }
  212. /* SSL Policy stuff */
  213. /*
  214. * SSLPLCY_Install
  215. *
  216. * Call the SSL_CipherPolicySet function for each ciphersuite.
  217. */
  218. PRStatus
  219. SSLPLCY_Install(void)
  220. {
  221. SECStatus s = 0;
  222. s = NSS_SetDomesticPolicy();
  223. return s?PR_FAILURE:PR_SUCCESS;
  224. }
  225. static void
  226. slapd_SSL_report(int degree, char *fmt, va_list args)
  227. {
  228. char buf[2048];
  229. PR_vsnprintf( buf, sizeof(buf), fmt, args );
  230. LDAPDebug( LDAP_DEBUG_ANY, "SSL %s: %s\n",
  231. (degree == LOG_FAILURE) ? "failure" : "alert",
  232. buf, 0 );
  233. }
  234. void
  235. slapd_SSL_error(char *fmt, ...)
  236. {
  237. va_list args;
  238. va_start(args, fmt);
  239. slapd_SSL_report(LOG_FAILURE, fmt, args);
  240. exit(1);
  241. }
  242. void
  243. slapd_SSL_warn(char *fmt, ...)
  244. {
  245. va_list args;
  246. va_start(args, fmt);
  247. slapd_SSL_report(LOG_WARN, fmt, args);
  248. va_end(args);
  249. }
  250. /**
  251. * Get a particular entry
  252. */
  253. static Slapi_Entry *
  254. getConfigEntry( const char *dn, Slapi_Entry **e2 ) {
  255. Slapi_DN sdn;
  256. slapi_sdn_init_dn_byref( &sdn, dn );
  257. slapi_search_internal_get_entry( &sdn, NULL, e2,
  258. plugin_get_default_component_id());
  259. slapi_sdn_done( &sdn );
  260. return *e2;
  261. }
  262. /**
  263. * Free an entry
  264. */
  265. static void
  266. freeConfigEntry( Slapi_Entry ** e ) {
  267. if ( (e != NULL) && (*e != NULL) ) {
  268. slapi_entry_free( *e );
  269. *e = NULL;
  270. }
  271. }
  272. /**
  273. * Get a list of child DNs
  274. */
  275. static char **
  276. getChildren( char *dn ) {
  277. Slapi_PBlock *new_pb = NULL;
  278. Slapi_Entry **e;
  279. int search_result = 1;
  280. int nEntries = 0;
  281. char **list = NULL;
  282. new_pb = slapi_search_internal ( dn, LDAP_SCOPE_ONELEVEL,
  283. "(objectclass=nsEncryptionModule)",
  284. NULL, NULL, 0);
  285. slapi_pblock_get( new_pb, SLAPI_NENTRIES, &nEntries);
  286. if ( nEntries > 0 ) {
  287. slapi_pblock_get( new_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
  288. slapi_pblock_get( new_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &e);
  289. if ( e != NULL ) {
  290. int i;
  291. list = (char **)slapi_ch_malloc( sizeof(*list) * (nEntries + 1));
  292. for ( i = 0; e[i] != NULL; i++ ) {
  293. list[i] = slapi_ch_strdup(slapi_entry_get_dn(e[i]));
  294. }
  295. list[nEntries] = NULL;
  296. }
  297. }
  298. slapi_free_search_results_internal(new_pb);
  299. slapi_pblock_destroy(new_pb );
  300. return list;
  301. }
  302. /**
  303. * Free a list of child DNs
  304. */
  305. static void
  306. freeChildren( char **list ) {
  307. if ( list != NULL ) {
  308. int i;
  309. for ( i = 0; list[i] != NULL; i++ ) {
  310. slapi_ch_free( (void **)(&list[i]) );
  311. }
  312. slapi_ch_free( (void **)(&list) );
  313. }
  314. }
  315. static void
  316. warn_if_no_cert_file(const char *filename)
  317. {
  318. PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
  319. if (PR_SUCCESS != status) {
  320. /* if file ends in -cert7.db and the corresponding -cert8.db exists, just
  321. warn */
  322. char *cert8 = slapi_ch_strdup(filename);
  323. char *ptr;
  324. if ((ptr = PL_strrstr(cert8, "-cert7.db"))) {
  325. strcpy(ptr, "-cert8.db");
  326. status = PR_Access(cert8, PR_ACCESS_READ_OK);
  327. if (PR_SUCCESS == status) {
  328. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  329. "Notice: certificate DB file %s does not exist but %s does - suggest updating nscertfile\n",
  330. filename, cert8);
  331. }
  332. }
  333. slapi_ch_free_string(&cert8);
  334. if (PR_SUCCESS != status) {
  335. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  336. "Warning: certificate DB file %s does not exist - SSL initialization will likely fail\n",
  337. filename);
  338. }
  339. }
  340. }
  341. static void
  342. warn_if_no_key_file(const char *path, const char *name)
  343. {
  344. char last = path[strlen(path)-1];
  345. char *filename = slapi_ch_smprintf("%s%s%s", path, ((last == '/' || last == '\\') ? "" : "/"), name);
  346. PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
  347. if (PR_SUCCESS != status) {
  348. /* if file ends in -key3.db and the corresponding -key4.db exists, just
  349. warn */
  350. char *key4 = slapi_ch_strdup(filename);
  351. char *ptr;
  352. if ((ptr = PL_strrstr(key4, "-key3.db"))) {
  353. strcpy(ptr, "-key4.db");
  354. status = PR_Access(key4, PR_ACCESS_READ_OK);
  355. if (PR_SUCCESS == status) {
  356. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  357. "Notice: key DB file %s does not exist but %s does - suggest updating nskeyfile\n",
  358. filename, key4);
  359. }
  360. }
  361. slapi_ch_free_string(&key4);
  362. if (PR_SUCCESS != status) {
  363. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  364. "Warning: key DB file %s does not exist - SSL initialization will likely fail\n",
  365. filename);
  366. }
  367. }
  368. slapi_ch_free_string(&filename);
  369. }
  370. /*
  371. * slapd_nss_init() is always called from main(), even if we do not
  372. * plan to listen on a secure port. If config_available is 0, the
  373. * config. entries from dse.ldif are NOT available (used only when
  374. * running in referral mode).
  375. * As of DS6.1, the init_ssl flag passed is ignored.
  376. *
  377. * richm 20070126 - By default now we put the key/cert db files
  378. * in an instance specific directory (the certdir directory) so
  379. * we do not need a prefix any more.
  380. */
  381. int
  382. slapd_nss_init(int init_ssl, int config_available)
  383. {
  384. SECStatus secStatus;
  385. PRErrorCode errorCode;
  386. PRStatus status;
  387. int rv = 0;
  388. int len = 0;
  389. PRUint32 nssFlags = 0;
  390. char *certdir;
  391. /* set in slapd_bootstrap_config,
  392. thus certdir is available even if config_available is false */
  393. certdir = config_get_certdir();
  394. /* make sure path does not end in the path separator character */
  395. len = strlen(certdir);
  396. if (certdir[len-1] == '/' || certdir[len-1] == '\\') {
  397. certdir[len-1] = '\0';
  398. }
  399. /* we open the key/cert db in rw mode, so make sure the directory
  400. is writable */
  401. if (PR_SUCCESS != (status = PR_Access(certdir, PR_ACCESS_WRITE_OK))) {
  402. char *serveruser = "unknown";
  403. #ifndef _WIN32
  404. serveruser = config_get_localuser();
  405. #endif
  406. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  407. "Warning: The key/cert database directory [%s] is not writable by "
  408. "the server uid [%s]: initialization likely to fail.\n",
  409. certdir, serveruser);
  410. #ifndef _WIN32
  411. slapi_ch_free_string(&serveruser);
  412. #endif
  413. }
  414. /******** Initialise NSS *********/
  415. nssFlags &= (~NSS_INIT_READONLY);
  416. slapd_pk11_configurePKCS11(NULL, NULL, tokDes, ptokDes, NULL, NULL, NULL, NULL, 0, 0 );
  417. secStatus = NSS_Initialize(certdir, NULL, NULL, "secmod.db", nssFlags);
  418. dongle_file_name = PR_smprintf("%s/pin.txt", certdir);
  419. if (secStatus != SECSuccess) {
  420. errorCode = PR_GetError();
  421. slapd_SSL_warn("Security Initialization: NSS initialization failed ("
  422. SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
  423. "certdir: %s",
  424. errorCode, slapd_pr_strerror(errorCode), certdir);
  425. rv = -1;
  426. }
  427. /****** end of NSS Initialization ******/
  428. slapi_ch_free_string(&certdir);
  429. return rv;
  430. }
  431. /*
  432. * slapd_ssl_init() is called from main() if we plan to listen
  433. * on a secure port.
  434. */
  435. int
  436. slapd_ssl_init() {
  437. PRErrorCode errorCode;
  438. char ** family_list;
  439. char *val = NULL;
  440. char cipher_string[1024];
  441. int rv = 0;
  442. PK11SlotInfo *slot;
  443. #ifndef _WIN32
  444. SVRCOREStdPinObj *StdPinObj;
  445. #else
  446. SVRCOREFilePinObj *FilePinObj;
  447. SVRCOREAltPinObj *AltPinObj;
  448. SVRCORENTUserPinObj *NTUserPinObj;
  449. #endif
  450. Slapi_Entry *entry = NULL;
  451. /* Get general information */
  452. getConfigEntry( configDN, &entry );
  453. val = slapi_entry_attr_get_charptr( entry, "nssslSessionTimeout" );
  454. ciphers = slapi_entry_attr_get_charptr( entry, "nsssl3ciphers" );
  455. /* We are currently using the value of sslSessionTimeout
  456. for ssl3SessionTimeout, see SSL_ConfigServerSessionIDCache() */
  457. /* Note from Tom Weinstein on the meaning of the timeout:
  458. Timeouts are in seconds. '0' means use the default, which is
  459. 24hrs for SSL3 and 100 seconds for SSL2.
  460. */
  461. if(!val) {
  462. errorCode = PR_GetError();
  463. slapd_SSL_warn("Security Initialization: Failed to retrieve SSL "
  464. "configuration information ("
  465. SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
  466. "nssslSessionTimeout: %s ",
  467. errorCode, slapd_pr_strerror(errorCode),
  468. (val ? "found" : "not found"));
  469. slapi_ch_free((void **) &val);
  470. slapi_ch_free((void **) &ciphers);
  471. return -1;
  472. }
  473. stimeout = atoi(val);
  474. slapi_ch_free((void **) &val);
  475. #ifndef _WIN32
  476. if ( SVRCORE_CreateStdPinObj(&StdPinObj, dongle_file_name, PR_TRUE) !=
  477. SVRCORE_Success) {
  478. errorCode = PR_GetError();
  479. slapd_SSL_warn("Security Initialization: Unable to create PinObj ("
  480. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  481. errorCode, slapd_pr_strerror(errorCode));
  482. return -1;
  483. }
  484. SVRCORE_RegisterPinObj((SVRCOREPinObj *)StdPinObj);
  485. #else
  486. if (SVRCORE_CreateFilePinObj(&FilePinObj, dongle_file_name) !=
  487. SVRCORE_Success) {
  488. errorCode = PR_GetError();
  489. slapd_SSL_warn("Security Initialization: Unable to create FilePinObj ("
  490. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  491. errorCode, slapd_pr_strerror(errorCode));
  492. return -1;
  493. }
  494. if (SVRCORE_CreateNTUserPinObj(&NTUserPinObj) != SVRCORE_Success){
  495. errorCode = PR_GetError();
  496. slapd_SSL_warn("Security Initialization: Unable to create NTUserPinObj ("
  497. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  498. errorCode, slapd_pr_strerror(errorCode));
  499. return -1;
  500. }
  501. if (SVRCORE_CreateAltPinObj(&AltPinObj, (SVRCOREPinObj *)FilePinObj,
  502. (SVRCOREPinObj *)NTUserPinObj) != SVRCORE_Success) {
  503. errorCode = PR_GetError();
  504. slapd_SSL_warn("Security Initialization: Unable to create AltPinObj ("
  505. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  506. errorCode, slapd_pr_strerror(errorCode));
  507. return -1;
  508. }
  509. SVRCORE_RegisterPinObj((SVRCOREPinObj *)AltPinObj);
  510. #endif /* _WIN32 */
  511. if((family_list = getChildren(configDN))) {
  512. char **family;
  513. char *token;
  514. char *activation;
  515. for (family = family_list; *family; family++) {
  516. token = NULL;
  517. activation = NULL;
  518. freeConfigEntry( &entry );
  519. getConfigEntry( *family, &entry );
  520. if ( entry == NULL ) {
  521. continue;
  522. }
  523. activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
  524. if((!activation) || (!strcasecmp(activation, "off"))) {
  525. /* this family was turned off, goto next */
  526. slapi_ch_free((void **) &activation);
  527. continue;
  528. }
  529. slapi_ch_free((void **) &activation);
  530. token = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  531. if( token ) {
  532. if( !strcasecmp(token, "internal") ||
  533. !strcasecmp(token, "internal (software)"))
  534. slot = slapd_pk11_getInternalKeySlot();
  535. else
  536. slot = slapd_pk11_findSlotByName(token);
  537. } else {
  538. errorCode = PR_GetError();
  539. slapd_SSL_warn("Security Initialization: Unable to get token ("
  540. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  541. errorCode, slapd_pr_strerror(errorCode));
  542. return -1;
  543. }
  544. slapi_ch_free((void **) &token);
  545. if (!slot) {
  546. errorCode = PR_GetError();
  547. slapd_SSL_warn("Security Initialization: Unable to find slot ("
  548. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  549. errorCode, slapd_pr_strerror(errorCode));
  550. return -1;
  551. }
  552. /* authenticate */
  553. if(slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess)
  554. {
  555. errorCode = PR_GetError();
  556. slapd_SSL_warn("Security Initialization: Unable to authenticate ("
  557. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  558. errorCode, slapd_pr_strerror(errorCode));
  559. return -1;
  560. }
  561. }
  562. freeChildren( family_list );
  563. }
  564. freeConfigEntry( &entry );
  565. if(SSLPLCY_Install() != PR_SUCCESS) {
  566. errorCode = PR_GetError();
  567. slapd_SSL_warn("Security Initialization: Unable to set SSL export policy ("
  568. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  569. errorCode, slapd_pr_strerror(errorCode));
  570. return -1;
  571. }
  572. /* ugaston- Cipher preferences must be set before any sslSocket is created
  573. * for such sockets to take preferences into account.
  574. */
  575. /* Step Three.5: Set SSL cipher preferences */
  576. *cipher_string = 0;
  577. if(ciphers && (*ciphers) && strcmp(ciphers, "blank"))
  578. PL_strncpyz(cipher_string, ciphers, sizeof(cipher_string));
  579. slapi_ch_free((void **) &ciphers);
  580. if( NULL != (val = _conf_setciphers(cipher_string)) ) {
  581. errorCode = PR_GetError();
  582. slapd_SSL_warn("Security Initialization: Failed to set SSL cipher "
  583. "preference information: %s (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  584. val, errorCode, slapd_pr_strerror(errorCode));
  585. rv = 3;
  586. slapi_ch_free((void **) &val);
  587. }
  588. freeConfigEntry( &entry );
  589. /* Introduce a way of knowing whether slapd_ssl_init has
  590. * already been executed. */
  591. _security_library_initialized = 1;
  592. if ( rv != 0 )
  593. return rv;
  594. return 0;
  595. }
  596. int slapd_ssl_init2(PRFileDesc **fd, int startTLS)
  597. {
  598. PRFileDesc *pr_sock, *sock = (*fd);
  599. PRErrorCode errorCode;
  600. SECStatus rv = SECFailure;
  601. char ** family_list;
  602. CERTCertificate *cert = NULL;
  603. SECKEYPrivateKey *key = NULL;
  604. char errorbuf[BUFSIZ];
  605. char *val = NULL;
  606. char *default_val = NULL;
  607. int nFamilies = 0;
  608. SECStatus sslStatus;
  609. int slapd_SSLclientAuth;
  610. char* tmpDir;
  611. Slapi_Entry *e = NULL;
  612. /* turn off the PKCS11 pin interactive mode */
  613. #ifndef _WIN32
  614. SVRCOREStdPinObj *StdPinObj;
  615. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  616. SVRCORE_SetStdPinInteractive(StdPinObj, PR_FALSE);
  617. #endif
  618. errorbuf[0] = '\0';
  619. /* Import pr fd into SSL */
  620. pr_sock = SSL_ImportFD( NULL, sock );
  621. if( pr_sock == (PRFileDesc *)NULL ) {
  622. errorCode = PR_GetError();
  623. slapd_SSL_warn("Security Initialization: Failed to import NSPR "
  624. "fd into SSL (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  625. errorCode, slapd_pr_strerror(errorCode));
  626. return 1;
  627. }
  628. (*fd) = pr_sock;
  629. /* Step / Three.6 /
  630. * - If in FIPS mode, authenticate to the token before
  631. * doing anything else
  632. */
  633. {
  634. PK11SlotInfo *slot = slapd_pk11_getInternalSlot();
  635. if (!slot) {
  636. errorCode = PR_GetError();
  637. slapd_SSL_warn("Security Initialization: Unable to get internal slot ("
  638. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  639. errorCode, slapd_pr_strerror(errorCode));
  640. return -1;
  641. }
  642. if(slapd_pk11_isFIPS()) {
  643. if(slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess) {
  644. errorCode = PR_GetError();
  645. slapd_SSL_warn("Security Initialization: Unable to authenticate ("
  646. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  647. errorCode, slapd_pr_strerror(errorCode));
  648. return -1;
  649. }
  650. }
  651. slapd_pk11_setSlotPWValues(slot, 0, 0);
  652. }
  653. /*
  654. * Now, get the complete list of cipher families. Each family
  655. * has a token name and personality name which we'll use to find
  656. * appropriate keys and certs, and call SSL_ConfigSecureServer
  657. * with.
  658. */
  659. if((family_list = getChildren(configDN))) {
  660. char **family;
  661. char cert_name[1024];
  662. char *token;
  663. char *personality;
  664. char *activation;
  665. for (family = family_list; *family; family++) {
  666. token = NULL;
  667. personality = NULL;
  668. activation = NULL;
  669. getConfigEntry( *family, &e );
  670. if ( e == NULL ) {
  671. continue;
  672. }
  673. activation = slapi_entry_attr_get_charptr( e, "nssslactivation" );
  674. if((!activation) || (!strcasecmp(activation, "off"))) {
  675. /* this family was turned off, goto next */
  676. slapi_ch_free((void **) &activation);
  677. freeConfigEntry( &e );
  678. continue;
  679. }
  680. slapi_ch_free((void **) &activation);
  681. token = slapi_entry_attr_get_charptr( e, "nsssltoken" );
  682. personality = slapi_entry_attr_get_charptr( e, "nssslpersonalityssl" );
  683. if( token && personality ) {
  684. if( !strcasecmp(token, "internal") ||
  685. !strcasecmp(token, "internal (software)") )
  686. PL_strncpyz(cert_name, personality, sizeof(cert_name));
  687. else
  688. /* external PKCS #11 token - attach token name */
  689. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  690. }
  691. else {
  692. errorCode = PR_GetError();
  693. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  694. "family information. Missing nsssltoken or"
  695. "nssslpersonalityssl in %s ("
  696. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  697. *family, errorCode, slapd_pr_strerror(errorCode));
  698. slapi_ch_free((void **) &token);
  699. slapi_ch_free((void **) &personality);
  700. freeConfigEntry( &e );
  701. continue;
  702. }
  703. slapi_ch_free((void **) &token);
  704. /* Step Four -- Locate the server certificate */
  705. cert = slapd_pk11_findCertFromNickname(cert_name, NULL);
  706. if (cert == NULL) {
  707. errorCode = PR_GetError();
  708. slapd_SSL_warn("Security Initialization: Can't find "
  709. "certificate (%s) for family %s ("
  710. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  711. cert_name, *family,
  712. errorCode, slapd_pr_strerror(errorCode));
  713. }
  714. /* Step Five -- Get the private key from cert */
  715. if( cert != NULL )
  716. key = slapd_pk11_findKeyByAnyCert(cert, NULL);
  717. if (key == NULL) {
  718. errorCode = PR_GetError();
  719. slapd_SSL_warn("Security Initialization: Unable to retrieve "
  720. "private key for cert %s of family %s ("
  721. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  722. cert_name, *family,
  723. errorCode, slapd_pr_strerror(errorCode));
  724. slapi_ch_free((void **) &personality);
  725. CERT_DestroyCertificate(cert);
  726. cert = NULL;
  727. freeConfigEntry( &e );
  728. continue;
  729. }
  730. /* Step Six -- Configure Secure Server Mode */
  731. if(pr_sock) {
  732. SECCertificateUsage returnedUsages;
  733. rv = CERT_VerifyCertificateNow(
  734. CERT_GetDefaultCertDB(), cert, PR_TRUE,
  735. certificateUsageSSLServer,
  736. SSL_RevealPinArg(pr_sock),
  737. &returnedUsages);
  738. if (SECSuccess == rv) {
  739. if( slapd_pk11_fortezzaHasKEA(cert) == PR_TRUE ) {
  740. rv = SSL_ConfigSecureServer(*fd, cert, key, kt_fortezza);
  741. }
  742. else {
  743. rv = SSL_ConfigSecureServer(*fd, cert, key, kt_rsa);
  744. }
  745. if (SECSuccess != rv) {
  746. errorCode = PR_GetError();
  747. slapd_SSL_warn("ConfigSecureServer: "
  748. "Server key/certificate is "
  749. "bad for cert %s of family %s ("
  750. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  751. cert_name, *family, errorCode,
  752. slapd_pr_strerror(errorCode));
  753. }
  754. } else {
  755. /* verify certificate failed */
  756. /* If the common name in the subject DN for the certificate
  757. * is not identical to the domain name passed in the
  758. * hostname parameter, SECFailure. */
  759. errorCode = PR_GetError();
  760. slapd_SSL_warn("CERT_VerifyCertificateNow: "
  761. "verify certificate failed "
  762. "for cert %s of family %s ("
  763. SLAPI_COMPONENT_NAME_NSPR
  764. " error %d - %s)",
  765. cert_name, *family, errorCode,
  766. slapd_pr_strerror(errorCode));
  767. }
  768. }
  769. if (cert) {
  770. CERT_DestroyCertificate(cert);
  771. cert = NULL;
  772. }
  773. slapi_ch_free((void **) &personality);
  774. if (SECSuccess != rv) {
  775. freeConfigEntry( &e );
  776. continue;
  777. }
  778. nFamilies++;
  779. freeConfigEntry( &e );
  780. }
  781. freeChildren( family_list );
  782. }
  783. if ( !nFamilies ) {
  784. slapd_SSL_error("None of the cipher are valid");
  785. return -1;
  786. }
  787. /* Step Seven -- Configure Server Session ID Cache */
  788. tmpDir = slapd_get_tmp_dir();
  789. slapi_log_error(SLAPI_LOG_TRACE,
  790. "slapd_ssl_init2", "tmp dir = %s\n", tmpDir);
  791. rv = SSL_ConfigServerSessionIDCache(0, stimeout, stimeout, tmpDir);
  792. slapi_ch_free(&tmpDir);
  793. if (rv) {
  794. errorCode = PR_GetError();
  795. if (errorCode == ENOSPC) {
  796. slapd_SSL_error("Config of server nonce cache failed, "
  797. "out of disk space! Make more room in /tmp "
  798. "and try again. (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  799. errorCode, slapd_pr_strerror(errorCode));
  800. }
  801. else {
  802. slapd_SSL_error("Config of server nonce cache failed (error %d - %s)",
  803. errorCode, slapd_pr_strerror(errorCode));
  804. }
  805. return rv;
  806. }
  807. sslStatus = SSL_OptionSet(pr_sock, SSL_SECURITY, PR_TRUE);
  808. if (sslStatus != SECSuccess) {
  809. errorCode = PR_GetError();
  810. slapd_SSL_warn("Security Initialization: Failed to enable security "
  811. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  812. errorCode, slapd_pr_strerror(errorCode));
  813. return -1;
  814. }
  815. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL3, PR_TRUE);
  816. if (sslStatus != SECSuccess) {
  817. errorCode = PR_GetError();
  818. slapd_SSL_warn("Security Initialization: Failed to enable SSLv3 "
  819. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  820. errorCode, slapd_pr_strerror(errorCode));
  821. }
  822. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_TLS, PR_TRUE);
  823. if (sslStatus != SECSuccess) {
  824. errorCode = PR_GetError();
  825. slapd_SSL_warn("Security Initialization: Failed to enable TLS "
  826. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  827. errorCode, slapd_pr_strerror(errorCode));
  828. }
  829. /* Explicitly disabling SSL2 - NGK */
  830. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL2, PR_FALSE);
  831. /* Retrieve the SSL Client Authentication status from cn=config */
  832. /* Set a default value if no value found */
  833. getConfigEntry( configDN, &e );
  834. val = NULL;
  835. if ( e != NULL ) {
  836. val = slapi_entry_attr_get_charptr( e, "nssslclientauth" );
  837. }
  838. if( !val ) {
  839. errorCode = PR_GetError();
  840. slapd_SSL_warn("Security Initialization: Cannot get SSL Client "
  841. "Authentication status. No nsslclientauth in %s ("
  842. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  843. configDN, errorCode, slapd_pr_strerror(errorCode));
  844. switch( SLAPD_SSLCLIENTAUTH_DEFAULT ) {
  845. case SLAPD_SSLCLIENTAUTH_OFF:
  846. default_val = "off";
  847. break;
  848. case SLAPD_SSLCLIENTAUTH_ALLOWED:
  849. default_val = "allowed";
  850. break;
  851. case SLAPD_SSLCLIENTAUTH_REQUIRED:
  852. default_val = "required";
  853. break;
  854. default:
  855. default_val = "allowed";
  856. break;
  857. }
  858. val = default_val;
  859. }
  860. if( config_set_SSLclientAuth( "nssslclientauth", val, errorbuf,
  861. CONFIG_APPLY ) != LDAP_SUCCESS ) {
  862. errorCode = PR_GetError();
  863. slapd_SSL_warn("Security Initialization: Cannot set SSL Client "
  864. "Authentication status to \"%s\", error (%s). "
  865. "Supported values are \"off\", \"allowed\" "
  866. "and \"required\". (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  867. val, errorbuf, errorCode, slapd_pr_strerror(errorCode));
  868. }
  869. if (val != default_val) {
  870. slapi_ch_free_string(&val);
  871. }
  872. freeConfigEntry( &e );
  873. if(( slapd_SSLclientAuth = config_get_SSLclientAuth()) != SLAPD_SSLCLIENTAUTH_OFF ) {
  874. int err;
  875. switch (slapd_SSLclientAuth) {
  876. case SLAPD_SSLCLIENTAUTH_ALLOWED:
  877. #ifdef SSL_REQUIRE_CERTIFICATE /* new feature */
  878. if ((err = SSL_OptionSet (pr_sock, SSL_REQUIRE_CERTIFICATE, PR_FALSE)) < 0) {
  879. PRErrorCode prerr = PR_GetError();
  880. LDAPDebug (LDAP_DEBUG_ANY,
  881. "SSL_OptionSet(SSL_REQUIRE_CERTIFICATE,PR_FALSE) %d "
  882. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  883. err, prerr, slapd_pr_strerror(prerr));
  884. }
  885. #endif
  886. /* Give the client a clear opportunity to send her certificate: */
  887. case SLAPD_SSLCLIENTAUTH_REQUIRED:
  888. if ((err = SSL_OptionSet (pr_sock, SSL_REQUEST_CERTIFICATE, PR_TRUE)) < 0) {
  889. PRErrorCode prerr = PR_GetError();
  890. LDAPDebug (LDAP_DEBUG_ANY,
  891. "SSL_OptionSet(SSL_REQUEST_CERTIFICATE,PR_TRUE) %d "
  892. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  893. err, prerr, slapd_pr_strerror(prerr));
  894. }
  895. default: break;
  896. }
  897. }
  898. /* Introduce a way of knowing whether slapd_ssl_init2 has
  899. * already been executed.
  900. * The cases in which slapd_ssl_init2 is executed during an
  901. * Start TLS operation are not taken into account, for it is
  902. * the fact of being executed by the server's SSL listener socket
  903. * that matters. */
  904. if (!startTLS)
  905. _ssl_listener_initialized = 1; /* --ugaston */
  906. return 0;
  907. }
  908. /* richm 20020227
  909. To do LDAP client SSL init, we need to do
  910. static void
  911. ldapssl_basic_init( void )
  912. {
  913. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  914. PR_SetConcurrency( 4 );
  915. }
  916. NSS_Init(certdbpath);
  917. SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE);
  918. SSL_OptionSetDefault(SSL_ENABLE_SSLdirs
  919. 3, PR_TRUE);
  920. s = NSS_SetDomesticPolicy();
  921. We already do pr_init, we don't need pr_setconcurrency, we already do nss_init and the rest
  922. */
  923. int
  924. slapd_SSL_client_init()
  925. {
  926. return 0;
  927. }
  928. static int
  929. slapd_SSL_client_auth (LDAP* ld)
  930. {
  931. int rc = 0;
  932. PRErrorCode errorCode;
  933. char* pw = NULL;
  934. char ** family_list;
  935. Slapi_Entry *entry = NULL;
  936. char cert_name[1024];
  937. char *token = NULL;
  938. #ifndef _WIN32
  939. SVRCOREStdPinObj *StdPinObj;
  940. #else
  941. SVRCOREAltPinObj *AltPinObj;
  942. #endif
  943. SVRCOREError err = SVRCORE_Success;
  944. if((family_list = getChildren(configDN))) {
  945. char **family;
  946. char *personality = NULL;
  947. char *activation = NULL;
  948. char *cipher = NULL;
  949. for (family = family_list; *family; family++) {
  950. getConfigEntry( *family, &entry );
  951. if ( entry == NULL ) {
  952. continue;
  953. }
  954. activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
  955. if((!activation) || (!strcasecmp(activation, "off"))) {
  956. /* this family was turned off, goto next */
  957. slapi_ch_free((void **) &activation);
  958. freeConfigEntry( &entry );
  959. continue;
  960. }
  961. slapi_ch_free((void **) &activation);
  962. personality = slapi_entry_attr_get_charptr( entry, "nssslpersonalityssl" );
  963. cipher = slapi_entry_attr_get_charptr( entry, "cn" );
  964. if ( cipher && !strcasecmp(cipher, "RSA" )) {
  965. char *ssltoken;
  966. /* If there already is a token name, use it */
  967. if (token) {
  968. slapi_ch_free((void **) &personality);
  969. slapi_ch_free((void **) &cipher);
  970. freeConfigEntry( &entry );
  971. continue;
  972. }
  973. ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  974. if( ssltoken && personality ) {
  975. if( !strcasecmp(ssltoken, "internal") ||
  976. !strcasecmp(ssltoken, "internal (software)") ) {
  977. /* Translate config internal name to more
  978. * readable form. Certificate name is just
  979. * the personality for internal tokens.
  980. */
  981. token = slapi_ch_strdup(internalTokenName);
  982. PL_strncpyz(cert_name, personality, sizeof(cert_name));
  983. slapi_ch_free((void **) &ssltoken);
  984. } else {
  985. /* external PKCS #11 token - attach token name */
  986. /*ssltoken was already dupped and we don't need it anymore*/
  987. token = ssltoken;
  988. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  989. }
  990. } else {
  991. errorCode = PR_GetError();
  992. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  993. "family information. Missing nsssltoken or"
  994. "nssslpersonalityssl in %s ("
  995. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  996. *family, errorCode, slapd_pr_strerror(errorCode));
  997. slapi_ch_free((void **) &ssltoken);
  998. slapi_ch_free((void **) &personality);
  999. slapi_ch_free((void **) &cipher);
  1000. freeConfigEntry( &entry );
  1001. continue;
  1002. }
  1003. } else { /* external PKCS #11 cipher */
  1004. char *ssltoken;
  1005. ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  1006. if( token && personality ) {
  1007. /* free the old token and remember the new one */
  1008. if (token) slapi_ch_free((void **)&token);
  1009. token = ssltoken; /*ssltoken was already dupped and we don't need it anymore*/
  1010. /* external PKCS #11 token - attach token name */
  1011. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1012. } else {
  1013. errorCode = PR_GetError();
  1014. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1015. "family information. Missing nsssltoken or"
  1016. "nssslpersonalityssl in %s ("
  1017. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1018. *family, errorCode, slapd_pr_strerror(errorCode));
  1019. slapi_ch_free((void **) &ssltoken);
  1020. slapi_ch_free((void **) &personality);
  1021. slapi_ch_free((void **) &cipher);
  1022. freeConfigEntry( &entry );
  1023. continue;
  1024. }
  1025. }
  1026. slapi_ch_free((void **) &personality);
  1027. slapi_ch_free((void **) &cipher);
  1028. freeConfigEntry( &entry );
  1029. } /* end of for */
  1030. freeChildren( family_list );
  1031. }
  1032. /* Free config data */
  1033. /* We cannot allow NSS to cache outgoing client auth connections -
  1034. each client auth connection must have it's own non-shared SSL
  1035. connection to the peer so that it will go through the
  1036. entire handshake protocol every time including the use of its
  1037. own unique client cert - see bug 605457
  1038. */
  1039. ldapssl_set_option(ld, SSL_NO_CACHE, PR_TRUE);
  1040. #ifndef _WIN32
  1041. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  1042. err = SVRCORE_StdPinGetPin( &pw, StdPinObj, token );
  1043. #else
  1044. AltPinObj = (SVRCOREAltPinObj *)SVRCORE_GetRegisteredPinObj();
  1045. pw = SVRCORE_GetPin( (SVRCOREPinObj *)AltPinObj, token, PR_FALSE);
  1046. #endif
  1047. if ( err != SVRCORE_Success || pw == NULL) {
  1048. errorCode = PR_GetError();
  1049. slapd_SSL_warn("SSL client authentication cannot be used "
  1050. "(no password). (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1051. errorCode, slapd_pr_strerror(errorCode));
  1052. } else {
  1053. rc = ldapssl_enable_clientauth (ld, SERVER_KEY_NAME, pw, cert_name);
  1054. if (rc != 0) {
  1055. errorCode = PR_GetError();
  1056. slapd_SSL_warn("ldapssl_enable_clientauth(%s, %s) %i ("
  1057. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1058. SERVER_KEY_NAME, cert_name, rc,
  1059. errorCode, slapd_pr_strerror(errorCode));
  1060. }
  1061. }
  1062. if (token) slapi_ch_free((void**)&token);
  1063. slapi_ch_free((void**)&pw);
  1064. LDAPDebug (LDAP_DEBUG_TRACE, "slapd_SSL_client_auth() %i\n", rc, 0, 0);
  1065. return rc;
  1066. }
  1067. int
  1068. slapd_simple_client_bind_s(LDAP* ld, char* DN, char* pw, int LDAPv)
  1069. {
  1070. int rc;
  1071. PRErrorCode errorCode;
  1072. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *) &LDAPv);
  1073. rc = ldap_simple_bind_s (ld, DN, pw);
  1074. if (rc != 0) {
  1075. errorCode = PR_GetError();
  1076. slapd_SSL_warn("ldap_simple_bind_s(%s, %s) %i (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1077. DN, pw, rc, errorCode, slapd_pr_strerror(errorCode));
  1078. }
  1079. LDAPDebug (LDAP_DEBUG_TRACE, "slapd_simple_client_bind_s(%s, %i) %i\n", DN, LDAPv, rc);
  1080. return rc;
  1081. }
  1082. int
  1083. slapd_SSL_client_bind_s (LDAP* ld, char* DN, char* pw, int use_SSL, int LDAPv)
  1084. {
  1085. int rc;
  1086. struct berval noCred = {0, 0};
  1087. if (!use_SSL || LDAPv == LDAP_VERSION2) {
  1088. rc = slapd_simple_client_bind_s(ld, DN, pw, LDAPv);
  1089. } else {
  1090. LDAPDebug (
  1091. LDAP_DEBUG_TRACE,
  1092. "slapd_SSL_client_bind_s: Trying SSL Client Authentication\n",
  1093. 0, 0, 0);
  1094. rc = slapd_SSL_client_auth(ld);
  1095. if(rc != 0)
  1096. {
  1097. LDAPDebug (
  1098. LDAP_DEBUG_TRACE,
  1099. "slapd_SSL_client_bind_s: SSL Client Auth Failed during replication Bind\n",
  1100. 0, 0, 0);
  1101. return rc;
  1102. }
  1103. rc = ldap_sasl_bind_s (ld, "", LDAP_SASL_EXTERNAL, &noCred,
  1104. NULL /* LDAPControl **serverctrls */,
  1105. NULL /* LDAPControl **clientctrls */,
  1106. NULL /* struct berval **servercredp */);
  1107. }
  1108. LDAPDebug (
  1109. LDAP_DEBUG_TRACE,
  1110. "slapd_SSL_client_bind_s(%i,%i) %i\n", use_SSL, LDAPv, rc);
  1111. return rc;
  1112. }
  1113. int
  1114. slapd_sasl_ext_client_bind (LDAP* ld, int **msgid)
  1115. {
  1116. int rc;
  1117. PRErrorCode errorCode;
  1118. struct berval noCred = {0, 0};
  1119. LDAPDebug (
  1120. LDAP_DEBUG_TRACE,
  1121. "slapd_sasl_ext_client_bind: Trying SSL Client Authentication\n",
  1122. 0, 0, 0);
  1123. rc = slapd_SSL_client_auth(ld);
  1124. if(rc != 0)
  1125. {
  1126. LDAPDebug (
  1127. LDAP_DEBUG_TRACE,
  1128. "slapd_sasl_ext_client_bind: SSL Client Auth Failed during replication Bind\n",
  1129. 0, 0, 0);
  1130. return rc;
  1131. }
  1132. rc = ldap_sasl_bind (ld, "", LDAP_SASL_EXTERNAL, &noCred,
  1133. NULL,
  1134. NULL,
  1135. *msgid);
  1136. if (rc != 0) {
  1137. errorCode = PR_GetError();
  1138. slapd_SSL_warn("ldap_sasl_bind(\"\",LDAP_SASL_EXTERNAL) %i (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1139. rc, errorCode, slapd_pr_strerror(errorCode));
  1140. }
  1141. LDAPDebug (
  1142. LDAP_DEBUG_TRACE,
  1143. "slapd_sasl_ext_client_bind %i\n", rc, 0, 0);
  1144. return rc;
  1145. }
  1146. int slapd_Client_auth(LDAP* ld)
  1147. {
  1148. int rc=0;
  1149. rc = slapd_SSL_client_auth (ld);
  1150. return rc;
  1151. }
  1152. /* Function for keeping track of the SSL initialization status:
  1153. * - returns 1: when slapd_ssl_init has been executed
  1154. */
  1155. int
  1156. slapd_security_library_is_initialized()
  1157. {
  1158. return _security_library_initialized;
  1159. }
  1160. /* Function for keeping track of the SSL listener socket initialization status:
  1161. * - returns 1: when slapd_ssl_init2 has been executed
  1162. */
  1163. int
  1164. slapd_ssl_listener_is_initialized()
  1165. {
  1166. return _ssl_listener_initialized;
  1167. }
  1168. /* memory to store tmpdir is allocated and returned; caller should free it. */
  1169. char* slapd_get_tmp_dir()
  1170. {
  1171. static char tmp[MAXPATHLEN];
  1172. char* tmpdir = NULL;;
  1173. #if defined( XP_WIN32 )
  1174. unsigned ilen;
  1175. char pch;
  1176. #endif
  1177. struct stat ffinfo;
  1178. tmp[0] = '\0';
  1179. if((tmpdir = config_get_tmpdir()) == NULL)
  1180. {
  1181. slapi_log_error(
  1182. SLAPI_LOG_FATAL,
  1183. "slapd_get_tmp_dir",
  1184. "config_get_tmpdir returns NULL Setting tmp dir to default\n");
  1185. #if defined( XP_WIN32 )
  1186. ilen = sizeof(tmp);
  1187. GetTempPath( ilen, tmp );
  1188. tmp[ilen-1] = (char)0;
  1189. ilen = strlen(tmp);
  1190. /* Remove trailing slash. */
  1191. pch = tmp[ilen-1];
  1192. if( pch == '\\' || pch == '/' )
  1193. tmp[ilen-1] = '\0';
  1194. #else
  1195. strcpy(tmp, "/tmp");
  1196. #endif
  1197. return slapi_ch_strdup(tmp);
  1198. }
  1199. #if defined( XP_WIN32 )
  1200. {
  1201. char *ptr = NULL;
  1202. char *endptr = tmpdir + strlen(tmpdir);
  1203. for(ptr = tmpdir; ptr < endptr; ptr++)
  1204. {
  1205. if('/' == *ptr)
  1206. *ptr = '\\';
  1207. }
  1208. }
  1209. #endif
  1210. if(stat(tmpdir, &ffinfo) == -1)
  1211. #if defined( XP_WIN32 )
  1212. if(CreateDirectory(tmpdir, NULL) == 0)
  1213. {
  1214. slapi_log_error(
  1215. SLAPI_LOG_FATAL,
  1216. "slapd_get_tmp_dir",
  1217. "CreateDirectory(%s, NULL) Error: %s\n",
  1218. tmpdir, strerror(errno));
  1219. }
  1220. #else
  1221. if(mkdir(tmpdir, 00770) == -1)
  1222. {
  1223. slapi_log_error(
  1224. SLAPI_LOG_FATAL,
  1225. "slapd_get_tmp_dir",
  1226. "mkdir(%s, 00770) Error: %s\n",
  1227. tmpdir, strerror(errno));
  1228. }
  1229. #endif
  1230. return ( tmpdir );
  1231. }