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

#pragma once

#include <memory>

#include <boost/noncopyable.hpp>
#include <boost/signals2.hpp>

#include <openssl/ssl.h>

#include <Swiften/Base/ByteArray.h>
#include <Swiften/TLS/CertificateWithKey.h>
#include <Swiften/TLS/TLSContext.h>

namespace std {
    template<>
    class default_delete<SSL_CTX> {
    public:
        void operator()(SSL_CTX *ptr) {
            SSL_CTX_free(ptr);
        }
    };

    template<>
    class default_delete<SSL> {
    public:
        void operator()(SSL *ptr) {
            SSL_free(ptr);
        }
    };
}

namespace Swift {
    class OpenSSLContext : public TLSContext, boost::noncopyable {
        public:
            OpenSSLContext(Mode mode);
            virtual ~OpenSSLContext() override final;

            void accept() override final;
            void connect() override final;
            void connect(const std::string& requestHostname) override final;

            bool setCertificateChain(const std::vector<Certificate::ref>& certificateChain) override final;
            bool setPrivateKey(const PrivateKey::ref& privateKey) override final;
            bool setClientCertificate(CertificateWithKey::ref cert) override final;
            void setAbortTLSHandshake(bool abort) override final;
            bool setDiffieHellmanParameters(const ByteArray& parametersInOpenSslDer) override final;

            void handleDataFromNetwork(const SafeByteArray&) override final;
            void handleDataFromApplication(const SafeByteArray&) override final;

            std::vector<Certificate::ref> getPeerCertificateChain() const override final;
            std::shared_ptr<CertificateVerificationError> getPeerCertificateVerificationError() const override final;

            virtual ByteArray getFinishMessage() const override final;
            virtual ByteArray getPeerFinishMessage() const override final;

        private:
            static void ensureLibraryInitialized();
            static int handleServerNameCallback(SSL *ssl, int *ad, void *arg);
            static CertificateVerificationError::Type getVerificationErrorTypeForResult(int);

            void initAndSetBIOs();
            void doAccept();
            void doConnect();
            void sendPendingDataToNetwork();
            void sendPendingDataToApplication();

        private:
            enum class State { Start, Accepting, Connecting, Connected, Error };

            const Mode mode_;
            State state_;
            std::unique_ptr<SSL_CTX> context_;
            std::unique_ptr<SSL> handle_;
            BIO* readBIO_ = nullptr;
            BIO* writeBIO_ = nullptr;
            bool abortTLSHandshake_ = false;
    };
}