Skip to content

Commit 46db25e

Browse files
author
Valerie Peng
committed
8176837: SunPKCS11 provider needs to check more details on PKCS11 Mechanism
Disable mechanisms with partial support, e.g. can decrypt but cannot encrypt Reviewed-by: xuelei
1 parent d564ab7 commit 46db25e

File tree

1 file changed

+37
-8
lines changed
  • src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11

1 file changed

+37
-8
lines changed

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public final class SunPKCS11 extends AuthProvider {
6262
private static final long serialVersionUID = -1354835039035306505L;
6363

6464
static final Debug debug = Debug.getInstance("sunpkcs11");
65-
6665
// the PKCS11 object through which we make the native calls
6766
final PKCS11 p11;
6867

@@ -913,6 +912,25 @@ public Object run() {
913912
createPoller();
914913
}
915914

915+
private static boolean isLegacy(CK_MECHANISM_INFO mechInfo)
916+
throws PKCS11Exception {
917+
// assume full support if no mech info available
918+
// For vendor-specific mechanisms, often no mech info is provided
919+
boolean partialSupport = false;
920+
921+
if (mechInfo != null) {
922+
if ((mechInfo.flags & CKF_DECRYPT) != 0) {
923+
// non-legacy cipher mechs should support encryption
924+
partialSupport |= ((mechInfo.flags & CKF_ENCRYPT) == 0);
925+
}
926+
if ((mechInfo.flags & CKF_VERIFY) != 0) {
927+
// non-legacy signature mechs should support signing
928+
partialSupport |= ((mechInfo.flags & CKF_SIGN) == 0);
929+
}
930+
}
931+
return partialSupport;
932+
}
933+
916934
// test if a token is present and initialize this provider for it if so.
917935
// does nothing if no token is found
918936
// called from constructor and by poller
@@ -946,24 +964,35 @@ private void initToken(CK_SLOT_INFO slotInfo) throws PKCS11Exception {
946964
// return a CKM_DES_CBC_PAD.
947965
final Map<Descriptor,Integer> supportedAlgs =
948966
new HashMap<Descriptor,Integer>();
967+
949968
for (int i = 0; i < supportedMechanisms.length; i++) {
950969
long longMech = supportedMechanisms[i];
951-
boolean isEnabled = config.isEnabled(longMech);
970+
CK_MECHANISM_INFO mechInfo = token.getMechanismInfo(longMech);
952971
if (showInfo) {
953-
CK_MECHANISM_INFO mechInfo =
954-
p11.C_GetMechanismInfo(slotID, longMech);
955972
System.out.println("Mechanism " +
956-
Functions.getMechanismName(longMech) + ":");
957-
if (isEnabled == false) {
973+
Functions.getMechanismName(longMech) + ":");
974+
System.out.println(mechInfo == null?
975+
(Constants.INDENT + "info n/a") :
976+
mechInfo);
977+
}
978+
if (!config.isEnabled(longMech)) {
979+
if (showInfo) {
958980
System.out.println("DISABLED in configuration");
959981
}
960-
System.out.println(mechInfo);
982+
continue;
961983
}
962-
if (isEnabled == false) {
984+
if (isLegacy(mechInfo)) {
985+
if (showInfo) {
986+
System.out.println("DISABLED due to legacy");
987+
}
963988
continue;
964989
}
990+
965991
// we do not know of mechs with the upper 32 bits set
966992
if (longMech >>> 32 != 0) {
993+
if (showInfo) {
994+
System.out.println("DISABLED due to unknown mech value");
995+
}
967996
continue;
968997
}
969998
int mech = (int)longMech;

0 commit comments

Comments
 (0)