• Skip to content
  • Skip to link menu
KDE 4.2 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 
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::SslV3 | 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     static 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::InvalidCertificateAuthorityCertificate;
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     static QString errorString(KSslError::Error e)                      
00172     {                                                                   
00173         switch (e) {                                                    
00174         case KSslError::NoError:                                        
00175             return "No error";                                          
00176         case KSslError::InvalidCertificateAuthorityCertificate:         
00177             return "The certificate authority's certificate is invalid";
00178         case KSslError::ExpiredCertificate:                             
00179             return "The certificate has expired";                       
00180         case KSslError::InvalidCertificate:                             
00181             return "The certificate is invalid";                        
00182         case KSslError::SelfSignedCertificate:                          
00183             return "The certificate is not signed by any trusted certificate authority";
00184         case KSslError::RevokedCertificate:                                             
00185             return "The certificate has been revoked";                                  
00186         case KSslError::InvalidCertificatePurpose:                                      
00187             return "The certificate is unsuitable for this purpose";                    
00188         case KSslError::UntrustedCertificate:                                           
00189             return "The root certificate authority's certificate is not trusted for this purpose";
00190         case KSslError::RejectedCertificate:                                                      
00191             return "The certificate authority's certificate is marked to reject this certificate's purpose";
00192         case KSslError::NoPeerCertificate:                                                                  
00193             return "The peer did not present any certificate";                                              
00194         case KSslError::HostNameMismatch:                                                                   
00195             return "The certificate does not apply to the given host";                                      
00196         case KSslError::CertificateSignatureFailed:                                                         
00197             return "The certificate cannot be verified for internal reasons";                               
00198         case KSslError::PathLengthExceeded:                                                                 
00199             return "The certificate chain is too long";                                                     
00200         case KSslError::UnknownError:                                                                       
00201         default:                                                                                            
00202             return "Unknown error";                                                                         
00203         }                                                                                                   
00204     }                                                                                                       
00205              
00206     KSslError::Error error;
00207     QSslCertificate certificate;
00208 };
00209 
00210 
00211 KSslError::KSslError(Error errorCode, const QSslCertificate &certificate)
00212  : d(new KSslErrorPrivate())
00213 {
00214     d->error = errorCode;
00215     d->certificate = certificate;
00216 }
00217 
00218 
00219 KSslError::KSslError(const QSslError &other)
00220  : d(new KSslErrorPrivate())
00221 {
00222     d->error = KSslErrorPrivate::errorFromQSslError(other.error());  
00223     d->certificate = other.certificate();
00224 }
00225 
00226 
00227 KSslError::KSslError(const KSslError &other)
00228  : d(new KSslErrorPrivate())
00229 {
00230     *d = *other.d;
00231 }
00232 
00233 
00234 KSslError::~KSslError()
00235 {
00236     delete d;
00237 }
00238 
00239 
00240 KSslError &KSslError::operator=(const KSslError &other)
00241 {
00242     *d = *other.d;
00243     return *this;
00244 }
00245 
00246 
00247 KSslError::Error KSslError::error() const
00248 {
00249     return d->error;
00250 }
00251 
00252 
00253 QString KSslError::errorString() const
00254 {
00255     return KSslErrorPrivate::errorString(d->error); 
00256 }
00257 
00258 
00259 QSslCertificate KSslError::certificate() const
00260 {
00261     return d->certificate;
00262 }
00263 
00264 
00265 class KTcpSocketPrivate
00266 {
00267 public:
00268     KTcpSocketPrivate(KTcpSocket *qq)
00269      : q(qq),
00270        emittedReadyRead(false)
00271     {
00272         initKSslSocket();
00273 
00274         Q_ASSERT(kdeCaCertificateList);
00275         sock.setCaCertificates(*kdeCaCertificateList);
00276     }
00277 
00278     KTcpSocket::State state(QAbstractSocket::SocketState s)
00279     {
00280         switch (s) {
00281         case QAbstractSocket::UnconnectedState:
00282             return KTcpSocket::UnconnectedState;
00283         case QAbstractSocket::HostLookupState:
00284             return KTcpSocket::HostLookupState;
00285         case QAbstractSocket::ConnectingState:
00286             return KTcpSocket::ConnectingState;
00287         case QAbstractSocket::ConnectedState:
00288             return KTcpSocket::ConnectedState;
00289         case QAbstractSocket::ClosingState:
00290             return KTcpSocket::ClosingState;
00291         case QAbstractSocket::BoundState:
00292         case QAbstractSocket::ListeningState:
00293             //### these two are not relevant as long as this can't be a server socket
00294         default:
00295             return KTcpSocket::UnconnectedState; //the closest to "error"
00296         }
00297     }
00298 
00299     KTcpSocket::EncryptionMode encryptionMode(QSslSocket::SslMode mode)
00300     {
00301         switch (mode) {
00302         case QSslSocket::SslClientMode:
00303             return KTcpSocket::SslClientMode;
00304         case QSslSocket::SslServerMode:
00305             return KTcpSocket::SslServerMode;
00306         default:
00307             return KTcpSocket::UnencryptedMode;
00308         }
00309     }
00310 
00311     KTcpSocket::Error errorFromAbsSocket(QAbstractSocket::SocketError e)
00312     {
00313         switch (e) {
00314         case QAbstractSocket::ConnectionRefusedError:
00315             return KTcpSocket::ConnectionRefusedError;
00316         case QAbstractSocket::RemoteHostClosedError:
00317             return KTcpSocket::RemoteHostClosedError;
00318         case QAbstractSocket::HostNotFoundError:
00319             return KTcpSocket::HostNotFoundError;
00320         case QAbstractSocket::SocketAccessError:
00321             return KTcpSocket::SocketAccessError;
00322         case QAbstractSocket::SocketResourceError:
00323             return KTcpSocket::SocketResourceError;
00324         case QAbstractSocket::SocketTimeoutError:
00325             return KTcpSocket::SocketTimeoutError;
00326         case QAbstractSocket::NetworkError:
00327             return KTcpSocket::NetworkError;
00328         case QAbstractSocket::UnsupportedSocketOperationError:
00329             return KTcpSocket::UnsupportedSocketOperationError;
00330         case QAbstractSocket::DatagramTooLargeError:
00331             //we don't do UDP
00332         case QAbstractSocket::AddressInUseError:
00333         case QAbstractSocket::SocketAddressNotAvailableError:
00334             //### own values if/when we ever get server socket support
00335         case QAbstractSocket::ProxyAuthenticationRequiredError:
00336             //### maybe we need an enum value for this
00337         case QAbstractSocket::UnknownSocketError:
00338         default:
00339             return KTcpSocket::UnknownError;
00340         }
00341     }
00342 
00343     //private slots
00344     void reemitSocketError(QAbstractSocket::SocketError e)
00345     {
00346         emit q->error(errorFromAbsSocket(e));
00347     }
00348 
00349     void reemitSslErrors(const QList<QSslError> &errors)
00350     {
00351         q->showSslErrors(); //H4X
00352         QList<KSslError> kErrors;
00353         foreach (const QSslError &e, errors) {
00354             kErrors.append(KSslError(e));
00355         }
00356         emit q->sslErrors(kErrors);
00357     }
00358 
00359     void reemitStateChanged(QAbstractSocket::SocketState s)
00360     {
00361         emit q->stateChanged(state(s));
00362     }
00363 
00364     void reemitModeChanged(QSslSocket::SslMode m)
00365     {
00366         emit q->encryptionModeChanged(encryptionMode(m));
00367     }
00368 
00369     // This method is needed because we might emit readyRead() due to this QIODevice
00370     // having some data buffered, so we need to care about blocking, too.
00371     //### useless ATM as readyRead() now just calls d->sock.readyRead().
00372     void reemitReadyRead()
00373     {
00374         if (!emittedReadyRead) {
00375             emittedReadyRead = true;
00376             emit q->readyRead();
00377             emittedReadyRead = false;
00378         }
00379     }
00380 
00381     KTcpSocket *const q;
00382     bool emittedReadyRead;
00383     QSslSocket sock;
00384     QList<KSslCipher> ciphers;
00385     KTcpSocket::SslVersion advertisedSslVersion;
00386     CipherCc ccc;
00387 };
00388 
00389 
00390 KTcpSocket::KTcpSocket(QObject *parent)
00391  : QIODevice(parent),
00392    d(new KTcpSocketPrivate(this))
00393 {
00394     d->advertisedSslVersion = SslV3;
00395 
00396     connect(&d->sock, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose()));
00397     connect(&d->sock, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64)));
00398     connect(&d->sock, SIGNAL(readyRead()), this, SLOT(reemitReadyRead()));
00399     connect(&d->sock, SIGNAL(connected()), this, SIGNAL(connected()));
00400     connect(&d->sock, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
00401     connect(&d->sock, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)),
00402             this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
00403     connect(&d->sock, SIGNAL(error(QAbstractSocket::SocketError)),
00404             this, SLOT(reemitSocketError(QAbstractSocket::SocketError)));
00405     connect(&d->sock, SIGNAL(sslErrors(const QList<QSslError> &)),
00406             this, SLOT(reemitSslErrors(const QList<QSslError> &)));
00407     connect(&d->sock, SIGNAL(hostFound()), this, SIGNAL(hostFound()));
00408     connect(&d->sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
00409             this, SLOT(reemitStateChanged(QAbstractSocket::SocketState)));
00410     connect(&d->sock, SIGNAL(modeChanged(QSslSocket::SslMode)),
00411             this, SLOT(reemitModeChanged(QSslSocket::SslMode)));
00412 }
00413 
00414 
00415 KTcpSocket::~KTcpSocket()
00416 {
00417     delete d;
00418 }
00419 
00421 
00422 bool KTcpSocket::atEnd() const
00423 {
00424     return d->sock.atEnd() && QIODevice::atEnd();
00425 }
00426 
00427 
00428 qint64 KTcpSocket::bytesAvailable() const
00429 {
00430     return d->sock.bytesAvailable() + QIODevice::bytesAvailable();
00431 }
00432 
00433 
00434 qint64 KTcpSocket::bytesToWrite() const
00435 {
00436     return d->sock.bytesToWrite();
00437 }
00438 
00439 
00440 bool KTcpSocket::canReadLine() const
00441 {
00442     return d->sock.canReadLine() || QIODevice::canReadLine();
00443 }
00444 
00445 
00446 void KTcpSocket::close()
00447 {
00448     d->sock.close();
00449     QIODevice::close();
00450 }
00451 
00452 
00453 bool KTcpSocket::isSequential() const
00454 {
00455     return true;
00456 }
00457 
00458 
00459 bool KTcpSocket::open(QIODevice::OpenMode open)
00460 {
00461     bool ret = d->sock.open(open);
00462     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00463     return ret;
00464 }
00465 
00466 
00467 bool KTcpSocket::waitForBytesWritten(int msecs)
00468 {
00469     return d->sock.waitForBytesWritten(msecs);
00470 }
00471 
00472 
00473 bool KTcpSocket::waitForReadyRead(int msecs)
00474 {
00475     return d->sock.waitForReadyRead(msecs);
00476 }
00477 
00478 
00479 qint64 KTcpSocket::readData(char *data, qint64 maxSize)
00480 {
00481     return d->sock.read(data, maxSize);
00482 }
00483 
00484 
00485 qint64 KTcpSocket::writeData(const char *data, qint64 maxSize)
00486 {
00487     return d->sock.write(data, maxSize);
00488 }
00489 
00491 
00492 void KTcpSocket::abort()
00493 {
00494     d->sock.abort();
00495 }
00496 
00497 
00498 void KTcpSocket::connectToHost(const QString &hostName, quint16 port, ProxyPolicy policy)
00499 {
00500     if (policy == AutoProxy) {
00501         //###
00502     }
00503     d->sock.connectToHost(hostName, port);
00504     // there are enough layers of buffers between us and the network, and there is a quirk
00505     // in QIODevice that can make it try to readData() twice per read() call if buffered and
00506     // reaData() does not deliver enough data the first time. like when the other side is
00507     // simply not sending any more data...
00508     // this can *apparently* lead to long delays sometimes which stalls applications.
00509     // do not want.
00510     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00511 }
00512 
00513 
00514 void KTcpSocket::connectToHost(const QHostAddress &hostAddress, quint16 port, ProxyPolicy policy)
00515 {
00516     if (policy == AutoProxy) {
00517         //###
00518     }
00519     d->sock.connectToHost(hostAddress, port);
00520     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00521 }
00522 
00523 
00524 void KTcpSocket::connectToHost(const KUrl &url, ProxyPolicy policy)
00525 {
00526     if (policy == AutoProxy) {
00527         //###
00528     }
00529     d->sock.connectToHost(url.host(), url.port());
00530     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00531 }
00532 
00533 
00534 void KTcpSocket::disconnectFromHost()
00535 {
00536     d->sock.disconnectFromHost();
00537     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00538 }
00539 
00540 
00541 KTcpSocket::Error KTcpSocket::error() const
00542 {
00543     return d->errorFromAbsSocket(d->sock.error());
00544 }
00545 
00546 
00547 QList<KSslError> KTcpSocket::sslErrors() const
00548 {
00549     //### pretty slow; also consider throwing out duplicate error codes. We may get
00550     //    duplicates even though there were none in the original list because KSslError
00551     //    has a smallest common denominator range of SSL error codes.
00552     QList<KSslError> ret;
00553     foreach (const QSslError &e, d->sock.sslErrors())
00554         ret.append(KSslError(e));
00555     return ret;
00556 }
00557 
00558 
00559 bool KTcpSocket::flush()
00560 {
00561     return d->sock.flush();
00562 }
00563 
00564 
00565 bool KTcpSocket::isValid() const
00566 {
00567     return d->sock.isValid();
00568 }
00569 
00570 
00571 QHostAddress KTcpSocket::localAddress() const
00572 {
00573     return d->sock.localAddress();
00574 }
00575 
00576 
00577 QHostAddress KTcpSocket::peerAddress() const
00578 {
00579     return d->sock.peerAddress();
00580 }
00581 
00582 
00583 QString KTcpSocket::peerName() const
00584 {
00585     return d->sock.peerName();
00586 }
00587 
00588 
00589 quint16 KTcpSocket::peerPort() const
00590 {
00591     return d->sock.peerPort();
00592 }
00593 
00594 
00595 QNetworkProxy KTcpSocket::proxy() const
00596 {
00597     return d->sock.proxy();
00598 }
00599 
00600 
00601 qint64 KTcpSocket::readBufferSize() const
00602 {
00603     return d->sock.readBufferSize();
00604 }
00605 
00606 
00607 void KTcpSocket::setProxy(const QNetworkProxy &proxy)
00608 {
00609     d->sock.setProxy(proxy);
00610 }
00611 
00612 
00613 void KTcpSocket::setReadBufferSize(qint64 size)
00614 {
00615     d->sock.setReadBufferSize(size);
00616 }
00617 
00618 
00619 KTcpSocket::State KTcpSocket::state() const
00620 {
00621     return d->state(d->sock.state());
00622 }
00623 
00624 
00625 bool KTcpSocket::waitForConnected(int msecs)
00626 {
00627     bool ret = d->sock.waitForConnected(msecs);
00628     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00629     return ret;
00630 }
00631 
00632 
00633 bool KTcpSocket::waitForDisconnected(int msecs)
00634 {
00635     bool ret = d->sock.waitForDisconnected(msecs);
00636     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00637     return ret;
00638 }
00639 
00641 
00642 void KTcpSocket::addCaCertificate(const QSslCertificate &certificate)
00643 {
00644     d->sock.addCaCertificate(certificate);
00645 }
00646 
00647 
00648 /*
00649 bool KTcpSocket::addCaCertificates(const QString &path, QSsl::EncodingFormat format,
00650                                    QRegExp::PatternSyntax syntax)
00651 {
00652     return d->sock.addCaCertificates(path, format, syntax);
00653 }
00654 */
00655 
00656 
00657 void KTcpSocket::addCaCertificates(const QList<QSslCertificate> &certificates)
00658 {
00659     d->sock.addCaCertificates(certificates);
00660 }
00661 
00662 
00663 QList<QSslCertificate> KTcpSocket::caCertificates() const
00664 {
00665     return d->sock.caCertificates();
00666 }
00667 
00668 
00669 QList<KSslCipher> KTcpSocket::ciphers() const
00670 {
00671     return d->ciphers;
00672 }
00673 
00674 
00675 void KTcpSocket::connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode openMode)
00676 {
00677     d->sock.setProtocol(qSslProtocolFromK(d->advertisedSslVersion));
00678     d->sock.connectToHostEncrypted(hostName, port, openMode);
00679     setOpenMode(d->sock.openMode() | QIODevice::Unbuffered);
00680 }
00681 
00682 
00683 QSslCertificate KTcpSocket::localCertificate() const
00684 {
00685     return d->sock.localCertificate();
00686 }
00687 
00688 
00689 QList<QSslCertificate> KTcpSocket::peerCertificateChain() const
00690 {
00691     return d->sock.peerCertificateChain();
00692 }
00693 
00694 
00695 KSslKey KTcpSocket::privateKey() const
00696 {
00697     return KSslKey(d->sock.privateKey());
00698 }
00699 
00700 
00701 KSslCipher KTcpSocket::sessionCipher() const
00702 {
00703     return KSslCipher(d->sock.sessionCipher());
00704 }
00705 
00706 
00707 void KTcpSocket::setCaCertificates(const QList<QSslCertificate> &certificates)
00708 {
00709     d->sock.setCaCertificates(certificates);
00710 }
00711 
00712 
00713 void KTcpSocket::setCiphers(const QList<KSslCipher> &ciphers)
00714 {
00715     QList<QSslCipher> cl;
00716     foreach (const KSslCipher &c, d->ciphers) {
00717         cl.append(d->ccc.converted(c));
00718     }
00719     d->sock.setCiphers(cl);
00720 }
00721 
00722 
00723 void KTcpSocket::setLocalCertificate(const QSslCertificate &certificate)
00724 {
00725     d->sock.setLocalCertificate(certificate);
00726 }
00727 
00728 
00729 void KTcpSocket::setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format)
00730 {
00731     d->sock.setLocalCertificate(fileName, format);
00732 }
00733 
00734 
00735 //TODO
00736 void KTcpSocket::setPrivateKey(const KSslKey &key)
00737 {
00738 }
00739 
00740 
00741 //TODO
00742 void KTcpSocket::setPrivateKey(const QString &fileName, KSslKey::Algorithm algorithm,
00743                                QSsl::EncodingFormat format, const QByteArray &passPhrase)
00744 {
00745 }
00746 
00747 
00748 bool KTcpSocket::waitForEncrypted(int msecs)
00749 {
00750     return d->sock.waitForEncrypted(msecs);
00751 }
00752 
00753 
00754 KTcpSocket::EncryptionMode KTcpSocket::encryptionMode() const
00755 {
00756     return d->encryptionMode(d->sock.mode());
00757 }
00758 
00759 
00760 //slot
00761 void KTcpSocket::ignoreSslErrors()
00762 {
00763     d->sock.ignoreSslErrors();
00764 }
00765 
00766 
00767 //slot
00768 void KTcpSocket::startClientEncryption()
00769 {
00770     d->sock.setProtocol(qSslProtocolFromK(d->advertisedSslVersion));
00771     d->sock.startClientEncryption();
00772 }
00773 
00774 
00775 //debugging H4X
00776 void KTcpSocket::showSslErrors()
00777 {
00778     foreach (const QSslError &e, d->sock.sslErrors())
00779         kDebug(7029) << e.errorString();
00780 }
00781 
00782 
00783 void KTcpSocket::setAdvertisedSslVersion(KTcpSocket::SslVersion version)
00784 {
00785     d->advertisedSslVersion = version;
00786 }
00787 
00788 
00789 KTcpSocket::SslVersion KTcpSocket::advertisedSslVersion() const
00790 {
00791     return d->advertisedSslVersion;
00792 }
00793 
00794 
00795 KTcpSocket::SslVersion KTcpSocket::negotiatedSslVersion() const
00796 {
00797     if (!d->sock.isEncrypted()) {
00798         return UnknownSslVersion;
00799     }
00800     return kSslVersionFromQ(d->sock.protocol());
00801 }
00802 
00803 
00804 QString KTcpSocket::negotiatedSslVersionName() const
00805 {
00806     if (!d->sock.isEncrypted()) {
00807         return QString();
00808     }
00809     return d->sock.sessionCipher().protocolString();
00810 }
00811 
00812 
00814 
00815 class KSslKeyPrivate
00816 {
00817 public:
00818     KSslKey::Algorithm convertAlgorithm(QSsl::KeyAlgorithm a)
00819     {
00820         switch(a) {
00821         case QSsl::Dsa:
00822             return KSslKey::Dsa;
00823         default:
00824             return KSslKey::Rsa;
00825         }
00826     }
00827 
00828     KSslKey::Algorithm algorithm;
00829     KSslKey::KeySecrecy secrecy;
00830     bool isExportable;
00831     QByteArray der;
00832 };
00833 
00834 
00835 KSslKey::KSslKey()
00836  : d(new KSslKeyPrivate)
00837 {
00838     d->algorithm = Rsa;
00839     d->secrecy = PublicKey;
00840     d->isExportable = true;
00841 }
00842 
00843 
00844 KSslKey::KSslKey(const KSslKey &other)
00845  : d(new KSslKeyPrivate)
00846 {
00847     *d = *other.d;
00848 }
00849 
00850 
00851 KSslKey::KSslKey(const QSslKey &qsk)
00852  : d(new KSslKeyPrivate)
00853 {
00854     d->algorithm = d->convertAlgorithm(qsk.algorithm());
00855     d->secrecy = (qsk.type() == QSsl::PrivateKey) ? PrivateKey : PublicKey;
00856     d->isExportable = true;
00857     d->der = qsk.toDer();
00858 }
00859 
00860 
00861 KSslKey::~KSslKey()
00862 {
00863     delete d;
00864 }
00865 
00866 
00867 KSslKey &KSslKey::operator=(const KSslKey &other)
00868 {
00869     *d = *other.d;
00870     return *this;
00871 }
00872 
00873 
00874 KSslKey::Algorithm KSslKey::algorithm() const
00875 {
00876     return d->algorithm;
00877 }
00878 
00879 
00880 bool KSslKey::isExportable() const
00881 {
00882     return d->isExportable;
00883 }
00884 
00885 
00886 KSslKey::KeySecrecy KSslKey::secrecy() const
00887 {
00888     return d->secrecy;
00889 }
00890 
00891 
00892 QByteArray KSslKey::toDer() const
00893 {
00894     return d->der;
00895 }
00896 
00898 
00899 //nice-to-have: make implicitly shared
00900 class KSslCipherPrivate
00901 {
00902 public:
00903 
00904     QString authenticationMethod;
00905     QString encryptionMethod;
00906     QString keyExchangeMethod;
00907     QString name;
00908     bool isNull;
00909     int supportedBits;
00910     int usedBits;
00911 };
00912 
00913 
00914 KSslCipher::KSslCipher()
00915  : d(new KSslCipherPrivate)
00916 {
00917     d->isNull = true;
00918     d->supportedBits = 0;
00919     d->usedBits = 0;
00920 }
00921 
00922 
00923 KSslCipher::KSslCipher(const KSslCipher &other)
00924  : d(new KSslCipherPrivate)
00925 {
00926     *d = *other.d;
00927 }
00928 
00929 
00930 KSslCipher::KSslCipher(const QSslCipher &qsc)
00931  : d(new KSslCipherPrivate)
00932 {
00933     d->authenticationMethod = qsc.authenticationMethod();
00934     d->encryptionMethod = qsc.encryptionMethod();
00935     //Qt likes to append the number of bits (usedBits?) to the algorithm,
00936     //for example "AES(256)". We only want the pure algorithm name, though.
00937     int parenIdx = d->encryptionMethod.indexOf('(');
00938     if (parenIdx > 0)
00939         d->encryptionMethod.truncate(parenIdx);
00940     d->keyExchangeMethod = qsc.keyExchangeMethod();
00941     d->name = qsc.name();
00942     d->isNull = qsc.isNull();
00943     d->supportedBits = qsc.supportedBits();
00944     d->usedBits = qsc.usedBits();
00945 }
00946 
00947 
00948 KSslCipher::~KSslCipher()
00949 {
00950     delete d;
00951 }
00952 
00953 
00954 KSslCipher &KSslCipher::operator=(const KSslCipher &other)
00955 {
00956     *d = *other.d;
00957     return *this;
00958 }
00959 
00960 
00961 bool KSslCipher::isNull() const
00962 {
00963     return d->isNull;
00964 }
00965 
00966 
00967 QString KSslCipher::authenticationMethod() const
00968 {
00969     return d->authenticationMethod;
00970 }
00971 
00972 
00973 QString KSslCipher::encryptionMethod() const
00974 {
00975     return d->encryptionMethod;
00976 }
00977 
00978 
00979 QString KSslCipher::keyExchangeMethod() const
00980 {
00981     return d->keyExchangeMethod;
00982 }
00983 
00984 
00985 QString KSslCipher::digestMethod() const
00986 {
00987     //### This is not really backend neutral. It works for OpenSSL and
00988     //    for RFC compliant names, though.
00989     if (d->name.endsWith("SHA"))
00990         return "SHA-1";
00991     else if (d->name.endsWith("MD5"))
00992         return "MD5";
00993     else
00994         return "";
00995 }
00996 
00997 
00998 QString KSslCipher::name() const
00999 {
01000     return d->name;
01001 }
01002 
01003 
01004 int KSslCipher::supportedBits() const
01005 {
01006     return d->supportedBits;
01007 }
01008 
01009 
01010 int KSslCipher::usedBits() const
01011 {
01012     return d->usedBits;
01013 }
01014 
01015 
01016 //static 
01017 QList<KSslCipher> KSslCipher::supportedCiphers()
01018 {
01019     QList<KSslCipher> ret;
01020     QList<QSslCipher> candidates = QSslSocket::supportedCiphers();
01021     foreach(const QSslCipher &c, candidates) {
01022         ret.append(KSslCipher(c));
01023     }
01024     return ret;
01025 }
01026 
01027 
01028 #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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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