From 8888ad6902b0bb12bab0a1d16389e30f4916f413 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Tue, 7 Jan 2025 02:14:46 +0900 Subject: [PATCH] [ruby/openssl] ossl.c: avoid using sk_*() functions with NULL Always use explicit NULL checks before interacting with STACK_OF(*). Even though most OpenSSL functions named sk_*() do not crash if we pass NULL as the receiver object, depending on this behavior would be a bad idea. Checks for a negative number return from sk_*_num() are removed. This can only happen when the stack is NULL. ossl_*_sk2ary() must no longer be called with NULL. https://github.com/ruby/openssl/commit/84cffd4f77 --- ext/openssl/ossl.c | 11 ++------- ext/openssl/ossl_pkcs7.c | 46 +++++++++++++++++++++----------------- ext/openssl/ossl_ssl.c | 4 +++- ext/openssl/ossl_x509crl.c | 22 +++++++++--------- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c index 27d7f9cfdf..60780790b0 100644 --- a/ext/openssl/ossl.c +++ b/ext/openssl/ossl.c @@ -69,16 +69,9 @@ ossl_##name##_sk2ary(const STACK_OF(type) *sk) \ int i, num; \ VALUE ary; \ \ - if (!sk) { \ - OSSL_Debug("empty sk!"); \ - return Qnil; \ - } \ + RUBY_ASSERT(sk != NULL); \ num = sk_##type##_num(sk); \ - if (num < 0) { \ - OSSL_Debug("items in sk < -1???"); \ - return rb_ary_new(); \ - } \ - ary = rb_ary_new2(num); \ + ary = rb_ary_new_capa(num); \ \ for (i=0; id.signed_and_enveloped->recipientinfo; else sk = NULL; if (!sk) return rb_ary_new(); - if ((num = sk_PKCS7_RECIP_INFO_num(sk)) < 0) { - ossl_raise(ePKCS7Error, "Negative number of recipient!"); - } - ary = rb_ary_new2(num); + num = sk_PKCS7_RECIP_INFO_num(sk); + ary = rb_ary_new_capa(num); for (i=0; i [x509name, ...] + * ssl.client_ca => [x509name, ...] or nil * * Returns the list of client CAs. Please note that in contrast to * SSLContext#client_ca= no array of X509::Certificate is returned but @@ -2468,6 +2468,8 @@ ossl_ssl_get_client_ca_list(VALUE self) GetSSL(self, ssl); ca = SSL_get_client_CA_list(ssl); + if (!ca) + return Qnil; return ossl_x509name_sk2ary(ca); } diff --git a/ext/openssl/ossl_x509crl.c b/ext/openssl/ossl_x509crl.c index 644d70a581..cfaf39640b 100644 --- a/ext/openssl/ossl_x509crl.c +++ b/ext/openssl/ossl_x509crl.c @@ -276,21 +276,19 @@ ossl_x509crl_get_revoked(VALUE self) { X509_CRL *crl; int i, num; - X509_REVOKED *rev; - VALUE ary, revoked; + STACK_OF(X509_REVOKED) *sk; + VALUE ary; GetX509CRL(self, crl); - num = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); - if (num < 0) { - OSSL_Debug("num < 0???"); - return rb_ary_new(); - } - ary = rb_ary_new2(num); + sk = X509_CRL_get_REVOKED(crl); + if (!sk) + return rb_ary_new(); + + num = sk_X509_REVOKED_num(sk); + ary = rb_ary_new_capa(num); for(i=0; i