Skip to content

Commit 46b1093

Browse files
authored
Merge pull request #1034 from ndossche/clesss-9
x509attr: check for errors of sk_ASN1_TYPE_push() & use reserve call
2 parents 1d46d12 + 0966d04 commit 46b1093

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

ext/openssl/extconf.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def find_openssl_library
135135
evp_h = "openssl/evp.h".freeze
136136
ts_h = "openssl/ts.h".freeze
137137
ssl_h = "openssl/ssl.h".freeze
138+
stack_h = "openssl/stack.h".freeze
138139

139140
# compile options
140141
have_func("RAND_egd()", "openssl/rand.h")
@@ -150,6 +151,9 @@ def find_openssl_library
150151
# added in OpenSSL 1.1.1 and LibreSSL 3.5.0, then removed in LibreSSL 4.0.0
151152
have_func("EVP_PKEY_check(NULL)", evp_h)
152153

154+
# added in OpenSSL 1.1.1, currently not in LibreSSL
155+
have_func("OPENSSL_sk_new_reserve(NULL, 0)", stack_h)
156+
153157
# added in 3.0.0
154158
have_func("SSL_CTX_set0_tmp_dh_pkey(NULL, NULL)", ssl_h)
155159
have_func("ERR_get_error_all(NULL, NULL, NULL, NULL, NULL)", "openssl/err.h")

ext/openssl/ossl_x509attr.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,22 @@ ossl_x509attr_get_value(VALUE self)
235235
unsigned char *p;
236236

237237
GetX509Attr(self, attr);
238+
count = X509_ATTRIBUTE_count(attr);
238239
/* there is no X509_ATTRIBUTE_get0_set() :( */
240+
#ifdef HAVE_OPENSSL_SK_NEW_RESERVE
241+
if (!(sk = sk_ASN1_TYPE_new_reserve(NULL, count)))
242+
ossl_raise(eX509AttrError, "sk_new_reserve");
243+
#else
239244
if (!(sk = sk_ASN1_TYPE_new_null()))
240245
ossl_raise(eX509AttrError, "sk_new");
246+
#endif
241247

242-
count = X509_ATTRIBUTE_count(attr);
243-
for (i = 0; i < count; i++)
244-
sk_ASN1_TYPE_push(sk, (ASN1_TYPE *)X509_ATTRIBUTE_get0_type(attr, i));
248+
for (i = 0; i < count; i++) {
249+
if (!sk_ASN1_TYPE_push(sk, (ASN1_TYPE *)X509_ATTRIBUTE_get0_type(attr, i))) {
250+
sk_ASN1_TYPE_free(sk);
251+
ossl_raise(eX509AttrError, NULL);
252+
}
253+
}
245254

246255
if ((len = i2d_ASN1_SET_ANY(sk, NULL)) <= 0) {
247256
sk_ASN1_TYPE_free(sk);

0 commit comments

Comments
 (0)