ssl.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* SSL-related stuff for slapd */
  42. #if defined( _WINDOWS )
  43. #include <windows.h>
  44. #include <winsock.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include "proto-ntutil.h"
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <direct.h>
  51. #include <io.h>
  52. #endif
  53. #include <stdio.h>
  54. #include <sys/param.h>
  55. #include <ssl.h>
  56. #include <nss.h>
  57. #include <key.h>
  58. #include <sslproto.h>
  59. #include "secmod.h"
  60. #include <string.h>
  61. #include <errno.h>
  62. #define NEED_TOK_DES /* defines tokDes and ptokDes - see slap.h */
  63. #include "slap.h"
  64. #include "svrcore.h"
  65. #include "fe.h"
  66. #include "certdb.h"
  67. #if !defined(USE_OPENLDAP)
  68. #include "ldap_ssl.h"
  69. #endif
  70. /* For IRIX... */
  71. #ifndef MAXPATHLEN
  72. #define MAXPATHLEN 1024
  73. #endif
  74. #if NSS_VMAJOR * 100 + NSS_VMINOR >= 315
  75. /* 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, tokDes, ptokDes, 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. /* ugaston- Cipher preferences must be set before any sslSocket is created
  1231. * for such sockets to take preferences into account.
  1232. */
  1233. freeConfigEntry( &entry );
  1234. /* Introduce a way of knowing whether slapd_ssl_init has
  1235. * already been executed. */
  1236. _security_library_initialized = 1;
  1237. return 0;
  1238. }
  1239. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1240. /*
  1241. * val: sslVersionMin/Max value set in cn=encription,cn=config (INPUT)
  1242. * rval: Corresponding value to set SSLVersionRange (OUTPUT)
  1243. * ismin: True if val is sslVersionMin value
  1244. */
  1245. #define SSLSTR "ssl"
  1246. #define SSLLEN (sizeof(SSLSTR) - 1)
  1247. #define TLSSTR "tls"
  1248. #define TLSLEN (sizeof(TLSSTR) - 1)
  1249. static int
  1250. set_NSS_version(char *val, PRUint16 *rval, int ismin)
  1251. {
  1252. char *vp, *endp;
  1253. int vnum;
  1254. char emin[VERSION_STR_LENGTH], emax[VERSION_STR_LENGTH];
  1255. if (NULL == rval) {
  1256. return 1;
  1257. }
  1258. (void) slapi_getSSLVersion_str(enabledNSSVersions.min, emin, sizeof(emin));
  1259. (void) slapi_getSSLVersion_str(enabledNSSVersions.max, emax, sizeof(emax));
  1260. if (!strncasecmp(val, SSLSTR, SSLLEN)) { /* ssl# */
  1261. vp = val + SSLLEN;
  1262. vnum = strtol(vp, &endp, 10);
  1263. if (2 == vnum) {
  1264. if (ismin) {
  1265. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_2) {
  1266. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1267. "\"%s\" is lower than the supported version; "
  1268. "the default value \"%s\" is used.",
  1269. val, emin);
  1270. (*rval) = enabledNSSVersions.min;
  1271. } else {
  1272. (*rval) = SSL_LIBRARY_VERSION_2;
  1273. }
  1274. } else {
  1275. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_2) {
  1276. /* never happens */
  1277. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1278. "\"%s\" is higher than the supported version; "
  1279. "the default value \"%s\" is used.",
  1280. val, emax);
  1281. (*rval) = enabledNSSVersions.max;
  1282. } else {
  1283. (*rval) = SSL_LIBRARY_VERSION_2;
  1284. }
  1285. }
  1286. } else if (3 == vnum) {
  1287. if (ismin) {
  1288. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_3_0) {
  1289. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1290. "\"%s\" is lower than the supported version; "
  1291. "the default value \"%s\" is used.",
  1292. val, emin);
  1293. (*rval) = enabledNSSVersions.min;
  1294. } else {
  1295. (*rval) = SSL_LIBRARY_VERSION_3_0;
  1296. }
  1297. } else {
  1298. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_3_0) {
  1299. /* never happens */
  1300. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1301. "\"%s\" is higher than the supported version; "
  1302. "the default value \"%s\" is used.",
  1303. val, emax);
  1304. (*rval) = enabledNSSVersions.max;
  1305. } else {
  1306. (*rval) = SSL_LIBRARY_VERSION_3_0;
  1307. }
  1308. }
  1309. } else {
  1310. if (ismin) {
  1311. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1312. "\"%s\" is invalid; the default value \"%s\" is used.",
  1313. val, emin);
  1314. (*rval) = enabledNSSVersions.min;
  1315. } else {
  1316. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1317. "\"%s\" is invalid; the default value \"%s\" is used.",
  1318. val, emax);
  1319. (*rval) = enabledNSSVersions.max;
  1320. }
  1321. }
  1322. } else if (!strncasecmp(val, TLSSTR, TLSLEN)) { /* tls# */
  1323. float tlsv;
  1324. vp = val + TLSLEN;
  1325. sscanf(vp, "%4f", &tlsv);
  1326. if (tlsv < 1.1) { /* TLS1.0 */
  1327. if (ismin) {
  1328. if (enabledNSSVersions.min > CURRENT_DEFAULT_SSL_VERSION) {
  1329. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1330. "\"%s\" is lower than the supported version; "
  1331. "the default value \"%s\" is used.",
  1332. val, emin);
  1333. (*rval) = enabledNSSVersions.min;
  1334. } else {
  1335. (*rval) = CURRENT_DEFAULT_SSL_VERSION;
  1336. }
  1337. } else {
  1338. if (enabledNSSVersions.max < CURRENT_DEFAULT_SSL_VERSION) {
  1339. /* never happens */
  1340. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1341. "\"%s\" is higher than the supported version; "
  1342. "the default value \"%s\" is used.",
  1343. val, emax);
  1344. (*rval) = enabledNSSVersions.max;
  1345. } else {
  1346. (*rval) = CURRENT_DEFAULT_SSL_VERSION;
  1347. }
  1348. }
  1349. } else if (tlsv < 1.2) { /* TLS1.1 */
  1350. if (ismin) {
  1351. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_TLS_1_1) {
  1352. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1353. "\"%s\" is lower than the supported version; "
  1354. "the default value \"%s\" is used.",
  1355. val, emin);
  1356. (*rval) = enabledNSSVersions.min;
  1357. } else {
  1358. (*rval) = SSL_LIBRARY_VERSION_TLS_1_1;
  1359. }
  1360. } else {
  1361. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_TLS_1_1) {
  1362. /* never happens */
  1363. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1364. "\"%s\" is higher than the supported version; "
  1365. "the default value \"%s\" is used.",
  1366. val, emax);
  1367. (*rval) = enabledNSSVersions.max;
  1368. } else {
  1369. (*rval) = SSL_LIBRARY_VERSION_TLS_1_1;
  1370. }
  1371. }
  1372. } else if (tlsv < 1.3) { /* TLS1.2 */
  1373. #if defined(NSS_TLS12)
  1374. if (ismin) {
  1375. if (enabledNSSVersions.min > SSL_LIBRARY_VERSION_TLS_1_2) {
  1376. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1377. "\"%s\" is lower than the supported version; "
  1378. "the default value \"%s\" is used.",
  1379. val, emin);
  1380. (*rval) = enabledNSSVersions.min;
  1381. } else {
  1382. (*rval) = SSL_LIBRARY_VERSION_TLS_1_2;
  1383. }
  1384. } else {
  1385. if (enabledNSSVersions.max < SSL_LIBRARY_VERSION_TLS_1_2) {
  1386. /* never happens */
  1387. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1388. "\"%s\" is higher than the supported version; "
  1389. "the default value \"%s\" is used.",
  1390. val, emax);
  1391. (*rval) = enabledNSSVersions.max;
  1392. } else {
  1393. (*rval) = SSL_LIBRARY_VERSION_TLS_1_2;
  1394. }
  1395. }
  1396. #endif
  1397. } else { /* Specified TLS is newer than supported */
  1398. if (ismin) {
  1399. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1400. "\"%s\" is out of the range of the supported version; "
  1401. "the default value \"%s\" is used.",
  1402. val, emin);
  1403. (*rval) = enabledNSSVersions.min;
  1404. } else {
  1405. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1406. "\"%s\" is out of the range of the supported version; "
  1407. "the default value \"%s\" is used.",
  1408. val, emax);
  1409. (*rval) = enabledNSSVersions.max;
  1410. }
  1411. }
  1412. } else {
  1413. if (ismin) {
  1414. slapd_SSL_warn("Security Initialization: The value of sslVersionMin "
  1415. "\"%s\" is invalid; the default value \"%s\" is used.",
  1416. val, emin);
  1417. (*rval) = enabledNSSVersions.min;
  1418. } else {
  1419. slapd_SSL_warn("Security Initialization: The value of sslVersionMax "
  1420. "\"%s\" is invalid; the default value \"%s\" is used.",
  1421. val, emax);
  1422. (*rval) = enabledNSSVersions.max;
  1423. }
  1424. }
  1425. return 0;
  1426. }
  1427. #undef SSLSTR
  1428. #undef SSLLEN
  1429. #undef TLSSTR
  1430. #undef TLSLEN
  1431. #endif
  1432. int
  1433. slapd_ssl_init2(PRFileDesc **fd, int startTLS)
  1434. {
  1435. PRFileDesc *pr_sock, *sock = (*fd);
  1436. PRErrorCode errorCode;
  1437. SECStatus rv = SECFailure;
  1438. char ** family_list;
  1439. CERTCertificate *cert = NULL;
  1440. SECKEYPrivateKey *key = NULL;
  1441. char errorbuf[BUFSIZ];
  1442. char *val = NULL;
  1443. char *default_val = NULL;
  1444. int nFamilies = 0;
  1445. SECStatus sslStatus;
  1446. int slapd_SSLclientAuth;
  1447. char* tmpDir;
  1448. Slapi_Entry *e = NULL;
  1449. PRBool fipsMode = PR_FALSE;
  1450. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1451. PRUint16 NSSVersionMin = enabledNSSVersions.min;
  1452. PRUint16 NSSVersionMax = enabledNSSVersions.max;
  1453. char mymin[VERSION_STR_LENGTH], mymax[VERSION_STR_LENGTH];
  1454. char newmax[VERSION_STR_LENGTH];
  1455. #endif
  1456. char cipher_string[1024];
  1457. int allowweakcipher = CIPHER_SET_DEFAULTWEAKCIPHER;
  1458. /* turn off the PKCS11 pin interactive mode */
  1459. #ifndef _WIN32
  1460. SVRCOREStdPinObj *StdPinObj;
  1461. if (svrcore_setup()) {
  1462. return 1;
  1463. }
  1464. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  1465. SVRCORE_SetStdPinInteractive(StdPinObj, PR_FALSE);
  1466. #endif
  1467. errorbuf[0] = '\0';
  1468. /* Import pr fd into SSL */
  1469. pr_sock = SSL_ImportFD( NULL, sock );
  1470. if( pr_sock == (PRFileDesc *)NULL ) {
  1471. errorCode = PR_GetError();
  1472. slapd_SSL_warn("Security Initialization: Failed to import NSPR "
  1473. "fd into SSL (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1474. errorCode, slapd_pr_strerror(errorCode));
  1475. return 1;
  1476. }
  1477. (*fd) = pr_sock;
  1478. /* Step / Three.6 /
  1479. * - If in FIPS mode, authenticate to the token before
  1480. * doing anything else
  1481. */
  1482. {
  1483. PK11SlotInfo *slot = slapd_pk11_getInternalSlot();
  1484. if (!slot) {
  1485. errorCode = PR_GetError();
  1486. slapd_SSL_warn("Security Initialization: Unable to get internal slot ("
  1487. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1488. errorCode, slapd_pr_strerror(errorCode));
  1489. return -1;
  1490. }
  1491. if(slapd_pk11_isFIPS()) {
  1492. if(slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess) {
  1493. errorCode = PR_GetError();
  1494. slapd_SSL_warn("Security Initialization: Unable to authenticate ("
  1495. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1496. errorCode, slapd_pr_strerror(errorCode));
  1497. return -1;
  1498. }
  1499. fipsMode = PR_TRUE;
  1500. /* FIPS does not like to use SSLv3 */
  1501. enableSSL3 = PR_FALSE;
  1502. }
  1503. slapd_pk11_setSlotPWValues(slot, 0, 0);
  1504. }
  1505. /*
  1506. * Now, get the complete list of cipher families. Each family
  1507. * has a token name and personality name which we'll use to find
  1508. * appropriate keys and certs, and call SSL_ConfigSecureServer
  1509. * with.
  1510. */
  1511. if((family_list = getChildren(configDN))) {
  1512. char **family;
  1513. char cert_name[1024];
  1514. char *token;
  1515. char *personality;
  1516. char *activation;
  1517. for (family = family_list; *family; family++) {
  1518. token = NULL;
  1519. personality = NULL;
  1520. activation = NULL;
  1521. getConfigEntry( *family, &e );
  1522. if ( e == NULL ) {
  1523. continue;
  1524. }
  1525. activation = slapi_entry_attr_get_charptr( e, "nssslactivation" );
  1526. if((!activation) || (!PL_strcasecmp(activation, "off"))) {
  1527. /* this family was turned off, goto next */
  1528. slapi_ch_free((void **) &activation);
  1529. freeConfigEntry( &e );
  1530. continue;
  1531. }
  1532. slapi_ch_free((void **) &activation);
  1533. token = slapi_entry_attr_get_charptr( e, "nsssltoken" );
  1534. personality = slapi_entry_attr_get_charptr( e, "nssslpersonalityssl" );
  1535. if( token && personality ) {
  1536. if( !PL_strcasecmp(token, "internal") ||
  1537. !PL_strcasecmp(token, "internal (software)") )
  1538. PL_strncpyz(cert_name, personality, sizeof(cert_name));
  1539. else
  1540. /* external PKCS #11 token - attach token name */
  1541. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1542. }
  1543. else {
  1544. errorCode = PR_GetError();
  1545. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1546. "family information. Missing nsssltoken or"
  1547. "nssslpersonalityssl in %s ("
  1548. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1549. *family, errorCode, slapd_pr_strerror(errorCode));
  1550. slapi_ch_free((void **) &token);
  1551. slapi_ch_free((void **) &personality);
  1552. freeConfigEntry( &e );
  1553. continue;
  1554. }
  1555. slapi_ch_free((void **) &token);
  1556. /* Step Four -- Locate the server certificate */
  1557. cert = slapd_pk11_findCertFromNickname(cert_name, NULL);
  1558. if (cert == NULL) {
  1559. errorCode = PR_GetError();
  1560. slapd_SSL_warn("Security Initialization: Can't find "
  1561. "certificate (%s) for family %s ("
  1562. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1563. cert_name, *family,
  1564. errorCode, slapd_pr_strerror(errorCode));
  1565. }
  1566. /* Step Five -- Get the private key from cert */
  1567. if( cert != NULL )
  1568. key = slapd_pk11_findKeyByAnyCert(cert, NULL);
  1569. if (key == NULL) {
  1570. errorCode = PR_GetError();
  1571. slapd_SSL_warn("Security Initialization: Unable to retrieve "
  1572. "private key for cert %s of family %s ("
  1573. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1574. cert_name, *family,
  1575. errorCode, slapd_pr_strerror(errorCode));
  1576. slapi_ch_free((void **) &personality);
  1577. CERT_DestroyCertificate(cert);
  1578. cert = NULL;
  1579. freeConfigEntry( &e );
  1580. continue;
  1581. }
  1582. /* Step Six -- Configure Secure Server Mode */
  1583. if(pr_sock) {
  1584. SECCertificateUsage returnedUsages;
  1585. if (config_get_validate_cert_switch() == SLAPD_VALIDATE_CERT_OFF) {
  1586. /* If we're set to ignore certificate verification issues,
  1587. * just skip performing verification. */
  1588. rv = SECSuccess;
  1589. } else {
  1590. /* Check if the certificate is valid. */
  1591. rv = CERT_VerifyCertificateNow(
  1592. CERT_GetDefaultCertDB(), cert, PR_TRUE,
  1593. certificateUsageSSLServer,
  1594. SSL_RevealPinArg(pr_sock),
  1595. &returnedUsages);
  1596. if (rv != SECSuccess) {
  1597. /* Log warning */
  1598. errorCode = PR_GetError();
  1599. slapd_SSL_warn("CERT_VerifyCertificateNow: "
  1600. "verify certificate failed "
  1601. "for cert %s of family %s ("
  1602. SLAPI_COMPONENT_NAME_NSPR
  1603. " error %d - %s)",
  1604. cert_name, *family, errorCode,
  1605. slapd_pr_strerror(errorCode));
  1606. /* If we're set to only warn, go ahead and
  1607. * override rv to allow us to start up. */
  1608. if (config_get_validate_cert_switch() == SLAPD_VALIDATE_CERT_WARN) {
  1609. rv = SECSuccess;
  1610. }
  1611. }
  1612. }
  1613. if (SECSuccess == rv) {
  1614. if( slapd_pk11_fortezzaHasKEA(cert) == PR_TRUE ) {
  1615. rv = SSL_ConfigSecureServer(*fd, cert, key, kt_fortezza);
  1616. }
  1617. else {
  1618. rv = SSL_ConfigSecureServer(*fd, cert, key, kt_rsa);
  1619. }
  1620. if (SECSuccess != rv) {
  1621. errorCode = PR_GetError();
  1622. slapd_SSL_warn("ConfigSecureServer: "
  1623. "Server key/certificate is "
  1624. "bad for cert %s of family %s ("
  1625. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1626. cert_name, *family, errorCode,
  1627. slapd_pr_strerror(errorCode));
  1628. }
  1629. }
  1630. }
  1631. if (cert) {
  1632. CERT_DestroyCertificate(cert);
  1633. cert = NULL;
  1634. }
  1635. if (key) {
  1636. slapd_pk11_DestroyPrivateKey(key);
  1637. key = NULL;
  1638. }
  1639. slapi_ch_free((void **) &personality);
  1640. if (SECSuccess != rv) {
  1641. freeConfigEntry( &e );
  1642. continue;
  1643. }
  1644. nFamilies++;
  1645. freeConfigEntry( &e );
  1646. }
  1647. freeChildren( family_list );
  1648. }
  1649. if ( !nFamilies ) {
  1650. slapd_SSL_error("None of the cipher are valid");
  1651. return -1;
  1652. }
  1653. /* Step Seven -- Configure Server Session ID Cache */
  1654. tmpDir = slapd_get_tmp_dir();
  1655. slapi_log_error(SLAPI_LOG_TRACE,
  1656. "slapd_ssl_init2", "tmp dir = %s\n", tmpDir);
  1657. rv = SSL_ConfigServerSessionIDCache(0, stimeout, stimeout, tmpDir);
  1658. slapi_ch_free_string(&tmpDir);
  1659. if (rv) {
  1660. errorCode = PR_GetError();
  1661. if (errorCode == ENOSPC) {
  1662. slapd_SSL_error("Config of server nonce cache failed, "
  1663. "out of disk space! Make more room in /tmp "
  1664. "and try again. (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1665. errorCode, slapd_pr_strerror(errorCode));
  1666. }
  1667. else {
  1668. slapd_SSL_error("Config of server nonce cache failed (error %d - %s)",
  1669. errorCode, slapd_pr_strerror(errorCode));
  1670. }
  1671. return rv;
  1672. }
  1673. sslStatus = SSL_OptionSet(pr_sock, SSL_SECURITY, PR_TRUE);
  1674. if (sslStatus != SECSuccess) {
  1675. errorCode = PR_GetError();
  1676. slapd_SSL_warn("Security Initialization: Failed to enable security "
  1677. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1678. errorCode, slapd_pr_strerror(errorCode));
  1679. return -1;
  1680. }
  1681. /* Explicitly disabling SSL2 - NGK */
  1682. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL2, enableSSL2);
  1683. if (sslStatus != SECSuccess) {
  1684. errorCode = PR_GetError();
  1685. slapd_SSL_warn("Security Initialization: Failed to %s SSLv2 "
  1686. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1687. enableSSL2 ? "enable" : "disable",
  1688. errorCode, slapd_pr_strerror(errorCode));
  1689. return -1;
  1690. }
  1691. /* Retrieve the SSL Client Authentication status from cn=config */
  1692. /* Set a default value if no value found */
  1693. getConfigEntry( configDN, &e );
  1694. val = NULL;
  1695. if ( e != NULL ) {
  1696. val = slapi_entry_attr_get_charptr( e, "nssslclientauth" );
  1697. }
  1698. if( !val ) {
  1699. errorCode = PR_GetError();
  1700. slapd_SSL_warn("Security Initialization: Cannot get SSL Client "
  1701. "Authentication status. No nsslclientauth in %s ("
  1702. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1703. configDN, errorCode, slapd_pr_strerror(errorCode));
  1704. switch( SLAPD_SSLCLIENTAUTH_DEFAULT ) {
  1705. case SLAPD_SSLCLIENTAUTH_OFF:
  1706. default_val = "off";
  1707. break;
  1708. case SLAPD_SSLCLIENTAUTH_ALLOWED:
  1709. default_val = "allowed";
  1710. break;
  1711. case SLAPD_SSLCLIENTAUTH_REQUIRED:
  1712. default_val = "required";
  1713. break;
  1714. default:
  1715. default_val = "allowed";
  1716. break;
  1717. }
  1718. val = default_val;
  1719. }
  1720. if( config_set_SSLclientAuth( "nssslclientauth", val, errorbuf,
  1721. CONFIG_APPLY ) != LDAP_SUCCESS ) {
  1722. errorCode = PR_GetError();
  1723. slapd_SSL_warn("Security Initialization: Cannot set SSL Client "
  1724. "Authentication status to \"%s\", error (%s). "
  1725. "Supported values are \"off\", \"allowed\" "
  1726. "and \"required\". (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1727. val, errorbuf, errorCode, slapd_pr_strerror(errorCode));
  1728. }
  1729. if (val != default_val) {
  1730. slapi_ch_free_string(&val);
  1731. }
  1732. if ( e != NULL ) {
  1733. val = slapi_entry_attr_get_charptr( e, "nsSSL3" );
  1734. if ( val ) {
  1735. if ( !PL_strcasecmp( val, "off" ) ) {
  1736. enableSSL3 = PR_FALSE;
  1737. } else if ( !PL_strcasecmp( val, "on" ) ) {
  1738. enableSSL3 = PR_TRUE;
  1739. } else {
  1740. enableSSL3 = slapi_entry_attr_get_bool( e, "nsSSL3" );
  1741. }
  1742. if ( fipsMode && enableSSL3 ) {
  1743. slapd_SSL_warn("Security Initialization: FIPS mode is enabled and "
  1744. "nsSSL3 explicitly set to on - SSLv3 is not approved "
  1745. "for use in FIPS mode - SSLv3 will be disabled - if "
  1746. "you want to use SSLv3, you must use modutil to "
  1747. "disable FIPS in the internal token.");
  1748. enableSSL3 = PR_FALSE;
  1749. }
  1750. }
  1751. slapi_ch_free_string( &val );
  1752. val = slapi_entry_attr_get_charptr( e, "nsTLS1" );
  1753. if ( val ) {
  1754. if ( !PL_strcasecmp( val, "off" ) ) {
  1755. enableTLS1 = PR_FALSE;
  1756. } else if ( !PL_strcasecmp( val, "on" ) ) {
  1757. enableTLS1 = PR_TRUE;
  1758. } else {
  1759. enableTLS1 = slapi_entry_attr_get_bool( e, "nsTLS1" );
  1760. }
  1761. } else if (enabledNSSVersions.max >= CURRENT_DEFAULT_SSL_VERSION) {
  1762. enableTLS1 = PR_TRUE; /* If available, enable TLS1 */
  1763. }
  1764. slapi_ch_free_string( &val );
  1765. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1766. val = slapi_entry_attr_get_charptr( e, "sslVersionMin" );
  1767. if ( val ) {
  1768. (void)set_NSS_version(val, &NSSVersionMin, 1);
  1769. }
  1770. slapi_ch_free_string( &val );
  1771. val = slapi_entry_attr_get_charptr( e, "sslVersionMax" );
  1772. if ( val ) {
  1773. (void)set_NSS_version(val, &NSSVersionMax, 0);
  1774. }
  1775. slapi_ch_free_string( &val );
  1776. if (NSSVersionMin > NSSVersionMax) {
  1777. (void) slapi_getSSLVersion_str(NSSVersionMin, mymin, sizeof(mymin));
  1778. (void) slapi_getSSLVersion_str(NSSVersionMax, mymax, sizeof(mymax));
  1779. slapd_SSL_warn("The min value of NSS version range \"%s\" is greater than the max value \"%s\".",
  1780. mymin, mymax);
  1781. (void) slapi_getSSLVersion_str(enabledNSSVersions.max, newmax, sizeof(newmax));
  1782. slapd_SSL_warn("Reset the max \"%s\" to supported max \"%s\".",
  1783. mymax, newmax);
  1784. NSSVersionMax = enabledNSSVersions.max;
  1785. }
  1786. #endif
  1787. }
  1788. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1789. if (NSSVersionMin > 0) {
  1790. /* Use new NSS API SSL_VersionRangeSet (NSS3.14 or newer) */
  1791. slapdNSSVersions.min = NSSVersionMin;
  1792. slapdNSSVersions.max = NSSVersionMax;
  1793. restrict_SSLVersionRange();
  1794. (void) slapi_getSSLVersion_str(slapdNSSVersions.min, mymin, sizeof(mymin));
  1795. (void) slapi_getSSLVersion_str(slapdNSSVersions.max, mymax, sizeof(mymax));
  1796. slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
  1797. "Configured SSL version range: min: %s, max: %s\n",
  1798. mymin, mymax);
  1799. sslStatus = SSL_VersionRangeSet(pr_sock, &slapdNSSVersions);
  1800. if (sslStatus == SECSuccess) {
  1801. /* Set the restricted value to the cn=encryption entry */
  1802. } else {
  1803. slapd_SSL_error("SSL Initialization 2: "
  1804. "Failed to set SSL range: min: %s, max: %s\n",
  1805. mymin, mymax);
  1806. }
  1807. } else {
  1808. #endif
  1809. /* deprecated code */
  1810. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL3, enableSSL3);
  1811. if (sslStatus != SECSuccess) {
  1812. errorCode = PR_GetError();
  1813. slapd_SSL_warn("Security Initialization: Failed to %s SSLv3 "
  1814. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1815. enableSSL3 ? "enable" : "disable",
  1816. errorCode, slapd_pr_strerror(errorCode));
  1817. }
  1818. sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_TLS, enableTLS1);
  1819. if (sslStatus != SECSuccess) {
  1820. errorCode = PR_GetError();
  1821. slapd_SSL_warn("Security Initialization: Failed to %s TLSv1 "
  1822. "on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1823. enableTLS1 ? "enable" : "disable",
  1824. errorCode, slapd_pr_strerror(errorCode));
  1825. }
  1826. #if !defined(NSS_TLS10) /* NSS_TLS11 or newer */
  1827. }
  1828. #endif
  1829. val = slapi_entry_attr_get_charptr(e, "allowWeakCipher");
  1830. if (val) {
  1831. if (!PL_strcasecmp(val, "off") || !PL_strcasecmp(val, "false") ||
  1832. !PL_strcmp(val, "0") || !PL_strcasecmp(val, "no")) {
  1833. allowweakcipher = CIPHER_SET_DISALLOWWEAKCIPHER;
  1834. } else if (!PL_strcasecmp(val, "on") || !PL_strcasecmp(val, "true") ||
  1835. !PL_strcmp(val, "1") || !PL_strcasecmp(val, "yes")) {
  1836. allowweakcipher = CIPHER_SET_ALLOWWEAKCIPHER;
  1837. } else {
  1838. slapd_SSL_warn("The value of allowWeakCipher \"%s\" in %s is invalid.",
  1839. "Ignoring it and set it to default.", val, configDN);
  1840. }
  1841. }
  1842. slapi_ch_free((void **) &val);
  1843. /* Set SSL cipher preferences */
  1844. *cipher_string = 0;
  1845. if(ciphers && (*ciphers) && PL_strcmp(ciphers, "blank"))
  1846. PL_strncpyz(cipher_string, ciphers, sizeof(cipher_string));
  1847. slapi_ch_free((void **) &ciphers);
  1848. if ( NULL != (val = _conf_setciphers(cipher_string, allowweakcipher)) ) {
  1849. errorCode = PR_GetError();
  1850. slapd_SSL_warn("Security Initialization: Failed to set SSL cipher "
  1851. "preference information: %s (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1852. val, errorCode, slapd_pr_strerror(errorCode));
  1853. rv = 3;
  1854. slapi_ch_free((void **) &val);
  1855. }
  1856. freeConfigEntry( &e );
  1857. if(( slapd_SSLclientAuth = config_get_SSLclientAuth()) != SLAPD_SSLCLIENTAUTH_OFF ) {
  1858. int err;
  1859. switch (slapd_SSLclientAuth) {
  1860. case SLAPD_SSLCLIENTAUTH_ALLOWED:
  1861. #ifdef SSL_REQUIRE_CERTIFICATE /* new feature */
  1862. if ((err = SSL_OptionSet (pr_sock, SSL_REQUIRE_CERTIFICATE, PR_FALSE)) < 0) {
  1863. PRErrorCode prerr = PR_GetError();
  1864. LDAPDebug (LDAP_DEBUG_ANY,
  1865. "SSL_OptionSet(SSL_REQUIRE_CERTIFICATE,PR_FALSE) %d "
  1866. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1867. err, prerr, slapd_pr_strerror(prerr));
  1868. }
  1869. #endif
  1870. /* Give the client a clear opportunity to send her certificate: */
  1871. case SLAPD_SSLCLIENTAUTH_REQUIRED:
  1872. if ((err = SSL_OptionSet (pr_sock, SSL_REQUEST_CERTIFICATE, PR_TRUE)) < 0) {
  1873. PRErrorCode prerr = PR_GetError();
  1874. LDAPDebug (LDAP_DEBUG_ANY,
  1875. "SSL_OptionSet(SSL_REQUEST_CERTIFICATE,PR_TRUE) %d "
  1876. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1877. err, prerr, slapd_pr_strerror(prerr));
  1878. }
  1879. default: break;
  1880. }
  1881. }
  1882. /* Introduce a way of knowing whether slapd_ssl_init2 has
  1883. * already been executed.
  1884. * The cases in which slapd_ssl_init2 is executed during an
  1885. * Start TLS operation are not taken into account, for it is
  1886. * the fact of being executed by the server's SSL listener socket
  1887. * that matters. */
  1888. if (!startTLS)
  1889. _ssl_listener_initialized = 1; /* --ugaston */
  1890. return 0;
  1891. }
  1892. /* richm 20020227
  1893. To do LDAP client SSL init, we need to do
  1894. static void
  1895. ldapssl_basic_init( void )
  1896. {
  1897. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  1898. PR_SetConcurrency( 4 );
  1899. }
  1900. NSS_Init(certdbpath);
  1901. SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE);
  1902. SSL_OptionSetDefault(SSL_ENABLE_SSL3, PR_TRUE);
  1903. s = NSS_SetDomesticPolicy();
  1904. We already do pr_init, we don't need pr_setconcurrency, we already do nss_init and the rest
  1905. */
  1906. int
  1907. slapd_SSL_client_auth (LDAP* ld)
  1908. {
  1909. int rc = 0;
  1910. PRErrorCode errorCode;
  1911. char* pw = NULL;
  1912. char ** family_list;
  1913. Slapi_Entry *entry = NULL;
  1914. char cert_name[1024];
  1915. char *token = NULL;
  1916. #ifndef _WIN32
  1917. SVRCOREStdPinObj *StdPinObj;
  1918. #else
  1919. SVRCOREAltPinObj *AltPinObj;
  1920. #endif
  1921. SVRCOREError err = SVRCORE_Success;
  1922. if((family_list = getChildren(configDN))) {
  1923. char **family;
  1924. char *personality = NULL;
  1925. char *activation = NULL;
  1926. char *cipher = NULL;
  1927. for (family = family_list; *family; family++) {
  1928. getConfigEntry( *family, &entry );
  1929. if ( entry == NULL ) {
  1930. continue;
  1931. }
  1932. activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
  1933. if((!activation) || (!PL_strcasecmp(activation, "off"))) {
  1934. /* this family was turned off, goto next */
  1935. slapi_ch_free((void **) &activation);
  1936. freeConfigEntry( &entry );
  1937. continue;
  1938. }
  1939. slapi_ch_free((void **) &activation);
  1940. personality = slapi_entry_attr_get_charptr( entry, "nssslpersonalityssl" );
  1941. cipher = slapi_entry_attr_get_charptr( entry, "cn" );
  1942. if ( cipher && !PL_strcasecmp(cipher, "RSA" )) {
  1943. char *ssltoken;
  1944. /* If there already is a token name, use it */
  1945. if (token) {
  1946. slapi_ch_free((void **) &personality);
  1947. slapi_ch_free((void **) &cipher);
  1948. freeConfigEntry( &entry );
  1949. continue;
  1950. }
  1951. ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  1952. if( ssltoken && personality ) {
  1953. if( !PL_strcasecmp(ssltoken, "internal") ||
  1954. !PL_strcasecmp(ssltoken, "internal (software)") ) {
  1955. /* Translate config internal name to more
  1956. * readable form. Certificate name is just
  1957. * the personality for internal tokens.
  1958. */
  1959. token = slapi_ch_strdup(internalTokenName);
  1960. #if defined(USE_OPENLDAP)
  1961. /* openldap needs tokenname:certnick */
  1962. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1963. #else
  1964. PL_strncpyz(cert_name, personality, sizeof(cert_name));
  1965. #endif
  1966. slapi_ch_free((void **) &ssltoken);
  1967. } else {
  1968. /* external PKCS #11 token - attach token name */
  1969. /*ssltoken was already dupped and we don't need it anymore*/
  1970. token = ssltoken;
  1971. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1972. }
  1973. } else {
  1974. errorCode = PR_GetError();
  1975. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1976. "family information. Missing nsssltoken or"
  1977. "nssslpersonalityssl in %s ("
  1978. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  1979. *family, errorCode, slapd_pr_strerror(errorCode));
  1980. slapi_ch_free((void **) &ssltoken);
  1981. slapi_ch_free((void **) &personality);
  1982. slapi_ch_free((void **) &cipher);
  1983. freeConfigEntry( &entry );
  1984. continue;
  1985. }
  1986. } else { /* external PKCS #11 cipher */
  1987. char *ssltoken;
  1988. ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
  1989. if( token && personality ) {
  1990. /* free the old token and remember the new one */
  1991. if (token) slapi_ch_free((void **)&token);
  1992. token = ssltoken; /*ssltoken was already dupped and we don't need it anymore*/
  1993. /* external PKCS #11 token - attach token name */
  1994. PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
  1995. } else {
  1996. errorCode = PR_GetError();
  1997. slapd_SSL_warn("Security Initialization: Failed to get cipher "
  1998. "family information. Missing nsssltoken or"
  1999. "nssslpersonalityssl in %s ("
  2000. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  2001. *family, errorCode, slapd_pr_strerror(errorCode));
  2002. slapi_ch_free((void **) &ssltoken);
  2003. slapi_ch_free((void **) &personality);
  2004. slapi_ch_free((void **) &cipher);
  2005. freeConfigEntry( &entry );
  2006. continue;
  2007. }
  2008. }
  2009. slapi_ch_free((void **) &personality);
  2010. slapi_ch_free((void **) &cipher);
  2011. freeConfigEntry( &entry );
  2012. } /* end of for */
  2013. freeChildren( family_list );
  2014. }
  2015. /* Free config data */
  2016. if (!svrcore_setup()) {
  2017. #ifndef _WIN32
  2018. StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
  2019. err = SVRCORE_StdPinGetPin( &pw, StdPinObj, token );
  2020. #else
  2021. AltPinObj = (SVRCOREAltPinObj *)SVRCORE_GetRegisteredPinObj();
  2022. pw = SVRCORE_GetPin( (SVRCOREPinObj *)AltPinObj, token, PR_FALSE);
  2023. #endif
  2024. if ( err != SVRCORE_Success || pw == NULL) {
  2025. errorCode = PR_GetError();
  2026. slapd_SSL_warn("SSL client authentication cannot be used "
  2027. "(no password). (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  2028. errorCode, slapd_pr_strerror(errorCode));
  2029. } else {
  2030. #if defined(USE_OPENLDAP)
  2031. rc = ldap_set_option(ld, LDAP_OPT_X_TLS_KEYFILE, SERVER_KEY_NAME);
  2032. if (rc) {
  2033. slapd_SSL_warn("SSL client authentication cannot be used "
  2034. "unable to set the key to use to %s", SERVER_KEY_NAME);
  2035. }
  2036. rc = ldap_set_option(ld, LDAP_OPT_X_TLS_CERTFILE, cert_name);
  2037. if (rc) {
  2038. slapd_SSL_warn("SSL client authentication cannot be used "
  2039. "unable to set the cert to use to %s", cert_name);
  2040. }
  2041. /* not sure what else needs to be done for client auth - don't
  2042. currently have a way to pass in the password to use to unlock
  2043. the keydb - nor a way to disable caching */
  2044. #else /* !USE_OPENLDAP */
  2045. rc = ldapssl_enable_clientauth (ld, SERVER_KEY_NAME, pw, cert_name);
  2046. if (rc != 0) {
  2047. errorCode = PR_GetError();
  2048. slapd_SSL_warn("ldapssl_enable_clientauth(%s, %s) %i ("
  2049. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  2050. SERVER_KEY_NAME, cert_name, rc,
  2051. errorCode, slapd_pr_strerror(errorCode));
  2052. } else {
  2053. /* We cannot allow NSS to cache outgoing client auth connections -
  2054. each client auth connection must have it's own non-shared SSL
  2055. connection to the peer so that it will go through the
  2056. entire handshake protocol every time including the use of its
  2057. own unique client cert - see bug 605457
  2058. */
  2059. ldapssl_set_option(ld, SSL_NO_CACHE, PR_TRUE);
  2060. }
  2061. #endif
  2062. }
  2063. }
  2064. if (token) slapi_ch_free((void**)&token);
  2065. slapi_ch_free((void**)&pw);
  2066. LDAPDebug (LDAP_DEBUG_TRACE, "slapd_SSL_client_auth() %i\n", rc, 0, 0);
  2067. return rc;
  2068. }
  2069. /* Function for keeping track of the SSL initialization status:
  2070. * - returns 1: when slapd_ssl_init has been executed
  2071. */
  2072. int
  2073. slapd_security_library_is_initialized()
  2074. {
  2075. return _security_library_initialized;
  2076. }
  2077. /* Function for keeping track of the SSL listener socket initialization status:
  2078. * - returns 1: when slapd_ssl_init2 has been executed
  2079. */
  2080. int
  2081. slapd_ssl_listener_is_initialized()
  2082. {
  2083. return _ssl_listener_initialized;
  2084. }
  2085. int
  2086. slapd_nss_is_initialized()
  2087. {
  2088. return _nss_initialized;
  2089. }
  2090. /* memory to store tmpdir is allocated and returned; caller should free it. */
  2091. char* slapd_get_tmp_dir()
  2092. {
  2093. static char tmp[MAXPATHLEN];
  2094. char* tmpdir = NULL;;
  2095. #if defined( XP_WIN32 )
  2096. unsigned ilen;
  2097. char pch;
  2098. #endif
  2099. tmp[0] = '\0';
  2100. if((tmpdir = config_get_tmpdir()) == NULL)
  2101. {
  2102. slapi_log_error(
  2103. SLAPI_LOG_FATAL,
  2104. "slapd_get_tmp_dir",
  2105. "config_get_tmpdir returns NULL Setting tmp dir to default\n");
  2106. #if defined( XP_WIN32 )
  2107. ilen = sizeof(tmp);
  2108. GetTempPath( ilen, tmp );
  2109. tmp[ilen-1] = (char)0;
  2110. ilen = strlen(tmp);
  2111. /* Remove trailing slash. */
  2112. pch = tmp[ilen-1];
  2113. if( pch == '\\' || pch == '/' )
  2114. tmp[ilen-1] = '\0';
  2115. #else
  2116. strcpy(tmp, "/tmp");
  2117. #endif
  2118. return slapi_ch_strdup(tmp);
  2119. }
  2120. #if defined( XP_WIN32 )
  2121. {
  2122. char *ptr = NULL;
  2123. char *endptr = tmpdir + strlen(tmpdir);
  2124. for(ptr = tmpdir; ptr < endptr; ptr++)
  2125. {
  2126. if('/' == *ptr)
  2127. *ptr = '\\';
  2128. }
  2129. }
  2130. #endif
  2131. #if defined( XP_WIN32 )
  2132. if(CreateDirectory(tmpdir, NULL) == 0)
  2133. {
  2134. slapi_log_error(
  2135. SLAPI_LOG_FATAL,
  2136. "slapd_get_tmp_dir",
  2137. "CreateDirectory(%s, NULL) Error: %s\n",
  2138. tmpdir, strerror(errno));
  2139. }
  2140. #else
  2141. if(mkdir(tmpdir, 00770) == -1)
  2142. {
  2143. if (errno == EEXIST) {
  2144. slapi_log_error(
  2145. SLAPI_LOG_TRACE,
  2146. "slapd_get_tmp_dir",
  2147. "mkdir(%s, 00770) - already exists\n",
  2148. tmpdir);
  2149. } else {
  2150. slapi_log_error(
  2151. SLAPI_LOG_FATAL,
  2152. "slapd_get_tmp_dir",
  2153. "mkdir(%s, 00770) Error: %s\n",
  2154. tmpdir, strerror(errno));
  2155. }
  2156. }
  2157. #endif
  2158. return ( tmpdir );
  2159. }
  2160. SECKEYPrivateKey *
  2161. slapd_get_unlocked_key_for_cert(CERTCertificate *cert, void *pin_arg)
  2162. {
  2163. SECKEYPrivateKey *key = NULL;
  2164. PK11SlotListElement *sle;
  2165. PK11SlotList *slotlist = PK11_GetAllSlotsForCert(cert, NULL);
  2166. const char *certsubject = cert->subjectName ? cert->subjectName : "unknown cert";
  2167. if (!slotlist) {
  2168. PRErrorCode errcode = PR_GetError();
  2169. slapi_log_error(SLAPI_LOG_FATAL, "slapd_get_unlocked_key_for_cert",
  2170. "Error: cannot get slot list for certificate [%s] (%d: %s)\n",
  2171. certsubject, errcode, slapd_pr_strerror(errcode));
  2172. return key;
  2173. }
  2174. for (sle = slotlist->head; sle; sle = sle->next) {
  2175. PK11SlotInfo *slot = sle->slot;
  2176. const char *slotname = (slot && PK11_GetSlotName(slot)) ? PK11_GetSlotName(slot) : "unknown slot";
  2177. const char *tokenname = (slot && PK11_GetTokenName(slot)) ? PK11_GetTokenName(slot) : "unknown token";
  2178. if (!slot) {
  2179. slapi_log_error(SLAPI_LOG_TRACE, "slapd_get_unlocked_key_for_cert",
  2180. "Missing slot for slot list element for certificate [%s]\n",
  2181. certsubject);
  2182. } else if (!PK11_NeedLogin(slot) || PK11_IsLoggedIn(slot, pin_arg)) {
  2183. key = PK11_FindKeyByDERCert(slot, cert, pin_arg);
  2184. slapi_log_error(SLAPI_LOG_TRACE, "slapd_get_unlocked_key_for_cert",
  2185. "Found unlocked slot [%s] token [%s] for certificate [%s]\n",
  2186. slotname, tokenname, certsubject);
  2187. break;
  2188. } else {
  2189. slapi_log_error(SLAPI_LOG_TRACE, "slapd_get_unlocked_key_for_cert",
  2190. "Skipping locked slot [%s] token [%s] for certificate [%s]\n",
  2191. slotname, tokenname, certsubject);
  2192. }
  2193. }
  2194. if (!key) {
  2195. slapi_log_error(SLAPI_LOG_FATAL, "slapd_get_unlocked_key_for_cert",
  2196. "Error: could not find any unlocked slots for certificate [%s]. "
  2197. "Please review your TLS/SSL configuration. The following slots were found:\n",
  2198. certsubject);
  2199. for (sle = slotlist->head; sle; sle = sle->next) {
  2200. PK11SlotInfo *slot = sle->slot;
  2201. const char *slotname = (slot && PK11_GetSlotName(slot)) ? PK11_GetSlotName(slot) : "unknown slot";
  2202. const char *tokenname = (slot && PK11_GetTokenName(slot)) ? PK11_GetTokenName(slot) : "unknown token";
  2203. slapi_log_error(SLAPI_LOG_FATAL, "slapd_get_unlocked_key_for_cert",
  2204. "Slot [%s] token [%s] was locked.\n",
  2205. slotname, tokenname);
  2206. }
  2207. }
  2208. PK11_FreeSlotList(slotlist);
  2209. return key;
  2210. }