Bug 2102434

Summary: SecretKey generate/import operations don't add the CKA_SIGN attribute in FIPS mode [rhel-9, openjdk-11]
Product: Red Hat Enterprise Linux 9 Reporter: Francisco Ferrari Bihurriet <fferrari>
Component: java-11-openjdkAssignee: Francisco Ferrari Bihurriet <fferrari>
Status: CLOSED ERRATA QA Contact: OpenJDK QA <java-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 9.1CC: ahughes, jandrlik, pmikova
Target Milestone: rcKeywords: Triaged, ZStream
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: java-11-openjdk-11.0.15.0.10-3.el9 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 2107866 (view as bug list) Environment:
Last Closed: 2022-11-15 10:09:26 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2029666, 2107866    

Description Francisco Ferrari Bihurriet 2022-06-29 22:01:08 UTC
This bug was initially created as a copy of Bug #2007331

I am copying this bug because: 

We need to fix this for RHEL 9 - OpenJDK 11 too.

Mac keys generated by KeyGenerator do not work with the corresponding Mac in FIPS mode. The same failure is obtained with keys imported from plain bytes (through the Bug 1991003 work). This second case had already been identified and fixed in Bug 2020290 (and reported upstream [5]).

The Mac service initialization fails with CKR_KEY_TYPE_INCONSISTENT because the generated key doesn't have the CKA_SIGN attribute: see how NSC_SignInit() calls sftk_InitGeneric(..., CKA_SIGN) to verify the attribute [1], this ends up calling sftk_isTrue(key, operation) with operation=CKA_SIGN [2].


Reproducer, for the 'generate' operation:
/////////////////////////////////////////
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import java.security.Key;

public class MacTest {
    public static void main(String[] args) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        KeyGenerator kg = KeyGenerator.getInstance("HmacSHA256");
        Key key = kg.generateKey();
        System.out.println("key: " + key);
        mac.init(key);
    }
}


Reproducer, for the 'import' operation (adapted from bug 2007331, comment 2):
/////////////////////////////////////////////////////////////////////////////
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;
import java.security.Key;

public class MacTest {
    public static void main(String[] args) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        final byte keyBytes[] = "ThisIsAPlainKey!".getBytes();
        Key key = new SecretKeySpec(keyBytes, "HmacSHA256");
        System.out.println("key: " + key);
        mac.init(key);
    }
}


Failure (the same for both reproducers):
////////////////////////////////////////
Exception in thread "main" java.security.InvalidKeyException: init() failed
	at jdk.crypto.cryptoki/sun.security.pkcs11.P11Mac.engineInit(P11Mac.java:214)
	at java.base/javax.crypto.Mac.chooseProvider(Mac.java:365)
	at java.base/javax.crypto.Mac.init(Mac.java:434)
	at MacTest.main(MacTest.java:11)
Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_TYPE_INCONSISTENT
	at jdk.crypto.cryptoki/sun.security.pkcs11.wrapper.PKCS11.C_SignInit(Native Method)
	at jdk.crypto.cryptoki/sun.security.pkcs11.P11Mac.initialize(P11Mac.java:183)
	at jdk.crypto.cryptoki/sun.security.pkcs11.P11Mac.engineInit(P11Mac.java:212)
	... 3 more


Additional info:
////////////////
Mac however works with key obtained from "SunTls12KeyMaterial" (internal class) following way: [3]. Note that SunPKCS11 provider in earlier JDKs did not provide Mac key generators and this was the only way (I am aware of) to obtain Mac key. Mac key generators were added to SunPKCS11 together with SHA3 support [4].

This means that the 'generate' operation isn't available in earlier JDKs, however the issue for the 'import' operation is reproducible in 8 & 11.

[1] https://github.com/nss-dev/nss/blob/NSS_3_67_RTM/lib/softoken/pkcs11c.c#L2777-L2778
[2] https://github.com/nss-dev/nss/blob/NSS_3_67_RTM/lib/softoken/pkcs11c.c#L444
[3] https://github.com/judovana/CryptoTest/blob/a4bb593d6faa2052606fffb0554141c63468efbb/cryptotest/utils/KeysNaiveGenerator.java#L144
[4] https://bugs.openjdk.java.net/browse/JDK-8242332
[5] https://bugs.openjdk.java.net/browse/JDK-8278640

Comment 1 Francisco Ferrari Bihurriet 2022-06-30 18:44:17 UTC
Proposed equivalent Fedora pull request for the fix: https://src.fedoraproject.org/rpms/java-11-openjdk/pull-request/157#request_diff

Comment 12 errata-xmlrpc 2022-11-15 10:09:26 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (java-11-openjdk bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:6694