Skip to content

Commit ef6bf9d

Browse files
committed
JSSE: implement WolfSSLPrincipal.toString() inside WolfSSLX509, add JUnit tests for Principal methods including default implies()
1 parent 1a1a4ab commit ef6bf9d

2 files changed

Lines changed: 244 additions & 4 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,5 +716,15 @@ public String getName() {
716716
return this.name;
717717
}
718718

719+
@Override
720+
public String toString() {
721+
722+
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
723+
() -> "entered toString()");
724+
725+
return this.name;
726+
}
727+
728+
719729
}
720730
}

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

Lines changed: 234 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
package com.wolfssl.provider.jsse.test;
2323

2424
import static org.junit.Assert.assertNotNull;
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
2527
import static org.junit.Assert.fail;
2628

2729
import java.io.FileInputStream;
@@ -38,6 +40,8 @@
3840
import java.security.NoSuchAlgorithmException;
3941
import java.security.NoSuchProviderException;
4042
import java.security.UnrecoverableKeyException;
43+
import java.security.Principal;
44+
import javax.security.auth.Subject;
4145
import java.security.cert.CertificateEncodingException;
4246
import java.security.cert.CertificateException;
4347
import java.security.cert.CertificateExpiredException;
@@ -389,25 +393,25 @@ public void testVerifyProvider() {
389393
server.verify(ca.getPublicKey(), sigProvider.getName());
390394
} catch (InvalidKeyException | SignatureException |
391395
NoSuchProviderException e) {
392-
error("\t... failed");
396+
error("\t\t\t... failed");
393397
fail("failed to verify certificate");
394398
}
395399

396400
try {
397401
server.verify(server.getPublicKey(), sigProvider);
398-
error("\t... failed");
402+
error("\t\t\t... failed");
399403
fail("able to verify when should not have been");
400404
} catch (InvalidKeyException | SignatureException e) {
401405
/* expected fail case */
402406
}
403407

404408
} catch (KeyStoreException | NoSuchAlgorithmException |
405409
CertificateException | IOException | WolfSSLException e) {
406-
error("\t... failed");
410+
error("\t\t\t... failed");
407411
e.printStackTrace();
408412
fail("general failure");
409413
}
410-
pass("\t... passed");
414+
pass("\t\t\t... passed");
411415
}
412416

413417

@@ -671,6 +675,232 @@ public void testSubjectAlternativeNames() {
671675
pass("\t... passed");
672676
}
673677

