Skip to content

Commit

Permalink
Handle empty signed data in PKCS7
Browse files Browse the repository at this point in the history
This will have certificates and crls return nil instead of
segfaulting.

Fixes [Bug #19974]
  • Loading branch information
jeremyevans committed Oct 26, 2023
1 parent c9b48f9 commit d84a5f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
24 changes: 16 additions & 8 deletions ext/openssl/ossl_pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,20 +609,24 @@ static STACK_OF(X509) *
pkcs7_get_certs(VALUE self)
{
PKCS7 *pkcs7;
STACK_OF(X509) *certs;
STACK_OF(X509) *certs = NULL;
int i;

GetPKCS7(self, pkcs7);
i = OBJ_obj2nid(pkcs7->type);
switch(i){
case NID_pkcs7_signed:
certs = pkcs7->d.sign->cert;
if (pkcs7->d.sign) {
certs = pkcs7->d.sign->cert;
}
break;
case NID_pkcs7_signedAndEnveloped:
certs = pkcs7->d.signed_and_enveloped->cert;
if (pkcs7->d.signed_and_enveloped) {
certs = pkcs7->d.signed_and_enveloped->cert;
}
break;
default:
certs = NULL;
; /* nothing */
}

return certs;
Expand All @@ -632,20 +636,24 @@ static STACK_OF(X509_CRL) *
pkcs7_get_crls(VALUE self)
{
PKCS7 *pkcs7;
STACK_OF(X509_CRL) *crls;
STACK_OF(X509_CRL) *crls = NULL;
int i;

GetPKCS7(self, pkcs7);
i = OBJ_obj2nid(pkcs7->type);
switch(i){
case NID_pkcs7_signed:
crls = pkcs7->d.sign->crl;
if (pkcs7->d.sign) {
crls = pkcs7->d.sign->crl;
}
break;
case NID_pkcs7_signedAndEnveloped:
crls = pkcs7->d.signed_and_enveloped->crl;
if (pkcs7->d.signed_and_enveloped) {
crls = pkcs7->d.signed_and_enveloped->crl;
}
break;
default:
crls = NULL;
; /* nothing */
}

return crls;
Expand Down
7 changes: 7 additions & 0 deletions test/openssl/test_pkcs7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ def test_enveloped
assert_equal(data, p7.decrypt(@rsa1024))
end

def test_empty_signed_data_ruby_bug_19974
data = "-----BEGIN PKCS7-----\nMAsGCSqGSIb3DQEHAg==\n-----END PKCS7-----\n"
p7 = OpenSSL::PKCS7.new(data)
assert_nil p7.certificates
assert_nil p7.crls
end

def test_graceful_parsing_failure #[ruby-core:43250]
contents = File.read(__FILE__)
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(contents) }
Expand Down

0 comments on commit d84a5f2

Please sign in to comment.