3131import java .io .InputStreamReader ;
3232import java .io .StringWriter ;
3333import java .security .PrivateKey ;
34+ import java .security .GeneralSecurityException ;
3435import java .security .cert .CertStore ;
3536import java .security .cert .CollectionCertStoreParameters ;
3637import java .security .cert .X509Certificate ;
4445import org .bouncycastle .cms .CMSProcessableByteArray ;
4546import org .bouncycastle .cms .CMSSignedData ;
4647import org .bouncycastle .cms .CMSSignedDataGenerator ;
48+ import org .bouncycastle .cms .CMSException ;
4749import org .bouncycastle .cms .SignerInformation ;
4850import org .bouncycastle .cms .SignerInformationStore ;
4951import org .jruby .Ruby ;
@@ -163,14 +165,27 @@ public static IRubyObject sign(IRubyObject recv, IRubyObject[] args) throws Exce
163165 x509s .add (x509 );
164166 }
165167
166- CMSSignedDataGenerator gen = new CMSSignedDataGenerator ();
168+ final CMSSignedDataGenerator gen = new CMSSignedDataGenerator ();
167169
168170 gen .addSigner (pkey ,x509 ,"1.3.14.3.2.26" ); //SHA1 OID
169171 if (x509s != null ) {
170- CertStore store = CertStore .getInstance ("Collection" , new CollectionCertStoreParameters (x509s ));
172+ CertStore store = CertStore .getInstance ("Collection" , new CollectionCertStoreParameters (x509s ), OpenSSLReal . PROVIDER );
171173 gen .addCertificatesAndCRLs (store );
172174 }
173- CMSSignedData sdata = gen .generate (new CMSProcessableByteArray (data .convertToString ().getBytes ()),"BC" );
175+
176+ final CMSSignedData [] result = new CMSSignedData [1 ];
177+ final byte [] bdata = data .convertToString ().getBytes ();
178+ OpenSSLReal .doWithBCProvider (new Runnable () {
179+ public void run () {
180+ try {
181+ result [0 ] = gen .generate (new CMSProcessableByteArray (bdata ), "BC" );
182+ } catch (GeneralSecurityException e ) {
183+ } catch (CMSException e ) {
184+ }
185+ }
186+ });
187+
188+ CMSSignedData sdata = result [0 ];
174189
175190 PKCS7 ret = new PKCS7 (recv .getRuntime (),((RubyClass )((RubyModule )(recv .getRuntime ().getModule ("OpenSSL" ).getConstant ("PKCS7" ))).getConstant ("PKCS7" )));
176191 ret .setInstanceVariable ("@data" ,recv .getRuntime ().getNil ());
@@ -276,7 +291,17 @@ public IRubyObject set_certificates(IRubyObject obj) {
276291 }
277292
278293 public IRubyObject certificates () throws Exception {
279- CertStore cc = signedData .getCertificatesAndCRLs ("Collection" ,"BC" );
294+ final CertStore [] result = new CertStore [1 ];
295+ OpenSSLReal .doWithBCProvider (new Runnable () {
296+ public void run () {
297+ try {
298+ result [0 ] = signedData .getCertificatesAndCRLs ("Collection" ,"BC" );
299+ } catch (GeneralSecurityException e ) {
300+ } catch (CMSException e ) {
301+ }
302+ }
303+ });
304+ CertStore cc = result [0 ];
280305 List l = X509_STORE_CTX .transform (cc .getCertificates (null ));
281306 return getRuntime ().newArray (l );
282307 }
@@ -327,17 +352,28 @@ public IRubyObject verify(IRubyObject[] args) throws Exception {
327352 }
328353 }
329354
330- CertStore _x509s = CertStore .getInstance ("Collection" , new CollectionCertStoreParameters (x509s ));
355+ CertStore _x509s = CertStore .getInstance ("Collection" , new CollectionCertStoreParameters (x509s ), OpenSSLReal . PROVIDER );
331356
332357 int verified = 0 ;
333358
334359 SignerInformationStore signers = signedData .getSignerInfos ();
335- CertStore cs = signedData .getCertificatesAndCRLs ("Collection" ,"BC" );
360+
361+ final CertStore [] result2 = new CertStore [1 ];
362+ OpenSSLReal .doWithBCProvider (new Runnable () {
363+ public void run () {
364+ try {
365+ result2 [0 ] = signedData .getCertificatesAndCRLs ("Collection" ,"BC" );
366+ } catch (GeneralSecurityException e ) {
367+ } catch (CMSException e ) {
368+ }
369+ }
370+ });
371+ CertStore cs = result2 [0 ];
336372 Collection c = signers .getSigners ();
337373 Iterator it = c .iterator ();
338374
339375 while (it .hasNext ()) {
340- SignerInformation signer = (SignerInformation )it .next ();
376+ final SignerInformation signer = (SignerInformation )it .next ();
341377 System .err .println (signer .getSignedAttributes ().toHashtable ());
342378
343379 Collection certCollection = _x509s .getCertificates (signer .getSID ());
@@ -354,9 +390,23 @@ public IRubyObject verify(IRubyObject[] args) throws Exception {
354390 cert = (X509Certificate )certIt2 .next ();
355391 }
356392 }
357- if (null != cert && signer .verify (cert ,"BC" )) {
358- verified ++;
359- }
393+
394+ final boolean [] result = new boolean []{false };
395+ final X509Certificate cert2 = cert ;
396+ if (null != cert ) {
397+ OpenSSLReal .doWithBCProvider (new Runnable () {
398+ public void run () {
399+ try {
400+ result [0 ] = signer .verify (cert2 , "BC" );
401+ } catch (GeneralSecurityException e ) {
402+ } catch (CMSException e ) {
403+ }
404+ }
405+ });
406+ if (result [0 ]) {
407+ verified ++;
408+ }
409+ }
360410 }
361411
362412 return (verified != 0 ) ? getRuntime ().getTrue () : getRuntime ().getFalse ();
0 commit comments