Skip to content

Commit ffc9769

Browse files
committed
wolfJSSE fixes for FIPS test coverage
1 parent 13cb353 commit ffc9769

17 files changed

Lines changed: 1276 additions & 198 deletions

src/java/com/wolfssl/WolfSSLCertManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ public synchronized int CertManagerLoadCAKeyStore(KeyStore ks)
179179
cert = (X509Certificate) ks.getCertificate(name);
180180
}
181181

182-
if (cert != null && cert.getBasicConstraints() >= 0) {
182+
if (cert != null && (cert.getBasicConstraints() >= 0 ||
183+
WolfSSL.trustPeerCertEnabled())) {
183184
ret = CertManagerLoadCABuffer(cert.getEncoded(),
184185
cert.getEncoded().length,
185186
WolfSSL.SSL_FILETYPE_ASN1);

src/java/com/wolfssl/provider/jsse/WolfSSLAuthStore.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,16 @@ protected void updateTimeouts(int in, int side) {
762762

763763
}
764764

765-
if (in > 0 && diff > in) {
765+
if (in > 0 && diff >= in) {
766+
current.invalidate();
767+
}
768+
try {
769+
current.setNativeTimeout(in);
770+
} catch (IllegalStateException e) {
771+
/* Native WolfSSLSession has been freed,
772+
* invalidate this session entry */
766773
current.invalidate();
767774
}
768-
current.setNativeTimeout(in);
769775
}
770776
}
771777
}

src/java/com/wolfssl/provider/jsse/WolfSSLContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ protected SSLEngine engineCreateSSLEngine()
488488
try {
489489
return new WolfSSLEngine(this.ctx, this.authStore, this.params);
490490
} catch (WolfSSLException ex) {
491-
throw new IllegalStateException("Unable to create engine");
491+
throw new IllegalStateException("Unable to create engine", ex);
492492
}
493493
}
494494

@@ -516,7 +516,7 @@ protected SSLEngine engineCreateSSLEngine(String host, int port)
516516
return new WolfSSLEngine(this.ctx, this.authStore, this.params,
517517
host, port);
518518
} catch (WolfSSLException ex) {
519-
throw new IllegalStateException("Unable to create engine");
519+
throw new IllegalStateException("Unable to create engine", ex);
520520
}
521521
}
522522

src/java/com/wolfssl/provider/jsse/WolfSSLEngine.java

Lines changed: 239 additions & 95 deletions
Large diffs are not rendered by default.

src/java/com/wolfssl/provider/jsse/WolfSSLEngineHelper.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,19 @@ protected synchronized WolfSSLImplementSSLSession getSession() {
475475
return this.session;
476476
}
477477

