@@ -44,6 +44,8 @@ static const unsigned char STR_ED448[] = {
4444# endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
4545#endif /* OPENSSL_NO_EC */
4646
47+ static int pkey_ex_index = 0 ;
48+
4749static int pkcs11_find_keys (PKCS11_SLOT_private * , CK_SESSION_HANDLE , unsigned int ,
4850 PKCS11_TEMPLATE * );
4951static int pkcs11_init_key (PKCS11_SLOT_private * , CK_SESSION_HANDLE session ,
@@ -58,6 +60,35 @@ static void pkcs11_common_pubkey_attr(PKCS11_TEMPLATE *, const char *,
5860static void pkcs11_common_privkey_attr (PKCS11_TEMPLATE * , const char * ,
5961 const unsigned char * , size_t , const PKCS11_params * );
6062
63+ static void alloc_pkey_ex_index (void )
64+ {
65+ if (pkey_ex_index == 0 ) {
66+ while (pkey_ex_index == 0 ) /* Workaround for OpenSSL RT3710 */
67+ pkey_ex_index = EVP_PKEY_get_ex_new_index (0 , "libp11 EVP_PKEY" ,
68+ NULL , NULL , NULL );
69+ if (pkey_ex_index < 0 )
70+ pkey_ex_index = 0 ; /* Fallback to app_data */
71+ }
72+ }
73+
74+ static void free_pkey_ex_index (void )
75+ {
76+ if (pkey_ex_index > 0 ) {
77+ CRYPTO_free_ex_index (CRYPTO_EX_INDEX_EVP_PKEY , pkey_ex_index );
78+ pkey_ex_index = 0 ;
79+ }
80+ }
81+
82+ static void pkcs11_set_ex_data_evp_pkey (EVP_PKEY * pkey , PKCS11_KEY * key )
83+ {
84+ EVP_PKEY_set_ex_data (pkey , pkey_ex_index , key );
85+ }
86+
87+ static PKCS11_KEY * pkcs11_get_ex_data_evp_pkey (const EVP_PKEY * pkey )
88+ {
89+ return EVP_PKEY_get_ex_data (pkey , pkey_ex_index );
90+ }
91+
6192/* Helper to acquire object handle from given template */
6293static CK_OBJECT_HANDLE pkcs11_handle_from_template (PKCS11_SLOT_private * slot ,
6394 CK_SESSION_HANDLE session , PKCS11_TEMPLATE * tmpl )
@@ -708,12 +739,21 @@ EVP_PKEY *pkcs11_get_key(PKCS11_OBJECT_private *key0, CK_OBJECT_CLASS object_cla
708739 default :
709740 pkcs11_log (key0 -> slot -> ctx , LOG_DEBUG , "Unsupported key type\n" );
710741 }
742+ alloc_pkey_ex_index ();
743+ pkcs11_set_ex_data_evp_pkey (ret , key -> public );
744+
711745err :
712746 if (key != key0 )
713747 pkcs11_object_free (key );
714748 return ret ;
715749}
716750
751+ /* Returns the PKCS11_KEY handle associated with the given EVP_PKEY */
752+ PKCS11_KEY * pkcs11_get_pkcs11_key (const EVP_PKEY * pk )
753+ {
754+ return pkcs11_get_ex_data_evp_pkey (pk );
755+ }
756+
717757/*
718758 * Authenticate a private the key operation if needed
719759 * This function *only* handles CKU_CONTEXT_SPECIFIC logins.
@@ -912,6 +952,9 @@ static int pkcs11_init_key(PKCS11_SLOT_private *slot, CK_SESSION_HANDLE session,
912952 key -> label = kpriv -> label ;
913953 key -> isPrivate = (type == CKO_PRIVATE_KEY );
914954
955+ /* Link back */
956+ kpriv -> public = key ;
957+
915958 if (ret )
916959 * ret = key ;
917960 return 0 ;
@@ -981,6 +1024,7 @@ void pkcs11_destroy_keys(PKCS11_SLOT_private *slot, unsigned int type)
9811024 OPENSSL_free (keys -> keys );
9821025 keys -> keys = NULL ;
9831026 keys -> num = 0 ;
1027+ free_pkey_ex_index ();
9841028}
9851029
9861030/* vim: set noexpandtab: */
0 commit comments