Skip to content

Commit 818c416

Browse files
fridrichmartinuy
andauthored
RH2173781: Avoid calling C_GetInfo() too early, before cryptoki is initialized (#26)
Co-authored-by: Martin Balao <[email protected]> Reviewed-by: @franferrax, @gnu-andrew
1 parent 4f7c708 commit 818c416

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ static P11Key derivePBEKey(Token token, PBEKeySpec keySpec, String algo)
220220
}
221221

222222
if (kdfData.kdfMech == CKM_PKCS5_PBKD2) {
223-
CK_VERSION p11Ver = token.p11.getInfo().cryptokiVersion;
224-
if (P11Util.isNSS(token) || p11Ver.major < 2 ||
225-
p11Ver.major == 2 && p11Ver.minor < 40) {
223+
CK_INFO p11Info = token.p11.getInfo();
224+
CK_VERSION p11Ver = (p11Info != null ? p11Info.cryptokiVersion
225+
: null);
226+
if (P11Util.isNSS(token) || p11Ver != null && (p11Ver.major <
227+
2 || p11Ver.major == 2 && p11Ver.minor < 40)) {
226228
// NSS keeps using the old structure beyond PKCS #11 v2.40
227229
ckMech = new CK_MECHANISM(kdfData.kdfMech,
228230
new CK_PKCS5_PBKD2_PARAMS(password, salt,

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static void loadNative() {
116116

117117
private long pNativeData;
118118

119-
private CK_INFO pInfo;
119+
private volatile CK_INFO pInfo;
120120

121121
/**
122122
* This method does the initialization of the native library. It is called
@@ -153,7 +153,6 @@ public static void loadNative() {
153153
throws IOException, PKCS11Exception {
154154
connect(pkcs11ModulePath, functionListName);
155155
this.pkcs11ModulePath = pkcs11ModulePath;
156-
pInfo = C_GetInfo();
157156
}
158157

159158
/*
@@ -215,7 +214,21 @@ public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
215214
* C_GetInfo. This structure represent Cryptoki library information.
216215
*/
217216
public CK_INFO getInfo() {
218-
return pInfo;
217+
CK_INFO lPInfo = pInfo;
218+
if (lPInfo == null) {
219+
synchronized (this) {
220+
lPInfo = pInfo;
221+
if (lPInfo == null) {
222+
try {
223+
lPInfo = C_GetInfo();
224+
pInfo = lPInfo;
225+
} catch (PKCS11Exception e) {
226+
// Some PKCS #11 tokens require initialization first.
227+
}
228+
}
229+
}
230+
}
231+
return lPInfo;
219232
}
220233

221234
/**

0 commit comments

Comments
 (0)