Skip to content

Commit 00c76e2

Browse files
jerboaagnu-andrew
authored andcommitted
OJ1357: Fix issue on FIPS with a SecurityManager in place (openjdk#25)
Resolves: OPENJDK-1357 Reviewed-by: @martinuy, @gnu-andrew
1 parent 3301ca2 commit 00c76e2

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/java.base/share/lib/security/default.policy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ grant codeBase "jrt:/jdk.crypto.cryptoki" {
151151
permission java.util.PropertyPermission "os.name", "read";
152152
permission java.util.PropertyPermission "os.arch", "read";
153153
permission java.util.PropertyPermission "jdk.crypto.KeyAgreement.legacyKDF", "read";
154+
permission java.util.PropertyPermission "fips.nssdb.path", "read,write";
155+
permission java.util.PropertyPermission "fips.nssdb.pin", "read";
154156
permission java.security.SecurityPermission "putProviderProperty.*";
155157
permission java.security.SecurityPermission "clearProviderProperties.*";
156158
permission java.security.SecurityPermission "removeProviderProperty.*";

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,21 @@ public SunPKCS11 run() throws Exception {
168168
* fips.nssdb.path System property after expansion.
169169
* Security properties expansion is unsupported.
170170
*/
171-
System.setProperty(
172-
FIPS_NSSDB_PATH_PROP,
171+
String nssdbPath =
173172
SecurityProperties.privilegedGetOverridable(
174-
FIPS_NSSDB_PATH_PROP));
173+
FIPS_NSSDB_PATH_PROP);
174+
if (System.getSecurityManager() != null) {
175+
AccessController.doPrivileged(
176+
(PrivilegedAction<Void>) () -> {
177+
System.setProperty(
178+
FIPS_NSSDB_PATH_PROP,
179+
nssdbPath);
180+
return null;
181+
});
182+
} else {
183+
System.setProperty(
184+
FIPS_NSSDB_PATH_PROP, nssdbPath);
185+
}
175186
}
176187
return new SunPKCS11(new Config(newConfigName));
177188
}
@@ -1450,6 +1461,7 @@ private static final class P11Service extends Service {
14501461
}
14511462

14521463
@Override
1464+
@SuppressWarnings("removal")
14531465
public Object newInstance(Object param)
14541466
throws NoSuchAlgorithmException {
14551467
if (!token.isValid()) {
@@ -1469,7 +1481,26 @@ public Object newInstance(Object param)
14691481
* property.
14701482
*/
14711483
try {
1472-
token.ensureLoggedIn(null);
1484+
if (System.getSecurityManager() != null) {
1485+
try {
1486+
AccessController.doPrivileged(
1487+
(PrivilegedExceptionAction<Void>) () -> {
1488+
token.ensureLoggedIn(null);
1489+
return null;
1490+
});
1491+
} catch (PrivilegedActionException pae) {
1492+
Exception e = pae.getException();
1493+
if (e instanceof LoginException le) {
1494+
throw le;
1495+
} else if (e instanceof PKCS11Exception p11e) {
1496+
throw p11e;
1497+
} else {
1498+
throw new RuntimeException(e);
1499+
}
1500+
}
1501+
} else {
1502+
token.ensureLoggedIn(null);
1503+
}
14731504
} catch (PKCS11Exception | LoginException e) {
14741505
throw new ProviderException("FIPS: error during the Token" +
14751506
" login required for the " + getType() +

0 commit comments

Comments
 (0)