• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

ktcpsocket.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2007, 2008 Andreas Hartmetz <ahartmetz@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "ktcpsocket.h"
00021 #include "ktcpsocket_p.h"
00022 
00023 #include <kdebug.h>
00024 #include <kurl.h>
00025 #include <kglobal.h>
00026 #include <ksslcertificatemanager.h>
00027 #include <kstandarddirs.h>
00028 #include <klocale.h>
00029 
00030 #include <QtCore/QMutex>
00031 #include <QtCore/QStringList>
00032 #include <QtNetwork/QSslKey>
00033 #include <QtNetwork/QSslCipher>
00034 #include <QtNetwork/QNetworkProxy>
00035 
00036 
00037 static KTcpSocket::SslVersion kSslVersionFromQ(QSsl::SslProtocol protocol)
00038 {
00039     switch (protocol) {
00040     case QSsl::SslV2:
00041         return KTcpSocket::SslV2;
00042     case QSsl::SslV3:
00043         return KTcpSocket::SslV3;
00044     case QSsl::TlsV1:
00045         return KTcpSocket::TlsV1;
00046     case QSsl::AnyProtocol:
00047         return KTcpSocket::AnySslVersion;
00048     default:
00049         return KTcpSocket::UnknownSslVersion;
00050     }
00051 }
00052 
00053 
00054 static QSsl::SslProtocol qSslProtocolFromK(KTcpSocket::SslVersion sslVersion)
00055 {
00056     //### this lowlevel bit-banging is a little dangerous and a likely source of bugs
00057     if (sslVersion == KTcpSocket::AnySslVersion) {
00058         return QSsl::AnyProtocol;
00059     }
00060     //does it contain any valid protocol?
00061     if (!(sslVersion & (KTcpSocket::SslV2 | KTcpSocket::SslV3 | KTcpSocket::TlsV1))) {
00062         return QSsl::UnknownProtocol;
00063     }
00064 
00065     switch (sslVersion) {
00066     case KTcpSocket::SslV2:
00067         return QSsl::SslV2;
00068     case KTcpSocket::SslV3:
00069         return QSsl::SslV3;
00070     case KTcpSocket::TlsV1:
00071         return QSsl::TlsV1;
00072     default:
00073         //QSslSocket doesn't really take arbitrary combinations. It's one or all.
00074         return QSsl::AnyProtocol;
00075     }
00076 }
00077 
00078 
00079 //cipher class converter KSslCipher -> QSslCipher
00080 class CipherCc
00081 {
00082 public:
00083     CipherCc()
00084     {
00085         foreach (const QSslCipher &c, QSslSocket::supportedCiphers()) {
00086             allCiphers.insert(c.name(), c);
00087         }
00088     }
00089 
00090     QSslCipher converted(const KSslCipher &ksc)
00091     {
00092         return allCiphers.value(ksc.name());
00093     }
00094 
00095 private:
00096     QHash<QString, QSslCipher> allCiphers;
00097 };
00098 
00099 
00100 class KSslErrorPrivate
00101 {
00102 public:
00103     static KSslError::Error errorFromQSslError(QSslError::SslError e)
00104     {
00105         switch (e) {
00106         case QSslError::NoError:
00107             return KSslError::NoError;
00108         case QSslError::UnableToGetLocalIssuerCertificate:
00109         case QSslError::InvalidCaCertificate:
00110             return KSslError::InvalidCertificateAuthorityCertificate;
00111         case QSslError::InvalidNotBeforeField:
00112         case QSslError::InvalidNotAfterField:
00113         case QSslError::CertificateNotYetValid:
00114         case QSslError::CertificateExpired:
00115             return KSslError::ExpiredCertificate;
00116         case QSslError::UnableToDecodeIssuerPublicKey:
00117         case QSslError::SubjectIssuerMismatch:
00118         case QSslError::AuthorityIssuerSerialNumberMismatch:
00119             return KSslError::InvalidCertificate;
00120         case QSslError::SelfSignedCertificate:
00121         case QSslError::SelfSignedCertificateInChain:
00122             return KSslError::SelfSignedCertificate;
00123         case QSslError::CertificateRevoked:
00124             return KSslError::RevokedCertificate;
00125         case QSslError::InvalidPurpose:
00126             return KSslError::InvalidCertificatePurpose;
00127         case QSslError::CertificateUntrusted:
00128             return KSslError::UntrustedCertificate;
00129         case QSslError::CertificateRejected:
00130             return KSslError::RejectedCertificate;
00131         case QSslError::NoPeerCertificate:
00132             return KSslError::NoPeerCertificate;
00133         case QSslError::HostNameMismatch:
00134             return KSslError::HostNameMismatch;
00135         case QSslError::UnableToVerifyFirstCertificate:
00136         case QSslError::UnableToDecryptCertificateSignature:
00137         case QSslError::UnableToGetIssuerCertificate:
00138         case QSslError::CertificateSignatureFailed:
00139             return KSslError::CertificateSignatureFailed;
00140         case QSslError::PathLengthExceeded:
00141             return KSslError::PathLengthExceeded;
00142         case QSslError::UnspecifiedError:
00143         case QSslError::NoSslSupport:
00144         default:
00145             return KSslError::UnknownError;
00146         }
00147     }
00148 
00149     static QString errorString(KSslError::Error e)
00150     {
00151         switch (e) {
00152         case KSslError::NoError:
00153             return i18nc("SSL error","No error");
00154         case KSslError::InvalidCertificateAuthorityCertificate:
00155             return i18nc("SSL error","The certificate authority's certificate is invalid");
00156         case KSslError::ExpiredCertificate:
00157             return i18nc("SSL error","The certificate has expired");
00158         case KSslError::InvalidCertificate:
00159             return i18nc("SSL error","The certificate is invalid");
00160         case KSslError::SelfSignedCertificate:
00161             return i18nc("SSL error","The certificate is not signed by any trusted certificate authority");
00162         case KSslError::RevokedCertificate:
00163             return i18nc("SSL error","The certificate has been revoked");
00164         case KSslError::InvalidCertificatePurpose:
00165             return i18nc("SSL error","The certificate is unsuitable for this purpose");
00166         case KSslError::UntrustedCertificate:
00167             return i18nc("SSL error","The root certificate authority's certificate is not trusted for this purpose");
00168         case KSslError::RejectedCertificate:
00169             return i18nc("SSL error","The certificate authority's certificate is marked to reject this certificate's purpose");
00170         case KSslError::NoPeerCertificate:
00171             return i18nc("SSL error","The peer did not present any certificate");
00172         case KSslError::HostNameMismatch:
00173             return i18nc("SSL error","The certificate does not apply to the given host");
00174         case KSslError::CertificateSignatureFailed:
00175             return i18nc("SSL error","The certificate cannot be verified for internal reasons");
00176         case KSslError::PathLengthExceeded:
00177             return i18nc("SSL error","The certificate chain is too long");
00178         case KSslError::UnknownError:
00179         default:
00180             return i18nc("SSL error","Unknown error");
00181         }
00182     }
00183 
00184     KSslError::Error error;
00185     QSslCertificate certificate;
00186 };
00187 
00188 
00189 KSslError::KSslError(Error errorCode, const QSslCertificate &certificate)
00190  : d(new KSslErrorPrivate())
00191 {
00192     d->error = errorCode;
00193     d->certificate = certificate;
00194 }
00195 
00196 
00197 KSslError::KSslError(const QSslError &other)
00198  : d(new KSslErrorPrivate())
00199 {
00200     d->error = KSslErrorPrivate::errorFromQSslError(other.error());
00201     d->certificate = other.certificate();
00202 }
00203 
00204 
00205 KSslError::KSslError(const KSslError &other)
00206  : d(new KSslErrorPrivate())
00207 {
00208     *d = *other.d;
00209 }
00210 
00211 
00212 KSslError::~KSslError()
00213 {
00214     delete d;
00215 }
00216 
00217 
00218 KSslError &KSslError::operator=(const KSslError &other)
00219 {
00220     *d = *other.d;
00221     return *this;
00222 }
00223 
00224 
00225 KSslError::Error KSslError::error() const
00226 {
00227     return d->error;
00228 }
00229 
00230 
00231 QString KSslError::errorString() const
00232 {
00233     return KSslErrorPrivate::errorString(d->error);
00234 }
00235 
00236 
00237 QSslCertificate KSslError::certificate() const
00238 {
00239     return d->certificate;
00240 }
00241 
00242 
00243 class KTcpSocketPrivate
00244 {
00245 public:
00246     KTcpSocketPrivate(KTcpSocket *qq)
00247      : q(qq),
00248        certificatesLoaded(false),
00249        emittedReadyRead(false)
00250     {
00251         // create the instance, which sets Qt's static internal cert set to empty.
00252         KSslCertificateManager::self();
00253     }
00254 
00255     KTcpSocket::State state(QAbstractSocket::SocketState s)
00256     {
00257         switch (s) {
00258         case QAbstractSocket::UnconnectedState:
00259             return KTcpSocket::UnconnectedState;
00260         case QAbstractSocket::HostLookupState:
00261             return KTcpSocket::HostLookupState;
00262         case QAbstractSocket::ConnectingState:
00263             return KTcpSocket::ConnectingState;
00264         case QAbstractSocket::ConnectedState:
00265             return KTcpSocket::ConnectedState;
00266         case QAbstractSocket::ClosingState:
00267             return KTcpSocket::ClosingState;
00268         case QAbstractSocket::BoundState:
00269         case QAbstractSocket::ListeningState:
00270             //### these two are not relevant as long as this can't be a server socket
00271         default:
00272             return KTcpSocket::UnconnectedState; //the closest to "error"
00273         }
00274     }
00275 
00276     KTcpSocket::EncryptionMode encryptionMode(QSslSocket::SslMode mode)
00277     {
00278         switch (mode) {
00279         case QSslSocket::SslClientMode:
00280             return KTcpSocket::SslClientMode;
00281         case QSslSocket::SslServerMode:
00282             return KTcpSocket::SslServerMode;
00283         default:
00284             return KTcpSocket::UnencryptedMode;
00285         }
00286     }
00287 
00288     KTcpSocket::Error errorFromAbsSocket(QAbstractSocket::SocketError e)
00289     {
00290         switch (e) {
00291         case QAbstractSocket::ConnectionRefusedError:
00292             return KTcpSocket::ConnectionRefusedError;
00293         case QAbstractSocket::RemoteHostClosedError:
00294             return KTcpSocket::RemoteHostClosedError;
00295         case QAbstractSocket::HostNotFoundError:
00296             return KTcpSocket::HostNotFoundError;
00297         case QAbstractSocket::SocketAccessError:
00298             return KTcpSocket::SocketAccessError;
00299         case QAbstractSocket::SocketResourceError:
00300             return KTcpSocket::SocketResourceError;
00301         case QAbstractSocket::SocketTimeoutError:
00302             return KTcpSocket::SocketTimeoutError;
00303         case QAbstractSocket::NetworkError:
00304             return KTcpSocket::NetworkError;
00305         case QAbstractSocket::UnsupportedSocketOperationError:
00306             return KTcpSocket::UnsupportedSocketOperationError;
00307         case QAbstractSocket::DatagramTooLargeError:
00308             //we don't do UDP
00309         case QAbstractSocket::AddressInUseError:
00310         case QAbstractSocket::SocketAddressNotAvailableError:
00311             //### own values if/when we ever get server socket support
00312         case QAbstractSocket::ProxyAuthenticationRequiredError:
00313             //### maybe we need an enum value for this
00314         case QAbstractSocket::UnknownSocketError:
00315         default:
00316             return KTcpSocket::UnknownError;
00317         }
00318     }
00319 
00320     //private slots
00321     void reemitSocketError(QAbstractSocket::SocketError e)
00322     {
00323         emit q->error(errorFromAbsSocket(e));
00324     }
00325 
00326     void reemitSslErrors(const QList<QSslError> &errors)
00327     {
00328         q->showSslErrors(); //H4X
00329         QList<KSslError> kErrors;
00330         foreach (const QSslError &e, errors) {
00331             kErrors.append(KSslError(e));
00332         }
00333         emit q->sslErrors(kErrors);
00334     }
00335 
00336     void reemitStateChanged(QAbstractSocket::SocketState s)
00337     {
00338         emit q->stateChanged(state(s));
00339     }
00340 
00341     void reemitModeChanged(QSslSocket::SslMode m)
00342     {
00343         emit q->encryptionModeChanged(encryptionMode(m));
00344     }
00345 
00346     // This method is needed because we might emit readyRead() due to this QIODevice
00347     // having some data buffered, so we need to care about blocking, too.
00348     //### useless ATM as readyRead() now just calls d->sock.readyRead().
00349     void reemitReadyRead()
00350     {
00351         if (!emittedReadyRead) {
00352             emittedReadyRead = true;
00353             emit q->readyRead();
00354             emittedReadyRead = false;
00355         }
00356     }
00357 
00358     void maybeLoadCertificates()
00359     {
00360         if (!certificatesLoaded) {
00361             sock.setCaCertificates(KSslCertificateManager::self()->rootCertificates());
00362             certificatesLoaded = true;
00363         }
00364     }
00365 
00366     KTcpSocket *const q;
00367     bool certificatesLoaded;
00368     bool emittedReadyRead;
00369     QSslSocket sock;
00370     QList<KSslCipher> ciphers;
00371     KTcpSocket::SslVersion advertisedSslVersion;
00372     CipherCc ccc;
00373 };
00374 
00375 
00376 KTcpSocket::KTcpSocket(QObject *parent)
00377  : QIODevice(parent),
00378    d(new KTcpSocketPrivate(this))
00379 {
00380     d->advertisedSslVersion = SslV3;
00381 
00382     connect(&d->sock, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose()));
00383     connect(&d->sock, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64)));
00384     connect(&d->sock, SIGNAL(readyRead()), this, SLOT(reemitReadyRead()));
00385     connect(&d->sock, SIGNAL(connected()), this, SIGNAL(connected()));
00386     connect(&d->sock, SIGNAL(encrypted()), this, SIGNAL(encrypted()));
00387     connect(&d->sock, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
00388     connect(&d->sock, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)),
00389             this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
00390     connect(&d->sock, SIGNAL(error(QAbstractSocket::SocketError)),
00391             this, SLOT(reemitSocketError(QAbstractSocket::SocketError)));
00392     connect(&d->sock, SIGNAL(sslErrors(const QList<QSslError> &)),
00393             this, SLOT(reemitSslErrors(const QList<QSslError> &)));
00394     connect(&d->sock, SIGNAL(hostFound()), this, SIGNAL(hostFound()));
00395     connect(&d->sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
00396             this, SLOT(reemitStateChanged(QAbstractSocket::SocketState)));
00397     connect(&d->sock, SIGNAL(modeChanged(QSslSocket::SslMode)),
00398             this, SLOT(reemitModeChanged(QSslSocket::SslMode)));
00399 }
00400 
00401 
00402 KTcpSocket::~KTcpSocket()
00403 {
00404     delete d;
00405 }
00406 
00408 
00409 bool KTcpSocket::atEnd() const
00410 {
00411     return d->sock.atEnd() && QIODevice::atEnd();
00412 }
00413 
00414 
00415 qint64 KTcpSocket::bytesAvailable() const
00416 {
00417     return d->sock.bytesAvailable() + QIODevice::bytesAvailable();
00418 }
00419 
00420 
00421 qint64 KTcpSocket::bytesToWrite() const
00422 {
00423     return d->sock.bytesToWrite();
00424 }
00425 
00426 
00427 bool KTcpSocket::canReadLine() const
00428 {
00429     return d->sock.canReadLine() || QIODevice::canReadLine();
00430 }
00431 
00432 
00433 void KTcpSocket::close()
00434 {
00435     d->sock.close();
00436     QIODevice::close();
00437 }
00438 
00439 
00440 bool KTcpSocket::isSequential() const
00441 {
00442     return true;
00443 }
00444 
00445 
00446 bool KTcpSocket::open(QIODevice::OpenMode open)
00447 {
00448     bool ret = d->sock.open(open);
00449     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00450     return ret;
00451 }
00452 
00453 
00454 bool KTcpSocket::waitForBytesWritten(int msecs)
00455 {
00456     return d->sock.waitForBytesWritten(msecs);
00457 }
00458 
00459 
00460 bool KTcpSocket::waitForReadyRead(int msecs)
00461 {
00462     return d->sock.waitForReadyRead(msecs);
00463 }
00464 
00465 
00466 qint64 KTcpSocket::readData(char *data, qint64 maxSize)
00467 {
00468     return d->sock.read(data, maxSize);
00469 }
00470 
00471 
00472 qint64 KTcpSocket::writeData(const char *data, qint64 maxSize)
00473 {
00474     return d->sock.write(data, maxSize);
00475 }
00476 
00478 
00479 void KTcpSocket::abort()
00480 {
00481     d->sock.abort();
00482 }
00483 
00484 
00485 void KTcpSocket::connectToHost(const QString &hostName, quint16 port, ProxyPolicy policy)
00486 {
00487     if (policy == AutoProxy) {
00488         //###
00489     }
00490     d->sock.connectToHost(hostName, port);
00491     // there are enough layers of buffers between us and the network, and there is a quirk
00492     // in QIODevice that can make it try to readData() twice per read() call if buffered and
00493     // reaData() does not deliver enough data the first time. like when the other side is
00494     // simply not sending any more data...
00495     // this can *apparently* lead to long delays sometimes which stalls applications.
00496     // do not want.
00497     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00498 }
00499 
00500 
00501 void KTcpSocket::connectToHost(const QHostAddress &hostAddress, quint16 port, ProxyPolicy policy)
00502 {
00503     if (policy == AutoProxy) {
00504         //###
00505     }
00506     d->sock.connectToHost(hostAddress, port);
00507     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00508 }
00509 
00510 
00511 void KTcpSocket::connectToHost(const KUrl &url, ProxyPolicy policy)
00512 {
00513     if (policy == AutoProxy) {
00514         //###
00515     }
00516     d->sock.connectToHost(url.host(), url.port());
00517     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00518 }
00519 
00520 
00521 void KTcpSocket::disconnectFromHost()
00522 {
00523     d->sock.disconnectFromHost();
00524     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00525 }
00526 
00527 
00528 KTcpSocket::Error KTcpSocket::error() const
00529 {
00530     return d->errorFromAbsSocket(d->sock.error());
00531 }
00532 
00533 
00534 QList<KSslError> KTcpSocket::sslErrors() const
00535 {
00536     //### pretty slow; also consider throwing out duplicate error codes. We may get
00537     //    duplicates even though there were none in the original list because KSslError
00538     //    has a smallest common denominator range of SSL error codes.
00539     QList<KSslError> ret;
00540     foreach (const QSslError &e, d->sock.sslErrors())
00541         ret.append(KSslError(e));
00542     return ret;
00543 }
00544 
00545 
00546 bool KTcpSocket::flush()
00547 {
00548     return d->sock.flush();
00549 }
00550 
00551 
00552 bool KTcpSocket::isValid() const
00553 {
00554     return d->sock.isValid();
00555 }
00556 
00557 
00558 QHostAddress KTcpSocket::localAddress() const
00559 {
00560     return d->sock.localAddress();
00561 }
00562 
00563 
00564 QHostAddress KTcpSocket::peerAddress() const
00565 {
00566     return d->sock.peerAddress();
00567 }
00568 
00569 
00570 QString KTcpSocket::peerName() const
00571 {
00572     return d->sock.peerName();
00573 }
00574 
00575 
00576 quint16 KTcpSocket::peerPort() const
00577 {
00578     return d->sock.peerPort();
00579 }
00580 
00581 
00582 QNetworkProxy KTcpSocket::proxy() const
00583 {
00584     return d->sock.proxy();
00585 }
00586 
00587 
00588 qint64 KTcpSocket::readBufferSize() const
00589 {
00590     return d->sock.readBufferSize();
00591 }
00592 
00593 
00594 void KTcpSocket::setProxy(const QNetworkProxy &proxy)
00595 {
00596     d->sock.setProxy(proxy);
00597 }
00598 
00599 
00600 void KTcpSocket::setReadBufferSize(qint64 size)
00601 {
00602     d->sock.setReadBufferSize(size);
00603 }
00604 
00605 
00606 KTcpSocket::State KTcpSocket::state() const
00607 {
00608     return d->state(d->sock.state());
00609 }
00610 
00611 
00612 bool KTcpSocket::waitForConnected(int msecs)
00613 {
00614     bool ret = d->sock.waitForConnected(msecs);
00615     if (!ret)
00616         setErrorString(d->sock.errorString());
00617     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00618     return ret;
00619 }
00620 
00621 
00622 bool KTcpSocket::waitForDisconnected(int msecs)
00623 {
00624     bool ret = d->sock.waitForDisconnected(msecs);
00625     if (!ret)
00626         setErrorString(d->sock.errorString());
00627     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00628     return ret;
00629 }
00630 
00632 
00633 void KTcpSocket::addCaCertificate(const QSslCertificate &certificate)
00634 {
00635     d->sock.addCaCertificate(certificate);
00636 }
00637 
00638 
00639 /*
00640 bool KTcpSocket::addCaCertificates(const QString &path, QSsl::EncodingFormat format,
00641                                    QRegExp::PatternSyntax syntax)
00642 {
00643     return d->sock.addCaCertificates(path, format, syntax);
00644 }
00645 */
00646 
00647 
00648 void KTcpSocket::addCaCertificates(const QList<QSslCertificate> &certificates)
00649 {
00650     d->sock.addCaCertificates(certificates);
00651 }
00652 
00653 
00654 QList<QSslCertificate> KTcpSocket::caCertificates() const
00655 {
00656     d->maybeLoadCertificates();
00657     return d->sock.caCertificates();
00658 }
00659 
00660 
00661 QList<KSslCipher> KTcpSocket::ciphers() const
00662 {
00663     return d->ciphers;
00664 }
00665 
00666 
00667 void KTcpSocket::connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode openMode)
00668 {
00669     d->maybeLoadCertificates();
00670     d->sock.setProtocol(qSslProtocolFromK(d->advertisedSslVersion));
00671     d->sock.connectToHostEncrypted(hostName, port, openMode);
00672     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00673 }
00674 
00675 
00676 QSslCertificate KTcpSocket::localCertificate() const
00677 {
00678     return d->sock.localCertificate();
00679 }
00680 
00681 
00682 QList<QSslCertificate> KTcpSocket::peerCertificateChain() const
00683 {
00684     return d->sock.peerCertificateChain();
00685 }
00686 
00687 
00688 KSslKey KTcpSocket::privateKey() const
00689 {
00690     return KSslKey(d->sock.privateKey());
00691 }
00692 
00693 
00694 KSslCipher KTcpSocket::sessionCipher() const
00695 {
00696     return KSslCipher(d->sock.sessionCipher());
00697 }
00698 
00699 
00700 void KTcpSocket::setCaCertificates(const QList<QSslCertificate> &certificates)
00701 {
00702     d->sock.setCaCertificates(certificates);
00703     d->certificatesLoaded = true;
00704 }
00705 
00706 
00707 void KTcpSocket::setCiphers(const QList<KSslCipher> &ciphers)
00708 {
00709     QList<QSslCipher> cl;
00710     foreach (const KSslCipher &c, d->ciphers) {
00711         cl.append(d->ccc.converted(c));
00712     }
00713     d->sock.setCiphers(cl);
00714 }
00715 
00716 
00717 void KTcpSocket::setLocalCertificate(const QSslCertificate &certificate)
00718 {
00719     d->sock.setLocalCertificate(certificate);
00720 }
00721 
00722 
00723 void KTcpSocket::setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format)
00724 {
00725     d->sock.setLocalCertificate(fileName, format);
00726 }
00727 
00728 
00729 //TODO
00730 void KTcpSocket::setPrivateKey(const KSslKey &key)
00731 {
00732 }
00733 
00734 
00735 //TODO
00736 void KTcpSocket::setPrivateKey(const QString &fileName, KSslKey::Algorithm algorithm,
00737                                QSsl::EncodingFormat format, const QByteArray &passPhrase)
00738 {
00739 }
00740 
00741 
00742 bool KTcpSocket::waitForEncrypted(int msecs)
00743 {
00744     return d->sock.waitForEncrypted(msecs);
00745 }
00746 
00747 
00748 KTcpSocket::EncryptionMode KTcpSocket::encryptionMode() const
00749 {
00750     return d->encryptionMode(d->sock.mode());
00751 }
00752 
00753 
00754 //slot
00755 void KTcpSocket::ignoreSslErrors()
00756 {
00757     d->sock.ignoreSslErrors();
00758 }
00759 
00760 
00761 //slot
00762 void KTcpSocket::startClientEncryption()
00763 {
00764     d->maybeLoadCertificates();
00765     d->sock.setProtocol(qSslProtocolFromK(d->advertisedSslVersion));
00766     d->sock.startClientEncryption();
00767 }
00768 
00769 
00770 //debugging H4X
00771 void KTcpSocket::showSslErrors()
00772 {
00773     foreach (const QSslError &e, d->sock.sslErrors())
00774         kDebug(7029) << e.errorString();
00775 }
00776 
00777 
00778 void KTcpSocket::setAdvertisedSslVersion(KTcpSocket::SslVersion version)
00779 {
00780     d->advertisedSslVersion = version;
00781 }
00782 
00783 
00784 KTcpSocket::SslVersion KTcpSocket::advertisedSslVersion() const
00785 {
00786     return d->advertisedSslVersion;
00787 }
00788 
00789 
00790 KTcpSocket::SslVersion KTcpSocket::negotiatedSslVersion() const
00791 {
00792     if (!d->sock.isEncrypted()) {
00793         return UnknownSslVersion;
00794     }
00795     return kSslVersionFromQ(d->sock.protocol());
00796 }
00797 
00798 
00799 QString KTcpSocket::negotiatedSslVersionName() const
00800 {
00801     if (!d->sock.isEncrypted()) {
00802         return QString();
00803     }
00804     return d->sock.sessionCipher().protocolString();
00805 }
00806 
00807 
00809 
00810 class KSslKeyPrivate
00811 {
00812 public:
00813     KSslKey::Algorithm convertAlgorithm(QSsl::KeyAlgorithm a)
00814     {
00815         switch(a) {
00816         case QSsl::Dsa:
00817             return KSslKey::Dsa;
00818         default:
00819             return KSslKey::Rsa;
00820         }
00821     }
00822 
00823     KSslKey::Algorithm algorithm;
00824     KSslKey::KeySecrecy secrecy;
00825     bool isExportable;
00826     QByteArray der;
00827 };
00828 
00829 
00830 KSslKey::KSslKey()
00831  : d(new KSslKeyPrivate)
00832 {
00833     d->algorithm = Rsa;
00834     d->secrecy = PublicKey;
00835     d->isExportable = true;
00836 }
00837 
00838 
00839 KSslKey::KSslKey(const KSslKey &other)
00840  : d(new KSslKeyPrivate)
00841 {
00842     *d = *other.d;
00843 }
00844 
00845 
00846 KSslKey::KSslKey(const QSslKey &qsk)
00847  : d(new KSslKeyPrivate)
00848 {
00849     d->algorithm = d->convertAlgorithm(qsk.algorithm());
00850     d->secrecy = (qsk.type() == QSsl::PrivateKey) ? PrivateKey : PublicKey;
00851     d->isExportable = true;
00852     d->der = qsk.toDer();
00853 }
00854 
00855 
00856 KSslKey::~KSslKey()
00857 {
00858     delete d;
00859 }
00860 
00861 
00862 KSslKey &KSslKey::operator=(const KSslKey &other)
00863 {
00864     *d = *other.d;
00865     return *this;
00866 }
00867 
00868 
00869 KSslKey::Algorithm KSslKey::algorithm() const
00870 {
00871     return d->algorithm;
00872 }
00873 
00874 
00875 bool KSslKey::isExportable() const
00876 {
00877     return d->isExportable;
00878 }
00879 
00880 
00881 KSslKey::KeySecrecy KSslKey::secrecy() const
00882 {
00883     return d->secrecy;
00884 }
00885 
00886 
00887 QByteArray KSslKey::toDer() const
00888 {
00889     return d->der;
00890 }
00891 
00893 
00894 //nice-to-have: make implicitly shared
00895 class KSslCipherPrivate
00896 {
00897 public:
00898 
00899     QString authenticationMethod;
00900     QString encryptionMethod;
00901     QString keyExchangeMethod;
00902     QString name;
00903     bool isNull;
00904     int supportedBits;
00905     int usedBits;
00906 };
00907 
00908 
00909 KSslCipher::KSslCipher()
00910  : d(new KSslCipherPrivate)
00911 {
00912     d->isNull = true;
00913     d->supportedBits = 0;
00914     d->usedBits = 0;
00915 }
00916 
00917 
00918 KSslCipher::KSslCipher(const KSslCipher &other)
00919  : d(new KSslCipherPrivate)
00920 {
00921     *d = *other.d;
00922 }
00923 
00924 
00925 KSslCipher::KSslCipher(const QSslCipher &qsc)
00926  : d(new KSslCipherPrivate)
00927 {
00928     d->authenticationMethod = qsc.authenticationMethod();
00929     d->encryptionMethod = qsc.encryptionMethod();
00930     //Qt likes to append the number of bits (usedBits?) to the algorithm,
00931     //for example "AES(256)". We only want the pure algorithm name, though.
00932     int parenIdx = d->encryptionMethod.indexOf('(');
00933     if (parenIdx > 0)
00934         d->encryptionMethod.truncate(parenIdx);
00935     d->keyExchangeMethod = qsc.keyExchangeMethod();
00936     d->name = qsc.name();
00937     d->isNull = qsc.isNull();
00938     d->supportedBits = qsc.supportedBits();
00939     d->usedBits = qsc.usedBits();
00940 }
00941 
00942 
00943 KSslCipher::~KSslCipher()
00944 {
00945     delete d;
00946 }
00947 
00948 
00949 KSslCipher &KSslCipher::operator=(const KSslCipher &other)
00950 {
00951     *d = *other.d;
00952     return *this;
00953 }
00954 
00955 
00956 bool KSslCipher::isNull() const
00957 {
00958     return d->isNull;
00959 }
00960 
00961 
00962 QString KSslCipher::authenticationMethod() const
00963 {
00964     return d->authenticationMethod;
00965 }
00966 
00967 
00968 QString KSslCipher::encryptionMethod() const
00969 {
00970     return d->encryptionMethod;
00971 }
00972 
00973 
00974 QString KSslCipher::keyExchangeMethod() const
00975 {
00976     return d->keyExchangeMethod;
00977 }
00978 
00979 
00980 QString KSslCipher::digestMethod() const
00981 {
00982     //### This is not really backend neutral. It works for OpenSSL and
00983     //    for RFC compliant names, though.
00984     if (d->name.endsWith("SHA"))
00985         return "SHA-1";
00986     else if (d->name.endsWith("MD5"))
00987         return "MD5";
00988     else
00989         return "";
00990 }
00991 
00992 
00993 QString KSslCipher::name() const
00994 {
00995     return d->name;
00996 }
00997 
00998 
00999 int KSslCipher::supportedBits() const
01000 {
01001     return d->supportedBits;
01002 }
01003 
01004 
01005 int KSslCipher::usedBits() const
01006 {
01007     return d->usedBits;
01008 }
01009 
01010 
01011 //static
01012 QList<KSslCipher> KSslCipher::supportedCiphers()
01013 {
01014     QList<KSslCipher> ret;
01015     QList<QSslCipher> candidates = QSslSocket::supportedCiphers();
01016     foreach(const QSslCipher &c, candidates) {
01017         ret.append(KSslCipher(c));
01018     }
01019     return ret;
01020 }
01021 
01022 
01023 KSslErrorUiData::KSslErrorUiData()
01024  : d(new Private())
01025 {
01026     d->usedBits = 0;
01027     d->bits = 0;
01028 }
01029 
01030 
01031 KSslErrorUiData::KSslErrorUiData(const KTcpSocket *socket)
01032  : d(new Private())
01033 {
01034     d->certificateChain = socket->peerCertificateChain();
01035     d->sslErrors = socket->sslErrors();
01036     d->ip = socket->peerAddress().toString();
01037     d->host = socket->peerName();
01038     d->sslProtocol = socket->negotiatedSslVersionName();
01039     d->cipher = socket->sessionCipher().name();
01040     d->usedBits = socket->sessionCipher().usedBits();
01041     d->bits = socket->sessionCipher().supportedBits();
01042 }
01043 
01044 
01045 KSslErrorUiData::KSslErrorUiData(const KSslErrorUiData &other)
01046  : d(new Private(*other.d))
01047 {}
01048 
01049 
01050 KSslErrorUiData &KSslErrorUiData::operator=(const KSslErrorUiData &other)
01051 {
01052     *d = *other.d;
01053     return *this;
01054 }
01055 
01056 
01057 #include "ktcpsocket.moc"

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal