Engine for the ICSF backend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3549 lines
92 KiB

  1. /* hw_pkcs11.c (replace hw_trustway.c) */
  2. /*
  3. * PKCS#11 engine for the OpenSSL project 2002
  4. * Developped by Bull Trustway R&D Networking & Security
  5. * Introduced and tested with Bull TrustWay CC2000 crypto hardware
  6. * Afchine.Madjlessi@bull.net Bull S.A. France
  7. * http://www.servers.bull.com/trustway
  8. *
  9. * ChangLog:
  10. * * 6/30/2010 Updates to compile stand-alone and against openssl 1.0
  11. * * 8/15/2005 Fixes suggested by opencryptoki-engines list
  12. * * 1/1/2004 Modified to support digests, ciphers and openCryptoki
  13. * http://www.sf.net/projects/opencryptoki
  14. * - Serge Hallyn <serue@us.ibm.com>
  15. * - Kent Yoder <yoder1@us.ibm.com>
  16. * - Peter Waltenberg <pwalten@au1.ibm.com>
  17. * (C) International Business Machines Corporation 2004, 2005, 2010
  18. */
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <pthread.h>
  24. #include <dlfcn.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <openssl/e_os2.h>
  28. #include <openssl/engine.h>
  29. #include <openssl/err.h>
  30. #include <openssl/bn.h>
  31. #include <openssl/pem.h>
  32. #include <openssl/rsa.h>
  33. #include <openssl/rand.h>
  34. #include <openssl/objects.h>
  35. #include <openssl/x509.h>
  36. #include <openssl/md5.h>
  37. #include <openssl/ripemd.h>
  38. #ifndef OPENSSL_NO_HW
  39. #ifndef OPENSSL_NO_HW_PKCS11
  40. #include "cryptoki.h"
  41. #include "e_pkcs11_err.h"
  42. #include "e_pkcs11.h"
  43. /* SHA224, CAMELLIA */
  44. #include "pkcs-11v2-20a3.h"
  45. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  46. #define OLDER_OPENSSL
  47. #endif
  48. /* Constants used when creating the ENGINE */
  49. static const char *engine_pkcs11_id = "ibmpkcs11";
  50. static const char *engine_pkcs11_name = "PKCS#11 hardware engine support";
  51. static int bind_pkcs11(ENGINE *e);
  52. /* ENGINE level stuff */
  53. static int pkcs11_init(ENGINE *e);
  54. static int pkcs11_finish(ENGINE *e);
  55. static int pkcs11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
  56. static int pkcs11_destroy(ENGINE *e);
  57. #ifndef OPENSSL_NO_RSA
  58. /* RSA stuff */
  59. static int pkcs11_RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
  60. RSA *rsa, int padding);
  61. static int pkcs11_RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
  62. RSA *rsa, int padding);
  63. static int pkcs11_RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
  64. RSA *rsa, int padding);
  65. static int pkcs11_RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
  66. RSA *rsa, int padding);
  67. static int pkcs11_RSA_init(RSA *rsa);
  68. static int pkcs11_RSA_finish(RSA *rsa);
  69. #ifdef OLDER_OPENSSL
  70. static int pkcs11_RSA_sign(int type, const unsigned char *m, unsigned int m_len,
  71. unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
  72. /* this API changed in OpenSSL version 1.0.0 */
  73. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  74. static int pkcs11_RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
  75. unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
  76. #else
  77. static int pkcs11_RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
  78. const unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
  79. #endif
  80. #endif
  81. static int pkcs11_RSA_generate_key(RSA *rsa, int bits, BIGNUM *bn_e, BN_GENCB *cb);
  82. static EVP_PKEY *pkcs11_load_privkey(ENGINE*, const char* pubkey_file,
  83. UI_METHOD *ui_method, void *callback_data);
  84. static EVP_PKEY *pkcs11_load_pubkey(ENGINE*, const char* pubkey_file,
  85. UI_METHOD *ui_method, void *callback_data);
  86. CK_OBJECT_HANDLE pkcs11_FindOrCreateKey(CK_SESSION_HANDLE h, RSA* rsa, CK_OBJECT_CLASS oKey, CK_BBOOL fKeyCreate);
  87. /* exported functions (not member of ENGINE inteface) */
  88. RSA* pkcs11_RSA_generate_tmp_key(int bits,unsigned long e_value,void (*callback)(int,int,void *),void *cb_arg);
  89. #endif
  90. /* RAND stuff */
  91. static void pkcs11_rand_seed(const void *buf, int num);
  92. static void pkcs11_rand_add(const void *buf, int num, double add_entropy);
  93. static void pkcs11_rand_cleanup(void);
  94. static int pkcs11_rand_bytes(unsigned char *buf, int num);
  95. static int pkcs11_rand_status(void);
  96. /* cipher function prototypes */
  97. static inline int pkcs11_init_key(EVP_CIPHER_CTX * ctx, const unsigned char *key,
  98. const unsigned char *iv, int enc, int alg);
  99. static inline int pkcs11_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out,
  100. const unsigned char *in, size_t inlen);
  101. static int pkcs11_cipher_cleanup(EVP_CIPHER_CTX *ctx);
  102. static int pkcs11_des_init_key(EVP_CIPHER_CTX * ctx, const unsigned char *key,
  103. const unsigned char *iv, int enc);
  104. static int pkcs11_tdes_init_key(EVP_CIPHER_CTX * ctx, const unsigned char *key,
  105. const unsigned char *iv, int enc);
  106. static int pkcs11_aes_init_key(EVP_CIPHER_CTX * ctx, const unsigned char *key,
  107. const unsigned char *iv, int enc);
  108. /* End cipher function prototypes */
  109. /* Digest function prototypes */
  110. static inline int pkcs11_digest_init(EVP_MD_CTX *ctx, int alg);
  111. static int pkcs11_digest_update(EVP_MD_CTX *ctx, const void *in, size_t len);
  112. static int pkcs11_digest_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in);
  113. static int pkcs11_digest_finish(EVP_MD_CTX *ctx, unsigned char *md);
  114. static inline int pkcs11_digest_cleanup(EVP_MD_CTX *ctx);
  115. static inline int pkcs11_sha1_init(EVP_MD_CTX *ctx);
  116. static inline int pkcs11_sha224_init(EVP_MD_CTX *ctx);
  117. static inline int pkcs11_sha256_init(EVP_MD_CTX *ctx);
  118. static inline int pkcs11_sha384_init(EVP_MD_CTX *ctx);
  119. static inline int pkcs11_sha512_init(EVP_MD_CTX *ctx);
  120. static inline int pkcs11_md5_init(EVP_MD_CTX *ctx);
  121. static inline int pkcs11_ripemd160_init(EVP_MD_CTX *ctx);
  122. /* End digest function prototypes */
  123. static int pre_init_pkcs11(ENGINE *e);
  124. static int pkcs11_engine_ciphers(ENGINE * e, const EVP_CIPHER ** cipher,
  125. const int **nids, int nid);
  126. static int pkcs11_engine_digests(ENGINE * e, const EVP_MD ** digest,
  127. const int **nids, int nid);
  128. /* The definitions for control commands specific to this engine */
  129. #define PKCS11_CMD_SO_PATH ENGINE_CMD_BASE
  130. #define PKCS11_CMD_SLOT_ID (ENGINE_CMD_BASE + 1)
  131. static const ENGINE_CMD_DEFN pkcs11_cmd_defns[] =
  132. {
  133. { PKCS11_CMD_SO_PATH,
  134. "SO_PATH",
  135. "Specifies the path to the 'pkcs#11' shared library",
  136. ENGINE_CMD_FLAG_STRING
  137. },
  138. { PKCS11_CMD_SLOT_ID,
  139. "SLOT_ID",
  140. "Specifies the slot containing the token to use",
  141. ENGINE_CMD_FLAG_NUMERIC
  142. },
  143. {0, NULL, NULL, 0}
  144. };
  145. #ifdef OPENSSL_NO_DYNAMIC_ENGINE
  146. static ENGINE *engine_pkcs11(void)
  147. {
  148. ENGINE *ret = ENGINE_new();
  149. if(!ret) {
  150. return NULL;
  151. }
  152. if(!bind_helper(ret)) {
  153. ENGINE_free(ret);
  154. return NULL;
  155. }
  156. return ret;
  157. }
  158. void ENGINE_load_pkcs11(void)
  159. {
  160. ENGINE *toadd = engine_pkcs11();
  161. if(!toadd) return;
  162. ENGINE_add(toadd);
  163. ENGINE_free(toadd);
  164. ERR_clear_error();
  165. }
  166. #else
  167. static int bind_helper(ENGINE *e, const char *id)
  168. {
  169. if((NULL != id) && (strcmp(id, engine_pkcs11_id) != 0))
  170. return 0;
  171. return(bind_pkcs11(e));
  172. }
  173. IMPLEMENT_DYNAMIC_CHECK_FN()
  174. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  175. #endif
  176. /*
  177. * Comments on approaches:
  178. * At the moment, ciphers and digests are treated differently.
  179. *
  180. * For ciphers, we use cipher_init to initialize the cryptoki
  181. * cipher action, and update to encrypt. There is no finalize
  182. * or cleanup.
  183. *
  184. * For digests, we use digest_init to initialize a context
  185. * struct, digest_update to call C_DigestUpdate on the data that
  186. * we are receiving, digest_finish to call C_DigestFinal(), and
  187. * cleanup() to free the context struct.
  188. */
  189. /*
  190. * Each cipher action requires a new session. We store the
  191. * session and its token in the context->cipher_data void* using
  192. * this struct
  193. */
  194. struct token_session {
  195. struct _token *token;
  196. CK_SESSION_HANDLE session;
  197. };
  198. /*
  199. * For digests:
  200. * We call digest_init to start the context, digest_update
  201. * to start computing the digest on the data that is being
  202. * received and digest_finish to finish the digest operation.
  203. */
  204. struct pkcs11_digest_ctx {
  205. int alg;
  206. int len;
  207. struct _token *token;
  208. CK_SESSION_HANDLE session;
  209. };
  210. /********/
  211. #ifdef OLDER_OPENSSL
  212. #define CIPHER_DATA(ctx) ((struct token_session *)(ctx->cipher_data))
  213. #define MD_DATA(ctx) ((struct pkcs11_digest_ctx *)(ctx->md_data))
  214. #else
  215. #define CIPHER_DATA(ctx) ((struct token_session *)(EVP_CIPHER_CTX_get_cipher_data(ctx)))
  216. #define MD_DATA(ctx) ((struct pkcs11_digest_ctx *)(EVP_MD_CTX_md_data(ctx)))
  217. #endif
  218. static int num_cipher_nids = 0;
  219. static int num_digest_nids = 0;
  220. #ifdef OLDER_OPENSSL
  221. const EVP_CIPHER pkcs11_des_ecb = {
  222. NID_des_ecb, /* NID */
  223. 8, /* Block size */
  224. 8, /* Key len */
  225. 8, /* IV len */
  226. EVP_CIPH_ECB_MODE, /* flags */
  227. pkcs11_des_init_key, /* init */
  228. pkcs11_cipher, /* do_cipher */
  229. pkcs11_cipher_cleanup, /* cleanup */
  230. sizeof(struct token_session),
  231. EVP_CIPHER_set_asn1_iv,
  232. EVP_CIPHER_get_asn1_iv,
  233. NULL, /* misc ctrl ops */
  234. NULL /* app data (ctx->cipher_data) */
  235. };
  236. const EVP_CIPHER pkcs11_des_cbc = {
  237. NID_des_cbc, /* NID */
  238. 8, /* Block size */
  239. 8, /* Key len */
  240. 8, /* IV len */
  241. EVP_CIPH_CBC_MODE, /* flags */
  242. pkcs11_des_init_key, /* init */
  243. pkcs11_cipher, /* do_cipher */
  244. pkcs11_cipher_cleanup, /* cleanup */
  245. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  246. EVP_CIPHER_set_asn1_iv,
  247. EVP_CIPHER_get_asn1_iv,
  248. NULL, /* misc ctrl ops */
  249. NULL /* app data (ctx->cipher_data) */
  250. };
  251. #else
  252. #define DECLARE_DES_EVP(lmode, umode) \
  253. static EVP_CIPHER *des_##lmode = NULL; \
  254. static const EVP_CIPHER *pkcs11_des_##lmode(void) \
  255. { \
  256. if (des_##lmode == NULL) { \
  257. EVP_CIPHER *cipher; \
  258. if (( cipher = EVP_CIPHER_meth_new(NID_des_##lmode, \
  259. 8, 8)) == NULL \
  260. || !EVP_CIPHER_meth_set_iv_length(cipher, 8) \
  261. || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_##umode##_MODE)\
  262. || !EVP_CIPHER_meth_set_init(cipher, pkcs11_des_init_key) \
  263. || !EVP_CIPHER_meth_set_do_cipher(cipher, pkcs11_cipher) \
  264. || !EVP_CIPHER_meth_set_cleanup(cipher, pkcs11_cipher_cleanup)\
  265. || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof( \
  266. struct token_session))\
  267. || !EVP_CIPHER_meth_set_set_asn1_params(cipher, \
  268. EVP_CIPHER_set_asn1_iv) \
  269. || !EVP_CIPHER_meth_set_get_asn1_params(cipher, \
  270. EVP_CIPHER_get_asn1_iv)) { \
  271. EVP_CIPHER_meth_free(cipher); \
  272. cipher = NULL; \
  273. } \
  274. des_##lmode = cipher; \
  275. } \
  276. return des_##lmode; \
  277. } \
  278. \
  279. static void pkcs11_des_##lmode##_destroy(void) \
  280. { \
  281. EVP_CIPHER_meth_free(des_##lmode); \
  282. des_##lmode = NULL; \
  283. }
  284. DECLARE_DES_EVP(ecb, ECB)
  285. DECLARE_DES_EVP(cbc, CBC)
  286. #endif
  287. #ifdef OLDER_OPENSSL
  288. const EVP_CIPHER pkcs11_tdes_ecb = {
  289. NID_des_ede3_ecb, /* NID */
  290. 8, /* Block size */
  291. 24, /* Key len */
  292. 8, /* IV len */
  293. EVP_CIPH_ECB_MODE, /* flags */
  294. pkcs11_tdes_init_key, /* init */
  295. pkcs11_cipher, /* do_cipher */
  296. pkcs11_cipher_cleanup, /* cleanup */
  297. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  298. EVP_CIPHER_set_asn1_iv,
  299. EVP_CIPHER_get_asn1_iv,
  300. NULL, /* misc ctrl ops */
  301. NULL /* app data (ctx->cipher_data) */
  302. };
  303. const EVP_CIPHER pkcs11_tdes_cbc = {
  304. NID_des_ede3_cbc, /* NID */
  305. 8, /* Block size */
  306. 24, /* Key len */
  307. 8, /* IV len */
  308. EVP_CIPH_CBC_MODE, /* flags */
  309. pkcs11_tdes_init_key, /* init */
  310. pkcs11_cipher, /* do_cipher */
  311. pkcs11_cipher_cleanup, /* cleanup */
  312. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  313. EVP_CIPHER_set_asn1_iv,
  314. EVP_CIPHER_get_asn1_iv,
  315. NULL, /* misc ctrl ops */
  316. NULL /* app data (ctx->cipher_data) */
  317. };
  318. #else
  319. #define DECLARE_TDES_EVP(lmode, umode) \
  320. static EVP_CIPHER *tdes_##lmode = NULL; \
  321. static const EVP_CIPHER *pkcs11_tdes_##lmode(void) \
  322. { \
  323. if (tdes_##lmode == NULL) { \
  324. EVP_CIPHER *cipher; \
  325. if (( cipher = EVP_CIPHER_meth_new(NID_des_ede3_##lmode, \
  326. 8, 24)) == NULL \
  327. || !EVP_CIPHER_meth_set_iv_length(cipher, 8) \
  328. || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_##umode##_MODE)\
  329. || !EVP_CIPHER_meth_set_init(cipher, pkcs11_tdes_init_key) \
  330. || !EVP_CIPHER_meth_set_do_cipher(cipher, pkcs11_cipher) \
  331. || !EVP_CIPHER_meth_set_cleanup(cipher, pkcs11_cipher_cleanup)\
  332. || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof( \
  333. struct token_session))\
  334. || !EVP_CIPHER_meth_set_set_asn1_params(cipher, \
  335. EVP_CIPHER_set_asn1_iv) \
  336. || !EVP_CIPHER_meth_set_get_asn1_params(cipher, \
  337. EVP_CIPHER_get_asn1_iv)) { \
  338. EVP_CIPHER_meth_free(cipher); \
  339. cipher = NULL; \
  340. } \
  341. tdes_##lmode = cipher; \
  342. } \
  343. return tdes_##lmode; \
  344. } \
  345. \
  346. static void pkcs11_tdes_##lmode##_destroy(void) \
  347. { \
  348. EVP_CIPHER_meth_free(tdes_##lmode); \
  349. tdes_##lmode = NULL; \
  350. }
  351. DECLARE_TDES_EVP(ecb, ECB)
  352. DECLARE_TDES_EVP(cbc, CBC)
  353. #endif
  354. #ifdef OLDER_OPENSSL
  355. /* AES ECB */
  356. const EVP_CIPHER pkcs11_aes_128_ecb = {
  357. NID_aes_128_cbc, /* NID */
  358. 16, /* Block size */
  359. 16, /* Key len */
  360. 16, /* IV len */
  361. EVP_CIPH_ECB_MODE, /* flags */
  362. pkcs11_aes_init_key, /* init */
  363. pkcs11_cipher, /* do_cipher */
  364. pkcs11_cipher_cleanup, /* cleanup */
  365. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  366. EVP_CIPHER_set_asn1_iv,
  367. EVP_CIPHER_get_asn1_iv,
  368. NULL, /* misc ctrl ops */
  369. NULL /* app data (ctx->cipher_data) */
  370. };
  371. const EVP_CIPHER pkcs11_aes_192_ecb = {
  372. NID_aes_192_ecb, /* NID */
  373. 16, /* Block size */
  374. 24, /* Key len */
  375. 16, /* IV len */
  376. EVP_CIPH_ECB_MODE, /* flags */
  377. pkcs11_aes_init_key, /* init */
  378. pkcs11_cipher, /* do_cipher */
  379. pkcs11_cipher_cleanup, /* cleanup */
  380. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  381. EVP_CIPHER_set_asn1_iv,
  382. EVP_CIPHER_get_asn1_iv,
  383. NULL, /* misc ctrl ops */
  384. NULL /* app data (ctx->cipher_data) */
  385. };
  386. const EVP_CIPHER pkcs11_aes_256_ecb = {
  387. NID_aes_256_ecb, /* NID */
  388. 16, /* Block size */
  389. 32, /* Key len */
  390. 16, /* IV len */
  391. EVP_CIPH_ECB_MODE, /* flags */
  392. pkcs11_aes_init_key, /* init */
  393. pkcs11_cipher, /* do_cipher */
  394. pkcs11_cipher_cleanup, /* cleanup */
  395. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  396. EVP_CIPHER_set_asn1_iv,
  397. EVP_CIPHER_get_asn1_iv,
  398. NULL, /* misc ctrl ops */
  399. NULL /* app data (ctx->cipher_data) */
  400. };
  401. /* AES CBC */
  402. const EVP_CIPHER pkcs11_aes_128_cbc = {
  403. NID_aes_128_cbc, /* NID */
  404. 16, /* Block size */
  405. 16, /* Key len */
  406. 16, /* IV len */
  407. EVP_CIPH_CBC_MODE, /* flags */
  408. pkcs11_aes_init_key, /* init */
  409. pkcs11_cipher, /* do_cipher */
  410. pkcs11_cipher_cleanup, /* cleanup */
  411. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  412. EVP_CIPHER_set_asn1_iv,
  413. EVP_CIPHER_get_asn1_iv,
  414. NULL, /* misc ctrl ops */
  415. NULL /* app data (ctx->cipher_data) */
  416. };
  417. const EVP_CIPHER pkcs11_aes_192_cbc = {
  418. NID_aes_192_cbc, /* NID */
  419. 16, /* Block size */
  420. 24, /* Key len */
  421. 16, /* IV len */
  422. EVP_CIPH_CBC_MODE, /* flags */
  423. pkcs11_aes_init_key, /* init */
  424. pkcs11_cipher, /* do_cipher */
  425. pkcs11_cipher_cleanup, /* cleanup */
  426. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  427. EVP_CIPHER_set_asn1_iv,
  428. EVP_CIPHER_get_asn1_iv,
  429. NULL, /* misc ctrl ops */
  430. NULL /* app data (ctx->cipher_data) */
  431. };
  432. const EVP_CIPHER pkcs11_aes_256_cbc = {
  433. NID_aes_256_cbc, /* NID */
  434. 16, /* Block size */
  435. 32, /* Key len */
  436. 16, /* IV len */
  437. EVP_CIPH_CBC_MODE, /* flags */
  438. pkcs11_aes_init_key, /* init */
  439. pkcs11_cipher, /* do_cipher */
  440. pkcs11_cipher_cleanup, /* cleanup */
  441. sizeof(struct token_session), /* sizeof(ctx->cipher_data) */
  442. EVP_CIPHER_set_asn1_iv,
  443. EVP_CIPHER_get_asn1_iv,
  444. NULL, /* misc ctrl ops */
  445. NULL /* app data (ctx->cipher_data) */
  446. };
  447. #else
  448. #define EVP_CIPHER_keylen_AES_128 16
  449. #define EVP_CIPHER_keylen_AES_192 24
  450. #define EVP_CIPHER_keylen_AES_256 32
  451. #define DECLARE_AES_EVP(ksize, lmode, umode) \
  452. static EVP_CIPHER *aes_##ksize##_##lmode = NULL; \
  453. static const EVP_CIPHER *pkcs11_aes_##ksize##_##lmode(void) \
  454. { \
  455. if (aes_##ksize##_##lmode == NULL) { \
  456. EVP_CIPHER *cipher; \
  457. if (( cipher = EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \
  458. 8, \
  459. EVP_CIPHER_keylen_AES_##ksize)) == NULL \
  460. || !EVP_CIPHER_meth_set_iv_length(cipher, 16) \
  461. || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_##umode##_MODE)\
  462. || !EVP_CIPHER_meth_set_init(cipher, pkcs11_aes_init_key) \
  463. || !EVP_CIPHER_meth_set_do_cipher(cipher, pkcs11_cipher) \
  464. || !EVP_CIPHER_meth_set_cleanup(cipher, pkcs11_cipher_cleanup)\
  465. || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof( \
  466. struct token_session))\
  467. || !EVP_CIPHER_meth_set_set_asn1_params(cipher, \
  468. EVP_CIPHER_set_asn1_iv) \
  469. || !EVP_CIPHER_meth_set_get_asn1_params(cipher, \
  470. EVP_CIPHER_get_asn1_iv)) { \
  471. EVP_CIPHER_meth_free(cipher); \
  472. cipher = NULL; \
  473. } \
  474. aes_##ksize##_##lmode = cipher; \
  475. } \
  476. return aes_##ksize##_##lmode; \
  477. } \
  478. \
  479. static void pkcs11_aes_##ksize##_##lmode##_destroy(void) \
  480. { \
  481. EVP_CIPHER_meth_free(aes_##ksize##_##lmode); \
  482. aes_##ksize##_##lmode = NULL; \
  483. }
  484. DECLARE_AES_EVP(128, cbc, CBC)
  485. DECLARE_AES_EVP(192, cbc, CBC)
  486. DECLARE_AES_EVP(256, cbc, CBC)
  487. DECLARE_AES_EVP(128, ecb, ECB)
  488. DECLARE_AES_EVP(192, ecb, ECB)
  489. DECLARE_AES_EVP(256, ecb, ECB)
  490. #endif
  491. #ifdef OLDER_OPENSSL
  492. /* Message Digests */
  493. const EVP_MD pkcs11_sha1 = {
  494. NID_sha1,
  495. NID_sha1WithRSAEncryption,
  496. SHA_DIGEST_LENGTH,
  497. EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
  498. pkcs11_sha1_init,
  499. pkcs11_digest_update,
  500. pkcs11_digest_finish, /* final */
  501. pkcs11_digest_copy,
  502. pkcs11_digest_cleanup, /* cleanup */
  503. EVP_PKEY_RSA_method,
  504. SHA_CBLOCK,
  505. sizeof(struct pkcs11_digest_ctx)
  506. };
  507. const EVP_MD pkcs11_sha224 = {
  508. NID_sha224,
  509. NID_sha224WithRSAEncryption,
  510. SHA224_DIGEST_LENGTH,
  511. EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
  512. pkcs11_sha224_init,
  513. pkcs11_digest_update,
  514. pkcs11_digest_finish, /* final */
  515. pkcs11_digest_copy,
  516. pkcs11_digest_cleanup, /* cleanup */
  517. EVP_PKEY_RSA_method,
  518. SHA256_CBLOCK,
  519. sizeof(struct pkcs11_digest_ctx)
  520. };
  521. const EVP_MD pkcs11_sha256 = {
  522. NID_sha256,
  523. NID_sha256WithRSAEncryption,
  524. SHA256_DIGEST_LENGTH,
  525. EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
  526. pkcs11_sha256_init,
  527. pkcs11_digest_update,
  528. pkcs11_digest_finish, /* final */
  529. pkcs11_digest_copy,
  530. pkcs11_digest_cleanup, /* cleanup */
  531. EVP_PKEY_RSA_method,
  532. SHA256_CBLOCK,
  533. sizeof(struct pkcs11_digest_ctx)
  534. };
  535. const EVP_MD pkcs11_sha384 = {
  536. NID_sha384,
  537. NID_sha384WithRSAEncryption,
  538. SHA384_DIGEST_LENGTH,
  539. EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
  540. pkcs11_sha384_init,
  541. pkcs11_digest_update,
  542. pkcs11_digest_finish, /* final */
  543. pkcs11_digest_copy,
  544. pkcs11_digest_cleanup, /* cleanup */
  545. EVP_PKEY_RSA_method,
  546. SHA512_CBLOCK,
  547. sizeof(struct pkcs11_digest_ctx)
  548. };
  549. const EVP_MD pkcs11_sha512 = {
  550. NID_sha512,
  551. NID_sha512WithRSAEncryption,
  552. SHA512_DIGEST_LENGTH,
  553. EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
  554. pkcs11_sha512_init,
  555. pkcs11_digest_update,
  556. pkcs11_digest_finish, /* final */
  557. pkcs11_digest_copy,
  558. pkcs11_digest_cleanup, /* cleanup */
  559. EVP_PKEY_RSA_method,
  560. SHA512_CBLOCK,
  561. sizeof(struct pkcs11_digest_ctx)
  562. };
  563. const EVP_MD pkcs11_md5 = {
  564. NID_md5,
  565. NID_md5WithRSAEncryption,
  566. MD5_DIGEST_LENGTH,
  567. 0,
  568. pkcs11_md5_init,
  569. pkcs11_digest_update,
  570. pkcs11_digest_finish, /* final */
  571. pkcs11_digest_copy,
  572. pkcs11_digest_cleanup, /* cleanup */
  573. EVP_PKEY_RSA_method,
  574. MD5_CBLOCK,
  575. sizeof(struct pkcs11_digest_ctx)
  576. };
  577. const EVP_MD pkcs11_ripemd160 = {
  578. NID_ripemd160,
  579. NID_ripemd160WithRSA,
  580. RIPEMD160_DIGEST_LENGTH,
  581. 0, /* flags */
  582. pkcs11_ripemd160_init,
  583. pkcs11_digest_update,
  584. pkcs11_digest_finish, /* final */
  585. pkcs11_digest_copy,
  586. pkcs11_digest_cleanup, /* cleanup */
  587. EVP_PKEY_RSA_method,
  588. RIPEMD160_CBLOCK,
  589. sizeof(struct pkcs11_digest_ctx)
  590. };
  591. #else
  592. #define DECLARE_DIGEST_EVP(dig, len, enc) \
  593. static EVP_MD *dig##_md = NULL; \
  594. static const EVP_MD *pkcs11_##dig(void) \
  595. { \
  596. if (dig##_md == NULL) { \
  597. EVP_MD *md; \
  598. if (( md = EVP_MD_meth_new(NID_##dig, \
  599. NID_##dig##WithRSA##enc)) == NULL \
  600. || !EVP_MD_meth_set_result_size(md, len##_DIGEST_LENGTH) \
  601. || !EVP_MD_meth_set_input_blocksize(md, len##_CBLOCK) \
  602. || !EVP_MD_meth_set_app_datasize(md, \
  603. sizeof(struct pkcs11_digest_ctx)) \
  604. || !EVP_MD_meth_set_flags(md, 0) \
  605. || !EVP_MD_meth_set_init(md, pkcs11_##dig##_init) \
  606. || !EVP_MD_meth_set_update(md, pkcs11_digest_update) \
  607. || !EVP_MD_meth_set_final(md, pkcs11_digest_finish) \
  608. || !EVP_MD_meth_set_copy(md, pkcs11_digest_copy) \
  609. || !EVP_MD_meth_set_cleanup(md, pkcs11_digest_cleanup)) { \
  610. EVP_MD_meth_free(md); \
  611. md = NULL; \
  612. } \
  613. dig##_md = md; \
  614. } \
  615. return dig##_md; \
  616. } \
  617. \
  618. static void pkcs11_##dig##_destroy(void) \
  619. { \
  620. EVP_MD_meth_free(dig##_md); \
  621. dig##_md = NULL; \
  622. }
  623. DECLARE_DIGEST_EVP(sha1, SHA, Encryption)
  624. DECLARE_DIGEST_EVP(sha224, SHA256, Encryption)
  625. DECLARE_DIGEST_EVP(sha256, SHA256, Encryption)
  626. DECLARE_DIGEST_EVP(sha384, SHA512, Encryption)
  627. DECLARE_DIGEST_EVP(sha512, SHA512, Encryption)
  628. DECLARE_DIGEST_EVP(md5, MD5, Encryption)
  629. DECLARE_DIGEST_EVP(ripemd160, RIPEMD160,)
  630. #endif
  631. /********/
  632. #ifndef OPENSSL_NO_RSA
  633. #ifdef OLDER_OPENSSL
  634. static RSA_METHOD pkcs11_rsa =
  635. {
  636. "PKCS#11 RSA",
  637. pkcs11_RSA_public_encrypt, /* rsa_pub_encrypt */
  638. pkcs11_RSA_public_decrypt, /* rsa_pub_decrypt */
  639. pkcs11_RSA_private_encrypt, /* rsa_priv_encrypt */
  640. pkcs11_RSA_private_decrypt, /* rsa_priv_decrypt */
  641. NULL, /* rsa_mod_exp */
  642. NULL, /* bn_mod_exp */
  643. pkcs11_RSA_init, /* init */
  644. pkcs11_RSA_finish, /* finish */
  645. RSA_FLAG_SIGN_VER, /* flags */
  646. NULL, /* app_data */
  647. pkcs11_RSA_sign, /* rsa_sign */
  648. pkcs11_RSA_verify, /* rsa_verify */
  649. pkcs11_RSA_generate_key /* rsa_generate_key */
  650. };
  651. #else
  652. static RSA_METHOD *pkcs11_rsa = NULL;
  653. #endif
  654. /*RSA_METHOD *PKCS11_RSA(void)
  655. {
  656. return(&pkcs11_rsa);
  657. }*/
  658. #endif
  659. extern const char *RAND_version;
  660. static RAND_METHOD pkcs11_random =
  661. {
  662. /* "PKCS11 RAND method", */
  663. pkcs11_rand_seed,
  664. pkcs11_rand_bytes,
  665. pkcs11_rand_cleanup,
  666. pkcs11_rand_add,
  667. pkcs11_rand_bytes,
  668. pkcs11_rand_status
  669. };
  670. RAND_METHOD *PKCS11_RAND(void)
  671. {
  672. return(&pkcs11_random);
  673. }
  674. static CK_FUNCTION_LIST_PTR pFunctionList = NULL;
  675. /* These are the static string constants for the DSO file name and the function
  676. * symbol names to bind to.
  677. */
  678. static unsigned char PKCS11_KEY_ID[] = "OpenSSL PKCS#11";
  679. /* String used to detect a CC2000 Bull TrustWay crypto card */
  680. #define BULL_TRUSTWAY_LIBRARY_DESCRIPTION "Bull CC2000 PKCS#11 Library "
  681. static CK_BBOOL Bull_TrustWay = FALSE;
  682. #undef BULL_CC2000 /* use Bull CC2000 hardware crypto */
  683. #undef BULL_CRYPTOBOX /* use Bull CryptoBox remote hardware crypto */
  684. #undef GPKCS11 /* use GPKCS11 software crypto */
  685. #if defined(BULL_CC2000)
  686. static const char def_PKCS11_LIBNAME[] = "gpkcs11cc2000";
  687. #elif defined(BULL_CRYPTOBOX)
  688. static const char def_PKCS11_LIBNAME[] = "cryptobox_clnt";
  689. extern CK_RV C_InitializeRpc (CK_CHAR_PTR, CK_CHAR_PTR, CK_ULONG_PTR);
  690. #elif defined(GPKCS11)
  691. static const char def_PKCS11_LIBNAME[] = "gpkcs11";
  692. #elif defined(OPENCRYPTOKI)
  693. static char *def_PKCS11_LIBNAME = "libopencryptoki.so";
  694. #else
  695. static const char def_PKCS11_LIBNAME[] = "pkcs11";
  696. #endif
  697. static const char PKCS11_GET_FUNCTION_LIST[] = "C_GetFunctionList";
  698. /* Size of an SSL signature: MD5+SHA1. up to allow SHA512 */
  699. //#define SSL_SIG_LENGTH 64
  700. #define SSL_SIG_LENGTH 36
  701. #define KEY_STORE 1
  702. static CK_BBOOL true = TRUE;
  703. static CK_BBOOL false = FALSE;
  704. static CK_SLOT_ID SLOTID = 0XFFFFFFFF;
  705. #ifndef OPENSSL_NO_RSA
  706. /* Where in the CRYPTO_EX_DATA stack we stick our per-key contexts */
  707. static int rsaPubKey = -1;
  708. static int rsaPrivKey = -1;
  709. static int deletePubKeyOnFree = -1;
  710. static int deletePrivKeyOnFree = -1;
  711. static int pkcs11Session = -1;
  712. #endif
  713. static int PKCS11_Initialized = 0;
  714. #ifdef PKCS11_DEBUG
  715. #define DBG_fprintf(args...) do { fprintf(stderr, args); fflush(stderr); } while (0)
  716. #else
  717. #define DBG_fprintf(args...)
  718. #endif
  719. void pkcs11_atfork_init(void)
  720. {
  721. DBG_fprintf("pkcs11_atfork_init: called (pid %d)\n", getpid());
  722. PKCS11_Initialized = 0;
  723. }
  724. #define pkcs11_die(func, reason, rv) \
  725. { \
  726. char tmpbuf[20]; \
  727. PKCS11err(func, reason); \
  728. sprintf(tmpbuf, "%lx", rv); \
  729. ERR_add_error_data(2, "PKCS11 CK_RV=0X", tmpbuf); \
  730. }
  731. struct token_session *pkcs11_getSession(void)
  732. {
  733. CK_RV rv;
  734. struct token_session *wrapper;
  735. if (!pkcs11_token) {
  736. PKCS11err(PKCS11_F_GETSESSION, PKCS11_R_NO_SLOT_SELECTED);
  737. return NULL;
  738. }
  739. wrapper = OPENSSL_malloc(sizeof (struct token_session));
  740. if (!wrapper) {
  741. PKCS11err(PKCS11_F_GETSESSION, PKCS11_R_MALLOC_FAILURE);
  742. return NULL;
  743. }
  744. wrapper->token = pkcs11_token;
  745. if (!PKCS11_Initialized) {
  746. rv = pFunctionList->C_Initialize(NULL);
  747. if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
  748. pkcs11_die(PKCS11_F_GETSESSION, PKCS11_R_INITIALIZE, rv);
  749. return NULL;
  750. }
  751. PKCS11_Initialized = 1;
  752. }
  753. rv = pFunctionList->C_OpenSession(wrapper->token->slot_id,
  754. CKF_SERIAL_SESSION | CKF_RW_SESSION,
  755. NULL_PTR,
  756. NULL_PTR,
  757. &wrapper->session);
  758. if (rv != CKR_OK) {
  759. pkcs11_die(PKCS11_F_GETSESSION, PKCS11_R_OPENSESSION, rv);
  760. return NULL;
  761. }
  762. return wrapper;
  763. }
  764. char *alg_to_string(int alg_type)
  765. {
  766. switch (alg_type) {
  767. case alg_des:
  768. return "des";
  769. case alg_tdes:
  770. return "tdes";
  771. case alg_aes:
  772. return "aes";
  773. case alg_rsa:
  774. return "rsa";
  775. case alg_sha:
  776. return "sha";
  777. case alg_md5:
  778. return "md5";
  779. case alg_ripemd:
  780. return "ripemd";
  781. default:
  782. return "invalid algorithm";
  783. }
  784. }
  785. /* This internal function is used by ENGINE_pkcs11() and possibly by the
  786. * "dynamic" ENGINE support too */
  787. static int bind_pkcs11(ENGINE *e)
  788. {
  789. DBG_fprintf("%s\n", __FUNCTION__);
  790. if (!ENGINE_set_id(e, engine_pkcs11_id) ||
  791. !ENGINE_set_name(e, engine_pkcs11_name) ||
  792. !ENGINE_set_RAND(e, &pkcs11_random) ||
  793. !ENGINE_set_init_function(e, pkcs11_init) ||
  794. !ENGINE_set_ciphers(e, pkcs11_engine_ciphers) ||
  795. !ENGINE_set_digests(e, pkcs11_engine_digests) ||
  796. !ENGINE_set_destroy_function(e, pkcs11_destroy) ||
  797. !ENGINE_set_finish_function(e, pkcs11_finish) ||
  798. !ENGINE_set_ctrl_function(e, pkcs11_ctrl) ||
  799. !ENGINE_set_cmd_defns(e, pkcs11_cmd_defns))
  800. return 0;
  801. /* Ensure the pkcs11 error handling is set up */
  802. ERR_load_pkcs11_strings();
  803. pre_init_pkcs11(e);
  804. return 1;
  805. }
  806. #ifdef ENGINE_DYNAMIC_SUPPORT
  807. static int bind_helper(ENGINE *e, const char *id)
  808. {
  809. DBG_fprintf("%s\n", __FUNCTION__);
  810. if(id && (strcmp(id, engine_pkcs11_id) != 0))
  811. return 0;
  812. if(!bind_pkcs11(e))
  813. return 0;
  814. return 1;
  815. }
  816. IMPLEMENT_DYNAMIC_CHECK_FN()
  817. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  818. #else
  819. static ENGINE *engine_pkcs11(void)
  820. {
  821. DBG_fprintf("%s\n", __FUNCTION__);
  822. ENGINE *ret = ENGINE_new();
  823. if(!ret)
  824. return NULL;
  825. if(!bind_pkcs11(ret))
  826. {
  827. ENGINE_free(ret);
  828. return NULL;
  829. }
  830. pre_init_pkcs11(ret);
  831. return ret;
  832. }
  833. void ENGINE_load_pkcs11(void)
  834. {
  835. DBG_fprintf("%s\n", __FUNCTION__);
  836. ENGINE *e_pkcs11 = engine_pkcs11();
  837. if(!e_pkcs11) return;
  838. ENGINE_add(e_pkcs11);
  839. ENGINE_free(e_pkcs11);
  840. ERR_clear_error();
  841. }
  842. #endif
  843. #define PKCS11_MAX_ALGS 20
  844. static int
  845. get_pkcs11_ciphers(const int **retnids)
  846. {
  847. static int nids[PKCS11_MAX_ALGS];
  848. int i, count = 0, *pkcs11_implemented_ciphers;
  849. if (pkcs11_token)
  850. pkcs11_implemented_ciphers = pkcs11_token->pkcs11_implemented_ciphers;
  851. else {
  852. PKCS11err(PKCS11_F_GET_PKCS11_CIPHERS, PKCS11_R_NO_SLOT_SELECTED);
  853. return 0;
  854. }
  855. memset(nids, 0, sizeof(nids));
  856. *retnids = NULL;
  857. for (i=0; i<NUM_NID; i++) {
  858. if (pkcs11_implemented_ciphers[i])
  859. nids[count++] = i;
  860. }
  861. if (count)
  862. *retnids = nids;
  863. return count;
  864. }
  865. static int
  866. get_pkcs11_digests(const int **retnids)
  867. {
  868. static int nids[PKCS11_MAX_ALGS];
  869. int i, count = 0, *pkcs11_implemented_digests;
  870. if (pkcs11_token)
  871. pkcs11_implemented_digests = pkcs11_token->pkcs11_implemented_digests;
  872. else {
  873. PKCS11err(PKCS11_F_GET_PKCS11_DIGESTS, PKCS11_R_NO_SLOT_SELECTED);
  874. return 0;
  875. }
  876. memset(nids, 0, sizeof(nids));
  877. *retnids = NULL;
  878. for (i=0; i<NUM_NID; i++) {
  879. if (pkcs11_implemented_digests[i])
  880. nids[count++] = i;
  881. }
  882. if (count)
  883. *retnids = nids;
  884. return count;
  885. }
  886. /*
  887. * ENGINE calls this to find out how to deal with
  888. * a particular NID in the ENGINE.
  889. */
  890. static int pkcs11_engine_ciphers(ENGINE * e, const EVP_CIPHER ** cipher,
  891. const int **nids, int nid)
  892. {
  893. if (!cipher)
  894. return get_pkcs11_ciphers(nids);
  895. if (!pkcs11_token) {
  896. PKCS11err(PKCS11_F_ENGINE_CIPHERS, PKCS11_R_NO_SLOT_SELECTED);
  897. return 0;
  898. }
  899. /* If the algorithm requested was not added to the list at
  900. * engine init time, don't return a reference to that structure.
  901. */
  902. if (pkcs11_token->pkcs11_implemented_ciphers[nid]) {
  903. switch (nid) {
  904. case NID_aes_128_ecb:
  905. #ifdef OLDER_OPENSSL
  906. *cipher = &pkcs11_aes_128_ecb;
  907. #else
  908. *cipher = pkcs11_aes_128_ecb();
  909. #endif
  910. break;
  911. case NID_aes_192_ecb:
  912. #ifdef OLDER_OPENSSL
  913. *cipher = &pkcs11_aes_192_ecb;
  914. #else
  915. *cipher = pkcs11_aes_192_ecb();
  916. #endif
  917. break;
  918. case NID_aes_256_ecb:
  919. #ifdef OLDER_OPENSSL
  920. *cipher = &pkcs11_aes_256_ecb;
  921. #else
  922. *cipher = pkcs11_aes_256_ecb();
  923. #endif
  924. break;
  925. case NID_aes_128_cbc:
  926. #ifdef OLDER_OPENSSL
  927. *cipher = &pkcs11_aes_128_cbc;
  928. #else
  929. *cipher = pkcs11_aes_128_cbc();
  930. #endif
  931. break;
  932. case NID_aes_192_cbc:
  933. #ifdef OLDER_OPENSSL
  934. *cipher = &pkcs11_aes_192_cbc;
  935. #else
  936. *cipher = pkcs11_aes_192_cbc();
  937. #endif
  938. break;
  939. case NID_aes_256_cbc:
  940. #ifdef OLDER_OPENSSL
  941. *cipher = &pkcs11_aes_256_cbc;
  942. #else
  943. *cipher = pkcs11_aes_256_cbc();
  944. #endif
  945. break;
  946. case NID_des_ecb:
  947. #ifdef OLDER_OPENSSL
  948. *cipher = &pkcs11_des_ecb;
  949. #else
  950. *cipher = pkcs11_des_ecb();
  951. #endif
  952. break;
  953. case NID_des_cbc:
  954. #ifdef OLDER_OPENSSL
  955. *cipher = &pkcs11_des_cbc;
  956. #else
  957. *cipher = pkcs11_des_cbc();
  958. #endif
  959. break;
  960. case NID_des_ede3_ecb:
  961. #ifdef OLDER_OPENSSL
  962. *cipher = &pkcs11_tdes_ecb;
  963. #else
  964. *cipher = pkcs11_tdes_ecb();
  965. #endif
  966. break;
  967. case NID_des_ede3_cbc:
  968. #ifdef OLDER_OPENSSL
  969. *cipher = &pkcs11_tdes_cbc;
  970. #else
  971. *cipher = pkcs11_tdes_cbc();
  972. #endif
  973. break;
  974. default:
  975. *cipher = NULL;
  976. break;
  977. }
  978. }
  979. return (*cipher != NULL);
  980. }
  981. static int pkcs11_engine_digests(ENGINE * e, const EVP_MD ** digest,
  982. const int **nids, int nid)
  983. {
  984. if (!digest)
  985. return get_pkcs11_digests(nids);
  986. if (!pkcs11_token) {
  987. PKCS11err(PKCS11_F_ENGINE_DIGESTS, PKCS11_R_NO_SLOT_SELECTED);
  988. return 0;
  989. }
  990. if (pkcs11_token->pkcs11_implemented_digests[nid]) {
  991. switch (nid) {
  992. case NID_ripemd160:
  993. #ifdef OLDER_OPENSSL
  994. *digest = &pkcs11_ripemd160;
  995. #else
  996. *digest = pkcs11_ripemd160();
  997. #endif
  998. break;
  999. case NID_md5:
  1000. #ifdef OLDER_OPENSSL
  1001. *digest = &pkcs11_md5;
  1002. #else
  1003. *digest = pkcs11_md5();
  1004. #endif
  1005. break;
  1006. case NID_sha1:
  1007. #ifdef OLDER_OPENSSL
  1008. *digest = &pkcs11_sha1;
  1009. #else
  1010. *digest = pkcs11_sha1();
  1011. #endif
  1012. break;
  1013. case NID_sha224:
  1014. #ifdef OLDER_OPENSSL
  1015. *digest = &pkcs11_sha224;
  1016. #else
  1017. *digest = pkcs11_sha224();
  1018. #endif
  1019. break;
  1020. case NID_sha256:
  1021. #ifdef OLDER_OPENSSL
  1022. *digest = &pkcs11_sha256;
  1023. #else
  1024. *digest = pkcs11_sha256();
  1025. #endif
  1026. break;
  1027. case NID_sha384:
  1028. #ifdef OLDER_OPENSSL
  1029. *digest = &pkcs11_sha384;
  1030. #else
  1031. *digest = pkcs11_sha384();
  1032. #endif
  1033. break;
  1034. case NID_sha512:
  1035. #ifdef OLDER_OPENSSL
  1036. *digest = &pkcs11_sha512;
  1037. #else
  1038. *digest = pkcs11_sha512();
  1039. #endif
  1040. break;
  1041. default:
  1042. *digest = NULL;
  1043. break;
  1044. }
  1045. }
  1046. return (*digest != NULL);
  1047. }
  1048. void *pkcs11_dso = NULL;
  1049. /* These are the static string constants for the DSO file name and the function
  1050. * symbol names to bind to.
  1051. */
  1052. static const char *PKCS11_LIBNAME = NULL;
  1053. static const char *get_PKCS11_LIBNAME(void)
  1054. {
  1055. if(PKCS11_LIBNAME)
  1056. return PKCS11_LIBNAME;
  1057. return def_PKCS11_LIBNAME;
  1058. }
  1059. static void free_PKCS11_LIBNAME(void)
  1060. {
  1061. if(PKCS11_LIBNAME)
  1062. OPENSSL_free((void*)PKCS11_LIBNAME);
  1063. PKCS11_LIBNAME = NULL;
  1064. }
  1065. static long set_PKCS11_LIBNAME(const char *name)
  1066. {
  1067. free_PKCS11_LIBNAME();
  1068. return ((PKCS11_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
  1069. }
  1070. /* Add new NID's based on this slot's token */
  1071. void pkcs11_regToken(ENGINE *e, struct _token *tok)
  1072. {
  1073. CK_RV rv;
  1074. CK_ULONG mech_cnt;
  1075. CK_MECHANISM_TYPE_PTR mech_list;
  1076. int i;
  1077. DBG_fprintf("%s\n", __FUNCTION__);
  1078. if (!tok)
  1079. return;
  1080. rv = pFunctionList->C_GetMechanismList(tok->slot_id, NULL_PTR, &mech_cnt);
  1081. if (rv != CKR_OK) {
  1082. pkcs11_die(PKCS11_F_ADDTOKEN, PKCS11_R_GETMECHANISMLIST, rv);
  1083. goto err;
  1084. }
  1085. /* Bounds check mech_cnt ? */
  1086. mech_list = (CK_MECHANISM_TYPE_PTR) OPENSSL_malloc(mech_cnt * sizeof(CK_MECHANISM_TYPE));
  1087. if (mech_list == NULL) {
  1088. pkcs11_die(PKCS11_F_ADDTOKEN, PKCS11_R_MALLOC_FAILURE, rv);
  1089. goto err;
  1090. }
  1091. rv = pFunctionList->C_GetMechanismList(tok->slot_id, mech_list, &mech_cnt);
  1092. if (rv != CKR_OK) {
  1093. pkcs11_die(PKCS11_F_ADDTOKEN, PKCS11_R_GETMECHANISMLIST, rv);
  1094. goto err_free;
  1095. }
  1096. /* Check which mechanisms are performed in hardware */
  1097. for( i = 0; i < mech_cnt; i++ ) {
  1098. switch (mech_list[i]) {
  1099. case CKM_RSA_PKCS_KEY_PAIR_GEN:
  1100. case CKM_RSA_PKCS:
  1101. case CKM_RSA_9796:
  1102. case CKM_RSA_X_509:
  1103. case CKM_RSA_PKCS_OAEP:
  1104. case CKM_RSA_X9_31:
  1105. case CKM_RSA_X9_31_KEY_PAIR_GEN:
  1106. case CKM_MD5_RSA_PKCS:
  1107. #ifndef OPENSSL_NO_RSA
  1108. DBG_fprintf("%s: registering RSA\n", __FUNCTION__);
  1109. #ifdef OLDER_OPENSSL
  1110. ENGINE_set_RSA(e, &pkcs11_rsa);
  1111. #else
  1112. ENGINE_set_RSA(e, pkcs11_rsa);
  1113. pkcs11_rsa = RSA_meth_new("PKCS#11 RSA", 0);
  1114. RSA_meth_set_pub_enc(pkcs11_rsa,
  1115. pkcs11_RSA_public_encrypt);
  1116. RSA_meth_set_pub_dec(pkcs11_rsa,
  1117. pkcs11_RSA_public_decrypt);
  1118. RSA_meth_set_priv_enc(pkcs11_rsa,
  1119. pkcs11_RSA_private_encrypt);
  1120. RSA_meth_set_priv_dec(pkcs11_rsa,
  1121. pkcs11_RSA_private_decrypt);
  1122. RSA_meth_set_init(pkcs11_rsa,
  1123. pkcs11_RSA_init);
  1124. RSA_meth_set_finish(pkcs11_rsa,
  1125. pkcs11_RSA_finish);
  1126. // RSA_meth_set_sign(pkcs11_rsa,
  1127. // pkcs11_RSA_sign);
  1128. // RSA_meth_set_verify(pkcs11_rsa,
  1129. // pkcs11_RSA_verify);
  1130. RSA_meth_set_keygen(pkcs11_rsa,
  1131. pkcs11_RSA_generate_key);
  1132. #endif
  1133. ENGINE_set_load_privkey_function(e, pkcs11_load_privkey);
  1134. ENGINE_set_load_pubkey_function(e, pkcs11_load_pubkey);
  1135. #endif
  1136. break;
  1137. case CKM_DH_PKCS_KEY_PAIR_GEN:
  1138. case CKM_DH_PKCS_DERIVE:
  1139. case CKM_X9_42_DH_KEY_PAIR_GEN:
  1140. case CKM_X9_42_DH_DERIVE:
  1141. case CKM_X9_42_DH_HYBRID_DERIVE:
  1142. case CKM_DH_PKCS_PARAMETER_GEN:
  1143. case CKM_X9_42_DH_PARAMETER_GEN:
  1144. break;
  1145. case CKM_DES_ECB:
  1146. tok->pkcs11_implemented_ciphers[NID_des_ecb] = 1;
  1147. num_cipher_nids++;
  1148. break;
  1149. case CKM_DES_CBC:
  1150. case CKM_DES_CBC_PAD:
  1151. tok->pkcs11_implemented_ciphers[NID_des_cbc] = 1;
  1152. num_cipher_nids++;
  1153. break;
  1154. case CKM_DES_KEY_GEN:
  1155. case CKM_DES_MAC:
  1156. case CKM_DES_MAC_GENERAL:
  1157. break;
  1158. case CKM_DES3_ECB:
  1159. tok->pkcs11_implemented_ciphers[NID_des_ede3_ecb] = 1;
  1160. num_cipher_nids++;
  1161. break;
  1162. case CKM_DES3_CBC:
  1163. case CKM_DES3_CBC_PAD:
  1164. tok->pkcs11_implemented_ciphers[NID_des_ede3_cbc] = 1;
  1165. num_cipher_nids++;
  1166. break;
  1167. case CKM_DES3_KEY_GEN:
  1168. case CKM_DES3_MAC:
  1169. case CKM_DES3_MAC_GENERAL:
  1170. break;
  1171. case CKM_SHA_1:
  1172. tok->pkcs11_implemented_digests[NID_sha1] = 1;
  1173. num_digest_nids++;
  1174. break;
  1175. case CKM_SHA_1_HMAC:
  1176. case CKM_SHA_1_HMAC_GENERAL:
  1177. tok->pkcs11_implemented_digests[NID_hmacWithSHA1] = 1;
  1178. num_digest_nids++;
  1179. break;
  1180. case CKM_PBA_SHA1_WITH_SHA1_HMAC:
  1181. case CKM_SHA1_KEY_DERIVATION:
  1182. case CKM_SHA1_RSA_PKCS:
  1183. tok->pkcs11_implemented_digests[NID_sha1WithRSAEncryption] = 1;
  1184. num_digest_nids++;
  1185. break;
  1186. case CKM_SHA224:
  1187. tok->pkcs11_implemented_digests[NID_sha224] = 1;
  1188. num_digest_nids++;
  1189. break;
  1190. case CKM_SHA224_KEY_DERIVATION:
  1191. case CKM_SHA224_RSA_PKCS:
  1192. tok->pkcs11_implemented_digests[NID_sha224WithRSAEncryption] = 1;
  1193. num_digest_nids++;
  1194. break;
  1195. case CKM_SHA256:
  1196. tok->pkcs11_implemented_digests[NID_sha256] = 1;
  1197. num_digest_nids++;
  1198. break;
  1199. case CKM_SHA256_KEY_DERIVATION:
  1200. case CKM_SHA256_RSA_PKCS:
  1201. tok->pkcs11_implemented_digests[NID_sha256WithRSAEncryption] = 1;
  1202. num_digest_nids++;
  1203. break;
  1204. case CKM_SHA384:
  1205. tok->pkcs11_implemented_digests[NID_sha384] = 1;
  1206. num_digest_nids++;
  1207. break;
  1208. case CKM_SHA384_KEY_DERIVATION:
  1209. case CKM_SHA384_RSA_PKCS:
  1210. tok->pkcs11_implemented_digests[NID_sha384WithRSAEncryption] = 1;
  1211. num_digest_nids++;
  1212. break;
  1213. case CKM_SHA512:
  1214. tok->pkcs11_implemented_digests[NID_sha512] = 1;
  1215. num_digest_nids++;
  1216. break;
  1217. case CKM_SHA512_KEY_DERIVATION:
  1218. case CKM_SHA512_RSA_PKCS:
  1219. tok->pkcs11_implemented_digests[NID_sha512WithRSAEncryption] = 1;
  1220. num_digest_nids++;
  1221. break;
  1222. case CKM_AES_ECB:
  1223. tok->pkcs11_implemented_ciphers[NID_aes_128_ecb] = 1;
  1224. tok->pkcs11_implemented_ciphers[NID_aes_192_ecb] = 1;
  1225. tok->pkcs11_implemented_ciphers[NID_aes_256_ecb] = 1;
  1226. num_cipher_nids += 3;
  1227. break;
  1228. case CKM_AES_KEY_GEN:
  1229. break;
  1230. case CKM_AES_CBC_PAD:
  1231. case CKM_AES_CBC:
  1232. tok->pkcs11_implemented_ciphers[NID_aes_128_cbc] = 1;
  1233. tok->pkcs11_implemented_ciphers[NID_aes_192_cbc] = 1;
  1234. tok->pkcs11_implemented_ciphers[NID_aes_256_cbc] = 1;
  1235. num_cipher_nids += 3;
  1236. break;
  1237. case CKM_AES_MAC:
  1238. case CKM_AES_MAC_GENERAL:
  1239. break;
  1240. case CKM_MD5:
  1241. tok->pkcs11_implemented_digests[NID_md5] = 1;
  1242. num_digest_nids++;
  1243. break;
  1244. case CKM_MD5_HMAC:
  1245. case CKM_MD5_HMAC_GENERAL:
  1246. case CKM_SSL3_PRE_MASTER_KEY_GEN:
  1247. case CKM_SSL3_MASTER_KEY_DERIVE:
  1248. case CKM_SSL3_KEY_AND_MAC_DERIVE:
  1249. case CKM_SSL3_MD5_MAC:
  1250. case CKM_SSL3_SHA1_MAC:
  1251. break;
  1252. case CKM_RIPEMD160:
  1253. tok->pkcs11_implemented_digests[NID_ripemd160] = 1;
  1254. num_digest_nids++;
  1255. break;
  1256. case CKM_RIPEMD160_HMAC:
  1257. case CKM_RIPEMD160_HMAC_GENERAL:
  1258. break;
  1259. default:
  1260. DBG_fprintf("The token in slot %lx has reported that it can "
  1261. "perform\nmechanism 0x%lx, which is not available to "
  1262. "accelerate in openssl.\n", tok->slot_id, mech_list[i]);
  1263. break;
  1264. }
  1265. }
  1266. err_free:
  1267. OPENSSL_free(mech_list);
  1268. err:
  1269. return;
  1270. }
  1271. /* Add a new token struct to the list
  1272. * This is called during the bind_pkcs11, in other words after openSSL has
  1273. * decided to use us for some operation.
  1274. */
  1275. struct _token *pkcs11_addToken(CK_SLOT_ID slot_id)
  1276. {
  1277. struct _token *new_tok = (struct _token *) OPENSSL_malloc(sizeof(struct _token));
  1278. if (new_tok == NULL) {
  1279. PKCS11err(PKCS11_F_ADDTOKEN, PKCS11_R_MALLOC_FAILURE);
  1280. return NULL;
  1281. }
  1282. memset(new_tok, 0, sizeof(struct _token));
  1283. new_tok->slot_id = slot_id;
  1284. new_tok->token_next = pkcs11_token_list;
  1285. pkcs11_token_list = new_tok;
  1286. return new_tok;
  1287. }
  1288. /*
  1289. * pre_init_pkcs11: this is called at openSSL startup. Here is where we
  1290. * try to convince openSSL to use us. If it decides not to, there is no
  1291. * guarantee that we will ever be asked to clean up. So everything we
  1292. * do must be self-contained.
  1293. */
  1294. static int pre_init_pkcs11(ENGINE *e)
  1295. {
  1296. CK_C_GetFunctionList p;
  1297. CK_RV rv = CKR_OK;
  1298. CK_INFO Info;
  1299. CK_SLOT_ID_PTR pSlotList;
  1300. CK_ULONG ulSlotCount;
  1301. CK_SLOT_INFO slotInfo;
  1302. struct _token *tok;
  1303. int i;
  1304. if(pkcs11_dso)
  1305. {
  1306. PKCS11err(PKCS11_F_PREINIT, PKCS11_R_ALREADY_LOADED);
  1307. goto err;
  1308. }
  1309. /* Attempt to load PKCS#11 library */
  1310. pkcs11_dso = dlopen(get_PKCS11_LIBNAME(), RTLD_NOW);
  1311. if(pkcs11_dso == NULL)
  1312. {
  1313. PKCS11err(PKCS11_F_PREINIT, PKCS11_R_DSO_FAILURE);
  1314. goto err;
  1315. }
  1316. /* get the C_GetFunctionList function from the loaded library */
  1317. p = (CK_C_GetFunctionList)dlsym(pkcs11_dso, PKCS11_GET_FUNCTION_LIST);
  1318. if ( !p )
  1319. {
  1320. PKCS11err(PKCS11_F_PREINIT, PKCS11_R_DSO_FAILURE);
  1321. goto err;
  1322. }
  1323. /* get the full function list from the loaded library */
  1324. rv = p(&pFunctionList);
  1325. if (rv != CKR_OK)
  1326. {
  1327. pkcs11_die(PKCS11_F_PREINIT, PKCS11_R_DSO_FAILURE, rv);
  1328. goto err;
  1329. }
  1330. #ifdef BULL_CRYPTOBOX
  1331. /* the address of the CryptoBox is in /etc/CryptoBox */
  1332. rv = C_InitializeRpc (NULL, NULL, NULL);
  1333. if (rv != CKR_OK)
  1334. goto err;
  1335. #endif
  1336. /* Initialize Cryptoki */
  1337. rv = pFunctionList->C_Initialize(NULL_PTR);
  1338. if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
  1339. {
  1340. pkcs11_die(PKCS11_F_PREINIT, PKCS11_R_INITIALIZE, rv);
  1341. goto err;
  1342. }
  1343. rv = pFunctionList->C_GetInfo(&Info);
  1344. if (rv != CKR_OK)
  1345. {
  1346. pkcs11_die(PKCS11_F_PREINIT, PKCS11_R_GETINFO, rv);
  1347. pFunctionList->C_Finalize(NULL);
  1348. goto err;
  1349. }
  1350. if (strncmp((char *)Info.libraryDescription, BULL_TRUSTWAY_LIBRARY_DESCRIPTION, 32))
  1351. {
  1352. rv = pFunctionList->C_GetSlotList(TRUE, NULL_PTR, &ulSlotCount);
  1353. if ((rv != CKR_OK) || (ulSlotCount == 0))
  1354. {
  1355. pkcs11_die(PKCS11_F_PREINIT, PKCS11_R_GETSLOTLIST, rv);
  1356. }
  1357. else
  1358. {
  1359. pSlotList = (CK_SLOT_ID_PTR) OPENSSL_malloc(ulSlotCount * sizeof(CK_SLOT_ID));
  1360. if ( pSlotList != NULL)
  1361. {
  1362. rv = pFunctionList->C_GetSlotList(TRUE, pSlotList, &ulSlotCount);
  1363. if (rv != CKR_OK)
  1364. {
  1365. pkcs11_die(PKCS11_F_PREINIT, PKCS11_R_GETSLOTLIST, rv);
  1366. pFunctionList->C_Finalize(NULL);
  1367. OPENSSL_free(pSlotList);
  1368. goto err;
  1369. }
  1370. /* Check each slot to see if there's a hardware token present.
  1371. */
  1372. for (i = 0; i < ulSlotCount; i++)
  1373. {
  1374. rv = pFunctionList->C_GetSlotInfo(pSlotList[i], &slotInfo);
  1375. if (rv != CKR_OK)
  1376. {
  1377. pkcs11_die(PKCS11_F_PREINIT, PKCS11_R_GETSLOTINFO,
  1378. rv);
  1379. pFunctionList->C_Finalize(NULL);
  1380. OPENSSL_free(pSlotList);
  1381. goto err;
  1382. }
  1383. /* we're mallocing memory here that may need to be freed
  1384. * if openssl chooses not to use us. We'll free it in
  1385. * the library destructor, pkcs11_engine_destructor */
  1386. tok = pkcs11_addToken(pSlotList[i]);
  1387. pkcs11_regToken(e, tok);
  1388. }
  1389. OPENSSL_free(pSlotList);
  1390. }
  1391. }
  1392. }
  1393. else
  1394. {
  1395. /* Bull Trustway CC2000 crypto hardware detected */
  1396. Bull_TrustWay = TRUE;
  1397. SLOTID = 0xFFFFFFFF;
  1398. }
  1399. /* Finish with Cryptoki: We will restart if openSSL calls one of our
  1400. * functions */
  1401. pFunctionList->C_Finalize(NULL);
  1402. dlclose(pkcs11_dso);
  1403. pkcs11_dso = NULL;
  1404. return 1;
  1405. err:
  1406. if(pkcs11_dso)
  1407. dlclose(pkcs11_dso);
  1408. pkcs11_dso = NULL;
  1409. return 0;
  1410. }
  1411. /* initialization function */
  1412. /* This is called when openSSL has decided to use us, and warns us to
  1413. * initialize. pkcs11_finish will be called when all is done. */
  1414. static int pkcs11_init(ENGINE *e)
  1415. {
  1416. CK_C_GetFunctionList p;
  1417. CK_RV rv = CKR_OK;
  1418. CK_INFO Info;
  1419. CK_SLOT_ID_PTR pSlotList;
  1420. CK_ULONG ulSlotCount;
  1421. CK_SLOT_INFO slotInfo;
  1422. int i;
  1423. if(pkcs11_dso)
  1424. {
  1425. PKCS11err(PKCS11_F_INIT, PKCS11_R_ALREADY_LOADED);
  1426. goto err;
  1427. }
  1428. /* Attempt to load PKCS#11 library */
  1429. pkcs11_dso = dlopen(get_PKCS11_LIBNAME(), RTLD_NOW);
  1430. if(pkcs11_dso == NULL)
  1431. {
  1432. PKCS11err(PKCS11_F_INIT, PKCS11_R_DSO_FAILURE);
  1433. goto err;
  1434. }
  1435. /* get the C_GetFunctionList function from the loaded library */
  1436. p = (CK_C_GetFunctionList)dlsym(pkcs11_dso, PKCS11_GET_FUNCTION_LIST);
  1437. if ( !p )
  1438. {
  1439. PKCS11err(PKCS11_F_INIT, PKCS11_R_DSO_FAILURE);
  1440. goto err;
  1441. }
  1442. /* get the full function list from the loaded library */
  1443. rv = p(&pFunctionList);
  1444. if (rv != CKR_OK)
  1445. {
  1446. pkcs11_die(PKCS11_F_INIT, PKCS11_R_DSO_FAILURE, rv);
  1447. goto err;
  1448. }
  1449. #ifdef BULL_CRYPTOBOX
  1450. /* the address of the CryptoBox is in /etc/CryptoBox */
  1451. rv = C_InitializeRpc (NULL, NULL, NULL);
  1452. if (rv != CKR_OK)
  1453. goto err;
  1454. #endif
  1455. rv = pFunctionList->C_Initialize(NULL_PTR);
  1456. if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
  1457. {
  1458. pkcs11_die(PKCS11_F_INIT, PKCS11_R_INITIALIZE, rv);
  1459. goto err;
  1460. }
  1461. rv = pFunctionList->C_GetInfo(&Info);
  1462. if (rv != CKR_OK)
  1463. {
  1464. pkcs11_die(PKCS11_F_INIT, PKCS11_R_GETINFO, rv);
  1465. pFunctionList->C_Finalize(NULL);
  1466. goto err;
  1467. }
  1468. if (strncmp((char *)Info.libraryDescription, BULL_TRUSTWAY_LIBRARY_DESCRIPTION, 32))
  1469. {
  1470. rv = pFunctionList->C_GetSlotList(TRUE, NULL_PTR, &ulSlotCount);
  1471. if ((rv != CKR_OK) || (ulSlotCount == 0))
  1472. {
  1473. pkcs11_die(PKCS11_F_INIT, PKCS11_R_GETSLOTLIST, rv);
  1474. }
  1475. else
  1476. {
  1477. pSlotList = (CK_SLOT_ID_PTR) OPENSSL_malloc(ulSlotCount * sizeof(CK_SLOT_ID));
  1478. if ( pSlotList != NULL)
  1479. {
  1480. rv = pFunctionList->C_GetSlotList(TRUE, pSlotList, &ulSlotCount);
  1481. if (rv != CKR_OK)
  1482. {
  1483. pkcs11_die(PKCS11_F_INIT, PKCS11_R_GETSLOTLIST, rv);
  1484. pFunctionList->C_Finalize(NULL);
  1485. OPENSSL_free(pSlotList);
  1486. goto err;
  1487. }
  1488. /* Check each slot to see if there's a hardware token present.
  1489. */
  1490. for (i = 0; i < ulSlotCount; i++)
  1491. {
  1492. rv = pFunctionList->C_GetSlotInfo(pSlotList[i], &slotInfo);
  1493. if (rv != CKR_OK)
  1494. {
  1495. pkcs11_die(PKCS11_F_INIT, PKCS11_R_GETSLOTINFO, rv);
  1496. pFunctionList->C_Finalize(NULL);
  1497. OPENSSL_free(pSlotList);
  1498. goto err;
  1499. }
  1500. pkcs11_addToken(pSlotList[i]);
  1501. }
  1502. OPENSSL_free(pSlotList);
  1503. }
  1504. }
  1505. }
  1506. else
  1507. {
  1508. /* Bull Trustway CC2000 crypto hardware detected */
  1509. Bull_TrustWay = TRUE;
  1510. SLOTID = 0xFFFFFFFF;
  1511. }
  1512. #ifndef OPENSSL_NO_RSA
  1513. /* Everything's fine. */
  1514. if (rsaPubKey == -1)
  1515. rsaPubKey = RSA_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  1516. if (rsaPrivKey == -1)
  1517. rsaPrivKey = RSA_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  1518. if (deletePubKeyOnFree == -1)
  1519. deletePubKeyOnFree = RSA_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  1520. if (deletePrivKeyOnFree == -1)
  1521. deletePrivKeyOnFree = RSA_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  1522. if (pkcs11Session == -1)
  1523. pkcs11Session = RSA_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  1524. #endif
  1525. if (pkcs11_token_list == NULL)
  1526. PKCS11err(PKCS11_F_INIT, PKCS11_R_NOTOKENS);
  1527. PKCS11_Initialized = 1;
  1528. /* TODO: This should only be done on linux systems */
  1529. pthread_atfork(NULL, NULL, (void(*)())pkcs11_atfork_init);
  1530. return 1;
  1531. err:
  1532. if(pkcs11_dso)
  1533. dlclose(pkcs11_dso);
  1534. pkcs11_dso = NULL;
  1535. return 0;
  1536. }
  1537. /* Destructor (complements the "ENGINE_pkcs11()" constructor) */
  1538. /* XXX HUH? Can we just DSO_load once, then??? */
  1539. static int pkcs11_destroy(ENGINE *e)
  1540. {
  1541. DBG_fprintf("%s: called\n", __FUNCTION__);
  1542. #ifndef OLDER_OPENSSL
  1543. pkcs11_des_ecb_destroy();
  1544. pkcs11_des_cbc_destroy();
  1545. pkcs11_tdes_ecb_destroy();
  1546. pkcs11_tdes_cbc_destroy();
  1547. pkcs11_aes_128_cbc_destroy();
  1548. pkcs11_aes_192_cbc_destroy();
  1549. pkcs11_aes_256_cbc_destroy();
  1550. pkcs11_aes_128_ecb_destroy();
  1551. pkcs11_aes_192_ecb_destroy();
  1552. pkcs11_aes_256_ecb_destroy();
  1553. pkcs11_sha1_destroy();
  1554. pkcs11_sha224_destroy();
  1555. pkcs11_sha256_destroy();
  1556. pkcs11_sha384_destroy();
  1557. pkcs11_sha512_destroy();
  1558. pkcs11_md5_destroy();
  1559. pkcs11_ripemd160_destroy();
  1560. #endif
  1561. free_PKCS11_LIBNAME();
  1562. ERR_unload_pkcs11_strings();
  1563. return 1;
  1564. }
  1565. /* termination function */
  1566. static int pkcs11_finish(ENGINE *e)
  1567. {
  1568. struct _token *tmp;
  1569. if(pkcs11_dso == NULL)
  1570. {
  1571. PKCS11err(PKCS11_F_FINISH, PKCS11_R_NOT_LOADED);
  1572. goto err;
  1573. }
  1574. assert(pFunctionList != NULL);
  1575. while (pkcs11_token_list) {
  1576. tmp = pkcs11_token_list->token_next;
  1577. OPENSSL_free(pkcs11_token_list);
  1578. pkcs11_token_list = tmp;
  1579. }
  1580. pFunctionList->C_Finalize(NULL);
  1581. if(dlclose(pkcs11_dso))
  1582. { PKCS11err(PKCS11_F_FINISH, PKCS11_R_DSO_FAILURE);
  1583. goto err;
  1584. }
  1585. pkcs11_dso = NULL;
  1586. pFunctionList = NULL;
  1587. return 1;
  1588. err:
  1589. pkcs11_dso = NULL;
  1590. pFunctionList = NULL;
  1591. return 0;
  1592. }
  1593. static int pkcs11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
  1594. {
  1595. int initialized = ((pkcs11_dso == NULL) ? 0 : 1);
  1596. struct _token *tok;
  1597. switch(cmd)
  1598. {
  1599. case PKCS11_CMD_SO_PATH:
  1600. if(p == NULL)
  1601. {
  1602. PKCS11err(PKCS11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  1603. return 0;
  1604. }
  1605. if(initialized)
  1606. {
  1607. PKCS11err(PKCS11_F_CTRL, PKCS11_R_ALREADY_LOADED);
  1608. return 0;
  1609. }
  1610. return set_PKCS11_LIBNAME((const char*)p);
  1611. case PKCS11_CMD_SLOT_ID:
  1612. tok = pkcs11_token_list;
  1613. while (tok) {
  1614. if (tok->slot_id == i) {
  1615. pkcs11_token = tok;
  1616. DBG_fprintf("slot %ld selected\n", i);
  1617. return 1;
  1618. }
  1619. tok = tok->token_next;
  1620. }
  1621. PKCS11err(PKCS11_F_CTRL, PKCS11_R_TOKEN_NOT_AVAILABLE);
  1622. return 0;
  1623. default:
  1624. break;
  1625. }
  1626. PKCS11err(PKCS11_F_CTRL,PKCS11_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  1627. return 0;
  1628. }
  1629. #ifndef OPENSSL_NO_RSA
  1630. CK_OBJECT_HANDLE pkcs11_FindOrCreateKey(CK_SESSION_HANDLE h,
  1631. RSA *rsa,
  1632. CK_OBJECT_CLASS oKey,
  1633. CK_BBOOL fKeyCreate)
  1634. {
  1635. CK_RV rv;
  1636. CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE;
  1637. CK_ULONG Matches;
  1638. CK_KEY_TYPE kType = CKK_RSA;
  1639. CK_ULONG ulKeyAttributeCount;
  1640. #ifndef OLDER_OPENSSL
  1641. const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
  1642. #endif
  1643. CK_ATTRIBUTE pubKeyTemplate[] =
  1644. {
  1645. {CKA_CLASS, &oKey, sizeof(CK_OBJECT_CLASS)},
  1646. {CKA_KEY_TYPE, &kType, sizeof(CK_KEY_TYPE)},
  1647. {CKA_MODULUS, (void *)NULL, 0},
  1648. {CKA_PUBLIC_EXPONENT, (void *)NULL, 0},
  1649. };
  1650. CK_ATTRIBUTE privKeyTemplate[] =
  1651. {
  1652. {CKA_CLASS, &oKey, sizeof(CK_OBJECT_CLASS)},
  1653. {CKA_KEY_TYPE, &kType, sizeof(CK_KEY_TYPE)},
  1654. {CKA_MODULUS, (void *)NULL, 0},
  1655. {CKA_PUBLIC_EXPONENT, (void *)NULL, 0},
  1656. {CKA_PRIVATE_EXPONENT, (void *)NULL, 0},
  1657. {CKA_PRIME_1, (void *)NULL, 0},
  1658. {CKA_PRIME_2, (void *)NULL, 0},
  1659. {CKA_EXPONENT_1, (void *)NULL, 0},
  1660. {CKA_EXPONENT_2, (void *)NULL, 0},
  1661. {CKA_COEFFICIENT, (void *)NULL, 0}
  1662. };
  1663. long deletePubKey;
  1664. DBG_fprintf("%s\n", __FUNCTION__);
  1665. if (oKey == CKO_PUBLIC_KEY) {
  1666. DBG_fprintf("looking up a public key\n");
  1667. #ifdef OLDER_OPENSSL
  1668. pubKeyTemplate[2].ulValueLen = BN_num_bytes(rsa->n);
  1669. pubKeyTemplate[3].ulValueLen = BN_num_bytes(rsa->e);
  1670. #else
  1671. RSA_get0_key(rsa, &n, &e, NULL);
  1672. pubKeyTemplate[2].ulValueLen = BN_num_bytes(n);
  1673. pubKeyTemplate[3].ulValueLen = BN_num_bytes(e);
  1674. #endif
  1675. pubKeyTemplate[2].pValue = (CK_VOID_PTR)OPENSSL_malloc(
  1676. (size_t)pubKeyTemplate[2].ulValueLen);
  1677. pubKeyTemplate[3].pValue = (CK_VOID_PTR)OPENSSL_malloc(
  1678. (size_t)pubKeyTemplate[3].ulValueLen);
  1679. #ifdef OLDER_OPENSSL
  1680. BN_bn2bin(rsa->n, pubKeyTemplate[2].pValue);
  1681. BN_bn2bin(rsa->e, pubKeyTemplate[3].pValue);
  1682. #else
  1683. BN_bn2bin(n, pubKeyTemplate[2].pValue);
  1684. BN_bn2bin(e, pubKeyTemplate[3].pValue);
  1685. #endif
  1686. ulKeyAttributeCount = 4;
  1687. rv = pFunctionList->C_FindObjectsInit(h, pubKeyTemplate, ulKeyAttributeCount);
  1688. } else {
  1689. DBG_fprintf("looking up a private key\n");
  1690. #ifdef OLDER_OPENSSL
  1691. privKeyTemplate[2].ulValueLen = BN_num_bytes(rsa->n);
  1692. privKeyTemplate[4].ulValueLen = BN_num_bytes(rsa->d);
  1693. privKeyTemplate[5].ulValueLen = BN_num_bytes(rsa->p);
  1694. privKeyTemplate[6].ulValueLen = BN_num_bytes(rsa->q);
  1695. privKeyTemplate[7].ulValueLen = BN_num_bytes(rsa->dmp1);
  1696. privKeyTemplate[8].ulValueLen = BN_num_bytes(rsa->dmq1);
  1697. privKeyTemplate[9].ulValueLen = BN_num_bytes(rsa->iqmp);
  1698. #else
  1699. RSA_get0_key(rsa, &n, NULL, &d);
  1700. RSA_get0_factors(rsa, &p, &q);
  1701. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  1702. privKeyTemplate[2].ulValueLen = BN_num_bytes(n);
  1703. privKeyTemplate[4].ulValueLen = BN_num_bytes(d);
  1704. privKeyTemplate[5].ulValueLen = BN_num_bytes(p);
  1705. privKeyTemplate[6].ulValueLen = BN_num_bytes(q);
  1706. privKeyTemplate[7].ulValueLen = BN_num_bytes(dmp1);
  1707. privKeyTemplate[8].ulValueLen = BN_num_bytes(dmq1);
  1708. privKeyTemplate[9].ulValueLen = BN_num_bytes(iqmp);
  1709. #endif
  1710. privKeyTemplate[2].pValue = (CK_VOID_PTR)OPENSSL_malloc(