Skip to content

Commit 3b71ccf

Browse files
authored
Merge pull request #752 from rhenium/pkcs7-empty-signed-data-19974
Handle missing content in PKCS7
2 parents 59ff543 + 07eceb7 commit 3b71ccf

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

ext/openssl/ossl_pkcs7.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ ossl_pkcs7_s_read_smime(VALUE klass, VALUE arg)
165165
out = NULL;
166166
pkcs7 = SMIME_read_PKCS7(in, &out);
167167
BIO_free(in);
168-
if(!pkcs7) ossl_raise(ePKCS7Error, NULL);
168+
if (!pkcs7)
169+
ossl_raise(ePKCS7Error, "Could not parse the PKCS7");
170+
if (!pkcs7->d.ptr)
171+
ossl_raise(ePKCS7Error, "No content in PKCS7");
172+
169173
data = out ? ossl_membio2str(out) : Qnil;
170174
SetPKCS7(ret, pkcs7);
171175
ossl_pkcs7_set_data(ret, data);
@@ -346,6 +350,8 @@ ossl_pkcs7_initialize(int argc, VALUE *argv, VALUE self)
346350
BIO_free(in);
347351
if (!p7)
348352
ossl_raise(rb_eArgError, "Could not parse the PKCS7");
353+
if (!p7->d.ptr)
354+
ossl_raise(rb_eArgError, "No content in PKCS7");
349355

350356
RTYPEDDATA_DATA(self) = p7;
351357
PKCS7_free(p7_orig);

test/openssl/test_pkcs7.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ def test_enveloped
155155
assert_equal(data, p7.decrypt(@rsa1024))
156156
end
157157

158+
def test_empty_signed_data_ruby_bug_19974
159+
data = "-----BEGIN PKCS7-----\nMAsGCSqGSIb3DQEHAg==\n-----END PKCS7-----\n"
160+
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(data) }
161+
162+
data = <<END
163+
MIME-Version: 1.0
164+
Content-Disposition: attachment; filename="smime.p7m"
165+
Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"
166+
Content-Transfer-Encoding: base64
167+
168+
#{data}
169+
END
170+
assert_raise(OpenSSL::PKCS7::PKCS7Error) { OpenSSL::PKCS7.read_smime(data) }
171+
end
172+
158173
def test_graceful_parsing_failure #[ruby-core:43250]
159174
contents = File.read(__FILE__)
160175
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(contents) }

0 commit comments

Comments
 (0)