Skip to content

Commit 48e96ad

Browse files
Martin Ottheadius
authored andcommitted
Implemented OpenSSL::PKCS7#type= and OpenSSL::PKCS7#add_data
Methods are required to create a PKCS#7 degenerate structure which contains only certificates. This is basically the same that 'openssl crl2pkcs7' [http://www.openssl.org/docs/apps/crl2pkcs7.html] does.
1 parent 84f9954 commit 48e96ad

File tree

3 files changed

+134
-5
lines changed

3 files changed

+134
-5
lines changed

src/org/jruby/ext/openssl/PKCS7.java

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,29 @@ public IRubyObject initialize_copy(IRubyObject obj) {
296296

297297
@JRubyMethod(name="type=")
298298
public IRubyObject set_type(IRubyObject obj) {
299-
System.err.println("WARNING: unimplemented method called PKCS7#type=");
300-
return getRuntime().getNil();
299+
int typeId = ASN1Registry.NID_undef;
300+
301+
String type = obj.convertToString().asJavaString();
302+
303+
if ("signed".equals(type)) {
304+
typeId = ASN1Registry.NID_pkcs7_signed;
305+
} else if ("data".equals(type)) {
306+
typeId = ASN1Registry.NID_pkcs7_data;
307+
} else if ("signedAndEnveloped".equals(type)) {
308+
typeId = ASN1Registry.NID_pkcs7_signedAndEnveloped;
309+
} else if ("enveloped".equals(type)) {
310+
typeId = ASN1Registry.NID_pkcs7_enveloped;
311+
} else if ("encrypted".equals(type)) {
312+
typeId = ASN1Registry.NID_pkcs7_encrypted;
313+
}
314+
315+
try {
316+
p7.setType(typeId);
317+
} catch (PKCS7Exception pkcs7e) {
318+
throw newPKCS7Exception(getRuntime(), pkcs7e);
319+
}
320+
321+
return obj;
301322
}
302323

303324
@JRubyMethod(name="type")
@@ -475,8 +496,44 @@ public IRubyObject crls() {
475496

476497
@JRubyMethod(name={"add_data", "data="})
477498
public IRubyObject add_data(IRubyObject obj) {
478-
System.err.println("WARNING: unimplemented method called PKCS7#add_data");
479-
return getRuntime().getNil();
499+
if (p7.isSigned()) {
500+
try {
501+
p7.contentNew(ASN1Registry.NID_pkcs7_data);
502+
} catch (PKCS7Exception pkcs7e) {
503+
throw newPKCS7Exception(getRuntime(), pkcs7e);
504+
}
505+
}
506+
507+
BIO in = obj2bio(obj);
508+
BIO out = null;
509+
try {
510+
out = p7.dataInit(null);
511+
} catch (PKCS7Exception pkcs7e) {
512+
throw newPKCS7Exception(getRuntime(), pkcs7e);
513+
}
514+
byte[] buf = new byte[4096];
515+
for(;;) {
516+
try {
517+
int i = in.read(buf, 0, buf.length);
518+
if(i <= 0) {
519+
break;
520+
}
521+
if(out != null) {
522+
out.write(buf, 0, i);
523+
}
524+
} catch(IOException e) {
525+
throw getRuntime().newIOErrorFromException(e);
526+
}
527+
}
528+
529+
try {
530+
p7.dataFinal(out);
531+
} catch (PKCS7Exception pkcs7e) {
532+
throw newPKCS7Exception(getRuntime(), pkcs7e);
533+
}
534+
setData(getRuntime().getNil());
535+
536+
return obj;
480537
}
481538

482539
@JRubyMethod(rest=true)

src/org/jruby/ext/openssl/impl/PKCS7.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,12 @@ public BIO dataInit(BIO bio) throws PKCS7Exception {
924924
bio.setMemEofReturn(0);
925925
}
926926
}
927-
out.push(bio);
927+
928+
if (out != null) {
929+
out.push(bio);
930+
} else {
931+
out = bio;
932+
}
928933
bio = null;
929934
return out;
930935
}

