ssl.c 85 KB

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