Skip to content

Commit a77b0cd

Browse files
committed
digest: use EVP_MD_CTX_get0_md() instead of EVP_MD_CTX_md() if exists
The function was renamed in OpenSSL 3.0 due to the change of the lifetime of EVP_MD objects. They are no longer necessarily statically allocated and can be reference-counted -- when an EVP_MD_CTX is free'd, the associated EVP_MD can also become inaccessible. Currently Ruby/OpenSSL only handles builtin algorithms, so no special handling is needed except for adapting to the rename.
1 parent 0a93497 commit a77b0cd

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

ext/openssl/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def find_openssl_library
176176
have_func("TS_VERIFY_CTX_set_certs(NULL, NULL)", "openssl/ts.h")
177177
have_func("SSL_CTX_load_verify_file")
178178
have_func("BN_check_prime")
179+
have_func("EVP_MD_CTX_get0_md")
179180

180181
Logging::message "=== Checking done. ===\n"
181182

ext/openssl/openssl_missing.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,8 @@ IMPL_PKEY_GETTER(EC_KEY, ec)
219219
# define TS_VERIFY_CTX_set_certs(ctx, crts) TS_VERIFY_CTS_set_certs(ctx, crts)
220220
#endif
221221

222+
#ifndef HAVE_EVP_MD_CTX_GET0_MD
223+
# define EVP_MD_CTX_get0_md(ctx) EVP_MD_CTX_md(ctx)
224+
#endif
225+
222226
#endif /* _OSSL_OPENSSL_MISSING_H_ */

ext/openssl/ossl_digest.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ossl_evp_get_digestbyname(VALUE obj)
6363

6464
GetDigest(obj, ctx);
6565

66-
md = EVP_MD_CTX_md(ctx);
66+
md = EVP_MD_CTX_get0_md(ctx);
6767
}
6868

6969
return md;
@@ -176,7 +176,7 @@ ossl_digest_reset(VALUE self)
176176
EVP_MD_CTX *ctx;
177177

178178
GetDigest(self, ctx);
179-
if (EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL) != 1) {
179+
if (EVP_DigestInit_ex(ctx, EVP_MD_CTX_get0_md(ctx), NULL) != 1) {
180180
ossl_raise(eDigestError, "Digest initialization failed.");
181181
}
182182

@@ -259,7 +259,7 @@ ossl_digest_name(VALUE self)
259259

260260
GetDigest(self, ctx);
261261

262-
return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
262+
return rb_str_new_cstr(EVP_MD_name(EVP_MD_CTX_get0_md(ctx)));
263263
}
264264

265265
/*

ext/openssl/ossl_hmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ ossl_hmac_reset(VALUE self)
239239

240240
GetHMAC(self, ctx);
241241
pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
242-
if (EVP_DigestSignInit(ctx, NULL, EVP_MD_CTX_md(ctx), NULL, pkey) != 1)
242+
if (EVP_DigestSignInit(ctx, NULL, EVP_MD_CTX_get0_md(ctx), NULL, pkey) != 1)
243243
ossl_raise(eHMACError, "EVP_DigestSignInit");
244244

245245
return self;

0 commit comments

Comments
 (0)