Bug 1881999

Summary: 8025 error code when creating subCAs
Product: Red Hat Enterprise Linux 8 Reporter: François Cami <fcami>
Component: nssAssignee: nss-nspr-maint <nss-nspr-maint>
Status: CLOSED CURRENTRELEASE QA Contact: Ivan Nikolchev <inikolch>
Severity: unspecified Docs Contact:
Priority: low    
Version: 8.3CC: cheimes, inikolch, rrelyea, ssidhaye, ssorce
Target Milestone: rcKeywords: OtherQA, Triaged, ZStream
Target Release: 8.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: nss-3.67.0-4.el8 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of:
: 1977412 1985061 (view as bug list) Environment:
Last Closed: 2021-12-16 11:54:57 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: 1977412, 1985061    

Comment 2 Alex Scheel 2020-09-23 16:28:21 UTC
I believe this is a race condition in NSS.

See this reproducer here: https://github.com/cipherboy/jss/blob/reproduce-1881999/org/mozilla/jss/tests/LWCAFailure.java

Change count to 10 on line 68, build JSS via https://github.com/dogtagpki/jss/blob/master/docs/building.md#in-source-build and then run via `cd build && ./run_test.sh org.mozilla.jss.tests.LWCAFailure`.

(I've added 1000 to see if it is reproducible via serial Key Gen. So far it hasn't after a couple hundred keygen attempts. So I'm inclined to believe race condition.)



This successfully reproduces the failure after sufficient (random) time:

org.mozilla.jss.crypto.TokenException: Keypair Generation failed on token with error: -8025 : 
Exception in thread "Thread-8" java.lang.RuntimeException: Keypair Generation failed on token with error: -8025 : 
	at org.mozilla.jss.tests.LWCAFailure$Smasher.run(LWCAFailure.java:63)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.mozilla.jss.crypto.TokenException: Keypair Generation failed on token with error: -8025 : 
	at org.mozilla.jss.pkcs11.PK11KeyPairGenerator.generateRSAKeyPairWithOpFlags(Native Method)
	at org.mozilla.jss.pkcs11.PK11KeyPairGenerator.generateKeyPair(PK11KeyPairGenerator.java:502)
	at org.mozilla.jss.crypto.KeyPairGenerator.genKeyPair(KeyPairGenerator.java:50)
	at org.mozilla.jss.tests.LWCAFailure.createSubCA(LWCAFailure.java:49)
	at org.mozilla.jss.tests.LWCAFailure$Smasher.run(LWCAFailure.java:59)
	... 1 more

It is (subjectively) faster to reproduce on NSS @ 3.53 (in RHEL 8.3) than it is in NSS @ 3.56 (in Fedora 32 currently). 

I do not know yet how to trigger this in a reliable fashion other than brute-forcing parallel keygen and waiting for it to fail.



My suggestion is we turn this over to the crypto/NSS team to see if they have any thoughts.

Comment 3 Alex Scheel 2020-09-23 16:33:52 UTC
For context, the relevant JSS function (generateRSAKeyPairWithOpFlags) is fairly trivial:

JNIEXPORT jobject JNICALL
Java_org_mozilla_jss_pkcs11_PK11KeyPairGenerator_generateRSAKeyPairWithOpFlags
  (JNIEnv *env, jobject this, jobject token, jint keySize, jlong publicExponent,
    jboolean temporary, jint sensitive, jint extractable,
    jint op_flags, jint op_flags_mask)
{
    PK11RSAGenParams params;

    PR_ASSERT(env!=NULL && this!=NULL && token!=NULL);

    /**************************************************
     * setup parameters
     *************************************************/
    params.keySizeInBits = keySize;
    params.pe = publicExponent;

    return PK11KeyPairGeneratorWithOpFlags(env, this, token, CKM_RSA_PKCS_KEY_PAIR_GEN,
     &params, temporary, sensitive, extractable, op_flags, op_flags_mask);
}


At the time it is called (and this executes), we've initialized NSS a while ago (because we've successfully generated keys $count times earlier). So the race condition must happen in the PK11 code somewhere.


The question is why is this failure in NSS... now :-)

Comment 4 Alex Scheel 2020-09-23 16:38:02 UTC
Sorry, PK11KeyPairGeneratorWithOpFlags is actually a JSS function. It calls JSS's JSS_PK11_generateKeyPairWithOpFlags and keysToKeyPair.

So, we call the following two PK11 functions:

 - PK11_Authenticate (should be a no-op if token is already logged in, which it is since we authed earlier).
 - PK11_GenerateKeyPairWithOpFlags

No JSS method returns SEC_ERROR_PKCS11_GENERAL_ERROR, so it is one of these two functions (or the interaction thereof). 

Guess the next step is a C reproducer without JSS.