キーを複製する必要が本当にない場合は、次のように参照カウントをインクリメントできます。
CRYPTO_add(&your_evp_pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
そうでなければ、あなたが提案したものと同様の(ほぼ同じ)アプローチは次のようになります:
int pkey_rsa_dup(EVP_PKEY *dst_pkey, EVP_PKEY *src_key) {
// Validate underlying key type - Only allow a RSA key
if (src_key->type != EVP_PKEY_RSA)
return -1;
RSA *rsa = EVP_PKEY_get1_RSA(src_key); // Get the underlying RSA key
RSA *dup_rsa = RSAPrivateKey_dup(rsa); // Duplicate the RSA key
RSA_free(rsa); // Decrement reference count
EVP_PKEY_set1_RSA(dst_pkey, dup_rsa); // Set the underlying RSA key in dst_pkey
// EVP_PKEY_set1_RSA also adjusts the other members in dst_pkey
return 0;
}
参照: Re: EVP_PKEY を複製する方法-> @X-Istence が以下に述べているように、RSA_dup
この参照スレッドで提案されている方法は OpenSSL には存在しません (少なくともこの更新日まで)。