Skip to content

Commit ae5020e

Browse files
committed
Return X500Principal from getPeerPrincipal() and getLocalPrincipal()
1 parent 276eff7 commit ae5020e

10 files changed

Lines changed: 52 additions & 73 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ protected void updateTimeouts(int in, int side) {
765765
diff = (now - current.creation.getTime()) / 1000;
766766

767767
if (diff < 0) {
768-
/* session is from the future ... */ /* TODO */
768+
/* session is from the future ... TODO */
769769

770770
}
771771

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ protected synchronized void cacheRequestedServerNamesFromNetData() {
321321
List<SNIServerName> names;
322322
WolfSSLImplementSSLSession session;
323323

324-
if (this.engineHelper == null ||
325-
this.engineHelper.getUseClientMode()) {
324+
if (this.engineHelper == null || this.engineHelper.getUseClientMode()) {
326325
return;
327326
}
328327

@@ -354,7 +353,8 @@ private List<SNIServerName> parseRequestedServerNamesFromNetData() {
354353
List<SNIServerName> names;
355354

356355
synchronized (netDataLock) {
357-
if (this.netData == null || this.netData.remaining() < 5) {
356+
if (this.netData == null ||
357+
this.netData.remaining() < TLS_RECORD_HEADER_LEN) {
358358
return null;
359359
}
360360
in = this.netData.asReadOnlyBuffer();
@@ -1199,8 +1199,7 @@ private synchronized int RecvAppData(ByteBuffer[] out, int ofst, int length)
11991199
default:
12001200
/* Throw SSLHandshakeException if handshake not finished */
12011201
if (!this.handshakeFinished) {
1202-
SSLHandshakeException hse =
1203-
new SSLHandshakeException(
1202+
SSLHandshakeException hse = new SSLHandshakeException(
12041203
"SSL/TLS handshake error in read: " + ret +
12051204
" , err = " + err);
12061205
if (this.engineHelper != null) {
@@ -1235,11 +1234,13 @@ private synchronized int RecvAppData(ByteBuffer[] out, int ofst, int length)
12351234
/* Copy from intermediate buffer to output bufs */
12361235
for (i = 0; i < ret;) {
12371236
if (idx + ofst >= length) {
1237+
/* no more output buffers left */
12381238
break;
12391239
}
12401240

12411241
bufSpace = out[idx + ofst].remaining();
12421242
if (bufSpace == 0) {
1243+
/* no more space in current out buffer, advance */
12431244
idx++;
12441245
continue;
12451246
}
@@ -1450,13 +1451,15 @@ else if (hs == SSLEngineResult.HandshakeStatus.NEED_WRAP &&
14501451
* report bytesConsumed() == 0. DTLS still
14511452
* relies on the native WANT_READ path. */
14521453
boolean bufferUnderflow = false;
1453-
if (inRemaining > 0 &&
1454-
(this.ssl.dtls() == 0)) {
1454+
if (inRemaining > 0 && (this.ssl.dtls() == 0)) {
14551455
synchronized (netDataLock) {
14561456
int pos = in.position();
14571457
if (inRemaining < TLS_RECORD_HEADER_LEN) {
1458+
/* Not enough for TLS record header */
14581459
bufferUnderflow = true;
14591460
} else {
1461+
/* Peek at record length from header
1462+
* bytes 3-4 (big-endian) */
14601463
int recLen =
14611464
((in.get(pos + TLS_RECORD_LEN_HI_OFF)
14621465
& 0xFF) << 8) |
@@ -1482,8 +1485,7 @@ else if (hs == SSLEngineResult.HandshakeStatus.NEED_WRAP &&
14821485
for (int pos = 0;
14831486
pos < this.pendingAppDataLen;) {
14841487
if (idx2 + ofst >= length) break;
1485-
int space =
1486-
out[idx2 + ofst].remaining();
1488+
int space = out[idx2 + ofst].remaining();
14871489
if (space == 0) { idx2++; continue; }
14881490
int sz2 = Math.min(space,
14891491
this.pendingAppDataLen - pos);
@@ -1537,8 +1539,7 @@ else if (inRemaining > 0 &&
15371539
in.position(inPosition);
15381540
}
15391541
produced = 0;
1540-
status =
1541-
SSLEngineResult.Status.BUFFER_OVERFLOW;
1542+
status = SSLEngineResult.Status.BUFFER_OVERFLOW;
15421543
}
15431544
else {
15441545
/* Check for BUFFER_OVERFLOW status. This can
@@ -1657,9 +1658,8 @@ else if (ret < 0 &&
16571658
"SSL/TLS handshake error, ret:err = " +
16581659
ret + " : " + err);
16591660
if (this.engineHelper != null) {
1660-
Exception verifyEx2 =
1661-
this.engineHelper
1662-
.getLastVerifyException();
1661+
Exception verifyEx2 = this.engineHelper
1662+
.getLastVerifyException();
16631663
if (verifyEx2 != null) {
16641664
hse2.initCause(verifyEx2);
16651665
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,7 @@ private boolean isTls13CipherSuite(String suite) {
866866
}
867867

868868
return suite.startsWith("TLS_AES_") ||
869-
suite.startsWith("TLS_CHACHA20_") ||
870-
suite.startsWith("TLS_SM4_");
869+
suite.startsWith("TLS_CHACHA20_") || suite.startsWith("TLS_SM4_");
871870
}
872871

873872
private String[] getEffectiveProtocolsForCiphers(String[] protocols,
@@ -948,8 +947,7 @@ else if ("TLSv1.2".equals(proto) || "DTLSv1.2".equals(proto) ||
948947
return protocols;
949948
}
950949

951-
private void applyConfiguredCipherProtocolSettings()
952-
throws SSLException {
950+
private void applyConfiguredCipherProtocolSettings() throws SSLException {
953951

954952
String[] suites;
955953
String[] protocols;
@@ -1081,8 +1079,7 @@ private void setLocalServerNames(SSLEngine engine) {
10811079
/* SSLEngine(host, port) should send SNI by default if no explicit
10821080
* server names were configured and SNI extension is enabled. */
10831081
boolean isEngineConnectionWithHost = this.clientMode &&
1084-
engine != null &&
1085-
this.hostname != null &&
1082+
engine != null && this.hostname != null &&
10861083
this.params.getServerNames() == null;
10871084

10881085
/* Enable SNI if explicitly requested via property, if

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,11 @@ public Certificate[] getLocalCertificates() {
673673
public synchronized Principal getPeerPrincipal()
674674
throws SSLPeerUnverifiedException {
675675

676-
/* Use standard Java X509Certificate.getSubjectDN()
677-
* for X500Name equals() compatibility */
676+
/* Return X500Principal for proper equals() symmetry */
678677
Certificate[] certs = getPeerCertificates();
679678
if (certs != null && certs.length > 0 &&
680679
certs[0] instanceof X509Certificate) {
681-
return ((X509Certificate) certs[0]).getSubjectDN();
680+
return ((X509Certificate) certs[0]).getSubjectX500Principal();
682681
}
683682
throw new SSLPeerUnverifiedException("No peer certificate");
684683
}
@@ -699,7 +698,7 @@ public Principal getLocalPrincipal() {
699698
if (certs.length > 0){
700699
/* When chain of certificates exceeds one,
701700
* the user certifcate is the first */
702-
localPrincipal = certs[0].getSubjectDN();
701+
localPrincipal = certs[0].getSubjectX500Principal();
703702
}
704703

705704
/* free native resources earlier than garbage collection if
@@ -1137,8 +1136,7 @@ public synchronized List<SNIServerName> getRequestedServerNames()
11371136
byte[] sniRequestArr = null;
11381137

11391138
if (this.ssl == null) {
1140-
if (this.sniServerNames != null &&
1141-
!this.sniServerNames.isEmpty()) {
1139+
if (this.sniServerNames != null && !this.sniServerNames.isEmpty()) {
11421140
return Collections.unmodifiableList(
11431141
new ArrayList<SNIServerName>(this.sniServerNames));
11441142
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ else if (sock != null) {
310310
}
311311
} catch (Exception e) {
312312
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
313-
() -> "Hostname verification error: " +
314-
e.getMessage());
313+
() -> "Hostname verification error: " + e.getMessage());
315314
this.verifyException = e;
316315
return 0;
317316
} finally {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public SSLSession getSession(byte[] sessionId) {
9191
/* Native session may have been freed already */
9292
}
9393
}
94-
WolfSSLImplementSSLSession session =
95-
store.getSession(sessionId, side);
94+
WolfSSLImplementSSLSession session = store.getSession(sessionId, side);
9695
if (session == null) {
9796
return null;
9897
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,19 @@ public String toString() {
802802
public boolean equals(Object obj) {
803803
if (this == obj) return true;
804804
if (obj instanceof Principal) {
805-
return getName().equals(((Principal) obj).getName());
805+
String thisName = getName();
806+
if (thisName == null) {
807+
return ((Principal) obj).getName() == null;
808+
}
809+
return thisName.equals(((Principal) obj).getName());
806810
}
807811
return false;
808812
}
809813

810814
@Override
811815
public int hashCode() {
812-
return getName().hashCode();
816+
String n = getName();
817+
return (n != null) ? n.hashCode() : 0;
813818
}
814819

815820
}

src/test/com/wolfssl/provider/jsse/test/WolfSSLEngineTest.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,8 +2565,7 @@ public void testGetPeerCertificateChainNoClientAuth() throws Exception {
25652565
public void testSSLHandshakeExceptionCauseChain()
25662566
throws NoSuchProviderException, NoSuchAlgorithmException,
25672567
KeyManagementException, KeyStoreException,
2568-
CertificateException, IOException,
2569-
UnrecoverableKeyException {
2568+
CertificateException, IOException, UnrecoverableKeyException {
25702569

25712570
System.out.print("\tSSLEngine SSLHandshakeException cause chain");
25722571

@@ -2629,10 +2628,9 @@ public X509Certificate[] getAcceptedIssuers() {
26292628

26302629
cliToSer.flip();
26312630
if (cliToSer.hasRemaining() &&
2632-
(server.getHandshakeStatus() ==
2633-
HandshakeStatus.NEED_UNWRAP ||
2631+
(server.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP ||
26342632
server.getHandshakeStatus() ==
2635-
HandshakeStatus.NOT_HANDSHAKING)) {
2633+
HandshakeStatus.NOT_HANDSHAKING)) {
26362634
try {
26372635
server.unwrap(cliToSer, sink);
26382636
} catch (SSLException e) {
@@ -2651,10 +2649,9 @@ public X509Certificate[] getAcceptedIssuers() {
26512649

26522650
serToCli.flip();
26532651
if (serToCli.hasRemaining() &&
2654-
(client.getHandshakeStatus() ==
2655-
HandshakeStatus.NEED_UNWRAP ||
2652+
(client.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP ||
26562653
client.getHandshakeStatus() ==
2657-
HandshakeStatus.NOT_HANDSHAKING)) {
2654+
HandshakeStatus.NOT_HANDSHAKING)) {
26582655
try {
26592656
client.unwrap(serToCli, sink);
26602657
} catch (SSLHandshakeException e) {
@@ -2701,8 +2698,7 @@ public void testCloseNotifyTLS13HandshakeStatus()
27012698
}
27022699

27032700
SSLEngine server = this.ctx.createSSLEngine();
2704-
SSLEngine client =
2705-
this.ctx.createSSLEngine("wolfSSL test", 11111);
2701+
SSLEngine client = this.ctx.createSSLEngine("wolfSSL test", 11111);
27062702

27072703
server.setUseClientMode(false);
27082704
server.setNeedClientAuth(false);
@@ -2733,8 +2729,7 @@ public void testCloseNotifyTLS13HandshakeStatus()
27332729
client.getSession().getApplicationBufferSize());
27342730

27352731
/* Wrap the close_notify */
2736-
SSLEngineResult result = client.wrap(
2737-
ByteBuffer.allocate(0), netBuf);
2732+
SSLEngineResult result = client.wrap(ByteBuffer.allocate(0), netBuf);
27382733
if (result.getStatus() != SSLEngineResult.Status.CLOSED) {
27392734
error("\t... failed");
27402735
fail("wrap after closeOutbound should return CLOSED");
@@ -2785,17 +2780,15 @@ public void testCloseNotifyTLS13HandshakeStatus()
27852780
public void testBufferUnderflowPartialRecord()
27862781
throws NoSuchProviderException, NoSuchAlgorithmException,
27872782
KeyManagementException, KeyStoreException,
2788-
CertificateException, IOException,
2789-
UnrecoverableKeyException {
2783+
CertificateException, IOException, UnrecoverableKeyException {
27902784

27912785
/* Test that unwrap() returns BUFFER_UNDERFLOW with 0 bytes
27922786
* consumed when given a partial TLS record. */
27932787
System.out.print("\tTesting BUFFER_UNDERFLOW partial record");
27942788

27952789
this.ctx = tf.createSSLContext("TLS", engineProvider);
27962790
SSLEngine server = this.ctx.createSSLEngine();
2797-
SSLEngine client =
2798-
this.ctx.createSSLEngine("wolfSSL test", 11111);
2791+
SSLEngine client = this.ctx.createSSLEngine("wolfSSL test", 11111);
27992792

28002793
server.setUseClientMode(false);
28012794
server.setNeedClientAuth(false);
@@ -2812,16 +2805,13 @@ public void testBufferUnderflowPartialRecord()
28122805
}
28132806

28142807
/* Client wraps some application data */
2815-
String testData =
2816-
"Hello from client for underflow test";
2817-
ByteBuffer appBuf =
2818-
ByteBuffer.wrap(testData.getBytes());
2808+
String testData = "Hello from client for underflow test";
2809+
ByteBuffer appBuf = ByteBuffer.wrap(testData.getBytes());
28192810
ByteBuffer netBuf = ByteBuffer.allocateDirect(
28202811
client.getSession().getPacketBufferSize());
28212812

28222813
SSLEngineResult result = client.wrap(appBuf, netBuf);
2823-
if (result.getStatus() !=
2824-
SSLEngineResult.Status.OK) {
2814+
if (result.getStatus() != SSLEngineResult.Status.OK) {
28252815
error("\t... failed");
28262816
fail("wrap failed: " + result.getStatus());
28272817
}
@@ -2835,22 +2825,19 @@ public void testBufferUnderflowPartialRecord()
28352825

28362826
/* Create a partial record (only first 3 bytes of the
28372827
* TLS record header, less than the 5-byte header) */
2838-
ByteBuffer partialBuf =
2839-
ByteBuffer.allocateDirect(3);
2828+
ByteBuffer partialBuf = ByteBuffer.allocateDirect(3);
28402829
byte[] partial = new byte[3];
28412830
netBuf.get(partial);
28422831
partialBuf.put(partial);
28432832
partialBuf.flip();
28442833

28452834
ByteBuffer outBuf = ByteBuffer.allocate(
2846-
server.getSession()
2847-
.getApplicationBufferSize());
2835+
server.getSession().getApplicationBufferSize());
28482836

28492837
/* Unwrap partial record: BUFFER_UNDERFLOW expected */
28502838
result = server.unwrap(partialBuf, outBuf);
28512839

2852-
if (result.getStatus() !=
2853-
SSLEngineResult.Status.BUFFER_UNDERFLOW) {
2840+
if (result.getStatus() != SSLEngineResult.Status.BUFFER_UNDERFLOW) {
28542841
error("\t... failed");
28552842
fail("expected BUFFER_UNDERFLOW for partial " +
28562843
"TLS record, got " + result.getStatus());
@@ -2878,8 +2865,7 @@ public void testBufferOverflowSmallOutput()
28782865

28792866
this.ctx = tf.createSSLContext("TLS", engineProvider);
28802867
SSLEngine server = this.ctx.createSSLEngine();
2881-
SSLEngine client =
2882-
this.ctx.createSSLEngine("wolfSSL test", 11111);
2868+
SSLEngine client = this.ctx.createSSLEngine("wolfSSL test", 11111);
28832869

28842870
server.setUseClientMode(false);
28852871
server.setNeedClientAuth(false);
@@ -2917,8 +2903,7 @@ public void testBufferOverflowSmallOutput()
29172903

29182904
result = server.unwrap(netCopy, tinyOut);
29192905

2920-
if (result.getStatus() !=
2921-
SSLEngineResult.Status.BUFFER_OVERFLOW) {
2906+
if (result.getStatus() != SSLEngineResult.Status.BUFFER_OVERFLOW) {
29222907
error("\t... failed");
29232908
fail("expected BUFFER_OVERFLOW for small output buffer, " +
29242909
"got " + result.getStatus());

src/test/com/wolfssl/provider/jsse/test/WolfSSLSessionContextTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,12 @@ public void testSessionTimeoutBoundaryExpiry()
551551

552552
String originalProp = Security.getProperty(
553553
"wolfjsse.clientSessionCache.disabled");
554-
Security.setProperty("wolfjsse.clientSessionCache.disabled",
555-
"false");
554+
Security.setProperty("wolfjsse.clientSessionCache.disabled", "false");
556555

557556
try {
558557
this.ctx = tf.createSSLContext("TLS", engineProvider);
559558
server = this.ctx.createSSLEngine();
560-
client = this.ctx.createSSLEngine(
561-
"wolfSSL timeout test", 11111);
559+
client = this.ctx.createSSLEngine("wolfSSL timeout test", 11111);
562560

563561
server.setUseClientMode(false);
564562
server.setNeedClientAuth(false);
@@ -658,8 +656,7 @@ public void testSessionInvalidationFilteredFromGetIds()
658656

659657
String originalProp = Security.getProperty(
660658
"wolfjsse.clientSessionCache.disabled");
661-
Security.setProperty("wolfjsse.clientSessionCache.disabled",
662-
"false");
659+
Security.setProperty("wolfjsse.clientSessionCache.disabled", "false");
663660

664661
try {
665662
this.ctx = tf.createSSLContext("TLS", engineProvider);

src/test/com/wolfssl/provider/jsse/test/WolfSSLTestFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,7 @@ public int CloseConnection(SSLEngine server, SSLEngine client,
870870
if (!s.toString().equals("NEED_WRAP") ||
871871
!result.getStatus().name().equals("CLOSED") ) {
872872
throw new SSLException(
873-
"Bad status: HS=" + s +
874-
" status=" + result.getStatus());
873+
"Bad status: HS=" + s + " status=" + result.getStatus());
875874
}
876875

877876
/* server wraps its own close_notify */

0 commit comments

Comments
 (0)