test/externals/ruby1.9/openssl/test_pkcs7.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,73 @@ def test_graceful_parsing_failure #[ruby-core:43250]
151151
contents = File.read(__FILE__)
152152
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(contents) }
153153
end
154+
155+
def test_set_type_signed
156+
p7 = OpenSSL::PKCS7.new
157+
p7.type = "signed"
158+
assert_equal(p7.type, :signed)
159+
end
160+
161+
def test_set_type_data
162+
p7 = OpenSSL::PKCS7.new
163+
p7.type = "data"
164+
assert_equal(p7.type, :data)
165+
end
166+
167+
def test_set_type_signed_and_enveloped
168+
p7 = OpenSSL::PKCS7.new
169+
p7.type = "signedAndEnveloped"
170+
assert_equal(p7.type, :signedAndEnveloped)
171+
end
172+
173+
def test_set_type_enveloped
174+
p7 = OpenSSL::PKCS7.new
175+
p7.type = "enveloped"
176+
assert_equal(p7.type, :enveloped)
177+
end
178+
179+
def test_set_type_encrypted
180+
p7 = OpenSSL::PKCS7.new
181+
p7.type = "encrypted"
182+
assert_equal(p7.type, :encrypted)
183+
end
184+
185+
def test_degenerate_pkcs7
186+
ca_cert_pem = <<END
187+
-----BEGIN CERTIFICATE-----
188+
MIID4DCCAsigAwIBAgIJAL1oVI72wmQwMA0GCSqGSIb3DQEBBQUAMFMxCzAJBgNV
189+
BAYTAkFVMQ4wDAYDVQQIEwVTdGF0ZTENMAsGA1UEBxMEQ2l0eTEQMA4GA1UEChMH
190+
RXhhbXBsZTETMBEGA1UEAxMKRXhhbXBsZSBDQTAeFw0xMjEwMTgwOTE2NTBaFw0y
191+
MjEwMTYwOTE2NTBaMFMxCzAJBgNVBAYTAkFVMQ4wDAYDVQQIEwVTdGF0ZTENMAsG
192+
A1UEBxMEQ2l0eTEQMA4GA1UEChMHRXhhbXBsZTETMBEGA1UEAxMKRXhhbXBsZSBD
193+
QTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMTSPNxOkd5NN19XO0fJ
194+
tGVlWN4DWuvVL9WbWnXJXX9rU6X8sSOL9RrRA64eEZf2UBFjz9fMHZj/OGcxZpus
195+
4YtzfSrMU6xfvsIHeqX+mT60ms2RfX4UXab50MQArBin3JVKHGnOi25uyAOylVFU
196+
TuzzQJvKyB67vjuRPMlVAgVAZAP07ru9gW0ajt/ODxvUfvXxp5SFF68mVP2ipMBr
197+
4fujUwQC6cVHmnuL6p87VFoo9uk87TSQVDOQGL8MK4moMFtEW9oUTU22CgnxnCsS
198+
sCCELYhy9BdaTWQH26LzMfhnwSuIRHZyprW4WZtU0akrYXNiCj8o92rZmQWXJDbl
199+
qNECAwEAAaOBtjCBszAdBgNVHQ4EFgQUNtVw4jvkZZbkdQbkYi2/F4QN79owgYMG
200+
A1UdIwR8MHqAFDbVcOI75GWW5HUG5GItvxeEDe/aoVekVTBTMQswCQYDVQQGEwJB
201+
VTEOMAwGA1UECBMFU3RhdGUxDTALBgNVBAcTBENpdHkxEDAOBgNVBAoTB0V4YW1w
202+
bGUxEzARBgNVBAMTCkV4YW1wbGUgQ0GCCQC9aFSO9sJkMDAMBgNVHRMEBTADAQH/
203+
MA0GCSqGSIb3DQEBBQUAA4IBAQBvJIsY9bIqliZ3WD1KoN4cvAQeRAPsoLXQkkHg
204+
P6Nrcw9rJ5JvoHfYbo5aNlwbnkbt/B2xlVEXUYpJoBZFXafgxG2gJleioIgnaDS4
205+
FPPwZf1C5ZrOgUBfxTGjHex4ghSAoNGOd35jQzin5NGKOvZclPjZ2vQ++LP3aA2l
206+
9Fn2qASS46IzMGJlC75mlTOTQwDM16UunMAK26lNG9J6q02o4d/oU2a7x0fD80yF
207+
64kNA1wDAwaVCYiUH541qKp+b4iDqer8nf8HqzYDFlpje18xYZMEd1hj8dVOharM
208+
pISJ+D52hV/BGEYF8r5k3hpC5d76gSP2oCcaY0XvLBf97qik
209+
-----END CERTIFICATE-----
210+
END
211+
p7 = OpenSSL::PKCS7.new
212+
p7.type = "signed"
213+
ca_cert = OpenSSL::X509::Certificate.new(ca_cert_pem)
214+
p7.add_certificate ca_cert
215+
p7.add_data ""
216+
217+
assert_nothing_raised do
218+
p7.to_pem
219+
end
220+
end
154221
end
155222

156223
end

0 commit comments

Comments
 (0)