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