ssl.c 89 KB

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