• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.4 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • network
ktcpsocket.h
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
3  Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef KTCPSOCKET_H
22 #define KTCPSOCKET_H
23 
24 #include <QtNetwork/QSslSocket>
25 #include <QtNetwork/QSslConfiguration>
26 
27 #include "kdecore_export.h"
28 
29 /*
30  Notes on QCA::TLS compatibility
31  In order to check for all validation problems as far as possible we need to use:
32  Validity QCA::TLS::peerCertificateValidity()
33  TLS::IdentityResult QCA::TLS::peerIdentityResult()
34  CertificateChain QCA::TLS::peerCertificateChain().validate() - to find the failing cert!
35  TLS::Error QCA::TLS::errorCode() - for more generic (but stil SSL) errors
36  */
37 
38 
39 class KSslKeyPrivate;
40 
41 class KDECORE_EXPORT KSslKey {
42 public:
43  enum Algorithm {
44  Rsa = 0,
45  Dsa,
46  Dh
47  };
48  enum KeySecrecy {
49  PublicKey,
50  PrivateKey
51  };
52 
53  KSslKey();
54  KSslKey(const KSslKey &other);
55  KSslKey(const QSslKey &sslKey);
56  ~KSslKey();
57  KSslKey &operator=(const KSslKey &other);
58 
59  Algorithm algorithm() const;
60  bool isExportable() const;
61  KeySecrecy secrecy() const;
62  QByteArray toDer() const;
63 private:
64  KSslKeyPrivate *const d;
65 };
66 
67 
68 class KSslCipherPrivate;
69 
70 class KDECORE_EXPORT KSslCipher {
71 public:
72  KSslCipher();
73  KSslCipher(const KSslCipher &other);
74  KSslCipher(const QSslCipher &);
75  ~KSslCipher();
76  KSslCipher &operator=(const KSslCipher &other);
77 
78  bool isNull() const;
79  QString authenticationMethod() const;
80  QString encryptionMethod() const;
81  QString keyExchangeMethod() const;
82  QString digestMethod() const;
83  /* mainly for internal use */
84  QString name() const;
85  int supportedBits() const;
86  int usedBits() const;
87 
88  static QList<KSslCipher> supportedCiphers();
89 
90 private:
91  KSslCipherPrivate *const d;
92 };
93 
94 
95 class KSslErrorPrivate;
96 class KTcpSocket;
97 
98 class KDECORE_EXPORT KSslError
99 {
100 public:
101  enum Error {
102  NoError = 0,
103  UnknownError,
104  InvalidCertificateAuthorityCertificate,
105  InvalidCertificate,
106  CertificateSignatureFailed,
107  SelfSignedCertificate,
108  ExpiredCertificate,
109  RevokedCertificate,
110  InvalidCertificatePurpose,
111  RejectedCertificate,
112  UntrustedCertificate,
113  NoPeerCertificate,
114  HostNameMismatch,
115  PathLengthExceeded
116  };
117  KSslError(KSslError::Error error = NoError, const QSslCertificate &cert = QSslCertificate());
118  KSslError(const QSslError &error); //### explicit yes or no?
119  KSslError(const KSslError &other);
120  ~KSslError();
121  KSslError &operator=(const KSslError &other);
122 
123  Error error() const;
124  QString errorString() const;
125  QSslCertificate certificate() const;
126 private:
127  KSslErrorPrivate *const d;
128 };
129 
130 
131 //consider killing more convenience functions with huge signatures
132 //### do we need setSession() / session() ?
133 
134 //BIG FAT TODO: do we keep openMode() up to date everywhere it can change?
135 
136 //other TODO: limit possible error strings?, SSL key stuff
137 
138 //TODO protocol (or maybe even application?) dependent automatic proxy choice
139 
140 class KTcpSocketPrivate;
141 class QHostAddress;
142 class KUrl;
143 
144 class KDECORE_EXPORT KTcpSocket: public QIODevice
145 {
146  Q_OBJECT
147 public:
148  enum State {
149  UnconnectedState = 0,
150  HostLookupState,
151  ConnectingState,
152  ConnectedState,
153  BoundState,
154  ListeningState,
155  ClosingState
156  //hmmm, do we need an SslNegotiatingState?
157  };
158  enum SslVersion {
159  UnknownSslVersion = 0x01,
160  SslV2 = 0x02,
161  SslV3 = 0x04,
162  TlsV1 = 0x08,
163  SslV3_1 = 0x08,
164  TlsV1SslV3 = 0x10,
165  SecureProtocols = 0x20,
166  AnySslVersion = SslV2 | SslV3 | TlsV1
167  };
168  Q_DECLARE_FLAGS(SslVersions, SslVersion)
169  enum Error {
170  UnknownError = 0,
171  ConnectionRefusedError,
172  RemoteHostClosedError,
173  HostNotFoundError,
174  SocketAccessError,
175  SocketResourceError,
176  SocketTimeoutError,
177  NetworkError,
178  UnsupportedSocketOperationError
179  };
180 /*
181 The following is based on reading the OpenSSL interface code of both QSslSocket
182 and QCA::TLS. Barring oversights it should be accurate. The two cases with the
183 question marks apparently will never be emitted by QSslSocket so there is nothing
184 to compare.
185 
186 QSslError::NoError KTcpSocket::NoError
187 QSslError::UnableToGetIssuerCertificate QCA::ErrorSignatureFailed
188 QSslError::UnableToDecryptCertificateSignature QCA::ErrorSignatureFailed
189 QSslError::UnableToDecodeIssuerPublicKey QCA::ErrorInvalidCA
190 QSslError::CertificateSignatureFailed QCA::ErrorSignatureFailed
191 QSslError::CertificateNotYetValid QCA::ErrorExpired
192 QSslError::CertificateExpired QCA::ErrorExpired
193 QSslError::InvalidNotBeforeField QCA::ErrorExpired
194 QSslError::InvalidNotAfterField QCA::ErrorExpired
195 QSslError::SelfSignedCertificate QCA::ErrorSelfSigned
196 QSslError::SelfSignedCertificateInChain QCA::ErrorSelfSigned
197 QSslError::UnableToGetLocalIssuerCertificate QCA::ErrorInvalidCA
198 QSslError::UnableToVerifyFirstCertificate QCA::ErrorSignatureFailed
199 QSslError::CertificateRevoked QCA::ErrorRevoked
200 QSslError::InvalidCaCertificate QCA::ErrorInvalidCA
201 QSslError::PathLengthExceeded QCA::ErrorPathLengthExceeded
202 QSslError::InvalidPurpose QCA::ErrorInvalidPurpose
203 QSslError::CertificateUntrusted QCA::ErrorUntrusted
204 QSslError::CertificateRejected QCA::ErrorRejected
205 QSslError::SubjectIssuerMismatch QCA::TLS::InvalidCertificate ?
206 QSslError::AuthorityIssuerSerialNumberMismatch QCA::TLS::InvalidCertificate ?
207 QSslError::NoPeerCertificate QCA::TLS::NoCertificate
208 QSslError::HostNameMismatch QCA::TLS::HostMismatch
209 QSslError::UnspecifiedError KTcpSocket::UnknownError
210 QSslError::NoSslSupport Never happens :)
211  */
212  enum EncryptionMode {
213  UnencryptedMode = 0,
214  SslClientMode,
215  SslServerMode //### not implemented
216  };
217  enum ProxyPolicy {
219  AutoProxy = 0,
221  ManualProxy
222  };
223 
224  KTcpSocket(QObject *parent = 0);
225  ~KTcpSocket();
226 
227  //from QIODevice
228  //reimplemented virtuals - the ones not reimplemented are OK for us
229  virtual bool atEnd() const;
230  virtual qint64 bytesAvailable() const;
231  virtual qint64 bytesToWrite() const;
232  virtual bool canReadLine() const;
233  virtual void close();
234  virtual bool isSequential() const;
235  virtual bool open(QIODevice::OpenMode open);
236  virtual bool waitForBytesWritten(int msecs);
237  //### Document that this actually tries to read *more* data
238  virtual bool waitForReadyRead(int msecs = 30000);
239 protected:
240  virtual qint64 readData (char *data, qint64 maxSize);
241  virtual qint64 writeData (const char *data, qint64 maxSize);
242 signals:
245  void encryptedBytesWritten( qint64 written );
246 public:
247  //from QAbstractSocket
248  void abort();
249  void connectToHost(const QString &hostName, quint16 port, ProxyPolicy policy = AutoProxy);
250  void connectToHost(const QHostAddress &hostAddress, quint16 port, ProxyPolicy policy = AutoProxy);
251 
258  void connectToHost(const KUrl &url, ProxyPolicy policy = AutoProxy);
259  void disconnectFromHost();
260  Error error() const; //### QAbstractSocket's model is strange. error() should be related to the
261  //current state and *NOT* just report the last error if there was one.
262  QList<KSslError> sslErrors() const; //### the errors returned can only have a subset of all
263  //possible QSslError::SslError enum values depending on backend
264  bool flush();
265  bool isValid() const;
266  QHostAddress localAddress() const;
267  QHostAddress peerAddress() const;
268  QString peerName() const;
269  quint16 peerPort() const;
270  void setVerificationPeerName(const QString& hostName);
271 
272 #ifndef QT_NO_NETWORKPROXY
273 
276  QNetworkProxy proxy() const;
277 #endif
278  qint64 readBufferSize() const; //probably hard to implement correctly
279 
280 #ifndef QT_NO_NETWORKPROXY
281 
284  void setProxy(const QNetworkProxy &proxy); //people actually seem to need it
285 #endif
286  void setReadBufferSize(qint64 size);
287  State state() const;
288  bool waitForConnected(int msecs = 30000);
289  bool waitForDisconnected(int msecs = 30000);
290 
291  //from QSslSocket
292  void addCaCertificate(const QSslCertificate &certificate);
293 // bool addCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
294 // QRegExp::PatternSyntax syntax = QRegExp::FixedString);
295  void addCaCertificates(const QList<QSslCertificate> &certificates);
296  QList<QSslCertificate> caCertificates() const;
297  QList<KSslCipher> ciphers() const;
298  void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode openMode = ReadWrite);
299  // bool isEncrypted() const { return encryptionMode() != UnencryptedMode }
300  QSslCertificate localCertificate() const;
301  QList<QSslCertificate> peerCertificateChain() const;
302  KSslKey privateKey() const;
303  KSslCipher sessionCipher() const;
304  void setCaCertificates(const QList<QSslCertificate> &certificates);
305  void setCiphers(const QList<KSslCipher> &ciphers);
306  //### void setCiphers(const QString &ciphers); //what about i18n?
307  void setLocalCertificate(const QSslCertificate &certificate);
308  void setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format = QSsl::Pem);
309  void setPrivateKey(const KSslKey &key);
310  void setPrivateKey(const QString &fileName, KSslKey::Algorithm algorithm = KSslKey::Rsa,
311  QSsl::EncodingFormat format = QSsl::Pem,
312  const QByteArray &passPhrase = QByteArray());
313  void setAdvertisedSslVersion(SslVersion version);
314  SslVersion advertisedSslVersion() const; //always equal to last setSslAdvertisedVersion
315  SslVersion negotiatedSslVersion() const; //negotiated version; downgrades are possible.
316  QString negotiatedSslVersionName() const;
317  bool waitForEncrypted(int msecs = 30000);
318 
319  EncryptionMode encryptionMode() const;
320 
328  QVariant socketOption(QAbstractSocket::SocketOption options) const;
329 
337  void setSocketOption(QAbstractSocket::SocketOption options, const QVariant &value);
338 
344  QSslConfiguration sslConfiguration() const;
345 
351  void setSslConfiguration(const QSslConfiguration& configuration);
352 
353 Q_SIGNALS:
354  //from QAbstractSocket
355  void connected();
356  void disconnected();
357  void error(KTcpSocket::Error);
358  void hostFound();
359 #ifndef QT_NO_NETWORKPROXY
360  void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
361 #endif
362  // only for raw socket state, SSL is separate
363  void stateChanged(KTcpSocket::State);
364 
365  //from QSslSocket
366  void encrypted();
367  void encryptionModeChanged(EncryptionMode);
368  void sslErrors(const QList<KSslError> &errors);
369 
370 public Q_SLOTS:
371  void ignoreSslErrors();
372  void startClientEncryption();
373  // void startServerEncryption(); //not implemented
374 private:
375  Q_PRIVATE_SLOT(d, void reemitReadyRead())
376  Q_PRIVATE_SLOT(d, void reemitSocketError(QAbstractSocket::SocketError))
377  Q_PRIVATE_SLOT(d, void reemitSslErrors(const QList<QSslError> &))
378  Q_PRIVATE_SLOT(d, void reemitStateChanged(QAbstractSocket::SocketState))
379  Q_PRIVATE_SLOT(d, void reemitModeChanged(QSslSocket::SslMode))
380 
381 //debugging H4X
382  void showSslErrors();
383 
384  friend class KTcpSocketPrivate;
385  KTcpSocketPrivate *const d;
386 };
387 
388 
396 class KDECORE_EXPORT KSslErrorUiData
397 {
398 public:
402  KSslErrorUiData();
406  KSslErrorUiData(const KTcpSocket *socket);
410  KSslErrorUiData(const QSslSocket *socket);
411  KSslErrorUiData(const KSslErrorUiData &other);
412  KSslErrorUiData &operator=(const KSslErrorUiData &);
417  ~KSslErrorUiData();
418  class Private;
419 private:
420  friend class Private;
421  Private *const d;
422 };
423 
424 
425 #endif // KTCPSOCKET_H
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 12:05:04 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.10.4 API Reference

Skip menu "kdelibs-4.10.4 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal