Skip to content

Commit 6434f17

Browse files
committed
springboot fixes
1 parent 315faa5 commit 6434f17

4 files changed

Lines changed: 117 additions & 6 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,8 @@ private synchronized int DoHandshake(boolean fromWrap) throws SSLException {
580580
}
581581

582582
} catch (SocketTimeoutException | SocketException e) {
583-
throw new SSLException(e);
583+
throw new SSLHandshakeException(
584+
"Socket error during SSL/TLS handshake: " + e.getMessage());
584585
}
585586

586587
return ret;
@@ -1058,6 +1059,12 @@ private synchronized int RecvAppData(ByteBuffer[] out, int ofst, int length)
10581059
}
10591060
break;
10601061
default:
1062+
/* Throw SSLHandshakeException if handshake not finished */
1063+
if (!this.handshakeFinished) {
1064+
throw new SSLHandshakeException(
1065+
"SSL/TLS handshake error in read: " + ret +
1066+
" , err = " + err);
1067+
}
10611068
throw new SSLException(
10621069
"wolfSSL_read() error: " + ret + " , err = " + err);
10631070
}
@@ -1393,6 +1400,14 @@ else if (ret < 0 &&
13931400
* any more data */
13941401
this.outBoundOpen = false;
13951402
}
1403+
/* Throw SSLHandshakeException if handshake not
1404+
* finished, otherwise throw SSLException for
1405+
* post-handshake errors */
1406+
if (!this.handshakeFinished) {
1407+
throw new SSLHandshakeException(
1408+
"SSL/TLS handshake error, ret:err = " +
1409+
ret + " : " + err);
1410+
}
13961411
throw new SSLException(
13971412
"wolfSSL error, ret:err = " + ret + " : " +
13981413
err);

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,18 @@ public String chooseClientAlias(String[] type, Principal[] issuers,
375375
for (i = 0; i < type.length; i++) {
376376
String[] all = getAliases(type[i], issuers);
377377
if (all != null) {
378-
return all[0];
378+
/* Find first alias that has a private key, skip cert-only
379+
* entries (trustedCertEntry) which have no private key */
380+
for (String alias : all) {
381+
PrivateKey key = getPrivateKey(alias);
382+
if (key != null) {
383+
final String selectedAlias = alias;
384+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
385+
() -> "chooseClientAlias() returning alias " +
386+
"with private key: " + selectedAlias);
387+
return alias;
388+
}
389+
}
379390
}
380391
}
381392
return null;
@@ -398,7 +409,18 @@ public String chooseEngineClientAlias(String[] type, Principal[] issuers,
398409
for (i = 0; i < type.length; i++) {
399410
String[] all = getAliases(type[i], issuers);
400411
if (all != null) {
401-
return all[0];
412+
/* Find first alias that has a private key, skip cert-only
413+
* entries (trustedCertEntry) which have no private key */
414+
for (String alias : all) {
415+
PrivateKey key = getPrivateKey(alias);
416+
if (key != null) {
417+
final String selectedAlias = alias;
418+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
419+
() -> "chooseEngineClientAlias() returning " +
420+
"alias with private key: " + selectedAlias);
421+
return alias;
422+
}
423+
}
402424
}
403425
}
404426
return null;

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

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,78 @@ protected void engineInit(ManagerFactoryParameters arg0)
761761
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
762762
() -> "entered engineInit(ManagerFactoryParameters arg0)");
763763

764-
throw new UnsupportedOperationException(
765-
"TrustManagerFactory.init(ManagerFactoryParameters) " +
766-
"not supported yet");
764+
/* Handle CertPathTrustManagerParameters (used by Tomcat, etc) */
765+
if (arg0 instanceof javax.net.ssl.CertPathTrustManagerParameters) {
766+
javax.net.ssl.CertPathTrustManagerParameters certPathParams =
767+
(javax.net.ssl.CertPathTrustManagerParameters) arg0;
768+
java.security.cert.CertPathParameters certPathParameters =
769+
certPathParams.getParameters();
770+
771+
if (certPathParameters instanceof
772+
java.security.cert.PKIXParameters) {
773+
java.security.cert.PKIXParameters pkixParams =
774+
(java.security.cert.PKIXParameters) certPathParameters;
775+
java.util.Set<java.security.cert.TrustAnchor> anchors =
776+
pkixParams.getTrustAnchors();
777+
778+
try {
779+
java.security.KeyStore ks =
780+
java.security.KeyStore.getInstance(
781+
java.security.KeyStore.getDefaultType());
782+
ks.load(null, null);
783+
int count = 0;
784+
for (java.security.cert.TrustAnchor anchor : anchors) {
785+
java.security.cert.X509Certificate cert =
786+
anchor.getTrustedCert();
787+
if (cert != null) {
788+
ks.setCertificateEntry(
789+
"trustanchor-" + count, cert);
790+
count++;
791+
}
792+
}
793+
final int finalCount = count;
794+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
795+
() -> "Initialized TrustManager from " +
796+
"CertPathTrustManagerParameters with " +
797+
finalCount + " anchors");
798+
engineInit(ks);
799+
return;
800+
} catch (Exception e) {
801+
throw new InvalidAlgorithmParameterException(
802+
"Failed to create KeyStore from TrustAnchors: " +
803+
e.getMessage(), e);
804+
}
805+
}
806+
}
807+
808+
/* Handle KeyStoreBuilderParameters */
809+
if (arg0 instanceof javax.net.ssl.KeyStoreBuilderParameters) {
810+
javax.net.ssl.KeyStoreBuilderParameters ksParams =
811+
(javax.net.ssl.KeyStoreBuilderParameters) arg0;
812+
java.util.List<java.security.KeyStore.Builder> builders =
813+
ksParams.getParameters();
814+
815+
if (builders != null && !builders.isEmpty()) {
816+
try {
817+
/* Use the first KeyStore builder */
818+
java.security.KeyStore ks =
819+
builders.get(0).getKeyStore();
820+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
821+
() -> "Initialized TrustManager from " +
822+
"KeyStoreBuilderParameters");
823+
engineInit(ks);
824+
return;
825+
} catch (Exception e) {
826+
throw new InvalidAlgorithmParameterException(
827+
"Failed to get KeyStore from Builder: " +
828+
e.getMessage(), e);
829+
}
830+
}
831+
}
832+
833+
throw new InvalidAlgorithmParameterException(
834+
"Unsupported ManagerFactoryParameters type: " +
835+
(arg0 != null ? arg0.getClass().getName() : "null"));
767836
}
768837

769838
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public WolfSSLUtil() {
7575
protected static String[] sanitizeProtocols(String[] protocols,
7676
WolfSSL.TLS_VERSION currentVersion) {
7777

78+
/* Return null if protocols is null, let caller handle */
79+
if (protocols == null) {
80+
return null;
81+
}
82+
7883
ArrayList<String> filtered = new ArrayList<String>();
7984

8085
String disabledAlgos =

0 commit comments

Comments
 (0)