Skip to content

Commit 9f6cf1b

Browse files
author
olabini
committed
Update everything so that a global provider is not needed
git-svn-id: svn+ssh://rubyforge.org/var/svn/jruby-extras/trunk/jopenssl@825 8ba958d5-0c1a-0410-94a6-a65dfc1b28a6
1 parent b8c652e commit 9f6cf1b

18 files changed

+265
-98
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private static String[] rubyToJavaCipher(String inName, String padding) {
165165

166166
private static boolean tryCipher(String rubyName) {
167167
try {
168-
javax.crypto.Cipher.getInstance(rubyToJavaCipher(rubyName, null)[3],"BC");
168+
javax.crypto.Cipher.getInstance(rubyToJavaCipher(rubyName, null)[3],OpenSSLReal.PROVIDER);
169169
return true;
170170
} catch(Exception e) {
171171
return false;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static void createHMAC(Ruby runtime, RubyModule ossl) {
7474
public static IRubyObject s_digest(IRubyObject recv, IRubyObject digest, IRubyObject kay, IRubyObject data) {
7575
String name = "HMAC" + ((Digest)digest).getAlgorithm();
7676
try {
77-
Mac mac = Mac.getInstance(name);
77+
Mac mac = Mac.getInstance(name,OpenSSLReal.PROVIDER);
7878
byte[] key = kay.convertToString().getBytes();
7979
SecretKey keysp = new SecretKeySpec(key,name);
8080
mac.init(keysp);
@@ -87,7 +87,7 @@ public static IRubyObject s_digest(IRubyObject recv, IRubyObject digest, IRubyOb
8787
public static IRubyObject s_hexdigest(IRubyObject recv, IRubyObject digest, IRubyObject kay, IRubyObject data) {
8888
String name = "HMAC" + ((Digest)digest).getAlgorithm();
8989
try {
90-
Mac mac = Mac.getInstance(name);
90+
Mac mac = Mac.getInstance(name,OpenSSLReal.PROVIDER);
9191
byte[] key = kay.convertToString().getBytes();
9292
SecretKey keysp = new SecretKeySpec(key,name);
9393
mac.init(keysp);
@@ -108,7 +108,7 @@ public HMAC(Ruby runtime, RubyClass type) {
108108
public IRubyObject initialize(IRubyObject kay, IRubyObject digest, Block unusedBlock) {
109109
String name = "HMAC" + ((Digest)digest).getAlgorithm();
110110
try {
111-
mac = Mac.getInstance(name);
111+
mac = Mac.getInstance(name,OpenSSLReal.PROVIDER);
112112
key = kay.convertToString().getBytes();
113113
SecretKey keysp = new SecretKeySpec(key,name);
114114
mac.init(keysp);
@@ -125,7 +125,7 @@ public IRubyObject initialize_copy(IRubyObject obj) {
125125
checkFrozen();
126126
String name = ((HMAC)obj).mac.getAlgorithm();
127127
try {
128-
mac = Mac.getInstance(name);
128+
mac = Mac.getInstance(name,OpenSSLReal.PROVIDER);
129129
key = ((HMAC)obj).key;
130130
SecretKey keysp = new SecretKeySpec(key,name);
131131
mac.init(keysp);

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,26 @@ public IRubyObject _initialize(IRubyObject[] args, Block unusedBlock) throws Exc
9292
b = Base64Coder.decode(b);
9393
} catch(Exception e) {
9494
}
95-
cert = new NetscapeCertRequest(b);
96-
this.challenge = getRuntime().newString(cert.getChallenge());
97-
String algo = cert.getPublicKey().getAlgorithm();;
98-
byte[] enc = cert.getPublicKey().getEncoded();
95+
final byte[] b2 = b;
96+
97+
final String[] result1 = new String[1];
98+
final byte[][] result2 = new byte[1][];
99+
100+
OpenSSLReal.doWithBCProvider(new Runnable() {
101+
public void run() {
102+
try {
103+
cert = new NetscapeCertRequest(b2); //Uses "BC" as provider
104+
challenge = getRuntime().newString(cert.getChallenge()); //Uses "BC" as provider
105+
result1[0] = cert.getPublicKey().getAlgorithm(); //Uses "BC" as provider
106+
result2[0] = cert.getPublicKey().getEncoded(); //Uses "BC" as provider
107+
} catch(java.io.IOException e) {
108+
}
109+
}
110+
});
111+
112+
String algo = result1[0];
113+
byte[] enc = result2[0];
114+
99115
if("RSA".equalsIgnoreCase(algo)) {
100116
this.public_key = ((RubyModule)(getRuntime().getModule("OpenSSL").getConstant("PKey"))).getClass("RSA").callMethod(getRuntime().getCurrentContext(),"new",RubyString.newString(getRuntime(), enc));
101117
} else if("DSA".equalsIgnoreCase(algo)) {
@@ -155,18 +171,35 @@ public IRubyObject set_public_key(IRubyObject arg) {
155171
return arg;
156172
}
157173

158-
public IRubyObject sign(IRubyObject key, IRubyObject digest) throws Exception {
174+
public IRubyObject sign(final IRubyObject key, IRubyObject digest) throws Exception {
159175
String keyAlg = ((PKey)key).getAlgorithm();
160176
String digAlg = ((Digest)digest).getAlgorithm();
161177
DERObjectIdentifier alg = (DERObjectIdentifier)(ASN1.getOIDLookup(getRuntime()).get(keyAlg.toLowerCase() + "-" + digAlg.toLowerCase()));
162178
cert = new NetscapeCertRequest(challenge.toString(),new AlgorithmIdentifier(alg),((PKey)public_key).getPublicKey());
163-
cert.sign(((PKey)key).getPrivateKey());
179+
180+
OpenSSLReal.doWithBCProvider(new Runnable() {
181+
public void run() {
182+
try {
183+
cert.sign(((PKey)key).getPrivateKey());
184+
} catch(java.security.GeneralSecurityException e) {}
185+
}
186+
});
164187
return this;
165188
}
166189

167-
public IRubyObject verify(IRubyObject pkey) throws Exception {
190+
public IRubyObject verify(final IRubyObject pkey) throws Exception {
168191
cert.setPublicKey(((PKey)pkey).getPublicKey());
169-
return cert.verify(challenge.toString()) ? getRuntime().getTrue() : getRuntime().getFalse();
192+
193+
final boolean[] result = new boolean[1];
194+
OpenSSLReal.doWithBCProvider(new Runnable() {
195+
public void run() {
196+
try {
197+
result[0] = cert.verify(challenge.toString());
198+
} catch(java.security.GeneralSecurityException e) {}
199+
}
200+
});
201+
202+
return result[0] ? getRuntime().getTrue() : getRuntime().getFalse();
170203
}
171204

172205
public IRubyObject challenge() {

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,21 @@
3535
* @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
3636
*/
3737
public class OpenSSLReal {
38+
public static java.security.Provider PROVIDER;
39+
40+
public static void doWithBCProvider(Runnable toRun) {
41+
try {
42+
java.security.Security.insertProviderAt(PROVIDER,1);
43+
toRun.run();
44+
} finally {
45+
java.security.Security.removeProvider("BC");
46+
}
47+
}
48+
3849
public static void createOpenSSL(Ruby runtime) {
39-
java.security.Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(),2);
50+
if(PROVIDER == null) {
51+
PROVIDER = new org.bouncycastle.jce.provider.BouncyCastleProvider();
52+
}
4053

4154
RubyModule ossl = runtime.getOrCreateModule("OpenSSL");
4255
RubyClass standardError = runtime.getClass("StandardError");
@@ -58,7 +71,7 @@ public static void createOpenSSL(Ruby runtime) {
5871
ossl.setConstant("OPENSSL_VERSION",runtime.newString("OpenSSL 0.9.8b 04 May 2006 (Java fake)"));
5972

6073
try {
61-
java.security.MessageDigest.getInstance("SHA224");
74+
java.security.MessageDigest.getInstance("SHA224", PROVIDER);
6275
ossl.setConstant("OPENSSL_VERSION_NUMBER",runtime.newFixnum(9469999));
6376
} catch(java.security.NoSuchAlgorithmException e) {
6477
ossl.setConstant("OPENSSL_VERSION_NUMBER",runtime.newFixnum(9469952));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public int getVersion() {
137137
}
138138

139139
public boolean verify(PublicKey pubkey) throws Exception {
140-
Signature sig = Signature.getInstance(sigAlgId.getObjectId().getId(),"BC");
140+
Signature sig = Signature.getInstance(sigAlgId.getObjectId().getId(),OpenSSLReal.PROVIDER);
141141
sig.initVerify(pubkey);
142142

143143
try

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

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.InputStreamReader;
3232
import java.io.StringWriter;
3333
import java.security.PrivateKey;
34+
import java.security.GeneralSecurityException;
3435
import java.security.cert.CertStore;
3536
import java.security.cert.CollectionCertStoreParameters;
3637
import java.security.cert.X509Certificate;
@@ -44,6 +45,7 @@
4445
import org.bouncycastle.cms.CMSProcessableByteArray;
4546
import org.bouncycastle.cms.CMSSignedData;
4647
import org.bouncycastle.cms.CMSSignedDataGenerator;
48+
import org.bouncycastle.cms.CMSException;
4749
import org.bouncycastle.cms.SignerInformation;
4850
import org.bouncycastle.cms.SignerInformationStore;
4951
import 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();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public IRubyObject sign(IRubyObject digest, IRubyObject data) throws Exception {
8989
if(!this.callMethod(getRuntime().getCurrentContext(),"private?").isTrue()) {
9090
throw getRuntime().newArgumentError("Private key is needed.");
9191
}
92-
Signature sig = Signature.getInstance(((Digest)digest).getAlgorithm() + "WITH" + getAlgorithm(),"BC");
92+
Signature sig = Signature.getInstance(((Digest)digest).getAlgorithm() + "WITH" + getAlgorithm(),OpenSSLReal.PROVIDER);
9393
sig.initSign(getPrivateKey());
9494
byte[] inp = data.convertToString().getBytes();
9595
sig.update(inp);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) {
125125
Object val = null;
126126
KeyFactory fact = null;
127127
try {
128-
fact = KeyFactory.getInstance("DSA");
128+
fact = KeyFactory.getInstance("DSA",OpenSSLReal.PROVIDER);
129129
} catch(Exception e) {
130130
throw getRuntime().newLoadError("unsupported key algorithm (DSA)");
131131
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public IRubyObject initialize(IRubyObject[] args, Block block) {
135135
pubExp = BigInteger.valueOf(RubyNumeric.num2long(pass));
136136
}
137137
try {
138-
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
138+
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA",OpenSSLReal.PROVIDER);
139139
gen.initialize(new RSAKeyGenParameterSpec(keyLen,pubExp));
140140
KeyPair pair = gen.generateKeyPair();
141141
privKey = (RSAPrivateCrtKey)(pair.getPrivate());
@@ -152,7 +152,7 @@ public IRubyObject initialize(IRubyObject[] args, Block block) {
152152
Object val = null;
153153
KeyFactory fact = null;
154154
try {
155-
fact = KeyFactory.getInstance("RSA");
155+
fact = KeyFactory.getInstance("RSA", OpenSSLReal.PROVIDER);
156156
} catch(Exception e) {
157157
throw getRuntime().newLoadError("unsupported key algorithm (RSA)");
158158
}
@@ -336,7 +336,7 @@ public IRubyObject private_encrypt(IRubyObject[] args) throws Exception {
336336
throw new RaiseException(getRuntime(), (RubyClass)(((RubyModule)(getRuntime().getModule("OpenSSL").getConstant("PKey"))).getConstant("RSAError")), "private key needed.", true);
337337
}
338338

339-
Cipher engine = Cipher.getInstance("RSA"+p);
339+
Cipher engine = Cipher.getInstance("RSA"+p,OpenSSLReal.PROVIDER);
340340
engine.init(Cipher.ENCRYPT_MODE,privKey);
341341
byte[] outp = engine.doFinal(buffer.getBytes());
342342
return RubyString.newString(getRuntime(), outp);
@@ -354,7 +354,7 @@ public IRubyObject private_decrypt(IRubyObject[] args) throws Exception {
354354
throw new RaiseException(getRuntime(), (RubyClass)(((RubyModule)(getRuntime().getModule("OpenSSL").getConstant("PKey"))).getConstant("RSAError")), "private key needed.", true);
355355
}
356356

357-
Cipher engine = Cipher.getInstance("RSA"+p);
357+
Cipher engine = Cipher.getInstance("RSA"+p,OpenSSLReal.PROVIDER);
358358
engine.init(Cipher.DECRYPT_MODE,privKey);
359359
byte[] outp = engine.doFinal(buffer.getBytes());
360360
return RubyString.newString(getRuntime(), outp);
@@ -368,7 +368,7 @@ public IRubyObject public_encrypt(IRubyObject[] args) throws Exception {
368368
String p = getPadding(padding);
369369

370370
RubyString buffer = args[0].convertToString();
371-
Cipher engine = Cipher.getInstance("RSA"+p);
371+
Cipher engine = Cipher.getInstance("RSA"+p,OpenSSLReal.PROVIDER);
372372
engine.init(Cipher.ENCRYPT_MODE,pubKey);
373373
byte[] outp = engine.doFinal(buffer.getBytes());
374374
return RubyString.newString(getRuntime(), outp);
@@ -382,7 +382,7 @@ public IRubyObject public_decrypt(IRubyObject[] args) throws Exception {
382382
String p = getPadding(padding);
383383

384384
RubyString buffer = args[0].convertToString();
385-
Cipher engine = Cipher.getInstance("RSA"+p);
385+
Cipher engine = Cipher.getInstance("RSA"+p,OpenSSLReal.PROVIDER);
386386
engine.init(Cipher.DECRYPT_MODE,pubKey);
387387
byte[] outp = engine.doFinal(buffer.getBytes());
388388
return RubyString.newString(getRuntime(), outp);

0 commit comments

Comments
 (0)