00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef KTCPSOCKET_H
00022 #define KTCPSOCKET_H
00023
00024 #include <QtNetwork/QSslSocket>
00025
00026
00027 #include "kdecore_export.h"
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 class KSslKeyPrivate;
00040
00041 class KDECORE_EXPORT KSslKey {
00042 public:
00043 enum Algorithm {
00044 Rsa = 0,
00045 Dsa,
00046 Dh
00047 };
00048 enum KeySecrecy {
00049 PublicKey,
00050 PrivateKey
00051 };
00052
00053 KSslKey();
00054 KSslKey(const KSslKey &other);
00055 KSslKey(const QSslKey &sslKey);
00056 ~KSslKey();
00057 KSslKey &operator=(const KSslKey &other);
00058
00059 Algorithm algorithm() const;
00060 bool isExportable() const;
00061 KeySecrecy secrecy() const;
00062 QByteArray toDer() const;
00063 private:
00064 KSslKeyPrivate *const d;
00065 };
00066
00067
00068 class KSslCipherPrivate;
00069
00070 class KDECORE_EXPORT KSslCipher {
00071 public:
00072 KSslCipher();
00073 KSslCipher(const KSslCipher &other);
00074 KSslCipher(const QSslCipher &);
00075 ~KSslCipher();
00076 KSslCipher &operator=(const KSslCipher &other);
00077
00078 bool isNull() const;
00079 QString authenticationMethod() const;
00080 QString encryptionMethod() const;
00081 QString keyExchangeMethod() const;
00082 QString digestMethod() const;
00083
00084 QString name() const;
00085 int supportedBits() const;
00086 int usedBits() const;
00087
00088 static QList<KSslCipher> supportedCiphers();
00089
00090 private:
00091 KSslCipherPrivate *const d;
00092 };
00093
00094
00095 class KSslErrorPrivate;
00096 class KTcpSocket;
00097
00098 class KDECORE_EXPORT KSslError
00099 {
00100 public:
00101 enum Error {
00102 NoError = 0,
00103 UnknownError,
00104 InvalidCertificateAuthorityCertificate,
00105 InvalidCertificate,
00106 CertificateSignatureFailed,
00107 SelfSignedCertificate,
00108 ExpiredCertificate,
00109 RevokedCertificate,
00110 InvalidCertificatePurpose,
00111 RejectedCertificate,
00112 UntrustedCertificate,
00113 NoPeerCertificate,
00114 HostNameMismatch,
00115 PathLengthExceeded
00116 };
00117 KSslError(KSslError::Error error = NoError, const QSslCertificate &cert = QSslCertificate());
00118 KSslError(const QSslError &error);
00119 KSslError(const KSslError &other);
00120 ~KSslError();
00121 KSslError &operator=(const KSslError &other);
00122
00123 Error error() const;
00124 QString errorString() const;
00125 QSslCertificate certificate() const;
00126 private:
00127 KSslErrorPrivate *const d;
00128 };
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 class KTcpSocketPrivate;
00141 class QHostAddress;
00142 class KUrl;
00143
00144 class KDECORE_EXPORT KTcpSocket: public QIODevice
00145 {
00146 Q_OBJECT
00147 public:
00148 enum State {
00149 UnconnectedState = 0,
00150 HostLookupState,
00151 ConnectingState,
00152 ConnectedState,
00153 BoundState,
00154 ListeningState,
00155 ClosingState
00156
00157 };
00158 enum SslVersion {
00159 UnknownSslVersion = 0x01,
00160 SslV2 = 0x02,
00161 SslV3 = 0x04,
00162 TlsV1 = 0x08,
00163 SslV3_1 = 0x08,
00164 AnySslVersion = SslV2 | SslV3 | TlsV1
00165 };
00166 Q_DECLARE_FLAGS(SslVersions, SslVersion)
00167 enum Error {
00168 UnknownError = 0,
00169 ConnectionRefusedError,
00170 RemoteHostClosedError,
00171 HostNotFoundError,
00172 SocketAccessError,
00173 SocketResourceError,
00174 SocketTimeoutError,
00175 NetworkError,
00176 UnsupportedSocketOperationError
00177 };
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 enum EncryptionMode {
00211 UnencryptedMode = 0,
00212 SslClientMode,
00213 SslServerMode
00214 };
00215 enum ProxyPolicy {
00217 AutoProxy = 0,
00219 ManualProxy
00220 };
00221
00222 KTcpSocket(QObject *parent = 0);
00223 ~KTcpSocket();
00224
00225
00226
00227 virtual bool atEnd() const;
00228 virtual qint64 bytesAvailable() const;
00229 virtual qint64 bytesToWrite() const;
00230 virtual bool canReadLine() const;
00231 virtual void close();
00232 virtual bool isSequential() const;
00233 virtual bool open(QIODevice::OpenMode open);
00234 virtual bool waitForBytesWritten(int msecs);
00235
00236 virtual bool waitForReadyRead(int msecs = 30000);
00237 protected:
00238 virtual qint64 readData (char *data, qint64 maxSize);
00239 virtual qint64 writeData (const char *data, qint64 maxSize);
00240 public:
00241
00242 void abort();
00243 void connectToHost(const QString &hostName, quint16 port, ProxyPolicy policy = AutoProxy);
00244 void connectToHost(const QHostAddress &hostAddress, quint16 port, ProxyPolicy policy = AutoProxy);
00245
00252 void connectToHost(const KUrl &url, ProxyPolicy policy = AutoProxy);
00253 void disconnectFromHost();
00254 Error error() const;
00255
00256 QList<KSslError> sslErrors() const;
00257
00258 bool flush();
00259 bool isValid() const;
00260 QHostAddress localAddress() const;
00261 QHostAddress peerAddress() const;
00262 QString peerName() const;
00263 quint16 peerPort() const;
00264
00268 QNetworkProxy proxy() const;
00269 qint64 readBufferSize() const;
00270
00274 void setProxy(const QNetworkProxy &proxy);
00275 void setReadBufferSize(qint64 size);
00276 State state() const;
00277 bool waitForConnected(int msecs = 30000);
00278 bool waitForDisconnected(int msecs = 30000);
00279
00280
00281 void addCaCertificate(const QSslCertificate &certificate);
00282
00283
00284 void addCaCertificates(const QList<QSslCertificate> &certificates);
00285 QList<QSslCertificate> caCertificates() const;
00286 QList<KSslCipher> ciphers() const;
00287 void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode openMode = ReadWrite);
00288
00289 QSslCertificate localCertificate() const;
00290 QList<QSslCertificate> peerCertificateChain() const;
00291 KSslKey privateKey() const;
00292 KSslCipher sessionCipher() const;
00293 void setCaCertificates(const QList<QSslCertificate> &certificates);
00294 void setCiphers(const QList<KSslCipher> &ciphers);
00295
00296 void setLocalCertificate(const QSslCertificate &certificate);
00297 void setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format = QSsl::Pem);
00298 void setPrivateKey(const KSslKey &key);
00299 void setPrivateKey(const QString &fileName, KSslKey::Algorithm algorithm = KSslKey::Rsa,
00300 QSsl::EncodingFormat format = QSsl::Pem,
00301 const QByteArray &passPhrase = QByteArray());
00302 void setAdvertisedSslVersion(SslVersion version);
00303 SslVersion advertisedSslVersion() const;
00304 SslVersion negotiatedSslVersion() const;
00305 QString negotiatedSslVersionName() const;
00306 bool waitForEncrypted(int msecs = 30000);
00307
00308 EncryptionMode encryptionMode() const;
00309
00310 Q_SIGNALS:
00311
00312 void aboutToClose();
00313 void bytesWritten(qint64 bytes);
00314 void readyRead();
00315
00316
00317 void connected();
00318 void disconnected();
00319 void error(KTcpSocket::Error);
00320 void hostFound();
00321 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
00322
00323 void stateChanged(State);
00324
00325
00326
00327 void encryptionModeChanged(EncryptionMode);
00328 void sslErrors(const QList<KSslError> &errors);
00329
00330 public Q_SLOTS:
00331 void ignoreSslErrors();
00332 void startClientEncryption();
00333
00334 private:
00335 Q_PRIVATE_SLOT(d, void reemitReadyRead())
00336 Q_PRIVATE_SLOT(d, void reemitSocketError(QAbstractSocket::SocketError))
00337 Q_PRIVATE_SLOT(d, void reemitSslErrors(const QList<QSslError> &))
00338 Q_PRIVATE_SLOT(d, void reemitStateChanged(QAbstractSocket::SocketState))
00339 Q_PRIVATE_SLOT(d, void reemitModeChanged(QSslSocket::SslMode))
00340
00341
00342 void showSslErrors();
00343
00344 friend class KTcpSocketPrivate;
00345 KTcpSocketPrivate *const d;
00346 };
00347
00348
00349 #endif // KTCPSOCKET_H