478+
/**
479+
* Get the last exception from TrustManager certificate verification.
480+
* Delegates to the internal verify callback if available.
481+
*
482+
* @return Exception from last failed verification, or null
483+
*/
484+
protected synchronized Exception getLastVerifyException() {
485+
if (this.wicb != null) {
486+
return this.wicb.getVerifyException();
487+
}
488+
return null;
489+
}
490+
478491
/**
479492
* Get all supported cipher suites in native wolfSSL library, which
480493
* are also allowed by "wolfjsse.enabledCipherSuites" system Security
@@ -870,7 +883,7 @@ private void setLocalAuth(SSLSocket socket, SSLEngine engine) {
870883
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
871884
() -> "Using checkClientTrusted/ServerTrusted() " +
872885
"for verification");
873-
this.verifyMask = WolfSSL.SSL_VERIFY_PEER;
886+
this.verifyMask = mask;
874887
}
875888

876889
this.ssl.setVerify(this.verifyMask, wicb);
@@ -916,7 +929,7 @@ private static boolean checkBooleanProperty(String prop,
916929
* name depending on what createSocket() API the user has called and with
917930
* what String.
918931
*/
919-
private void setLocalServerNames() {
932+
private void setLocalServerNames(SSLEngine engine) {
920933
boolean autoSNI = this.wolfjsseAutoSni;
921934

922935
/* Detect HttpsURLConnection usage by checking:
@@ -931,9 +944,16 @@ private void setLocalServerNames() {
931944
this.peerAddr != null &&
932945
this.params.getServerNames() == null;
933946

934-
/* Enable SNI if explicitly requested via property or if
935-
* HttpsURLConnection is detected */
936-
autoSNI = autoSNI || isHttpsConnection;
947+
/* SSLEngine(host, port) should send SNI by default if no explicit
948+
* server names were configured and SNI extension is enabled. */
949+
boolean isEngineConnectionWithHost = this.clientMode &&
950+
engine != null &&
951+
this.hostname != null &&
952+
this.params.getServerNames() == null;
953+
954+
/* Enable SNI if explicitly requested via property, if
955+
* HttpsURLConnection is detected, or for SSLEngine(host, port). */
956+
autoSNI = autoSNI || isHttpsConnection || isEngineConnectionWithHost;
937957

938958
if (!this.jsseEnableSniExtension) {
939959
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
@@ -1256,7 +1276,7 @@ private void setLocalParams(SSLSocket socket, SSLEngine engine)
12561276
WolfSSLUtil.sanitizeProtocols(
12571277
this.params.getProtocols(), WolfSSL.TLS_VERSION.INVALID));
12581278
this.setLocalAuth(socket, engine);
1259-
this.setLocalServerNames();
1279+
this.setLocalServerNames(engine);
12601280
this.setLocalSessionTicket();
12611281
this.setLocalAlpnProtocols();
12621282
this.setLocalSecureRenegotiation();
@@ -1708,4 +1728,3 @@ protected synchronized void finalize() throws Throwable {
17081728
super.finalize();
17091729
}
17101730
}
1711-

src/java/com/wolfssl/provider/jsse/WolfSSLImplementSSLSession.java

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,12 @@ public WolfSSLImplementSSLSession (WolfSSLImplementSSLSession orig) {
281281
this.sesPtr = orig.sesPtr;
282282
this.sesPtrUpdatedAfterTable = false;
283283

284-
/* Not copying binding, not needed */
285-
this.binding = null;
284+
/* Copy binding HashMap so session values are preserved */
285+
if (orig.binding != null) {
286+
this.binding = new HashMap<String, Object>(orig.binding);
287+
} else {
288+
this.binding = new HashMap<String, Object>();
289+
}
286290

287291
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
288292
() -> "created new session (WolfSSLImplementSSLSession)");
@@ -669,52 +673,14 @@ public Certificate[] getLocalCertificates() {
669673
public synchronized Principal getPeerPrincipal()
670674
throws SSLPeerUnverifiedException {
671675

672-
long peerX509 = 0;
673-
Principal peerPrincipal = null;
674-
WolfSSLX509 x509 = null;
675-
676-
if (ssl == null) {
677-
throw new SSLPeerUnverifiedException("handshake not done");
678-
}
679-
680-
/* Throw if server side with no client auth requested */
681-
if (this.side == WolfSSL.WOLFSSL_SERVER_END &&
682-
!this.clientAuthRequested) {
683-
throw new SSLPeerUnverifiedException(
684-
"peer not authenticated (no client auth requested)");
685-
}
686-
687-
try {
688-
peerX509 = this.ssl.getPeerCertificate();
689-
if (peerX509 == 0) {
690-
throw new SSLPeerUnverifiedException("No peer certificate");
691-
}
692-
693-
/* wolfSSL starting with 5.3.0 returns a new WOLFSSL_X509
694-
* structure from wolfSSL_get_peer_certificate(). In that case,
695-
* we need to free the pointer when finished. Prior to 5.3.0,
696-
* this memory was freed internally by wolfSSL since the API
697-
* only returned a pointer to internal memory */
698-
if (WolfSSL.getLibVersionHex() >= 0x05003000) {
699-
x509 = new WolfSSLX509(peerX509, true);
700-
}
701-
else {
702-
x509 = new WolfSSLX509(peerX509, false);
703-
}
704-
705-
if (x509 != null) {
706-
peerPrincipal = x509.getSubjectDN();
707-
x509.free();
708-
}
709-
710-
return peerPrincipal;
711-
712-
} catch (IllegalStateException | WolfSSLJNIException |
713-
WolfSSLException ex) {
714-
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
715-
() -> "Error getting peer principal: " + ex.getMessage());
676+
/* Use standard Java X509Certificate.getSubjectDN()
677+
* for X500Name equals() compatibility */
678+
Certificate[] certs = getPeerCertificates();
679+
if (certs != null && certs.length > 0 &&
680+
certs[0] instanceof X509Certificate) {
681+
return ((X509Certificate) certs[0]).getSubjectDN();
716682
}
717-
return null;
683+
throw new SSLPeerUnverifiedException("No peer certificate");
718684
}
719685

720686
@Override

0 commit comments

Comments
 (0)