xref: /onnv-gate/usr/src/common/openssl/crypto/engine/hw_pk11.c (revision 7560:99293d628284)
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 
1058*7560SVladimir.Kotal@Sun.COM 	if (!pk11_library_initialized)
1059*7560SVladimir.Kotal@Sun.COM 		return;
1060*7560SVladimir.Kotal@Sun.COM 
10617526SVladimir.Kotal@Sun.COM 	LOCK_OBJSTORE(OP_RSA);
10627526SVladimir.Kotal@Sun.COM 	LOCK_OBJSTORE(OP_DSA);
10637526SVladimir.Kotal@Sun.COM 	LOCK_OBJSTORE(OP_DH);
10647526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
10657526SVladimir.Kotal@Sun.COM 		{
10667526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[i].lock);
10677526SVladimir.Kotal@Sun.COM 		}
10687526SVladimir.Kotal@Sun.COM 	}
10697526SVladimir.Kotal@Sun.COM 
10707526SVladimir.Kotal@Sun.COM /* release all engine specific mutexes */
10717526SVladimir.Kotal@Sun.COM static void pk11_fork_parent(void)
10727526SVladimir.Kotal@Sun.COM 	{
10737526SVladimir.Kotal@Sun.COM 	int i;
10747526SVladimir.Kotal@Sun.COM 
1075*7560SVladimir.Kotal@Sun.COM 	if (!pk11_library_initialized)
1076*7560SVladimir.Kotal@Sun.COM 		return;
1077*7560SVladimir.Kotal@Sun.COM 
10787526SVladimir.Kotal@Sun.COM 	for (i = OP_MAX - 1; i >= 0; i--)
10797526SVladimir.Kotal@Sun.COM 		{
10807526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[i].lock);
10817526SVladimir.Kotal@Sun.COM 		}
10827526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DH);
10837526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DSA);
10847526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_RSA);
10857526SVladimir.Kotal@Sun.COM 	}
10867526SVladimir.Kotal@Sun.COM 
10877526SVladimir.Kotal@Sun.COM /*
10887526SVladimir.Kotal@Sun.COM  * same situation as in parent - we need to unlock all locks to make them
10897526SVladimir.Kotal@Sun.COM  * accessible to all threads.
10907526SVladimir.Kotal@Sun.COM  */
10917526SVladimir.Kotal@Sun.COM static void pk11_fork_child(void)
10927526SVladimir.Kotal@Sun.COM 	{
10937526SVladimir.Kotal@Sun.COM 	int i;
10947526SVladimir.Kotal@Sun.COM 
1095*7560SVladimir.Kotal@Sun.COM 	if (!pk11_library_initialized)
1096*7560SVladimir.Kotal@Sun.COM 		return;
1097*7560SVladimir.Kotal@Sun.COM 
10987526SVladimir.Kotal@Sun.COM 	for (i = OP_MAX - 1; i >= 0; i--)
10997526SVladimir.Kotal@Sun.COM 		{
11007526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[i].lock);
11017526SVladimir.Kotal@Sun.COM 		}
11027526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DH);
11037526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_DSA);
11047526SVladimir.Kotal@Sun.COM 	UNLOCK_OBJSTORE(OP_RSA);
11057526SVladimir.Kotal@Sun.COM 	}
11067526SVladimir.Kotal@Sun.COM 
11070Sstevel@tonic-gate /* Initialization function for the pk11 engine */
11080Sstevel@tonic-gate static int pk11_init(ENGINE *e)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	return pk11_library_init(e);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate /* Initialization function. Sets up various pk11 library components.
11140Sstevel@tonic-gate  * It selects a slot based on predefined critiera. In the process, it also
11150Sstevel@tonic-gate  * count how many ciphers and digests to support. Since the cipher and
11160Sstevel@tonic-gate  * digest information is needed when setting default engine, this function
11170Sstevel@tonic-gate  * needs to be called before calling ENGINE_set_default.
11180Sstevel@tonic-gate  */
11197534SVladimir.Kotal@Sun.COM /* ARGSUSED */
11200Sstevel@tonic-gate static int pk11_library_init(ENGINE *e)
11210Sstevel@tonic-gate 	{
11220Sstevel@tonic-gate 	CK_C_GetFunctionList p;
11230Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
11240Sstevel@tonic-gate 	CK_INFO info;
11250Sstevel@tonic-gate 	CK_ULONG ul_state_len;
11267211Sjp161948 	int any_slot_found;
11277526SVladimir.Kotal@Sun.COM 	int i;
11287211Sjp161948 
11297211Sjp161948 	/*
11307211Sjp161948 	 * pk11_library_initialized is set to 0 in pk11_finish() which is called
11317211Sjp161948 	 * from ENGINE_finish(). However, if there is still at least one
11327211Sjp161948 	 * existing functional reference to the engine (see engine(3) for more
11337211Sjp161948 	 * information), pk11_finish() is skipped. For example, this can happen
11347211Sjp161948 	 * if an application forgets to clear one cipher context. In case of a
11357211Sjp161948 	 * fork() when the application is finishing the engine so that it can be
11367211Sjp161948 	 * reinitialized in the child, forgotten functional reference causes
11377211Sjp161948 	 * pk11_library_initialized to stay 1. In that case we need the PID
11387211Sjp161948 	 * check so that we properly initialize the engine again.
11397211Sjp161948 	 */
11400Sstevel@tonic-gate 	if (pk11_library_initialized)
11417211Sjp161948 		{
11427211Sjp161948 		if (pk11_pid == getpid())
11437526SVladimir.Kotal@Sun.COM 			{
11447211Sjp161948 			return 1;
11457526SVladimir.Kotal@Sun.COM 			}
11467211Sjp161948 		else
11477526SVladimir.Kotal@Sun.COM 			{
11487211Sjp161948 			global_session = CK_INVALID_HANDLE;
11497526SVladimir.Kotal@Sun.COM 			/*
11507526SVladimir.Kotal@Sun.COM 			 * free the locks first to prevent memory leak in case
11517526SVladimir.Kotal@Sun.COM 			 * the application calls fork() without finishing the
11527526SVladimir.Kotal@Sun.COM 			 * engine first.
11537526SVladimir.Kotal@Sun.COM 			 */
11547526SVladimir.Kotal@Sun.COM 			pk11_free_all_locks();
11557526SVladimir.Kotal@Sun.COM 			}
11567211Sjp161948 		}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if (pk11_dso == NULL)
11590Sstevel@tonic-gate 		{
11600Sstevel@tonic-gate 		PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE);
11610Sstevel@tonic-gate 		goto err;
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 
11647211Sjp161948 #ifdef	SOLARIS_AES_CTR
11657211Sjp161948 	/*
11667211Sjp161948 	 * We must do this before we start working with slots since we need all
11677211Sjp161948 	 * NIDs there.
11687211Sjp161948 	 */
11697211Sjp161948 	if (pk11_add_aes_ctr_NIDs() == 0)
11707211Sjp161948 		goto err;
11717211Sjp161948 #endif	/* SOLARIS_AES_CTR */
11727211Sjp161948 
11737211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
11747211Sjp161948 	if (check_hw_mechanisms() == 0)
11757211Sjp161948 		goto err;
11767211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
11777211Sjp161948 
11780Sstevel@tonic-gate 	/* get the C_GetFunctionList function from the loaded library
11790Sstevel@tonic-gate 	 */
11800Sstevel@tonic-gate 	p = (CK_C_GetFunctionList)DSO_bind_func(pk11_dso,
11810Sstevel@tonic-gate 		PK11_GET_FUNCTION_LIST);
11820Sstevel@tonic-gate 	if ( !p )
11830Sstevel@tonic-gate 		{
11840Sstevel@tonic-gate 		PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE);
11850Sstevel@tonic-gate 		goto err;
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	/* get the full function list from the loaded library
11890Sstevel@tonic-gate 	 */
11900Sstevel@tonic-gate 	rv = p(&pFuncList);
11910Sstevel@tonic-gate 	if (rv != CKR_OK)
11920Sstevel@tonic-gate 		{
11937211Sjp161948 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE, rv);
11940Sstevel@tonic-gate 		goto err;
11950Sstevel@tonic-gate 		}
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	rv = pFuncList->C_Initialize(NULL_PTR);
11980Sstevel@tonic-gate 	if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
11990Sstevel@tonic-gate 		{
12007211Sjp161948 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_INITIALIZE, rv);
12010Sstevel@tonic-gate 		goto err;
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	rv = pFuncList->C_GetInfo(&info);
12050Sstevel@tonic-gate 	if (rv != CKR_OK)
12060Sstevel@tonic-gate 		{
12077211Sjp161948 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_GETINFO, rv);
12080Sstevel@tonic-gate 		goto err;
12090Sstevel@tonic-gate 		}
12100Sstevel@tonic-gate 
12117211Sjp161948 	if (pk11_choose_slots(&any_slot_found) == 0)
12120Sstevel@tonic-gate 		goto err;
12130Sstevel@tonic-gate 
12147211Sjp161948 	/*
12157211Sjp161948 	 * The library we use, set in def_PK11_LIBNAME, may not offer any
12167211Sjp161948 	 * slot(s). In that case, we must not proceed but we must not return an
12177211Sjp161948 	 * error. The reason is that applications that try to set up the PKCS#11
12187211Sjp161948 	 * engine don't exit on error during the engine initialization just
12197211Sjp161948 	 * because no slot was present.
12207211Sjp161948 	 */
12217211Sjp161948 	if (any_slot_found == 0)
12227211Sjp161948 		return 1;
12237211Sjp161948 
12240Sstevel@tonic-gate 	if (global_session == CK_INVALID_HANDLE)
12250Sstevel@tonic-gate 		{
12260Sstevel@tonic-gate 		/* Open the global_session for the new process */
12270Sstevel@tonic-gate 		rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
12280Sstevel@tonic-gate 			NULL_PTR, NULL_PTR, &global_session);
12290Sstevel@tonic-gate 		if (rv != CKR_OK)
12300Sstevel@tonic-gate 			{
12317211Sjp161948 			PK11err_add_data(PK11_F_LIBRARY_INIT,
12327211Sjp161948 			    PK11_R_OPENSESSION, rv);
12330Sstevel@tonic-gate 			goto err;
12340Sstevel@tonic-gate 			}
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	/* Disable digest if C_GetOperationState is not supported since
12380Sstevel@tonic-gate 	 * this function is required by OpenSSL digest copy function */
12390Sstevel@tonic-gate 	if (pFuncList->C_GetOperationState(global_session, NULL, &ul_state_len)
12407211Sjp161948 			== CKR_FUNCTION_NOT_SUPPORTED) {
12417211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
12427211Sjp161948 		fprintf(stderr, "%s: C_GetOperationState() not supported, "
12437211Sjp161948 		    "setting digest_count to 0\n", PK11_DBG);
12447211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
12450Sstevel@tonic-gate 		digest_count = 0;
12467211Sjp161948 	}
12470Sstevel@tonic-gate 
12487526SVladimir.Kotal@Sun.COM 	pk11_library_initialized = TRUE;
12497211Sjp161948 	pk11_pid = getpid();
12507526SVladimir.Kotal@Sun.COM 	/*
12517526SVladimir.Kotal@Sun.COM 	 * if initialization of the locks fails pk11_init_all_locks()
12527526SVladimir.Kotal@Sun.COM 	 * will do the cleanup.
12537526SVladimir.Kotal@Sun.COM 	 */
12547526SVladimir.Kotal@Sun.COM 	if (!pk11_init_all_locks())
12557526SVladimir.Kotal@Sun.COM 		goto err;
12567526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
12577526SVladimir.Kotal@Sun.COM 		session_cache[i].head = NULL;
12587526SVladimir.Kotal@Sun.COM 	/*
12597526SVladimir.Kotal@Sun.COM 	 * initialize active lists. We only use active lists
12607526SVladimir.Kotal@Sun.COM 	 * for asymmetric ciphers.
12617526SVladimir.Kotal@Sun.COM 	 */
12627526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
12637526SVladimir.Kotal@Sun.COM 		active_list[i] = NULL;
12647526SVladimir.Kotal@Sun.COM 
12657526SVladimir.Kotal@Sun.COM 	if (!pk11_atfork_initialized)
12667526SVladimir.Kotal@Sun.COM 		{
12677526SVladimir.Kotal@Sun.COM 		if (pthread_atfork(pk11_fork_prepare, pk11_fork_parent,
12687526SVladimir.Kotal@Sun.COM 		    pk11_fork_child) != 0)
12697526SVladimir.Kotal@Sun.COM 			{
12707526SVladimir.Kotal@Sun.COM 			PK11err(PK11_F_LIBRARY_INIT, PK11_R_ATFORK_FAILED);
12717526SVladimir.Kotal@Sun.COM 			goto err;
12727526SVladimir.Kotal@Sun.COM 			}
12737526SVladimir.Kotal@Sun.COM 		pk11_atfork_initialized = TRUE;
12747526SVladimir.Kotal@Sun.COM 		}
12757526SVladimir.Kotal@Sun.COM 
12760Sstevel@tonic-gate 	return 1;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate err:
12790Sstevel@tonic-gate 	return 0;
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate /* Destructor (complements the "ENGINE_pk11()" constructor)
12830Sstevel@tonic-gate  */
12847534SVladimir.Kotal@Sun.COM /* ARGSUSED */
12850Sstevel@tonic-gate static int pk11_destroy(ENGINE *e)
12860Sstevel@tonic-gate 	{
12870Sstevel@tonic-gate 	free_PK11_LIBNAME();
12880Sstevel@tonic-gate 	ERR_unload_pk11_strings();
12890Sstevel@tonic-gate 	return 1;
12900Sstevel@tonic-gate 	}
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate /* Termination function to clean up the session, the token, and
12930Sstevel@tonic-gate  * the pk11 library.
12940Sstevel@tonic-gate  */
12957534SVladimir.Kotal@Sun.COM /* ARGSUSED */
12960Sstevel@tonic-gate static int pk11_finish(ENGINE *e)
12970Sstevel@tonic-gate 	{
12987526SVladimir.Kotal@Sun.COM 	int i;
12997526SVladimir.Kotal@Sun.COM 
13000Sstevel@tonic-gate 	if (pk11_dso == NULL)
13010Sstevel@tonic-gate 		{
13020Sstevel@tonic-gate 		PK11err(PK11_F_FINISH, PK11_R_NOT_LOADED);
13030Sstevel@tonic-gate 		goto err;
13040Sstevel@tonic-gate 		}
13050Sstevel@tonic-gate 
13066847Svk199839 	OPENSSL_assert(pFuncList != NULL);
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	if (pk11_free_all_sessions() == 0)
13090Sstevel@tonic-gate 		goto err;
13100Sstevel@tonic-gate 
13117526SVladimir.Kotal@Sun.COM 	/* free all active lists */
13127526SVladimir.Kotal@Sun.COM 	for (i = 0; i < OP_MAX; i++)
13137526SVladimir.Kotal@Sun.COM 		pk11_free_active_list(i);
13147526SVladimir.Kotal@Sun.COM 
13150Sstevel@tonic-gate 	pFuncList->C_CloseSession(global_session);
13167211Sjp161948 	global_session = CK_INVALID_HANDLE;
13177211Sjp161948 
13187211Sjp161948 	/*
13197211Sjp161948 	 * Since we are part of a library (libcrypto.so), calling this function
13207211Sjp161948 	 * may have side-effects.
13217211Sjp161948 	 */
13227211Sjp161948 #if 0
13230Sstevel@tonic-gate 	pFuncList->C_Finalize(NULL);
13247211Sjp161948 #endif
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	if (!DSO_free(pk11_dso))
13270Sstevel@tonic-gate 		{
13280Sstevel@tonic-gate 		PK11err(PK11_F_FINISH, PK11_R_DSO_FAILURE);
13290Sstevel@tonic-gate 		goto err;
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 	pk11_dso = NULL;
13320Sstevel@tonic-gate 	pFuncList = NULL;
13337526SVladimir.Kotal@Sun.COM 	pk11_library_initialized = FALSE;
13347211Sjp161948 	pk11_pid = 0;
1335*7560SVladimir.Kotal@Sun.COM 	/*
1336*7560SVladimir.Kotal@Sun.COM 	 * There is no way how to unregister atfork handlers (other than
1337*7560SVladimir.Kotal@Sun.COM 	 * unloading the library) so we just free the locks. For this reason
1338*7560SVladimir.Kotal@Sun.COM 	 * the atfork handlers check if the engine is initialized and bail out
1339*7560SVladimir.Kotal@Sun.COM 	 * immediately if not. This is necessary in case a process finishes
1340*7560SVladimir.Kotal@Sun.COM 	 * the engine before calling fork().
1341*7560SVladimir.Kotal@Sun.COM 	 */
13427526SVladimir.Kotal@Sun.COM 	pk11_free_all_locks();
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	return 1;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate err:
13470Sstevel@tonic-gate 	return 0;
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate /* Standard engine interface function to set the dynamic library path */
13517534SVladimir.Kotal@Sun.COM /* ARGSUSED */
13520Sstevel@tonic-gate static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
13530Sstevel@tonic-gate 	{
13540Sstevel@tonic-gate 	int initialized = ((pk11_dso == NULL) ? 0 : 1);
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	switch(cmd)
13570Sstevel@tonic-gate 		{
13580Sstevel@tonic-gate 	case PK11_CMD_SO_PATH:
13590Sstevel@tonic-gate 		if (p == NULL)
13600Sstevel@tonic-gate 			{
13610Sstevel@tonic-gate 			PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER);
13620Sstevel@tonic-gate 			return 0;
13630Sstevel@tonic-gate 			}
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 		if (initialized)
13660Sstevel@tonic-gate 			{
13670Sstevel@tonic-gate 			PK11err(PK11_F_CTRL, PK11_R_ALREADY_LOADED);
13680Sstevel@tonic-gate 			return 0;
13690Sstevel@tonic-gate 			}
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		return set_PK11_LIBNAME((const char*)p);
13720Sstevel@tonic-gate 	default:
13730Sstevel@tonic-gate 		break;
13740Sstevel@tonic-gate 		}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	PK11err(PK11_F_CTRL,PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED);
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	return 0;
13790Sstevel@tonic-gate 	}
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate /* Required function by the engine random interface. It does nothing here
13830Sstevel@tonic-gate  */
13840Sstevel@tonic-gate static void pk11_rand_cleanup(void)
13850Sstevel@tonic-gate 	{
13860Sstevel@tonic-gate 	return;
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13897534SVladimir.Kotal@Sun.COM /* ARGSUSED */
13900Sstevel@tonic-gate static void pk11_rand_add(const void *buf, int num, double add)
13910Sstevel@tonic-gate 	{
13920Sstevel@tonic-gate 	PK11_SESSION *sp;
13930Sstevel@tonic-gate 
13947211Sjp161948 	if ((sp = pk11_get_session(OP_RAND)) == NULL)
13950Sstevel@tonic-gate 		return;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/* Ignore any errors (e.g. CKR_RANDOM_SEED_NOT_SUPPORTED) since
13980Sstevel@tonic-gate 	 * the calling functions do not care anyway
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	pFuncList->C_SeedRandom(sp->session, (unsigned char *) buf, num);
14017211Sjp161948 	pk11_return_session(sp, OP_RAND);
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	return;
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate static void pk11_rand_seed(const void *buf, int num)
14070Sstevel@tonic-gate 	{
14080Sstevel@tonic-gate 	pk11_rand_add(buf, num, 0);
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate static int pk11_rand_bytes(unsigned char *buf, int num)
14120Sstevel@tonic-gate 	{
14130Sstevel@tonic-gate 	CK_RV rv;
14140Sstevel@tonic-gate 	PK11_SESSION *sp;
14150Sstevel@tonic-gate 
14167211Sjp161948 	if ((sp = pk11_get_session(OP_RAND)) == NULL)
14170Sstevel@tonic-gate 		return 0;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	rv = pFuncList->C_GenerateRandom(sp->session, buf, num);
14200Sstevel@tonic-gate 	if (rv != CKR_OK)
14210Sstevel@tonic-gate 		{
14227211Sjp161948 		PK11err_add_data(PK11_F_RAND_BYTES, PK11_R_GENERATERANDOM, rv);
14237211Sjp161948 		pk11_return_session(sp, OP_RAND);
14240Sstevel@tonic-gate 		return 0;
14250Sstevel@tonic-gate 		}
14260Sstevel@tonic-gate 
14277211Sjp161948 	pk11_return_session(sp, OP_RAND);
14280Sstevel@tonic-gate 	return 1;
14290Sstevel@tonic-gate 	}
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate /* Required function by the engine random interface. It does nothing here
14320Sstevel@tonic-gate  */
14330Sstevel@tonic-gate static int pk11_rand_status(void)
14340Sstevel@tonic-gate 	{
14350Sstevel@tonic-gate 	return 1;
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 
14386847Svk199839 /*
14396847Svk199839  * Free all BIGNUM structures from PK11_SESSION.
14406847Svk199839  */
14417526SVladimir.Kotal@Sun.COM static void pk11_free_nums(PK11_SESSION *sp, PK11_OPTYPE optype)
14426847Svk199839 	{
14437526SVladimir.Kotal@Sun.COM 	switch (optype)
14447526SVladimir.Kotal@Sun.COM 		{
14456847Svk199839 #ifndef	OPENSSL_NO_RSA
14467526SVladimir.Kotal@Sun.COM 		case OP_RSA:
14477526SVladimir.Kotal@Sun.COM 			if (sp->opdata_rsa_n_num != NULL)
14487526SVladimir.Kotal@Sun.COM 				{
14497526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_rsa_n_num);
14507526SVladimir.Kotal@Sun.COM 				sp->opdata_rsa_n_num = NULL;
14517526SVladimir.Kotal@Sun.COM 				}
14527526SVladimir.Kotal@Sun.COM 			if (sp->opdata_rsa_e_num != NULL)
14537526SVladimir.Kotal@Sun.COM 				{
14547526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_rsa_e_num);
14557526SVladimir.Kotal@Sun.COM 				sp->opdata_rsa_e_num = NULL;
14567526SVladimir.Kotal@Sun.COM 				}
14577526SVladimir.Kotal@Sun.COM 			if (sp->opdata_rsa_d_num != NULL)
14587526SVladimir.Kotal@Sun.COM 				{
14597526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_rsa_d_num);
14607526SVladimir.Kotal@Sun.COM 				sp->opdata_rsa_d_num = NULL;
14617526SVladimir.Kotal@Sun.COM 				}
14627526SVladimir.Kotal@Sun.COM 			break;
14636847Svk199839 #endif
14646847Svk199839 #ifndef	OPENSSL_NO_DSA
14657526SVladimir.Kotal@Sun.COM 		case OP_DSA:
14667526SVladimir.Kotal@Sun.COM 			if (sp->opdata_dsa_pub_num != NULL)
14677526SVladimir.Kotal@Sun.COM 				{
14687526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_dsa_pub_num);
14697526SVladimir.Kotal@Sun.COM 				sp->opdata_dsa_pub_num = NULL;
14707526SVladimir.Kotal@Sun.COM 				}
14717526SVladimir.Kotal@Sun.COM 			if (sp->opdata_dsa_priv_num != NULL)
14727526SVladimir.Kotal@Sun.COM 				{
14737526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_dsa_priv_num);
14747526SVladimir.Kotal@Sun.COM 				sp->opdata_dsa_priv_num = NULL;
14757526SVladimir.Kotal@Sun.COM 				}
14767526SVladimir.Kotal@Sun.COM 			break;
14776847Svk199839 #endif
14786847Svk199839 #ifndef	OPENSSL_NO_DH
14797526SVladimir.Kotal@Sun.COM 		case OP_DH:
14807526SVladimir.Kotal@Sun.COM 			if (sp->opdata_dh_priv_num != NULL)
14817526SVladimir.Kotal@Sun.COM 				{
14827526SVladimir.Kotal@Sun.COM 				BN_free(sp->opdata_dh_priv_num);
14837526SVladimir.Kotal@Sun.COM 				sp->opdata_dh_priv_num = NULL;
14847526SVladimir.Kotal@Sun.COM 				}
14857526SVladimir.Kotal@Sun.COM 			break;
14866847Svk199839 #endif
14877526SVladimir.Kotal@Sun.COM 		default:
14887526SVladimir.Kotal@Sun.COM 			break;
14897526SVladimir.Kotal@Sun.COM 		}
14906847Svk199839 	}
14910Sstevel@tonic-gate 
14926847Svk199839 /*
14936847Svk199839  * Get new PK11_SESSION structure ready for use. Every process must have
14946847Svk199839  * its own freelist of PK11_SESSION structures so handle fork() here
14956847Svk199839  * by destroying the old and creating new freelist.
14966847Svk199839  * The returned PK11_SESSION structure is disconnected from the freelist.
14976847Svk199839  */
14987211Sjp161948 PK11_SESSION *pk11_get_session(PK11_OPTYPE optype)
14990Sstevel@tonic-gate 	{
15007526SVladimir.Kotal@Sun.COM 	PK11_SESSION *sp = NULL, *sp1, *freelist;
15017526SVladimir.Kotal@Sun.COM 	pthread_mutex_t *freelist_lock;
15020Sstevel@tonic-gate 	CK_RV rv;
15030Sstevel@tonic-gate 
15047211Sjp161948 	switch (optype)
15057211Sjp161948 		{
15067526SVladimir.Kotal@Sun.COM 		case OP_RSA:
15077526SVladimir.Kotal@Sun.COM 		case OP_DSA:
15087526SVladimir.Kotal@Sun.COM 		case OP_DH:
15097211Sjp161948 		case OP_RAND:
15107211Sjp161948 		case OP_DIGEST:
15117211Sjp161948 		case OP_CIPHER:
15127526SVladimir.Kotal@Sun.COM 			freelist_lock = session_cache[optype].lock;
15137211Sjp161948 			break;
15147211Sjp161948 		default:
15157211Sjp161948 			PK11err(PK11_F_GET_SESSION,
15167211Sjp161948 				PK11_R_INVALID_OPERATION_TYPE);
15177526SVladimir.Kotal@Sun.COM 			return (NULL);
15187211Sjp161948 		}
15197526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_lock(freelist_lock);
15207526SVladimir.Kotal@Sun.COM 	freelist = session_cache[optype].head;
15217211Sjp161948 	sp = freelist;
15227211Sjp161948 
15236847Svk199839 	/*
15246847Svk199839 	 * If the free list is empty, allocate new unitialized (filled
15256847Svk199839 	 * with zeroes) PK11_SESSION structure otherwise return first
15266847Svk199839 	 * structure from the freelist.
15276847Svk199839 	 */
15287211Sjp161948 	if (sp == NULL)
15290Sstevel@tonic-gate 		{
15300Sstevel@tonic-gate 		if ((sp = OPENSSL_malloc(sizeof(PK11_SESSION))) == NULL)
15310Sstevel@tonic-gate 			{
15320Sstevel@tonic-gate 			PK11err(PK11_F_GET_SESSION,
15330Sstevel@tonic-gate 				PK11_R_MALLOC_FAILURE);
15340Sstevel@tonic-gate 			goto err;
15350Sstevel@tonic-gate 			}
15367534SVladimir.Kotal@Sun.COM 		(void) memset(sp, 0, sizeof(PK11_SESSION));
15370Sstevel@tonic-gate 		}
15380Sstevel@tonic-gate 	else
15390Sstevel@tonic-gate 		{
15407211Sjp161948 		freelist = sp->next;
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (sp->pid != 0 && sp->pid != getpid())
15440Sstevel@tonic-gate 		{
15456847Svk199839 		/*
15466847Svk199839 		 * We are a new process and thus need to free any inherited
15470Sstevel@tonic-gate 		 * PK11_SESSION objects.
15480Sstevel@tonic-gate 		 */
15497211Sjp161948 		while ((sp1 = freelist) != NULL)
15500Sstevel@tonic-gate 			{
15517211Sjp161948 			freelist = sp1->next;
15526847Svk199839 			/*
15537211Sjp161948 			 * NOTE: we do not want to call pk11_free_all_sessions()
15547211Sjp161948 			 * here because it would close underlying PKCS#11
15557211Sjp161948 			 * sessions and destroy all objects.
15566847Svk199839 			 */
15577526SVladimir.Kotal@Sun.COM 			pk11_free_nums(sp1, optype);
15580Sstevel@tonic-gate 			OPENSSL_free(sp1);
15590Sstevel@tonic-gate 			}
15600Sstevel@tonic-gate 
15617526SVladimir.Kotal@Sun.COM 		/* we have to free the active list as well. */
15627526SVladimir.Kotal@Sun.COM 		pk11_free_active_list(optype);
15637526SVladimir.Kotal@Sun.COM 
15640Sstevel@tonic-gate 		/* Initialize the process */
15650Sstevel@tonic-gate 		rv = pFuncList->C_Initialize(NULL_PTR);
15660Sstevel@tonic-gate 		if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
15670Sstevel@tonic-gate 			{
15687211Sjp161948 			PK11err_add_data(PK11_F_GET_SESSION, PK11_R_INITIALIZE,
15697211Sjp161948 			    rv);
15700Sstevel@tonic-gate 			OPENSSL_free(sp);
15710Sstevel@tonic-gate 			sp = NULL;
15720Sstevel@tonic-gate 			goto err;
15730Sstevel@tonic-gate 			}
15740Sstevel@tonic-gate 
15756847Svk199839 		/*
15767211Sjp161948 		 * Choose slot here since the slot table is different on this
15777211Sjp161948 		 * process. If we are here then we must have found at least one
15787211Sjp161948 		 * usable slot before so we don't need to check any_slot_found.
15797211Sjp161948 		 * See pk11_library_init()'s usage of this function for more
15807211Sjp161948 		 * information.
15810Sstevel@tonic-gate 		 */
15827211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
15837211Sjp161948 		if (check_hw_mechanisms() == 0)
15847211Sjp161948 			goto err;
15857211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
15867211Sjp161948 		if (pk11_choose_slots(NULL) == 0)
15870Sstevel@tonic-gate 			goto err;
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		/* Open the global_session for the new process */
15900Sstevel@tonic-gate 		rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
15910Sstevel@tonic-gate 			NULL_PTR, NULL_PTR, &global_session);
15920Sstevel@tonic-gate 		if (rv != CKR_OK)
15930Sstevel@tonic-gate 			{
15947211Sjp161948 			PK11err_add_data(PK11_F_GET_SESSION, PK11_R_OPENSESSION,
15957211Sjp161948 			    rv);
15960Sstevel@tonic-gate 			OPENSSL_free(sp);
15970Sstevel@tonic-gate 			sp = NULL;
15980Sstevel@tonic-gate 			goto err;
15990Sstevel@tonic-gate 			}
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 		/* It is an inherited session and needs re-initialization.
16020Sstevel@tonic-gate 		 */
16037211Sjp161948 		if (pk11_setup_session(sp, optype) == 0)
16040Sstevel@tonic-gate 			{
16050Sstevel@tonic-gate 			OPENSSL_free(sp);
16060Sstevel@tonic-gate 			sp = NULL;
16070Sstevel@tonic-gate 			}
16080Sstevel@tonic-gate 		}
16090Sstevel@tonic-gate 	else if (sp->pid == 0)
16100Sstevel@tonic-gate 		{
16117211Sjp161948 		/* It is a new session and needs initialization. */
16127211Sjp161948 		if (pk11_setup_session(sp, optype) == 0)
16130Sstevel@tonic-gate 			{
16140Sstevel@tonic-gate 			OPENSSL_free(sp);
16150Sstevel@tonic-gate 			sp = NULL;
16160Sstevel@tonic-gate 			}
16170Sstevel@tonic-gate 		}
16180Sstevel@tonic-gate 
16197526SVladimir.Kotal@Sun.COM 	/* set new head for the list of PK11_SESSION objects */
16207526SVladimir.Kotal@Sun.COM 	session_cache[optype].head = freelist;
16217211Sjp161948 
16220Sstevel@tonic-gate err:
16236847Svk199839 	if (sp != NULL)
16240Sstevel@tonic-gate 		sp->next = NULL;
16250Sstevel@tonic-gate 
16267526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_unlock(freelist_lock);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	return sp;
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 
16327211Sjp161948 void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype)
16330Sstevel@tonic-gate 	{
16347526SVladimir.Kotal@Sun.COM 	pthread_mutex_t *freelist_lock;
16357526SVladimir.Kotal@Sun.COM 	PK11_SESSION *freelist;
16367526SVladimir.Kotal@Sun.COM 
16370Sstevel@tonic-gate 	if (sp == NULL || sp->pid != getpid())
16380Sstevel@tonic-gate 		return;
16390Sstevel@tonic-gate 
16407211Sjp161948 	switch (optype)
16417211Sjp161948 		{
16427526SVladimir.Kotal@Sun.COM 		case OP_RSA:
16437526SVladimir.Kotal@Sun.COM 		case OP_DSA:
16447526SVladimir.Kotal@Sun.COM 		case OP_DH:
16457211Sjp161948 		case OP_RAND:
16467211Sjp161948 		case OP_DIGEST:
16477211Sjp161948 		case OP_CIPHER:
16487526SVladimir.Kotal@Sun.COM 			freelist_lock = session_cache[optype].lock;
16497211Sjp161948 			break;
16507526SVladimir.Kotal@Sun.COM 		default:
16517526SVladimir.Kotal@Sun.COM 			PK11err(PK11_F_RETURN_SESSION,
16527526SVladimir.Kotal@Sun.COM 				PK11_R_INVALID_OPERATION_TYPE);
16537526SVladimir.Kotal@Sun.COM 			return;
16547211Sjp161948 		}
16550Sstevel@tonic-gate 
16567526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_lock(freelist_lock);
16577526SVladimir.Kotal@Sun.COM 	freelist = session_cache[optype].head;
16587526SVladimir.Kotal@Sun.COM 	sp->next = freelist;
16597526SVladimir.Kotal@Sun.COM 	session_cache[optype].head = sp;
16607526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_unlock(freelist_lock);
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate /* Destroy all objects. This function is called when the engine is finished
16650Sstevel@tonic-gate  */
16660Sstevel@tonic-gate static int pk11_free_all_sessions()
16670Sstevel@tonic-gate 	{
16687211Sjp161948 	int ret = 1;
16697526SVladimir.Kotal@Sun.COM 	int type;
16700Sstevel@tonic-gate 
16716847Svk199839 #ifndef OPENSSL_NO_RSA
16726847Svk199839 	(void) pk11_destroy_rsa_key_objects(NULL);
16737211Sjp161948 #endif	/* OPENSSL_NO_RSA */
16746847Svk199839 #ifndef OPENSSL_NO_DSA
16756847Svk199839 	(void) pk11_destroy_dsa_key_objects(NULL);
16767211Sjp161948 #endif	/* OPENSSL_NO_DSA */
16776847Svk199839 #ifndef OPENSSL_NO_DH
16786847Svk199839 	(void) pk11_destroy_dh_key_objects(NULL);
16797211Sjp161948 #endif	/* OPENSSL_NO_DH */
16806847Svk199839 	(void) pk11_destroy_cipher_key_objects(NULL);
16817211Sjp161948 
16827211Sjp161948 	/*
16837211Sjp161948 	 * We try to release as much as we can but any error means that we will
16847211Sjp161948 	 * return 0 on exit.
16857211Sjp161948 	 */
16867526SVladimir.Kotal@Sun.COM 	for (type = 0; type < OP_MAX; type++)
16877526SVladimir.Kotal@Sun.COM 		{
16887526SVladimir.Kotal@Sun.COM 		if (pk11_free_session_list(type) == 0)
16897526SVladimir.Kotal@Sun.COM 			ret = 0;
16907526SVladimir.Kotal@Sun.COM 		}
16917211Sjp161948 
16927211Sjp161948 	return ret;
16937211Sjp161948 	}
16940Sstevel@tonic-gate 
16957211Sjp161948 /*
16967526SVladimir.Kotal@Sun.COM  * Destroy session structures from the linked list specified. Free as many
16977526SVladimir.Kotal@Sun.COM  * sessions as possible but any failure in C_CloseSession() means that we
16987526SVladimir.Kotal@Sun.COM  * return an error on return.
16997211Sjp161948  */
17007526SVladimir.Kotal@Sun.COM static int pk11_free_session_list(PK11_OPTYPE optype)
17017211Sjp161948 	{
17027211Sjp161948 	CK_RV rv;
17037211Sjp161948 	PK11_SESSION *sp = NULL;
17047526SVladimir.Kotal@Sun.COM 	PK11_SESSION *freelist = NULL;
17057211Sjp161948 	pid_t mypid = getpid();
17067526SVladimir.Kotal@Sun.COM 	pthread_mutex_t *freelist_lock;
17077211Sjp161948 	int ret = 1;
17087211Sjp161948 
17097526SVladimir.Kotal@Sun.COM 	switch (optype)
17107526SVladimir.Kotal@Sun.COM 		{
17117526SVladimir.Kotal@Sun.COM 		case OP_RSA:
17127526SVladimir.Kotal@Sun.COM 		case OP_DSA:
17137526SVladimir.Kotal@Sun.COM 		case OP_DH:
17147526SVladimir.Kotal@Sun.COM 		case OP_RAND:
17157526SVladimir.Kotal@Sun.COM 		case OP_DIGEST:
17167526SVladimir.Kotal@Sun.COM 		case OP_CIPHER:
17177526SVladimir.Kotal@Sun.COM 			freelist_lock = session_cache[optype].lock;
17187526SVladimir.Kotal@Sun.COM 			break;
17197526SVladimir.Kotal@Sun.COM 		default:
17207526SVladimir.Kotal@Sun.COM 			PK11err(PK11_F_FREE_ALL_SESSIONS,
17217526SVladimir.Kotal@Sun.COM 				PK11_R_INVALID_OPERATION_TYPE);
17227526SVladimir.Kotal@Sun.COM 			return (0);
17237526SVladimir.Kotal@Sun.COM 		}
17247526SVladimir.Kotal@Sun.COM 
17257526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_lock(freelist_lock);
17267526SVladimir.Kotal@Sun.COM 	freelist = session_cache[optype].head;
17277526SVladimir.Kotal@Sun.COM 	while ((sp = freelist) != NULL)
17280Sstevel@tonic-gate 		{
17290Sstevel@tonic-gate 		if (sp->session != CK_INVALID_HANDLE && sp->pid == mypid)
17300Sstevel@tonic-gate 			{
17310Sstevel@tonic-gate 			rv = pFuncList->C_CloseSession(sp->session);
17320Sstevel@tonic-gate 			if (rv != CKR_OK)
17330Sstevel@tonic-gate 				{
17347211Sjp161948 				PK11err_add_data(PK11_F_FREE_ALL_SESSIONS,
17357211Sjp161948 					PK11_R_CLOSESESSION, rv);
17367211Sjp161948 				ret = 0;
17370Sstevel@tonic-gate 				}
17380Sstevel@tonic-gate 			}
17397526SVladimir.Kotal@Sun.COM 		freelist = sp->next;
17407526SVladimir.Kotal@Sun.COM 		pk11_free_nums(sp, optype);
17410Sstevel@tonic-gate 		OPENSSL_free(sp);
17420Sstevel@tonic-gate 		}
17437211Sjp161948 
17447526SVladimir.Kotal@Sun.COM 	(void) pthread_mutex_unlock(freelist_lock);
17450Sstevel@tonic-gate 	return ret;
17460Sstevel@tonic-gate 	}
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 
17497211Sjp161948 static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype)
17500Sstevel@tonic-gate 	{
17510Sstevel@tonic-gate 	CK_RV rv;
17527211Sjp161948 	CK_SLOT_ID myslot;
17537211Sjp161948 
17547211Sjp161948 	switch (optype)
17557211Sjp161948 		{
17567526SVladimir.Kotal@Sun.COM 		case OP_RSA:
17577526SVladimir.Kotal@Sun.COM 		case OP_DSA:
17587526SVladimir.Kotal@Sun.COM 		case OP_DH:
17597211Sjp161948 			myslot = pubkey_SLOTID;
17607211Sjp161948 			break;
17617211Sjp161948 		case OP_RAND:
17627211Sjp161948 			myslot = rand_SLOTID;
17637211Sjp161948 			break;
17647211Sjp161948 		case OP_DIGEST:
17657211Sjp161948 		case OP_CIPHER:
17667211Sjp161948 			myslot = SLOTID;
17677211Sjp161948 			break;
17687211Sjp161948 		default:
17697211Sjp161948 			PK11err(PK11_F_SETUP_SESSION,
17707211Sjp161948 			    PK11_R_INVALID_OPERATION_TYPE);
17717211Sjp161948 			return 0;
17727211Sjp161948 		}
17737211Sjp161948 
17740Sstevel@tonic-gate 	sp->session = CK_INVALID_HANDLE;
17757211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
17767211Sjp161948 	fprintf(stderr, "%s: myslot=%d optype=%d\n", PK11_DBG, myslot, optype);
17777211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
17787211Sjp161948 	rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION,
17790Sstevel@tonic-gate 		NULL_PTR, NULL_PTR, &sp->session);
17800Sstevel@tonic-gate 	if (rv == CKR_CRYPTOKI_NOT_INITIALIZED)
17810Sstevel@tonic-gate 		{
17820Sstevel@tonic-gate 		/*
17830Sstevel@tonic-gate 		 * We are probably a child process so force the
17840Sstevel@tonic-gate 		 * reinitialize of the session
17850Sstevel@tonic-gate 		 */
17867526SVladimir.Kotal@Sun.COM 		pk11_library_initialized = FALSE;
17870Sstevel@tonic-gate 		(void) pk11_library_init(NULL);
17887211Sjp161948 		rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION,
17890Sstevel@tonic-gate 			NULL_PTR, NULL_PTR, &sp->session);
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 	if (rv != CKR_OK)
17920Sstevel@tonic-gate 		{
17937211Sjp161948 		PK11err_add_data(PK11_F_SETUP_SESSION, PK11_R_OPENSESSION, rv);
17940Sstevel@tonic-gate 		return 0;
17950Sstevel@tonic-gate 		}
17960Sstevel@tonic-gate 
17977526SVladimir.Kotal@Sun.COM 	sp->pid = getpid();
17987526SVladimir.Kotal@Sun.COM 
17997526SVladimir.Kotal@Sun.COM 	switch (optype)
18000Sstevel@tonic-gate 		{
18016847Svk199839 #ifndef OPENSSL_NO_RSA
18027526SVladimir.Kotal@Sun.COM 		case OP_RSA:
18037526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
18047526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
18057526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_pub = NULL;
18067526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_n_num = NULL;
18077526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_e_num = NULL;
18087526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_priv = NULL;
18097526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_d_num = NULL;
18107526SVladimir.Kotal@Sun.COM 			break;
18117211Sjp161948 #endif	/* OPENSSL_NO_RSA */
18126847Svk199839 #ifndef OPENSSL_NO_DSA
18137526SVladimir.Kotal@Sun.COM 		case OP_DSA:
18147526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub_key = CK_INVALID_HANDLE;
18157526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv_key = CK_INVALID_HANDLE;
18167526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub = NULL;
18177526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub_num = NULL;
18187526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv = NULL;
18197526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv_num = NULL;
18207526SVladimir.Kotal@Sun.COM 			break;
18217211Sjp161948 #endif	/* OPENSSL_NO_DSA */
18226847Svk199839 #ifndef OPENSSL_NO_DH
18237526SVladimir.Kotal@Sun.COM 		case OP_DH:
18247526SVladimir.Kotal@Sun.COM 			sp->opdata_dh_key = CK_INVALID_HANDLE;
18257526SVladimir.Kotal@Sun.COM 			sp->opdata_dh = NULL;
18267526SVladimir.Kotal@Sun.COM 			sp->opdata_dh_priv_num = NULL;
18277526SVladimir.Kotal@Sun.COM 			break;
18287211Sjp161948 #endif	/* OPENSSL_NO_DH */
18297526SVladimir.Kotal@Sun.COM 		case OP_CIPHER:
18307526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key = CK_INVALID_HANDLE;
18317526SVladimir.Kotal@Sun.COM 			sp->opdata_encrypt = -1;
18327526SVladimir.Kotal@Sun.COM 			break;
18337526SVladimir.Kotal@Sun.COM 		}
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	return 1;
18360Sstevel@tonic-gate 	}
18370Sstevel@tonic-gate 
18386847Svk199839 #ifndef OPENSSL_NO_RSA
18396847Svk199839 /* Destroy RSA public key from single session. */
18406847Svk199839 int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock)
18416847Svk199839 	{
18426847Svk199839 	int ret = 0;
18436847Svk199839 
18447526SVladimir.Kotal@Sun.COM 	if (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE)
18456847Svk199839 		{
18467526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_rsa_pub_key,
18477526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_RSA);
18487526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
18497526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_pub = NULL;
18507526SVladimir.Kotal@Sun.COM 		if (sp->opdata_rsa_n_num != NULL)
18517526SVladimir.Kotal@Sun.COM 			{
18527526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_rsa_n_num);
18537526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_n_num = NULL;
18547526SVladimir.Kotal@Sun.COM 			}
18557526SVladimir.Kotal@Sun.COM 		if (sp->opdata_rsa_e_num != NULL)
18567526SVladimir.Kotal@Sun.COM 			{
18577526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_rsa_e_num);
18587526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_e_num = NULL;
18597526SVladimir.Kotal@Sun.COM 			}
18606847Svk199839 		}
18616847Svk199839 
18626847Svk199839 	return (ret);
18636847Svk199839 	}
18646847Svk199839 
18656847Svk199839 /* Destroy RSA private key from single session. */
18666847Svk199839 int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock)
18670Sstevel@tonic-gate 	{
18680Sstevel@tonic-gate 	int ret = 0;
18696847Svk199839 
18707526SVladimir.Kotal@Sun.COM 	if (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE)
18716847Svk199839 		{
18727526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_rsa_priv_key,
18737526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_RSA);
18747526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
18757526SVladimir.Kotal@Sun.COM 		sp->opdata_rsa_priv = NULL;
18767526SVladimir.Kotal@Sun.COM 		if (sp->opdata_rsa_d_num != NULL)
18777526SVladimir.Kotal@Sun.COM 			{
18787526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_rsa_d_num);
18797526SVladimir.Kotal@Sun.COM 			sp->opdata_rsa_d_num = NULL;
18807526SVladimir.Kotal@Sun.COM 			}
18816847Svk199839 		}
18826847Svk199839 
18836847Svk199839 	return (ret);
18846847Svk199839 	}
18856847Svk199839 
18866847Svk199839 /*
18877211Sjp161948  * Destroy RSA key object wrapper. If session is NULL, try to destroy all
18887211Sjp161948  * objects in the free list.
18896847Svk199839  */
18906847Svk199839 int pk11_destroy_rsa_key_objects(PK11_SESSION *session)
18916847Svk199839 	{
18926847Svk199839 	int ret = 1;
18930Sstevel@tonic-gate 	PK11_SESSION *sp = NULL;
18940Sstevel@tonic-gate 	PK11_SESSION *local_free_session;
18956847Svk199839 	CK_BBOOL uselock = TRUE;
18960Sstevel@tonic-gate 
18976847Svk199839 	if (session != NULL)
18980Sstevel@tonic-gate 		local_free_session = session;
18990Sstevel@tonic-gate 	else
19006847Svk199839 		{
19017526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_RSA].lock);
19027526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_RSA].head;
19036847Svk199839 		uselock = FALSE;
19046847Svk199839 		}
19050Sstevel@tonic-gate 
19066847Svk199839 	/*
19076847Svk199839 	 * go through the list of sessions and delete key objects
19086847Svk199839 	 */
19090Sstevel@tonic-gate 	while ((sp = local_free_session) != NULL)
19100Sstevel@tonic-gate 		{
19110Sstevel@tonic-gate 		local_free_session = sp->next;
19120Sstevel@tonic-gate 
19136847Svk199839 		/*
19146847Svk199839 		 * Do not terminate list traversal if one of the
19156847Svk199839 		 * destroy operations fails.
19166847Svk199839 		 */
19176847Svk199839 		if (pk11_destroy_rsa_object_pub(sp, uselock) == 0)
19180Sstevel@tonic-gate 			{
19196847Svk199839 			ret = 0;
19206847Svk199839 			continue;
19210Sstevel@tonic-gate 			}
19226847Svk199839 		if (pk11_destroy_rsa_object_priv(sp, uselock) == 0)
19236847Svk199839 			{
19246847Svk199839 			ret = 0;
19256847Svk199839 			continue;
19266847Svk199839 			}
19276847Svk199839 		}
19280Sstevel@tonic-gate 
19296847Svk199839 	if (session == NULL)
19307526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_RSA].lock);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	return ret;
19330Sstevel@tonic-gate 	}
19347211Sjp161948 #endif	/* OPENSSL_NO_RSA */
19350Sstevel@tonic-gate 
19366847Svk199839 #ifndef OPENSSL_NO_DSA
19376847Svk199839 /* Destroy DSA public key from single session. */
19386847Svk199839 int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock)
19390Sstevel@tonic-gate 	{
19400Sstevel@tonic-gate 	int ret = 0;
19416847Svk199839 
19427526SVladimir.Kotal@Sun.COM 	if (sp->opdata_dsa_pub_key != CK_INVALID_HANDLE)
19436847Svk199839 		{
19447526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_dsa_pub_key,
19457526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_DSA);
19467526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_pub_key = CK_INVALID_HANDLE;
19477526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_pub = NULL;
19487526SVladimir.Kotal@Sun.COM 		if (sp->opdata_dsa_pub_num != NULL)
19497526SVladimir.Kotal@Sun.COM 			{
19507526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_dsa_pub_num);
19517526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_pub_num = NULL;
19527526SVladimir.Kotal@Sun.COM 			}
19536847Svk199839 		}
19546847Svk199839 
19556847Svk199839 	return (ret);
19566847Svk199839 	}
19576847Svk199839 
19586847Svk199839 /* Destroy DSA private key from single session. */
19596847Svk199839 int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock)
19606847Svk199839 	{
19616847Svk199839 	int ret = 0;
19626847Svk199839 
19637526SVladimir.Kotal@Sun.COM 	if (sp->opdata_dsa_priv_key != CK_INVALID_HANDLE)
19646847Svk199839 		{
19657526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_dsa_priv_key,
19667526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_DSA);
19677526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_priv_key = CK_INVALID_HANDLE;
19687526SVladimir.Kotal@Sun.COM 		sp->opdata_dsa_priv = NULL;
19697526SVladimir.Kotal@Sun.COM 		if (sp->opdata_dsa_priv_num != NULL)
19707526SVladimir.Kotal@Sun.COM 			{
19717526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_dsa_priv_num);
19727526SVladimir.Kotal@Sun.COM 			sp->opdata_dsa_priv_num = NULL;
19737526SVladimir.Kotal@Sun.COM 			}
19746847Svk199839 		}
19756847Svk199839 
19766847Svk199839 	return (ret);
19776847Svk199839 	}
19786847Svk199839 
19796847Svk199839 /*
19807211Sjp161948  * Destroy DSA key object wrapper. If session is NULL, try to destroy all
19817211Sjp161948  * objects in the free list.
19826847Svk199839  */
19836847Svk199839 int pk11_destroy_dsa_key_objects(PK11_SESSION *session)
19846847Svk199839 	{
19856847Svk199839 	int ret = 1;
19860Sstevel@tonic-gate 	PK11_SESSION *sp = NULL;
19870Sstevel@tonic-gate 	PK11_SESSION *local_free_session;
19886847Svk199839 	CK_BBOOL uselock = TRUE;
19890Sstevel@tonic-gate 
19906847Svk199839 	if (session != NULL)
19910Sstevel@tonic-gate 		local_free_session = session;
19920Sstevel@tonic-gate 	else
19936847Svk199839 		{
19947526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_DSA].lock);
19957526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_DSA].head;
19966847Svk199839 		uselock = FALSE;
19976847Svk199839 		}
19986847Svk199839 
19996847Svk199839 	/*
20006847Svk199839 	 * go through the list of sessions and delete key objects
20016847Svk199839 	 */
20020Sstevel@tonic-gate 	while ((sp = local_free_session) != NULL)
20030Sstevel@tonic-gate 		{
20040Sstevel@tonic-gate 		local_free_session = sp->next;
20050Sstevel@tonic-gate 
20066847Svk199839 		/*
20076847Svk199839 		 * Do not terminate list traversal if one of the
20086847Svk199839 		 * destroy operations fails.
20096847Svk199839 		 */
20106847Svk199839 		if (pk11_destroy_dsa_object_pub(sp, uselock) == 0)
20110Sstevel@tonic-gate 			{
20126847Svk199839 			ret = 0;
20136847Svk199839 			continue;
20140Sstevel@tonic-gate 			}
20156847Svk199839 		if (pk11_destroy_dsa_object_priv(sp, uselock) == 0)
20166847Svk199839 			{
20176847Svk199839 			ret = 0;
20186847Svk199839 			continue;
20196847Svk199839 			}
20200Sstevel@tonic-gate 		}
20216847Svk199839 
20226847Svk199839 	if (session == NULL)
20237526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_DSA].lock);
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	return ret;
20260Sstevel@tonic-gate 	}
20277211Sjp161948 #endif	/* OPENSSL_NO_DSA */
20286847Svk199839 
20296847Svk199839 #ifndef OPENSSL_NO_DH
20306847Svk199839 /* Destroy DH key from single session. */
20316847Svk199839 int pk11_destroy_dh_object(PK11_SESSION *sp, CK_BBOOL uselock)
20326847Svk199839 	{
20336847Svk199839 	int ret = 0;
20346847Svk199839 
20357526SVladimir.Kotal@Sun.COM 	if (sp->opdata_dh_key != CK_INVALID_HANDLE)
20366847Svk199839 		{
20377526SVladimir.Kotal@Sun.COM 		TRY_OBJ_DESTROY(sp->session, sp->opdata_dh_key,
20387526SVladimir.Kotal@Sun.COM 		    ret, uselock, OP_DH);
20397526SVladimir.Kotal@Sun.COM 		sp->opdata_dh_key = CK_INVALID_HANDLE;
20407526SVladimir.Kotal@Sun.COM 		sp->opdata_dh = NULL;
20417526SVladimir.Kotal@Sun.COM 		if (sp->opdata_dh_priv_num != NULL)
20427526SVladimir.Kotal@Sun.COM 			{
20437526SVladimir.Kotal@Sun.COM 			BN_free(sp->opdata_dh_priv_num);
20447526SVladimir.Kotal@Sun.COM 			sp->opdata_dh_priv_num = NULL;
20457526SVladimir.Kotal@Sun.COM 			}
20466847Svk199839 		}
20476847Svk199839 
20486847Svk199839 	return (ret);
20496847Svk199839 	}
20506847Svk199839 
20516847Svk199839 /*
20526847Svk199839  * Destroy DH key object wrapper.
20536847Svk199839  *
20546847Svk199839  * arg0: pointer to PKCS#11 engine session structure
20556847Svk199839  *       if session is NULL, try to destroy all objects in the free list
20566847Svk199839  */
20576847Svk199839 int pk11_destroy_dh_key_objects(PK11_SESSION *session)
20586847Svk199839 	{
20596847Svk199839 	int ret = 1;
20606847Svk199839 	PK11_SESSION *sp = NULL;
20616847Svk199839 	PK11_SESSION *local_free_session;
20626847Svk199839 	CK_BBOOL uselock = TRUE;
20636847Svk199839 
20646847Svk199839 	if (session != NULL)
20656847Svk199839 		local_free_session = session;
20666847Svk199839 	else
20676847Svk199839 		{
20687526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_DH].lock);
20697526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_DH].head;
20706847Svk199839 		uselock = FALSE;
20716847Svk199839 		}
20726847Svk199839 
20736847Svk199839 	while ((sp = local_free_session) != NULL)
20746847Svk199839 		{
20756847Svk199839 		local_free_session = sp->next;
20766847Svk199839 
20776847Svk199839 		/*
20786847Svk199839 		 * Do not terminate list traversal if one of the
20796847Svk199839 		 * destroy operations fails.
20806847Svk199839 		 */
20816847Svk199839 		if (pk11_destroy_dh_object(sp, uselock) == 0)
20826847Svk199839 			{
20836847Svk199839 			ret = 0;
20846847Svk199839 			continue;
20856847Svk199839 			}
20866847Svk199839 		}
20876847Svk199839 err:
20886847Svk199839 	if (session == NULL)
20897526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_DH].lock);
20906847Svk199839 
20916847Svk199839 	return ret;
20926847Svk199839 	}
20937211Sjp161948 #endif	/* OPENSSL_NO_DH */
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate static int pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh)
20960Sstevel@tonic-gate 	{
20970Sstevel@tonic-gate 	CK_RV rv;
20980Sstevel@tonic-gate 	rv = pFuncList->C_DestroyObject(session, oh);
20990Sstevel@tonic-gate 	if (rv != CKR_OK)
21000Sstevel@tonic-gate 		{
21017211Sjp161948 		PK11err_add_data(PK11_F_DESTROY_OBJECT, PK11_R_DESTROYOBJECT,
21027211Sjp161948 		    rv);
21030Sstevel@tonic-gate 		return 0;
21040Sstevel@tonic-gate 		}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	return 1;
21070Sstevel@tonic-gate 	}
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate /* Symmetric ciphers and digests support functions
21110Sstevel@tonic-gate  */
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate static int
21140Sstevel@tonic-gate cipher_nid_to_pk11(int nid)
21150Sstevel@tonic-gate 	{
21160Sstevel@tonic-gate 	int i;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	for (i = 0; i < PK11_CIPHER_MAX; i++)
21190Sstevel@tonic-gate 		if (ciphers[i].nid == nid)
21200Sstevel@tonic-gate 			return (ciphers[i].id);
21210Sstevel@tonic-gate 	return (-1);
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate static int
21250Sstevel@tonic-gate pk11_usable_ciphers(const int **nids)
21260Sstevel@tonic-gate 	{
21270Sstevel@tonic-gate 	if (cipher_count > 0)
21280Sstevel@tonic-gate 		*nids = cipher_nids;
21290Sstevel@tonic-gate 	else
21300Sstevel@tonic-gate 		*nids = NULL;
21310Sstevel@tonic-gate 	return (cipher_count);
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate static int
21350Sstevel@tonic-gate pk11_usable_digests(const int **nids)
21360Sstevel@tonic-gate 	{
21370Sstevel@tonic-gate 	if (digest_count > 0)
21380Sstevel@tonic-gate 		*nids = digest_nids;
21390Sstevel@tonic-gate 	else
21400Sstevel@tonic-gate 		*nids = NULL;
21410Sstevel@tonic-gate 	return (digest_count);
21420Sstevel@tonic-gate 	}
21430Sstevel@tonic-gate 
21447211Sjp161948 /*
21457211Sjp161948  * Init context for encryption or decryption using a symmetric key.
21467211Sjp161948  */
21477211Sjp161948 static int pk11_init_symmetric(EVP_CIPHER_CTX *ctx, PK11_CIPHER *pcipher,
21487211Sjp161948 	PK11_SESSION *sp, CK_MECHANISM_PTR pmech)
21497211Sjp161948 	{
21507211Sjp161948 	CK_RV rv;
21517211Sjp161948 #ifdef	SOLARIS_AES_CTR
21527211Sjp161948 	CK_AES_CTR_PARAMS ctr_params;
21537211Sjp161948 #endif	/* SOLARIS_AES_CTR */
21547211Sjp161948 
21557211Sjp161948 	/*
21567211Sjp161948 	 * We expect pmech->mechanism to be already set and
21577211Sjp161948 	 * pParameter/ulParameterLen initialized to NULL/0 before
21587211Sjp161948 	 * pk11_init_symetric() is called.
21597211Sjp161948 	 */
21607211Sjp161948 	OPENSSL_assert(pmech->mechanism != NULL);
21617211Sjp161948 	OPENSSL_assert(pmech->pParameter == NULL);
21627211Sjp161948 	OPENSSL_assert(pmech->ulParameterLen == 0);
21637211Sjp161948 
21647211Sjp161948 #ifdef	SOLARIS_AES_CTR
21657211Sjp161948 	if (ctx->cipher->nid == NID_aes_128_ctr ||
21667211Sjp161948 	    ctx->cipher->nid == NID_aes_192_ctr ||
21677211Sjp161948 	    ctx->cipher->nid == NID_aes_256_ctr)
21687211Sjp161948 		{
21697211Sjp161948 		pmech->pParameter = (void *)(&ctr_params);
21707211Sjp161948 		pmech->ulParameterLen = sizeof(ctr_params);
21717211Sjp161948 		/*
21727211Sjp161948 		 * For now, we are limited to the fixed length of the counter,
21737211Sjp161948 		 * it covers the whole counter block. That's what RFC 4344
21747211Sjp161948 		 * needs. For more information on internal structure of the
21757211Sjp161948 		 * counter block, see RFC 3686. If needed in the future, we can
21767211Sjp161948 		 * add code so that the counter length can be set via
21777211Sjp161948 		 * ENGINE_ctrl() function.
21787211Sjp161948 		 */
21797211Sjp161948 		ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8;
21807211Sjp161948 		OPENSSL_assert(pcipher->iv_len == AES_BLOCK_SIZE);
21817534SVladimir.Kotal@Sun.COM 		(void) memcpy(ctr_params.cb, ctx->iv, AES_BLOCK_SIZE);
21827211Sjp161948 		}
21837211Sjp161948 	else
21847211Sjp161948 #endif	/* SOLARIS_AES_CTR */
21857211Sjp161948 		{
21867211Sjp161948 		if (pcipher->iv_len > 0)
21877211Sjp161948 			{
21887211Sjp161948 			pmech->pParameter = (void *)ctx->iv;
21897211Sjp161948 			pmech->ulParameterLen = pcipher->iv_len;
21907211Sjp161948 			}
21917211Sjp161948 		}
21927211Sjp161948 
21937211Sjp161948 	/* if we get here, the encryption needs to be reinitialized */
21947211Sjp161948 	if (ctx->encrypt)
21957526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_EncryptInit(sp->session, pmech,
21967526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key);
21977211Sjp161948 	else
21987526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_DecryptInit(sp->session, pmech,
21997526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key);
22007211Sjp161948 
22017211Sjp161948 	if (rv != CKR_OK)
22027211Sjp161948 		{
22037211Sjp161948 		PK11err_add_data(PK11_F_CIPHER_INIT, ctx->encrypt ?
22047211Sjp161948 		    PK11_R_ENCRYPTINIT : PK11_R_DECRYPTINIT, rv);
22057211Sjp161948 		pk11_return_session(sp, OP_CIPHER);
22067211Sjp161948 		return (0);
22077211Sjp161948 		}
22087211Sjp161948 
22097211Sjp161948 	return (1);
22107211Sjp161948 	}
22117211Sjp161948 
22127534SVladimir.Kotal@Sun.COM /* ARGSUSED */
22130Sstevel@tonic-gate static int
22140Sstevel@tonic-gate pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
22150Sstevel@tonic-gate     const unsigned char *iv, int enc)
22160Sstevel@tonic-gate 	{
22174320Sjp161948 	CK_MECHANISM mech;
22180Sstevel@tonic-gate 	int index;
22190Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data;
22200Sstevel@tonic-gate 	PK11_SESSION *sp;
22217211Sjp161948 	PK11_CIPHER *p_ciph_table_row;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	state->sp = NULL;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	index = cipher_nid_to_pk11(ctx->cipher->nid);
22260Sstevel@tonic-gate 	if (index < 0 || index >= PK11_CIPHER_MAX)
22270Sstevel@tonic-gate 		return 0;
22280Sstevel@tonic-gate 
22297211Sjp161948 	p_ciph_table_row = &ciphers[index];
22307211Sjp161948 	/*
22317211Sjp161948 	 * iv_len in the ctx->cipher structure is the maximum IV length for the
22327211Sjp161948 	 * current cipher and it must be less or equal to the IV length in our
22337211Sjp161948 	 * ciphers table. The key length must match precisely. Every application
22347211Sjp161948 	 * can define its own EVP functions so this code serves as a sanity
22357211Sjp161948 	 * check.
22367211Sjp161948 	 *
22377211Sjp161948 	 * Note that the reason why the IV length in ctx->cipher might be
22387211Sjp161948 	 * greater than the actual length is that OpenSSL uses BLOCK_CIPHER_defs
22397211Sjp161948 	 * macro to define functions that return EVP structures for all DES
22407211Sjp161948 	 * modes. So, even ECB modes get 8 byte IV.
22417211Sjp161948 	 */
22427211Sjp161948 	if (ctx->cipher->iv_len < p_ciph_table_row->iv_len ||
22437211Sjp161948 	    ctx->key_len != p_ciph_table_row->key_len)
22447211Sjp161948 		{
22457211Sjp161948 		PK11err(PK11_F_CIPHER_INIT, PK11_R_KEY_OR_IV_LEN_PROBLEM);
22460Sstevel@tonic-gate 		return 0;
22477211Sjp161948 		}
22487211Sjp161948 
22497211Sjp161948 	if ((sp = pk11_get_session(OP_CIPHER)) == NULL)
22500Sstevel@tonic-gate 		return 0;
22510Sstevel@tonic-gate 
22524320Sjp161948 	/* if applicable, the mechanism parameter is used for IV */
22537211Sjp161948 	mech.mechanism = p_ciph_table_row->mech_type;
22544320Sjp161948 	mech.pParameter = NULL;
22554320Sjp161948 	mech.ulParameterLen = 0;
22564320Sjp161948 
22570Sstevel@tonic-gate 	/* The key object is destroyed here if it is not the current key
22580Sstevel@tonic-gate 	 */
22597211Sjp161948 	(void) check_new_cipher_key(sp, key, p_ciph_table_row->key_len);
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	/* If the key is the same and the encryption is also the same,
22626847Svk199839 	 * then just reuse it. However, we must not forget to reinitialize the
22636847Svk199839 	 * context that was finalized in pk11_cipher_cleanup().
22640Sstevel@tonic-gate 	 */
22657526SVladimir.Kotal@Sun.COM 	if (sp->opdata_cipher_key != CK_INVALID_HANDLE &&
22667526SVladimir.Kotal@Sun.COM 	    sp->opdata_encrypt == ctx->encrypt)
22670Sstevel@tonic-gate 		{
22680Sstevel@tonic-gate 		state->sp = sp;
22697211Sjp161948 		if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0)
22706847Svk199839 			return (0);
22716847Svk199839 
22726847Svk199839 		return (1);
22730Sstevel@tonic-gate 		}
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	/* Check if the key has been invalidated. If so, a new key object
22760Sstevel@tonic-gate 	 * needs to be created.
22770Sstevel@tonic-gate 	 */
22787526SVladimir.Kotal@Sun.COM 	if (sp->opdata_cipher_key == CK_INVALID_HANDLE)
22790Sstevel@tonic-gate 		{
22807526SVladimir.Kotal@Sun.COM 		sp->opdata_cipher_key = pk11_get_cipher_key(
22817211Sjp161948 			ctx, key, p_ciph_table_row->key_type, sp);
22820Sstevel@tonic-gate 		}
22830Sstevel@tonic-gate 
22847526SVladimir.Kotal@Sun.COM 	if (sp->opdata_encrypt != ctx->encrypt && sp->opdata_encrypt != -1)
22850Sstevel@tonic-gate 		{
22860Sstevel@tonic-gate 		/* The previous encryption/decryption
22870Sstevel@tonic-gate 		 * is different. Need to terminate the previous
22880Sstevel@tonic-gate 		 * active encryption/decryption here
22890Sstevel@tonic-gate 		 */
22900Sstevel@tonic-gate 		if (!pk11_cipher_final(sp))
22910Sstevel@tonic-gate 			{
22927211Sjp161948 			pk11_return_session(sp, OP_CIPHER);
22930Sstevel@tonic-gate 			return 0;
22940Sstevel@tonic-gate 			}
22950Sstevel@tonic-gate 		}
22960Sstevel@tonic-gate 
22977526SVladimir.Kotal@Sun.COM 	if (sp->opdata_cipher_key == CK_INVALID_HANDLE)
22980Sstevel@tonic-gate 		{
22997211Sjp161948 		pk11_return_session(sp, OP_CIPHER);
23000Sstevel@tonic-gate 		return 0;
23010Sstevel@tonic-gate 		}
23020Sstevel@tonic-gate 
23036847Svk199839 	/* now initialize the context with a new key */
23047211Sjp161948 	if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0)
23056847Svk199839 		return (0);
23060Sstevel@tonic-gate 
23077526SVladimir.Kotal@Sun.COM 	sp->opdata_encrypt = ctx->encrypt;
23080Sstevel@tonic-gate 	state->sp = sp;
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	return 1;
23110Sstevel@tonic-gate 	}
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate /* When reusing the same key in an encryption/decryption session for a
23140Sstevel@tonic-gate  * decryption/encryption session, we need to close the active session
23150Sstevel@tonic-gate  * and recreate a new one. Note that the key is in the global session so
23160Sstevel@tonic-gate  * that it needs not be recreated.
23170Sstevel@tonic-gate  *
23180Sstevel@tonic-gate  * It is more appropriate to use C_En/DecryptFinish here. At the time of this
23190Sstevel@tonic-gate  * development, these two functions in the PKCS#11 libraries used return
23200Sstevel@tonic-gate  * unexpected errors when passing in 0 length output. It may be a good
23210Sstevel@tonic-gate  * idea to try them again if performance is a problem here and fix
23220Sstevel@tonic-gate  * C_En/DecryptFinial if there are bugs there causing the problem.
23230Sstevel@tonic-gate  */
23240Sstevel@tonic-gate static int
23250Sstevel@tonic-gate pk11_cipher_final(PK11_SESSION *sp)
23260Sstevel@tonic-gate 	{
23270Sstevel@tonic-gate 	CK_RV rv;
23280Sstevel@tonic-gate 
23297526SVladimir.Kotal@Sun.COM 	rv = pFuncList->C_CloseSession(sp->session);
23300Sstevel@tonic-gate 	if (rv != CKR_OK)
23310Sstevel@tonic-gate 		{
23327211Sjp161948 		PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_CLOSESESSION, rv);
23330Sstevel@tonic-gate 		return 0;
23340Sstevel@tonic-gate 		}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
23377526SVladimir.Kotal@Sun.COM 		NULL_PTR, NULL_PTR, &sp->session);
23380Sstevel@tonic-gate 	if (rv != CKR_OK)
23390Sstevel@tonic-gate 		{
23407211Sjp161948 		PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_OPENSESSION, rv);
23410Sstevel@tonic-gate 		return 0;
23420Sstevel@tonic-gate 		}
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	return 1;
23450Sstevel@tonic-gate 	}
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate /* An engine interface function. The calling function allocates sufficient
23480Sstevel@tonic-gate  * memory for the output buffer "out" to hold the results */
23490Sstevel@tonic-gate static int
23500Sstevel@tonic-gate pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
23510Sstevel@tonic-gate 	const unsigned char *in, unsigned int inl)
23520Sstevel@tonic-gate 	{
23530Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data;
23540Sstevel@tonic-gate 	PK11_SESSION *sp;
23550Sstevel@tonic-gate 	CK_RV rv;
23560Sstevel@tonic-gate 	unsigned long outl = inl;
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
23590Sstevel@tonic-gate 		return 0;
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	sp = (PK11_SESSION *) state->sp;
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	if (!inl)
23640Sstevel@tonic-gate 		return 1;
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	/* RC4 is the only stream cipher we support */
23670Sstevel@tonic-gate 	if (ctx->cipher->nid != NID_rc4 && (inl % ctx->cipher->block_size) != 0)
23680Sstevel@tonic-gate 		return 0;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	if (ctx->encrypt)
23710Sstevel@tonic-gate 		{
23727526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_EncryptUpdate(sp->session,
23730Sstevel@tonic-gate 			(unsigned char *)in, inl, out, &outl);
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 		if (rv != CKR_OK)
23760Sstevel@tonic-gate 			{
23777211Sjp161948 			PK11err_add_data(PK11_F_CIPHER_DO_CIPHER,
23787211Sjp161948 			    PK11_R_ENCRYPTUPDATE, rv);
23790Sstevel@tonic-gate 			return 0;
23800Sstevel@tonic-gate 			}
23810Sstevel@tonic-gate 		}
23820Sstevel@tonic-gate 	else
23830Sstevel@tonic-gate 		{
23847526SVladimir.Kotal@Sun.COM 		rv = pFuncList->C_DecryptUpdate(sp->session,
23850Sstevel@tonic-gate 			(unsigned char *)in, inl, out, &outl);
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 		if (rv != CKR_OK)
23880Sstevel@tonic-gate 			{
23897211Sjp161948 			PK11err_add_data(PK11_F_CIPHER_DO_CIPHER,
23907211Sjp161948 			    PK11_R_DECRYPTUPDATE, rv);
23910Sstevel@tonic-gate 			return 0;
23920Sstevel@tonic-gate 			}
23930Sstevel@tonic-gate 		}
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	/* for DES_CBC, DES3_CBC, AES_CBC, and RC4, the output size is always
23960Sstevel@tonic-gate 	 * the same size of input
23970Sstevel@tonic-gate 	 * The application has guaranteed to call the block ciphers with
23980Sstevel@tonic-gate 	 * correctly aligned buffers.
23990Sstevel@tonic-gate 	 */
24000Sstevel@tonic-gate 	if (inl != outl)
24010Sstevel@tonic-gate 		return 0;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	return 1;
24040Sstevel@tonic-gate 	}
24050Sstevel@tonic-gate 
24066847Svk199839 /*
24077211Sjp161948  * Return the session to the pool. Calling C_EncryptFinal() and C_DecryptFinal()
24087211Sjp161948  * here is the right thing because in EVP_DecryptFinal_ex(), engine's
24097211Sjp161948  * do_cipher() is not even called, and in EVP_EncryptFinal_ex() it is called but
24107211Sjp161948  * the engine can't find out that it's the finalizing call. We wouldn't
24117211Sjp161948  * necessarily have to finalize the context here since reinitializing it with
24127211Sjp161948  * C_(Encrypt|Decrypt)Init() should be fine but for the sake of correctness,
24137211Sjp161948  * let's do it. Some implementations might leak memory if the previously used
24147211Sjp161948  * context is initialized without finalizing it first.
24150Sstevel@tonic-gate  */
24160Sstevel@tonic-gate static int
24170Sstevel@tonic-gate pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx)
24180Sstevel@tonic-gate 	{
24196847Svk199839 	CK_RV rv;
24207211Sjp161948 	CK_ULONG len = EVP_MAX_BLOCK_LENGTH;
24216847Svk199839 	CK_BYTE buf[EVP_MAX_BLOCK_LENGTH];
24220Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = ctx->cipher_data;
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	if (state != NULL && state->sp != NULL)
24250Sstevel@tonic-gate 		{
24266847Svk199839 		/*
24276847Svk199839 		 * We are not interested in the data here, we just need to get
24286847Svk199839 		 * rid of the context.
24296847Svk199839 		 */
24306847Svk199839 		if (ctx->encrypt)
24316847Svk199839 			rv = pFuncList->C_EncryptFinal(
24327526SVladimir.Kotal@Sun.COM 			    state->sp->session, buf, &len);
24336847Svk199839 		else
24346847Svk199839 			rv = pFuncList->C_DecryptFinal(
24357526SVladimir.Kotal@Sun.COM 			    state->sp->session, buf, &len);
24366847Svk199839 
24376847Svk199839 		if (rv != CKR_OK)
24386847Svk199839 			{
24397211Sjp161948 			PK11err_add_data(PK11_F_CIPHER_CLEANUP, ctx->encrypt ?
24407211Sjp161948 			    PK11_R_ENCRYPTFINAL : PK11_R_DECRYPTFINAL, rv);
24417211Sjp161948 			pk11_return_session(state->sp, OP_CIPHER);
24426847Svk199839 			return (0);
24436847Svk199839 			}
24446847Svk199839 
24457211Sjp161948 		pk11_return_session(state->sp, OP_CIPHER);
24460Sstevel@tonic-gate 		state->sp = NULL;
24470Sstevel@tonic-gate 		}
24480Sstevel@tonic-gate 
24496847Svk199839 	return (1);
24506847Svk199839 	}
24516847Svk199839 
24520Sstevel@tonic-gate /* Registered by the ENGINE when used to find out how to deal with
24530Sstevel@tonic-gate  * a particular NID in the ENGINE. This says what we'll do at the
24540Sstevel@tonic-gate  * top level - note, that list is restricted by what we answer with
24550Sstevel@tonic-gate  */
24567534SVladimir.Kotal@Sun.COM /* ARGSUSED */
24570Sstevel@tonic-gate static int
24580Sstevel@tonic-gate pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
24590Sstevel@tonic-gate 	const int **nids, int nid)
24600Sstevel@tonic-gate 	{
24610Sstevel@tonic-gate 	if (!cipher)
24620Sstevel@tonic-gate 		return (pk11_usable_ciphers(nids));
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	switch (nid)
24650Sstevel@tonic-gate 		{
24660Sstevel@tonic-gate 		case NID_des_ede3_cbc:
24670Sstevel@tonic-gate 			*cipher = &pk11_3des_cbc;
24680Sstevel@tonic-gate 			break;
24690Sstevel@tonic-gate 		case NID_des_cbc:
24700Sstevel@tonic-gate 			*cipher = &pk11_des_cbc;
24710Sstevel@tonic-gate 			break;
24727211Sjp161948 		case NID_des_ede3_ecb:
24737211Sjp161948 			*cipher = &pk11_3des_ecb;
24747211Sjp161948 			break;
24757211Sjp161948 		case NID_des_ecb:
24767211Sjp161948 			*cipher = &pk11_des_ecb;
24777211Sjp161948 			break;
24780Sstevel@tonic-gate 		case NID_aes_128_cbc:
24797211Sjp161948 			*cipher = &pk11_aes_128_cbc;
24807211Sjp161948 			break;
24817211Sjp161948 		case NID_aes_192_cbc:
24827211Sjp161948 			*cipher = &pk11_aes_192_cbc;
24837211Sjp161948 			break;
24847211Sjp161948 		case NID_aes_256_cbc:
24857211Sjp161948 			*cipher = &pk11_aes_256_cbc;
24867211Sjp161948 			break;
24877211Sjp161948 		case NID_aes_128_ecb:
24887211Sjp161948 			*cipher = &pk11_aes_128_ecb;
24897211Sjp161948 			break;
24907211Sjp161948 		case NID_aes_192_ecb:
24917211Sjp161948 			*cipher = &pk11_aes_192_ecb;
24927211Sjp161948 			break;
24937211Sjp161948 		case NID_aes_256_ecb:
24947211Sjp161948 			*cipher = &pk11_aes_256_ecb;
24957211Sjp161948 			break;
24967211Sjp161948 		case NID_bf_cbc:
24977211Sjp161948 			*cipher = &pk11_bf_cbc;
24980Sstevel@tonic-gate 			break;
24990Sstevel@tonic-gate 		case NID_rc4:
25000Sstevel@tonic-gate 			*cipher = &pk11_rc4;
25010Sstevel@tonic-gate 			break;
25020Sstevel@tonic-gate 		default:
25037211Sjp161948 #ifdef	SOLARIS_AES_CTR
25047211Sjp161948 			/*
25057211Sjp161948 			 * These can't be in separated cases because the NIDs
25067211Sjp161948 			 * here are not constants.
25077211Sjp161948 			 */
25087211Sjp161948 			if (nid == NID_aes_128_ctr)
25097211Sjp161948 				*cipher = &pk11_aes_128_ctr;
25107211Sjp161948 			else if (nid == NID_aes_192_ctr)
25117211Sjp161948 				*cipher = &pk11_aes_192_ctr;
25127211Sjp161948 			else if (nid == NID_aes_256_ctr)
25137211Sjp161948 				*cipher = &pk11_aes_256_ctr;
25147211Sjp161948 			else
25157211Sjp161948 #endif	/* SOLARIS_AES_CTR */
25160Sstevel@tonic-gate 			*cipher = NULL;
25170Sstevel@tonic-gate 			break;
25180Sstevel@tonic-gate 		}
25190Sstevel@tonic-gate 	return (*cipher != NULL);
25200Sstevel@tonic-gate 	}
25210Sstevel@tonic-gate 
25227534SVladimir.Kotal@Sun.COM /* ARGSUSED */
25230Sstevel@tonic-gate static int
25240Sstevel@tonic-gate pk11_engine_digests(ENGINE *e, const EVP_MD **digest,
25250Sstevel@tonic-gate 	const int **nids, int nid)
25260Sstevel@tonic-gate 	{
25270Sstevel@tonic-gate 	if (!digest)
25280Sstevel@tonic-gate 		return (pk11_usable_digests(nids));
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	switch (nid)
25310Sstevel@tonic-gate 		{
25320Sstevel@tonic-gate 		case NID_md5:
25330Sstevel@tonic-gate 			*digest = &pk11_md5;
25340Sstevel@tonic-gate 			break;
25350Sstevel@tonic-gate 		case NID_sha1:
25360Sstevel@tonic-gate 			*digest = &pk11_sha1;
25370Sstevel@tonic-gate 			break;
25387211Sjp161948 		case NID_sha224:
25397211Sjp161948 			*digest = &pk11_sha224;
25407211Sjp161948 			break;
25417211Sjp161948 		case NID_sha256:
25427211Sjp161948 			*digest = &pk11_sha256;
25437211Sjp161948 			break;
25447211Sjp161948 		case NID_sha384:
25457211Sjp161948 			*digest = &pk11_sha384;
25467211Sjp161948 			break;
25477211Sjp161948 		case NID_sha512:
25487211Sjp161948 			*digest = &pk11_sha512;
25497211Sjp161948 			break;
25500Sstevel@tonic-gate 		default:
25510Sstevel@tonic-gate 			*digest = NULL;
25520Sstevel@tonic-gate 			break;
25530Sstevel@tonic-gate 		}
25540Sstevel@tonic-gate 	return (*digest != NULL);
25550Sstevel@tonic-gate 	}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate /* Create a secret key object in a PKCS#11 session
25590Sstevel@tonic-gate  */
25600Sstevel@tonic-gate static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx,
25610Sstevel@tonic-gate 	const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp)
25620Sstevel@tonic-gate 	{
25630Sstevel@tonic-gate 	CK_RV rv;
25640Sstevel@tonic-gate 	CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
25650Sstevel@tonic-gate 	CK_OBJECT_CLASS obj_key = CKO_SECRET_KEY;
25660Sstevel@tonic-gate 	CK_ULONG ul_key_attr_count = 6;
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	CK_ATTRIBUTE  a_key_template[] =
25690Sstevel@tonic-gate 		{
25700Sstevel@tonic-gate 		{CKA_CLASS, (void*) NULL, sizeof(CK_OBJECT_CLASS)},
25710Sstevel@tonic-gate 		{CKA_KEY_TYPE, (void*) NULL, sizeof(CK_KEY_TYPE)},
25720Sstevel@tonic-gate 		{CKA_TOKEN, &false, sizeof(false)},
25730Sstevel@tonic-gate 		{CKA_ENCRYPT, &true, sizeof(true)},
25740Sstevel@tonic-gate 		{CKA_DECRYPT, &true, sizeof(true)},
25750Sstevel@tonic-gate 		{CKA_VALUE, (void*) NULL, 0},
25760Sstevel@tonic-gate 		};
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	/* Create secret key object in global_session. All other sessions
25790Sstevel@tonic-gate 	 * can use the key handles. Here is why:
25800Sstevel@tonic-gate 	 * OpenSSL will call EncryptInit and EncryptUpdate using a secret key.
25810Sstevel@tonic-gate 	 * It may then call DecryptInit and DecryptUpdate using the same key.
25820Sstevel@tonic-gate 	 * To use the same key object, we need to call EncryptFinal with
25830Sstevel@tonic-gate 	 * a 0 length message. Currently, this does not work for 3DES
25840Sstevel@tonic-gate 	 * mechanism. To get around this problem, we close the session and
25850Sstevel@tonic-gate 	 * then create a new session to use the same key object. When a session
25860Sstevel@tonic-gate 	 * is closed, all the object handles will be invalid. Thus, create key
25870Sstevel@tonic-gate 	 * objects in a global session, an individual session may be closed to
25880Sstevel@tonic-gate 	 * terminate the active operation.
25890Sstevel@tonic-gate 	 */
25900Sstevel@tonic-gate 	CK_SESSION_HANDLE session = global_session;
25910Sstevel@tonic-gate 	a_key_template[0].pValue = &obj_key;
25920Sstevel@tonic-gate 	a_key_template[1].pValue = &key_type;
25930Sstevel@tonic-gate 	a_key_template[5].pValue = (void *) key;
25940Sstevel@tonic-gate 	a_key_template[5].ulValueLen = (unsigned long) ctx->key_len;
25950Sstevel@tonic-gate 
25964602Sjp161948 	rv = pFuncList->C_CreateObject(session,
25974602Sjp161948 		a_key_template, ul_key_attr_count, &h_key);
25980Sstevel@tonic-gate 	if (rv != CKR_OK)
25990Sstevel@tonic-gate 		{
26007211Sjp161948 		PK11err_add_data(PK11_F_GET_CIPHER_KEY, PK11_R_CREATEOBJECT,
26017211Sjp161948 		    rv);
26020Sstevel@tonic-gate 		goto err;
26030Sstevel@tonic-gate 		}
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	/* Save the key information used in this session.
26060Sstevel@tonic-gate 	 * The max can be saved is PK11_KEY_LEN_MAX.
26070Sstevel@tonic-gate 	 */
26087526SVladimir.Kotal@Sun.COM 	sp->opdata_key_len = ctx->key_len > PK11_KEY_LEN_MAX ?
26090Sstevel@tonic-gate 		PK11_KEY_LEN_MAX : ctx->key_len;
26107534SVladimir.Kotal@Sun.COM 	(void) memcpy(sp->opdata_key, key, sp->opdata_key_len);
26110Sstevel@tonic-gate err:
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	return h_key;
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate static int
26170Sstevel@tonic-gate md_nid_to_pk11(int nid)
26180Sstevel@tonic-gate 	{
26190Sstevel@tonic-gate 	int i;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	for (i = 0; i < PK11_DIGEST_MAX; i++)
26220Sstevel@tonic-gate 		if (digests[i].nid == nid)
26230Sstevel@tonic-gate 			return (digests[i].id);
26240Sstevel@tonic-gate 	return (-1);
26250Sstevel@tonic-gate 	}
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate static int
26280Sstevel@tonic-gate pk11_digest_init(EVP_MD_CTX *ctx)
26290Sstevel@tonic-gate         {
26300Sstevel@tonic-gate 	CK_RV rv;
26314320Sjp161948 	CK_MECHANISM mech;
26320Sstevel@tonic-gate 	int index;
26330Sstevel@tonic-gate 	PK11_SESSION *sp;
26340Sstevel@tonic-gate 	PK11_DIGEST *pdp;
26350Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data;
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	state->sp = NULL;
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	index = md_nid_to_pk11(ctx->digest->type);
26400Sstevel@tonic-gate 	if (index < 0 || index >= PK11_DIGEST_MAX)
26410Sstevel@tonic-gate 		return 0;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	pdp = &digests[index];
26447211Sjp161948 	if ((sp = pk11_get_session(OP_DIGEST)) == NULL)
26450Sstevel@tonic-gate 		return 0;
26460Sstevel@tonic-gate 
26474320Sjp161948 	/* at present, no parameter is needed for supported digests */
26484320Sjp161948 	mech.mechanism = pdp->mech_type;
26494320Sjp161948 	mech.pParameter = NULL;
26504320Sjp161948 	mech.ulParameterLen = 0;
26514320Sjp161948 
26524320Sjp161948 	rv = pFuncList->C_DigestInit(sp->session, &mech);
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	if (rv != CKR_OK)
26550Sstevel@tonic-gate 		{
26567211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_INIT, PK11_R_DIGESTINIT, rv);
26577211Sjp161948 		pk11_return_session(sp, OP_DIGEST);
26580Sstevel@tonic-gate 		return 0;
26590Sstevel@tonic-gate 		}
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	state->sp = sp;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	return 1;
26640Sstevel@tonic-gate 	}
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate static int
26672139Sjp161948 pk11_digest_update(EVP_MD_CTX *ctx,const void *data,size_t count)
26680Sstevel@tonic-gate         {
26690Sstevel@tonic-gate 	CK_RV rv;
26700Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data;
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	/* 0 length message will cause a failure in C_DigestFinal */
26730Sstevel@tonic-gate 	if (count == 0)
26740Sstevel@tonic-gate 		return 1;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
26770Sstevel@tonic-gate 		return 0;
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 	rv = pFuncList->C_DigestUpdate(state->sp->session, (CK_BYTE *) data,
26800Sstevel@tonic-gate 		count);
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 	if (rv != CKR_OK)
26830Sstevel@tonic-gate 		{
26847211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_UPDATE, PK11_R_DIGESTUPDATE, rv);
26857211Sjp161948 		pk11_return_session(state->sp, OP_DIGEST);
26860Sstevel@tonic-gate 		state->sp = NULL;
26870Sstevel@tonic-gate 		return 0;
26880Sstevel@tonic-gate 		}
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	return 1;
26910Sstevel@tonic-gate 	}
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate static int
26940Sstevel@tonic-gate pk11_digest_final(EVP_MD_CTX *ctx,unsigned char *md)
26950Sstevel@tonic-gate         {
26960Sstevel@tonic-gate 	CK_RV rv;
26970Sstevel@tonic-gate 	unsigned long len;
26980Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data;
26990Sstevel@tonic-gate 	len = ctx->digest->md_size;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
27020Sstevel@tonic-gate 		return 0;
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	rv = pFuncList->C_DigestFinal(state->sp->session, md, &len);
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 	if (rv != CKR_OK)
27070Sstevel@tonic-gate 		{
27087211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_FINAL, PK11_R_DIGESTFINAL, rv);
27097211Sjp161948 		pk11_return_session(state->sp, OP_DIGEST);
27100Sstevel@tonic-gate 		state->sp = NULL;
27110Sstevel@tonic-gate 		return 0;
27120Sstevel@tonic-gate 		}
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	if (ctx->digest->md_size != len)
27150Sstevel@tonic-gate 		return 0;
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	/* Final is called and digest is returned, so return the session
27180Sstevel@tonic-gate 	 * to the pool
27190Sstevel@tonic-gate 	 */
27207211Sjp161948 	pk11_return_session(state->sp, OP_DIGEST);
27210Sstevel@tonic-gate 	state->sp = NULL;
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	return 1;
27240Sstevel@tonic-gate 	}
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate static int
27270Sstevel@tonic-gate pk11_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
27280Sstevel@tonic-gate         {
27290Sstevel@tonic-gate 	CK_RV rv;
27300Sstevel@tonic-gate 	int ret = 0;
27310Sstevel@tonic-gate 	PK11_CIPHER_STATE *state, *state_to;
27320Sstevel@tonic-gate 	CK_BYTE_PTR pstate = NULL;
27330Sstevel@tonic-gate 	CK_ULONG ul_state_len;
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 	/* The copy-from state */
27360Sstevel@tonic-gate 	state = (PK11_CIPHER_STATE *) from->md_data;
27370Sstevel@tonic-gate 	if (state == NULL || state->sp == NULL)
27380Sstevel@tonic-gate 		goto err;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	/* Initialize the copy-to state */
27410Sstevel@tonic-gate 	if (!pk11_digest_init(to))
27420Sstevel@tonic-gate 		goto err;
27430Sstevel@tonic-gate 	state_to = (PK11_CIPHER_STATE *) to->md_data;
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	/* Get the size of the operation state of the copy-from session */
27460Sstevel@tonic-gate 	rv = pFuncList->C_GetOperationState(state->sp->session, NULL,
27470Sstevel@tonic-gate 		&ul_state_len);
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	if (rv != CKR_OK)
27500Sstevel@tonic-gate 		{
27517211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE,
27527211Sjp161948 		    rv);
27530Sstevel@tonic-gate 		goto err;
27540Sstevel@tonic-gate 		}
27550Sstevel@tonic-gate 	if (ul_state_len == 0)
27560Sstevel@tonic-gate 		{
27570Sstevel@tonic-gate 		goto err;
27580Sstevel@tonic-gate 		}
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	pstate = OPENSSL_malloc(ul_state_len);
27610Sstevel@tonic-gate 	if (pstate == NULL)
27620Sstevel@tonic-gate 		{
27636847Svk199839 		PK11err(PK11_F_DIGEST_COPY, PK11_R_MALLOC_FAILURE);
27640Sstevel@tonic-gate 		goto err;
27650Sstevel@tonic-gate 		}
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	/* Get the operation state of the copy-from session */
27680Sstevel@tonic-gate 	rv = pFuncList->C_GetOperationState(state->sp->session, pstate,
27690Sstevel@tonic-gate 		&ul_state_len);
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	if (rv != CKR_OK)
27720Sstevel@tonic-gate 		{
27737211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE,
27747211Sjp161948 		    rv);
27750Sstevel@tonic-gate 		goto err;
27760Sstevel@tonic-gate 		}
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	/* Set the operation state of the copy-to session */
27790Sstevel@tonic-gate 	rv = pFuncList->C_SetOperationState(state_to->sp->session, pstate,
27800Sstevel@tonic-gate 		ul_state_len, 0, 0);
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	if (rv != CKR_OK)
27830Sstevel@tonic-gate 		{
27847211Sjp161948 		PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_SET_OPERATION_STATE, rv);
27850Sstevel@tonic-gate 		goto err;
27860Sstevel@tonic-gate 		}
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	ret = 1;
27890Sstevel@tonic-gate err:
27900Sstevel@tonic-gate 	if (pstate != NULL)
27910Sstevel@tonic-gate 		OPENSSL_free(pstate);
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	return ret;
27940Sstevel@tonic-gate 	}
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate /* Return any pending session state to the pool */
27970Sstevel@tonic-gate static int
27980Sstevel@tonic-gate pk11_digest_cleanup(EVP_MD_CTX *ctx)
27990Sstevel@tonic-gate 	{
28000Sstevel@tonic-gate 	PK11_CIPHER_STATE *state = ctx->md_data;
28014602Sjp161948 	unsigned char buf[EVP_MAX_MD_SIZE];
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	if (state != NULL && state->sp != NULL)
28040Sstevel@tonic-gate 		{
28054602Sjp161948 		/*
28064602Sjp161948 		 * If state->sp is not NULL then pk11_digest_final() has not
28074602Sjp161948 		 * been called yet. We must call it now to free any memory
28084602Sjp161948 		 * that might have been allocated in the token when
28094602Sjp161948 		 * pk11_digest_init() was called.
28104602Sjp161948 		 */
28117526SVladimir.Kotal@Sun.COM 		(void) pk11_digest_final(ctx, buf);
28127211Sjp161948 		pk11_return_session(state->sp, OP_DIGEST);
28130Sstevel@tonic-gate 		state->sp = NULL;
28140Sstevel@tonic-gate 		}
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	return 1;
28170Sstevel@tonic-gate 	}
28180Sstevel@tonic-gate 
28196847Svk199839 /*
28207211Sjp161948  * Check if the new key is the same as the key object in the session. If the key
28217211Sjp161948  * is the same, no need to create a new key object. Otherwise, the old key
28227211Sjp161948  * object needs to be destroyed and a new one will be created. Return 1 for
28237211Sjp161948  * cache hit, 0 for cache miss. Note that we must check the key length first
28247211Sjp161948  * otherwise we could end up reusing a different, longer key with the same
28257211Sjp161948  * prefix.
28260Sstevel@tonic-gate  */
28277211Sjp161948 static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key,
28287211Sjp161948 	int key_len)
28290Sstevel@tonic-gate 	{
28307526SVladimir.Kotal@Sun.COM 	if (sp->opdata_key_len != key_len ||
28317526SVladimir.Kotal@Sun.COM 	    memcmp(sp->opdata_key, key, key_len) != 0)
28326847Svk199839 		{
28337211Sjp161948 		(void) pk11_destroy_cipher_key_objects(sp);
28346847Svk199839 		return (0);
28356847Svk199839 		}
28366847Svk199839 	return (1);
28370Sstevel@tonic-gate 	}
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate /* Destroy one or more secret key objects.
28400Sstevel@tonic-gate  */
28410Sstevel@tonic-gate static int pk11_destroy_cipher_key_objects(PK11_SESSION *session)
28420Sstevel@tonic-gate 	{
28430Sstevel@tonic-gate 	int ret = 0;
28440Sstevel@tonic-gate 	PK11_SESSION *sp = NULL;
28450Sstevel@tonic-gate 	PK11_SESSION *local_free_session;
28460Sstevel@tonic-gate 
28477526SVladimir.Kotal@Sun.COM 	if (session != NULL)
28480Sstevel@tonic-gate 		local_free_session = session;
28490Sstevel@tonic-gate 	else
28507526SVladimir.Kotal@Sun.COM 		{
28517526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_lock(session_cache[OP_CIPHER].lock);
28527526SVladimir.Kotal@Sun.COM 		local_free_session = session_cache[OP_CIPHER].head;
28537526SVladimir.Kotal@Sun.COM 		}
28547526SVladimir.Kotal@Sun.COM 
28550Sstevel@tonic-gate 	while ((sp = local_free_session) != NULL)
28560Sstevel@tonic-gate 		{
28570Sstevel@tonic-gate 		local_free_session = sp->next;
28580Sstevel@tonic-gate 
28597526SVladimir.Kotal@Sun.COM 		if (sp->opdata_cipher_key != CK_INVALID_HANDLE)
28600Sstevel@tonic-gate 			{
28610Sstevel@tonic-gate 			/* The secret key object is created in the
28620Sstevel@tonic-gate 			 * global_session. See pk11_get_cipher_key
28630Sstevel@tonic-gate 			 */
28640Sstevel@tonic-gate 			if (pk11_destroy_object(global_session,
28657526SVladimir.Kotal@Sun.COM 				sp->opdata_cipher_key) == 0)
28660Sstevel@tonic-gate 				goto err;
28677526SVladimir.Kotal@Sun.COM 			sp->opdata_cipher_key = CK_INVALID_HANDLE;
28680Sstevel@tonic-gate 			}
28690Sstevel@tonic-gate 		}
28700Sstevel@tonic-gate 	ret = 1;
28710Sstevel@tonic-gate err:
28727526SVladimir.Kotal@Sun.COM 
28737526SVladimir.Kotal@Sun.COM 	if (session == NULL)
28747526SVladimir.Kotal@Sun.COM 		(void) pthread_mutex_unlock(session_cache[OP_CIPHER].lock);
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	return ret;
28770Sstevel@tonic-gate 	}
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate /*
28817211Sjp161948  * Public key mechanisms optionally supported
28820Sstevel@tonic-gate  *
28830Sstevel@tonic-gate  * CKM_RSA_X_509
28840Sstevel@tonic-gate  * CKM_RSA_PKCS
28850Sstevel@tonic-gate  * CKM_DSA
28860Sstevel@tonic-gate  *
28877211Sjp161948  * The first slot that supports at least one of those mechanisms is chosen as a
28887211Sjp161948  * public key slot.
28890Sstevel@tonic-gate  *
28900Sstevel@tonic-gate  * Symmetric ciphers optionally supported
28910Sstevel@tonic-gate  *
28920Sstevel@tonic-gate  * CKM_DES3_CBC
28930Sstevel@tonic-gate  * CKM_DES_CBC
28940Sstevel@tonic-gate  * CKM_AES_CBC
28957211Sjp161948  * CKM_DES3_ECB
28967211Sjp161948  * CKM_DES_ECB
28977211Sjp161948  * CKM_AES_ECB
28987211Sjp161948  * CKM_AES_CTR
28990Sstevel@tonic-gate  * CKM_RC4
29007211Sjp161948  * CKM_BLOWFISH_CBC
29010Sstevel@tonic-gate  *
29020Sstevel@tonic-gate  * Digests optionally supported
29030Sstevel@tonic-gate  *
29040Sstevel@tonic-gate  * CKM_MD5
29050Sstevel@tonic-gate  * CKM_SHA_1
29067211Sjp161948  * CKM_SHA224
29077211Sjp161948  * CKM_SHA256
29087211Sjp161948  * CKM_SHA384
29097211Sjp161948  * CKM_SHA512
29107211Sjp161948  *
29117211Sjp161948  * The output of this function is a set of global variables indicating which
29127211Sjp161948  * mechanisms from RSA, DSA, DH and RAND are present, and also two arrays of
29137211Sjp161948  * mechanisms, one for symmetric ciphers and one for digests. Also, 3 global
29147211Sjp161948  * variables carry information about which slot was chosen for (a) public key
29157211Sjp161948  * mechanisms, (b) random operations, and (c) symmetric ciphers and digests.
29160Sstevel@tonic-gate  */
29170Sstevel@tonic-gate static int
29187211Sjp161948 pk11_choose_slots(int *any_slot_found)
29190Sstevel@tonic-gate 	{
29200Sstevel@tonic-gate 	CK_SLOT_ID_PTR pSlotList = NULL_PTR;
29210Sstevel@tonic-gate 	CK_ULONG ulSlotCount = 0;
29220Sstevel@tonic-gate 	CK_MECHANISM_INFO mech_info;
29230Sstevel@tonic-gate 	CK_TOKEN_INFO token_info;
29240Sstevel@tonic-gate 	int i;
29250Sstevel@tonic-gate 	CK_RV rv;
29260Sstevel@tonic-gate 	CK_SLOT_ID best_slot_sofar;
29270Sstevel@tonic-gate 	CK_BBOOL found_candidate_slot = CK_FALSE;
29280Sstevel@tonic-gate 	int slot_n_cipher = 0;
29290Sstevel@tonic-gate 	int slot_n_digest = 0;
29300Sstevel@tonic-gate 	CK_SLOT_ID current_slot = 0;
29310Sstevel@tonic-gate 	int current_slot_n_cipher = 0;
29320Sstevel@tonic-gate 	int current_slot_n_digest = 0;
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 	int local_cipher_nids[PK11_CIPHER_MAX];
29350Sstevel@tonic-gate 	int local_digest_nids[PK11_DIGEST_MAX];
29367211Sjp161948 
29377211Sjp161948 	/* let's initialize the output parameter */
29387211Sjp161948 	if (any_slot_found != NULL)
29397211Sjp161948 		*any_slot_found = 0;
29407211Sjp161948 
29417211Sjp161948 	/* Get slot list for memory allocation */
29420Sstevel@tonic-gate 	rv = pFuncList->C_GetSlotList(0, NULL_PTR, &ulSlotCount);
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	if (rv != CKR_OK)
29450Sstevel@tonic-gate 		{
29467211Sjp161948 		PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv);
29477211Sjp161948 		return 0;
29480Sstevel@tonic-gate 		}
29490Sstevel@tonic-gate 
29507211Sjp161948 	/* it's not an error if we didn't find any providers */
29510Sstevel@tonic-gate 	if (ulSlotCount == 0)
29520Sstevel@tonic-gate 		{
29537211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29547211Sjp161948 		fprintf(stderr, "%s: no crypto providers found\n", PK11_DBG);
29557211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29567211Sjp161948 		return 1;
29570Sstevel@tonic-gate 		}
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID));
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	if (pSlotList == NULL)
29620Sstevel@tonic-gate 		{
29636847Svk199839 		PK11err(PK11_F_CHOOSE_SLOT, PK11_R_MALLOC_FAILURE);
29647211Sjp161948 		return 0;
29650Sstevel@tonic-gate 		}
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	/* Get the slot list for processing */
29680Sstevel@tonic-gate 	rv = pFuncList->C_GetSlotList(0, pSlotList, &ulSlotCount);
29690Sstevel@tonic-gate 	if (rv != CKR_OK)
29700Sstevel@tonic-gate 		{
29717211Sjp161948 		PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv);
29720Sstevel@tonic-gate 		OPENSSL_free(pSlotList);
29737211Sjp161948 		return 0;
29740Sstevel@tonic-gate 		}
29750Sstevel@tonic-gate 
29767211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29777211Sjp161948 	fprintf(stderr, "%s: provider: %s\n", PK11_DBG, def_PK11_LIBNAME);
29787211Sjp161948 	fprintf(stderr, "%s: number of slots: %d\n", PK11_DBG, ulSlotCount);
29797211Sjp161948 
29807211Sjp161948 	fprintf(stderr, "%s: == checking rand slots ==\n", PK11_DBG);
29817211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29827211Sjp161948 	for (i = 0; i < ulSlotCount; i++)
29837211Sjp161948 		{
29847211Sjp161948 		current_slot = pSlotList[i];
29857211Sjp161948 
29867211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29877211Sjp161948 	fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i);
29887211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29897211Sjp161948 		/* Check if slot has random support. */
29907211Sjp161948 		rv = pFuncList->C_GetTokenInfo(current_slot, &token_info);
29917211Sjp161948 		if (rv != CKR_OK)
29927211Sjp161948 			continue;
29937211Sjp161948 
29947211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
29957211Sjp161948 	fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label);
29967211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
29977211Sjp161948 
29987211Sjp161948 		if (token_info.flags & CKF_RNG)
29997211Sjp161948 			{
30007211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30017211Sjp161948 	fprintf(stderr, "%s: this token has CKF_RNG flag\n", PK11_DBG);
30027211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30037211Sjp161948 			pk11_have_random = CK_TRUE;
30047211Sjp161948 			break;
30057211Sjp161948 			}
30067211Sjp161948 		}
30077211Sjp161948 
30087211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30097211Sjp161948 	fprintf(stderr, "%s: == checking pubkey slots ==\n", PK11_DBG);
30107211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30110Sstevel@tonic-gate 	for (i = 0; i < ulSlotCount; i++)
30120Sstevel@tonic-gate 		{
30130Sstevel@tonic-gate 		CK_BBOOL slot_has_rsa = CK_FALSE;
30140Sstevel@tonic-gate 		CK_BBOOL slot_has_dsa = CK_FALSE;
30150Sstevel@tonic-gate 		CK_BBOOL slot_has_dh = CK_FALSE;
30160Sstevel@tonic-gate 		current_slot = pSlotList[i];
30177211Sjp161948 
30187211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30197211Sjp161948 	fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i);
30207211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30210Sstevel@tonic-gate 		rv = pFuncList->C_GetTokenInfo(current_slot, &token_info);
30220Sstevel@tonic-gate 		if (rv != CKR_OK)
30230Sstevel@tonic-gate 			continue;
30240Sstevel@tonic-gate 
30257211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30267211Sjp161948 	fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label);
30277211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30287211Sjp161948 
30296847Svk199839 #ifndef OPENSSL_NO_RSA
30300Sstevel@tonic-gate 		/*
30310Sstevel@tonic-gate 		 * Check if this slot is capable of signing and
30320Sstevel@tonic-gate 		 * verifying with CKM_RSA_PKCS.
30330Sstevel@tonic-gate 		 */
30340Sstevel@tonic-gate 		rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_RSA_PKCS,
30350Sstevel@tonic-gate 			&mech_info);
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 		if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) &&
30380Sstevel@tonic-gate 				(mech_info.flags & CKF_VERIFY)))
30390Sstevel@tonic-gate 			{
30400Sstevel@tonic-gate 			/*
30410Sstevel@tonic-gate 			 * Check if this slot is capable of encryption,
30420Sstevel@tonic-gate 			 * decryption, sign, and verify with CKM_RSA_X_509.
30430Sstevel@tonic-gate 			 */
30440Sstevel@tonic-gate 			rv = pFuncList->C_GetMechanismInfo(current_slot,
30450Sstevel@tonic-gate 			  CKM_RSA_X_509, &mech_info);
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 			if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) &&
30480Sstevel@tonic-gate 			    (mech_info.flags & CKF_VERIFY) &&
30490Sstevel@tonic-gate 			    (mech_info.flags & CKF_ENCRYPT) &&
30500Sstevel@tonic-gate 			    (mech_info.flags & CKF_VERIFY_RECOVER) &&
30510Sstevel@tonic-gate 			    (mech_info.flags & CKF_DECRYPT)))
30527211Sjp161948 				{
30530Sstevel@tonic-gate 				slot_has_rsa = CK_TRUE;
30547211Sjp161948 				}
30550Sstevel@tonic-gate 			}
30567211Sjp161948 #endif	/* OPENSSL_NO_RSA */
30577211Sjp161948 
30586847Svk199839 #ifndef OPENSSL_NO_DSA
30590Sstevel@tonic-gate 		/*
30600Sstevel@tonic-gate 		 * Check if this slot is capable of signing and
30610Sstevel@tonic-gate 		 * verifying with CKM_DSA.
30620Sstevel@tonic-gate 		 */
30630Sstevel@tonic-gate 		rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_DSA,
30640Sstevel@tonic-gate 			&mech_info);
30650Sstevel@tonic-gate 		if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) &&
30660Sstevel@tonic-gate 		    (mech_info.flags & CKF_VERIFY)))
30677211Sjp161948 			{
30680Sstevel@tonic-gate 			slot_has_dsa = CK_TRUE;
30697211Sjp161948 			}
30707526SVladimir.Kotal@Sun.COM 
30717211Sjp161948 #endif	/* OPENSSL_NO_DSA */
30727211Sjp161948 
30736847Svk199839 #ifndef OPENSSL_NO_DH
30740Sstevel@tonic-gate 		/*
30750Sstevel@tonic-gate 		 * Check if this slot is capable of DH key generataion and
30760Sstevel@tonic-gate 		 * derivation.
30770Sstevel@tonic-gate 		 */
30780Sstevel@tonic-gate 		rv = pFuncList->C_GetMechanismInfo(current_slot,
30790Sstevel@tonic-gate 		  CKM_DH_PKCS_KEY_PAIR_GEN, &mech_info);
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 		if (rv == CKR_OK && (mech_info.flags & CKF_GENERATE_KEY_PAIR))
30820Sstevel@tonic-gate 			{
30830Sstevel@tonic-gate 			rv = pFuncList->C_GetMechanismInfo(current_slot,
30840Sstevel@tonic-gate 				CKM_DH_PKCS_DERIVE, &mech_info);
30850Sstevel@tonic-gate 			if (rv == CKR_OK && (mech_info.flags & CKF_DERIVE))
30867211Sjp161948 				{
30870Sstevel@tonic-gate 				slot_has_dh = CK_TRUE;
30887211Sjp161948 				}
30890Sstevel@tonic-gate 			}
30907211Sjp161948 #endif	/* OPENSSL_NO_DH */
30917211Sjp161948 
30920Sstevel@tonic-gate 		if (!found_candidate_slot &&
30930Sstevel@tonic-gate 		    (slot_has_rsa || slot_has_dsa || slot_has_dh))
30940Sstevel@tonic-gate 			{
30957211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
30960Sstevel@tonic-gate 			fprintf(stderr,
30977211Sjp161948 			  "%s: potential slot: %d\n", PK11_DBG, current_slot);
30987211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
30990Sstevel@tonic-gate 			best_slot_sofar = current_slot;
31000Sstevel@tonic-gate 			pk11_have_rsa = slot_has_rsa;
31010Sstevel@tonic-gate 			pk11_have_dsa = slot_has_dsa;
31020Sstevel@tonic-gate 			pk11_have_dh = slot_has_dh;
31030Sstevel@tonic-gate 			found_candidate_slot = CK_TRUE;
31047211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31057211Sjp161948 			fprintf(stderr,
31067211Sjp161948 		            "%s: setting found_candidate_slot to CK_TRUE\n",
31077211Sjp161948 			    PK11_DBG);
31080Sstevel@tonic-gate 			fprintf(stderr,
31097211Sjp161948 		            "%s: best so far slot: %d\n", PK11_DBG,
31100Sstevel@tonic-gate 		    	    best_slot_sofar);
31117211Sjp161948 			}
31127211Sjp161948 		else
31137211Sjp161948 			{
31147211Sjp161948 			fprintf(stderr,
31157211Sjp161948 			  "%s: no rsa/dsa/dh\n", PK11_DBG);
31160Sstevel@tonic-gate 			}
31177211Sjp161948 #else
31187211Sjp161948 			} /* if */
31197211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31207211Sjp161948 		} /* for */
31217211Sjp161948 
31227211Sjp161948 	if (found_candidate_slot)
31237211Sjp161948 		{
31247211Sjp161948 		pubkey_SLOTID = best_slot_sofar;
31257211Sjp161948 		}
31267211Sjp161948 
31277211Sjp161948 	found_candidate_slot = CK_FALSE;
31287211Sjp161948 	best_slot_sofar = 0;
31297211Sjp161948 
31307211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31317211Sjp161948 	fprintf(stderr, "%s: == checking cipher/digest ==\n", PK11_DBG);
31327211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31337211Sjp161948 	for (i = 0; i < ulSlotCount; i++)
31347211Sjp161948 		{
31357211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31367211Sjp161948 	fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i);
31377211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31387211Sjp161948 
31397211Sjp161948 		current_slot = pSlotList[i];
31407211Sjp161948 		current_slot_n_cipher = 0;
31417211Sjp161948 		current_slot_n_digest = 0;
31427534SVladimir.Kotal@Sun.COM 		(void) memset(local_cipher_nids, 0, sizeof(local_cipher_nids));
31437534SVladimir.Kotal@Sun.COM 		(void) memset(local_digest_nids, 0, sizeof(local_digest_nids));
31447211Sjp161948 
31457211Sjp161948 		pk11_find_symmetric_ciphers(pFuncList, current_slot,
31467211Sjp161948 		    &current_slot_n_cipher, local_cipher_nids);
31477211Sjp161948 
31487211Sjp161948 		pk11_find_digests(pFuncList, current_slot,
31497211Sjp161948 		    &current_slot_n_digest, local_digest_nids);
31507211Sjp161948 
31517211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31527211Sjp161948 		fprintf(stderr, "%s: current_slot_n_cipher %d\n", PK11_DBG,
31537211Sjp161948 			current_slot_n_cipher);
31547211Sjp161948 		fprintf(stderr, "%s: current_slot_n_digest %d\n", PK11_DBG,
31557211Sjp161948 			current_slot_n_digest);
31567211Sjp161948 		fprintf(stderr, "%s: best so far cipher/digest slot: %d\n",
31577211Sjp161948 			PK11_DBG, best_slot_sofar);
31587211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 		/*
31610Sstevel@tonic-gate 		 * If the current slot supports more ciphers/digests than
31627250Sjp161948 		 * the previous best one we change the current best to this one,
31630Sstevel@tonic-gate 		 * otherwise leave it where it is.
31640Sstevel@tonic-gate 		 */
31657250Sjp161948 		if ((current_slot_n_cipher + current_slot_n_digest) >
31667250Sjp161948 		    (slot_n_cipher + slot_n_digest))
31670Sstevel@tonic-gate 			{
31687211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31697211Sjp161948 			fprintf(stderr,
31707211Sjp161948 				"%s: changing best so far slot to %d\n",
31717211Sjp161948 				PK11_DBG, current_slot);
31727211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
31737211Sjp161948 			best_slot_sofar = SLOTID = current_slot;
31747250Sjp161948 			cipher_count = slot_n_cipher = current_slot_n_cipher;
31757250Sjp161948 			digest_count = slot_n_digest = current_slot_n_digest;
31767534SVladimir.Kotal@Sun.COM 			(void) memcpy(cipher_nids, local_cipher_nids,
31777526SVladimir.Kotal@Sun.COM 			    sizeof (local_cipher_nids));
31787534SVladimir.Kotal@Sun.COM 			(void) memcpy(digest_nids, local_digest_nids,
31797526SVladimir.Kotal@Sun.COM 			    sizeof (local_digest_nids));
31800Sstevel@tonic-gate 			}
31810Sstevel@tonic-gate 		}
31820Sstevel@tonic-gate 
31837211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
31847211Sjp161948 	fprintf(stderr,
31857526SVladimir.Kotal@Sun.COM 	    "%s: chosen pubkey slot: %d\n", PK11_DBG, pubkey_SLOTID);
31860Sstevel@tonic-gate 	fprintf(stderr,
31877526SVladimir.Kotal@Sun.COM 	    "%s: chosen rand slot: %d\n", PK11_DBG, rand_SLOTID);
31880Sstevel@tonic-gate 	fprintf(stderr,
31897526SVladimir.Kotal@Sun.COM 	    "%s: chosen cipher/digest slot: %d\n", PK11_DBG, SLOTID);
31907211Sjp161948 	fprintf(stderr,
31917526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_rsa %d\n", PK11_DBG, pk11_have_rsa);
31920Sstevel@tonic-gate 	fprintf(stderr,
31937526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_dsa %d\n", PK11_DBG, pk11_have_dsa);
31947211Sjp161948 	fprintf(stderr,
31957526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_dh %d\n", PK11_DBG, pk11_have_dh);
31960Sstevel@tonic-gate 	fprintf(stderr,
31977526SVladimir.Kotal@Sun.COM 	    "%s: pk11_have_random %d\n", PK11_DBG, pk11_have_random);
31980Sstevel@tonic-gate 	fprintf(stderr,
31997526SVladimir.Kotal@Sun.COM 	    "%s: cipher_count %d\n", PK11_DBG, cipher_count);
32007211Sjp161948 	fprintf(stderr,
32017526SVladimir.Kotal@Sun.COM 	    "%s: digest_count %d\n", PK11_DBG, digest_count);
32027211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32030Sstevel@tonic-gate 
32046847Svk199839 	if (pSlotList != NULL)
32056847Svk199839 		OPENSSL_free(pSlotList);
32060Sstevel@tonic-gate 
32077211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32087211Sjp161948 	OPENSSL_free(hw_cnids);
32097211Sjp161948 	OPENSSL_free(hw_dnids);
32107211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32117211Sjp161948 
32127211Sjp161948 	if (any_slot_found != NULL)
32137211Sjp161948 		*any_slot_found = 1;
32147211Sjp161948 	return 1;
32150Sstevel@tonic-gate 	}
32160Sstevel@tonic-gate 
32177211Sjp161948 static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR pflist,
32187211Sjp161948     int slot_id, CK_MECHANISM_TYPE mech, int *current_slot_n_cipher,
32197211Sjp161948     int *local_cipher_nids, int id)
32207211Sjp161948 	{
32217211Sjp161948 	CK_MECHANISM_INFO mech_info;
32227211Sjp161948 	CK_RV rv;
32237211Sjp161948 
32247211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32257211Sjp161948 	fprintf(stderr, "%s: checking mech: %x", PK11_DBG, mech);
32267211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32277211Sjp161948 	rv = pflist->C_GetMechanismInfo(slot_id, mech, &mech_info);
32287211Sjp161948 
32297211Sjp161948 	if (rv != CKR_OK)
32307211Sjp161948 		{
32317211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32327211Sjp161948 		fprintf(stderr, " not found\n");
32337211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32347211Sjp161948 		return;
32357211Sjp161948 		}
32367211Sjp161948 
32377211Sjp161948 	if ((mech_info.flags & CKF_ENCRYPT) &&
32387211Sjp161948 	    (mech_info.flags & CKF_DECRYPT))
32397211Sjp161948 		{
32407211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32417211Sjp161948 		if (nid_in_table(ciphers[id].nid, hw_cnids))
32427211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32437211Sjp161948 			{
32447211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32457211Sjp161948 		fprintf(stderr, " usable\n");
32467211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32477211Sjp161948 			local_cipher_nids[(*current_slot_n_cipher)++] =
32487211Sjp161948 			    ciphers[id].nid;
32497211Sjp161948 			}
32507211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32517211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32527211Sjp161948 		else
32537211Sjp161948 			{
32547211Sjp161948 		fprintf(stderr, " rejected, software implementation only\n");
32557211Sjp161948 			}
32567211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32577211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32587211Sjp161948 		}
32597211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32607211Sjp161948 	else
32617211Sjp161948 		{
32627211Sjp161948 		fprintf(stderr, " unusable\n");
32637211Sjp161948 		}
32647211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32657211Sjp161948 
32667211Sjp161948 	return;
32677211Sjp161948 	}
32687211Sjp161948 
32697211Sjp161948 static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id,
32707211Sjp161948     CK_MECHANISM_TYPE mech, int *current_slot_n_digest, int *local_digest_nids,
32717211Sjp161948     int id)
32720Sstevel@tonic-gate 	{
32730Sstevel@tonic-gate 	CK_MECHANISM_INFO mech_info;
32740Sstevel@tonic-gate 	CK_RV rv;
32750Sstevel@tonic-gate 
32767211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32777211Sjp161948 	fprintf(stderr, "%s: checking mech: %x", PK11_DBG, mech);
32787211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32797211Sjp161948 	rv = pflist->C_GetMechanismInfo(slot_id, mech, &mech_info);
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 	if (rv != CKR_OK)
32820Sstevel@tonic-gate 		{
32837211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32847211Sjp161948 		fprintf(stderr, " not found\n");
32857211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32867211Sjp161948 		return;
32870Sstevel@tonic-gate 		}
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 	if (mech_info.flags & CKF_DIGEST)
32900Sstevel@tonic-gate 		{
32917211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
32927211Sjp161948 	    	if (nid_in_table(digests[id].nid, hw_dnids))
32937211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
32947211Sjp161948 			{
32957211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
32967211Sjp161948 		fprintf(stderr, " usable\n");
32977211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
32987211Sjp161948 			local_digest_nids[(*current_slot_n_digest)++] =
32997211Sjp161948 			    digests[id].nid;
33007211Sjp161948 			}
33017211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
33027211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
33037211Sjp161948 		else
33047211Sjp161948 			{
33057211Sjp161948 		fprintf(stderr, " rejected, software implementation only\n");
33067211Sjp161948 			}
33077211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
33087211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
33097211Sjp161948 		}
33107211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
33117211Sjp161948 	else
33127211Sjp161948 		{
33137211Sjp161948 		fprintf(stderr, " unusable\n");
33140Sstevel@tonic-gate 		}
33157211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
33167211Sjp161948 
33177211Sjp161948 	return;
33187211Sjp161948 	}
33197211Sjp161948 
33207211Sjp161948 #ifdef	SOLARIS_AES_CTR
33217211Sjp161948 /* create a new NID when we have no OID for that mechanism */
33227211Sjp161948 static int pk11_add_NID(char *sn, char *ln)
33237211Sjp161948 	{
33247211Sjp161948 	ASN1_OBJECT *o;
33257211Sjp161948 	int nid;
33267211Sjp161948 
33277211Sjp161948 	if ((o = ASN1_OBJECT_create(OBJ_new_nid(1), (unsigned char *)"",
33287211Sjp161948 	    1, sn, ln)) == NULL)
33297211Sjp161948 		{
33307211Sjp161948 		return 0;
33317211Sjp161948 		}
33327211Sjp161948 
33337211Sjp161948 	/* will return NID_undef on error */
33347211Sjp161948 	nid = OBJ_add_object(o);
33357211Sjp161948 	ASN1_OBJECT_free(o);
33367211Sjp161948 
33377211Sjp161948 	return (nid);
33387211Sjp161948 	}
33397211Sjp161948 
33407211Sjp161948 /*
33417211Sjp161948  * Create new NIDs for AES counter mode. OpenSSL doesn't support them now so we
33427211Sjp161948  * have to help ourselves here.
33437211Sjp161948  */
33447211Sjp161948 static int pk11_add_aes_ctr_NIDs(void)
33457211Sjp161948 	{
33467211Sjp161948 	/* are we already set? */
33477211Sjp161948 	if (NID_aes_256_ctr != NID_undef)
33487211Sjp161948 		return 1;
33497211Sjp161948 
33507211Sjp161948 	/*
33517211Sjp161948 	 * There are no official names for AES counter modes yet so we just
33527211Sjp161948 	 * follow the format of those that exist.
33537211Sjp161948 	 */
33547211Sjp161948 	if ((NID_aes_128_ctr = pk11_add_NID("AES-128-CTR", "aes-128-ctr")) ==
33557211Sjp161948 	    NID_undef)
33567211Sjp161948 		goto err;
33577211Sjp161948 	ciphers[PK11_AES_128_CTR].nid = pk11_aes_128_ctr.nid = NID_aes_128_ctr;
33587211Sjp161948 	if ((NID_aes_192_ctr = pk11_add_NID("AES-192-CTR", "aes-192-ctr")) ==
33597211Sjp161948 	    NID_undef)
33607211Sjp161948 		goto err;
33617211Sjp161948 	ciphers[PK11_AES_192_CTR].nid = pk11_aes_192_ctr.nid = NID_aes_192_ctr;
33627211Sjp161948 	if ((NID_aes_256_ctr = pk11_add_NID("AES-256-CTR", "aes-256-ctr")) ==
33637211Sjp161948 	    NID_undef)
33647211Sjp161948 		goto err;
33657211Sjp161948 	ciphers[PK11_AES_256_CTR].nid = pk11_aes_256_ctr.nid = NID_aes_256_ctr;
33660Sstevel@tonic-gate 	return 1;
33677211Sjp161948 
33687211Sjp161948 err:
33697211Sjp161948 	PK11err(PK11_F_ADD_AES_CTR_NIDS, PK11_R_ADD_NID_FAILED);
33707211Sjp161948 	return 0;
33710Sstevel@tonic-gate 	}
33727211Sjp161948 #endif	/* SOLARIS_AES_CTR */
33737211Sjp161948 
33747211Sjp161948 /* Find what symmetric ciphers this slot supports. */
33757211Sjp161948 static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist,
33767211Sjp161948     CK_SLOT_ID current_slot, int *current_slot_n_cipher, int *local_cipher_nids)
33777211Sjp161948 	{
33787211Sjp161948 	int i;
33797211Sjp161948 
33807211Sjp161948 	for (i = 0; i < PK11_CIPHER_MAX; ++i)
33817211Sjp161948 		{
33827211Sjp161948 		pk11_get_symmetric_cipher(pflist, current_slot,
33837211Sjp161948 		    ciphers[i].mech_type, current_slot_n_cipher,
33847211Sjp161948 		    local_cipher_nids, ciphers[i].id);
33857211Sjp161948 		}
33867211Sjp161948 	}
33877211Sjp161948 
33887211Sjp161948 /* Find what digest algorithms this slot supports. */
33897211Sjp161948 static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist,
33907211Sjp161948     CK_SLOT_ID current_slot, int *current_slot_n_digest, int *local_digest_nids)
33917211Sjp161948 	{
33927211Sjp161948 	int i;
33937211Sjp161948 
33947211Sjp161948 	for (i = 0; i < PK11_DIGEST_MAX; ++i)
33957211Sjp161948 		{
33967211Sjp161948 		pk11_get_digest(pflist, current_slot, digests[i].mech_type,
33977211Sjp161948 		    current_slot_n_digest, local_digest_nids, digests[i].id);
33987211Sjp161948 		}
33997211Sjp161948 	}
34007211Sjp161948 
34017211Sjp161948 #ifdef	SOLARIS_HW_SLOT_SELECTION
34027211Sjp161948 /*
34037211Sjp161948  * It would be great if we could use pkcs11_kernel directly since this library
34047211Sjp161948  * offers hardware slots only. That's the easiest way to achieve the situation
34057211Sjp161948  * where we use the hardware accelerators when present and OpenSSL native code
34067211Sjp161948  * otherwise. That presumes the fact that OpenSSL native code is faster than the
34077211Sjp161948  * code in the soft token. It's a logical assumption - Crypto Framework has some
34087211Sjp161948  * inherent overhead so going there for the software implementation of a
34097211Sjp161948  * mechanism should be logically slower in contrast to the OpenSSL native code,
34107211Sjp161948  * presuming that both implementations are of similar speed. For example, the
34117211Sjp161948  * soft token for AES is roughly three times slower than OpenSSL for 64 byte
34127211Sjp161948  * blocks and still 20% slower for 8KB blocks. So, if we want to ship products
34137211Sjp161948  * that use the PKCS#11 engine by default, we must somehow avoid that regression
34147211Sjp161948  * on machines without hardware acceleration. That's why switching to the
34157211Sjp161948  * pkcs11_kernel library seems like a very good idea.
34167211Sjp161948  *
34177211Sjp161948  * The problem is that OpenSSL built with SunStudio is roughly 2x slower for
34187211Sjp161948  * asymmetric operations (RSA/DSA/DH) than the soft token built with the same
34197211Sjp161948  * compiler. That means that if we switched to pkcs11_kernel from the libpkcs11
34207211Sjp161948  * library, we would have had a performance regression on machines without
34217211Sjp161948  * hardware acceleration for asymmetric operations for all applications that use
34227211Sjp161948  * the PKCS#11 engine. There is one such application - Apache web server since
34237211Sjp161948  * it's shipped configured to use the PKCS#11 engine by default. Having said
34247211Sjp161948  * that, we can't switch to the pkcs11_kernel library now and have to come with
34257211Sjp161948  * a solution that, on non-accelerated machines, uses the OpenSSL native code
34267211Sjp161948  * for all symmetric ciphers and digests while it uses the soft token for
34277211Sjp161948  * asymmetric operations.
34287211Sjp161948  *
34297211Sjp161948  * This is the idea: dlopen() pkcs11_kernel directly and find out what
34307211Sjp161948  * mechanisms are there. We don't care about duplications (more slots can
34317211Sjp161948  * support the same mechanism), we just want to know what mechanisms can be
34327211Sjp161948  * possibly supported in hardware on that particular machine. As said before,
34337211Sjp161948  * pkcs11_kernel will show you hardware providers only.
34347211Sjp161948  *
34357211Sjp161948  * Then, we rely on the fact that since we use libpkcs11 library we will find
34367211Sjp161948  * the metaslot. When we go through the metaslot's mechanisms for symmetric
34377211Sjp161948  * ciphers and digests, we check that any found mechanism is in the table
34387211Sjp161948  * created using the pkcs11_kernel library. So, as a result we have two arrays
34397211Sjp161948  * of mechanisms that were advertised as supported in hardware which was the
34407211Sjp161948  * goal of that whole excercise. Thus, we can use libpkcs11 but avoid soft token
34417211Sjp161948  * code for symmetric ciphers and digests. See pk11_choose_slots() for more
34427211Sjp161948  * information.
34437211Sjp161948  *
34447211Sjp161948  * This is Solaris specific code, if SOLARIS_HW_SLOT_SELECTION is not defined
34457211Sjp161948  * the code won't be used.
34467211Sjp161948  */
34477211Sjp161948 #if defined(__sparcv9) || defined(__x86_64) || defined(__amd64)
34487211Sjp161948 static const char pkcs11_kernel[] = "/usr/lib/security/64/pkcs11_kernel.so.1";
34497211Sjp161948 #else
34507211Sjp161948 static const char pkcs11_kernel[] = "/usr/lib/security/pkcs11_kernel.so.1";
34517211Sjp161948 #endif
34527211Sjp161948 
34537211Sjp161948 /*
34547211Sjp161948  * Check hardware capabilities of the machines. The output are two lists,
34557211Sjp161948  * hw_cnids and hw_dnids, that contain hardware mechanisms found in all hardware
34567211Sjp161948  * providers together. They are not sorted and may contain duplicate mechanisms.
34577211Sjp161948  */
34587211Sjp161948 static int check_hw_mechanisms(void)
34597211Sjp161948 	{
34607211Sjp161948 	int i;
34617211Sjp161948 	CK_RV rv;
34627211Sjp161948 	void *handle;
34637211Sjp161948 	CK_C_GetFunctionList p;
34647211Sjp161948 	CK_TOKEN_INFO token_info;
34657211Sjp161948 	CK_ULONG ulSlotCount = 0;
34667211Sjp161948 	int n_cipher = 0, n_digest = 0;
34677211Sjp161948 	CK_FUNCTION_LIST_PTR pflist = NULL;
34687211Sjp161948 	CK_SLOT_ID_PTR pSlotList = NULL_PTR;
34697211Sjp161948 	int *tmp_hw_cnids, *tmp_hw_dnids;
34707211Sjp161948 	int hw_ctable_size, hw_dtable_size;
34717211Sjp161948 
34727211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
34737211Sjp161948 	fprintf(stderr, "%s: SOLARIS_HW_SLOT_SELECTION code running\n",
34747211Sjp161948 	    PK11_DBG);
34750Sstevel@tonic-gate #endif
34767211Sjp161948 	if ((handle = dlopen(pkcs11_kernel, RTLD_LAZY)) == NULL)
34777211Sjp161948 		{
34787211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE);
34797211Sjp161948 		goto err;
34807211Sjp161948 		}
34817211Sjp161948 
34827211Sjp161948 	if ((p = (CK_C_GetFunctionList)dlsym(handle,
34837211Sjp161948 	    PK11_GET_FUNCTION_LIST)) == NULL)
34847211Sjp161948 		{
34857211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE);
34867211Sjp161948 		goto err;
34877211Sjp161948 		}
34887211Sjp161948 
34897211Sjp161948 	/* get the full function list from the loaded library
34907211Sjp161948 	 */
34917211Sjp161948 	if (p(&pflist) != CKR_OK)
34927211Sjp161948 		{
34937211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE);
34947211Sjp161948 		goto err;
34957211Sjp161948 		}
34967211Sjp161948 
34977211Sjp161948 	rv = pflist->C_Initialize(NULL_PTR);
34987211Sjp161948 	if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
34997211Sjp161948 		{
35007211Sjp161948 		PK11err_add_data(PK11_F_CHECK_HW_MECHANISMS,
35017211Sjp161948 		    PK11_R_INITIALIZE, rv);
35027211Sjp161948 		goto err;
35037211Sjp161948 		}
35047211Sjp161948 
35057211Sjp161948 	if (pflist->C_GetSlotList(0, NULL_PTR, &ulSlotCount) != CKR_OK)
35067211Sjp161948 		{
35077211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST);
35087211Sjp161948 		goto err;
35097211Sjp161948 		}
35107211Sjp161948 
35117211Sjp161948 	/* no slots, set the hw mechanism tables as empty */
35127211Sjp161948 	if (ulSlotCount == 0)
35137211Sjp161948 		{
35147211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
35157211Sjp161948 	fprintf(stderr, "%s: no hardware mechanisms found\n", PK11_DBG);
35160Sstevel@tonic-gate #endif
35177211Sjp161948 		hw_cnids = OPENSSL_malloc(sizeof (int));
35187211Sjp161948 		hw_dnids = OPENSSL_malloc(sizeof (int));
35197211Sjp161948 		if (hw_cnids == NULL || hw_dnids == NULL)
35207211Sjp161948 			{
35217211Sjp161948 			PK11err(PK11_F_CHECK_HW_MECHANISMS,
35227211Sjp161948 			    PK11_R_MALLOC_FAILURE);
35237211Sjp161948 			return (0);
35247211Sjp161948 			}
35257211Sjp161948 		/* this means empty tables */
35267211Sjp161948 		hw_cnids[0] = NID_undef;
35277211Sjp161948 		hw_dnids[0] = NID_undef;
35287211Sjp161948 		return (1);
35297211Sjp161948 		}
35307211Sjp161948 
35317211Sjp161948 	pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID));
35327211Sjp161948 	if (pSlotList == NULL)
35337211Sjp161948 		{
35347211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE);
35357211Sjp161948 		goto err;
35367211Sjp161948 		}
35377211Sjp161948 
35387211Sjp161948 	/* Get the slot list for processing */
35397211Sjp161948 	if (pflist->C_GetSlotList(0, pSlotList, &ulSlotCount) != CKR_OK)
35407211Sjp161948 		{
35417211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST);
35427211Sjp161948 		goto err;
35437211Sjp161948 		}
35447211Sjp161948 
35457211Sjp161948 	/*
35467211Sjp161948 	 * We don't care about duplicit mechanisms in multiple slots and also
35477211Sjp161948 	 * reserve one slot for the terminal NID_undef which we use to stop the
35487211Sjp161948 	 * search.
35497211Sjp161948 	 */
35507211Sjp161948 	hw_ctable_size = ulSlotCount * PK11_CIPHER_MAX + 1;
35517211Sjp161948 	hw_dtable_size = ulSlotCount * PK11_DIGEST_MAX + 1;
35527211Sjp161948 	tmp_hw_cnids = OPENSSL_malloc(hw_ctable_size * sizeof (int));
35537211Sjp161948 	tmp_hw_dnids = OPENSSL_malloc(hw_dtable_size * sizeof (int));
35547211Sjp161948 	if (tmp_hw_cnids == NULL || tmp_hw_dnids == NULL)
35557211Sjp161948 		{
35567211Sjp161948 		PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE);
35577211Sjp161948 		goto err;
35587211Sjp161948 		}
35597211Sjp161948 
35607211Sjp161948 	/*
35617211Sjp161948 	 * Do not use memset since we should not rely on the fact that NID_undef
35627211Sjp161948 	 * is zero now.
35637211Sjp161948 	 */
35647211Sjp161948 	for (i = 0; i < hw_ctable_size; ++i)
35657211Sjp161948 		tmp_hw_cnids[i] = NID_undef;
35667211Sjp161948 	for (i = 0; i < hw_dtable_size; ++i)
35677211Sjp161948 		tmp_hw_dnids[i] = NID_undef;
35687211Sjp161948 
35697211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
35707211Sjp161948 	fprintf(stderr, "%s: provider: %s\n", PK11_DBG, pkcs11_kernel);
35717211Sjp161948 	fprintf(stderr, "%s: found %d hardware slots\n", PK11_DBG, ulSlotCount);
35727211Sjp161948 	fprintf(stderr, "%s: now looking for mechs supported in hw\n",
35737211Sjp161948 	    PK11_DBG);
35747211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
35757211Sjp161948 
35767211Sjp161948 	for (i = 0; i < ulSlotCount; i++)
35777211Sjp161948 		{
35787211Sjp161948 		if (pflist->C_GetTokenInfo(pSlotList[i], &token_info) != CKR_OK)
35797211Sjp161948 			continue;
35807211Sjp161948 
35817211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
35827211Sjp161948 	fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label);
35837211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
35847211Sjp161948 
35857211Sjp161948 		/*
35867211Sjp161948 		 * We are filling the hw mech tables here. Global tables are
35877211Sjp161948 		 * still NULL so all mechanisms are put into tmp tables.
35887211Sjp161948 		 */
35897211Sjp161948 		pk11_find_symmetric_ciphers(pflist, pSlotList[i],
35907211Sjp161948 		    &n_cipher, tmp_hw_cnids);
35917211Sjp161948 		pk11_find_digests(pflist, pSlotList[i],
35927211Sjp161948 		    &n_digest, tmp_hw_dnids);
35937211Sjp161948 		}
35947211Sjp161948 
35957211Sjp161948 	/*
35967211Sjp161948 	 * Since we are part of a library (libcrypto.so), calling this function
35977211Sjp161948 	 * may have side-effects. Also, C_Finalize() is triggered by
35987211Sjp161948 	 * dlclose(3C).
35997211Sjp161948 	 */
36007211Sjp161948 #if 0
36017211Sjp161948 	pflist->C_Finalize(NULL);
36027211Sjp161948 #endif
36037211Sjp161948 	OPENSSL_free(pSlotList);
36047534SVladimir.Kotal@Sun.COM 	(void) dlclose(handle);
36057211Sjp161948 	hw_cnids = tmp_hw_cnids;
36067211Sjp161948 	hw_dnids = tmp_hw_dnids;
36077211Sjp161948 
36087211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
36097211Sjp161948 	fprintf(stderr, "%s: hw mechs check complete\n", PK11_DBG);
36107211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
36117211Sjp161948 	return (1);
36127211Sjp161948 
36137211Sjp161948 err:
36147211Sjp161948 	if (pSlotList != NULL)
36157211Sjp161948 		OPENSSL_free(pSlotList);
36167211Sjp161948 	if (tmp_hw_cnids != NULL)
36177211Sjp161948 		OPENSSL_free(tmp_hw_cnids);
36187211Sjp161948 	if (tmp_hw_dnids != NULL)
36197211Sjp161948 		OPENSSL_free(tmp_hw_dnids);
36207211Sjp161948 
36217211Sjp161948 	return (0);
36227211Sjp161948 	}
36237211Sjp161948 
36247211Sjp161948 /*
36257211Sjp161948  * Check presence of a NID in the table of NIDs. The table may be NULL (i.e.,
36267211Sjp161948  * non-existent).
36277211Sjp161948  */
36287211Sjp161948 static int nid_in_table(int nid, int *nid_table)
36297211Sjp161948 	{
36307211Sjp161948 	int i = 0;
36317211Sjp161948 
36327211Sjp161948 	/*
36337211Sjp161948 	 * a special case. NULL means that we are initializing a new
36347211Sjp161948 	 * table.
36357211Sjp161948 	 */
36367211Sjp161948 	if (nid_table == NULL)
36377211Sjp161948 		return (1);
36387211Sjp161948 
36397211Sjp161948 	/*
36407211Sjp161948 	 * the table is never full, there is always at least one
36417211Sjp161948 	 * NID_undef.
36427211Sjp161948 	 */
36437211Sjp161948 	while (nid_table[i] != NID_undef)
36447211Sjp161948 		{
36457211Sjp161948 		if (nid_table[i++] == nid)
36467211Sjp161948 			{
36477211Sjp161948 #ifdef	DEBUG_SLOT_SELECTION
36487211Sjp161948 	fprintf(stderr, " (NID %d in hw table, idx %d)", nid, i);
36497211Sjp161948 #endif	/* DEBUG_SLOT_SELECTION */
36507211Sjp161948 			return (1);
36517211Sjp161948 			}
36527211Sjp161948 		}
36537211Sjp161948 
36547211Sjp161948 	return (0);
36557211Sjp161948 	}
36567211Sjp161948 #endif	/* SOLARIS_HW_SLOT_SELECTION */
36577211Sjp161948 
36587211Sjp161948 #endif	/* OPENSSL_NO_HW_PK11 */
36597211Sjp161948 #endif	/* OPENSSL_NO_HW */
3660