|
22 | 22 | package com.wolfssl.provider.jsse.test; |
23 | 23 |
|
24 | 24 | import static org.junit.Assert.assertNotNull; |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertTrue; |
25 | 27 | import static org.junit.Assert.fail; |
26 | 28 |
|
27 | 29 | import java.io.FileInputStream; |
|
38 | 40 | import java.security.NoSuchAlgorithmException; |
39 | 41 | import java.security.NoSuchProviderException; |
40 | 42 | import java.security.UnrecoverableKeyException; |
| 43 | +import java.security.Principal; |
| 44 | +import javax.security.auth.Subject; |
41 | 45 | import java.security.cert.CertificateEncodingException; |
42 | 46 | import java.security.cert.CertificateException; |
43 | 47 | import java.security.cert.CertificateExpiredException; |
@@ -389,25 +393,25 @@ public void testVerifyProvider() { |
389 | 393 | server.verify(ca.getPublicKey(), sigProvider.getName()); |
390 | 394 | } catch (InvalidKeyException | SignatureException | |
391 | 395 | NoSuchProviderException e) { |
392 | | - error("\t... failed"); |
| 396 | + error("\t\t\t... failed"); |
393 | 397 | fail("failed to verify certificate"); |
394 | 398 | } |
395 | 399 |
|
396 | 400 | try { |
397 | 401 | server.verify(server.getPublicKey(), sigProvider); |
398 | | - error("\t... failed"); |
| 402 | + error("\t\t\t... failed"); |
399 | 403 | fail("able to verify when should not have been"); |
400 | 404 | } catch (InvalidKeyException | SignatureException e) { |
401 | 405 | /* expected fail case */ |
402 | 406 | } |
403 | 407 |
|
404 | 408 | } catch (KeyStoreException | NoSuchAlgorithmException | |
405 | 409 | CertificateException | IOException | WolfSSLException e) { |
406 | | - error("\t... failed"); |
| 410 | + error("\t\t\t... failed"); |
407 | 411 | e.printStackTrace(); |
408 | 412 | fail("general failure"); |
409 | 413 | } |
410 | | - pass("\t... passed"); |
| 414 | + pass("\t\t\t... passed"); |
411 | 415 | } |
412 | 416 |
|
413 | 417 |
|
@@ -671,6 +675,232 @@ public void testSubjectAlternativeNames() { |
671 | 675 | pass("\t... passed"); |
672 | 676 | } |
673 | 677 |
|
| 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 | + |
674 | 904 | private void pass(String msg) { |
675 | 905 | WolfSSLTestFactory.pass(msg); |
676 | 906 | } |
|
0 commit comments