ssl.c 72 KB

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