678+
@Test
679+
public void testWolfSSLPrincipalToString() {
680+
WolfSSLX509 x509;
681+
Principal issuerDN, subjectDN;
682+
683+
System.out.print("\tWolfSSLPrincipal toString");
684+
685+
try {
686+
byte[] der = tf.getCert("server");
687+
x509 = new WolfSSLX509(der);
688+
689+
issuerDN = x509.getIssuerDN();
690+
subjectDN = x509.getSubjectDN();
691+
692+
assertNotNull("issuerDN should not be null", issuerDN);
693+
assertNotNull("subjectDN should not be null", subjectDN);
694+
695+
/* Test that toString() returns a non-null, non-empty string */
696+
String issuerString = issuerDN.toString();
697+
String subjectString = subjectDN.toString();
698+
699+
assertNotNull("issuerDN.toString() should not be null",
700+
issuerString);
701+
assertNotNull("subjectDN.toString() should not be null",
702+
subjectString);
703+
assertTrue("issuerDN.toString() should not be empty",
704+
!issuerString.isEmpty());
705+
assertTrue("subjectDN.toString() should not be empty",
706+
!subjectString.isEmpty());
707+
708+
/* Test that toString() returns the same value as getName() */
709+
assertEquals("issuerDN.toString() should equal getName()",
710+
issuerDN.getName(), issuerString);
711+
assertEquals("subjectDN.toString() should equal getName()",
712+
subjectDN.getName(), subjectString);
713+
714+
} catch (KeyStoreException | WolfSSLException |
715+
NoSuchAlgorithmException |
716+
CertificateException | IOException e) {
717+
error("\t... failed");
718+
fail("Exception during WolfSSLPrincipal toString test: " +
719+
e.getMessage());
720+
}
721+
722+
pass("\t... passed");
723+
}
724+
725+
@Test
726+
public void testWolfSSLPrincipalGetName() {
727+
WolfSSLX509 x509;
728+
Principal issuerDN, subjectDN;
729+
730+
System.out.print("\tWolfSSLPrincipal getName");
731+
732+
try {
733+
byte[] der = tf.getCert("server");
734+
x509 = new WolfSSLX509(der);
735+
736+
issuerDN = x509.getIssuerDN();
737+
subjectDN = x509.getSubjectDN();
738+
739+
assertNotNull("issuerDN should not be null", issuerDN);
740+
assertNotNull("subjectDN should not be null", subjectDN);
741+
742+
/* Test that getName() returns formatted DN strings */
743+
String issuerName = issuerDN.getName();
744+
String subjectName = subjectDN.getName();
745+
746+
assertNotNull("issuerDN.getName() should not be null",
747+
issuerName);
748+
assertNotNull("subjectDN.getName() should not be null",
749+
subjectName);
750+
assertTrue("issuerDN.getName() should not be empty",
751+
!issuerName.isEmpty());
752+
assertTrue("subjectDN.getName() should not be empty",
753+
!subjectName.isEmpty());
754+
755+
/* Test that names contain expected DN components */
756+
/* Names should be in "DN=value, DN=value" format,
757+
* not "/DN=value" */
758+
assertTrue("issuer name should not start with /",
759+
!issuerName.startsWith("/"));
760+
assertTrue("subject name should not start with /",
761+
!subjectName.startsWith("/"));
762+
763+
} catch (KeyStoreException | WolfSSLException |
764+
NoSuchAlgorithmException |
765+
CertificateException | IOException e) {
766+
error("\t... failed");
767+
fail("Exception during WolfSSLPrincipal getName test: " +
768+
e.getMessage());
769+
}
770+
771+
pass("\t... passed");
772+
}
773+
774+
@Test
775+
public void testWolfSSLPrincipalInterfaceImplementation() {
776+
WolfSSLX509 x509;
777+
Principal issuerDN, subjectDN;
778+
779+
System.out.print("\tWolfSSLPrincipal interface");
780+
781+
try {
782+
byte[] der = tf.getCert("server");
783+
x509 = new WolfSSLX509(der);
784+
785+
issuerDN = x509.getIssuerDN();
786+
subjectDN = x509.getSubjectDN();
787+
788+
assertNotNull("issuerDN should not be null", issuerDN);
789+
assertNotNull("subjectDN should not be null", subjectDN);
790+
791+
/* Test that objects implement Principal interface */
792+
assertTrue("issuerDN should implement Principal",
793+
issuerDN instanceof Principal);
794+
assertTrue("subjectDN should implement Principal",
795+
subjectDN instanceof Principal);
796+
797+
/* Test equals() behavior -
798+
* same principal should be equal to itself */
799+
assertTrue("issuerDN should equal itself",
800+
issuerDN.equals(issuerDN));
801+
assertTrue("subjectDN should equal itself",
802+
subjectDN.equals(subjectDN));
803+
804+
/* Test hashCode() consistency */
805+
int issuerHash1 = issuerDN.hashCode();
806+
int issuerHash2 = issuerDN.hashCode();
807+
int subjectHash1 = subjectDN.hashCode();
808+
int subjectHash2 = subjectDN.hashCode();
809+
810+
assertEquals("issuerDN hashCode should be consistent",
811+
issuerHash1, issuerHash2);
812+
assertEquals("subjectDN hashCode should be consistent",
813+
subjectHash1, subjectHash2);
814+
815+
} catch (KeyStoreException | WolfSSLException |
816+
NoSuchAlgorithmException |
817+
CertificateException | IOException e) {
818+
error("\t... failed");
819+
fail("Exception during WolfSSLPrincipal interface test: " +
820+
e.getMessage());
821+
}
822+
823+
pass("\t... passed");
824+
}
825+
826+
@Test
827+
public void testWolfSSLPrincipalImplies() {
828+
WolfSSLX509 x509;
829+
Principal issuerDN, subjectDN;
830+
Subject emptySubject, subjectWithPrincipals;
831+
832+
System.out.print("\tWolfSSLPrincipal implies");
833+
834+
try {
835+
byte[] der = tf.getCert("server");
836+
x509 = new WolfSSLX509(der);
837+
838+
issuerDN = x509.getIssuerDN();
839+
subjectDN = x509.getSubjectDN();
840+
841+
assertNotNull("issuerDN should not be null", issuerDN);
842+
assertNotNull("subjectDN should not be null", subjectDN);
843+
844+
/* Test implies() with null Subject */
845+
assertTrue("implies(null) should return false",
846+
!issuerDN.implies(null));
847+
assertTrue("implies(null) should return false",
848+
!subjectDN.implies(null));
849+
850+
/* Test implies() with empty Subject */
851+
emptySubject = new Subject();
852+
assertTrue("implies(empty Subject) should return false",
853+
!issuerDN.implies(emptySubject));
854+
assertTrue("implies(empty Subject) should return false",
855+
!subjectDN.implies(emptySubject));
856+
857+
/* Test implies() with Subject containing different principals */
858+
subjectWithPrincipals = new Subject();
859+
subjectWithPrincipals.getPrincipals().add(issuerDN);
860+
subjectWithPrincipals.getPrincipals().add(subjectDN);
861+
862+
/* Default implementation should return true when Subject
863+
* contains the same principal (equals() returns true) */
864+
assertTrue("implies() should return true when Subject " +
865+
"contains this principal",
866+
issuerDN.implies(subjectWithPrincipals));
867+
assertTrue("implies() should return true when Subject " +
868+
"contains this principal",
869+
subjectDN.implies(subjectWithPrincipals));
870+
871+
/* Test implies() with Subject containing only one principal */
872+
Subject subjectWithIssuerOnly = new Subject();
873+
subjectWithIssuerOnly.getPrincipals().add(issuerDN);
874+
875+
assertTrue("issuerDN.implies() should return true when Subject " +
876+
"contains issuerDN",
877+
issuerDN.implies(subjectWithIssuerOnly));
878+
assertTrue("subjectDN.implies() should return false when " +
879+
"Subject doesn't contain subjectDN",
880+
!subjectDN.implies(subjectWithIssuerOnly));
881+
882+
/* Test implies() with Subject containing only other principal */
883+
Subject subjectWithSubjectOnly = new Subject();
884+
subjectWithSubjectOnly.getPrincipals().add(subjectDN);
885+
886+
assertTrue("subjectDN.implies() should return true when " +
887+
"Subject contains subjectDN",
888+
subjectDN.implies(subjectWithSubjectOnly));
889+
assertTrue("issuerDN.implies() should return false when " +
890+
"Subject doesn't contain issuerDN",
891+
!issuerDN.implies(subjectWithSubjectOnly));
892+
893+
} catch (KeyStoreException | WolfSSLException |
894+
NoSuchAlgorithmException |
895+
CertificateException | IOException e) {
896+
error("\t... failed");
897+
fail("Exception during WolfSSLPrincipal implies test: " +
898+
e.getMessage());
899+
}
900+
901+
pass("\t... passed");
902+
}
903+
674904
private void pass(String msg) {
675905
WolfSSLTestFactory.pass(msg);
676906
}

0 commit comments

Comments
 (0)