qsslsocket.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtNetwork module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 
00043 #ifndef QSSLSOCKET_H
00044 #define QSSLSOCKET_H
00045 
00046 #include <QtCore/qlist.h>
00047 #include <QtCore/qregexp.h>
00048 #ifndef QT_NO_OPENSSL
00049 #   include <QtNetwork/qtcpsocket.h>
00050 #   include <QtNetwork/qsslerror.h>
00051 #endif
00052 
00053 QT_BEGIN_HEADER
00054 
00055 QT_BEGIN_NAMESPACE
00056 
00057 QT_MODULE(Network)
00058 
00059 #ifndef QT_NO_OPENSSL
00060 
00061 class QDir;
00062 class QSslCipher;
00063 class QSslCertificate;
00064 class QSslConfiguration;
00065 
00066 class QSslSocketPrivate;
00067 class Q_NETWORK_EXPORT QSslSocket : public QTcpSocket
00068 {
00069     Q_OBJECT
00070 public:
00071     enum SslMode {
00072         UnencryptedMode,
00073         SslClientMode,
00074         SslServerMode
00075     };
00076 
00077     enum PeerVerifyMode {
00078         VerifyNone,
00079         QueryPeer,
00080         VerifyPeer,
00081         AutoVerifyPeer
00082     };
00083 
00084     QSslSocket(QObject *parent = 0);
00085     ~QSslSocket();
00086 
00087     // Autostarting the SSL client handshake.
00088     void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
00089     void connectToHostEncrypted(const QString &hostName, quint16 port, const QString &sslPeerName, OpenMode mode = ReadWrite);
00090     bool setSocketDescriptor(int socketDescriptor, SocketState state = ConnectedState,
00091                              OpenMode openMode = ReadWrite);
00092 
00093     // ### Qt 5: Make virtual
00094     void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
00095     QVariant socketOption(QAbstractSocket::SocketOption option);
00096 
00097     SslMode mode() const;
00098     bool isEncrypted() const;
00099 
00100     QSsl::SslProtocol protocol() const;
00101     void setProtocol(QSsl::SslProtocol protocol);
00102 
00103     QSslSocket::PeerVerifyMode peerVerifyMode() const;
00104     void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode);
00105 
00106     int peerVerifyDepth() const;
00107     void setPeerVerifyDepth(int depth);
00108 
00109     // From QIODevice
00110     qint64 bytesAvailable() const;
00111     qint64 bytesToWrite() const;
00112     bool canReadLine() const;
00113     void close();
00114     bool atEnd() const;
00115     bool flush();
00116     void abort();
00117 
00118     // From QAbstractSocket:
00119     void setReadBufferSize(qint64 size);
00120 
00121     // Similar to QIODevice's:
00122     qint64 encryptedBytesAvailable() const;
00123     qint64 encryptedBytesToWrite() const;
00124 
00125     // SSL configuration
00126     QSslConfiguration sslConfiguration() const;
00127     void setSslConfiguration(const QSslConfiguration &config);
00128 
00129     // Certificate & cipher accessors.
00130     void setLocalCertificate(const QSslCertificate &certificate);
00131     void setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format = QSsl::Pem);
00132     QSslCertificate localCertificate() const;
00133     QSslCertificate peerCertificate() const;
00134     QList<QSslCertificate> peerCertificateChain() const;
00135     QSslCipher sessionCipher() const;
00136 
00137     // Private keys, for server sockets.
00138     void setPrivateKey(const QSslKey &key);
00139     void setPrivateKey(const QString &fileName, QSsl::KeyAlgorithm algorithm = QSsl::Rsa,
00140                        QSsl::EncodingFormat format = QSsl::Pem,
00141                        const QByteArray &passPhrase = QByteArray());
00142     QSslKey privateKey() const;
00143 
00144     // Cipher settings.
00145     QList<QSslCipher> ciphers() const;
00146     void setCiphers(const QList<QSslCipher> &ciphers);
00147     void setCiphers(const QString &ciphers);
00148     static void setDefaultCiphers(const QList<QSslCipher> &ciphers);
00149     static QList<QSslCipher> defaultCiphers();
00150     static QList<QSslCipher> supportedCiphers();
00151 
00152     // CA settings.
00153     bool addCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
00154                            QRegExp::PatternSyntax syntax = QRegExp::FixedString);
00155     void addCaCertificate(const QSslCertificate &certificate);
00156     void addCaCertificates(const QList<QSslCertificate> &certificates);
00157     void setCaCertificates(const QList<QSslCertificate> &certificates);
00158     QList<QSslCertificate> caCertificates() const;
00159     static bool addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
00160                                          QRegExp::PatternSyntax syntax = QRegExp::FixedString);
00161     static void addDefaultCaCertificate(const QSslCertificate &certificate);
00162     static void addDefaultCaCertificates(const QList<QSslCertificate> &certificates);
00163     static void setDefaultCaCertificates(const QList<QSslCertificate> &certificates);
00164     static QList<QSslCertificate> defaultCaCertificates();
00165     static QList<QSslCertificate> systemCaCertificates();
00166 
00167     bool waitForConnected(int msecs = 30000);
00168     bool waitForEncrypted(int msecs = 30000);
00169     bool waitForReadyRead(int msecs = 30000);
00170     bool waitForBytesWritten(int msecs = 30000);
00171     bool waitForDisconnected(int msecs = 30000);
00172 
00173     QList<QSslError> sslErrors() const;
00174 
00175     static bool supportsSsl();
00176     void ignoreSslErrors(const QList<QSslError> &errors);
00177 
00178 public Q_SLOTS:
00179     void startClientEncryption();
00180     void startServerEncryption();
00181     void ignoreSslErrors();
00182 
00183 Q_SIGNALS:
00184     void encrypted();
00185     void peerVerifyError(const QSslError &error);
00186     void sslErrors(const QList<QSslError> &errors);
00187     void modeChanged(QSslSocket::SslMode newMode);
00188     void encryptedBytesWritten(qint64 totalBytes);
00189 
00190 protected Q_SLOTS:
00191     void connectToHostImplementation(const QString &hostName, quint16 port,
00192                                      OpenMode openMode);
00193     void disconnectFromHostImplementation();
00194 
00195 protected:
00196     qint64 readData(char *data, qint64 maxlen);
00197     qint64 writeData(const char *data, qint64 len);
00198 
00199 private:
00200     Q_DECLARE_PRIVATE(QSslSocket)
00201     Q_DISABLE_COPY(QSslSocket)
00202     Q_PRIVATE_SLOT(d_func(), void _q_connectedSlot())
00203     Q_PRIVATE_SLOT(d_func(), void _q_hostFoundSlot())
00204     Q_PRIVATE_SLOT(d_func(), void _q_disconnectedSlot())
00205     Q_PRIVATE_SLOT(d_func(), void _q_stateChangedSlot(QAbstractSocket::SocketState))
00206     Q_PRIVATE_SLOT(d_func(), void _q_errorSlot(QAbstractSocket::SocketError))
00207     Q_PRIVATE_SLOT(d_func(), void _q_readyReadSlot())
00208     Q_PRIVATE_SLOT(d_func(), void _q_bytesWrittenSlot(qint64))
00209     Q_PRIVATE_SLOT(d_func(), void _q_flushWriteBuffer())
00210     Q_PRIVATE_SLOT(d_func(), void _q_flushReadBuffer())
00211     friend class QSslSocketBackendPrivate;
00212 };
00213 
00214 #endif // QT_NO_OPENSSL
00215 
00216 QT_END_NAMESPACE
00217 
00218 #ifndef QT_NO_OPENSSL
00219 Q_DECLARE_METATYPE(QList<QSslError>)
00220 #endif
00221 
00222 QT_END_HEADER
00223 
00224 #endif