File indexing completed on 2023-10-03 04:18:00

0001 /*
0002  * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
0003  *   @author Andre Moreira Magalhaes <andre.magalhaes@collabora.co.uk>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include "tls-handler.h"
0021 
0022 #include "tls-cert-verifier-op.h"
0023 
0024 #include <KTp/telepathy-handler-application.h>
0025 
0026 #include <QDBusConnection>
0027 
0028 #include <TelepathyQt/Channel>
0029 #include <TelepathyQt/ChannelClassSpecList>
0030 #include <TelepathyQt/ChannelDispatchOperation>
0031 #include <TelepathyQt/MethodInvocationContext>
0032 
0033 #include <QDebug>
0034 
0035 static inline Tp::ChannelClassSpecList channelFilter() {
0036     Tp::ChannelClassSpecList filter;
0037     filter.append(Tp::ChannelClassSpec(TP_QT_IFACE_CHANNEL_TYPE_SERVER_TLS_CONNECTION,
0038                 Tp::HandleTypeNone, false));
0039     return filter;
0040 }
0041 
0042 TlsHandler::TlsHandler()
0043     : Tp::AbstractClientHandler(channelFilter())
0044 {
0045 }
0046 
0047 TlsHandler::~TlsHandler()
0048 {
0049 }
0050 
0051 bool TlsHandler::bypassApproval() const
0052 {
0053     return true;
0054 }
0055 
0056 void TlsHandler::handleChannels(const Tp::MethodInvocationContextPtr<> &context,
0057         const Tp::AccountPtr &account,
0058         const Tp::ConnectionPtr &connection,
0059         const QList<Tp::ChannelPtr> &channels,
0060         const QList<Tp::ChannelRequestPtr> &requestsSatisfied,
0061         const QDateTime &userActionTime,
0062         const Tp::AbstractClientHandler::HandlerInfo &handlerInfo)
0063 {
0064     Q_UNUSED(requestsSatisfied);
0065     Q_UNUSED(userActionTime);
0066     Q_UNUSED(handlerInfo);
0067 
0068     Q_ASSERT(channels.size() == 1);
0069 
0070     KTp::TelepathyHandlerApplication::newJob();
0071     TlsCertVerifierOp *verifier = new TlsCertVerifierOp(
0072             account, connection, channels.first());
0073     connect(verifier,
0074             SIGNAL(ready(Tp::PendingOperation*)),
0075             SLOT(onCertVerifierReady(Tp::PendingOperation*)));
0076     connect(verifier,
0077             SIGNAL(finished(Tp::PendingOperation*)),
0078             SLOT(onCertVerifierFinished(Tp::PendingOperation*)));
0079     mVerifiers.insert(verifier, context);
0080 }
0081 
0082 void TlsHandler::onCertVerifierReady(Tp::PendingOperation *op)
0083 {
0084     TlsCertVerifierOp *verifier = qobject_cast<TlsCertVerifierOp*>(op);
0085     Q_ASSERT(mVerifiers.contains(verifier));
0086 
0087     Tp::MethodInvocationContextPtr<> context = mVerifiers.value(verifier);
0088     context->setFinished();
0089 }
0090 
0091 void TlsHandler::onCertVerifierFinished(Tp::PendingOperation *op)
0092 {
0093     TlsCertVerifierOp *verifier = qobject_cast<TlsCertVerifierOp*>(op);
0094     Q_ASSERT(mVerifiers.contains(verifier));
0095 
0096     if (op->isError()) {
0097         qWarning() << "Error verifying TLS certificate:" << op->errorName() << "-" << op->errorMessage();
0098     }
0099 
0100     mVerifiers.remove(verifier);
0101     KTp::TelepathyHandlerApplication::jobFinished();
0102 }