summaryrefslogtreecommitdiffstats
blob: a82cd2e70e377f076be0f357e1fb5fb58203d5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2010 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Base/ByteArray.h>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <vector>

#include <Swiften/TLS/ServerIdentityVerifier.h>
#include <Swiften/TLS/SimpleCertificate.h>
#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/IDN/PlatformIDNConverter.h>

using namespace Swift;

class ServerIdentityVerifierTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(ServerIdentityVerifierTest);
		CPPUNIT_TEST(testCertificateVerifies_WithoutMatchingDNSName);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingDNSName);
		CPPUNIT_TEST(testCertificateVerifies_WithSecondMatchingDNSName);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingInternationalDNSName);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingDNSNameWithWildcard);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingDNSNameWithWildcardMatchingNoComponents);
		CPPUNIT_TEST(testCertificateVerifies_WithDNSNameWithWildcardMatchingTwoComponents);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingSRVNameWithoutService);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingSRVNameWithService);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingSRVNameWithServiceAndWildcard);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingSRVNameWithDifferentService);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingXmppAddr);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingXmppAddrWithWildcard);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingInternationalXmppAddr);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingCNWithoutSAN);
		CPPUNIT_TEST(testCertificateVerifies_WithMatchingCNWithSAN);
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			idnConverter = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create());
		}

		void testCertificateVerifies_WithoutMatchingDNSName() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("foo.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingDNSName() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithSecondMatchingDNSName() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("foo.com");
			certificate->addDNSName("bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingInternationalDNSName() {
			ServerIdentityVerifier testling(JID("foo@tron\xc3\xa7on.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("xn--tronon-zua.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingDNSNameWithWildcard() {
			ServerIdentityVerifier testling(JID("foo@im.bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("*.bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingDNSNameWithWildcardMatchingNoComponents() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("*.bar.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithDNSNameWithWildcardMatchingTwoComponents() {
			ServerIdentityVerifier testling(JID("foo@xmpp.im.bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addDNSName("*.bar.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingSRVNameWithoutService() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addSRVName("bar.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingSRVNameWithService() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addSRVName("_xmpp-client.bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingSRVNameWithServiceAndWildcard() {
			ServerIdentityVerifier testling(JID("foo@im.bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addSRVName("_xmpp-client.*.bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingSRVNameWithDifferentService() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addSRVName("_xmpp-server.bar.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingXmppAddr() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addXMPPAddress("bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingXmppAddrWithWildcard() {
			ServerIdentityVerifier testling(JID("foo@im.bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addXMPPAddress("*.bar.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingInternationalXmppAddr() {
			ServerIdentityVerifier testling(JID("foo@tron\xc3\xa7.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addXMPPAddress("tron\xc3\xa7.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingCNWithoutSAN() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addCommonName("bar.com");

			CPPUNIT_ASSERT(testling.certificateVerifies(certificate));
		}

		void testCertificateVerifies_WithMatchingCNWithSAN() {
			ServerIdentityVerifier testling(JID("foo@bar.com/baz"), idnConverter.get());
			SimpleCertificate::ref certificate(new SimpleCertificate());
			certificate->addSRVName("foo.com");
			certificate->addCommonName("bar.com");

			CPPUNIT_ASSERT(!testling.certificateVerifies(certificate));
		}

		boost::shared_ptr<IDNConverter> idnConverter;
};

CPPUNIT_TEST_SUITE_REGISTRATION(ServerIdentityVerifierTest);