11/* libp11, a simple layer on to of PKCS#11 API
22 * Copyright (C) 2005 Olaf Kirch <[email protected] > 3- * Copyright (C) 2016-2018 Michał Trojnara <[email protected] > 3+ * Copyright (C) 2016-2024 Michał Trojnara <[email protected] > 44 *
55 * This library is free software; you can redistribute it and/or
66 * modify it under the terms of the GNU Lesser General Public
@@ -481,7 +481,10 @@ EVP_PKEY *pkcs11_get_key(PKCS11_OBJECT_private *key0, CK_OBJECT_CLASS object_cla
481481 EVP_PKEY_free (ret );
482482 goto err ;
483483 }
484- pkcs11_object_ref (key );
484+ if (key -> object_class == CKO_PRIVATE_KEY )
485+ pkcs11_object_ref (key );
486+ else /* Public key -> detach PKCS11_OBJECT */
487+ pkcs11_set_ex_data_rsa (rsa , NULL );
485488 break ;
486489 case EVP_PKEY_EC :
487490#if OPENSSL_VERSION_NUMBER < 0x30000000L || defined(LIBRESSL_VERSION_NUMBER )
@@ -498,9 +501,17 @@ EVP_PKEY *pkcs11_get_key(PKCS11_OBJECT_private *key0, CK_OBJECT_CLASS object_cla
498501 EVP_PKEY_free (ret );
499502 goto err ;
500503 }
501- pkcs11_object_ref (key );
504+ if (key -> object_class == CKO_PRIVATE_KEY )
505+ pkcs11_object_ref (key );
506+ else /* Public key -> detach PKCS11_OBJECT */
507+ pkcs11_set_ex_data_ec (ec_key , NULL );
502508#else
503509 ret = EVP_PKEY_dup (key -> evp_key );
510+ if (key -> object_class != CKO_PRIVATE_KEY ) {
511+ /* Public key -> detach and free PKCS11_OBJECT */
512+ pkcs11_set_ex_data_ec ((EC_KEY * )EVP_PKEY_get0_EC_KEY (ret ), NULL );
513+ pkcs11_object_free (key );
514+ }
504515#endif
505516 break ;
506517 default :
0 commit comments