summaryrefslogtreecommitdiffstats
blob: 463ef9e29e588d89685c9ad2603f01c6742ed85b (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <memory>

#include <boost/bind.hpp>

#include <QA/Checker/IO.h>

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

#include <Swiften/Base/ByteArray.h>
#include <Swiften/TLS/CertificateFactory.h>
#include <Swiften/TLS/TLSContext.h>
#include <Swiften/TLS/PlatformTLSFactories.h>
#include <Swiften/TLS/TLSContextFactory.h>

#include <SwifTools/Application/PlatformApplicationPathProvider.h>

using namespace Swift;

template<typename CERTIFICATE_FACTORY>
class CertificateTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(CertificateTest);
        CPPUNIT_TEST(testConstructFromDER);
        CPPUNIT_TEST(testToDER);
        //CPPUNIT_TEST(testGetSubjectName);
        CPPUNIT_TEST(testGetCommonNames);
        CPPUNIT_TEST(testGetSRVNames);
        CPPUNIT_TEST(testGetDNSNames);
        CPPUNIT_TEST(testGetXMPPAddresses);
        CPPUNIT_TEST(testCreateCertificateChain);
        CPPUNIT_TEST(testCreateTlsContext);
        CPPUNIT_TEST(testCreateTlsContextDisableSystemTAs);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            pathProvider = std::make_unique<PlatformApplicationPathProvider>("FileReadBytestreamTest");
            readByteArrayFromFile(certificateData, (pathProvider->getExecutableDir() / "jabber_org.crt"));
            readByteArrayFromFile(chainData, (pathProvider->getExecutableDir() / "certificateChain.pem"));
            readByteArrayFromFile(keyData, (pathProvider->getExecutableDir() / "privateKey.pem"));
            certificateFactory = std::unique_ptr<CertificateFactory>(new CERTIFICATE_FACTORY());

            PlatformTLSFactories* tlsFactories_ = new PlatformTLSFactories();
            tlsContextFactory_ = tlsFactories_->getTLSContextFactory();
        }

        void testConstructFromDER() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData));

            CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]);
        }

        void testToDER() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData));

            CPPUNIT_ASSERT_EQUAL(certificateData, testling->toDER());
        }

/*
        void testGetSubjectName() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData);

            CPPUNIT_ASSERT_EQUAL(std::string("/description=114072-VMk8pdi1aj5kTXxO/C=US/ST=Colorado/L=Denver/O=Peter Saint-Andre/OU=StartCom Trusted Certificate Member/CN=*.jabber.org/emailAddress=hostmaster@jabber.org"), testling->getSubjectName());
        }
        */

        void testGetCommonNames() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData));

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getCommonNames().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]);
        }

        void testGetSRVNames() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData));

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getSRVNames().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getSRVNames()[0]);
        }

        void testGetDNSNames() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData));

            CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(testling->getDNSNames().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getDNSNames()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("jabber.org"), testling->getDNSNames()[1]);
        }

        void testGetXMPPAddresses() {
            Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData));

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getXMPPAddresses().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getXMPPAddresses()[0]);
        }

        void testCreateCertificateChain() {
            // The input chain contains a 2-certificate chain:
            // the first certificate has:
            // a subject of "O=messaging,CN=Mixer Messaging Configuration,CN=badger.isode.net"
            // an issuer of "O=messaging, CN=New Messaging CA"
            // the second certificate has:
            // a subject of "O=messaging, CN=New Messaging CA"
            // an issuer of "O=messaging, CN=New Messaging CA"
            // i.e. it is a self-signed certificate
            std::vector<std::shared_ptr<Certificate>> chain = certificateFactory->createCertificateChain(chainData);
            CPPUNIT_ASSERT_EQUAL(2,static_cast<int>(chain.size()));
            CPPUNIT_ASSERT_EQUAL(std::string("Mixer Messaging Configuration"), chain[0]->getCommonNames()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("badger.isode.net"), chain[0]->getCommonNames()[1]);
            CPPUNIT_ASSERT_EQUAL(std::string("New Messaging CA"), chain[1]->getCommonNames()[0]);
        }

        void testCreateTlsContext() {
            // Create 2-certificate chain as in previous test
            std::vector<std::shared_ptr<Certificate>> chain = certificateFactory->createCertificateChain(chainData);
            CPPUNIT_ASSERT_EQUAL(2,static_cast<int>(chain.size()));

            // Load private key from string
            PrivateKey::ref key = certificateFactory->createPrivateKey(Swift::createSafeByteArray(keyData));
            CPPUNIT_ASSERT(key);

            const TLSOptions options;
            auto context = tlsContextFactory_->createTLSContext(options, TLSContext::Mode::Server);
            CPPUNIT_ASSERT(context);

            context->setCertificateChain(chain);
            context->setPrivateKey(key);
        }

    /**
     * This test does not actually verify that use of system TAs has been disabled, it just provides
     * a convenient mechanism for testing via a debugger.
     **/
        void testCreateTlsContextDisableSystemTAs() {
            // Create 2-certificate chain as in previous test
            std::vector<std::shared_ptr<Certificate>> chain = certificateFactory->createCertificateChain(chainData);
            CPPUNIT_ASSERT_EQUAL(2,static_cast<int>(chain.size()));

            // Load private key from string
            PrivateKey::ref key = certificateFactory->createPrivateKey(Swift::createSafeByteArray(keyData));
            CPPUNIT_ASSERT(key);

            // Turn off use of system TAs
            TLSOptions options;
            options.ignoreSystemTrustAnchors = true;
            auto context = tlsContextFactory_->createTLSContext(options, TLSContext::Mode::Server);
            CPPUNIT_ASSERT(context);

            context->setCertificateChain(chain);
            context->setPrivateKey(key);
        }
    private:
        std::unique_ptr<PlatformApplicationPathProvider> pathProvider;
        ByteArray certificateData;
        ByteArray chainData;
        ByteArray keyData;
        std::unique_ptr<CertificateFactory> certificateFactory;
        TLSContextFactory* tlsContextFactory_;
};

#ifdef HAVE_OPENSSL
#include <Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h>
CPPUNIT_TEST_SUITE_REGISTRATION(CertificateTest<OpenSSLCertificateFactory>);
#endif