Skip to content

Commit 1cf64c6

Browse files
committed
Fix [Dev] intermittent SSL alert received: HANDSHAKE_FAILURE - error 552 - phase 2
This JSS code workaround takes steps to make sure ONLY SSL Server Cert private keys are marked as NOT temporary. We have a case on a certain HSM where NSS makes a copy of the SSL Server private key on the token and hands it to our JSS SSLEngine implementation. After serveral separate incoming SSL connections, the Java GC attempts to clean up the private key references periodically. Since the SSL Server private key copy is marked as temporary, the normal call to clean up a private key reference has been known to delete the key from the token. Since NSS uses this copy over a long perid of time, any early deletion of this key will make subsequent SSL connections to fail, until the server is reset. This fix makes sure the private key refernces kept inour java objects are NOT set to temporary. When the GC runs all calls to clean up the private key reference will not accidentally remove the key from the token. Fixed to adust to the fact that JSSEngineImpl no longer has a cert and key object, instead some ArrayList.
1 parent c5566fc commit 1cf64c6

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

base/src/main/java/org/mozilla/jss/pkcs11/PK11PrivKey.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public String getAlgorithm() {
7676
*/
7777
public native PK11PubKey getPublicKey();
7878

79+
/**
80+
* Sets the private key PRBool pkcs11IsTemp property.
81+
* Use with care
82+
*/
83+
public native void setTemporary(boolean isTemporary);
84+
7985
/**
8086
* Imports a PrivateKeyInfo, storing it as a temporary PrivateKey
8187
* on the given token.

base/src/main/java/org/mozilla/jss/ssl/javax/JSSEngineReferenceImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,17 @@ private void initServer() throws SSLException {
417417
List<PK11Cert> lstCerts = new ArrayList<>();
418418
for (Pair<PK11Cert, PK11PrivKey> pairKeys: certs) {
419419
lstCerts.add(pairKeys.getLeft());
420+
PK11PrivKey key = pairKeys.getRight();
421+
422+
// Workaround to account for NSS giving us a copy of the actual SSL Server private key.
423+
// This is to keep calls to SECKEY_DestroyPrivateKey from blowing the long lived SSL cert
424+
// private key off the token.
425+
426+
if(key != null) {
427+
key.setTemporary(false);
428+
}
420429
}
421-
430+
422431
session.setLocalCertificates(lstCerts.toArray(new PK11Cert[0]));
423432

424433
// Create a small server session cache.

lib/jss.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ JSS_5.9.0 {
529529
global:
530530
Java_org_mozilla_jss_pkcs11_PK11KeyPairGenerator_generateMLDSAKeyPair;
531531
Java_org_mozilla_jss_pkcs11_PK11KeyPairGenerator_generateMLDSAKeyPairWithOpFlags;
532+
Java_org_mozilla_jss_pkcs11_PK11PrivKey_setTemporary;
532533
local:
533534
*;
534535
};

native/src/main/native/org/mozilla/jss/pkcs11/PK11PrivKey.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,28 @@ Java_org_mozilla_jss_pkcs11_PK11PrivKey_getPublicKey
756756

757757
return JSS_PK11_wrapPubKey(env, &pubKey);
758758
}
759+
760+
/**********************************************************************
761+
* PK11PrivKey.setTemporary
762+
* Workaround to account for SSL Server Cert private keys created as copies by NSS on a token.
763+
* Can either make it temporary of not temporary based on boolean argument.
764+
* Not recommended for normal use.
765+
*/
766+
JNIEXPORT void JNICALL
767+
Java_org_mozilla_jss_pkcs11_PK11PrivKey_setTemporary
768+
(JNIEnv *env, jobject this, jboolean isTemporary)
769+
{
770+
SECKEYPrivateKey *key = NULL;
771+
772+
PR_ASSERT(env!=NULL && this!=NULL);
773+
774+
/***************************************************
775+
* Get the private key and slot C structures
776+
***************************************************/
777+
if( JSS_PK11_getPrivKeyPtr(env, this, &key) != PR_SUCCESS) {
778+
PR_ASSERT( (*env)->ExceptionOccurred(env) != NULL);
779+
return;
780+
}
781+
782+
key->pkcs11IsTemp = isTemporary;
783+
}

0 commit comments

Comments
 (0)