Skip to content

Commit

Permalink
Merge pull request #679 from WilburZjh/exportSecretKey
Browse files Browse the repository at this point in the history
Support exporting plain SecretKey in FIPS mode
  • Loading branch information
keithc-ca authored Jul 26, 2023
2 parents 8d52f27 + 384cc9a commit 535df28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions jdk/src/share/classes/sun/security/pkcs11/P11Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,24 @@ static SecretKey secretKey(Session session, long keyID, String algorithm,
new CK_ATTRIBUTE(CKA_SENSITIVE),
new CK_ATTRIBUTE(CKA_EXTRACTABLE),
});

if ((SunPKCS11.mysunpkcs11 != null) && !SunPKCS11.isExportWrapKey.get()
&& ("AES".equals(algorithm) || "TripleDES".equals(algorithm))
) {
if (attributes[0].getBoolean() || attributes[1].getBoolean() || (attributes[2].getBoolean() == false)) {
try {
byte[] key = SunPKCS11.mysunpkcs11.exportKey(session.id(), attributes, keyID);
SecretKey secretKey = new SecretKeySpec(key, algorithm);
return new P11SecretKeyFIPS(session, keyID, algorithm, keyLength, attributes, secretKey);
} catch (PKCS11Exception e) {
// Attempt failed, create a P11SecretKey object.
if (debug != null) {
debug.println("Attempt failed, creating a SecretKey object for " + algorithm);
}
}
}
}

return new P11SecretKey(session, keyID, algorithm, keyLength,
attributes);
}
Expand Down Expand Up @@ -475,6 +493,29 @@ byte[] getEncodedInternal() {
}
}

private static final class P11SecretKeyFIPS extends P11Key implements SecretKey {

private static final long serialVersionUID = -9186806495402041696L;
private final SecretKey key;

P11SecretKeyFIPS(Session session, long keyID, String algorithm,
int keyLength, CK_ATTRIBUTE[] attributes, SecretKey key) {
super(SECRET, session, keyID, algorithm, keyLength, attributes);
this.key = key;
}

@Override
public String getFormat() {
return "RAW";
}

@Override
byte[] getEncodedInternal() {
return key.getEncoded();
}

}

private static class P11SecretKey extends P11Key implements SecretKey {
private static final long serialVersionUID = -7828241727014329084L;
private volatile byte[] encoded;
Expand Down
4 changes: 4 additions & 0 deletions jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public final class SunPKCS11 extends AuthProvider {
// FIPS mode.
static SunPKCS11 mysunpkcs11;

static final ThreadLocal<Boolean> isExportWrapKey = ThreadLocal.withInitial(() -> Boolean.FALSE);

Token getToken() {
return token;
}
Expand Down Expand Up @@ -461,10 +463,12 @@ byte[] exportKey(long hSession, CK_ATTRIBUTE[] attributes, long keyId) throws PK

try {
long genKeyId = token.p11.C_GenerateKey(wrapKeyGenSession.id(), new CK_MECHANISM(CKM_AES_KEY_GEN), wrapKeyAttributes);
isExportWrapKey.set(Boolean.TRUE);
wrapKey = (P11Key)P11Key.secretKey(wrapKeyGenSession, genKeyId, "AES", 256 >> 3, null);
} catch (PKCS11Exception e) {
throw e;
} finally {
isExportWrapKey.set(Boolean.TRUE);
token.releaseSession(wrapKeyGenSession);
}

Expand Down

0 comments on commit 535df28

Please sign in to comment.