From 6b47f048c12894f6c2677b85edda844d168c969c Mon Sep 17 00:00:00 2001 From: tb <> Date: Wed, 2 Jul 2025 06:36:52 +0000 Subject: Const correct EVP_PKEY_get{0,1}_{DH,DSA,EC_KEY,RSA}() These are safe to call concurrently and they don't modify the memory region pointed to by the pkey - they only bump the refcount of the key hanging off of it. The returned "legacy" key has to be handled with care in threaded constexts, so it is handed back as non-const. This also matches what EVP_PKEY_get0() always had. This way our signature is identical to BoringSSL's and doesn't cause compiler warnings in code that overuses const because one of the many API incoherencies added by OpenSSL 3 was to turn get0 into a function that takes and returns const while leaving get1 as it was. dlg agrees ok kenjiro --- src/lib/libcrypto/evp/evp.h | 30 +++++++++++++----------------- src/lib/libcrypto/evp/p_lib.c | 18 +++++++++--------- 2 files changed, 22 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h index c2b81d0576..94295e1262 100644 --- a/src/lib/libcrypto/evp/evp.h +++ b/src/lib/libcrypto/evp/evp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: evp.h,v 1.137 2024/08/31 10:38:49 tb Exp $ */ +/* $OpenBSD: evp.h,v 1.138 2025/07/02 06:36:52 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -778,28 +778,24 @@ void *EVP_PKEY_get0(const EVP_PKEY *pkey); const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len); #ifndef OPENSSL_NO_RSA -struct rsa_st; -struct rsa_st *EVP_PKEY_get0_RSA(EVP_PKEY *pkey); -struct rsa_st *EVP_PKEY_get1_RSA(EVP_PKEY *pkey); -int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, struct rsa_st *key); +RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey); +RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey); +int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key); #endif #ifndef OPENSSL_NO_DSA -struct dsa_st; -struct dsa_st *EVP_PKEY_get0_DSA(EVP_PKEY *pkey); -struct dsa_st *EVP_PKEY_get1_DSA(EVP_PKEY *pkey); -int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, struct dsa_st *key); +DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey); +DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey); +int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key); #endif #ifndef OPENSSL_NO_DH -struct dh_st; -struct dh_st *EVP_PKEY_get0_DH(EVP_PKEY *pkey); -struct dh_st *EVP_PKEY_get1_DH(EVP_PKEY *pkey); -int EVP_PKEY_set1_DH(EVP_PKEY *pkey, struct dh_st *key); +DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey); +DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey); +int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key); #endif #ifndef OPENSSL_NO_EC -struct ec_key_st; -struct ec_key_st *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey); -struct ec_key_st *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey); -int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, struct ec_key_st *key); +EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey); +EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey); +int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); #endif EVP_PKEY *EVP_PKEY_new(void); diff --git a/src/lib/libcrypto/evp/p_lib.c b/src/lib/libcrypto/evp/p_lib.c index 9623bb59a1..3f88185737 100644 --- a/src/lib/libcrypto/evp/p_lib.c +++ b/src/lib/libcrypto/evp/p_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: p_lib.c,v 1.62 2025/05/10 05:54:38 tb Exp $ */ +/* $OpenBSD: p_lib.c,v 1.63 2025/07/02 06:36:52 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -628,7 +628,7 @@ LCRYPTO_ALIAS(EVP_PKEY_get0_hmac); #ifndef OPENSSL_NO_RSA RSA * -EVP_PKEY_get0_RSA(EVP_PKEY *pkey) +EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) { if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS) return pkey->pkey.rsa; @@ -639,7 +639,7 @@ EVP_PKEY_get0_RSA(EVP_PKEY *pkey) LCRYPTO_ALIAS(EVP_PKEY_get0_RSA); RSA * -EVP_PKEY_get1_RSA(EVP_PKEY *pkey) +EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) { RSA *rsa; @@ -665,7 +665,7 @@ LCRYPTO_ALIAS(EVP_PKEY_set1_RSA); #ifndef OPENSSL_NO_DSA DSA * -EVP_PKEY_get0_DSA(EVP_PKEY *pkey) +EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) { if (pkey->type != EVP_PKEY_DSA) { EVPerror(EVP_R_EXPECTING_A_DSA_KEY); @@ -676,7 +676,7 @@ EVP_PKEY_get0_DSA(EVP_PKEY *pkey) LCRYPTO_ALIAS(EVP_PKEY_get0_DSA); DSA * -EVP_PKEY_get1_DSA(EVP_PKEY *pkey) +EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) { DSA *dsa; @@ -702,7 +702,7 @@ LCRYPTO_ALIAS(EVP_PKEY_set1_DSA); #ifndef OPENSSL_NO_EC EC_KEY * -EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) +EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) { if (pkey->type != EVP_PKEY_EC) { EVPerror(EVP_R_EXPECTING_A_EC_KEY); @@ -713,7 +713,7 @@ EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) LCRYPTO_ALIAS(EVP_PKEY_get0_EC_KEY); EC_KEY * -EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) +EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) { EC_KEY *key; @@ -740,7 +740,7 @@ LCRYPTO_ALIAS(EVP_PKEY_set1_EC_KEY); #ifndef OPENSSL_NO_DH DH * -EVP_PKEY_get0_DH(EVP_PKEY *pkey) +EVP_PKEY_get0_DH(const EVP_PKEY *pkey) { if (pkey->type != EVP_PKEY_DH) { EVPerror(EVP_R_EXPECTING_A_DH_KEY); @@ -751,7 +751,7 @@ EVP_PKEY_get0_DH(EVP_PKEY *pkey) LCRYPTO_ALIAS(EVP_PKEY_get0_DH); DH * -EVP_PKEY_get1_DH(EVP_PKEY *pkey) +EVP_PKEY_get1_DH(const EVP_PKEY *pkey) { DH *dh; -- cgit v1.2.3-55-g6feb