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

#pragma once

#include <memory>
#include <string>

#include <boost/signals2.hpp>

#include <Swiften/Base/API.h>
#include <Swiften/Base/Error.h>
#include <Swiften/Elements/ToplevelElement.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Session/SessionStream.h>

namespace Swift {
    class CertificateTrustChecker;
    class ClientAuthenticator;
    class CryptoProvider;
    class IDNConverter;
    class Stanza;
    class StanzaAckRequester;
    class StanzaAckResponder;
    class TimerFactory;
    class Timer;

    class SWIFTEN_API ClientSession : public std::enable_shared_from_this<ClientSession> {
        public:
            enum class State {
                Initial,
                WaitingForStreamStart,
                Negotiating,
                Compressing,
                WaitingForEncrypt,
                Encrypting,
                WaitingForCredentials,
                Authenticating,
                EnablingSessionManagement,
                BindingResource,
                StartingSession,
                Initialized,
                Finishing,
                Finished
            };

            struct Error : public Swift::Error {
                enum Type {
                    AuthenticationFailedError,
                    CompressionFailedError,
                    ServerVerificationFailedError,
                    NoSupportedAuthMechanismsError,
                    UnexpectedElementError,
                    ResourceBindError,
                    SessionStartError,
                    TLSClientCertificateError,
                    TLSError,
                    StreamError,
                    StreamEndError, // The server send a closing stream tag.
                } type;
                std::shared_ptr<boost::system::error_code> errorCode;
                Error(Type type) : type(type) {}
            };

            enum UseTLS {
                NeverUseTLS,
                UseTLSWhenAvailable,
                RequireTLS
            };

            ~ClientSession();

            static std::shared_ptr<ClientSession> create(const JID& jid, std::shared_ptr<SessionStream> stream, IDNConverter* idnConverter, CryptoProvider* crypto, TimerFactory* timerFactory) {
                return std::shared_ptr<ClientSession>(new ClientSession(jid, stream, idnConverter, crypto, timerFactory));
            }

            State getState() const {
                return state;
            }

            void setAllowPLAINOverNonTLS(bool b) {
                allowPLAINOverNonTLS = b;
            }

            void setUseStreamCompression(bool b) {
                useStreamCompression = b;
            }

            void setUseTLS(UseTLS b) {
                useTLS = b;
            }

            void setUseAcks(bool b) {
                useAcks = b;
            }

            bool getStreamManagementEnabled() const {
                // Explicitly convert to bool. In C++11, it would be cleaner to
                // compare to nullptr.
                return static_cast<bool>(stanzaAckRequester_);
            }

            bool getRosterVersioningSupported() const {
                return rosterVersioningSupported;
            }

            std::vector<Certificate::ref> getPeerCertificateChain() const {
                return stream->getPeerCertificateChain();
            }

            const JID& getLocalJID() const {
                return localJID;
            }

            void start();
            void finish();

            bool isFinished() const {
                return getState() == State::Finished;
            }

            void sendCredentials(const SafeByteArray& password);
            void sendStanza(std::shared_ptr<Stanza>);

            void setCertificateTrustChecker(CertificateTrustChecker* checker) {
                certificateTrustChecker = checker;
            }

            void setSingleSignOn(bool b) {
                singleSignOn = b;
            }

            /**
             * Sets the port number used in Kerberos authentication
             * Does not affect network connectivity.
             */
            void setAuthenticationPort(int i) {
                authenticationPort = i;
            }

            void setSessionShutdownTimeout(int timeoutInMilliseconds) {
                sessionShutdownTimeoutInMilliseconds = timeoutInMilliseconds;
            }

        public:
            boost::signals2::signal<void ()> onNeedCredentials;
            boost::signals2::signal<void ()> onInitialized;
            boost::signals2::signal<void (std::shared_ptr<Swift::Error>)> onFinished;
            boost::signals2::signal<void (std::shared_ptr<Stanza>)> onStanzaReceived;
            boost::signals2::signal<void (std::shared_ptr<Stanza>)> onStanzaAcked;

        private:
            ClientSession(
                    const JID& jid,
                    std::shared_ptr<SessionStream>,
                    IDNConverter* idnConverter,
                    CryptoProvider* crypto,
                    TimerFactory* timerFactory);

            void finishSession(Error::Type error);
            void finishSession(std::shared_ptr<Swift::Error> error);

            JID getRemoteJID() const {
                return JID("", localJID.getDomain());
            }

            void sendStreamHeader();

            void handleElement(std::shared_ptr<ToplevelElement>);
            void handleStreamStart(const ProtocolHeader&);
            void handleStreamEnd();
            void handleStreamClosed(std::shared_ptr<Swift::Error>);
            void handleStreamShutdownTimeout();

            void handleTLSEncrypted();

            bool checkState(State);
            void continueSessionInitialization();

            void requestAck();
            void handleStanzaAcked(std::shared_ptr<Stanza> stanza);
            void ack(unsigned int handledStanzasCount);
            void continueAfterTLSEncrypted();
            void checkTrustOrFinish(const std::vector<Certificate::ref>& certificateChain, std::shared_ptr<CertificateVerificationError> error);
            void initiateShutdown(bool sendFooter);

        private:
            JID localJID;
            State state;
            std::shared_ptr<SessionStream> stream;
            IDNConverter* idnConverter = nullptr;
            CryptoProvider* crypto = nullptr;
            TimerFactory* timerFactory = nullptr;
            std::shared_ptr<Timer> streamShutdownTimeout;
            int sessionShutdownTimeoutInMilliseconds = 10000;
            bool allowPLAINOverNonTLS;
            bool useStreamCompression;
            UseTLS useTLS;
            bool useAcks;
            bool needSessionStart;
            bool needResourceBind;
            bool needAcking;
            bool rosterVersioningSupported;
            ClientAuthenticator* authenticator;
            std::shared_ptr<StanzaAckRequester> stanzaAckRequester_;
            std::shared_ptr<StanzaAckResponder> stanzaAckResponder_;
            std::shared_ptr<Swift::Error> error_;
            CertificateTrustChecker* certificateTrustChecker;
            bool singleSignOn;
            int authenticationPort;
    };
}