ssl.c 79 KB

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