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

#pragma once

#include <Security/SecureTransport.h>

#include <Swiften/TLS/TLSContext.h>

namespace Swift {

class SecureTransportContext : public TLSContext {
    public:
        SecureTransportContext(bool checkCertificateRevocation);
        virtual ~SecureTransportContext();

        virtual void connect();

        virtual bool setClientCertificate(CertificateWithKey::ref cert);

        virtual void handleDataFromNetwork(const SafeByteArray&);
        virtual void handleDataFromApplication(const SafeByteArray&);

        virtual std::vector<Certificate::ref> getPeerCertificateChain() const;
        virtual CertificateVerificationError::ref getPeerCertificateVerificationError() const;

        virtual ByteArray getFinishMessage() const;

    private:
        static OSStatus SSLSocketReadCallback(SSLConnectionRef connection, void *data, size_t *dataLength);
        static OSStatus SSLSocketWriteCallback(SSLConnectionRef connection, const void *data, size_t *dataLength);

    private:
        enum State { None, Handshake, HandshakeDone, Error};
        static std::string stateToString(State state);
        void setState(State newState);

        static std::shared_ptr<TLSError> nativeToTLSError(OSStatus error);
        std::shared_ptr<CertificateVerificationError> CSSMErrorToVerificationError(OSStatus resultCode);

        void processHandshake();
        void verifyServerCertificate();

        void fatalError(std::shared_ptr<TLSError> error, std::shared_ptr<CertificateVerificationError> certificateError);

    private:
        std::shared_ptr<SSLContext> sslContext_;
        SafeByteArray readingBuffer_;
        State state_;
        CertificateVerificationError::ref verificationError_;
        CertificateWithKey::ref clientCertificate_;
        bool checkCertificateRevocation_;
};

}