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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* Copyright (c) 2015-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
/*
This file uses http://www.tls-o-matic.com/ to test the currently configured TLS backend for correct certificate validation behavior.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <Swiften/Base/Log.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/IDN/PlatformIDNConverter.h>
#include <Swiften/Network/BoostConnectionFactory.h>
#include <Swiften/Network/BoostIOServiceThread.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/PlatformDomainNameResolver.h>
#include <Swiften/Network/TLSConnection.h>
#include <Swiften/Network/TLSConnectionFactory.h>
#include <Swiften/TLS/CertificateVerificationError.h>
#include <Swiften/TLS/PlatformTLSFactories.h>
#include <Swiften/TLS/TLSContext.h>
#include <Swiften/TLS/TLSContextFactory.h>
using namespace Swift;
class CertificateErrorTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(CertificateErrorTest);
// These test require the TLS-O-Matic testing CA to be trusted. For more info see https://www.tls-o-matic.com/https/test1 .
CPPUNIT_TEST(testTLS_O_MaticTrusted);
CPPUNIT_TEST(testTLS_O_MaticCertificateFromTheFuture);
CPPUNIT_TEST(testTLS_O_MaticCertificateFromThePast);
CPPUNIT_TEST(testTLS_O_MaticCertificateFromUnknownCA);
CPPUNIT_TEST(testTLS_O_MaticCertificateWrongPurpose);
#if !defined(HAVE_OPENSSL)
// Our OpenSSL backend does not support revocation. We excluded it from the revocation tests.
CPPUNIT_TEST(testRevokedCertificateRevocationDisabled);
CPPUNIT_TEST(testRevokedCertificateRevocationEnabled);
#endif
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
eventLoop_ = new DummyEventLoop();
boostIOServiceThread_ = new BoostIOServiceThread();
boostIOService_ = std::make_shared<boost::asio::io_service>();
connectionFactory_ = new BoostConnectionFactory(boostIOServiceThread_->getIOService(), eventLoop_);
idnConverter_ = PlatformIDNConverter::create();
domainNameResolver_ = new PlatformDomainNameResolver(idnConverter_, eventLoop_);
tlsFactories_ = new PlatformTLSFactories();
tlsContextFactory_ = tlsFactories_->getTLSContextFactory();
tlsContextFactory_->setCheckCertificateRevocation(false);
tlsConnectionFactory_ = new TLSConnectionFactory(tlsContextFactory_, connectionFactory_, TLSOptions());
connectFinished_ = false;
connectFinishedWithError_ = false;
}
void tearDown() {
delete tlsConnectionFactory_;
delete tlsFactories_;
delete domainNameResolver_;
delete idnConverter_;
delete connectionFactory_;
delete boostIOServiceThread_;
while (eventLoop_->hasEvents()) {
eventLoop_->processEvents();
}
delete eventLoop_;
}
HostAddress resolveName(const std::string& name) {
std::shared_ptr<DomainNameAddressQuery> query = domainNameResolver_->createAddressQuery(name);
query->onResult.connect(boost::bind(&CertificateErrorTest::handleAddressQueryResult, this, _1, _2));
lastResoverResult_ = HostAddress();
resolvingDone_ = false;
query->run();
while(!resolvingDone_) {
eventLoop_->processEvents();
}
return lastResoverResult_;
}
void connectToServer(std::shared_ptr<TLSConnection> connection, const std::string& hostname, int port) {
connection->onConnectFinished.connect(boost::bind(&CertificateErrorTest::handleConnectFinished, this, _1));
HostAddress address = resolveName(hostname);
connection->connect(HostAddressPort(address, port));
while (!connectFinished_) {
eventLoop_->processEvents();
}
}
void testTLS_O_MaticTrusted() {
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "test1.tls-o-matic.com", 443);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::ref(), context->getPeerCertificateVerificationError());
}
void testTLS_O_MaticCertificateFromTheFuture() {
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "test5.tls-o-matic.com", 405);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT(context->getPeerCertificateVerificationError());
#if defined(HAVE_SCHANNEL)
// Windows SChannel API does not differentiate between expired and not yet valid.
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Expired, context->getPeerCertificateVerificationError()->getType());
#else
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::NotYetValid, context->getPeerCertificateVerificationError()->getType());
#endif
}
void testTLS_O_MaticCertificateFromThePast() {
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "test6.tls-o-matic.com", 406);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT(context->getPeerCertificateVerificationError());
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Expired, context->getPeerCertificateVerificationError()->getType());
}
void testTLS_O_MaticCertificateFromUnknownCA() {
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "test7.tls-o-matic.com", 407);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT(context->getPeerCertificateVerificationError());
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Untrusted, context->getPeerCertificateVerificationError()->getType());
}
// test14.tls-o-matic.com:414
void testTLS_O_MaticCertificateWrongPurpose() {
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "test14.tls-o-matic.com", 414);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT(context->getPeerCertificateVerificationError());
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::InvalidPurpose, context->getPeerCertificateVerificationError()->getType());
}
void testRevokedCertificateRevocationDisabled() {
tlsContextFactory_->setCheckCertificateRevocation(false);
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "revoked.grc.com", 443);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT(!context->getPeerCertificateVerificationError());
}
void testRevokedCertificateRevocationEnabled() {
tlsContextFactory_->setCheckCertificateRevocation(true);
std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection());
TLSContext* context = connection->getTLSContext();
connectToServer(connection, "revoked.grc.com", 443);
CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_);
CPPUNIT_ASSERT(context->getPeerCertificateVerificationError());
CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Revoked, context->getPeerCertificateVerificationError()->getType());
}
private:
void handleAddressQueryResult(const std::vector<HostAddress>& address, boost::optional<DomainNameResolveError> /* error */) {
if (address.size() > 0) {
lastResoverResult_ = address[0];
}
resolvingDone_ = true;
}
void handleConnectFinished(bool error) {
connectFinished_ = true;
connectFinishedWithError_ = error;
}
private:
BoostIOServiceThread* boostIOServiceThread_;
std::shared_ptr<boost::asio::io_service> boostIOService_;
DummyEventLoop* eventLoop_;
ConnectionFactory* connectionFactory_;
PlatformTLSFactories* tlsFactories_;
TLSContextFactory* tlsContextFactory_;
TLSConnectionFactory* tlsConnectionFactory_;
IDNConverter* idnConverter_;
DomainNameResolver* domainNameResolver_;
HostAddress lastResoverResult_;
bool resolvingDone_;
bool connectFinished_;
bool connectFinishedWithError_;
};
CPPUNIT_TEST_SUITE_REGISTRATION(CertificateErrorTest);
|