Skip to content

Commit 796cb09

Browse files
committed
Disable JceSecurity to allow the use of the repackaged BouncyCastle provider (Fixes #163)
1 parent d88a258 commit 796cb09

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

jsign-core/src/main/java/net/jsign/PrivateKeyUtils.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import java.io.File;
2020
import java.io.FileReader;
2121
import java.io.IOException;
22+
import java.lang.reflect.Field;
2223
import java.security.KeyException;
2324
import java.security.PrivateKey;
25+
import java.util.HashMap;
26+
import java.util.function.Function;
2427

2528
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
2629
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
@@ -36,6 +39,7 @@
3639
import org.bouncycastle.operator.OperatorCreationException;
3740
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
3841
import org.bouncycastle.pkcs.PKCSException;
42+
import sun.misc.Unsafe;
3943

4044
/**
4145
* Helper class for loading private keys (PVK or PEM, encrypted or not).
@@ -72,6 +76,36 @@ public static PrivateKey load(File file, String password) throws KeyException {
7276
throw new IllegalArgumentException("Unsupported private key format (PEM or PVK file expected");
7377
}
7478

79+
/**
80+
* Disables the signature verification of the jar containing the BouncyCastle provider.
81+
*/
82+
private static void disableJceSecurity() {
83+
try {
84+
Class<?> jceSecurityClass = Class.forName("javax.crypto.JceSecurity");
85+
Field field = jceSecurityClass.getDeclaredField("verificationResults");
86+
field.setAccessible(true);
87+
88+
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
89+
unsafeField.setAccessible(true);
90+
Unsafe unsafe = (Unsafe) unsafeField.get(null);
91+
92+
unsafe.putObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), new HashMap<Object, Boolean>() {
93+
@Override
94+
public Boolean get(Object key) {
95+
// This is not the provider you are looking for, you don't need to see its identification, move along
96+
return Boolean.TRUE;
97+
}
98+
99+
@Override
100+
public Boolean computeIfAbsent(Object key, Function<? super Object, ? extends Boolean> mappingFunction) {
101+
return super.computeIfAbsent(key, object -> Boolean.TRUE);
102+
}
103+
});
104+
} catch (Exception e) {
105+
e.printStackTrace();
106+
}
107+
}
108+
75109
private static PrivateKey readPrivateKeyPEM(File file, char[] password) throws IOException, OperatorCreationException, PKCSException {
76110
try (FileReader reader = new FileReader(file)) {
77111
PEMParser parser = new PEMParser(reader);
@@ -84,7 +118,11 @@ private static PrivateKey readPrivateKeyPEM(File file, char[] password) throws I
84118
if (object == null) {
85119
throw new IllegalArgumentException("No key found in " + file);
86120
}
87-
121+
122+
if (BouncyCastleProvider.class.getName().startsWith("net.jsign")) {
123+
// disable JceSecurity to allow the use of the repackaged BouncyCastle provider
124+
disableJceSecurity();
125+
}
88126
BouncyCastleProvider provider = new BouncyCastleProvider();
89127
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(provider);
90128

jsign/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<mainClass>net.jsign.JsignCLI</mainClass>
147147
<manifestEntries>
148148
<Add-Exports>jdk.crypto.cryptoki/sun.security.pkcs11.wrapper</Add-Exports>
149+
<Add-Opens>java.base/javax.crypto</Add-Opens>
149150
</manifestEntries>
150151
</transformer>
151152
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
--add-exports java.base/sun.net.www.protocol.http=ALL-UNNAMED
256256
--add-exports java.base/sun.net.www.protocol.https=ALL-UNNAMED
257257
--add-exports jdk.crypto.cryptoki/sun.security.pkcs11.wrapper=ALL-UNNAMED
258+
--add-opens java.base/javax.crypto=ALL-UNNAMED
258259
--add-opens java.base/sun.net.www.protocol.http=ALL-UNNAMED
259260
--add-opens java.base/sun.net.www.protocol.https=ALL-UNNAMED
260261
-Djava.security.manager=allow

0 commit comments

Comments
 (0)