ssl.c 44 KB

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