|
23 | 23 |
|
24 | 24 | #include <tests/api/test_asn.h> |
25 | 25 |
|
| 26 | +#include <wolfssl/wolfcrypt/asn.h> |
| 27 | + |
26 | 28 | #if defined(WC_ENABLE_ASYM_KEY_EXPORT) && defined(HAVE_ED25519) |
27 | 29 | static int test_SetAsymKeyDer_once(byte* privKey, word32 privKeySz, byte* pubKey, |
28 | 30 | word32 pubKeySz, byte* trueDer, word32 trueDerSz) |
@@ -638,3 +640,150 @@ int test_wc_IndexSequenceOf(void) |
638 | 640 |
|
639 | 641 | return EXPECT_RESULT(); |
640 | 642 | } |
| 643 | + |
| 644 | +int test_wolfssl_local_MatchBaseName(void) |
| 645 | +{ |
| 646 | + EXPECT_DECLS; |
| 647 | + |
| 648 | +#if !defined(NO_CERTS) && !defined(NO_ASN) && !defined(IGNORE_NAME_CONSTRAINTS) |
| 649 | + /* |
| 650 | + * Tests for DNS type (ASN_DNS_TYPE = 0x02) |
| 651 | + */ |
| 652 | + |
| 653 | + /* Positive tests - should match */ |
| 654 | + /* Exact match */ |
| 655 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 656 | + "domain.com", 10, "domain.com", 10), 1); |
| 657 | + /* Case insensitive match */ |
| 658 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 659 | + "DOMAIN.COM", 10, "domain.com", 10), 1); |
| 660 | + /* Subdomain match (RFC 5280: adding labels to the left) */ |
| 661 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 662 | + "sub.domain.com", 14, "domain.com", 10), 1); |
| 663 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 664 | + "a.b.domain.com", 14, "domain.com", 10), 1); |
| 665 | + /* Leading dot constraint with subdomain (not RFC 5280 compliant for DNS, |
| 666 | + * but kept for backwards compatibility) */ |
| 667 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 668 | + "sub.domain.com", 14, ".domain.com", 11), 1); |
| 669 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 670 | + "a.b.domain.com", 14, ".domain.com", 11), 1); |
| 671 | + |
| 672 | + /* Negative tests - should NOT match */ |
| 673 | + /* Bug #3: fakedomain.com should NOT match domain.com (no dot boundary) */ |
| 674 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 675 | + "fakedomain.com", 14, "domain.com", 10), 0); |
| 676 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 677 | + "notdomain.com", 13, "domain.com", 10), 0); |
| 678 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 679 | + "xexample.com", 12, "example.com", 11), 0); |
| 680 | + /* Bug #3: fakedomain.com should NOT match .domain.com */ |
| 681 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 682 | + "fakedomain.com", 14, ".domain.com", 11), 0); |
| 683 | + /* domain.com should NOT match .domain.com (leading dot requires subdomain) */ |
| 684 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 685 | + "domain.com", 10, ".domain.com", 11), 0); |
| 686 | + /* Different domain */ |
| 687 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 688 | + "other.com", 9, "domain.com", 10), 0); |
| 689 | + /* Name starting with dot */ |
| 690 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 691 | + ".domain.com", 11, "domain.com", 10), 0); |
| 692 | + |
| 693 | + /* |
| 694 | + * Tests for email type (ASN_RFC822_TYPE = 0x01) |
| 695 | + */ |
| 696 | + |
| 697 | + /* Positive tests - should match */ |
| 698 | + /* Exact email match */ |
| 699 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 700 | + |
| 701 | + /* Email with domain constraint (leading dot) - subdomain present */ |
| 702 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 703 | + "[email protected]", 19, ".domain.com", 11), 1); |
| 704 | + /* Email with domain constraint (no leading dot) - exact domain */ |
| 705 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 706 | + "[email protected]", 15, "domain.com", 10), 1); |
| 707 | + |
| 708 | + /* Negative tests - should NOT match */ |
| 709 | + /* [email protected] should NOT match .domain.com (subdomain required) */ |
| 710 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 711 | + "[email protected]", 15, ".domain.com", 11), 0); |
| 712 | + /* [email protected] should NOT match domain.com (exact domain only) */ |
| 713 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 714 | + "[email protected]", 19, "domain.com", 10), 0); |
| 715 | + /* @ at start is invalid */ |
| 716 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 717 | + "@domain.com", 11, ".domain.com", 11), 0); |
| 718 | + /* @ at end is invalid */ |
| 719 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 720 | + "user@", 5, ".domain.com", 11), 0); |
| 721 | + /* double @ is invalid */ |
| 722 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 723 | + "user@@domain.com", 16, ".domain.com", 11), 0); |
| 724 | + /* multiple @ is invalid */ |
| 725 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 726 | + "user@[email protected]", 21, ".domain.com", 11), 0); |
| 727 | + /* No @ in email name */ |
| 728 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 729 | + "userdomain.com", 14, ".domain.com", 11), 0); |
| 730 | + /* Email domain doesn't match constraint */ |
| 731 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 732 | + "[email protected]", 14, ".domain.com", 11), 0); |
| 733 | + /* Email suffix without dot boundary (fakedomain) */ |
| 734 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 735 | + "[email protected]", 19, ".domain.com", 11), 0); |
| 736 | + /* Base constraint with invalid @ position */ |
| 737 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 738 | + "[email protected]", 15, "@domain.com", 11), 0); |
| 739 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE, |
| 740 | + "[email protected]", 15, "user@", 5), 0); |
| 741 | + |
| 742 | + /* |
| 743 | + * Tests for directory type (ASN_DIR_TYPE = 0x04) |
| 744 | + */ |
| 745 | + |
| 746 | + /* Positive tests - should match */ |
| 747 | + /* Exact match */ |
| 748 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE, |
| 749 | + "CN=test", 7, "CN=test", 7), 1); |
| 750 | + /* Prefix match (name longer than base) */ |
| 751 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE, |
| 752 | + "CN=test,O=org", 13, "CN=test", 7), 1); |
| 753 | + |
| 754 | + /* Negative tests - should NOT match */ |
| 755 | + /* Different content */ |
| 756 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE, |
| 757 | + "CN=other", 8, "CN=test", 7), 0); |
| 758 | + /* Case sensitive for directory */ |
| 759 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE, |
| 760 | + "CN=TEST", 7, "CN=test", 7), 0); |
| 761 | + |
| 762 | + /* |
| 763 | + * Edge cases and error handling |
| 764 | + */ |
| 765 | + |
| 766 | + /* NULL pointers */ |
| 767 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 768 | + NULL, 10, "domain.com", 10), 0); |
| 769 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 770 | + "domain.com", 10, NULL, 10), 0); |
| 771 | + /* Empty/zero size */ |
| 772 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 773 | + "", 0, "domain.com", 10), 0); |
| 774 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 775 | + "domain.com", 10, "", 0), 0); |
| 776 | + /* Invalid type */ |
| 777 | + ExpectIntEQ(wolfssl_local_MatchBaseName(0xFF, |
| 778 | + "domain.com", 10, "domain.com", 10), 0); |
| 779 | + /* Name starting with dot */ |
| 780 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 781 | + ".", 1, ".", 1), 0); |
| 782 | + /* Name shorter than base */ |
| 783 | + ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE, |
| 784 | + "a.com", 5, "domain.com", 10), 0); |
| 785 | + |
| 786 | +#endif /* !NO_CERTS && !NO_ASN && !IGNORE_NAME_CONSTRAINTS */ |
| 787 | + |
| 788 | + return EXPECT_RESULT(); |
| 789 | +} |
0 commit comments