File indexing completed on 2024-05-19 05:01:24

0001 /*
0002     This file is part of the KDE project.
0003 
0004     SPDX-FileCopyrightText: 2009 Dawit Alemayehu <adawit@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "websslinfo.h"
0010 
0011 #include <QVariant>
0012 
0013 
0014 class WebSslInfo::WebSslInfoPrivate
0015 {
0016 public:
0017   WebSslInfoPrivate()
0018       : usedCipherBits(0), supportedCipherBits(0) {}
0019 
0020   QUrl url;
0021   QString ciphers;
0022   QString protocol;
0023   QString certErrors;
0024   QHostAddress peerAddress;
0025   QHostAddress parentAddress;
0026   QList<QSslCertificate> certificateChain;
0027 
0028   int usedCipherBits;
0029   int supportedCipherBits;
0030 };
0031 
0032 WebSslInfo::WebSslInfo()
0033            :d(new WebSslInfo::WebSslInfoPrivate)
0034 {
0035 }
0036 
0037 WebSslInfo::WebSslInfo(const WebSslInfo& other)
0038            :d(new WebSslInfo::WebSslInfoPrivate)
0039 {
0040   *this = other;
0041 }
0042 
0043 WebSslInfo::~WebSslInfo()
0044 {
0045   delete d;
0046   d = nullptr;
0047 }
0048 
0049 bool WebSslInfo::isValid() const
0050 {
0051   return (d ? !d->peerAddress.isNull() : false);
0052 }
0053 
0054 QUrl WebSslInfo::url() const
0055 {
0056   return (d ? d->url : QUrl());
0057 }
0058 
0059 QHostAddress WebSslInfo::parentAddress() const
0060 {
0061   return (d ? d->parentAddress : QHostAddress());
0062 }
0063 
0064 QHostAddress WebSslInfo::peerAddress() const
0065 {
0066   return (d ? d->peerAddress : QHostAddress());
0067 }
0068 
0069 QString WebSslInfo::protocol() const
0070 {
0071   return (d ? d->protocol : QString());
0072 }
0073 
0074 QString WebSslInfo::ciphers() const
0075 {
0076   return (d ?  d->ciphers : QString());
0077 }
0078 
0079 QString WebSslInfo::certificateErrors() const
0080 {
0081   return (d ?  d->certErrors : QString());
0082 }
0083 
0084 int WebSslInfo::supportedChiperBits () const
0085 {
0086   return (d ? d->supportedCipherBits : 0);
0087 }
0088 
0089 int WebSslInfo::usedChiperBits () const
0090 {
0091   return (d ?  d->usedCipherBits : 0);
0092 }
0093 
0094 QList<QSslCertificate> WebSslInfo::certificateChain() const
0095 {
0096   return (d ? d->certificateChain : QList<QSslCertificate>());
0097 }
0098 
0099 WebSslInfo& WebSslInfo::operator=(const WebSslInfo& other)
0100 {
0101   if (d) {
0102     d->ciphers = other.d->ciphers;
0103     d->protocol = other.d->protocol;
0104     d->certErrors = other.d->certErrors;
0105     d->peerAddress = other.d->peerAddress;
0106     d->parentAddress = other.d->parentAddress;
0107     d->certificateChain = other.d->certificateChain;
0108 
0109     d->usedCipherBits = other.d->usedCipherBits;
0110     d->supportedCipherBits = other.d->supportedCipherBits;
0111     d->url = other.d->url;
0112   }
0113 
0114   return *this;
0115 }
0116 
0117 bool WebSslInfo::saveTo(QMap<QString, QVariant>& data) const
0118 {
0119   const bool ok = isValid();
0120   if (ok) {
0121     data.insert(QStringLiteral("ssl_in_use"), true);
0122     data.insert(QStringLiteral("ssl_peer_ip"), d->peerAddress.toString());
0123     data.insert(QStringLiteral("ssl_parent_ip"), d->parentAddress.toString());
0124     data.insert(QStringLiteral("ssl_protocol_version"), d->protocol);
0125     data.insert(QStringLiteral("ssl_cipher"), d->ciphers);
0126     data.insert(QStringLiteral("ssl_cert_errors"), d->certErrors);
0127     data.insert(QStringLiteral("ssl_cipher_used_bits"), d->usedCipherBits);
0128     data.insert(QStringLiteral("ssl_cipher_bits"), d->supportedCipherBits);
0129     QByteArray certChain;
0130     for (const QSslCertificate& cert: d->certificateChain)
0131         certChain += cert.toPem();
0132     data.insert(QStringLiteral("ssl_peer_chain"), certChain);
0133   }
0134 
0135   return ok;
0136 }
0137 
0138 void WebSslInfo::restoreFrom(const QVariant& value, const QUrl& url, bool reset)
0139 {
0140   if (reset) {
0141       *this = WebSslInfo();
0142   }
0143 
0144   if (value.isValid() && value.type() == QVariant::Map) {
0145     QMap<QString,QVariant> metaData = value.toMap();
0146     if (metaData.value(QStringLiteral("ssl_in_use"), false).toBool()) {
0147         setCertificateChain(metaData.value(QStringLiteral("ssl_peer_chain")).toByteArray());
0148         setPeerAddress(metaData.value(QStringLiteral("ssl_peer_ip")).toString());
0149         setParentAddress(metaData.value(QStringLiteral("ssl_parent_ip")).toString());
0150         setProtocol(metaData.value(QStringLiteral("ssl_protocol_version")).toString());
0151         setCiphers(metaData.value(QStringLiteral("ssl_cipher")).toString());
0152         setCertificateErrors(metaData.value(QStringLiteral("ssl_cert_errors")).toString());
0153         setUsedCipherBits(metaData.value(QStringLiteral("ssl_cipher_used_bits")).toString());
0154         setSupportedCipherBits(metaData.value(QStringLiteral("ssl_cipher_bits")).toString());
0155         setUrl(url);
0156     }
0157   }
0158 }
0159 
0160 void WebSslInfo::setUrl (const QUrl &url)
0161 {
0162   if (d)  
0163     d->url = url;
0164 }
0165 
0166 void WebSslInfo::setPeerAddress(const QString& address)
0167 {
0168   if (d)
0169     d->peerAddress = QHostAddress(address);
0170 }
0171 
0172 void WebSslInfo::setParentAddress(const QString& address)
0173 {
0174   if (d)  
0175     d->parentAddress = QHostAddress(address);
0176 }
0177 
0178 void WebSslInfo::setProtocol(const QString& protocol)
0179 {
0180   if (d)
0181     d->protocol = protocol;
0182 }
0183 
0184 void WebSslInfo::setCertificateChain(const QByteArray& chain)
0185 {
0186   if (d)
0187     d->certificateChain = QSslCertificate::fromData(chain);
0188 }
0189 
0190 void WebSslInfo::setCiphers(const QString& ciphers)
0191 {
0192   if (d)
0193     d->ciphers = ciphers;
0194 }
0195 
0196 void WebSslInfo::setUsedCipherBits(const QString& bits)
0197 {
0198   if (d)
0199     d->usedCipherBits = bits.toInt();
0200 }
0201 
0202 void WebSslInfo::setSupportedCipherBits(const QString& bits)
0203 {
0204   if (d)  
0205     d->supportedCipherBits = bits.toInt();
0206 }
0207 
0208 void WebSslInfo::setCertificateErrors(const QString& certErrors)
0209 {
0210   if (d)  
0211     d->certErrors = certErrors;
0212 }