1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 |
|
---|
43 | #ifndef QSSLSOCKET_H
|
---|
44 | #define QSSLSOCKET_H
|
---|
45 |
|
---|
46 | #include <QtCore/qlist.h>
|
---|
47 | #include <QtCore/qregexp.h>
|
---|
48 | #ifndef QT_NO_OPENSSL
|
---|
49 | # include <QtNetwork/qtcpsocket.h>
|
---|
50 | # include <QtNetwork/qsslerror.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | QT_BEGIN_HEADER
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | QT_MODULE(Network)
|
---|
58 |
|
---|
59 | #ifndef QT_NO_OPENSSL
|
---|
60 |
|
---|
61 | class QDir;
|
---|
62 | class QSslCipher;
|
---|
63 | class QSslCertificate;
|
---|
64 | class QSslConfiguration;
|
---|
65 |
|
---|
66 | class QSslSocketPrivate;
|
---|
67 | class Q_NETWORK_EXPORT QSslSocket : public QTcpSocket
|
---|
68 | {
|
---|
69 | Q_OBJECT
|
---|
70 | public:
|
---|
71 | enum SslMode {
|
---|
72 | UnencryptedMode,
|
---|
73 | SslClientMode,
|
---|
74 | SslServerMode
|
---|
75 | };
|
---|
76 |
|
---|
77 | enum PeerVerifyMode {
|
---|
78 | VerifyNone,
|
---|
79 | QueryPeer,
|
---|
80 | VerifyPeer,
|
---|
81 | AutoVerifyPeer
|
---|
82 | };
|
---|
83 |
|
---|
84 | QSslSocket(QObject *parent = 0);
|
---|
85 | ~QSslSocket();
|
---|
86 |
|
---|
87 | // Autostarting the SSL client handshake.
|
---|
88 | void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
|
---|
89 | void connectToHostEncrypted(const QString &hostName, quint16 port, const QString &sslPeerName, OpenMode mode = ReadWrite);
|
---|
90 | bool setSocketDescriptor(int socketDescriptor, SocketState state = ConnectedState,
|
---|
91 | OpenMode openMode = ReadWrite);
|
---|
92 |
|
---|
93 | // ### Qt 5: Make virtual
|
---|
94 | void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
|
---|
95 | QVariant socketOption(QAbstractSocket::SocketOption option);
|
---|
96 |
|
---|
97 | SslMode mode() const;
|
---|
98 | bool isEncrypted() const;
|
---|
99 |
|
---|
100 | QSsl::SslProtocol protocol() const;
|
---|
101 | void setProtocol(QSsl::SslProtocol protocol);
|
---|
102 |
|
---|
103 | QSslSocket::PeerVerifyMode peerVerifyMode() const;
|
---|
104 | void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode);
|
---|
105 |
|
---|
106 | int peerVerifyDepth() const;
|
---|
107 | void setPeerVerifyDepth(int depth);
|
---|
108 |
|
---|
109 | // From QIODevice
|
---|
110 | qint64 bytesAvailable() const;
|
---|
111 | qint64 bytesToWrite() const;
|
---|
112 | bool canReadLine() const;
|
---|
113 | void close();
|
---|
114 | bool atEnd() const;
|
---|
115 | bool flush();
|
---|
116 | void abort();
|
---|
117 |
|
---|
118 | // From QAbstractSocket:
|
---|
119 | void setReadBufferSize(qint64 size);
|
---|
120 |
|
---|
121 | // Similar to QIODevice's:
|
---|
122 | qint64 encryptedBytesAvailable() const;
|
---|
123 | qint64 encryptedBytesToWrite() const;
|
---|
124 |
|
---|
125 | // SSL configuration
|
---|
126 | QSslConfiguration sslConfiguration() const;
|
---|
127 | void setSslConfiguration(const QSslConfiguration &config);
|
---|
128 |
|
---|
129 | // Certificate & cipher accessors.
|
---|
130 | void setLocalCertificate(const QSslCertificate &certificate);
|
---|
131 | void setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format = QSsl::Pem);
|
---|
132 | QSslCertificate localCertificate() const;
|
---|
133 | QSslCertificate peerCertificate() const;
|
---|
134 | QList<QSslCertificate> peerCertificateChain() const;
|
---|
135 | QSslCipher sessionCipher() const;
|
---|
136 |
|
---|
137 | // Private keys, for server sockets.
|
---|
138 | void setPrivateKey(const QSslKey &key);
|
---|
139 | void setPrivateKey(const QString &fileName, QSsl::KeyAlgorithm algorithm = QSsl::Rsa,
|
---|
140 | QSsl::EncodingFormat format = QSsl::Pem,
|
---|
141 | const QByteArray &passPhrase = QByteArray());
|
---|
142 | QSslKey privateKey() const;
|
---|
143 |
|
---|
144 | // Cipher settings.
|
---|
145 | QList<QSslCipher> ciphers() const;
|
---|
146 | void setCiphers(const QList<QSslCipher> &ciphers);
|
---|
147 | void setCiphers(const QString &ciphers);
|
---|
148 | static void setDefaultCiphers(const QList<QSslCipher> &ciphers);
|
---|
149 | static QList<QSslCipher> defaultCiphers();
|
---|
150 | static QList<QSslCipher> supportedCiphers();
|
---|
151 |
|
---|
152 | // CA settings.
|
---|
153 | bool addCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
|
---|
154 | QRegExp::PatternSyntax syntax = QRegExp::FixedString);
|
---|
155 | void addCaCertificate(const QSslCertificate &certificate);
|
---|
156 | void addCaCertificates(const QList<QSslCertificate> &certificates);
|
---|
157 | void setCaCertificates(const QList<QSslCertificate> &certificates);
|
---|
158 | QList<QSslCertificate> caCertificates() const;
|
---|
159 | static bool addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
|
---|
160 | QRegExp::PatternSyntax syntax = QRegExp::FixedString);
|
---|
161 | static void addDefaultCaCertificate(const QSslCertificate &certificate);
|
---|
162 | static void addDefaultCaCertificates(const QList<QSslCertificate> &certificates);
|
---|
163 | static void setDefaultCaCertificates(const QList<QSslCertificate> &certificates);
|
---|
164 | static QList<QSslCertificate> defaultCaCertificates();
|
---|
165 | static QList<QSslCertificate> systemCaCertificates();
|
---|
166 |
|
---|
167 | bool waitForConnected(int msecs = 30000);
|
---|
168 | bool waitForEncrypted(int msecs = 30000);
|
---|
169 | bool waitForReadyRead(int msecs = 30000);
|
---|
170 | bool waitForBytesWritten(int msecs = 30000);
|
---|
171 | bool waitForDisconnected(int msecs = 30000);
|
---|
172 |
|
---|
173 | QList<QSslError> sslErrors() const;
|
---|
174 |
|
---|
175 | static bool supportsSsl();
|
---|
176 | void ignoreSslErrors(const QList<QSslError> &errors);
|
---|
177 |
|
---|
178 | public Q_SLOTS:
|
---|
179 | void startClientEncryption();
|
---|
180 | void startServerEncryption();
|
---|
181 | void ignoreSslErrors();
|
---|
182 |
|
---|
183 | Q_SIGNALS:
|
---|
184 | void encrypted();
|
---|
185 | void peerVerifyError(const QSslError &error);
|
---|
186 | void sslErrors(const QList<QSslError> &errors);
|
---|
187 | void modeChanged(QSslSocket::SslMode newMode);
|
---|
188 | void encryptedBytesWritten(qint64 totalBytes);
|
---|
189 |
|
---|
190 | protected Q_SLOTS:
|
---|
191 | void connectToHostImplementation(const QString &hostName, quint16 port,
|
---|
192 | OpenMode openMode);
|
---|
193 | void disconnectFromHostImplementation();
|
---|
194 |
|
---|
195 | protected:
|
---|
196 | qint64 readData(char *data, qint64 maxlen);
|
---|
197 | qint64 writeData(const char *data, qint64 len);
|
---|
198 |
|
---|
199 | private:
|
---|
200 | Q_DECLARE_PRIVATE(QSslSocket)
|
---|
201 | Q_DISABLE_COPY(QSslSocket)
|
---|
202 | Q_PRIVATE_SLOT(d_func(), void _q_connectedSlot())
|
---|
203 | Q_PRIVATE_SLOT(d_func(), void _q_hostFoundSlot())
|
---|
204 | Q_PRIVATE_SLOT(d_func(), void _q_disconnectedSlot())
|
---|
205 | Q_PRIVATE_SLOT(d_func(), void _q_stateChangedSlot(QAbstractSocket::SocketState))
|
---|
206 | Q_PRIVATE_SLOT(d_func(), void _q_errorSlot(QAbstractSocket::SocketError))
|
---|
207 | Q_PRIVATE_SLOT(d_func(), void _q_readyReadSlot())
|
---|
208 | Q_PRIVATE_SLOT(d_func(), void _q_bytesWrittenSlot(qint64))
|
---|
209 | Q_PRIVATE_SLOT(d_func(), void _q_flushWriteBuffer())
|
---|
210 | Q_PRIVATE_SLOT(d_func(), void _q_flushReadBuffer())
|
---|
211 | friend class QSslSocketBackendPrivate;
|
---|
212 | };
|
---|
213 |
|
---|
214 | #endif // QT_NO_OPENSSL
|
---|
215 |
|
---|
216 | QT_END_NAMESPACE
|
---|
217 |
|
---|
218 | #ifndef QT_NO_OPENSSL
|
---|
219 | Q_DECLARE_METATYPE(QList<QSslError>)
|
---|
220 | #endif
|
---|
221 |
|
---|
222 | QT_END_HEADER
|
---|
223 |
|
---|
224 | #endif
|
---|