Skip to content

Commit

Permalink
Merge pull request #752 from rhenium/pkcs7-empty-signed-data-19974
Browse files Browse the repository at this point in the history
Handle missing content in PKCS7
  • Loading branch information
rhenium committed May 2, 2024
2 parents 59ff543 + 07eceb7 commit 3b71ccf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ext/openssl/ossl_pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ ossl_pkcs7_s_read_smime(VALUE klass, VALUE arg)
out = NULL;
pkcs7 = SMIME_read_PKCS7(in, &out);
BIO_free(in);
if(!pkcs7) ossl_raise(ePKCS7Error, NULL);
if (!pkcs7)
ossl_raise(ePKCS7Error, "Could not parse the PKCS7");
if (!pkcs7->d.ptr)
ossl_raise(ePKCS7Error, "No content in PKCS7");

data = out ? ossl_membio2str(out) : Qnil;
SetPKCS7(ret, pkcs7);
ossl_pkcs7_set_data(ret, data);
Expand Down Expand Up @@ -346,6 +350,8 @@ ossl_pkcs7_initialize(int argc, VALUE *argv, VALUE self)
BIO_free(in);
if (!p7)
ossl_raise(rb_eArgError, "Could not parse the PKCS7");
if (!p7->d.ptr)
ossl_raise(rb_eArgError, "No content in PKCS7");

RTYPEDDATA_DATA(self) = p7;
PKCS7_free(p7_orig);
Expand Down
15 changes: 15 additions & 0 deletions test/openssl/test_pkcs7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ 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"
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(data) }

data = <<END
MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"
Content-Transfer-Encoding: base64
#{data}
END
assert_raise(OpenSSL::PKCS7::PKCS7Error) { OpenSSL::PKCS7.read_smime(data) }
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 3b71ccf

Please sign in to comment.