ssl.c 90 KB

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