File indexing completed on 2024-04-14 04:37:47

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