xref: /onnv-gate/usr/src/common/openssl/crypto/engine/hw_pk11.c (revision 7534:8194222f8a8b)
10Sstevel@tonic-gate /*
26847Svk199839  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /* crypto/engine/hw_pk11.c */
70Sstevel@tonic-gate /* This product includes software developed by the OpenSSL Project for
80Sstevel@tonic-gate  * use in the OpenSSL Toolkit (http://www.openssl.org/).
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * This project also referenced hw_pkcs11-0.9.7b.patch written by
110Sstevel@tonic-gate  * Afchine Madjlessi.
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate /* ====================================================================
140Sstevel@tonic-gate  * Copyright (c) 2000-2001 The OpenSSL Project.  All rights reserved.
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
170Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
180Sstevel@tonic-gate  * are met:
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
210Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
240Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
250Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
260Sstevel@tonic-gate  *    distribution.
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
290Sstevel@tonic-gate  *    software must display the following acknowledgment:
300Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
310Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
340Sstevel@tonic-gate  *    endorse or promote products derived from this software without
350Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
360Sstevel@tonic-gate  *    licensing@OpenSSL.org.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
390Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
400Sstevel@tonic-gate  *    permission of the OpenSSL Project.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
430Sstevel@tonic-gate  *    acknowledgment:
440Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
450Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
480Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
490Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
500Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
510Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
520Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
530Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
540Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
550Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
560Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
570Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
580Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
590Sstevel@tonic-gate  * ====================================================================
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
620Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
630Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <stdio.h>
680Sstevel@tonic-gate #include <stdlib.h>
690Sstevel@tonic-gate #include <string.h>
700Sstevel@tonic-gate #include <sys/types.h>
710Sstevel@tonic-gate #include <unistd.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include <openssl/e_os2.h>
746847Svk199839 #include <openssl/crypto.h>
750Sstevel@tonic-gate #include <openssl/engine.h>
760Sstevel@tonic-gate #include <openssl/dso.h>
770Sstevel@tonic-gate #include <openssl/err.h>
780Sstevel@tonic-gate #include <openssl/bn.h>
792139Sjp161948 #include <openssl/md5.h>
800Sstevel@tonic-gate #include <openssl/pem.h>
816847Svk199839 #ifndef OPENSSL_NO_RSA
820Sstevel@tonic-gate #include <openssl/rsa.h>
836847Svk199839 #endif
846847Svk199839 #ifndef OPENSSL_NO_DSA
856847Svk199839 #include <openssl/dsa.h>
866847Svk199839 #endif
876847Svk199839 #ifndef OPENSSL_NO_DH
886847Svk199839 #include <openssl/dh.h>
896847Svk199839 #endif
900Sstevel@tonic-gate #include <openssl/rand.h>
910Sstevel@tonic-gate #include <openssl/objects.h>
920Sstevel@tonic-gate #include <openssl/x509.h>
937211Sjp161948 #include <openssl/aes.h>
940Sstevel@tonic-gate #include <cryptlib.h>
957211Sjp161948 #include <dlfcn.h>
967526SVladimir.Kotal@Sun.COM #include <pthread.h>
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #ifndef OPENSSL_NO_HW
990Sstevel@tonic-gate #ifndef OPENSSL_NO_HW_PK11
1000Sstevel@tonic-gate 
1017211Sjp161948 /* label for debug messages printed on stderr */
1027211Sjp161948 #define	PK11_DBG	"PKCS#11 ENGINE DEBUG"
1037211Sjp161948 /* prints a lot of debug messages on stderr about slot selection process */
1047211Sjp161948 #undef	DEBUG_SLOT_SELECTION
1057211Sjp161948 /*
1067211Sjp161948  * Solaris specific code. See comment at check_hw_mechanisms() for more
1077211Sjp161948  * information.
1087211Sjp161948  */
1097211Sjp161948 #define	SOLARIS_HW_SLOT_SELECTION
1107211Sjp161948 
1117211Sjp161948 /*
1127211Sjp161948  * AES counter mode is not supported in the OpenSSL EVP API yet and neither
1137211Sjp161948  * there are official OIDs for mechanisms based on this mode. With our changes,
1147211Sjp161948  * an application can define its own EVP calls for AES counter mode and then
1157211Sjp161948  * it can make use of hardware acceleration through this engine. However, it's
1167211Sjp161948  * better if we keep AES CTR support code under ifdef's.
1177211Sjp161948  */
1187211Sjp161948 #define	SOLARIS_AES_CTR
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #include "security/cryptoki.h"
1210Sstevel@tonic-gate #include "security/pkcs11.h"
1220Sstevel@tonic-gate #include "hw_pk11_err.c"
1230Sstevel@tonic-gate 
1247211Sjp161948 #ifdef	SOLARIS_AES_CTR
1257211Sjp161948 /*
1267211Sjp161948  * NIDs for AES counter mode that will be defined during the engine
1277211Sjp161948  * initialization.
1287211Sjp161948  */
1297211Sjp161948 int NID_aes_128_ctr = NID_undef;
1307211Sjp161948 int NID_aes_192_ctr = NID_undef;
1317211Sjp161948 int NID_aes_256_ctr = NID_undef;
1327211Sjp161948 #endif	/* SOLARIS_AES_CTR */
1337211Sjp161948 
1347211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
1357211Sjp161948 /*
1367211Sjp161948  * Tables for symmetric ciphers and digest mechs found in the pkcs11_kernel
1377211Sjp161948  * library. See comment at check_hw_mechanisms() for more information.
1387211Sjp161948  */
1397211Sjp161948 int *hw_cnids;
1407211Sjp161948 int *hw_dnids;
1417211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
1427211Sjp161948 
1437526SVladimir.Kotal@Sun.COM /* PKCS#11 session caches and their locks for all operation types */
1447526SVladimir.Kotal@Sun.COM static PK11_CACHE session_cache[OP_MAX];
1457526SVladimir.Kotal@Sun.COM 
1467211Sjp161948 /*
1477526SVladimir.Kotal@Sun.COM  * As stated in v2.20, 11.7 Object Management Function, in section for
1487526SVladimir.Kotal@Sun.COM  * C_FindObjectsInit(), at most one search operation may be active at a given
1497526SVladimir.Kotal@Sun.COM  * time in a given session. Therefore, C_Find{,Init,Final}Objects() should be
1507526SVladimir.Kotal@Sun.COM  * grouped together to form one atomic search operation. This is already
1517526SVladimir.Kotal@Sun.COM  * ensured by the property of unique PKCS#11 session handle used for each
1527526SVladimir.Kotal@Sun.COM  * PK11_SESSION object.
1537526SVladimir.Kotal@Sun.COM  *
1547526SVladimir.Kotal@Sun.COM  * This is however not the biggest concern - maintaining consistency of the
1557526SVladimir.Kotal@Sun.COM  * underlying object store is more important. The same section of the spec also
1567526SVladimir.Kotal@Sun.COM  * says that one thread can be in the middle of a search operation while another
1577526SVladimir.Kotal@Sun.COM  * thread destroys the object matching the search template which would result in
1587526SVladimir.Kotal@Sun.COM  * invalid handle returned from the search operation.
1597526SVladimir.Kotal@Sun.COM  *
1607526SVladimir.Kotal@Sun.COM  * Hence, the following locks are used for both protection of the object stores.
1617526SVladimir.Kotal@Sun.COM  * They are also used for active list protection.
1627211Sjp161948  */
1637526SVladimir.Kotal@Sun.COM pthread_mutex_t *find_lock[OP_MAX] = { NULL };
1647526SVladimir.Kotal@Sun.COM 
1657526SVladimir.Kotal@Sun.COM /*
1667526SVladimir.Kotal@Sun.COM  * lists of asymmetric key handles which are active (referenced by at least one
1677526SVladimir.Kotal@Sun.COM  * PK11_SESSION structure, either held by a thread or present in free_session
1687526SVladimir.Kotal@Sun.COM  * list) for given algorithm type
1697526SVladimir.Kotal@Sun.COM  */
1707526SVladimir.Kotal@Sun.COM PK11_active *active_list[OP_MAX] = { NULL };
1717526SVladimir.Kotal@Sun.COM 
1727526SVladimir.Kotal@Sun.COM /*
1737526SVladimir.Kotal@Sun.COM  * Create all secret key objects in a global session so that they are available
1740Sstevel@tonic-gate  * to use for other sessions. These other sessions may be opened or closed
1757526SVladimir.Kotal@Sun.COM  * without losing the secret key objects.
1767526SVladimir.Kotal@Sun.COM  */
1770Sstevel@tonic-gate static CK_SESSION_HANDLE	global_session = CK_INVALID_HANDLE;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /* ENGINE level stuff */
1800Sstevel@tonic-gate static int pk11_init(ENGINE *e);
1810Sstevel@tonic-gate static int pk11_library_init(ENGINE *e);
1820Sstevel@tonic-gate static int pk11_finish(ENGINE *e);
1830Sstevel@tonic-gate static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
1840Sstevel@tonic-gate static int pk11_destroy(ENGINE *e);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /* RAND stuff */
1870Sstevel@tonic-gate static void pk11_rand_seed(const void *buf, int num);
1880Sstevel@tonic-gate static void pk11_rand_add(const void *buf, int num, double add_entropy);
1890Sstevel@tonic-gate static void pk11_rand_cleanup(void);
1900Sstevel@tonic-gate static int pk11_rand_bytes(unsigned char *buf, int num);
1910Sstevel@tonic-gate static int pk11_rand_status(void);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /* These functions are also used in other files */
1947211Sjp161948 PK11_SESSION *pk11_get_session(PK11_OPTYPE optype);
1957211Sjp161948 void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype);
1966847Svk199839 
1977526SVladimir.Kotal@Sun.COM /* active list manipulation functions used in this file */
1987526SVladimir.Kotal@Sun.COM extern int pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type);
1997526SVladimir.Kotal@Sun.COM extern void pk11_free_active_list(PK11_OPTYPE type);
2006847Svk199839 
2016847Svk199839 #ifndef OPENSSL_NO_RSA
2020Sstevel@tonic-gate int pk11_destroy_rsa_key_objects(PK11_SESSION *session);
2036847Svk199839 int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock);
2046847Svk199839 int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock);
2056847Svk199839 #endif
2066847Svk199839 #ifndef OPENSSL_NO_DSA
2070Sstevel@tonic-gate int pk11_destroy_dsa_key_objects(PK11_SESSION *session);
2086847Svk199839 int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock);
2096847Svk199839 int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock);
2106847Svk199839 #endif
2116847Svk199839 #ifndef OPENSSL_NO_DH
2120Sstevel@tonic-gate int pk11_destroy_dh_key_objects(PK11_SESSION *session);
2136847Svk199839 int pk11_destroy_dh_object(PK11_SESSION *session, CK_BBOOL uselock);
2146847Svk199839 #endif
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /* Local helper functions */
2177211Sjp161948 static int pk11_free_all_sessions(void);
2187526SVladimir.Kotal@Sun.COM static int pk11_free_session_list(PK11_OPTYPE optype);
2197211Sjp161948 static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype);
2200Sstevel@tonic-gate static int pk11_destroy_cipher_key_objects(PK11_SESSION *session);
2216847Svk199839 static int pk11_destroy_object(CK_SESSION_HANDLE session,
2220Sstevel@tonic-gate 	CK_OBJECT_HANDLE oh);
2230Sstevel@tonic-gate static const char *get_PK11_LIBNAME(void);
2240Sstevel@tonic-gate static void free_PK11_LIBNAME(void);
2250Sstevel@tonic-gate static long set_PK11_LIBNAME(const char *name);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /* Symmetric cipher and digest support functions */
2280Sstevel@tonic-gate static int cipher_nid_to_pk11(int nid);
2297211Sjp161948 #ifdef	SOLARIS_AES_CTR
2307211Sjp161948 static int pk11_add_NID(char *sn, char *ln);
2317211Sjp161948 static int pk11_add_aes_ctr_NIDs(void);
2327211Sjp161948 #endif	/* SOLARIS_AES_CTR */
2330Sstevel@tonic-gate static int pk11_usable_ciphers(const int **nids);
2340Sstevel@tonic-gate static int pk11_usable_digests(const int **nids);
2350Sstevel@tonic-gate static int pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2360Sstevel@tonic-gate 	const unsigned char *iv, int enc);
2370Sstevel@tonic-gate static int pk11_cipher_final(PK11_SESSION *sp);
2380Sstevel@tonic-gate static int pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2390Sstevel@tonic-gate 	const unsigned char *in, unsigned int inl);
2400Sstevel@tonic-gate static int pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx);
2410Sstevel@tonic-gate static int pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
2420Sstevel@tonic-gate 	const int **nids, int nid);
2430Sstevel@tonic-gate static int pk11_engine_digests(ENGINE *e, const EVP_MD **digest,
2440Sstevel@tonic-gate 	const int **nids, int nid);
2450Sstevel@tonic-gate static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx,
2460Sstevel@tonic-gate 	const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp);
2477211Sjp161948 static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key,
2487211Sjp161948 	int key_len);
2490Sstevel@tonic-gate static int md_nid_to_pk11(int nid);
2500Sstevel@tonic-gate static int pk11_digest_init(EVP_MD_CTX *ctx);
2510Sstevel@tonic-gate static int pk11_digest_update(EVP_MD_CTX *ctx,const void *data,
2522139Sjp161948 	size_t count);
2530Sstevel@tonic-gate static int pk11_digest_final(EVP_MD_CTX *ctx,unsigned char *md);
2540Sstevel@tonic-gate static int pk11_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from);
2550Sstevel@tonic-gate static int pk11_digest_cleanup(EVP_MD_CTX *ctx);
2560Sstevel@tonic-gate 
2577211Sjp161948 static int pk11_choose_slots(int *any_slot_found);
2587211Sjp161948 static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist,
2597211Sjp161948     CK_SLOT_ID current_slot, int *current_slot_n_cipher,
2607211Sjp161948     int *local_cipher_nids);
2617211Sjp161948 static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist,
2627211Sjp161948     CK_SLOT_ID current_slot, int *current_slot_n_digest,
2637211Sjp161948     int *local_digest_nids);
2647211Sjp161948 static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR, int slot_id,
2657211Sjp161948     CK_MECHANISM_TYPE mech, int *current_slot_n_cipher, int *local_cipher_nids,
2667211Sjp161948     int id);
2677211Sjp161948 static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id,
2687211Sjp161948     CK_MECHANISM_TYPE mech, int *current_slot_n_digest, int *local_digest_nids,
2697211Sjp161948     int id);
2707211Sjp161948 
2717526SVladimir.Kotal@Sun.COM static int pk11_init_all_locks(void);
2727526SVladimir.Kotal@Sun.COM static void pk11_free_all_locks(void);
2737526SVladimir.Kotal@Sun.COM 
2747211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
2757211Sjp161948 static int check_hw_mechanisms(void);
2767211Sjp161948 static int nid_in_table(int nid, int *nid_table);
2777211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /* Index for the supported ciphers */
2807211Sjp161948 enum pk11_cipher_id {
2817211Sjp161948 	PK11_DES_CBC,
2827211Sjp161948 	PK11_DES3_CBC,
2837211Sjp161948 	PK11_DES_ECB,
2847211Sjp161948 	PK11_DES3_ECB,
2857211Sjp161948 	PK11_RC4,
2867211Sjp161948 	PK11_AES_128_CBC,
2877211Sjp161948 	PK11_AES_192_CBC,
2887211Sjp161948 	PK11_AES_256_CBC,
2897211Sjp161948 	PK11_AES_128_ECB,
2907211Sjp161948 	PK11_AES_192_ECB,
2917211Sjp161948 	PK11_AES_256_ECB,
2927211Sjp161948 	PK11_BLOWFISH_CBC,
2937211Sjp161948 #ifdef	SOLARIS_AES_CTR
2947211Sjp161948 	PK11_AES_128_CTR,
2957211Sjp161948 	PK11_AES_192_CTR,
2967211Sjp161948 	PK11_AES_256_CTR,
2977211Sjp161948 #endif	/* SOLARIS_AES_CTR */
2987211Sjp161948 	PK11_CIPHER_MAX
2997211Sjp161948 };
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /* Index for the supported digests */
3027211Sjp161948 enum pk11_digest_id {
3037211Sjp161948 	PK11_MD5,
3047211Sjp161948 	PK11_SHA1,
3057211Sjp161948 	PK11_SHA224,
3067211Sjp161948 	PK11_SHA256,
3077211Sjp161948 	PK11_SHA384,
3087211Sjp161948 	PK11_SHA512,
3097211Sjp161948 	PK11_DIGEST_MAX
3107211Sjp161948 };
3110Sstevel@tonic-gate 
3127526SVladimir.Kotal@Sun.COM #define	TRY_OBJ_DESTROY(sess_hdl, obj_hdl, retval, uselock, alg_type)	\
3136847Svk199839 	{								\
3146847Svk199839 	if (uselock)							\
3157526SVladimir.Kotal@Sun.COM 		LOCK_OBJSTORE(alg_type);				\
3167526SVladimir.Kotal@Sun.COM 	if (pk11_active_delete(obj_hdl, alg_type) == 1)			\
3176847Svk199839 		{							\
3186847Svk199839 		retval = pk11_destroy_object(sess_hdl, obj_hdl);	\
3196847Svk199839 		}							\
3206847Svk199839 	if (uselock)							\
3217526SVladimir.Kotal@Sun.COM 		UNLOCK_OBJSTORE(alg_type);				\
3226847Svk199839 	}
3236847Svk199839 
3240Sstevel@tonic-gate static int cipher_nids[PK11_CIPHER_MAX];
3250Sstevel@tonic-gate static int digest_nids[PK11_DIGEST_MAX];
3260Sstevel@tonic-gate static int cipher_count		= 0;
3270Sstevel@tonic-gate static int digest_count		= 0;
3280Sstevel@tonic-gate static CK_BBOOL pk11_have_rsa	= CK_FALSE;
3290Sstevel@tonic-gate static CK_BBOOL pk11_have_dsa	= CK_FALSE;
3300Sstevel@tonic-gate static CK_BBOOL pk11_have_dh	= CK_FALSE;
3310Sstevel@tonic-gate static CK_BBOOL pk11_have_random = CK_FALSE;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate typedef struct PK11_CIPHER_st
3340Sstevel@tonic-gate 	{
3357211Sjp161948 	enum pk11_cipher_id	id;
3364320Sjp161948 	int			nid;
3377211Sjp161948 	int			iv_len;
3384320Sjp161948 	int			key_len;
3394320Sjp161948 	CK_KEY_TYPE		key_type;
3404320Sjp161948 	CK_MECHANISM_TYPE	mech_type;
3410Sstevel@tonic-gate 	} PK11_CIPHER;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static PK11_CIPHER ciphers[] =
3440Sstevel@tonic-gate 	{
3457211Sjp161948 	{PK11_DES_CBC,	   NID_des_cbc,      8,  8, CKK_DES,      CKM_DES_CBC, },
3467211Sjp161948 	{PK11_DES3_CBC,	   NID_des_ede3_cbc, 8, 24, CKK_DES3,     CKM_DES3_CBC, },
3477211Sjp161948 	{PK11_DES_ECB,	   NID_des_ecb,      0,  8, CKK_DES,      CKM_DES_ECB, },
3487211Sjp161948 	{PK11_DES3_ECB,	   NID_des_ede3_ecb, 0, 24, CKK_DES3,     CKM_DES3_ECB, },
3497211Sjp161948 	{PK11_RC4,	   NID_rc4,          0, 16, CKK_RC4,      CKM_RC4, },
3507211Sjp161948 	{PK11_AES_128_CBC, NID_aes_128_cbc, 16, 16, CKK_AES,      CKM_AES_CBC, },
3517211Sjp161948 	{PK11_AES_192_CBC, NID_aes_192_cbc, 16, 24, CKK_AES,      CKM_AES_CBC, },
3527211Sjp161948 	{PK11_AES_256_CBC, NID_aes_256_cbc, 16, 32, CKK_AES,      CKM_AES_CBC, },
3537211Sjp161948 	{PK11_AES_128_ECB, NID_aes_128_ecb,  0, 16, CKK_AES,      CKM_AES_ECB, },
3547211Sjp161948 	{PK11_AES_192_ECB, NID_aes_192_ecb,  0, 24, CKK_AES,      CKM_AES_ECB, },
3557211Sjp161948 	{PK11_AES_256_ECB, NID_aes_256_ecb,  0, 32, CKK_AES,      CKM_AES_ECB, },
3567211Sjp161948 	{PK11_BLOWFISH_CBC,NID_bf_cbc,       8, 16, CKK_BLOWFISH, CKM_BLOWFISH_CBC,},
3577211Sjp161948 #ifdef	SOLARIS_AES_CTR
3587211Sjp161948 	/* we don't know the correct NIDs until the engine is initialized */
3597211Sjp161948 	{PK11_AES_128_CTR, NID_undef,	    16, 16, CKK_AES,      CKM_AES_CTR, },
3607211Sjp161948 	{PK11_AES_192_CTR, NID_undef,	    16, 24, CKK_AES,      CKM_AES_CTR, },
3617211Sjp161948 	{PK11_AES_256_CTR, NID_undef,	    16, 32, CKK_AES,      CKM_AES_CTR, },
3627211Sjp161948 #endif	/* SOLARIS_AES_CTR */
3630Sstevel@tonic-gate 	};
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate typedef struct PK11_DIGEST_st
3660Sstevel@tonic-gate 	{
3677211Sjp161948 	enum pk11_digest_id	id;
3684320Sjp161948 	int			nid;
3694320Sjp161948 	CK_MECHANISM_TYPE	mech_type;
3700Sstevel@tonic-gate 	} PK11_DIGEST;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static PK11_DIGEST digests[] =
3730Sstevel@tonic-gate 	{
3744320Sjp161948 	{PK11_MD5,	NID_md5,	CKM_MD5, },
3754320Sjp161948 	{PK11_SHA1,	NID_sha1,	CKM_SHA_1, },
3767211Sjp161948 	{PK11_SHA224,	NID_sha224,	CKM_SHA224, },
3777211Sjp161948 	{PK11_SHA256,	NID_sha256,	CKM_SHA256, },
3787211Sjp161948 	{PK11_SHA384,	NID_sha384,	CKM_SHA384, },
3797211Sjp161948 	{PK11_SHA512,	NID_sha512,	CKM_SHA512, },
3804320Sjp161948 	{0,		NID_undef,	0xFFFF, },
3810Sstevel@tonic-gate 	};
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /* Structure to be used for the cipher_data/md_data in
3840Sstevel@tonic-gate  * EVP_CIPHER_CTX/EVP_MD_CTX structures in order to use the same
3850Sstevel@tonic-gate  * pk11 session in multiple cipher_update calls
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate typedef struct PK11_CIPHER_STATE_st
3880Sstevel@tonic-gate 	{
3890Sstevel@tonic-gate 	PK11_SESSION	*sp;
3900Sstevel@tonic-gate 	} PK11_CIPHER_STATE;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 
3937211Sjp161948 /*
3947211Sjp161948  * libcrypto EVP stuff - this is how we get wired to EVP so the engine gets
3957211Sjp161948  * called when libcrypto requests a cipher NID.
3967211Sjp161948  *
3970Sstevel@tonic-gate  * Note how the PK11_CIPHER_STATE is used here.
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /* DES CBC EVP */
4010Sstevel@tonic-gate static const EVP_CIPHER pk11_des_cbc =
4020Sstevel@tonic-gate 	{
4030Sstevel@tonic-gate 	NID_des_cbc,
4040Sstevel@tonic-gate 	8, 8, 8,
4050Sstevel@tonic-gate 	EVP_CIPH_CBC_MODE,
4060Sstevel@tonic-gate 	pk11_cipher_init,
4070Sstevel@tonic-gate 	pk11_cipher_do_cipher,
4080Sstevel@tonic-gate 	pk11_cipher_cleanup,
4090Sstevel@tonic-gate 	sizeof(PK11_CIPHER_STATE),
4100Sstevel@tonic-gate 	EVP_CIPHER_set_asn1_iv,
4110Sstevel@tonic-gate 	EVP_CIPHER_get_asn1_iv,
4120Sstevel@tonic-gate 	NULL
4130Sstevel@tonic-gate 	};
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /* 3DES CBC EVP */
4160Sstevel@tonic-gate static const EVP_CIPHER pk11_3des_cbc =
4170Sstevel@tonic-gate 	{
4180Sstevel@tonic-gate 	NID_des_ede3_cbc,
4190Sstevel@tonic-gate 	8, 24, 8,
4200Sstevel@tonic-gate 	EVP_CIPH_CBC_MODE,
4210Sstevel@tonic-gate 	pk11_cipher_init,
4220Sstevel@tonic-gate 	pk11_cipher_do_cipher,
4230Sstevel@tonic-gate 	pk11_cipher_cleanup,
4240Sstevel@tonic-gate 	sizeof(PK11_CIPHER_STATE),
4250Sstevel@tonic-gate 	EVP_CIPHER_set_asn1_iv,
4260Sstevel@tonic-gate 	EVP_CIPHER_get_asn1_iv,
4270Sstevel@tonic-gate 	NULL
4280Sstevel@tonic-gate 	};
4290Sstevel@tonic-gate 
4307211Sjp161948 /*
4317211Sjp161948  * ECB modes don't use an Initial Vector so that's why set_asn1_parameters and
4327211Sjp161948  * get_asn1_parameters fields are set to NULL.
4337211Sjp161948  */
4347211Sjp161948 static const EVP_CIPHER pk11_des_ecb =
4357211Sjp161948 	{
4367211Sjp161948 	NID_des_ecb,
4377211Sjp161948 	8, 8, 8,
4387211Sjp161948 	EVP_CIPH_ECB_MODE,
4397211Sjp161948 	pk11_cipher_init,
4407211Sjp161948 	pk11_cipher_do_cipher,
4417211Sjp161948 	pk11_cipher_cleanup,
4427211Sjp161948 	sizeof(PK11_CIPHER_STATE),
4437211Sjp161948 	NULL,
4447211Sjp161948 	NULL,
4457211Sjp161948 	NULL
4467211Sjp161948 	};
4477211Sjp161948 
4487211Sjp161948 static const EVP_CIPHER pk11_3des_ecb =
4497211Sjp161948 	{
4507211Sjp161948 	NID_des_ede3_ecb,
4517211Sjp161948 	8, 24, 8,
4527211Sjp161948 	EVP_CIPH_ECB_MODE,
4537211Sjp161948 	pk11_cipher_init,
4547211Sjp161948 	pk11_cipher_do_cipher,
4557211Sjp161948 	pk11_cipher_cleanup,
4567211Sjp161948 	sizeof(PK11_CIPHER_STATE),
4577211Sjp161948 	NULL,
4587211Sjp161948 	NULL,
4597211Sjp161948 	NULL
4607211Sjp161948 	};
4617211Sjp161948 
4627211Sjp161948 
4637211Sjp161948 static const EVP_CIPHER pk11_aes_128_cbc =
4640Sstevel@tonic-gate 	{
4650Sstevel@tonic-gate 	NID_aes_128_cbc,
4660Sstevel@tonic-gate 	16, 16, 16,
4670Sstevel@tonic-gate 	EVP_CIPH_CBC_MODE,
4680Sstevel@tonic-gate 	pk11_cipher_init,
4690Sstevel@tonic-gate 	pk11_cipher_do_cipher,
4700Sstevel@tonic-gate 	pk11_cipher_cleanup,
4710Sstevel@tonic-gate 	sizeof(PK11_CIPHER_STATE),
4720Sstevel@tonic-gate 	EVP_CIPHER_set_asn1_iv,
4730Sstevel@tonic-gate 	EVP_CIPHER_get_asn1_iv,
4740Sstevel@tonic-gate 	NULL
4750Sstevel@tonic-gate 	};
4760Sstevel@tonic-gate 
4777211Sjp161948 static const EVP_CIPHER pk11_aes_192_cbc =
4787211Sjp161948 	{
4797211Sjp161948 	NID_aes_192_cbc,
4807211Sjp161948 	16, 24, 16,
4817211Sjp161948 	EVP_CIPH_CBC_MODE,
4827211Sjp161948 	pk11_cipher_init,
4837211Sjp161948 	pk11_cipher_do_cipher,
4847211Sjp161948 	pk11_cipher_cleanup,
4857211Sjp161948 	sizeof(PK11_CIPHER_STATE),
4867211Sjp161948 	EVP_CIPHER_set_asn1_iv,
4877211Sjp161948 	EVP_CIPHER_get_asn1_iv,
4887211Sjp161948 	NULL
4897211Sjp161948 	};
4907211Sjp161948 
4917211Sjp161948 static const EVP_CIPHER pk11_aes_256_cbc =
4927211Sjp161948 	{
4937211Sjp161948 	NID_aes_256_cbc,
4947211Sjp161948 	16, 32, 16,
4957211Sjp161948 	EVP_CIPH_CBC_MODE,
4967211Sjp161948 	pk11_cipher_init,
4977211Sjp161948 	pk11_cipher_do_cipher,
4987211Sjp161948 	pk11_cipher_cleanup,
4997211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5007211Sjp161948 	EVP_CIPHER_set_asn1_iv,
5017211Sjp161948 	EVP_CIPHER_get_asn1_iv,
5027211Sjp161948 	NULL
5037211Sjp161948 	};
5047211Sjp161948 
5057211Sjp161948 /*
5067211Sjp161948  * ECB modes don't use IV so that's why set_asn1_parameters and
5077211Sjp161948  * get_asn1_parameters are set to NULL.
5087211Sjp161948  */
5097211Sjp161948 static const EVP_CIPHER pk11_aes_128_ecb =
5107211Sjp161948 	{
5117211Sjp161948 	NID_aes_128_ecb,
5127211Sjp161948 	16, 16, 0,
5137211Sjp161948 	EVP_CIPH_ECB_MODE,
5147211Sjp161948 	pk11_cipher_init,
5157211Sjp161948 	pk11_cipher_do_cipher,
5167211Sjp161948 	pk11_cipher_cleanup,
5177211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5187211Sjp161948 	NULL,
5197211Sjp161948 	NULL,
5207211Sjp161948 	NULL
5217211Sjp161948 	};
5227211Sjp161948 
5237211Sjp161948 static const EVP_CIPHER pk11_aes_192_ecb =
5247211Sjp161948 	{
5257211Sjp161948 	NID_aes_192_ecb,
5267211Sjp161948 	16, 24, 0,
5277211Sjp161948 	EVP_CIPH_ECB_MODE,
5287211Sjp161948 	pk11_cipher_init,
5297211Sjp161948 	pk11_cipher_do_cipher,
5307211Sjp161948 	pk11_cipher_cleanup,
5317211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5327211Sjp161948 	NULL,
5337211Sjp161948 	NULL,
5347211Sjp161948 	NULL
5357211Sjp161948 	};
5367211Sjp161948 
5377211Sjp161948 static const EVP_CIPHER pk11_aes_256_ecb =
5387211Sjp161948 	{
5397211Sjp161948 	NID_aes_256_ecb,
5407211Sjp161948 	16, 32, 0,
5417211Sjp161948 	EVP_CIPH_ECB_MODE,
5427211Sjp161948 	pk11_cipher_init,
5437211Sjp161948 	pk11_cipher_do_cipher,
5447211Sjp161948 	pk11_cipher_cleanup,
5457211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5467211Sjp161948 	NULL,
5477211Sjp161948 	NULL,
5487211Sjp161948 	NULL
5497211Sjp161948 	};
5507211Sjp161948 
5517211Sjp161948 #ifdef	SOLARIS_AES_CTR
5527211Sjp161948 /*
5537211Sjp161948  * NID_undef's will be changed to the AES counter mode NIDs as soon they are
5547211Sjp161948  * created in pk11_library_init(). Note that the need to change these structures
5557211Sjp161948  * is the reason why we don't define them with the const keyword.
5567211Sjp161948  */
5577211Sjp161948 static EVP_CIPHER pk11_aes_128_ctr =
5587211Sjp161948 	{
5597211Sjp161948 	NID_undef,
5607211Sjp161948 	16, 16, 16,
5617211Sjp161948 	EVP_CIPH_CBC_MODE,
5627211Sjp161948 	pk11_cipher_init,
5637211Sjp161948 	pk11_cipher_do_cipher,
5647211Sjp161948 	pk11_cipher_cleanup,
5657211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5667211Sjp161948 	EVP_CIPHER_set_asn1_iv,
5677211Sjp161948 	EVP_CIPHER_get_asn1_iv,
5687211Sjp161948 	NULL
5697211Sjp161948 	};
5707211Sjp161948 
5717211Sjp161948 static EVP_CIPHER pk11_aes_192_ctr =
5727211Sjp161948 	{
5737211Sjp161948 	NID_undef,
5747211Sjp161948 	16, 24, 16,
5757211Sjp161948 	EVP_CIPH_CBC_MODE,
5767211Sjp161948 	pk11_cipher_init,
5777211Sjp161948 	pk11_cipher_do_cipher,
5787211Sjp161948 	pk11_cipher_cleanup,
5797211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5807211Sjp161948 	EVP_CIPHER_set_asn1_iv,
5817211Sjp161948 	EVP_CIPHER_get_asn1_iv,
5827211Sjp161948 	NULL
5837211Sjp161948 	};
5847211Sjp161948 
5857211Sjp161948 static EVP_CIPHER pk11_aes_256_ctr =
5867211Sjp161948 	{
5877211Sjp161948 	NID_undef,
5887211Sjp161948 	16, 32, 16,
5897211Sjp161948 	EVP_CIPH_CBC_MODE,
5907211Sjp161948 	pk11_cipher_init,
5917211Sjp161948 	pk11_cipher_do_cipher,
5927211Sjp161948 	pk11_cipher_cleanup,
5937211Sjp161948 	sizeof(PK11_CIPHER_STATE),
5947211Sjp161948 	EVP_CIPHER_set_asn1_iv,
5957211Sjp161948 	EVP_CIPHER_get_asn1_iv,
5967211Sjp161948 	NULL
5977211Sjp161948 	};
5987211Sjp161948 #endif	/* SOLARIS_AES_CTR */
5997211Sjp161948 
6007211Sjp161948 static const EVP_CIPHER pk11_bf_cbc =
6017211Sjp161948 	{
6027211Sjp161948 	NID_bf_cbc,
6037211Sjp161948 	8, 16, 8,
6047211Sjp161948 	EVP_CIPH_VARIABLE_LENGTH,
6057211Sjp161948 	pk11_cipher_init,
6067211Sjp161948 	pk11_cipher_do_cipher,
6077211Sjp161948 	pk11_cipher_cleanup,
6087211Sjp161948 	sizeof(PK11_CIPHER_STATE),
6097211Sjp161948 	EVP_CIPHER_set_asn1_iv,
6107211Sjp161948 	EVP_CIPHER_get_asn1_iv,
6117211Sjp161948 	NULL
6127211Sjp161948 	};
6137211Sjp161948 
6140Sstevel@tonic-gate static const EVP_CIPHER pk11_rc4 =
6150Sstevel@tonic-gate 	{
6160Sstevel@tonic-gate 	NID_rc4,
6177211Sjp161948 	1, 16, 0,
6180Sstevel@tonic-gate 	EVP_CIPH_VARIABLE_LENGTH,
6190Sstevel@tonic-gate 	pk11_cipher_init,
6200Sstevel@tonic-gate 	pk11_cipher_do_cipher,
6210Sstevel@tonic-gate 	pk11_cipher_cleanup,
6220Sstevel@tonic-gate 	sizeof(PK11_CIPHER_STATE),
6230Sstevel@tonic-gate 	NULL,
6240Sstevel@tonic-gate 	NULL,
6250Sstevel@tonic-gate 	NULL
6260Sstevel@tonic-gate 	};
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate static const EVP_MD pk11_md5 =
6290Sstevel@tonic-gate 	{
6300Sstevel@tonic-gate 	NID_md5,
6310Sstevel@tonic-gate 	NID_md5WithRSAEncryption,
6320Sstevel@tonic-gate 	MD5_DIGEST_LENGTH,
6330Sstevel@tonic-gate 	0,
6340Sstevel@tonic-gate 	pk11_digest_init,
6350Sstevel@tonic-gate 	pk11_digest_update,
6360Sstevel@tonic-gate 	pk11_digest_final,
6370Sstevel@tonic-gate 	pk11_digest_copy,
6380Sstevel@tonic-gate 	pk11_digest_cleanup,
6390Sstevel@tonic-gate 	EVP_PKEY_RSA_method,
6400Sstevel@tonic-gate 	MD5_CBLOCK,
6410Sstevel@tonic-gate 	sizeof(PK11_CIPHER_STATE),
6420Sstevel@tonic-gate 	};
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate static const EVP_MD pk11_sha1 =
6450Sstevel@tonic-gate 	{
6460Sstevel@tonic-gate 	NID_sha1,
6470Sstevel@tonic-gate 	NID_sha1WithRSAEncryption,
6480Sstevel@tonic-gate 	SHA_DIGEST_LENGTH,
6490Sstevel@tonic-gate 	0,
6500Sstevel@tonic-gate 	pk11_digest_init,
6510Sstevel@tonic-gate 	pk11_digest_update,
6520Sstevel@tonic-gate 	pk11_digest_final,
6530Sstevel@tonic-gate 	pk11_digest_copy,
6540Sstevel@tonic-gate 	pk11_digest_cleanup,
6550Sstevel@tonic-gate 	EVP_PKEY_RSA_method,
6560Sstevel@tonic-gate 	SHA_CBLOCK,
6570Sstevel@tonic-gate 	sizeof(PK11_CIPHER_STATE),
6580Sstevel@tonic-gate 	};
6590Sstevel@tonic-gate 
6607211Sjp161948 static const EVP_MD pk11_sha224 =
6617211Sjp161948 	{
6627211Sjp161948 	NID_sha224,
6637211Sjp161948 	NID_sha224WithRSAEncryption,
6647211Sjp161948 	SHA224_DIGEST_LENGTH,
6657211Sjp161948 	0,
6667211Sjp161948 	pk11_digest_init,
6677211Sjp161948 	pk11_digest_update,
6687211Sjp161948 	pk11_digest_final,
6697211Sjp161948 	pk11_digest_copy,
6707211Sjp161948 	pk11_digest_cleanup,
6717211Sjp161948 	EVP_PKEY_RSA_method,
6727211Sjp161948 	/* SHA-224 uses the same cblock size as SHA-256 */
6737211Sjp161948 	SHA256_CBLOCK,
6747211Sjp161948 	sizeof(PK11_CIPHER_STATE),
6757211Sjp161948 	};
6767211Sjp161948 
6777211Sjp161948 static const EVP_MD pk11_sha256 =
6787211Sjp161948 	{
6797211Sjp161948 	NID_sha256,
6807211Sjp161948 	NID_sha256WithRSAEncryption,
6817211Sjp161948 	SHA256_DIGEST_LENGTH,
6827211Sjp161948 	0,
6837211Sjp161948 	pk11_digest_init,
6847211Sjp161948 	pk11_digest_update,
6857211Sjp161948 	pk11_digest_final,
6867211Sjp161948 	pk11_digest_copy,
6877211Sjp161948 	pk11_digest_cleanup,
6887211Sjp161948 	EVP_PKEY_RSA_method,
6897211Sjp161948 	SHA256_CBLOCK,
6907211Sjp161948 	sizeof(PK11_CIPHER_STATE),
6917211Sjp161948 	};
6927211Sjp161948 
6937211Sjp161948 static const EVP_MD pk11_sha384 =
6947211Sjp161948 	{
6957211Sjp161948 	NID_sha384,
6967211Sjp161948 	NID_sha384WithRSAEncryption,
6977211Sjp161948 	SHA384_DIGEST_LENGTH,
6987211Sjp161948 	0,
6997211Sjp161948 	pk11_digest_init,
7007211Sjp161948 	pk11_digest_update,
7017211Sjp161948 	pk11_digest_final,
7027211Sjp161948 	pk11_digest_copy,
7037211Sjp161948 	pk11_digest_cleanup,
7047211Sjp161948 	EVP_PKEY_RSA_method,
7057211Sjp161948 	/* SHA-384 uses the same cblock size as SHA-512 */
7067211Sjp161948 	SHA512_CBLOCK,
7077211Sjp161948 	sizeof(PK11_CIPHER_STATE),
7087211Sjp161948 	};
7097211Sjp161948 
7107211Sjp161948 static const EVP_MD pk11_sha512 =
7117211Sjp161948 	{
7127211Sjp161948 	NID_sha512,
7137211Sjp161948 	NID_sha512WithRSAEncryption,
7147211Sjp161948 	SHA512_DIGEST_LENGTH,
7157211Sjp161948 	0,
7167211Sjp161948 	pk11_digest_init,
7177211Sjp161948 	pk11_digest_update,
7187211Sjp161948 	pk11_digest_final,
7197211Sjp161948 	pk11_digest_copy,
7207211Sjp161948 	pk11_digest_cleanup,
7217211Sjp161948 	EVP_PKEY_RSA_method,
7227211Sjp161948 	SHA512_CBLOCK,
7237211Sjp161948 	sizeof(PK11_CIPHER_STATE),
7247211Sjp161948 	};
7257211Sjp161948 
7260Sstevel@tonic-gate /* Initialization function. Sets up various pk11 library components.
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate /* The definitions for control commands specific to this engine
7290Sstevel@tonic-gate  */
7300Sstevel@tonic-gate #define PK11_CMD_SO_PATH		ENGINE_CMD_BASE
7310Sstevel@tonic-gate static const ENGINE_CMD_DEFN pk11_cmd_defns[] =
7320Sstevel@tonic-gate 	{
7330Sstevel@tonic-gate 		{
7340Sstevel@tonic-gate 		PK11_CMD_SO_PATH,
7350Sstevel@tonic-gate 		"SO_PATH",
7360Sstevel@tonic-gate 		"Specifies the path to the 'pkcs#11' shared library",
7370Sstevel@tonic-gate 		ENGINE_CMD_FLAG_STRING
7380Sstevel@tonic-gate 		},
7390Sstevel@tonic-gate 		{0, NULL, NULL, 0}
7400Sstevel@tonic-gate 	};
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate static RAND_METHOD pk11_random =
7440Sstevel@tonic-gate 	{
7450Sstevel@tonic-gate 	pk11_rand_seed,
7460Sstevel@tonic-gate 	pk11_rand_bytes,
7470Sstevel@tonic-gate 	pk11_rand_cleanup,
7480Sstevel@tonic-gate 	pk11_rand_add,
7490Sstevel@tonic-gate 	pk11_rand_bytes,
7500Sstevel@tonic-gate 	pk11_rand_status
7510Sstevel@tonic-gate 	};
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /* Constants used when creating the ENGINE
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate static const char *engine_pk11_id = "pkcs11";
7570Sstevel@tonic-gate static const char *engine_pk11_name = "PKCS #11 engine support";
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate CK_FUNCTION_LIST_PTR pFuncList = NULL;
7600Sstevel@tonic-gate static const char PK11_GET_FUNCTION_LIST[] = "C_GetFunctionList";
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate /* These are the static string constants for the DSO file name and the function
7637211Sjp161948  * symbol names to bind to.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate #if defined(__sparcv9) || defined(__x86_64) || defined(__amd64)
7660Sstevel@tonic-gate static const char def_PK11_LIBNAME[] = "/usr/lib/64/libpkcs11.so.1";
7670Sstevel@tonic-gate #else
7680Sstevel@tonic-gate static const char def_PK11_LIBNAME[] = "/usr/lib/libpkcs11.so.1";
7690Sstevel@tonic-gate #endif
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate static CK_BBOOL true = TRUE;
7720Sstevel@tonic-gate static CK_BBOOL false = FALSE;
7737211Sjp161948 static CK_SLOT_ID pubkey_SLOTID = 0;
7747211Sjp161948 static CK_SLOT_ID rand_SLOTID = 0;
7750Sstevel@tonic-gate static CK_SLOT_ID SLOTID = 0;
7767526SVladimir.Kotal@Sun.COM static CK_BBOOL pk11_library_initialized = FALSE;
7777526SVladimir.Kotal@Sun.COM static CK_BBOOL pk11_atfork_initialized = FALSE;
7787211Sjp161948 static int pk11_pid = 0;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate static DSO *pk11_dso = NULL;
7810Sstevel@tonic-gate 
7827526SVladimir.Kotal@Sun.COM /* allocate and initialize all locks used by the engine itself */
7837526SVladimir.Kotal@Sun.COM static int pk11_init_all_locks(void)
7847526SVladimir.Kotal@Sun.COM 	{
7857526SVladimir.Kotal@Sun.COM 	int type;
7867526SVladimir.Kotal@Sun.COM 
7877526SVladimir.Kotal@Sun.COM #ifndef OPENSSL_NO_RSA
7887526SVladimir.Kotal@Sun.COM 	find_lock[OP_RSA] = OPENSSL_malloc(sizeof (pthread_mutex_t));
7897526SVladimir.Kotal@Sun.COM 	if (find_lock[OP_RSA] == NULL)
7907526SVladimir.Kotal@Sun.COM 		goto malloc_err;
7917526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_init(find_lock[OP_RSA], NULL);
7927526SVladimir.Kotal@Sun.COM #endif /* OPENSSL_NO_RSA */
7937526SVladimir.Kotal@Sun.COM 
7947526SVladimir.Kotal@Sun.COM #ifndef OPENSSL_NO_DSA
7957526SVladimir.Kotal@Sun.COM 	find_lock[OP_DSA] = OPENSSL_malloc(sizeof (pthread_mutex_t));
7967526SVladimir.Kotal@Sun.COM 	if (find_lock[OP_DSA] == NULL)
7977526SVladimir.Kotal@Sun.COM 		goto malloc_err;
7987526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_init(find_lock[OP_DSA], NULL);
7997526SVladimir.Kotal@Sun.COM #endif /* OPENSSL_NO_DSA */
8007526SVladimir.Kotal@Sun.COM 
8017526SVladimir.Kotal@Sun.COM #ifndef OPENSSL_NO_DH
8027526SVladimir.Kotal@Sun.COM 	find_lock[OP_DH] = OPENSSL_malloc(sizeof (pthread_mutex_t));
8037526SVladimir.Kotal@Sun.COM 	if (find_lock[OP_DH] == NULL)
8047526SVladimir.Kotal@Sun.COM 		goto malloc_err;
8057526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_init(find_lock[OP_DH], NULL);
8067526SVladimir.Kotal@Sun.COM #endif /* OPENSSL_NO_DH */
8077526SVladimir.Kotal@Sun.COM 
8087526SVladimir.Kotal@Sun.COM 	for (type = 0; type < OP_MAX; type++)
8097526SVladimir.Kotal@Sun.COM 		{
8107526SVladimir.Kotal@Sun.COM 		session_cache[type].lock =
8117526SVladimir.Kotal@Sun.COM 		    OPENSSL_malloc(sizeof (pthread_mutex_t));
8127526SVladimir.Kotal@Sun.COM 		if (session_cache[type].lock == NULL)
8137526SVladimir.Kotal@Sun.COM 			goto malloc_err;
8147526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_init(session_cache[type].lock, NULL);
8157526SVladimir.Kotal@Sun.COM 		}
8167526SVladimir.Kotal@Sun.COM 
8177526SVladimir.Kotal@Sun.COM 	return (1);
8187526SVladimir.Kotal@Sun.COM 
8197526SVladimir.Kotal@Sun.COM malloc_err:
8207526SVladimir.Kotal@Sun.COM 	pk11_free_all_locks();
8217526SVladimir.Kotal@Sun.COM 	PK11err(PK11_F_INIT_ALL_LOCKS, PK11_R_MALLOC_FAILURE);
8227526SVladimir.Kotal@Sun.COM 	return (0);
8237526SVladimir.Kotal@Sun.COM 	}
8247526SVladimir.Kotal@Sun.COM 
8257526SVladimir.Kotal@Sun.COM static void pk11_free_all_locks(void)
8267526SVladimir.Kotal@Sun.COM 	{
8277526SVladimir.Kotal@Sun.COM 	int type;
8287526SVladimir.Kotal@Sun.COM 
8297526SVladimir.Kotal@Sun.COM #ifndef OPENSSL_NO_RSA
8307526SVladimir.Kotal@Sun.COM 	if (find_lock[OP_RSA] != NULL)
8317526SVladimir.Kotal@Sun.COM 		{
8327526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_destroy(find_lock[OP_RSA]);
8337526SVladimir.Kotal@Sun.COM 		OPENSSL_free(find_lock[OP_RSA]);
8347526SVladimir.Kotal@Sun.COM 		find_lock[OP_RSA] = NULL;
8357526SVladimir.Kotal@Sun.COM 		}
8367526SVladimir.Kotal@Sun.COM #endif /* OPENSSL_NO_RSA */
8377526SVladimir.Kotal@Sun.COM #ifndef OPENSSL_NO_DSA
8387526SVladimir.Kotal@Sun.COM 	if (find_lock[OP_DSA] != NULL)
8397526SVladimir.Kotal@Sun.COM 		{
8407526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_destroy(find_lock[OP_DSA]);
8417526SVladimir.Kotal@Sun.COM 		OPENSSL_free(find_lock[OP_DSA]);
8427526SVladimir.Kotal@Sun.COM 		find_lock[OP_DSA] = NULL;
8437526SVladimir.Kotal@Sun.COM 		}
8447526SVladimir.Kotal@Sun.COM #endif /* OPENSSL_NO_DSA */
8457526SVladimir.Kotal@Sun.COM #ifndef OPENSSL_NO_DH
8467526SVladimir.Kotal@Sun.COM 	if (find_lock[OP_DH] != NULL)
8477526SVladimir.Kotal@Sun.COM 		{
8487526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_destroy(find_lock[OP_DH]);
8497526SVladimir.Kotal@Sun.COM 		OPENSSL_free(find_lock[OP_DH]);
8507526SVladimir.Kotal@Sun.COM 		find_lock[OP_DH] = NULL;
8517526SVladimir.Kotal@Sun.COM 		}
8527526SVladimir.Kotal@Sun.COM #endif /* OPENSSL_NO_DH */
8537526SVladimir.Kotal@Sun.COM 
8547526SVladimir.Kotal@Sun.COM 	for (type = 0; type < OP_MAX; type++)
8557526SVladimir.Kotal@Sun.COM 		{
8567526SVladimir.Kotal@Sun.COM 		if (session_cache[type].lock != NULL)
8577526SVladimir.Kotal@Sun.COM 			{
8587526SVladimir.Kotal@Sun.COM 			(void) pthread_mutex_destroy(session_cache[type].lock);
8597526SVladimir.Kotal@Sun.COM 			OPENSSL_free(session_cache[type].lock);
8607526SVladimir.Kotal@Sun.COM 			session_cache[type].lock = NULL;
8617526SVladimir.Kotal@Sun.COM 			}
8627526SVladimir.Kotal@Sun.COM 		}
8637526SVladimir.Kotal@Sun.COM 	}
8647526SVladimir.Kotal@Sun.COM 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * This internal function is used by ENGINE_pk11() and "dynamic" ENGINE support.
8670Sstevel@tonic-gate  */
8680Sstevel@tonic-gate static int bind_pk11(ENGINE *e)
8690Sstevel@tonic-gate 	{
8706847Svk199839 #ifndef OPENSSL_NO_RSA
8710Sstevel@tonic-gate 	const RSA_METHOD *rsa = NULL;
8720Sstevel@tonic-gate 	RSA_METHOD *pk11_rsa = PK11_RSA();
8737211Sjp161948 #endif	/* OPENSSL_NO_RSA */
8740Sstevel@tonic-gate 	if (!pk11_library_initialized)
8757526SVladimir.Kotal@Sun.COM 		(void) pk11_library_init(e);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	if(!ENGINE_set_id(e, engine_pk11_id) ||
8780Sstevel@tonic-gate 	   !ENGINE_set_name(e, engine_pk11_name) ||
8790Sstevel@tonic-gate 	   !ENGINE_set_ciphers(e, pk11_engine_ciphers) ||
8800Sstevel@tonic-gate 	   !ENGINE_set_digests(e, pk11_engine_digests))
8810Sstevel@tonic-gate 	   	return 0;
8820Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
8830Sstevel@tonic-gate 	if(pk11_have_rsa == CK_TRUE)
8840Sstevel@tonic-gate 		{
8850Sstevel@tonic-gate 		if(!ENGINE_set_RSA(e, PK11_RSA()) ||
8860Sstevel@tonic-gate 	           !ENGINE_set_load_privkey_function(e, pk11_load_privkey) ||
8870Sstevel@tonic-gate 	           !ENGINE_set_load_pubkey_function(e, pk11_load_pubkey))
8880Sstevel@tonic-gate 			return 0;
8897211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
8907211Sjp161948 		fprintf(stderr, "%s: registered RSA\n", PK11_DBG);
8917211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
8920Sstevel@tonic-gate 		}
8937211Sjp161948 #endif	/* OPENSSL_NO_RSA */
8940Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
8950Sstevel@tonic-gate 	if(pk11_have_dsa == CK_TRUE)
8960Sstevel@tonic-gate 		{
8970Sstevel@tonic-gate 	  	if (!ENGINE_set_DSA(e, PK11_DSA()))
8980Sstevel@tonic-gate 			return 0;
8997211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
9007211Sjp161948 		fprintf(stderr, "%s: registered DSA\n", PK11_DBG);
9017211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
9020Sstevel@tonic-gate 	    	}
9037211Sjp161948 #endif	/* OPENSSL_NO_DSA */
9040Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
9050Sstevel@tonic-gate 	if(pk11_have_dh == CK_TRUE)
9060Sstevel@tonic-gate 		{
9070Sstevel@tonic-gate 	  	if (!ENGINE_set_DH(e, PK11_DH()))
9080Sstevel@tonic-gate 			return 0;
9097211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
9107211Sjp161948 		fprintf(stderr, "%s: registered DH\n", PK11_DBG);
9117211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
9120Sstevel@tonic-gate 	    	}
9137211Sjp161948 #endif	/* OPENSSL_NO_DH */
9140Sstevel@tonic-gate 	if(pk11_have_random)
9150Sstevel@tonic-gate 		{
9160Sstevel@tonic-gate 		if(!ENGINE_set_RAND(e, &pk11_random))
9170Sstevel@tonic-gate 			return 0;
9187211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
9197211Sjp161948 		fprintf(stderr, "%s: registered random\n", PK11_DBG);
9207211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
9210Sstevel@tonic-gate 		}
9220Sstevel@tonic-gate 	if(!ENGINE_set_init_function(e, pk11_init) ||
9230Sstevel@tonic-gate 	   !ENGINE_set_destroy_function(e, pk11_destroy) ||
9240Sstevel@tonic-gate 	   !ENGINE_set_finish_function(e, pk11_finish) ||
9250Sstevel@tonic-gate 	   !ENGINE_set_ctrl_function(e, pk11_ctrl) ||
9260Sstevel@tonic-gate 	   !ENGINE_set_cmd_defns(e, pk11_cmd_defns))
9270Sstevel@tonic-gate 		return 0;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /* Apache calls OpenSSL function RSA_blinding_on() once during startup
9300Sstevel@tonic-gate  * which in turn calls bn_mod_exp. Since we do not implement bn_mod_exp
9310Sstevel@tonic-gate  * here, we wire it back to the OpenSSL software implementation.
9320Sstevel@tonic-gate  * Since it is used only once, performance is not a concern. */
9330Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
9340Sstevel@tonic-gate         rsa = RSA_PKCS1_SSLeay();
9350Sstevel@tonic-gate         pk11_rsa->rsa_mod_exp = rsa->rsa_mod_exp;
9360Sstevel@tonic-gate         pk11_rsa->bn_mod_exp = rsa->bn_mod_exp;
9377211Sjp161948 #endif	/* OPENSSL_NO_RSA */
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	/* Ensure the pk11 error handling is set up */
9400Sstevel@tonic-gate 	ERR_load_pk11_strings();
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	return 1;
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate /* Dynamic engine support is disabled at a higher level for Solaris
9460Sstevel@tonic-gate  */
9477211Sjp161948 #ifdef	ENGINE_DYNAMIC_SUPPORT
9480Sstevel@tonic-gate static int bind_helper(ENGINE *e, const char *id)
9490Sstevel@tonic-gate 	{
9500Sstevel@tonic-gate 	if (id && (strcmp(id, engine_pk11_id) != 0))
9510Sstevel@tonic-gate 		return 0;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	if (!bind_pk11(e))
9540Sstevel@tonic-gate 		return 0;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	return 1;
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate IMPLEMENT_DYNAMIC_CHECK_FN()
9600Sstevel@tonic-gate IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate #else
9630Sstevel@tonic-gate static ENGINE *engine_pk11(void)
9640Sstevel@tonic-gate 	{
9650Sstevel@tonic-gate 	ENGINE *ret = ENGINE_new();
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	if (!ret)
9680Sstevel@tonic-gate 		return NULL;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	if (!bind_pk11(ret))
9710Sstevel@tonic-gate 		{
9720Sstevel@tonic-gate 		ENGINE_free(ret);
9730Sstevel@tonic-gate 		return NULL;
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	return ret;
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate void ENGINE_load_pk11(void)
9800Sstevel@tonic-gate 	{
9810Sstevel@tonic-gate 	ENGINE *e_pk11 = NULL;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/* Do not use dynamic PKCS#11 library on Solaris due to
9840Sstevel@tonic-gate 	 * security reasons. We will link it in statically
9850Sstevel@tonic-gate 	 */
9860Sstevel@tonic-gate 	/* Attempt to load PKCS#11 library
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	if (!pk11_dso)
9890Sstevel@tonic-gate 		pk11_dso = DSO_load(NULL, get_PK11_LIBNAME(), NULL, 0);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	if (pk11_dso == NULL)
9920Sstevel@tonic-gate 		{
9930Sstevel@tonic-gate 		PK11err(PK11_F_LOAD, PK11_R_DSO_FAILURE);
9940Sstevel@tonic-gate 		return;
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	e_pk11 = engine_pk11();
9980Sstevel@tonic-gate 	if (!e_pk11)
9990Sstevel@tonic-gate 		{
10000Sstevel@tonic-gate 		DSO_free(pk11_dso);
10010Sstevel@tonic-gate 		pk11_dso = NULL;
10020Sstevel@tonic-gate 		return;
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/* At this point, the pk11 shared library is either dynamically
10060Sstevel@tonic-gate 	 * loaded or statically linked in. So, initialize the pk11
10070Sstevel@tonic-gate 	 * library before calling ENGINE_set_default since the latter
10080Sstevel@tonic-gate 	 * needs cipher and digest algorithm information
10090Sstevel@tonic-gate 	 */
10100Sstevel@tonic-gate 	if (!pk11_library_init(e_pk11))
10110Sstevel@tonic-gate 		{
10120Sstevel@tonic-gate 		DSO_free(pk11_dso);
10130Sstevel@tonic-gate 		pk11_dso = NULL;
10140Sstevel@tonic-gate 		ENGINE_free(e_pk11);
10150Sstevel@tonic-gate 		return;
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	ENGINE_add(e_pk11);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	ENGINE_free(e_pk11);
10210Sstevel@tonic-gate 	ERR_clear_error();
10220Sstevel@tonic-gate 	}
10237211Sjp161948 #endif	/* ENGINE_DYNAMIC_SUPPORT */
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate /* These are the static string constants for the DSO file name and
10260Sstevel@tonic-gate  * the function symbol names to bind to.
10270Sstevel@tonic-gate  */
10280Sstevel@tonic-gate static const char *PK11_LIBNAME = NULL;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate static const char *get_PK11_LIBNAME(void)
10310Sstevel@tonic-gate 	{
10320Sstevel@tonic-gate 	if (PK11_LIBNAME)
10330Sstevel@tonic-gate 		return PK11_LIBNAME;
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	return def_PK11_LIBNAME;
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate static void free_PK11_LIBNAME(void)
10390Sstevel@tonic-gate 	{
10400Sstevel@tonic-gate 	if (PK11_LIBNAME)
10410Sstevel@tonic-gate 		OPENSSL_free((void*)PK11_LIBNAME);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	PK11_LIBNAME = NULL;
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate static long set_PK11_LIBNAME(const char *name)
10470Sstevel@tonic-gate 	{
10480Sstevel@tonic-gate 	free_PK11_LIBNAME();
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	return ((PK11_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10537526SVladimir.Kotal@Sun.COM /* acquire all engine specific mutexes before fork */
10547526SVladimir.Kotal@Sun.COM static void pk11_fork_prepare(void)
10557526SVladimir.Kotal@Sun.COM 	{
10567526SVladimir.Kotal@Sun.COM 	int i;
10577526SVladimir.Kotal@Sun.COM 
10587526SVladimir.Kotal@Sun.COM 	LOCK_OBJSTORE(OP_RSA);
10597526SVladimir.Kotal@Sun.COM 	LOCK_OBJSTORE(OP_DSA);
10607526SVladimir.Kotal@Sun.COM 	LOCK_OBJSTORE(OP_DH);
10617526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
10627526SVladimir.Kotal@Sun.COM 		{
10637526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[i].lock);
10647526SVladimir.Kotal@Sun.COM 		}
10657526SVladimir.Kotal@Sun.COM 	}
10667526SVladimir.Kotal@Sun.COM 
10677526SVladimir.Kotal@Sun.COM /* release all engine specific mutexes */
10687526SVladimir.Kotal@Sun.COM static void pk11_fork_parent(void)
10697526SVladimir.Kotal@Sun.COM 	{
10707526SVladimir.Kotal@Sun.COM 	int i;
10717526SVladimir.Kotal@Sun.COM 
10727526SVladimir.Kotal@Sun.COM 	for (i = OP_MAX - 1; i >= 0; i--)
10737526SVladimir.Kotal@Sun.COM 		{
10747526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[i].lock);
10757526SVladimir.Kotal@Sun.COM 		}
10767526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DH);
10777526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DSA);
10787526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_RSA);
10797526SVladimir.Kotal@Sun.COM 	}
10807526SVladimir.Kotal@Sun.COM 
10817526SVladimir.Kotal@Sun.COM /*
10827526SVladimir.Kotal@Sun.COM  * same situation as in parent - we need to unlock all locks to make them
10837526SVladimir.Kotal@Sun.COM  * accessible to all threads.
10847526SVladimir.Kotal@Sun.COM  */
10857526SVladimir.Kotal@Sun.COM static void pk11_fork_child(void)
10867526SVladimir.Kotal@Sun.COM 	{
10877526SVladimir.Kotal@Sun.COM 	int i;
10887526SVladimir.Kotal@Sun.COM 
10897526SVladimir.Kotal@Sun.COM 	for (i = OP_MAX - 1; i >= 0; i--)
10907526SVladimir.Kotal@Sun.COM 		{
10917526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[i].lock);
10927526SVladimir.Kotal@Sun.COM 		}
10937526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DH);
10947526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DSA);
10957526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_RSA);
10967526SVladimir.Kotal@Sun.COM 	}
10977526SVladimir.Kotal@Sun.COM 
10980Sstevel@tonic-gate /* Initialization function for the pk11 engine */
10990Sstevel@tonic-gate static int pk11_init(ENGINE *e)
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate 	return pk11_library_init(e);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /* Initialization function. Sets up various pk11 library components.
11050Sstevel@tonic-gate  * It selects a slot based on predefined critiera. In the process, it also
11060Sstevel@tonic-gate  * count how many ciphers and digests to support. Since the cipher and
11070Sstevel@tonic-gate  * digest information is needed when setting default engine, this function
11080Sstevel@tonic-gate  * needs to be called before calling ENGINE_set_default.
11090Sstevel@tonic-gate  */
1110*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
11110Sstevel@tonic-gate static int pk11_library_init(ENGINE *e)
11120Sstevel@tonic-gate 	{
11130Sstevel@tonic-gate 	CK_C_GetFunctionList p;
11140Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
11150Sstevel@tonic-gate 	CK_INFO info;
11160Sstevel@tonic-gate 	CK_ULONG ul_state_len;
11177211Sjp161948 	int any_slot_found;
11187526SVladimir.Kotal@Sun.COM 	int i;
11197211Sjp161948 
11207211Sjp161948 	/*
11217211Sjp161948 	 * pk11_library_initialized is set to 0 in pk11_finish() which is called
11227211Sjp161948 	 * from ENGINE_finish(). However, if there is still at least one
11237211Sjp161948 	 * existing functional reference to the engine (see engine(3) for more
11247211Sjp161948 	 * information), pk11_finish() is skipped. For example, this can happen
11257211Sjp161948 	 * if an application forgets to clear one cipher context. In case of a
11267211Sjp161948 	 * fork() when the application is finishing the engine so that it can be
11277211Sjp161948 	 * reinitialized in the child, forgotten functional reference causes
11287211Sjp161948 	 * pk11_library_initialized to stay 1. In that case we need the PID
11297211Sjp161948 	 * check so that we properly initialize the engine again.
11307211Sjp161948 	 */
11310Sstevel@tonic-gate 	if (pk11_library_initialized)
11327211Sjp161948 		{
11337211Sjp161948 		if (pk11_pid == getpid())
11347526SVladimir.Kotal@Sun.COM 			{
11357211Sjp161948 			return 1;
11367526SVladimir.Kotal@Sun.COM 			}
11377211Sjp161948 		else
11387526SVladimir.Kotal@Sun.COM 			{
11397211Sjp161948 			global_session = CK_INVALID_HANDLE;
11407526SVladimir.Kotal@Sun.COM 			/*
11417526SVladimir.Kotal@Sun.COM 			 * free the locks first to prevent memory leak in case
11427526SVladimir.Kotal@Sun.COM 			 * the application calls fork() without finishing the
11437526SVladimir.Kotal@Sun.COM 			 * engine first.
11447526SVladimir.Kotal@Sun.COM 			 */
11457526SVladimir.Kotal@Sun.COM 			pk11_free_all_locks();
11467526SVladimir.Kotal@Sun.COM 			}
11477211Sjp161948 		}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (pk11_dso == NULL)
11500Sstevel@tonic-gate 		{
11510Sstevel@tonic-gate 		PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE);
11520Sstevel@tonic-gate 		goto err;
11530Sstevel@tonic-gate 		}
11540Sstevel@tonic-gate 
11557211Sjp161948 #ifdef	SOLARIS_AES_CTR
11567211Sjp161948 	/*
11577211Sjp161948 	 * We must do this before we start working with slots since we need all
11587211Sjp161948 	 * NIDs there.
11597211Sjp161948 	 */
11607211Sjp161948 	if (pk11_add_aes_ctr_NIDs() == 0)
11617211Sjp161948 		goto err;
11627211Sjp161948 #endif	/* SOLARIS_AES_CTR */
11637211Sjp161948 
11647211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
11657211Sjp161948 	if (check_hw_mechanisms() == 0)
11667211Sjp161948 		goto err;
11677211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
11687211Sjp161948 
11690Sstevel@tonic-gate 	/* get the C_GetFunctionList function from the loaded library
11700Sstevel@tonic-gate 	 */
11710Sstevel@tonic-gate 	p = (CK_C_GetFunctionList)DSO_bind_func(pk11_dso,
11720Sstevel@tonic-gate 		PK11_GET_FUNCTION_LIST);
11730Sstevel@tonic-gate 	if ( !p )
11740Sstevel@tonic-gate 		{
11750Sstevel@tonic-gate 		PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE);
11760Sstevel@tonic-gate 		goto err;
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/* get the full function list from the loaded library
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	rv = p(&pFuncList);
11820Sstevel@tonic-gate 	if (rv != CKR_OK)
11830Sstevel@tonic-gate 		{
11847211Sjp161948 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE, rv);
11850Sstevel@tonic-gate 		goto err;
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	rv = pFuncList->C_Initialize(NULL_PTR);
11890Sstevel@tonic-gate 	if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
11900Sstevel@tonic-gate 		{
11917211Sjp161948 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_INITIALIZE, rv);
11920Sstevel@tonic-gate 		goto err;
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	rv = pFuncList->C_GetInfo(&info);
11960Sstevel@tonic-gate 	if (rv != CKR_OK)
11970Sstevel@tonic-gate 		{
11987211Sjp161948 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_GETINFO, rv);
11990Sstevel@tonic-gate 		goto err;
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 
12027211Sjp161948 	if (pk11_choose_slots(&any_slot_found) == 0)
12030Sstevel@tonic-gate 		goto err;
12040Sstevel@tonic-gate 
12057211Sjp161948 	/*
12067211Sjp161948 	 * The library we use, set in def_PK11_LIBNAME, may not offer any
12077211Sjp161948 	 * slot(s). In that case, we must not proceed but we must not return an
12087211Sjp161948 	 * error. The reason is that applications that try to set up the PKCS#11
12097211Sjp161948 	 * engine don't exit on error during the engine initialization just
12107211Sjp161948 	 * because no slot was present.
12117211Sjp161948 	 */
12127211Sjp161948 	if (any_slot_found == 0)
12137211Sjp161948 		return 1;
12147211Sjp161948 
12150Sstevel@tonic-gate 	if (global_session == CK_INVALID_HANDLE)
12160Sstevel@tonic-gate 		{
12170Sstevel@tonic-gate 		/* Open the global_session for the new process */
12180Sstevel@tonic-gate 		rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
12190Sstevel@tonic-gate 			NULL_PTR, NULL_PTR, &global_session);
12200Sstevel@tonic-gate 		if (rv != CKR_OK)
12210Sstevel@tonic-gate 			{
12227211Sjp161948 			PK11err_add_data(PK11_F_LIBRARY_INIT,
12237211Sjp161948 			    PK11_R_OPENSESSION, rv);
12240Sstevel@tonic-gate 			goto err;
12250Sstevel@tonic-gate 			}
12260Sstevel@tonic-gate 		}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	/* Disable digest if C_GetOperationState is not supported since
12290Sstevel@tonic-gate 	 * this function is required by OpenSSL digest copy function */
12300Sstevel@tonic-gate 	if (pFuncList->C_GetOperationState(global_session, NULL, &ul_state_len)
12317211Sjp161948 			== CKR_FUNCTION_NOT_SUPPORTED) {
12327211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
12337211Sjp161948 		fprintf(stderr, "%s: C_GetOperationState() not supported, "
12347211Sjp161948 		    "setting digest_count to 0\n", PK11_DBG);
12357211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
12360Sstevel@tonic-gate 		digest_count = 0;
12377211Sjp161948 	}
12380Sstevel@tonic-gate 
12397526SVladimir.Kotal@Sun.COM 	pk11_library_initialized = TRUE;
12407211Sjp161948 	pk11_pid = getpid();
12417526SVladimir.Kotal@Sun.COM 	/*
12427526SVladimir.Kotal@Sun.COM 	 * if initialization of the locks fails pk11_init_all_locks()
12437526SVladimir.Kotal@Sun.COM 	 * will do the cleanup.
12447526SVladimir.Kotal@Sun.COM 	 */
12457526SVladimir.Kotal@Sun.COM 	if (!pk11_init_all_locks())
12467526SVladimir.Kotal@Sun.COM 		goto err;
12477526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
12487526SVladimir.Kotal@Sun.COM 		session_cache[i].head = NULL;
12497526SVladimir.Kotal@Sun.COM 	/*
12507526SVladimir.Kotal@Sun.COM 	 * initialize active lists. We only use active lists
12517526SVladimir.Kotal@Sun.COM 	 * for asymmetric ciphers.
12527526SVladimir.Kotal@Sun.COM 	 */
12537526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
12547526SVladimir.Kotal@Sun.COM 		active_list[i] = NULL;
12557526SVladimir.Kotal@Sun.COM 
12567526SVladimir.Kotal@Sun.COM 	if (!pk11_atfork_initialized)
12577526SVladimir.Kotal@Sun.COM 		{
12587526SVladimir.Kotal@Sun.COM 		if (pthread_atfork(pk11_fork_prepare, pk11_fork_parent,
12597526SVladimir.Kotal@Sun.COM 		    pk11_fork_child) != 0)
12607526SVladimir.Kotal@Sun.COM 			{
12617526SVladimir.Kotal@Sun.COM 			PK11err(PK11_F_LIBRARY_INIT, PK11_R_ATFORK_FAILED);
12627526SVladimir.Kotal@Sun.COM 			goto err;
12637526SVladimir.Kotal@Sun.COM 			}
12647526SVladimir.Kotal@Sun.COM 		pk11_atfork_initialized = TRUE;
12657526SVladimir.Kotal@Sun.COM 		}
12667526SVladimir.Kotal@Sun.COM 
12670Sstevel@tonic-gate 	return 1;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate err:
12700Sstevel@tonic-gate 	return 0;
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate /* Destructor (complements the "ENGINE_pk11()" constructor)
12740Sstevel@tonic-gate  */
1275*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
12760Sstevel@tonic-gate static int pk11_destroy(ENGINE *e)
12770Sstevel@tonic-gate 	{
12780Sstevel@tonic-gate 	free_PK11_LIBNAME();
12790Sstevel@tonic-gate 	ERR_unload_pk11_strings();
12800Sstevel@tonic-gate 	return 1;
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate /* Termination function to clean up the session, the token, and
12840Sstevel@tonic-gate  * the pk11 library.
12850Sstevel@tonic-gate  */
1286*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
12870Sstevel@tonic-gate static int pk11_finish(ENGINE *e)
12880Sstevel@tonic-gate 	{
12897526SVladimir.Kotal@Sun.COM 	int i;
12907526SVladimir.Kotal@Sun.COM 
12910Sstevel@tonic-gate 	if (pk11_dso == NULL)
12920Sstevel@tonic-gate 		{
12930Sstevel@tonic-gate 		PK11err(PK11_F_FINISH, PK11_R_NOT_LOADED);
12940Sstevel@tonic-gate 		goto err;
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 
12976847Svk199839 	OPENSSL_assert(pFuncList != NULL);
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (pk11_free_all_sessions() == 0)
13000Sstevel@tonic-gate 		goto err;
13010Sstevel@tonic-gate 
13027526SVladimir.Kotal@Sun.COM 	/* free all active lists */
13037526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
13047526SVladimir.Kotal@Sun.COM 		pk11_free_active_list(i);
13057526SVladimir.Kotal@Sun.COM 
13060Sstevel@tonic-gate 	pFuncList->C_CloseSession(global_session);
13077211Sjp161948 	global_session = CK_INVALID_HANDLE;
13087211Sjp161948 
13097211Sjp161948 	/*
13107211Sjp161948 	 * Since we are part of a library (libcrypto.so), calling this function
13117211Sjp161948 	 * may have side-effects.
13127211Sjp161948 	 */
13137211Sjp161948 #if 0
13140Sstevel@tonic-gate 	pFuncList->C_Finalize(NULL);
13157211Sjp161948 #endif
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	if (!DSO_free(pk11_dso))
13180Sstevel@tonic-gate 		{
13190Sstevel@tonic-gate 		PK11err(PK11_F_FINISH, PK11_R_DSO_FAILURE);
13200Sstevel@tonic-gate 		goto err;
13210Sstevel@tonic-gate 		}
13220Sstevel@tonic-gate 	pk11_dso = NULL;
13230Sstevel@tonic-gate 	pFuncList = NULL;
13247526SVladimir.Kotal@Sun.COM 	pk11_library_initialized = FALSE;
13257211Sjp161948 	pk11_pid = 0;
13267526SVladimir.Kotal@Sun.COM 	pk11_free_all_locks();
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	return 1;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate err:
13310Sstevel@tonic-gate 	return 0;
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate /* Standard engine interface function to set the dynamic library path */
1335*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
13360Sstevel@tonic-gate static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
13370Sstevel@tonic-gate 	{
13380Sstevel@tonic-gate 	int initialized = ((pk11_dso == NULL) ? 0 : 1);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	switch(cmd)
13410Sstevel@tonic-gate 		{
13420Sstevel@tonic-gate 	case PK11_CMD_SO_PATH:
13430Sstevel@tonic-gate 		if (p == NULL)
13440Sstevel@tonic-gate 			{
13450Sstevel@tonic-gate 			PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER);
13460Sstevel@tonic-gate 			return 0;
13470Sstevel@tonic-gate 			}
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		if (initialized)
13500Sstevel@tonic-gate 			{
13510Sstevel@tonic-gate 			PK11err(PK11_F_CTRL, PK11_R_ALREADY_LOADED);
13520Sstevel@tonic-gate 			return 0;
13530Sstevel@tonic-gate 			}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		return set_PK11_LIBNAME((const char*)p);
13560Sstevel@tonic-gate 	default:
13570Sstevel@tonic-gate 		break;
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	PK11err(PK11_F_CTRL,PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED);
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	return 0;
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate /* Required function by the engine random interface. It does nothing here
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate static void pk11_rand_cleanup(void)
13690Sstevel@tonic-gate 	{
13700Sstevel@tonic-gate 	return;
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate 
1373*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
13740Sstevel@tonic-gate static void pk11_rand_add(const void *buf, int num, double add)
13750Sstevel@tonic-gate 	{
13760Sstevel@tonic-gate 	PK11_SESSION *sp;
13770Sstevel@tonic-gate 
13787211Sjp161948 	if ((sp = pk11_get_session(OP_RAND)) == NULL)
13790Sstevel@tonic-gate 		return;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	/* Ignore any errors (e.g. CKR_RANDOM_SEED_NOT_SUPPORTED) since
13820Sstevel@tonic-gate 	 * the calling functions do not care anyway
13830Sstevel@tonic-gate 	 */
13840Sstevel@tonic-gate 	pFuncList->C_SeedRandom(sp->session, (unsigned char *) buf, num);
13857211Sjp161948 	pk11_return_session(sp, OP_RAND);
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	return;
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate static void pk11_rand_seed(const void *buf, int num)
13910Sstevel@tonic-gate 	{
13920Sstevel@tonic-gate 	pk11_rand_add(buf, num, 0);
13930Sstevel@tonic-gate 	}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate static int pk11_rand_bytes(unsigned char *buf, int num)
13960Sstevel@tonic-gate 	{
13970Sstevel@tonic-gate 	CK_RV rv;
13980Sstevel@tonic-gate 	PK11_SESSION *sp;
13990Sstevel@tonic-gate 
14007211Sjp161948 	if ((sp = pk11_get_session(OP_RAND)) == NULL)
14010Sstevel@tonic-gate 		return 0;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	rv = pFuncList->C_GenerateRandom(sp->session, buf, num);
14040Sstevel@tonic-gate 	if (rv != CKR_OK)
14050Sstevel@tonic-gate 		{
14067211Sjp161948 		PK11err_add_data(PK11_F_RAND_BYTES, PK11_R_GENERATERANDOM, rv);
14077211Sjp161948 		pk11_return_session(sp, OP_RAND);
14080Sstevel@tonic-gate 		return 0;
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 
14117211Sjp161948 	pk11_return_session(sp, OP_RAND);
14120Sstevel@tonic-gate 	return 1;
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /* Required function by the engine random interface. It does nothing here
14160Sstevel@tonic-gate  */
14170Sstevel@tonic-gate static int pk11_rand_status(void)
14180Sstevel@tonic-gate 	{
14190Sstevel@tonic-gate 	return 1;
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 
14226847Svk199839 /*
14236847Svk199839  * Free all BIGNUM structures from PK11_SESSION.
14246847Svk199839  */
14257526SVladimir.Kotal@Sun.COM static void pk11_free_nums(PK11_SESSION *sp, PK11_OPTYPE optype)
14266847Svk199839 	{
14277526SVladimir.Kotal@Sun.COM 	switch (optype)
14287526SVladimir.Kotal@Sun.COM 		{
14296847Svk199839 #ifndef	OPENSSL_NO_RSA
14307526SVladimir.Kotal@Sun.COM 		case OP_RSA:
14317526SVladimir.Kotal@Sun.COM 			if (sp->opdata_rsa_n_num != NULL)
14327526SVladimir.Kotal@Sun.COM 				{
14337526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_rsa_n_num);
14347526SVladimir.Kotal@Sun.COM 				sp->opdata_rsa_n_num = NULL;
14357526SVladimir.Kotal@Sun.COM 				}
14367526SVladimir.Kotal@Sun.COM 			if (sp->opdata_rsa_e_num != NULL)
14377526SVladimir.Kotal@Sun.COM 				{
14387526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_rsa_e_num);
14397526SVladimir.Kotal@Sun.COM 				sp->opdata_rsa_e_num = NULL;
14407526SVladimir.Kotal@Sun.COM 				}
14417526SVladimir.Kotal@Sun.COM 			if (sp->opdata_rsa_d_num != NULL)
14427526SVladimir.Kotal@Sun.COM 				{
14437526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_rsa_d_num);
14447526SVladimir.Kotal@Sun.COM 				sp->opdata_rsa_d_num = NULL;
14457526SVladimir.Kotal@Sun.COM 				}
14467526SVladimir.Kotal@Sun.COM 			break;
14476847Svk199839 #endif
14486847Svk199839 #ifndef	OPENSSL_NO_DSA
14497526SVladimir.Kotal@Sun.COM 		case OP_DSA:
14507526SVladimir.Kotal@Sun.COM 			if (sp->opdata_dsa_pub_num != NULL)
14517526SVladimir.Kotal@Sun.COM 				{
14527526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_dsa_pub_num);
14537526SVladimir.Kotal@Sun.COM 				sp->opdata_dsa_pub_num = NULL;
14547526SVladimir.Kotal@Sun.COM 				}
14557526SVladimir.Kotal@Sun.COM 			if (sp->opdata_dsa_priv_num != NULL)
14567526SVladimir.Kotal@Sun.COM 				{
14577526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_dsa_priv_num);
14587526SVladimir.Kotal@Sun.COM 				sp->opdata_dsa_priv_num = NULL;
14597526SVladimir.Kotal@Sun.COM 				}
14607526SVladimir.Kotal@Sun.COM 			break;
14616847Svk199839 #endif
14626847Svk199839 #ifndef	OPENSSL_NO_DH
14637526SVladimir.Kotal@Sun.COM 		case OP_DH:
14647526SVladimir.Kotal@Sun.COM 			if (sp->opdata_dh_priv_num != NULL)
14657526SVladimir.Kotal@Sun.COM 				{
14667526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_dh_priv_num);
14677526SVladimir.Kotal@Sun.COM 				sp->opdata_dh_priv_num = NULL;
14687526SVladimir.Kotal@Sun.COM 				}
14697526SVladimir.Kotal@Sun.COM 			break;
14706847Svk199839 #endif
14717526SVladimir.Kotal@Sun.COM 		default:
14727526SVladimir.Kotal@Sun.COM 			break;
14737526SVladimir.Kotal@Sun.COM 		}
14746847Svk199839 	}
14750Sstevel@tonic-gate 
14766847Svk199839 /*
14776847Svk199839  * Get new PK11_SESSION structure ready for use. Every process must have
14786847Svk199839  * its own freelist of PK11_SESSION structures so handle fork() here
14796847Svk199839  * by destroying the old and creating new freelist.
14806847Svk199839  * The returned PK11_SESSION structure is disconnected from the freelist.
14816847Svk199839  */
14827211Sjp161948 PK11_SESSION *pk11_get_session(PK11_OPTYPE optype)
14830Sstevel@tonic-gate 	{
14847526SVladimir.Kotal@Sun.COM 	PK11_SESSION *sp = NULL, *sp1, *freelist;
14857526SVladimir.Kotal@Sun.COM 	pthread_mutex_t *freelist_lock;
14860Sstevel@tonic-gate 	CK_RV rv;
14870Sstevel@tonic-gate 
14887211Sjp161948 	switch (optype)
14897211Sjp161948 		{
14907526SVladimir.Kotal@Sun.COM 		case OP_RSA:
14917526SVladimir.Kotal@Sun.COM 		case OP_DSA:
14927526SVladimir.Kotal@Sun.COM 		case OP_DH:
14937211Sjp161948 		case OP_RAND:
14947211Sjp161948 		case OP_DIGEST:
14957211Sjp161948 		case OP_CIPHER:
14967526SVladimir.Kotal@Sun.COM 			freelist_lock = session_cache[optype].lock;
14977211Sjp161948 			break;
14987211Sjp161948 		default:
14997211Sjp161948 			PK11err(PK11_F_GET_SESSION,
15007211Sjp161948 				PK11_R_INVALID_OPERATION_TYPE);
15017526SVladimir.Kotal@Sun.COM 			return (NULL);
15027211Sjp161948 		}
15037526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_lock(freelist_lock);
15047526SVladimir.Kotal@Sun.COM 	freelist = session_cache[optype].head;
15057211Sjp161948 	sp = freelist;
15067211Sjp161948 
15076847Svk199839 	/*
15086847Svk199839 	 * If the free list is empty, allocate new unitialized (filled
15096847Svk199839 	 * with zeroes) PK11_SESSION structure otherwise return first
15106847Svk199839 	 * structure from the freelist.
15116847Svk199839 	 */
15127211Sjp161948 	if (sp == NULL)
15130Sstevel@tonic-gate 		{
15140Sstevel@tonic-gate 		if ((sp = OPENSSL_malloc(sizeof(PK11_SESSION))) == NULL)
15150Sstevel@tonic-gate 			{
15160Sstevel@tonic-gate 			PK11err(PK11_F_GET_SESSION,
15170Sstevel@tonic-gate 				PK11_R_MALLOC_FAILURE);
15180Sstevel@tonic-gate 			goto err;
15190Sstevel@tonic-gate 			}
1520*7534SVladimir.Kotal@Sun.COM 		(void) memset(sp, 0, sizeof(PK11_SESSION));
15210Sstevel@tonic-gate 		}
15220Sstevel@tonic-gate 	else
15230Sstevel@tonic-gate 		{
15247211Sjp161948 		freelist = sp->next;
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	if (sp->pid != 0 && sp->pid != getpid())
15280Sstevel@tonic-gate 		{
15296847Svk199839 		/*
15306847Svk199839 		 * We are a new process and thus need to free any inherited
15310Sstevel@tonic-gate 		 * PK11_SESSION objects.
15320Sstevel@tonic-gate 		 */
15337211Sjp161948 		while ((sp1 = freelist) != NULL)
15340Sstevel@tonic-gate 			{
15357211Sjp161948 			freelist = sp1->next;
15366847Svk199839 			/*
15377211Sjp161948 			 * NOTE: we do not want to call pk11_free_all_sessions()
15387211Sjp161948 			 * here because it would close underlying PKCS#11
15397211Sjp161948 			 * sessions and destroy all objects.
15406847Svk199839 			 */
15417526SVladimir.Kotal@Sun.COM 			pk11_free_nums(sp1, optype);
15420Sstevel@tonic-gate 			OPENSSL_free(sp1);
15430Sstevel@tonic-gate 			}
15440Sstevel@tonic-gate 
15457526SVladimir.Kotal@Sun.COM 		/* we have to free the active list as well. */
15467526SVladimir.Kotal@Sun.COM 		pk11_free_active_list(optype);
15477526SVladimir.Kotal@Sun.COM 
15480Sstevel@tonic-gate 		/* Initialize the process */
15490Sstevel@tonic-gate 		rv = pFuncList->C_Initialize(NULL_PTR);
15500Sstevel@tonic-gate 		if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
15510Sstevel@tonic-gate 			{
15527211Sjp161948 			PK11err_add_data(PK11_F_GET_SESSION, PK11_R_INITIALIZE,
15537211Sjp161948 			    rv);
15540Sstevel@tonic-gate 			OPENSSL_free(sp);
15550Sstevel@tonic-gate 			sp = NULL;
15560Sstevel@tonic-gate 			goto err;
15570Sstevel@tonic-gate 			}
15580Sstevel@tonic-gate 
15596847Svk199839 		/*
15607211Sjp161948 		 * Choose slot here since the slot table is different on this
15617211Sjp161948 		 * process. If we are here then we must have found at least one
15627211Sjp161948 		 * usable slot before so we don't need to check any_slot_found.
15637211Sjp161948 		 * See pk11_library_init()'s usage of this function for more
15647211Sjp161948 		 * information.
15650Sstevel@tonic-gate 		 */
15667211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
15677211Sjp161948 		if (check_hw_mechanisms() == 0)
15687211Sjp161948 			goto err;
15697211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
15707211Sjp161948 		if (pk11_choose_slots(NULL) == 0)
15710Sstevel@tonic-gate 			goto err;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 		/* Open the global_session for the new process */
15740Sstevel@tonic-gate 		rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
15750Sstevel@tonic-gate 			NULL_PTR, NULL_PTR, &global_session);
15760Sstevel@tonic-gate 		if (rv != CKR_OK)
15770Sstevel@tonic-gate 			{
15787211Sjp161948 			PK11err_add_data(PK11_F_GET_SESSION, PK11_R_OPENSESSION,
15797211Sjp161948 			    rv);
15800Sstevel@tonic-gate 			OPENSSL_free(sp);
15810Sstevel@tonic-gate 			sp = NULL;
15820Sstevel@tonic-gate 			goto err;
15830Sstevel@tonic-gate 			}
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 		/* It is an inherited session and needs re-initialization.
15860Sstevel@tonic-gate 		 */
15877211Sjp161948 		if (pk11_setup_session(sp, optype) == 0)
15880Sstevel@tonic-gate 			{
15890Sstevel@tonic-gate 			OPENSSL_free(sp);
15900Sstevel@tonic-gate 			sp = NULL;
15910Sstevel@tonic-gate 			}
15920Sstevel@tonic-gate 		}
15930Sstevel@tonic-gate 	else if (sp->pid == 0)
15940Sstevel@tonic-gate 		{
15957211Sjp161948 		/* It is a new session and needs initialization. */
15967211Sjp161948 		if (pk11_setup_session(sp, optype) == 0)
15970Sstevel@tonic-gate 			{
15980Sstevel@tonic-gate 			OPENSSL_free(sp);
15990Sstevel@tonic-gate 			sp = NULL;
16000Sstevel@tonic-gate 			}
16010Sstevel@tonic-gate 		}
16020Sstevel@tonic-gate 
16037526SVladimir.Kotal@Sun.COM 	/* set new head for the list of PK11_SESSION objects */
16047526SVladimir.Kotal@Sun.COM 	session_cache[optype].head = freelist;
16057211Sjp161948 
16060Sstevel@tonic-gate err:
16076847Svk199839 	if (sp != NULL)
16080Sstevel@tonic-gate 		sp->next = NULL;
16090Sstevel@tonic-gate 
16107526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_unlock(freelist_lock);
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	return sp;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 
16167211Sjp161948 void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype)
16170Sstevel@tonic-gate 	{
16187526SVladimir.Kotal@Sun.COM 	pthread_mutex_t *freelist_lock;
16197526SVladimir.Kotal@Sun.COM 	PK11_SESSION *freelist;
16207526SVladimir.Kotal@Sun.COM 
16210Sstevel@tonic-gate 	if (sp == NULL || sp->pid != getpid())
16220Sstevel@tonic-gate 		return;
16230Sstevel@tonic-gate 
16247211Sjp161948 	switch (optype)
16257211Sjp161948 		{
16267526SVladimir.Kotal@Sun.COM 		case OP_RSA:
16277526SVladimir.Kotal@Sun.COM 		case OP_DSA:
16287526SVladimir.Kotal@Sun.COM 		case OP_DH:
16297211Sjp161948 		case OP_RAND:
16307211Sjp161948 		case OP_DIGEST:
16317211Sjp161948 		case OP_CIPHER:
16327526SVladimir.Kotal@Sun.COM 			freelist_lock = session_cache[optype].lock;
16337211Sjp161948 			break;
16347526SVladimir.Kotal@Sun.COM 		default:
16357526SVladimir.Kotal@Sun.COM 			PK11err(PK11_F_RETURN_SESSION,
16367526SVladimir.Kotal@Sun.COM 				PK11_R_INVALID_OPERATION_TYPE);
16377526SVladimir.Kotal@Sun.COM 			return;
16387211Sjp161948 		}
16390Sstevel@tonic-gate 
16407526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_lock(freelist_lock);
16417526SVladimir.Kotal@Sun.COM 	freelist = session_cache[optype].head;
16427526SVladimir.Kotal@Sun.COM 	sp->next = freelist;
16437526SVladimir.Kotal@Sun.COM 	session_cache[optype].head = sp;
16447526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_unlock(freelist_lock);
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate /* Destroy all objects. This function is called when the engine is finished
16490Sstevel@tonic-gate  */
16500Sstevel@tonic-gate static int pk11_free_all_sessions()
16510Sstevel@tonic-gate 	{
16527211Sjp161948 	int ret = 1;
16537526SVladimir.Kotal@Sun.COM 	int type;
16540Sstevel@tonic-gate 
16556847Svk199839 #ifndef OPENSSL_NO_RSA
16566847Svk199839 	(void) pk11_destroy_rsa_key_objects(NULL);
16577211Sjp161948 #endif	/* OPENSSL_NO_RSA */
16586847Svk199839 #ifndef OPENSSL_NO_DSA
16596847Svk199839 	(void) pk11_destroy_dsa_key_objects(NULL);
16607211Sjp161948 #endif	/* OPENSSL_NO_DSA */
16616847Svk199839 #ifndef OPENSSL_NO_DH
16626847Svk199839 	(void) pk11_destroy_dh_key_objects(NULL);
16637211Sjp161948 #endif	/* OPENSSL_NO_DH */
16646847Svk199839 	(void) pk11_destroy_cipher_key_objects(NULL);
16657211Sjp161948 
16667211Sjp161948 	/*
16677211Sjp161948 	 * We try to release as much as we can but any error means that we will
16687211Sjp161948 	 * return 0 on exit.
16697211Sjp161948 	 */
16707526SVladimir.Kotal@Sun.COM 	for (type = 0; type < OP_MAX; type++)
16717526SVladimir.Kotal@Sun.COM 		{
16727526SVladimir.Kotal@Sun.COM 		if (pk11_free_session_list(type) == 0)
16737526SVladimir.Kotal@Sun.COM 			ret = 0;
16747526SVladimir.Kotal@Sun.COM 		}
16757211Sjp161948 
16767211Sjp161948 	return ret;
16777211Sjp161948 	}
16780Sstevel@tonic-gate 
16797211Sjp161948 /*
16807526SVladimir.Kotal@Sun.COM  * Destroy session structures from the linked list specified. Free as many
16817526SVladimir.Kotal@Sun.COM  * sessions as possible but any failure in C_CloseSession() means that we
16827526SVladimir.Kotal@Sun.COM  * return an error on return.
16837211Sjp161948  */
16847526SVladimir.Kotal@Sun.COM static int pk11_free_session_list(PK11_OPTYPE optype)
16857211Sjp161948 	{
16867211Sjp161948 	CK_RV rv;
16877211Sjp161948 	PK11_SESSION *sp = NULL;
16887526SVladimir.Kotal@Sun.COM 	PK11_SESSION *freelist = NULL;
16897211Sjp161948 	pid_t mypid = getpid();
16907526SVladimir.Kotal@Sun.COM 	pthread_mutex_t *freelist_lock;
16917211Sjp161948 	int ret = 1;
16927211Sjp161948 
16937526SVladimir.Kotal@Sun.COM 	switch (optype)
16947526SVladimir.Kotal@Sun.COM 		{
16957526SVladimir.Kotal@Sun.COM 		case OP_RSA:
16967526SVladimir.Kotal@Sun.COM 		case OP_DSA:
16977526SVladimir.Kotal@Sun.COM 		case OP_DH:
16987526SVladimir.Kotal@Sun.COM 		case OP_RAND:
16997526SVladimir.Kotal@Sun.COM 		case OP_DIGEST:
17007526SVladimir.Kotal@Sun.COM 		case OP_CIPHER:
17017526SVladimir.Kotal@Sun.COM 			freelist_lock = session_cache[optype].lock;
17027526SVladimir.Kotal@Sun.COM 			break;
17037526SVladimir.Kotal@Sun.COM 		default:
17047526SVladimir.Kotal@Sun.COM 			PK11err(PK11_F_FREE_ALL_SESSIONS,
17057526SVladimir.Kotal@Sun.COM 				PK11_R_INVALID_OPERATION_TYPE);
17067526SVladimir.Kotal@Sun.COM 			return (0);
17077526SVladimir.Kotal@Sun.COM 		}
17087526SVladimir.Kotal@Sun.COM 
17097526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_lock(freelist_lock);
17107526SVladimir.Kotal@Sun.COM 	freelist = session_cache[optype].head;
17117526SVladimir.Kotal@Sun.COM 	while ((sp = freelist) != NULL)
17120Sstevel@tonic-gate 		{
17130Sstevel@tonic-gate 		if (sp->session != CK_INVALID_HANDLE && sp->pid == mypid)
17140Sstevel@tonic-gate 			{
17150Sstevel@tonic-gate 			rv = pFuncList->C_CloseSession(sp->session);
17160Sstevel@tonic-gate 			if (rv != CKR_OK)
17170Sstevel@tonic-gate 				{
17187211Sjp161948 				PK11err_add_data(PK11_F_FREE_ALL_SESSIONS,
17197211Sjp161948 					PK11_R_CLOSESESSION, rv);
17207211Sjp161948 				ret = 0;
17210Sstevel@tonic-gate 				}
17220Sstevel@tonic-gate 			}
17237526SVladimir.Kotal@Sun.COM 		freelist = sp->next;
17247526SVladimir.Kotal@Sun.COM 		pk11_free_nums(sp, optype);
17250Sstevel@tonic-gate 		OPENSSL_free(sp);
17260Sstevel@tonic-gate 		}
17277211Sjp161948 
17287526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_unlock(freelist_lock);
17290Sstevel@tonic-gate 	return ret;
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 
17337211Sjp161948 static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype)
17340Sstevel@tonic-gate 	{
17350Sstevel@tonic-gate 	CK_RV rv;
17367211Sjp161948 	CK_SLOT_ID myslot;
17377211Sjp161948 
17387211Sjp161948 	switch (optype)
17397211Sjp161948 		{
17407526SVladimir.Kotal@Sun.COM 		case OP_RSA:
17417526SVladimir.Kotal@Sun.COM 		case OP_DSA:
17427526SVladimir.Kotal@Sun.COM 		case OP_DH:
17437211Sjp161948 			myslot = pubkey_SLOTID;
17447211Sjp161948 			break;
17457211Sjp161948 		case OP_RAND:
17467211Sjp161948 			myslot = rand_SLOTID;
17477211Sjp161948 			break;
17487211Sjp161948 		case OP_DIGEST:
17497211Sjp161948 		case OP_CIPHER:
17507211Sjp161948 			myslot = SLOTID;
17517211Sjp161948 			break;
17527211Sjp161948 		default:
17537211Sjp161948 			PK11err(PK11_F_SETUP_SESSION,
17547211Sjp161948 			    PK11_R_INVALID_OPERATION_TYPE);
17557211Sjp161948 			return 0;
17567211Sjp161948 		}
17577211Sjp161948 
17580Sstevel@tonic-gate 	sp->session = CK_INVALID_HANDLE;
17597211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
17607211Sjp161948 	fprintf(stderr, "%s: myslot=%d optype=%d\n", PK11_DBG, myslot, optype);
17617211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
17627211Sjp161948 	rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION,
17630Sstevel@tonic-gate 		NULL_PTR, NULL_PTR, &sp->session);
17640Sstevel@tonic-gate 	if (rv == CKR_CRYPTOKI_NOT_INITIALIZED)
17650Sstevel@tonic-gate 		{
17660Sstevel@tonic-gate 		/*
17670Sstevel@tonic-gate 		 * We are probably a child process so force the
17680Sstevel@tonic-gate 		 * reinitialize of the session
17690Sstevel@tonic-gate 		 */
17707526SVladimir.Kotal@Sun.COM 		pk11_library_initialized = FALSE;
17710Sstevel@tonic-gate 		(void) pk11_library_init(NULL);
17727211Sjp161948 		rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION,
17730Sstevel@tonic-gate 			NULL_PTR, NULL_PTR, &sp->session);
17740Sstevel@tonic-gate 		}
17750Sstevel@tonic-gate 	if (rv != CKR_OK)
17760Sstevel@tonic-gate 		{
17777211Sjp161948 		PK11err_add_data(PK11_F_SETUP_SESSION, PK11_R_OPENSESSION, rv);
17780Sstevel@tonic-gate 		return 0;
17790Sstevel@tonic-gate 		}
17800Sstevel@tonic-gate 
17817526SVladimir.Kotal@Sun.COM 	sp->pid = getpid();
17827526SVladimir.Kotal@Sun.COM 
17837526SVladimir.Kotal@Sun.COM 	switch (optype)
17840Sstevel@tonic-gate 		{
17856847Svk199839 #ifndef OPENSSL_NO_RSA
17867526SVladimir.Kotal@Sun.COM 		case OP_RSA:
17877526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
17887526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
17897526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_pub = NULL;
17907526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_n_num = NULL;
17917526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_e_num = NULL;
17927526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_priv = NULL;
17937526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_d_num = NULL;
17947526SVladimir.Kotal@Sun.COM 			break;
17957211Sjp161948 #endif	/* OPENSSL_NO_RSA */
17966847Svk199839 #ifndef OPENSSL_NO_DSA
17977526SVladimir.Kotal@Sun.COM 		case OP_DSA:
17987526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub_key = CK_INVALID_HANDLE;
17997526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv_key = CK_INVALID_HANDLE;
18007526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub = NULL;
18017526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub_num = NULL;
18027526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv = NULL;
18037526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv_num = NULL;
18047526SVladimir.Kotal@Sun.COM 			break;
18057211Sjp161948 #endif	/* OPENSSL_NO_DSA */
18066847Svk199839 #ifndef OPENSSL_NO_DH
18077526SVladimir.Kotal@Sun.COM 		case OP_DH:
18087526SVladimir.Kotal@Sun.COM 			sp->opdata_dh_key = CK_INVALID_HANDLE;
18097526SVladimir.Kotal@Sun.COM 			sp->opdata_dh = NULL;
18107526SVladimir.Kotal@Sun.COM 			sp->opdata_dh_priv_num = NULL;
18117526SVladimir.Kotal@Sun.COM 			break;
18127211Sjp161948 #endif	/* OPENSSL_NO_DH */
18137526SVladimir.Kotal@Sun.COM 		case OP_CIPHER:
18147526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key = CK_INVALID_HANDLE;
18157526SVladimir.Kotal@Sun.COM 			sp->opdata_encrypt = -1;
18167526SVladimir.Kotal@Sun.COM 			break;
18177526SVladimir.Kotal@Sun.COM 		}
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	return 1;
18200Sstevel@tonic-gate 	}
18210Sstevel@tonic-gate 
18226847Svk199839 #ifndef OPENSSL_NO_RSA
18236847Svk199839 /* Destroy RSA public key from single session. */
18246847Svk199839 int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock)
18256847Svk199839 	{
18266847Svk199839 	int ret = 0;
18276847Svk199839 
18287526SVladimir.Kotal@Sun.COM 	if (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE)
18296847Svk199839 		{
18307526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_rsa_pub_key,
18317526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_RSA);
18327526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
18337526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_pub = NULL;
18347526SVladimir.Kotal@Sun.COM 		if (sp->opdata_rsa_n_num != NULL)
18357526SVladimir.Kotal@Sun.COM 			{
18367526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_rsa_n_num);
18377526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_n_num = NULL;
18387526SVladimir.Kotal@Sun.COM 			}
18397526SVladimir.Kotal@Sun.COM 		if (sp->opdata_rsa_e_num != NULL)
18407526SVladimir.Kotal@Sun.COM 			{
18417526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_rsa_e_num);
18427526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_e_num = NULL;
18437526SVladimir.Kotal@Sun.COM 			}
18446847Svk199839 		}
18456847Svk199839 
18466847Svk199839 	return (ret);
18476847Svk199839 	}
18486847Svk199839 
18496847Svk199839 /* Destroy RSA private key from single session. */
18506847Svk199839 int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock)
18510Sstevel@tonic-gate 	{
18520Sstevel@tonic-gate 	int ret = 0;
18536847Svk199839 
18547526SVladimir.Kotal@Sun.COM 	if (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE)
18556847Svk199839 		{
18567526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_rsa_priv_key,
18577526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_RSA);
18587526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
18597526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_priv = NULL;
18607526SVladimir.Kotal@Sun.COM 		if (sp->opdata_rsa_d_num != NULL)
18617526SVladimir.Kotal@Sun.COM 			{
18627526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_rsa_d_num);
18637526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_d_num = NULL;
18647526SVladimir.Kotal@Sun.COM 			}
18656847Svk199839 		}
18666847Svk199839 
18676847Svk199839 	return (ret);
18686847Svk199839 	}
18696847Svk199839 
18706847Svk199839 /*
18717211Sjp161948  * Destroy RSA key object wrapper. If session is NULL, try to destroy all
18727211Sjp161948  * objects in the free list.
18736847Svk199839  */
18746847Svk199839 int pk11_destroy_rsa_key_objects(PK11_SESSION *session)
18756847Svk199839 	{
18766847Svk199839 	int ret = 1;
18770Sstevel@tonic-gate 	PK11_SESSION *sp = NULL;
18780Sstevel@tonic-gate 	PK11_SESSION *local_free_session;
18796847Svk199839 	CK_BBOOL uselock = TRUE;
18800Sstevel@tonic-gate 
18816847Svk199839 	if (session != NULL)
18820Sstevel@tonic-gate 		local_free_session = session;
18830Sstevel@tonic-gate 	else
18846847Svk199839 		{
18857526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_RSA].lock);
18867526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_RSA].head;
18876847Svk199839 		uselock = FALSE;
18886847Svk199839 		}
18890Sstevel@tonic-gate 
18906847Svk199839 	/*
18916847Svk199839 	 * go through the list of sessions and delete key objects
18926847Svk199839 	 */
18930Sstevel@tonic-gate 	while ((sp = local_free_session) != NULL)
18940Sstevel@tonic-gate 		{
18950Sstevel@tonic-gate 		local_free_session = sp->next;
18960Sstevel@tonic-gate 
18976847Svk199839 		/*
18986847Svk199839 		 * Do not terminate list traversal if one of the
18996847Svk199839 		 * destroy operations fails.
19006847Svk199839 		 */
19016847Svk199839 		if (pk11_destroy_rsa_object_pub(sp, uselock) == 0)
19020Sstevel@tonic-gate 			{
19036847Svk199839 			ret = 0;
19046847Svk199839 			continue;
19050Sstevel@tonic-gate 			}
19066847Svk199839 		if (pk11_destroy_rsa_object_priv(sp, uselock) == 0)
19076847Svk199839 			{
19086847Svk199839 			ret = 0;
19096847Svk199839 			continue;
19106847Svk199839 			}
19116847Svk199839 		}
19120Sstevel@tonic-gate 
19136847Svk199839 	if (session == NULL)
19147526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_RSA].lock);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	return ret;
19170Sstevel@tonic-gate 	}
19187211Sjp161948 #endif	/* OPENSSL_NO_RSA */
19190Sstevel@tonic-gate 
19206847Svk199839 #ifndef OPENSSL_NO_DSA
19216847Svk199839 /* Destroy DSA public key from single session. */
19226847Svk199839 int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock)
19230Sstevel@tonic-gate 	{
19240Sstevel@tonic-gate 	int ret = 0;
19256847Svk199839 
19267526SVladimir.Kotal@Sun.COM 	if (sp->opdata_dsa_pub_key != CK_INVALID_HANDLE)
19276847Svk199839 		{
19287526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_dsa_pub_key,
19297526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_DSA);
19307526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_pub_key = CK_INVALID_HANDLE;
19317526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_pub = NULL;
19327526SVladimir.Kotal@Sun.COM 		if (sp->opdata_dsa_pub_num != NULL)
19337526SVladimir.Kotal@Sun.COM 			{
19347526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_dsa_pub_num);
19357526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub_num = NULL;
19367526SVladimir.Kotal@Sun.COM 			}
19376847Svk199839 		}
19386847Svk199839 
19396847Svk199839 	return (ret);
19406847Svk199839 	}
19416847Svk199839 
19426847Svk199839 /* Destroy DSA private key from single session. */
19436847Svk199839 int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock)
19446847Svk199839 	{
19456847Svk199839 	int ret = 0;
19466847Svk199839 
19477526SVladimir.Kotal@Sun.COM 	if (sp->opdata_dsa_priv_key != CK_INVALID_HANDLE)
19486847Svk199839 		{
19497526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_dsa_priv_key,
19507526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_DSA);
19517526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_priv_key = CK_INVALID_HANDLE;
19527526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_priv = NULL;
19537526SVladimir.Kotal@Sun.COM 		if (sp->opdata_dsa_priv_num != NULL)
19547526SVladimir.Kotal@Sun.COM 			{
19557526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_dsa_priv_num);
19567526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv_num = NULL;
19577526SVladimir.Kotal@Sun.COM 			}
19586847Svk199839 		}
19596847Svk199839 
19606847Svk199839 	return (ret);
19616847Svk199839 	}
19626847Svk199839 
19636847Svk199839 /*
19647211Sjp161948  * Destroy DSA key object wrapper. If session is NULL, try to destroy all
19657211Sjp161948  * objects in the free list.
19666847Svk199839  */
19676847Svk199839 int pk11_destroy_dsa_key_objects(PK11_SESSION *session)
19686847Svk199839 	{
19696847Svk199839 	int ret = 1;
19700Sstevel@tonic-gate 	PK11_SESSION *sp = NULL;
19710Sstevel@tonic-gate 	PK11_SESSION *local_free_session;
19726847Svk199839 	CK_BBOOL uselock = TRUE;
19730Sstevel@tonic-gate 
19746847Svk199839 	if (session != NULL)
19750Sstevel@tonic-gate 		local_free_session = session;
19760Sstevel@tonic-gate 	else
19776847Svk199839 		{
19787526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_DSA].lock);
19797526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_DSA].head;
19806847Svk199839 		uselock = FALSE;
19816847Svk199839 		}
19826847Svk199839 
19836847Svk199839 	/*
19846847Svk199839 	 * go through the list of sessions and delete key objects
19856847Svk199839 	 */
19860Sstevel@tonic-gate 	while ((sp = local_free_session) != NULL)
19870Sstevel@tonic-gate 		{
19880Sstevel@tonic-gate 		local_free_session = sp->next;
19890Sstevel@tonic-gate 
19906847Svk199839 		/*
19916847Svk199839 		 * Do not terminate list traversal if one of the
19926847Svk199839 		 * destroy operations fails.
19936847Svk199839 		 */
19946847Svk199839 		if (pk11_destroy_dsa_object_pub(sp, uselock) == 0)
19950Sstevel@tonic-gate 			{
19966847Svk199839 			ret = 0;
19976847Svk199839 			continue;
19980Sstevel@tonic-gate 			}
19996847Svk199839 		if (pk11_destroy_dsa_object_priv(sp, uselock) == 0)
20006847Svk199839 			{
20016847Svk199839 			ret = 0;
20026847Svk199839 			continue;
20036847Svk199839 			}
20040Sstevel@tonic-gate 		}
20056847Svk199839 
20066847Svk199839 	if (session == NULL)
20077526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_DSA].lock);
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	return ret;
20100Sstevel@tonic-gate 	}
20117211Sjp161948 #endif	/* OPENSSL_NO_DSA */
20126847Svk199839 
20136847Svk199839 #ifndef OPENSSL_NO_DH
20146847Svk199839 /* Destroy DH key from single session. */
20156847Svk199839 int pk11_destroy_dh_object(PK11_SESSION *sp, CK_BBOOL uselock)
20166847Svk199839 	{
20176847Svk199839 	int ret = 0;
20186847Svk199839 
20197526SVladimir.Kotal@Sun.COM 	if (sp->opdata_dh_key != CK_INVALID_HANDLE)
20206847Svk199839 		{
20217526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_dh_key,
20227526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_DH);
20237526SVladimir.Kotal@Sun.COM 		sp->opdata_dh_key = CK_INVALID_HANDLE;
20247526SVladimir.Kotal@Sun.COM 		sp->opdata_dh = NULL;
20257526SVladimir.Kotal@Sun.COM 		if (sp->opdata_dh_priv_num != NULL)
20267526SVladimir.Kotal@Sun.COM 			{
20277526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_dh_priv_num);
20287526SVladimir.Kotal@Sun.COM 			sp->opdata_dh_priv_num = NULL;
20297526SVladimir.Kotal@Sun.COM 			}
20306847Svk199839 		}
20316847Svk199839 
20326847Svk199839 	return (ret);
20336847Svk199839 	}
20346847Svk199839 
20356847Svk199839 /*
20366847Svk199839  * Destroy DH key object wrapper.
20376847Svk199839  *
20386847Svk199839  * arg0: pointer to PKCS#11 engine session structure
20396847Svk199839  *       if session is NULL, try to destroy all objects in the free list
20406847Svk199839  */
20416847Svk199839 int pk11_destroy_dh_key_objects(PK11_SESSION *session)
20426847Svk199839 	{
20436847Svk199839 	int ret = 1;
20446847Svk199839 	PK11_SESSION *sp = NULL;
20456847Svk199839 	PK11_SESSION *local_free_session;
20466847Svk199839 	CK_BBOOL uselock = TRUE;
20476847Svk199839 
20486847Svk199839 	if (session != NULL)
20496847Svk199839 		local_free_session = session;
20506847Svk199839 	else
20516847Svk199839 		{
20527526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_DH].lock);
20537526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_DH].head;
20546847Svk199839 		uselock = FALSE;
20556847Svk199839 		}
20566847Svk199839 
20576847Svk199839 	while ((sp = local_free_session) != NULL)
20586847Svk199839 		{
20596847Svk199839 		local_free_session = sp->next;
20606847Svk199839 
20616847Svk199839 		/*
20626847Svk199839 		 * Do not terminate list traversal if one of the
20636847Svk199839 		 * destroy operations fails.
20646847Svk199839 		 */
20656847Svk199839 		if (pk11_destroy_dh_object(sp, uselock) == 0)
20666847Svk199839 			{
20676847Svk199839 			ret = 0;
20686847Svk199839 			continue;
20696847Svk199839 			}
20706847Svk199839 		}
20716847Svk199839 err:
20726847Svk199839 	if (session == NULL)
20737526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_DH].lock);
20746847Svk199839 
20756847Svk199839 	return ret;
20766847Svk199839 	}
20777211Sjp161948 #endif	/* OPENSSL_NO_DH */
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate static int pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh)
20800Sstevel@tonic-gate 	{
20810Sstevel@tonic-gate 	CK_RV rv;
20820Sstevel@tonic-gate 	rv = pFuncList->C_DestroyObject(session, oh);
20830Sstevel@tonic-gate 	if (rv != CKR_OK)
20840Sstevel@tonic-gate 		{
20857211Sjp161948 		PK11err_add_data(PK11_F_DESTROY_OBJECT, PK11_R_DESTROYOBJECT,
20867211Sjp161948 		    rv);
20870Sstevel@tonic-gate 		return 0;
20880Sstevel@tonic-gate 		}
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	return 1;
20910Sstevel@tonic-gate 	}
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate /* Symmetric ciphers and digests support functions
20950Sstevel@tonic-gate  */
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate static int
20980Sstevel@tonic-gate cipher_nid_to_pk11(int nid)
20990Sstevel@tonic-gate 	{
21000Sstevel@tonic-gate 	int i;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	for (i = 0; i < PK11_CIPHER_MAX; i++)
21030Sstevel@tonic-gate 		if (ciphers[i].nid == nid)
21040Sstevel@tonic-gate 			return (ciphers[i].id);
21050Sstevel@tonic-gate 	return (-1);
21060Sstevel@tonic-gate 	}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate static int
21090Sstevel@tonic-gate pk11_usable_ciphers(const int **nids)
21100Sstevel@tonic-gate 	{
21110Sstevel@tonic-gate 	if (cipher_count > 0)
21120Sstevel@tonic-gate 		*nids = cipher_nids;
21130Sstevel@tonic-gate 	else
21140Sstevel@tonic-gate 		*nids = NULL;
21150Sstevel@tonic-gate 	return (cipher_count);
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate static int
21190Sstevel@tonic-gate pk11_usable_digests(const int **nids)
21200Sstevel@tonic-gate 	{
21210Sstevel@tonic-gate 	if (digest_count > 0)
21220Sstevel@tonic-gate 		*nids = digest_nids;
21230Sstevel@tonic-gate 	else
21240Sstevel@tonic-gate 		*nids = NULL;
21250Sstevel@tonic-gate 	return (digest_count);
21260Sstevel@tonic-gate 	}
21270Sstevel@tonic-gate 
21287211Sjp161948 /*
21297211Sjp161948  * Init context for encryption or decryption using a symmetric key.
21307211Sjp161948  */
21317211Sjp161948 static int pk11_init_symmetric(EVP_CIPHER_CTX *ctx, PK11_CIPHER *pcipher,
21327211Sjp161948 	PK11_SESSION *sp, CK_MECHANISM_PTR pmech)
21337211Sjp161948 	{
21347211Sjp161948 	CK_RV rv;
21357211Sjp161948 #ifdef	SOLARIS_AES_CTR
21367211Sjp161948 	CK_AES_CTR_PARAMS ctr_params;
21377211Sjp161948 #endif	/* SOLARIS_AES_CTR */
21387211Sjp161948 
21397211Sjp161948 	/*
21407211Sjp161948 	 * We expect pmech->mechanism to be already set and
21417211Sjp161948 	 * pParameter/ulParameterLen initialized to NULL/0 before
21427211Sjp161948 	 * pk11_init_symetric() is called.
21437211Sjp161948 	 */
21447211Sjp161948 	OPENSSL_assert(pmech->mechanism != NULL);
21457211Sjp161948 	OPENSSL_assert(pmech->pParameter == NULL);
21467211Sjp161948 	OPENSSL_assert(pmech->ulParameterLen == 0);
21477211Sjp161948 
21487211Sjp161948 #ifdef	SOLARIS_AES_CTR
21497211Sjp161948 	if (ctx->cipher->nid == NID_aes_128_ctr ||
21507211Sjp161948 	    ctx->cipher->nid == NID_aes_192_ctr ||
21517211Sjp161948 	    ctx->cipher->nid == NID_aes_256_ctr)
21527211Sjp161948 		{
21537211Sjp161948 		pmech->pParameter = (void *)(&ctr_params);
21547211Sjp161948 		pmech->ulParameterLen = sizeof(ctr_params);
21557211Sjp161948 		/*
21567211Sjp161948 		 * For now, we are limited to the fixed length of the counter,
21577211Sjp161948 		 * it covers the whole counter block. That's what RFC 4344
21587211Sjp161948 		 * needs. For more information on internal structure of the
21597211Sjp161948 		 * counter block, see RFC 3686. If needed in the future, we can
21607211Sjp161948 		 * add code so that the counter length can be set via
21617211Sjp161948 		 * ENGINE_ctrl() function.
21627211Sjp161948 		 */
21637211Sjp161948 		ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8;
21647211Sjp161948 		OPENSSL_assert(pcipher->iv_len == AES_BLOCK_SIZE);
2165*7534SVladimir.Kotal@Sun.COM 		(void) memcpy(ctr_params.cb, ctx->iv, AES_BLOCK_SIZE);
21667211Sjp161948 		}
21677211Sjp161948 	else
21687211Sjp161948 #endif	/* SOLARIS_AES_CTR */
21697211Sjp161948 		{
21707211Sjp161948 		if (pcipher->iv_len > 0)
21717211Sjp161948 			{
21727211Sjp161948 			pmech->pParameter = (void *)ctx->iv;
21737211Sjp161948 			pmech->ulParameterLen = pcipher->iv_len;
21747211Sjp161948 			}
21757211Sjp161948 		}
21767211Sjp161948 
21777211Sjp161948 	/* if we get here, the encryption needs to be reinitialized */
21787211Sjp161948 	if (ctx->encrypt)
21797526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_EncryptInit(sp->session, pmech,
21807526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key);
21817211Sjp161948 	else
21827526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_DecryptInit(sp->session, pmech,
21837526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key);
21847211Sjp161948 
21857211Sjp161948 	if (rv != CKR_OK)
21867211Sjp161948 		{
21877211Sjp161948 		PK11err_add_data(PK11_F_CIPHER_INIT, ctx->encrypt ?
21887211Sjp161948 		    PK11_R_ENCRYPTINIT : PK11_R_DECRYPTINIT, rv);
21897211Sjp161948 		pk11_return_session(sp, OP_CIPHER);
21907211Sjp161948 		return (0);
21917211Sjp161948 		}
21927211Sjp161948 
21937211Sjp161948 	return (1);
21947211Sjp161948 	}
21957211Sjp161948 
2196*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
21970Sstevel@tonic-gate static int
21980Sstevel@tonic-gate pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
21990Sstevel@tonic-gate     const unsigned char *iv, int enc)
22000Sstevel@tonic-gate 	{
22014320Sjp161948 	CK_MECHANISM mech;
22020Sstevel@tonic-gate 	int index;
22030Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data;
22040Sstevel@tonic-gate 	PK11_SESSION *sp;
22057211Sjp161948 	PK11_CIPHER *p_ciph_table_row;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	state->sp = NULL;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	index = cipher_nid_to_pk11(ctx->cipher->nid);
22100Sstevel@tonic-gate 	if (index < 0 || index >= PK11_CIPHER_MAX)
22110Sstevel@tonic-gate 		return 0;
22120Sstevel@tonic-gate 
22137211Sjp161948 	p_ciph_table_row = &ciphers[index];
22147211Sjp161948 	/*
22157211Sjp161948 	 * iv_len in the ctx->cipher structure is the maximum IV length for the
22167211Sjp161948 	 * current cipher and it must be less or equal to the IV length in our
22177211Sjp161948 	 * ciphers table. The key length must match precisely. Every application
22187211Sjp161948 	 * can define its own EVP functions so this code serves as a sanity
22197211Sjp161948 	 * check.
22207211Sjp161948 	 *
22217211Sjp161948 	 * Note that the reason why the IV length in ctx->cipher might be
22227211Sjp161948 	 * greater than the actual length is that OpenSSL uses BLOCK_CIPHER_defs
22237211Sjp161948 	 * macro to define functions that return EVP structures for all DES
22247211Sjp161948 	 * modes. So, even ECB modes get 8 byte IV.
22257211Sjp161948 	 */
22267211Sjp161948 	if (ctx->cipher->iv_len < p_ciph_table_row->iv_len ||
22277211Sjp161948 	    ctx->key_len != p_ciph_table_row->key_len)
22287211Sjp161948 		{
22297211Sjp161948 		PK11err(PK11_F_CIPHER_INIT, PK11_R_KEY_OR_IV_LEN_PROBLEM);
22300Sstevel@tonic-gate 		return 0;
22317211Sjp161948 		}
22327211Sjp161948 
22337211Sjp161948 	if ((sp = pk11_get_session(OP_CIPHER)) == NULL)
22340Sstevel@tonic-gate 		return 0;
22350Sstevel@tonic-gate 
22364320Sjp161948 	/* if applicable, the mechanism parameter is used for IV */
22377211Sjp161948 	mech.mechanism = p_ciph_table_row->mech_type;
22384320Sjp161948 	mech.pParameter = NULL;
22394320Sjp161948 	mech.ulParameterLen = 0;
22404320Sjp161948 
22410Sstevel@tonic-gate 	/* The key object is destroyed here if it is not the current key
22420Sstevel@tonic-gate 	 */
22437211Sjp161948 	(void) check_new_cipher_key(sp, key, p_ciph_table_row->key_len);
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	/* If the key is the same and the encryption is also the same,
22466847Svk199839 	 * then just reuse it. However, we must not forget to reinitialize the
22476847Svk199839 	 * context that was finalized in pk11_cipher_cleanup().
22480Sstevel@tonic-gate 	 */
22497526SVladimir.Kotal@Sun.COM 	if (sp->opdata_cipher_key != CK_INVALID_HANDLE &&
22507526SVladimir.Kotal@Sun.COM 	    sp->opdata_encrypt == ctx->encrypt)
22510Sstevel@tonic-gate 		{
22520Sstevel@tonic-gate 		state->sp = sp;
22537211Sjp161948 		if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0)
22546847Svk199839 			return (0);
22556847Svk199839 
22566847Svk199839 		return (1);
22570Sstevel@tonic-gate 		}
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/* Check if the key has been invalidated. If so, a new key object
22600Sstevel@tonic-gate 	 * needs to be created.
22610Sstevel@tonic-gate 	 */
22627526SVladimir.Kotal@Sun.COM 	if (sp->opdata_cipher_key == CK_INVALID_HANDLE)
22630Sstevel@tonic-gate 		{
22647526SVladimir.Kotal@Sun.COM 		sp->opdata_cipher_key = pk11_get_cipher_key(
22657211Sjp161948 			ctx, key, p_ciph_table_row->key_type, sp);
22660Sstevel@tonic-gate 		}
22670Sstevel@tonic-gate 
22687526SVladimir.Kotal@Sun.COM 	if (sp->opdata_encrypt != ctx->encrypt && sp->opdata_encrypt != -1)
22690Sstevel@tonic-gate 		{
22700Sstevel@tonic-gate 		/* The previous encryption/decryption
22710Sstevel@tonic-gate 		 * is different. Need to terminate the previous
22720Sstevel@tonic-gate 		 * active encryption/decryption here
22730Sstevel@tonic-gate 		 */
22740Sstevel@tonic-gate 		if (!pk11_cipher_final(sp))
22750Sstevel@tonic-gate 			{
22767211Sjp161948 			pk11_return_session(sp, OP_CIPHER);
22770Sstevel@tonic-gate 			return 0;
22780Sstevel@tonic-gate 			}
22790Sstevel@tonic-gate 		}
22800Sstevel@tonic-gate 
22817526SVladimir.Kotal@Sun.COM 	if (sp->opdata_cipher_key == CK_INVALID_HANDLE)
22820Sstevel@tonic-gate 		{
22837211Sjp161948 		pk11_return_session(sp, OP_CIPHER);
22840Sstevel@tonic-gate 		return 0;
22850Sstevel@tonic-gate 		}
22860Sstevel@tonic-gate 
22876847Svk199839 	/* now initialize the context with a new key */
22887211Sjp161948 	if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0)
22896847Svk199839 		return (0);
22900Sstevel@tonic-gate 
22917526SVladimir.Kotal@Sun.COM 	sp->opdata_encrypt = ctx->encrypt;
22920Sstevel@tonic-gate 	state->sp = sp;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	return 1;
22950Sstevel@tonic-gate 	}
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate /* When reusing the same key in an encryption/decryption session for a
22980Sstevel@tonic-gate  * decryption/encryption session, we need to close the active session
22990Sstevel@tonic-gate  * and recreate a new one. Note that the key is in the global session so
23000Sstevel@tonic-gate  * that it needs not be recreated.
23010Sstevel@tonic-gate  *
23020Sstevel@tonic-gate  * It is more appropriate to use C_En/DecryptFinish here. At the time of this
23030Sstevel@tonic-gate  * development, these two functions in the PKCS#11 libraries used return
23040Sstevel@tonic-gate  * unexpected errors when passing in 0 length output. It may be a good
23050Sstevel@tonic-gate  * idea to try them again if performance is a problem here and fix
23060Sstevel@tonic-gate  * C_En/DecryptFinial if there are bugs there causing the problem.
23070Sstevel@tonic-gate  */
23080Sstevel@tonic-gate static int
23090Sstevel@tonic-gate pk11_cipher_final(PK11_SESSION *sp)
23100Sstevel@tonic-gate 	{
23110Sstevel@tonic-gate 	CK_RV rv;
23120Sstevel@tonic-gate 
23137526SVladimir.Kotal@Sun.COM 	rv = pFuncList->C_CloseSession(sp->session);
23140Sstevel@tonic-gate 	if (rv != CKR_OK)
23150Sstevel@tonic-gate 		{
23167211Sjp161948 		PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_CLOSESESSION, rv);
23170Sstevel@tonic-gate 		return 0;
23180Sstevel@tonic-gate 		}
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
23217526SVladimir.Kotal@Sun.COM 		NULL_PTR, NULL_PTR, &sp->session);
23220Sstevel@tonic-gate 	if (rv != CKR_OK)
23230Sstevel@tonic-gate 		{
23247211Sjp161948 		PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_OPENSESSION, rv);
23250Sstevel@tonic-gate 		return 0;
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	return 1;
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate /* An engine interface function. The calling function allocates sufficient
23320Sstevel@tonic-gate  * memory for the output buffer "out" to hold the results */
23330Sstevel@tonic-gate static int
23340Sstevel@tonic-gate pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
23350Sstevel@tonic-gate 	const unsigned char *in, unsigned int inl)
23360Sstevel@tonic-gate 	{
23370Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data;
23380Sstevel@tonic-gate 	PK11_SESSION *sp;
23390Sstevel@tonic-gate 	CK_RV rv;
23400Sstevel@tonic-gate 	unsigned long outl = inl;
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
23430Sstevel@tonic-gate 		return 0;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	sp = (PK11_SESSION *) state->sp;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	if (!inl)
23480Sstevel@tonic-gate 		return 1;
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	/* RC4 is the only stream cipher we support */
23510Sstevel@tonic-gate 	if (ctx->cipher->nid != NID_rc4 && (inl % ctx->cipher->block_size) != 0)
23520Sstevel@tonic-gate 		return 0;
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	if (ctx->encrypt)
23550Sstevel@tonic-gate 		{
23567526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_EncryptUpdate(sp->session,
23570Sstevel@tonic-gate 			(unsigned char *)in, inl, out, &outl);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 		if (rv != CKR_OK)
23600Sstevel@tonic-gate 			{
23617211Sjp161948 			PK11err_add_data(PK11_F_CIPHER_DO_CIPHER,
23627211Sjp161948 			    PK11_R_ENCRYPTUPDATE, rv);
23630Sstevel@tonic-gate 			return 0;
23640Sstevel@tonic-gate 			}
23650Sstevel@tonic-gate 		}
23660Sstevel@tonic-gate 	else
23670Sstevel@tonic-gate 		{
23687526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_DecryptUpdate(sp->session,
23690Sstevel@tonic-gate 			(unsigned char *)in, inl, out, &outl);
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 		if (rv != CKR_OK)
23720Sstevel@tonic-gate 			{
23737211Sjp161948 			PK11err_add_data(PK11_F_CIPHER_DO_CIPHER,
23747211Sjp161948 			    PK11_R_DECRYPTUPDATE, rv);
23750Sstevel@tonic-gate 			return 0;
23760Sstevel@tonic-gate 			}
23770Sstevel@tonic-gate 		}
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 	/* for DES_CBC, DES3_CBC, AES_CBC, and RC4, the output size is always
23800Sstevel@tonic-gate 	 * the same size of input
23810Sstevel@tonic-gate 	 * The application has guaranteed to call the block ciphers with
23820Sstevel@tonic-gate 	 * correctly aligned buffers.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	if (inl != outl)
23850Sstevel@tonic-gate 		return 0;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	return 1;
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23906847Svk199839 /*
23917211Sjp161948  * Return the session to the pool. Calling C_EncryptFinal() and C_DecryptFinal()
23927211Sjp161948  * here is the right thing because in EVP_DecryptFinal_ex(), engine's
23937211Sjp161948  * do_cipher() is not even called, and in EVP_EncryptFinal_ex() it is called but
23947211Sjp161948  * the engine can't find out that it's the finalizing call. We wouldn't
23957211Sjp161948  * necessarily have to finalize the context here since reinitializing it with
23967211Sjp161948  * C_(Encrypt|Decrypt)Init() should be fine but for the sake of correctness,
23977211Sjp161948  * let's do it. Some implementations might leak memory if the previously used
23987211Sjp161948  * context is initialized without finalizing it first.
23990Sstevel@tonic-gate  */
24000Sstevel@tonic-gate static int
24010Sstevel@tonic-gate pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx)
24020Sstevel@tonic-gate 	{
24036847Svk199839 	CK_RV rv;
24047211Sjp161948 	CK_ULONG len = EVP_MAX_BLOCK_LENGTH;
24056847Svk199839 	CK_BYTE buf[EVP_MAX_BLOCK_LENGTH];
24060Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = ctx->cipher_data;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	if (state != NULL && state->sp != NULL)
24090Sstevel@tonic-gate 		{
24106847Svk199839 		/*
24116847Svk199839 		 * We are not interested in the data here, we just need to get
24126847Svk199839 		 * rid of the context.
24136847Svk199839 		 */
24146847Svk199839 		if (ctx->encrypt)
24156847Svk199839 			rv = pFuncList->C_EncryptFinal(
24167526SVladimir.Kotal@Sun.COM 			    state->sp->session, buf, &len);
24176847Svk199839 		else
24186847Svk199839 			rv = pFuncList->C_DecryptFinal(
24197526SVladimir.Kotal@Sun.COM 			    state->sp->session, buf, &len);
24206847Svk199839 
24216847Svk199839 		if (rv != CKR_OK)
24226847Svk199839 			{
24237211Sjp161948 			PK11err_add_data(PK11_F_CIPHER_CLEANUP, ctx->encrypt ?
24247211Sjp161948 			    PK11_R_ENCRYPTFINAL : PK11_R_DECRYPTFINAL, rv);
24257211Sjp161948 			pk11_return_session(state->sp, OP_CIPHER);
24266847Svk199839 			return (0);
24276847Svk199839 			}
24286847Svk199839 
24297211Sjp161948 		pk11_return_session(state->sp, OP_CIPHER);
24300Sstevel@tonic-gate 		state->sp = NULL;
24310Sstevel@tonic-gate 		}
24320Sstevel@tonic-gate 
24336847Svk199839 	return (1);
24346847Svk199839 	}
24356847Svk199839 
24360Sstevel@tonic-gate /* Registered by the ENGINE when used to find out how to deal with
24370Sstevel@tonic-gate  * a particular NID in the ENGINE. This says what we'll do at the
24380Sstevel@tonic-gate  * top level - note, that list is restricted by what we answer with
24390Sstevel@tonic-gate  */
2440*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
24410Sstevel@tonic-gate static int
24420Sstevel@tonic-gate pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
24430Sstevel@tonic-gate 	const int **nids, int nid)
24440Sstevel@tonic-gate 	{
24450Sstevel@tonic-gate 	if (!cipher)
24460Sstevel@tonic-gate 		return (pk11_usable_ciphers(nids));
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	switch (nid)
24490Sstevel@tonic-gate 		{
24500Sstevel@tonic-gate 		case NID_des_ede3_cbc:
24510Sstevel@tonic-gate 			*cipher = &pk11_3des_cbc;
24520Sstevel@tonic-gate 			break;
24530Sstevel@tonic-gate 		case NID_des_cbc:
24540Sstevel@tonic-gate 			*cipher = &pk11_des_cbc;
24550Sstevel@tonic-gate 			break;
24567211Sjp161948 		case NID_des_ede3_ecb:
24577211Sjp161948 			*cipher = &pk11_3des_ecb;
24587211Sjp161948 			break;
24597211Sjp161948 		case NID_des_ecb:
24607211Sjp161948 			*cipher = &pk11_des_ecb;
24617211Sjp161948 			break;
24620Sstevel@tonic-gate 		case NID_aes_128_cbc:
24637211Sjp161948 			*cipher = &pk11_aes_128_cbc;
24647211Sjp161948 			break;
24657211Sjp161948 		case NID_aes_192_cbc:
24667211Sjp161948 			*cipher = &pk11_aes_192_cbc;
24677211Sjp161948 			break;
24687211Sjp161948 		case NID_aes_256_cbc:
24697211Sjp161948 			*cipher = &pk11_aes_256_cbc;
24707211Sjp161948 			break;
24717211Sjp161948 		case NID_aes_128_ecb:
24727211Sjp161948 			*cipher = &pk11_aes_128_ecb;
24737211Sjp161948 			break;
24747211Sjp161948 		case NID_aes_192_ecb:
24757211Sjp161948 			*cipher = &pk11_aes_192_ecb;
24767211Sjp161948 			break;
24777211Sjp161948 		case NID_aes_256_ecb:
24787211Sjp161948 			*cipher = &pk11_aes_256_ecb;
24797211Sjp161948 			break;
24807211Sjp161948 		case NID_bf_cbc:
24817211Sjp161948 			*cipher = &pk11_bf_cbc;
24820Sstevel@tonic-gate 			break;
24830Sstevel@tonic-gate 		case NID_rc4:
24840Sstevel@tonic-gate 			*cipher = &pk11_rc4;
24850Sstevel@tonic-gate 			break;
24860Sstevel@tonic-gate 		default:
24877211Sjp161948 #ifdef	SOLARIS_AES_CTR
24887211Sjp161948 			/*
24897211Sjp161948 			 * These can't be in separated cases because the NIDs
24907211Sjp161948 			 * here are not constants.
24917211Sjp161948 			 */
24927211Sjp161948 			if (nid == NID_aes_128_ctr)
24937211Sjp161948 				*cipher = &pk11_aes_128_ctr;
24947211Sjp161948 			else if (nid == NID_aes_192_ctr)
24957211Sjp161948 				*cipher = &pk11_aes_192_ctr;
24967211Sjp161948 			else if (nid == NID_aes_256_ctr)
24977211Sjp161948 				*cipher = &pk11_aes_256_ctr;
24987211Sjp161948 			else
24997211Sjp161948 #endif	/* SOLARIS_AES_CTR */
25000Sstevel@tonic-gate 			*cipher = NULL;
25010Sstevel@tonic-gate 			break;
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 	return (*cipher != NULL);
25040Sstevel@tonic-gate 	}
25050Sstevel@tonic-gate 
2506*7534SVladimir.Kotal@Sun.COM /* ARGSUSED */
25070Sstevel@tonic-gate static int
25080Sstevel@tonic-gate pk11_engine_digests(ENGINE *e, const EVP_MD **digest,
25090Sstevel@tonic-gate 	const int **nids, int nid)
25100Sstevel@tonic-gate 	{
25110Sstevel@tonic-gate 	if (!digest)
25120Sstevel@tonic-gate 		return (pk11_usable_digests(nids));
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	switch (nid)
25150Sstevel@tonic-gate 		{
25160Sstevel@tonic-gate 		case NID_md5:
25170Sstevel@tonic-gate 			*digest = &pk11_md5;
25180Sstevel@tonic-gate 			break;
25190Sstevel@tonic-gate 		case NID_sha1:
25200Sstevel@tonic-gate 			*digest = &pk11_sha1;
25210Sstevel@tonic-gate 			break;
25227211Sjp161948 		case NID_sha224:
25237211Sjp161948 			*digest = &pk11_sha224;
25247211Sjp161948 			break;
25257211Sjp161948 		case NID_sha256:
25267211Sjp161948 			*digest = &pk11_sha256;
25277211Sjp161948 			break;
25287211Sjp161948 		case NID_sha384:
25297211Sjp161948 			*digest = &pk11_sha384;
25307211Sjp161948 			break;
25317211Sjp161948 		case NID_sha512:
25327211Sjp161948 			*digest = &pk11_sha512;
25337211Sjp161948 			break;
25340Sstevel@tonic-gate 		default:
25350Sstevel@tonic-gate 			*digest = NULL;
25360Sstevel@tonic-gate 			break;
25370Sstevel@tonic-gate 		}
25380Sstevel@tonic-gate 	return (*digest != NULL);
25390Sstevel@tonic-gate 	}
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate /* Create a secret key object in a PKCS#11 session
25430Sstevel@tonic-gate  */
25440Sstevel@tonic-gate static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx,
25450Sstevel@tonic-gate 	const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp)
25460Sstevel@tonic-gate 	{
25470Sstevel@tonic-gate 	CK_RV rv;
25480Sstevel@tonic-gate 	CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
25490Sstevel@tonic-gate 	CK_OBJECT_CLASS obj_key = CKO_SECRET_KEY;
25500Sstevel@tonic-gate 	CK_ULONG ul_key_attr_count = 6;
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	CK_ATTRIBUTE  a_key_template[] =
25530Sstevel@tonic-gate 		{
25540Sstevel@tonic-gate 		{CKA_CLASS, (void*) NULL, sizeof(CK_OBJECT_CLASS)},
25550Sstevel@tonic-gate 		{CKA_KEY_TYPE, (void*) NULL, sizeof(CK_KEY_TYPE)},
25560Sstevel@tonic-gate 		{CKA_TOKEN, &false, sizeof(false)},
25570Sstevel@tonic-gate 		{CKA_ENCRYPT, &true, sizeof(true)},
25580Sstevel@tonic-gate 		{CKA_DECRYPT, &true, sizeof(true)},
25590Sstevel@tonic-gate 		{CKA_VALUE, (void*) NULL, 0},
25600Sstevel@tonic-gate 		};
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	/* Create secret key object in global_session. All other sessions
25630Sstevel@tonic-gate 	 * can use the key handles. Here is why:
25640Sstevel@tonic-gate 	 * OpenSSL will call EncryptInit and EncryptUpdate using a secret key.
25650Sstevel@tonic-gate 	 * It may then call DecryptInit and DecryptUpdate using the same key.
25660Sstevel@tonic-gate 	 * To use the same key object, we need to call EncryptFinal with
25670Sstevel@tonic-gate 	 * a 0 length message. Currently, this does not work for 3DES
25680Sstevel@tonic-gate 	 * mechanism. To get around this problem, we close the session and
25690Sstevel@tonic-gate 	 * then create a new session to use the same key object. When a session
25700Sstevel@tonic-gate 	 * is closed, all the object handles will be invalid. Thus, create key
25710Sstevel@tonic-gate 	 * objects in a global session, an individual session may be closed to
25720Sstevel@tonic-gate 	 * terminate the active operation.
25730Sstevel@tonic-gate 	 */
25740Sstevel@tonic-gate 	CK_SESSION_HANDLE session = global_session;
25750Sstevel@tonic-gate 	a_key_template[0].pValue = &obj_key;
25760Sstevel@tonic-gate 	a_key_template[1].pValue = &key_type;
25770Sstevel@tonic-gate 	a_key_template[5].pValue = (void *) key;
25780Sstevel@tonic-gate 	a_key_template[5].ulValueLen = (unsigned long) ctx->key_len;
25790Sstevel@tonic-gate 
25804602Sjp161948 	rv = pFuncList->C_CreateObject(session,
25814602Sjp161948 		a_key_template, ul_key_attr_count, &h_key);
25820Sstevel@tonic-gate 	if (rv != CKR_OK)
25830Sstevel@tonic-gate 		{
25847211Sjp161948 		PK11err_add_data(PK11_F_GET_CIPHER_KEY, PK11_R_CREATEOBJECT,
25857211Sjp161948 		    rv);
25860Sstevel@tonic-gate 		goto err;
25870Sstevel@tonic-gate 		}
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	/* Save the key information used in this session.
25900Sstevel@tonic-gate 	 * The max can be saved is PK11_KEY_LEN_MAX.
25910Sstevel@tonic-gate 	 */
25927526SVladimir.Kotal@Sun.COM 	sp->opdata_key_len = ctx->key_len > PK11_KEY_LEN_MAX ?
25930Sstevel@tonic-gate 		PK11_KEY_LEN_MAX : ctx->key_len;
2594*7534SVladimir.Kotal@Sun.COM 	(void) memcpy(sp->opdata_key, key, sp->opdata_key_len);
25950Sstevel@tonic-gate err:
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	return h_key;
25980Sstevel@tonic-gate 	}
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate static int
26010Sstevel@tonic-gate md_nid_to_pk11(int nid)
26020Sstevel@tonic-gate 	{
26030Sstevel@tonic-gate 	int i;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	for (i = 0; i < PK11_DIGEST_MAX; i++)
26060Sstevel@tonic-gate 		if (digests[i].nid == nid)
26070Sstevel@tonic-gate 			return (digests[i].id);
26080Sstevel@tonic-gate 	return (-1);
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate static int
26120Sstevel@tonic-gate pk11_digest_init(EVP_MD_CTX *ctx)
26130Sstevel@tonic-gate         {
26140Sstevel@tonic-gate 	CK_RV rv;
26154320Sjp161948 	CK_MECHANISM mech;
26160Sstevel@tonic-gate 	int index;
26170Sstevel@tonic-gate 	PK11_SESSION *sp;
26180Sstevel@tonic-gate 	PK11_DIGEST *pdp;
26190Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	state->sp = NULL;
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 	index = md_nid_to_pk11(ctx->digest->type);
26240Sstevel@tonic-gate 	if (index < 0 || index >= PK11_DIGEST_MAX)
26250Sstevel@tonic-gate 		return 0;
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	pdp = &digests[index];
26287211Sjp161948 	if ((sp = pk11_get_session(OP_DIGEST)) == NULL)
26290Sstevel@tonic-gate 		return 0;
26300Sstevel@tonic-gate 
26314320Sjp161948 	/* at present, no parameter is needed for supported digests */
26324320Sjp161948 	mech.mechanism = pdp->mech_type;
26334320Sjp161948 	mech.pParameter = NULL;
26344320Sjp161948 	mech.ulParameterLen = 0;
26354320Sjp161948 
26364320Sjp161948 	rv = pFuncList->C_DigestInit(sp->session, &mech);
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	if (rv != CKR_OK)
26390Sstevel@tonic-gate 		{
26407211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_INIT, PK11_R_DIGESTINIT, rv);
26417211Sjp161948 		pk11_return_session(sp, OP_DIGEST);
26420Sstevel@tonic-gate 		return 0;
26430Sstevel@tonic-gate 		}
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	state->sp = sp;
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	return 1;
26480Sstevel@tonic-gate 	}
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate static int
26512139Sjp161948 pk11_digest_update(EVP_MD_CTX *ctx,const void *data,size_t count)
26520Sstevel@tonic-gate         {
26530Sstevel@tonic-gate 	CK_RV rv;
26540Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data;
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/* 0 length message will cause a failure in C_DigestFinal */
26570Sstevel@tonic-gate 	if (count == 0)
26580Sstevel@tonic-gate 		return 1;
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
26610Sstevel@tonic-gate 		return 0;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	rv = pFuncList->C_DigestUpdate(state->sp->session, (CK_BYTE *) data,
26640Sstevel@tonic-gate 		count);
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate 	if (rv != CKR_OK)
26670Sstevel@tonic-gate 		{
26687211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_UPDATE, PK11_R_DIGESTUPDATE, rv);
26697211Sjp161948 		pk11_return_session(state->sp, OP_DIGEST);
26700Sstevel@tonic-gate 		state->sp = NULL;
26710Sstevel@tonic-gate 		return 0;
26720Sstevel@tonic-gate 		}
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	return 1;
26750Sstevel@tonic-gate 	}
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate static int
26780Sstevel@tonic-gate pk11_digest_final(EVP_MD_CTX *ctx,unsigned char *md)
26790Sstevel@tonic-gate         {
26800Sstevel@tonic-gate 	CK_RV rv;
26810Sstevel@tonic-gate 	unsigned long len;
26820Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data;
26830Sstevel@tonic-gate 	len = ctx->digest->md_size;
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
26860Sstevel@tonic-gate 		return 0;
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	rv = pFuncList->C_DigestFinal(state->sp->session, md, &len);
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	if (rv != CKR_OK)
26910Sstevel@tonic-gate 		{
26927211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_FINAL, PK11_R_DIGESTFINAL, rv);
26937211Sjp161948 		pk11_return_session(state->sp, OP_DIGEST);
26940Sstevel@tonic-gate 		state->sp = NULL;
26950Sstevel@tonic-gate 		return 0;
26960Sstevel@tonic-gate 		}
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 	if (ctx->digest->md_size != len)
26990Sstevel@tonic-gate 		return 0;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 	/* Final is called and digest is returned, so return the session
27020Sstevel@tonic-gate 	 * to the pool
27030Sstevel@tonic-gate 	 */
27047211Sjp161948 	pk11_return_session(state->sp, OP_DIGEST);
27050Sstevel@tonic-gate 	state->sp = NULL;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	return 1;
27080Sstevel@tonic-gate 	}
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate static int
27110Sstevel@tonic-gate pk11_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
27120Sstevel@tonic-gate         {
27130Sstevel@tonic-gate 	CK_RV rv;
27140Sstevel@tonic-gate 	int ret = 0;
27150Sstevel@tonic-gate 	PK11_CIPHER_STATE *state, *state_to;
27160Sstevel@tonic-gate 	CK_BYTE_PTR pstate = NULL;
27170Sstevel@tonic-gate 	CK_ULONG ul_state_len;
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	/* The copy-from state */
27200Sstevel@tonic-gate 	state = (PK11_CIPHER_STATE *) from->md_data;
27210Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
27220Sstevel@tonic-gate 		goto err;
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	/* Initialize the copy-to state */
27250Sstevel@tonic-gate 	if (!pk11_digest_init(to))
27260Sstevel@tonic-gate 		goto err;
27270Sstevel@tonic-gate 	state_to = (PK11_CIPHER_STATE *) to->md_data;
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	/* Get the size of the operation state of the copy-from session */
27300Sstevel@tonic-gate 	rv = pFuncList->C_GetOperationState(state->sp->session, NULL,
27310Sstevel@tonic-gate 		&ul_state_len);
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	if (rv != CKR_OK)
27340Sstevel@tonic-gate 		{
27357211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE,
27367211Sjp161948 		    rv);
27370Sstevel@tonic-gate 		goto err;
27380Sstevel@tonic-gate 		}
27390Sstevel@tonic-gate 	if (ul_state_len == 0)
27400Sstevel@tonic-gate 		{
27410Sstevel@tonic-gate 		goto err;
27420Sstevel@tonic-gate 		}
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 	pstate = OPENSSL_malloc(ul_state_len);
27450Sstevel@tonic-gate 	if (pstate == NULL)
27460Sstevel@tonic-gate 		{
27476847Svk199839 		PK11err(PK11_F_DIGEST_COPY, PK11_R_MALLOC_FAILURE);
27480Sstevel@tonic-gate 		goto err;
27490Sstevel@tonic-gate 		}
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	/* Get the operation state of the copy-from session */
27520Sstevel@tonic-gate 	rv = pFuncList->C_GetOperationState(state->sp->session, pstate,
27530Sstevel@tonic-gate 		&ul_state_len);
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate 	if (rv != CKR_OK)
27560Sstevel@tonic-gate 		{
27577211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE,
27587211Sjp161948 		    rv);
27590Sstevel@tonic-gate 		goto err;
27600Sstevel@tonic-gate 		}
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	/* Set the operation state of the copy-to session */
27630Sstevel@tonic-gate 	rv = pFuncList->C_SetOperationState(state_to->sp->session, pstate,
27640Sstevel@tonic-gate 		ul_state_len, 0, 0);
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	if (rv != CKR_OK)
27670Sstevel@tonic-gate 		{
27687211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_SET_OPERATION_STATE, rv);
27690Sstevel@tonic-gate 		goto err;
27700Sstevel@tonic-gate 		}
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate 	ret = 1;
27730Sstevel@tonic-gate err:
27740Sstevel@tonic-gate 	if (pstate != NULL)
27750Sstevel@tonic-gate 		OPENSSL_free(pstate);
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	return ret;
27780Sstevel@tonic-gate 	}
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate /* Return any pending session state to the pool */
27810Sstevel@tonic-gate static int
27820Sstevel@tonic-gate pk11_digest_cleanup(EVP_MD_CTX *ctx)
27830Sstevel@tonic-gate 	{
27840Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = ctx->md_data;
27854602Sjp161948 	unsigned char buf[EVP_MAX_MD_SIZE];
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	if (state != NULL && state->sp != NULL)
27880Sstevel@tonic-gate 		{
27894602Sjp161948 		/*
27904602Sjp161948 		 * If state->sp is not NULL then pk11_digest_final() has not
27914602Sjp161948 		 * been called yet. We must call it now to free any memory
27924602Sjp161948 		 * that might have been allocated in the token when
27934602Sjp161948 		 * pk11_digest_init() was called.
27944602Sjp161948 		 */
27957526SVladimir.Kotal@Sun.COM 		(void) pk11_digest_final(ctx, buf);
27967211Sjp161948 		pk11_return_session(state->sp, OP_DIGEST);
27970Sstevel@tonic-gate 		state->sp = NULL;
27980Sstevel@tonic-gate 		}
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	return 1;
28010Sstevel@tonic-gate 	}
28020Sstevel@tonic-gate 
28036847Svk199839 /*
28047211Sjp161948  * Check if the new key is the same as the key object in the session. If the key
28057211Sjp161948  * is the same, no need to create a new key object. Otherwise, the old key
28067211Sjp161948  * object needs to be destroyed and a new one will be created. Return 1 for
28077211Sjp161948  * cache hit, 0 for cache miss. Note that we must check the key length first
28087211Sjp161948  * otherwise we could end up reusing a different, longer key with the same
28097211Sjp161948  * prefix.
28100Sstevel@tonic-gate  */
28117211Sjp161948 static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key,
28127211Sjp161948 	int key_len)
28130Sstevel@tonic-gate 	{
28147526SVladimir.Kotal@Sun.COM 	if (sp->opdata_key_len != key_len ||
28157526SVladimir.Kotal@Sun.COM 	    memcmp(sp->opdata_key, key, key_len) != 0)
28166847Svk199839 		{
28177211Sjp161948 		(void) pk11_destroy_cipher_key_objects(sp);
28186847Svk199839 		return (0);
28196847Svk199839 		}
28206847Svk199839 	return (1);
28210Sstevel@tonic-gate 	}
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate /* Destroy one or more secret key objects.
28240Sstevel@tonic-gate  */
28250Sstevel@tonic-gate static int pk11_destroy_cipher_key_objects(PK11_SESSION *session)
28260Sstevel@tonic-gate 	{
28270Sstevel@tonic-gate 	int ret = 0;
28280Sstevel@tonic-gate 	PK11_SESSION *sp = NULL;
28290Sstevel@tonic-gate 	PK11_SESSION *local_free_session;
28300Sstevel@tonic-gate 
28317526SVladimir.Kotal@Sun.COM 	if (session != NULL)
28320Sstevel@tonic-gate 		local_free_session = session;
28330Sstevel@tonic-gate 	else
28347526SVladimir.Kotal@Sun.COM 		{
28357526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_CIPHER].lock);
28367526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_CIPHER].head;
28377526SVladimir.Kotal@Sun.COM 		}
28387526SVladimir.Kotal@Sun.COM 
28390Sstevel@tonic-gate 	while ((sp = local_free_session) != NULL)
28400Sstevel@tonic-gate 		{
28410Sstevel@tonic-gate 		local_free_session = sp->next;
28420Sstevel@tonic-gate 
28437526SVladimir.Kotal@Sun.COM 		if (sp->opdata_cipher_key != CK_INVALID_HANDLE)
28440Sstevel@tonic-gate 			{
28450Sstevel@tonic-gate 			/* The secret key object is created in the
28460Sstevel@tonic-gate 			 * global_session. See pk11_get_cipher_key
28470Sstevel@tonic-gate 			 */
28480Sstevel@tonic-gate 			if (pk11_destroy_object(global_session,
28497526SVladimir.Kotal@Sun.COM 				sp->opdata_cipher_key) == 0)
28500Sstevel@tonic-gate 				goto err;
28517526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key = CK_INVALID_HANDLE;
28520Sstevel@tonic-gate 			}
28530Sstevel@tonic-gate 		}
28540Sstevel@tonic-gate 	ret = 1;
28550Sstevel@tonic-gate err:
28567526SVladimir.Kotal@Sun.COM 
28577526SVladimir.Kotal@Sun.COM 	if (session == NULL)
28587526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_CIPHER].lock);
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	return ret;
28610Sstevel@tonic-gate 	}
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate /*
28657211Sjp161948  * Public key mechanisms optionally supported
28660Sstevel@tonic-gate  *
28670Sstevel@tonic-gate  * CKM_RSA_X_509
28680Sstevel@tonic-gate  * CKM_RSA_PKCS
28690Sstevel@tonic-gate  * CKM_DSA
28700Sstevel@tonic-gate  *
28717211Sjp161948  * The first slot that supports at least one of those mechanisms is chosen as a
28727211Sjp161948  * public key slot.
28730Sstevel@tonic-gate  *
28740Sstevel@tonic-gate  * Symmetric ciphers optionally supported
28750Sstevel@tonic-gate  *
28760Sstevel@tonic-gate  * CKM_DES3_CBC
28770Sstevel@tonic-gate  * CKM_DES_CBC
28780Sstevel@tonic-gate  * CKM_AES_CBC
28797211Sjp161948  * CKM_DES3_ECB
28807211Sjp161948  * CKM_DES_ECB
28817211Sjp161948  * CKM_AES_ECB
28827211Sjp161948  * CKM_AES_CTR
28830Sstevel@tonic-gate  * CKM_RC4
28847211Sjp161948  * CKM_BLOWFISH_CBC
28850Sstevel@tonic-gate  *
28860Sstevel@tonic-gate  * Digests optionally supported
28870Sstevel@tonic-gate  *
28880Sstevel@tonic-gate  * CKM_MD5
28890Sstevel@tonic-gate  * CKM_SHA_1
28907211Sjp161948  * CKM_SHA224
28917211Sjp161948  * CKM_SHA256
28927211Sjp161948  * CKM_SHA384
28937211Sjp161948  * CKM_SHA512
28947211Sjp161948  *
28957211Sjp161948  * The output of this function is a set of global variables indicating which
28967211Sjp161948  * mechanisms from RSA, DSA, DH and RAND are present, and also two arrays of
28977211Sjp161948  * mechanisms, one for symmetric ciphers and one for digests. Also, 3 global
28987211Sjp161948  * variables carry information about which slot was chosen for (a) public key
28997211Sjp161948  * mechanisms, (b) random operations, and (c) symmetric ciphers and digests.
29000Sstevel@tonic-gate  */
29010Sstevel@tonic-gate static int
29027211Sjp161948 pk11_choose_slots(int *any_slot_found)
29030Sstevel@tonic-gate 	{
29040Sstevel@tonic-gate 	CK_SLOT_ID_PTR pSlotList = NULL_PTR;
29050Sstevel@tonic-gate 	CK_ULONG ulSlotCount = 0;
29060Sstevel@tonic-gate 	CK_MECHANISM_INFO mech_info;
29070Sstevel@tonic-gate 	CK_TOKEN_INFO token_info;
29080Sstevel@tonic-gate 	int i;
29090Sstevel@tonic-gate 	CK_RV rv;
29100Sstevel@tonic-gate 	CK_SLOT_ID best_slot_sofar;
29110Sstevel@tonic-gate 	CK_BBOOL found_candidate_slot = CK_FALSE;
29120Sstevel@tonic-gate 	int slot_n_cipher = 0;
29130Sstevel@tonic-gate 	int slot_n_digest = 0;
29140Sstevel@tonic-gate 	CK_SLOT_ID current_slot = 0;
29150Sstevel@tonic-gate 	int current_slot_n_cipher = 0;
29160Sstevel@tonic-gate 	int current_slot_n_digest = 0;
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	int local_cipher_nids[PK11_CIPHER_MAX];
29190Sstevel@tonic-gate 	int local_digest_nids[PK11_DIGEST_MAX];
29207211Sjp161948 
29217211Sjp161948 	/* let's initialize the output parameter */
29227211Sjp161948 	if (any_slot_found != NULL)
29237211Sjp161948 		*any_slot_found = 0;
29247211Sjp161948 
29257211Sjp161948 	/* Get slot list for memory allocation */
29260Sstevel@tonic-gate 	rv = pFuncList->C_GetSlotList(0, NULL_PTR, &ulSlotCount);
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 	if (rv != CKR_OK)
29290Sstevel@tonic-gate 		{
29307211Sjp161948 		PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv);
29317211Sjp161948 		return 0;
29320Sstevel@tonic-gate 		}
29330Sstevel@tonic-gate 
29347211Sjp161948 	/* it's not an error if we didn't find any providers */
29350Sstevel@tonic-gate 	if (ulSlotCount == 0)
29360Sstevel@tonic-gate 		{
29377211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29387211Sjp161948 		fprintf(stderr, "%s: no crypto providers found\n", PK11_DBG);
29397211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29407211Sjp161948 		return 1;
29410Sstevel@tonic-gate 		}
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID));
29440Sstevel@tonic-gate 
29450Sstevel@tonic-gate 	if (pSlotList == NULL)
29460Sstevel@tonic-gate 		{
29476847Svk199839 		PK11err(PK11_F_CHOOSE_SLOT, PK11_R_MALLOC_FAILURE);
29487211Sjp161948 		return 0;
29490Sstevel@tonic-gate 		}
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate 	/* Get the slot list for processing */
29520Sstevel@tonic-gate 	rv = pFuncList->C_GetSlotList(0, pSlotList, &ulSlotCount);
29530Sstevel@tonic-gate 	if (rv != CKR_OK)
29540Sstevel@tonic-gate 		{
29557211Sjp161948 		PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv);
29560Sstevel@tonic-gate 		OPENSSL_free(pSlotList);
29577211Sjp161948 		return 0;
29580Sstevel@tonic-gate 		}
29590Sstevel@tonic-gate 
29607211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29617211Sjp161948 	fprintf(stderr, "%s: provider: %s\n", PK11_DBG, def_PK11_LIBNAME);
29627211Sjp161948 	fprintf(stderr, "%s: number of slots: %d\n", PK11_DBG, ulSlotCount);
29637211Sjp161948 
29647211Sjp161948 	fprintf(stderr, "%s: == checking rand slots ==\n", PK11_DBG);
29657211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29667211Sjp161948 	for (i = 0; i < ulSlotCount; i++)
29677211Sjp161948 		{
29687211Sjp161948 		current_slot = pSlotList[i];
29697211Sjp161948 
29707211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29717211Sjp161948 	fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i);
29727211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29737211Sjp161948 		/* Check if slot has random support. */
29747211Sjp161948 		rv = pFuncList->C_GetTokenInfo(current_slot, &token_info);
29757211Sjp161948 		if (rv != CKR_OK)
29767211Sjp161948 			continue;
29777211Sjp161948 
29787211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29797211Sjp161948 	fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label);
29807211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29817211Sjp161948 
29827211Sjp161948 		if (token_info.flags & CKF_RNG)
29837211Sjp161948 			{
29847211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29857211Sjp161948 	fprintf(stderr, "%s: this token has CKF_RNG flag\n", PK11_DBG);
29867211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29877211Sjp161948 			pk11_have_random = CK_TRUE;
29887211Sjp161948 			break;
29897211Sjp161948 			}
29907211Sjp161948 		}
29917211Sjp161948 
29927211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29937211Sjp161948 	fprintf(stderr, "%s: == checking pubkey slots ==\n", PK11_DBG);
29947211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29950Sstevel@tonic-gate 	for (i = 0; i < ulSlotCount; i++)
29960Sstevel@tonic-gate 		{
29970Sstevel@tonic-gate 		CK_BBOOL slot_has_rsa = CK_FALSE;
29980Sstevel@tonic-gate 		CK_BBOOL slot_has_dsa = CK_FALSE;
29990Sstevel@tonic-gate 		CK_BBOOL slot_has_dh = CK_FALSE;
30000Sstevel@tonic-gate 		current_slot = pSlotList[i];
30017211Sjp161948 
30027211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30037211Sjp161948 	fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i);
30047211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30050Sstevel@tonic-gate 		rv = pFuncList->C_GetTokenInfo(current_slot, &token_info);
30060Sstevel@tonic-gate 		if (rv != CKR_OK)
30070Sstevel@tonic-gate 			continue;
30080Sstevel@tonic-gate 
30097211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30107211Sjp161948 	fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label);
30117211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30127211Sjp161948 
30136847Svk199839 #ifndef OPENSSL_NO_RSA
30140Sstevel@tonic-gate 		/*
30150Sstevel@tonic-gate 		 * Check if this slot is capable of signing and
30160Sstevel@tonic-gate 		 * verifying with CKM_RSA_PKCS.
30170Sstevel@tonic-gate 		 */
30180Sstevel@tonic-gate 		rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_RSA_PKCS,
30190Sstevel@tonic-gate 			&mech_info);
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 		if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) &&
30220Sstevel@tonic-gate 				(mech_info.flags & CKF_VERIFY)))
30230Sstevel@tonic-gate 			{
30240Sstevel@tonic-gate 			/*
30250Sstevel@tonic-gate 			 * Check if this slot is capable of encryption,
30260Sstevel@tonic-gate 			 * decryption, sign, and verify with CKM_RSA_X_509.
30270Sstevel@tonic-gate 			 */
30280Sstevel@tonic-gate 			rv = pFuncList->C_GetMechanismInfo(current_slot,
30290Sstevel@tonic-gate 			  CKM_RSA_X_509, &mech_info);
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 			if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) &&
30320Sstevel@tonic-gate 			    (mech_info.flags & CKF_VERIFY) &&
30330Sstevel@tonic-gate 			    (mech_info.flags & CKF_ENCRYPT) &&
30340Sstevel@tonic-gate 			    (mech_info.flags & CKF_VERIFY_RECOVER) &&
30350Sstevel@tonic-gate 			    (mech_info.flags & CKF_DECRYPT)))
30367211Sjp161948 				{
30370Sstevel@tonic-gate 				slot_has_rsa = CK_TRUE;
30387211Sjp161948 				}
30390Sstevel@tonic-gate 			}
30407211Sjp161948 #endif	/* OPENSSL_NO_RSA */
30417211Sjp161948 
30426847Svk199839 #ifndef OPENSSL_NO_DSA
30430Sstevel@tonic-gate 		/*
30440Sstevel@tonic-gate 		 * Check if this slot is capable of signing and
30450Sstevel@tonic-gate 		 * verifying with CKM_DSA.
30460Sstevel@tonic-gate 		 */
30470Sstevel@tonic-gate 		rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_DSA,
30480Sstevel@tonic-gate 			&mech_info);
30490Sstevel@tonic-gate 		if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) &&
30500Sstevel@tonic-gate 		    (mech_info.flags & CKF_VERIFY)))
30517211Sjp161948 			{
30520Sstevel@tonic-gate 			slot_has_dsa = CK_TRUE;
30537211Sjp161948 			}
30547526SVladimir.Kotal@Sun.COM 
30557211Sjp161948 #endif	/* OPENSSL_NO_DSA */
30567211Sjp161948 
30576847Svk199839 #ifndef OPENSSL_NO_DH
30580Sstevel@tonic-gate 		/*
30590Sstevel@tonic-gate 		 * Check if this slot is capable of DH key generataion and
30600Sstevel@tonic-gate 		 * derivation.
30610Sstevel@tonic-gate 		 */
30620Sstevel@tonic-gate 		rv = pFuncList->C_GetMechanismInfo(current_slot,
30630Sstevel@tonic-gate 		  CKM_DH_PKCS_KEY_PAIR_GEN, &mech_info);
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate 		if (rv == CKR_OK && (mech_info.flags & CKF_GENERATE_KEY_PAIR))
30660Sstevel@tonic-gate 			{
30670Sstevel@tonic-gate 			rv = pFuncList->C_GetMechanismInfo(current_slot,
30680Sstevel@tonic-gate 				CKM_DH_PKCS_DERIVE, &mech_info);
30690Sstevel@tonic-gate 			if (rv == CKR_OK && (mech_info.flags & CKF_DERIVE))
30707211Sjp161948 				{
30710Sstevel@tonic-gate 				slot_has_dh = CK_TRUE;
30727211Sjp161948 				}
30730Sstevel@tonic-gate 			}
30747211Sjp161948 #endif	/* OPENSSL_NO_DH */
30757211Sjp161948 
30760Sstevel@tonic-gate 		if (!found_candidate_slot &&
30770Sstevel@tonic-gate 		    (slot_has_rsa || slot_has_dsa || slot_has_dh))
30780Sstevel@tonic-gate 			{
30797211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30800Sstevel@tonic-gate 			fprintf(stderr,
30817211Sjp161948 			  "%s: potential slot: %d\n", PK11_DBG, current_slot);
30827211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30830Sstevel@tonic-gate 			best_slot_sofar = current_slot;
30840Sstevel@tonic-gate 			pk11_have_rsa = slot_has_rsa;
30850Sstevel@tonic-gate 			pk11_have_dsa = slot_has_dsa;
30860Sstevel@tonic-gate 			pk11_have_dh = slot_has_dh;
30870Sstevel@tonic-gate 			found_candidate_slot = CK_TRUE;
30887211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30897211Sjp161948 			fprintf(stderr,
30907211Sjp161948 		            "%s: setting found_candidate_slot to CK_TRUE\n",
30917211Sjp161948 			    PK11_DBG);
30920Sstevel@tonic-gate 			fprintf(stderr,
30937211Sjp161948 		            "%s: best so far slot: %d\n", PK11_DBG,
30940Sstevel@tonic-gate 		    	    best_slot_sofar);
30957211Sjp161948 			}
30967211Sjp161948 		else
30977211Sjp161948 			{
30987211Sjp161948 			fprintf(stderr,
30997211Sjp161948 			  "%s: no rsa/dsa/dh\n", PK11_DBG);
31000Sstevel@tonic-gate 			}
31017211Sjp161948 #else
31027211Sjp161948 			} /* if */
31037211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31047211Sjp161948 		} /* for */
31057211Sjp161948 
31067211Sjp161948 	if (found_candidate_slot)
31077211Sjp161948 		{
31087211Sjp161948 		pubkey_SLOTID = best_slot_sofar;
31097211Sjp161948 		}
31107211Sjp161948 
31117211Sjp161948 	found_candidate_slot = CK_FALSE;
31127211Sjp161948 	best_slot_sofar = 0;
31137211Sjp161948 
31147211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31157211Sjp161948 	fprintf(stderr, "%s: == checking cipher/digest ==\n", PK11_DBG);
31167211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31177211Sjp161948 	for (i = 0; i < ulSlotCount; i++)
31187211Sjp161948 		{
31197211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31207211Sjp161948 	fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i);
31217211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31227211Sjp161948 
31237211Sjp161948 		current_slot = pSlotList[i];
31247211Sjp161948 		current_slot_n_cipher = 0;
31257211Sjp161948 		current_slot_n_digest = 0;
3126*7534SVladimir.Kotal@Sun.COM 		(void) memset(local_cipher_nids, 0, sizeof(local_cipher_nids));
3127*7534SVladimir.Kotal@Sun.COM 		(void) memset(local_digest_nids, 0, sizeof(local_digest_nids));
31287211Sjp161948 
31297211Sjp161948 		pk11_find_symmetric_ciphers(pFuncList, current_slot,
31307211Sjp161948 		    &current_slot_n_cipher, local_cipher_nids);
31317211Sjp161948 
31327211Sjp161948 		pk11_find_digests(pFuncList, current_slot,
31337211Sjp161948 		    &current_slot_n_digest, local_digest_nids);
31347211Sjp161948 
31357211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31367211Sjp161948 		fprintf(stderr, "%s: current_slot_n_cipher %d\n", PK11_DBG,
31377211Sjp161948 			current_slot_n_cipher);
31387211Sjp161948 		fprintf(stderr, "%s: current_slot_n_digest %d\n", PK11_DBG,
31397211Sjp161948 			current_slot_n_digest);
31407211Sjp161948 		fprintf(stderr, "%s: best so far cipher/digest slot: %d\n",
31417211Sjp161948 			PK11_DBG, best_slot_sofar);
31427211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 		/*
31450Sstevel@tonic-gate 		 * If the current slot supports more ciphers/digests than
31467250Sjp161948 		 * the previous best one we change the current best to this one,
31470Sstevel@tonic-gate 		 * otherwise leave it where it is.
31480Sstevel@tonic-gate 		 */
31497250Sjp161948 		if ((current_slot_n_cipher + current_slot_n_digest) >
31507250Sjp161948 		    (slot_n_cipher + slot_n_digest))
31510Sstevel@tonic-gate 			{
31527211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31537211Sjp161948 			fprintf(stderr,
31547211Sjp161948 				"%s: changing best so far slot to %d\n",
31557211Sjp161948 				PK11_DBG, current_slot);
31567211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31577211Sjp161948 			best_slot_sofar = SLOTID = current_slot;
31587250Sjp161948 			cipher_count = slot_n_cipher = current_slot_n_cipher;
31597250Sjp161948 			digest_count = slot_n_digest = current_slot_n_digest;
3160*7534SVladimir.Kotal@Sun.COM 			(void) memcpy(cipher_nids, local_cipher_nids,
31617526SVladimir.Kotal@Sun.COM 			    sizeof (local_cipher_nids));
3162*7534SVladimir.Kotal@Sun.COM 			(void) memcpy(digest_nids, local_digest_nids,
31637526SVladimir.Kotal@Sun.COM 			    sizeof (local_digest_nids));
31640Sstevel@tonic-gate 			}
31650Sstevel@tonic-gate 		}
31660Sstevel@tonic-gate 
31677211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31687211Sjp161948 	fprintf(stderr,
31697526SVladimir.Kotal@Sun.COM 	    "%s: chosen pubkey slot: %d\n", PK11_DBG, pubkey_SLOTID);
31700Sstevel@tonic-gate 	fprintf(stderr,
31717526SVladimir.Kotal@Sun.COM 	    "%s: chosen rand slot: %d\n", PK11_DBG, rand_SLOTID);
31720Sstevel@tonic-gate 	fprintf(stderr,
31737526SVladimir.Kotal@Sun.COM 	    "%s: chosen cipher/digest slot: %d\n", PK11_DBG, SLOTID);
31747211Sjp161948 	fprintf(stderr,
31757526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_rsa %d\n", PK11_DBG, pk11_have_rsa);
31760Sstevel@tonic-gate 	fprintf(stderr,
31777526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_dsa %d\n", PK11_DBG, pk11_have_dsa);
31787211Sjp161948 	fprintf(stderr,
31797526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_dh %d\n", PK11_DBG, pk11_have_dh);
31800Sstevel@tonic-gate 	fprintf(stderr,
31817526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_random %d\n", PK11_DBG, pk11_have_random);
31820Sstevel@tonic-gate 	fprintf(stderr,
31837526SVladimir.Kotal@Sun.COM 	    "%s: cipher_count %d\n", PK11_DBG, cipher_count);
31847211Sjp161948 	fprintf(stderr,
31857526SVladimir.Kotal@Sun.COM 	    "%s: digest_count %d\n", PK11_DBG, digest_count);
31867211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31870Sstevel@tonic-gate 
31886847Svk199839 	if (pSlotList != NULL)
31896847Svk199839 		OPENSSL_free(pSlotList);
31900Sstevel@tonic-gate 
31917211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
31927211Sjp161948 	OPENSSL_free(hw_cnids);
31937211Sjp161948 	OPENSSL_free(hw_dnids);
31947211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
31957211Sjp161948 
31967211Sjp161948 	if (any_slot_found != NULL)
31977211Sjp161948 		*any_slot_found = 1;
31987211Sjp161948 	return 1;
31990Sstevel@tonic-gate 	}
32000Sstevel@tonic-gate 
32017211Sjp161948 static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR pflist,
32027211Sjp161948     int slot_id, CK_MECHANISM_TYPE mech, int *current_slot_n_cipher,
32037211Sjp161948     int *local_cipher_nids, int id)
32047211Sjp161948 	{
32057211Sjp161948 	CK_MECHANISM_INFO mech_info;
32067211Sjp161948 	CK_RV rv;
32077211Sjp161948 
32087211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32097211Sjp161948 	fprintf(stderr, "%s: checking mech: %x", PK11_DBG, mech);
32107211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32117211Sjp161948 	rv = pflist->C_GetMechanismInfo(slot_id, mech, &mech_info);
32127211Sjp161948 
32137211Sjp161948 	if (rv != CKR_OK)
32147211Sjp161948 		{
32157211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32167211Sjp161948 		fprintf(stderr, " not found\n");
32177211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32187211Sjp161948 		return;
32197211Sjp161948 		}
32207211Sjp161948 
32217211Sjp161948 	if ((mech_info.flags & CKF_ENCRYPT) &&
32227211Sjp161948 	    (mech_info.flags & CKF_DECRYPT))
32237211Sjp161948 		{
32247211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32257211Sjp161948 		if (nid_in_table(ciphers[id].nid, hw_cnids))
32267211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32277211Sjp161948 			{
32287211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32297211Sjp161948 		fprintf(stderr, " usable\n");
32307211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32317211Sjp161948 			local_cipher_nids[(*current_slot_n_cipher)++] =
32327211Sjp161948 			    ciphers[id].nid;
32337211Sjp161948 			}
32347211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32357211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32367211Sjp161948 		else
32377211Sjp161948 			{
32387211Sjp161948 		fprintf(stderr, " rejected, software implementation only\n");
32397211Sjp161948 			}
32407211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32417211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32427211Sjp161948 		}
32437211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32447211Sjp161948 	else
32457211Sjp161948 		{
32467211Sjp161948 		fprintf(stderr, " unusable\n");
32477211Sjp161948 		}
32487211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32497211Sjp161948 
32507211Sjp161948 	return;
32517211Sjp161948 	}
32527211Sjp161948 
32537211Sjp161948 static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id,
32547211Sjp161948     CK_MECHANISM_TYPE mech, int *current_slot_n_digest, int *local_digest_nids,
32557211Sjp161948     int id)
32560Sstevel@tonic-gate 	{
32570Sstevel@tonic-gate 	CK_MECHANISM_INFO mech_info;
32580Sstevel@tonic-gate 	CK_RV rv;
32590Sstevel@tonic-gate 
32607211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32617211Sjp161948 	fprintf(stderr, "%s: checking mech: %x", PK11_DBG, mech);
32627211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32637211Sjp161948 	rv = pflist->C_GetMechanismInfo(slot_id, mech, &mech_info);
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	if (rv != CKR_OK)
32660Sstevel@tonic-gate 		{
32677211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32687211Sjp161948 		fprintf(stderr, " not found\n");
32697211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32707211Sjp161948 		return;
32710Sstevel@tonic-gate 		}
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate 	if (mech_info.flags & CKF_DIGEST)
32740Sstevel@tonic-gate 		{
32757211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32767211Sjp161948 	    	if (nid_in_table(digests[id].nid, hw_dnids))
32777211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32787211Sjp161948 			{
32797211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32807211Sjp161948 		fprintf(stderr, " usable\n");
32817211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32827211Sjp161948 			local_digest_nids[(*current_slot_n_digest)++] =
32837211Sjp161948 			    digests[id].nid;
32847211Sjp161948 			}
32857211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32867211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32877211Sjp161948 		else
32887211Sjp161948 			{
32897211Sjp161948 		fprintf(stderr, " rejected, software implementation only\n");
32907211Sjp161948 			}
32917211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32927211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32937211Sjp161948 		}
32947211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32957211Sjp161948 	else
32967211Sjp161948 		{
32977211Sjp161948 		fprintf(stderr, " unusable\n");
32980Sstevel@tonic-gate 		}
32997211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
33007211Sjp161948 
33017211Sjp161948 	return;
33027211Sjp161948 	}
33037211Sjp161948 
33047211Sjp161948 #ifdef	SOLARIS_AES_CTR
33057211Sjp161948 /* create a new NID when we have no OID for that mechanism */
33067211Sjp161948 static int pk11_add_NID(char *sn, char *ln)
33077211Sjp161948 	{
33087211Sjp161948 	ASN1_OBJECT *o;
33097211Sjp161948 	int nid;
33107211Sjp161948 
33117211Sjp161948 	if ((o = ASN1_OBJECT_create(OBJ_new_nid(1), (unsigned char *)"",
33127211Sjp161948 	    1, sn, ln)) == NULL)
33137211Sjp161948 		{
33147211Sjp161948 		return 0;
33157211Sjp161948 		}
33167211Sjp161948 
33177211Sjp161948 	/* will return NID_undef on error */
33187211Sjp161948 	nid = OBJ_add_object(o);
33197211Sjp161948 	ASN1_OBJECT_free(o);
33207211Sjp161948 
33217211Sjp161948 	return (nid);
33227211Sjp161948 	}
33237211Sjp161948 
33247211Sjp161948 /*
33257211Sjp161948  * Create new NIDs for AES counter mode. OpenSSL doesn't support them now so we
33267211Sjp161948  * have to help ourselves here.
33277211Sjp161948  */
33287211Sjp161948 static int pk11_add_aes_ctr_NIDs(void)
33297211Sjp161948 	{
33307211Sjp161948 	/* are we already set? */
33317211Sjp161948 	if (NID_aes_256_ctr != NID_undef)
33327211Sjp161948 		return 1;
33337211Sjp161948 
33347211Sjp161948 	/*
33357211Sjp161948 	 * There are no official names for AES counter modes yet so we just
33367211Sjp161948 	 * follow the format of those that exist.
33377211Sjp161948 	 */
33387211Sjp161948 	if ((NID_aes_128_ctr = pk11_add_NID("AES-128-CTR", "aes-128-ctr")) ==
33397211Sjp161948 	    NID_undef)
33407211Sjp161948 		goto err;
33417211Sjp161948 	ciphers[PK11_AES_128_CTR].nid = pk11_aes_128_ctr.nid = NID_aes_128_ctr;
33427211Sjp161948 	if ((NID_aes_192_ctr = pk11_add_NID("AES-192-CTR", "aes-192-ctr")) ==
33437211Sjp161948 	    NID_undef)
33447211Sjp161948 		goto err;
33457211Sjp161948 	ciphers[PK11_AES_192_CTR].nid = pk11_aes_192_ctr.nid = NID_aes_192_ctr;
33467211Sjp161948 	if ((NID_aes_256_ctr = pk11_add_NID("AES-256-CTR", "aes-256-ctr")) ==
33477211Sjp161948 	    NID_undef)
33487211Sjp161948 		goto err;
33497211Sjp161948 	ciphers[PK11_AES_256_CTR].nid = pk11_aes_256_ctr.nid = NID_aes_256_ctr;
33500Sstevel@tonic-gate 	return 1;
33517211Sjp161948 
33527211Sjp161948 err:
33537211Sjp161948 	PK11err(PK11_F_ADD_AES_CTR_NIDS, PK11_R_ADD_NID_FAILED);
33547211Sjp161948 	return 0;
33550Sstevel@tonic-gate 	}
33567211Sjp161948 #endif	/* SOLARIS_AES_CTR */
33577211Sjp161948 
33587211Sjp161948 /* Find what symmetric ciphers this slot supports. */
33597211Sjp161948 static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist,
33607211Sjp161948     CK_SLOT_ID current_slot, int *current_slot_n_cipher, int *local_cipher_nids)
33617211Sjp161948 	{
33627211Sjp161948 	int i;
33637211Sjp161948 
33647211Sjp161948 	for (i = 0; i < PK11_CIPHER_MAX; ++i)
33657211Sjp161948 		{
33667211Sjp161948 		pk11_get_symmetric_cipher(pflist, current_slot,
33677211Sjp161948 		    ciphers[i].mech_type, current_slot_n_cipher,
33687211Sjp161948 		    local_cipher_nids, ciphers[i].id);
33697211Sjp161948 		}
33707211Sjp161948 	}
33717211Sjp161948 
33727211Sjp161948 /* Find what digest algorithms this slot supports. */
33737211Sjp161948 static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist,
33747211Sjp161948     CK_SLOT_ID current_slot, int *current_slot_n_digest, int *local_digest_nids)
33757211Sjp161948 	{
33767211Sjp161948 	int i;
33777211Sjp161948 
33787211Sjp161948 	for (i = 0; i < PK11_DIGEST_MAX; ++i)
33797211Sjp161948 		{
33807211Sjp161948 		pk11_get_digest(pflist, current_slot, digests[i].mech_type,
33817211Sjp161948 		    current_slot_n_digest, local_digest_nids, digests[i].id);
33827211Sjp161948 		}
33837211Sjp161948 	}
33847211Sjp161948 
33857211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
33867211Sjp161948 /*
33877211Sjp161948  * It would be great if we could use pkcs11_kernel directly since this library
33887211Sjp161948  * offers hardware slots only. That's the easiest way to achieve the situation
33897211Sjp161948  * where we use the hardware accelerators when present and OpenSSL native code
33907211Sjp161948  * otherwise. That presumes the fact that OpenSSL native code is faster than the
33917211Sjp161948  * code in the soft token. It's a logical assumption - Crypto Framework has some
33927211Sjp161948  * inherent overhead so going there for the software implementation of a
33937211Sjp161948  * mechanism should be logically slower in contrast to the OpenSSL native code,
33947211Sjp161948  * presuming that both implementations are of similar speed. For example, the
33957211Sjp161948  * soft token for AES is roughly three times slower than OpenSSL for 64 byte
33967211Sjp161948  * blocks and still 20% slower for 8KB blocks. So, if we want to ship products
33977211Sjp161948  * that use the PKCS#11 engine by default, we must somehow avoid that regression
33987211Sjp161948  * on machines without hardware acceleration. That's why switching to the
33997211Sjp161948  * pkcs11_kernel library seems like a very good idea.
34007211Sjp161948  *
34017211Sjp161948  * The problem is that OpenSSL built with SunStudio is roughly 2x slower for
34027211Sjp161948  * asymmetric operations (RSA/DSA/DH) than the soft token built with the same
34037211Sjp161948  * compiler. That means that if we switched to pkcs11_kernel from the libpkcs11
34047211Sjp161948  * library, we would have had a performance regression on machines without
34057211Sjp161948  * hardware acceleration for asymmetric operations for all applications that use
34067211Sjp161948  * the PKCS#11 engine. There is one such application - Apache web server since
34077211Sjp161948  * it's shipped configured to use the PKCS#11 engine by default. Having said
34087211Sjp161948  * that, we can't switch to the pkcs11_kernel library now and have to come with
34097211Sjp161948  * a solution that, on non-accelerated machines, uses the OpenSSL native code
34107211Sjp161948  * for all symmetric ciphers and digests while it uses the soft token for
34117211Sjp161948  * asymmetric operations.
34127211Sjp161948  *
34137211Sjp161948  * This is the idea: dlopen() pkcs11_kernel directly and find out what
34147211Sjp161948  * mechanisms are there. We don't care about duplications (more slots can
34157211Sjp161948  * support the same mechanism), we just want to know what mechanisms can be
34167211Sjp161948  * possibly supported in hardware on that particular machine. As said before,
34177211Sjp161948  * pkcs11_kernel will show you hardware providers only.
34187211Sjp161948  *
34197211Sjp161948  * Then, we rely on the fact that since we use libpkcs11 library we will find
34207211Sjp161948  * the metaslot. When we go through the metaslot's mechanisms for symmetric
34217211Sjp161948  * ciphers and digests, we check that any found mechanism is in the table
34227211Sjp161948  * created using the pkcs11_kernel library. So, as a result we have two arrays
34237211Sjp161948  * of mechanisms that were advertised as supported in hardware which was the
34247211Sjp161948  * goal of that whole excercise. Thus, we can use libpkcs11 but avoid soft token
34257211Sjp161948  * code for symmetric ciphers and digests. See pk11_choose_slots() for more
34267211Sjp161948  * information.
34277211Sjp161948  *
34287211Sjp161948  * This is Solaris specific code, if SOLARIS_HW_SLOT_SELECTION is not defined
34297211Sjp161948  * the code won't be used.
34307211Sjp161948  */
34317211Sjp161948 #if defined(__sparcv9) || defined(__x86_64) || defined(__amd64)
34327211Sjp161948 static const char pkcs11_kernel[] = "/usr/lib/security/64/pkcs11_kernel.so.1";
34337211Sjp161948 #else
34347211Sjp161948 static const char pkcs11_kernel[] = "/usr/lib/security/pkcs11_kernel.so.1";
34357211Sjp161948 #endif
34367211Sjp161948 
34377211Sjp161948 /*
34387211Sjp161948  * Check hardware capabilities of the machines. The output are two lists,
34397211Sjp161948  * hw_cnids and hw_dnids, that contain hardware mechanisms found in all hardware
34407211Sjp161948  * providers together. They are not sorted and may contain duplicate mechanisms.
34417211Sjp161948  */
34427211Sjp161948 static int check_hw_mechanisms(void)
34437211Sjp161948 	{
34447211Sjp161948 	int i;
34457211Sjp161948 	CK_RV rv;
34467211Sjp161948 	void *handle;
34477211Sjp161948 	CK_C_GetFunctionList p;
34487211Sjp161948 	CK_TOKEN_INFO token_info;
34497211Sjp161948 	CK_ULONG ulSlotCount = 0;
34507211Sjp161948 	int n_cipher = 0, n_digest = 0;
34517211Sjp161948 	CK_FUNCTION_LIST_PTR pflist = NULL;
34527211Sjp161948 	CK_SLOT_ID_PTR pSlotList = NULL_PTR;
34537211Sjp161948 	int *tmp_hw_cnids, *tmp_hw_dnids;
34547211Sjp161948 	int hw_ctable_size, hw_dtable_size;
34557211Sjp161948 
34567211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
34577211Sjp161948 	fprintf(stderr, "%s: SOLARIS_HW_SLOT_SELECTION code running\n",
34587211Sjp161948 	    PK11_DBG);
34590Sstevel@tonic-gate #endif
34607211Sjp161948 	if ((handle = dlopen(pkcs11_kernel, RTLD_LAZY)) == NULL)
34617211Sjp161948 		{
34627211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE);
34637211Sjp161948 		goto err;
34647211Sjp161948 		}
34657211Sjp161948 
34667211Sjp161948 	if ((p = (CK_C_GetFunctionList)dlsym(handle,
34677211Sjp161948 	    PK11_GET_FUNCTION_LIST)) == NULL)
34687211Sjp161948 		{
34697211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE);
34707211Sjp161948 		goto err;
34717211Sjp161948 		}
34727211Sjp161948 
34737211Sjp161948 	/* get the full function list from the loaded library
34747211Sjp161948 	 */
34757211Sjp161948 	if (p(&pflist) != CKR_OK)
34767211Sjp161948 		{
34777211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE);
34787211Sjp161948 		goto err;
34797211Sjp161948 		}
34807211Sjp161948 
34817211Sjp161948 	rv = pflist->C_Initialize(NULL_PTR);
34827211Sjp161948 	if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
34837211Sjp161948 		{
34847211Sjp161948 		PK11err_add_data(PK11_F_CHECK_HW_MECHANISMS,
34857211Sjp161948 		    PK11_R_INITIALIZE, rv);
34867211Sjp161948 		goto err;
34877211Sjp161948 		}
34887211Sjp161948 
34897211Sjp161948 	if (pflist->C_GetSlotList(0, NULL_PTR, &ulSlotCount) != CKR_OK)
34907211Sjp161948 		{
34917211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST);
34927211Sjp161948 		goto err;
34937211Sjp161948 		}
34947211Sjp161948 
34957211Sjp161948 	/* no slots, set the hw mechanism tables as empty */
34967211Sjp161948 	if (ulSlotCount == 0)
34977211Sjp161948 		{
34987211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
34997211Sjp161948 	fprintf(stderr, "%s: no hardware mechanisms found\n", PK11_DBG);
35000Sstevel@tonic-gate #endif
35017211Sjp161948 		hw_cnids = OPENSSL_malloc(sizeof (int));
35027211Sjp161948 		hw_dnids = OPENSSL_malloc(sizeof (int));
35037211Sjp161948 		if (hw_cnids == NULL || hw_dnids == NULL)
35047211Sjp161948 			{
35057211Sjp161948 			PK11err(PK11_F_CHECK_HW_MECHANISMS,
35067211Sjp161948 			    PK11_R_MALLOC_FAILURE);
35077211Sjp161948 			return (0);
35087211Sjp161948 			}
35097211Sjp161948 		/* this means empty tables */
35107211Sjp161948 		hw_cnids[0] = NID_undef;
35117211Sjp161948 		hw_dnids[0] = NID_undef;
35127211Sjp161948 		return (1);
35137211Sjp161948 		}
35147211Sjp161948 
35157211Sjp161948 	pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID));
35167211Sjp161948 	if (pSlotList == NULL)
35177211Sjp161948 		{
35187211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE);
35197211Sjp161948 		goto err;
35207211Sjp161948 		}
35217211Sjp161948 
35227211Sjp161948 	/* Get the slot list for processing */
35237211Sjp161948 	if (pflist->C_GetSlotList(0, pSlotList, &ulSlotCount) != CKR_OK)
35247211Sjp161948 		{
35257211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST);
35267211Sjp161948 		goto err;
35277211Sjp161948 		}
35287211Sjp161948 
35297211Sjp161948 	/*
35307211Sjp161948 	 * We don't care about duplicit mechanisms in multiple slots and also
35317211Sjp161948 	 * reserve one slot for the terminal NID_undef which we use to stop the
35327211Sjp161948 	 * search.
35337211Sjp161948 	 */
35347211Sjp161948 	hw_ctable_size = ulSlotCount * PK11_CIPHER_MAX + 1;
35357211Sjp161948 	hw_dtable_size = ulSlotCount * PK11_DIGEST_MAX + 1;
35367211Sjp161948 	tmp_hw_cnids = OPENSSL_malloc(hw_ctable_size * sizeof (int));
35377211Sjp161948 	tmp_hw_dnids = OPENSSL_malloc(hw_dtable_size * sizeof (int));
35387211Sjp161948 	if (tmp_hw_cnids == NULL || tmp_hw_dnids == NULL)
35397211Sjp161948 		{
35407211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE);
35417211Sjp161948 		goto err;
35427211Sjp161948 		}
35437211Sjp161948 
35447211Sjp161948 	/*
35457211Sjp161948 	 * Do not use memset since we should not rely on the fact that NID_undef
35467211Sjp161948 	 * is zero now.
35477211Sjp161948 	 */
35487211Sjp161948 	for (i = 0; i < hw_ctable_size; ++i)
35497211Sjp161948 		tmp_hw_cnids[i] = NID_undef;
35507211Sjp161948 	for (i = 0; i < hw_dtable_size; ++i)
35517211Sjp161948 		tmp_hw_dnids[i] = NID_undef;
35527211Sjp161948 
35537211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
35547211Sjp161948 	fprintf(stderr, "%s: provider: %s\n", PK11_DBG, pkcs11_kernel);
35557211Sjp161948 	fprintf(stderr, "%s: found %d hardware slots\n", PK11_DBG, ulSlotCount);
35567211Sjp161948 	fprintf(stderr, "%s: now looking for mechs supported in hw\n",
35577211Sjp161948 	    PK11_DBG);
35587211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
35597211Sjp161948 
35607211Sjp161948 	for (i = 0; i < ulSlotCount; i++)
35617211Sjp161948 		{
35627211Sjp161948 		if (pflist->C_GetTokenInfo(pSlotList[i], &token_info) != CKR_OK)
35637211Sjp161948 			continue;
35647211Sjp161948 
35657211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
35667211Sjp161948 	fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label);
35677211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
35687211Sjp161948 
35697211Sjp161948 		/*
35707211Sjp161948 		 * We are filling the hw mech tables here. Global tables are
35717211Sjp161948 		 * still NULL so all mechanisms are put into tmp tables.
35727211Sjp161948 		 */
35737211Sjp161948 		pk11_find_symmetric_ciphers(pflist, pSlotList[i],
35747211Sjp161948 		    &n_cipher, tmp_hw_cnids);
35757211Sjp161948 		pk11_find_digests(pflist, pSlotList[i],
35767211Sjp161948 		    &n_digest, tmp_hw_dnids);
35777211Sjp161948 		}
35787211Sjp161948 
35797211Sjp161948 	/*
35807211Sjp161948 	 * Since we are part of a library (libcrypto.so), calling this function
35817211Sjp161948 	 * may have side-effects. Also, C_Finalize() is triggered by
35827211Sjp161948 	 * dlclose(3C).
35837211Sjp161948 	 */
35847211Sjp161948 #if 0
35857211Sjp161948 	pflist->C_Finalize(NULL);
35867211Sjp161948 #endif
35877211Sjp161948 	OPENSSL_free(pSlotList);
3588*7534SVladimir.Kotal@Sun.COM 	(void) dlclose(handle);
35897211Sjp161948 	hw_cnids = tmp_hw_cnids;
35907211Sjp161948 	hw_dnids = tmp_hw_dnids;
35917211Sjp161948 
35927211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
35937211Sjp161948 	fprintf(stderr, "%s: hw mechs check complete\n", PK11_DBG);
35947211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
35957211Sjp161948 	return (1);
35967211Sjp161948 
35977211Sjp161948 err:
35987211Sjp161948 	if (pSlotList != NULL)
35997211Sjp161948 		OPENSSL_free(pSlotList);
36007211Sjp161948 	if (tmp_hw_cnids != NULL)
36017211Sjp161948 		OPENSSL_free(tmp_hw_cnids);
36027211Sjp161948 	if (tmp_hw_dnids != NULL)
36037211Sjp161948 		OPENSSL_free(tmp_hw_dnids);
36047211Sjp161948 
36057211Sjp161948 	return (0);
36067211Sjp161948 	}
36077211Sjp161948 
36087211Sjp161948 /*
36097211Sjp161948  * Check presence of a NID in the table of NIDs. The table may be NULL (i.e.,
36107211Sjp161948  * non-existent).
36117211Sjp161948  */
36127211Sjp161948 static int nid_in_table(int nid, int *nid_table)
36137211Sjp161948 	{
36147211Sjp161948 	int i = 0;
36157211Sjp161948 
36167211Sjp161948 	/*
36177211Sjp161948 	 * a special case. NULL means that we are initializing a new
36187211Sjp161948 	 * table.
36197211Sjp161948 	 */
36207211Sjp161948 	if (nid_table == NULL)
36217211Sjp161948 		return (1);
36227211Sjp161948 
36237211Sjp161948 	/*
36247211Sjp161948 	 * the table is never full, there is always at least one
36257211Sjp161948 	 * NID_undef.
36267211Sjp161948 	 */
36277211Sjp161948 	while (nid_table[i] != NID_undef)
36287211Sjp161948 		{
36297211Sjp161948 		if (nid_table[i++] == nid)
36307211Sjp161948 			{
36317211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
36327211Sjp161948 	fprintf(stderr, " (NID %d in hw table, idx %d)", nid, i);
36337211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
36347211Sjp161948 			return (1);
36357211Sjp161948 			}
36367211Sjp161948 		}
36377211Sjp161948 
36387211Sjp161948 	return (0);
36397211Sjp161948 	}
36407211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
36417211Sjp161948 
36427211Sjp161948 #endif	/* OPENSSL_NO_HW_PK11 */
36437211Sjp161948 #endif	/* OPENSSL_NO_HW */
3644