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.

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