diff options
| author | Linus Jahn <lnj@kaidan.im> | 2019-12-14 22:47:28 +0100 |
|---|---|---|
| committer | LNJ <lnj@kaidan.im> | 2020-01-20 17:14:49 +0100 |
| commit | 28411b5995553eb3a50c826c55517b4c610959c2 (patch) | |
| tree | c22ea3b5bd5c1de2831cb3141940038180ce878e /src/client/QXmppTlsManager.cpp | |
| parent | 55966be2e3e80bd5ab2cc86da2492963dd8127ee (diff) | |
| download | qxmpp-28411b5995553eb3a50c826c55517b4c610959c2.tar.gz | |
Move TLS negotiation into new QXmppTlsManager
Diffstat (limited to 'src/client/QXmppTlsManager.cpp')
| -rw-r--r-- | src/client/QXmppTlsManager.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/client/QXmppTlsManager.cpp b/src/client/QXmppTlsManager.cpp new file mode 100644 index 00000000..0301e20c --- /dev/null +++ b/src/client/QXmppTlsManager.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2008-2019 The QXmpp developers + * + * Authors: + * Linus Jahn + * + * Source: + * https://github.com/qxmpp-project/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#include "QXmppTlsManager.h" + +#include "QXmppConstants_p.h" +#include "QXmppClient.h" +#include "QXmppStreamFeatures.h" +#include "QXmppStartTlsPacket.h" + +#include <QDomElement> + +QXmppTlsManager::QXmppTlsManager() = default; + +bool QXmppTlsManager::handleStanza(const QDomElement &stanza) +{ + if (QXmppStreamFeatures::isStreamFeatures(stanza) && !client()->isSocketEncrypted()) { + QXmppStreamFeatures features; + features.parse(stanza); + + // determine TLS mode to use + const QXmppConfiguration::StreamSecurityMode localSecurity = client()->configuration().streamSecurityMode(); + const QXmppStreamFeatures::Mode remoteSecurity = features.tlsMode(); + if (!client()->socketSupportsSsl() && + (localSecurity == QXmppConfiguration::TLSRequired || + remoteSecurity == QXmppStreamFeatures::Required)) { + warning("Disconnecting since TLS is required, but SSL support is not available"); + client()->disconnectFromServer(); + return true; + } + if (localSecurity == QXmppConfiguration::TLSRequired && + remoteSecurity == QXmppStreamFeatures::Disabled) { + warning("Disconnecting since TLS is required, but not supported by the server"); + client()->disconnectFromServer(); + return true; + } + + if (client()->socketSupportsSsl() && + localSecurity != QXmppConfiguration::TLSDisabled && + remoteSecurity != QXmppStreamFeatures::Disabled) { + // enable TLS since it is supported by both parties + client()->sendPacket(QXmppStartTlsPacket()); + return true; + } + } + + if (QXmppStartTlsPacket::isStartTlsPacket(stanza, QXmppStartTlsPacket::Proceed)) { + debug("Starting encryption"); + client()->startSocketEncryption(); + return true; + } + + return false; +} |
