Skip to content

Commit 0ccb615

Browse files
authored
Merge pull request #234 from WilburZjh/exportSecretKey
Support exporting plain SecretKey in FIPS mode
2 parents 38ae8ef + 78551ca commit 0ccb615

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,24 @@ static SecretKey secretKey(Session session, long keyID, String algorithm,
343343
new CK_ATTRIBUTE(CKA_SENSITIVE),
344344
new CK_ATTRIBUTE(CKA_EXTRACTABLE),
345345
});
346+
347+
if ((SunPKCS11.mysunpkcs11 != null) && !SunPKCS11.isExportWrapKey.get()
348+
&& ("AES".equals(algorithm) || "TripleDES".equals(algorithm))
349+
) {
350+
if (attributes[0].getBoolean() || attributes[1].getBoolean() || (attributes[2].getBoolean() == false)) {
351+
try {
352+
byte[] key = SunPKCS11.mysunpkcs11.exportKey(session.id(), attributes, keyID);
353+
SecretKey secretKey = new SecretKeySpec(key, algorithm);
354+
return new P11SecretKeyFIPS(session, keyID, algorithm, keyLength, attributes, secretKey);
355+
} catch (PKCS11Exception e) {
356+
// Attempt failed, create a P11SecretKey object.
357+
if (debug != null) {
358+
debug.println("Attempt failed, creating a SecretKey object for " + algorithm);
359+
}
360+
}
361+
}
362+
}
363+
346364
return new P11SecretKey(session, keyID, algorithm, keyLength,
347365
attributes);
348366
}
@@ -495,6 +513,29 @@ byte[] getEncodedInternal() {
495513
}
496514
}
497515

516+
private static final class P11SecretKeyFIPS extends P11Key implements SecretKey {
517+
@Serial
518+
private static final long serialVersionUID = -9186806495402041696L;
519+
private final SecretKey key;
520+
521+
P11SecretKeyFIPS(Session session, long keyID, String algorithm,
522+
int keyLength, CK_ATTRIBUTE[] attributes, SecretKey key) {
523+
super(SECRET, session, keyID, algorithm, keyLength, attributes);
524+
this.key = key;
525+
}
526+
527+
@Override
528+
public String getFormat() {
529+
return "RAW";
530+
}
531+
532+
@Override
533+
byte[] getEncodedInternal() {
534+
return key.getEncoded();
535+
}
536+
537+
}
538+
498539
private static class P11SecretKey extends P11Key implements SecretKey {
499540
private static final long serialVersionUID = -7828241727014329084L;
500541
private volatile byte[] encoded;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public final class SunPKCS11 extends AuthProvider {
120120
// FIPS mode.
121121
static SunPKCS11 mysunpkcs11;
122122

123+
static final ThreadLocal<Boolean> isExportWrapKey = ThreadLocal.withInitial(() -> Boolean.FALSE);
124+
123125
Token getToken() {
124126
return token;
125127
}
@@ -502,10 +504,12 @@ byte[] exportKey(long hSession, CK_ATTRIBUTE[] attributes, long keyId) throws PK
502504

503505
try {
504506
long genKeyId = token.p11.C_GenerateKey(wrapKeyGenSession.id(), new CK_MECHANISM(CKM_AES_KEY_GEN), wrapKeyAttributes);
507+
isExportWrapKey.set(Boolean.TRUE);
505508
wrapKey = (P11Key)P11Key.secretKey(wrapKeyGenSession, genKeyId, "AES", 256 >> 3, null);
506509
} catch (PKCS11Exception e) {
507510
throw e;
508511
} finally {
512+
isExportWrapKey.set(Boolean.FALSE);
509513
token.releaseSession(wrapKeyGenSession);
510514
}
511515

0 commit comments

Comments
 (0)