security_wrappers.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #include <ssl.h>
  42. #include <nss.h>
  43. #include <key.h>
  44. #include <certdb.h>
  45. #include <cert.h>
  46. #include <sslproto.h>
  47. #include <secoid.h>
  48. #include <secmod.h>
  49. #include <secmodt.h>
  50. #include <prtypes.h>
  51. #include <seccomon.h>
  52. #include <pkcs11.h>
  53. #include <pk11func.h>
  54. #include "slap.h"
  55. /*
  56. * Set of security wrapper functions, aimed at forcing every security-related call
  57. * pass through libslapd library.
  58. * Thus, we avoid finding any ambiguity or data inconsistency (which we would,
  59. * otherwise, have to face on NT.
  60. */
  61. /*
  62. * Note: slapd_ssl_handshakeCallback() is called with a Connection * in
  63. * client_data. That connection must have its c_mutex locked.
  64. */
  65. int
  66. slapd_ssl_handshakeCallback(PRFileDesc *fd, void * callback, void * client_data)
  67. {
  68. return SSL_HandshakeCallback(fd, (SSLHandshakeCallback) callback, client_data);
  69. }
  70. /*
  71. * Note: slapd_ssl_badCertHook() is called with a Connection * in
  72. * client_data. That connection must have its c_mutex locked.
  73. */
  74. int
  75. slapd_ssl_badCertHook(PRFileDesc *fd, void * callback, void * client_data)
  76. {
  77. return SSL_BadCertHook(fd, (SSLBadCertHandler) callback, client_data);
  78. }
  79. CERTCertificate *
  80. slapd_ssl_peerCertificate(PRFileDesc *fd)
  81. {
  82. return SSL_PeerCertificate(fd);
  83. }
  84. SECStatus
  85. slapd_ssl_getChannelInfo(PRFileDesc *fd, SSLChannelInfo *sinfo, PRUintn len)
  86. {
  87. return SSL_GetChannelInfo(fd, sinfo, len);
  88. }
  89. SECStatus
  90. slapd_ssl_getCipherSuiteInfo(PRUint16 ciphersuite, SSLCipherSuiteInfo *cinfo, PRUintn len)
  91. {
  92. return SSL_GetCipherSuiteInfo(ciphersuite, cinfo, len);
  93. }
  94. PRFileDesc *
  95. slapd_ssl_importFD(PRFileDesc *model, PRFileDesc *fd)
  96. {
  97. return SSL_ImportFD(model, fd);
  98. }
  99. SECStatus
  100. slapd_ssl_resetHandshake(PRFileDesc *fd, PRBool asServer)
  101. {
  102. return SSL_ResetHandshake(fd, asServer);
  103. }
  104. void
  105. slapd_pk11_configurePKCS11(char *man, char *libdes, char *tokdes, char *ptokdes,
  106. char *slotdes, char *pslotdes, char *fslotdes,
  107. char *fpslotdes, int minPwd,
  108. int pwdRequired)
  109. {
  110. PK11_ConfigurePKCS11(man, libdes, tokdes, ptokdes,
  111. slotdes, pslotdes, fslotdes, fpslotdes, minPwd,
  112. pwdRequired);
  113. return;
  114. }
  115. void
  116. slapd_pk11_freeSlot(PK11SlotInfo *slot)
  117. {
  118. PK11_FreeSlot(slot);
  119. return;
  120. }
  121. void
  122. slapd_pk11_freeSymKey(PK11SymKey *key)
  123. {
  124. PK11_FreeSymKey(key);
  125. return;
  126. }
  127. PK11SlotInfo *
  128. slapd_pk11_findSlotByName(char *name)
  129. {
  130. return PK11_FindSlotByName(name);
  131. }
  132. SECAlgorithmID *
  133. slapd_pk11_createPBEAlgorithmID(SECOidTag algorithm, int iteration, SECItem *salt)
  134. {
  135. return PK11_CreatePBEAlgorithmID(algorithm, iteration, salt);
  136. }
  137. PK11SymKey *
  138. slapd_pk11_pbeKeyGen(PK11SlotInfo *slot, SECAlgorithmID *algid, SECItem *pwitem,
  139. PRBool faulty3DES, void *wincx)
  140. {
  141. return PK11_PBEKeyGen(slot, algid, pwitem,
  142. faulty3DES, wincx);
  143. }
  144. CK_MECHANISM_TYPE
  145. slapd_pk11_algtagToMechanism(SECOidTag algTag)
  146. {
  147. return PK11_AlgtagToMechanism(algTag);
  148. }
  149. SECItem *
  150. slapd_pk11_paramFromAlgid(SECAlgorithmID *algid)
  151. {
  152. return PK11_ParamFromAlgid(algid);
  153. }
  154. CK_RV
  155. slapd_pk11_mapPBEMechanismToCryptoMechanism(CK_MECHANISM_PTR pPBEMechanism,
  156. CK_MECHANISM_PTR pCryptoMechanism,
  157. SECItem *pbe_pwd, PRBool bad3DES)
  158. {
  159. return PK11_MapPBEMechanismToCryptoMechanism(pPBEMechanism,
  160. pCryptoMechanism,
  161. pbe_pwd, bad3DES);
  162. }
  163. int
  164. slapd_pk11_getBlockSize(CK_MECHANISM_TYPE type,SECItem *params)
  165. {
  166. return PK11_GetBlockSize(type,params);
  167. }
  168. PK11Context *
  169. slapd_pk11_createContextBySymKey(CK_MECHANISM_TYPE type,
  170. CK_ATTRIBUTE_TYPE operation,
  171. PK11SymKey *symKey, SECItem *param)
  172. {
  173. return PK11_CreateContextBySymKey(type,
  174. operation, symKey, param);
  175. }
  176. SECStatus
  177. slapd_pk11_cipherOp(PK11Context *context, unsigned char * out, int *outlen,
  178. int maxout, unsigned char *in, int inlen)
  179. {
  180. return PK11_CipherOp(context, out, outlen, maxout, in, inlen);
  181. }
  182. SECStatus
  183. slapd_pk11_finalize(PK11Context *context)
  184. {
  185. return PK11_Finalize(context);
  186. }
  187. PK11SlotInfo *
  188. slapd_pk11_getInternalKeySlot()
  189. {
  190. return PK11_GetInternalKeySlot();
  191. }
  192. PK11SlotInfo *
  193. slapd_pk11_getInternalSlot()
  194. {
  195. return PK11_GetInternalSlot();
  196. }
  197. SECStatus
  198. slapd_pk11_authenticate(PK11SlotInfo *slot, PRBool loadCerts, void *wincx)
  199. {
  200. return PK11_Authenticate(slot, loadCerts, wincx);
  201. }
  202. void
  203. slapd_pk11_setSlotPWValues(PK11SlotInfo *slot,int askpw, int timeout)
  204. {
  205. PK11_SetSlotPWValues(slot, askpw, timeout);
  206. return;
  207. }
  208. PRBool
  209. slapd_pk11_isFIPS()
  210. {
  211. return PK11_IsFIPS();
  212. }
  213. CERTCertificate *
  214. slapd_pk11_findCertFromNickname(char *nickname, void *wincx)
  215. {
  216. return PK11_FindCertFromNickname(nickname, wincx);
  217. }
  218. SECKEYPrivateKey *
  219. slapd_pk11_findKeyByAnyCert(CERTCertificate *cert, void *wincx)
  220. {
  221. return PK11_FindKeyByAnyCert(cert, wincx);
  222. }
  223. PRBool
  224. slapd_pk11_fortezzaHasKEA(CERTCertificate *cert)
  225. {
  226. return PK11_FortezzaHasKEA(cert);
  227. }
  228. void
  229. slapd_pk11_destroyContext(PK11Context *context, PRBool freeit)
  230. {
  231. PK11_DestroyContext(context, freeit);
  232. }
  233. void secoid_destroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit)
  234. {
  235. SECOID_DestroyAlgorithmID(algid, freeit);
  236. }
  237. void slapd_pk11_CERT_DestroyCertificate(CERTCertificate *cert)
  238. {
  239. CERT_DestroyCertificate(cert);
  240. }
  241. SECKEYPublicKey *slapd_CERT_ExtractPublicKey(CERTCertificate *cert)
  242. {
  243. return CERT_ExtractPublicKey(cert);
  244. }
  245. SECKEYPrivateKey * slapd_pk11_FindPrivateKeyFromCert(PK11SlotInfo *slot,CERTCertificate *cert, void *wincx)
  246. {
  247. return PK11_FindPrivateKeyFromCert(slot,cert,wincx);
  248. }
  249. PK11SlotInfo *slapd_pk11_GetInternalKeySlot(void)
  250. {
  251. return PK11_GetInternalKeySlot();
  252. }
  253. SECStatus slapd_pk11_PubWrapSymKey(CK_MECHANISM_TYPE type, SECKEYPublicKey *pubKey,PK11SymKey *symKey, SECItem *wrappedKey)
  254. {
  255. return PK11_PubWrapSymKey(type,pubKey,symKey,wrappedKey);
  256. }
  257. PK11SymKey *slapd_pk11_KeyGen(PK11SlotInfo *slot,CK_MECHANISM_TYPE type,SECItem *param, int keySize,void *wincx)
  258. {
  259. return PK11_KeyGen(slot,type,param,keySize,wincx);
  260. }
  261. void slapd_pk11_FreeSlot(PK11SlotInfo *slot)
  262. {
  263. PK11_FreeSlot(slot);
  264. }
  265. void slapd_pk11_FreeSymKey(PK11SymKey *key)
  266. {
  267. PK11_FreeSymKey(key);
  268. }
  269. void slapd_pk11_DestroyContext(PK11Context *context, PRBool freeit)
  270. {
  271. PK11_DestroyContext(context,freeit);
  272. }
  273. SECItem *slapd_pk11_ParamFromIV(CK_MECHANISM_TYPE type,SECItem *iv)
  274. {
  275. return PK11_ParamFromIV(type,iv);
  276. }
  277. PK11SymKey *slapd_pk11_PubUnwrapSymKey(SECKEYPrivateKey *wrappingKey, SECItem *wrappedKey,CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, int keySize)
  278. {
  279. return PK11_PubUnwrapSymKey(wrappingKey,wrappedKey,target,operation,keySize);
  280. }
  281. unsigned slapd_SECKEY_PublicKeyStrength(SECKEYPublicKey *pubk)
  282. {
  283. return SECKEY_PublicKeyStrength(pubk);
  284. }
  285. SECStatus slapd_pk11_Finalize(PK11Context *context)
  286. {
  287. return PK11_Finalize(context);
  288. }
  289. SECStatus slapd_pk11_DigestFinal(PK11Context *context, unsigned char *data,unsigned int *outLen, unsigned int length)
  290. {
  291. return PK11_DigestFinal(context, data, outLen, length);
  292. }
  293. void
  294. slapd_SECITEM_FreeItem (SECItem *zap, PRBool freeit)
  295. {
  296. SECITEM_FreeItem(zap,freeit);
  297. }
  298. void slapd_pk11_DestroyPrivateKey(SECKEYPrivateKey *key)
  299. {
  300. SECKEY_DestroyPrivateKey(key);
  301. }
  302. void slapd_pk11_DestroyPublicKey(SECKEYPublicKey *key)
  303. {
  304. SECKEY_DestroyPublicKey(key);
  305. }