1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "certificateinfo.h"
|
---|
43 | #include "sslclient.h"
|
---|
44 | #include "ui_sslclient.h"
|
---|
45 | #include "ui_sslerrors.h"
|
---|
46 |
|
---|
47 | #include <QtGui/QScrollBar>
|
---|
48 | #include <QtGui/QStyle>
|
---|
49 | #include <QtGui/QToolButton>
|
---|
50 | #include <QtNetwork/QSslCipher>
|
---|
51 |
|
---|
52 | SslClient::SslClient(QWidget *parent)
|
---|
53 | : QWidget(parent), socket(0), padLock(0), executingDialog(false)
|
---|
54 | {
|
---|
55 | form = new Ui_Form;
|
---|
56 | form->setupUi(this);
|
---|
57 | form->hostNameEdit->setSelection(0, form->hostNameEdit->text().size());
|
---|
58 | form->sessionOutput->setHtml(tr("<not connected>"));
|
---|
59 |
|
---|
60 | connect(form->hostNameEdit, SIGNAL(textChanged(QString)),
|
---|
61 | this, SLOT(updateEnabledState()));
|
---|
62 | connect(form->connectButton, SIGNAL(clicked()),
|
---|
63 | this, SLOT(secureConnect()));
|
---|
64 | connect(form->sendButton, SIGNAL(clicked()),
|
---|
65 | this, SLOT(sendData()));
|
---|
66 | }
|
---|
67 |
|
---|
68 | SslClient::~SslClient()
|
---|
69 | {
|
---|
70 | delete form;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void SslClient::updateEnabledState()
|
---|
74 | {
|
---|
75 | bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState;
|
---|
76 |
|
---|
77 | form->hostNameEdit->setReadOnly(!unconnected);
|
---|
78 | form->hostNameEdit->setFocusPolicy(unconnected ? Qt::StrongFocus : Qt::NoFocus);
|
---|
79 |
|
---|
80 | form->hostNameLabel->setEnabled(unconnected);
|
---|
81 | form->portBox->setEnabled(unconnected);
|
---|
82 | form->portLabel->setEnabled(unconnected);
|
---|
83 | form->connectButton->setEnabled(unconnected && !form->hostNameEdit->text().isEmpty());
|
---|
84 |
|
---|
85 | bool connected = socket && socket->state() == QAbstractSocket::ConnectedState;
|
---|
86 | form->sessionBox->setEnabled(connected);
|
---|
87 | form->sessionOutput->setEnabled(connected);
|
---|
88 | form->sessionInput->setEnabled(connected);
|
---|
89 | form->sessionInputLabel->setEnabled(connected);
|
---|
90 | form->sendButton->setEnabled(connected);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void SslClient::secureConnect()
|
---|
94 | {
|
---|
95 | if (!socket) {
|
---|
96 | socket = new QSslSocket(this);
|
---|
97 | connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
|
---|
98 | this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
|
---|
99 | connect(socket, SIGNAL(encrypted()),
|
---|
100 | this, SLOT(socketEncrypted()));
|
---|
101 | connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
|
---|
102 | this, SLOT(sslErrors(QList<QSslError>)));
|
---|
103 | connect(socket, SIGNAL(readyRead()),
|
---|
104 | this, SLOT(socketReadyRead()));
|
---|
105 | }
|
---|
106 |
|
---|
107 | socket->connectToHostEncrypted(form->hostNameEdit->text(), form->portBox->value());
|
---|
108 | updateEnabledState();
|
---|
109 | }
|
---|
110 |
|
---|
111 | void SslClient::socketStateChanged(QAbstractSocket::SocketState state)
|
---|
112 | {
|
---|
113 | if (executingDialog)
|
---|
114 | return;
|
---|
115 |
|
---|
116 | updateEnabledState();
|
---|
117 | if (state == QAbstractSocket::UnconnectedState) {
|
---|
118 | form->hostNameEdit->setPalette(QPalette());
|
---|
119 | form->hostNameEdit->setFocus();
|
---|
120 | form->cipherLabel->setText(tr("<none>"));
|
---|
121 | if (padLock)
|
---|
122 | padLock->hide();
|
---|
123 | socket->deleteLater();
|
---|
124 | socket = 0;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | void SslClient::socketEncrypted()
|
---|
129 | {
|
---|
130 | if (!socket)
|
---|
131 | return; // might have disconnected already
|
---|
132 |
|
---|
133 | form->sessionOutput->clear();
|
---|
134 | form->sessionInput->setFocus();
|
---|
135 |
|
---|
136 | QPalette palette;
|
---|
137 | palette.setColor(QPalette::Base, QColor(255, 255, 192));
|
---|
138 | form->hostNameEdit->setPalette(palette);
|
---|
139 |
|
---|
140 | QSslCipher ciph = socket->sessionCipher();
|
---|
141 | QString cipher = QString("%1, %2 (%3/%4)").arg(ciph.authenticationMethod())
|
---|
142 | .arg(ciph.name()).arg(ciph.usedBits()).arg(ciph.supportedBits());;
|
---|
143 | form->cipherLabel->setText(cipher);
|
---|
144 |
|
---|
145 | if (!padLock) {
|
---|
146 | padLock = new QToolButton;
|
---|
147 | padLock->setIcon(QIcon(":/encrypted.png"));
|
---|
148 | padLock->setCursor(Qt::ArrowCursor);
|
---|
149 | padLock->setToolTip(tr("Display encryption details."));
|
---|
150 |
|
---|
151 | int extent = form->hostNameEdit->height() - 2;
|
---|
152 | padLock->resize(extent, extent);
|
---|
153 | padLock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
|
---|
154 |
|
---|
155 | QHBoxLayout *layout = new QHBoxLayout(form->hostNameEdit);
|
---|
156 | layout->setMargin(form->hostNameEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth));
|
---|
157 | layout->setSpacing(0);
|
---|
158 | layout->addStretch();
|
---|
159 | layout->addWidget(padLock);
|
---|
160 |
|
---|
161 | form->hostNameEdit->setLayout(layout);
|
---|
162 |
|
---|
163 | connect(padLock, SIGNAL(clicked()),
|
---|
164 | this, SLOT(displayCertificateInfo()));
|
---|
165 | } else {
|
---|
166 | padLock->show();
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | void SslClient::socketReadyRead()
|
---|
171 | {
|
---|
172 | appendString(QString::fromUtf8(socket->readAll()));
|
---|
173 | }
|
---|
174 |
|
---|
175 | void SslClient::sendData()
|
---|
176 | {
|
---|
177 | QString input = form->sessionInput->text();
|
---|
178 | appendString(input + "\n");
|
---|
179 | socket->write(input.toUtf8() + "\r\n");
|
---|
180 | form->sessionInput->clear();
|
---|
181 | }
|
---|
182 |
|
---|
183 | void SslClient::sslErrors(const QList<QSslError> &errors)
|
---|
184 | {
|
---|
185 | QDialog errorDialog(this);
|
---|
186 | Ui_SslErrors ui;
|
---|
187 | ui.setupUi(&errorDialog);
|
---|
188 | connect(ui.certificateChainButton, SIGNAL(clicked()),
|
---|
189 | this, SLOT(displayCertificateInfo()));
|
---|
190 |
|
---|
191 | foreach (const QSslError &error, errors)
|
---|
192 | ui.sslErrorList->addItem(error.errorString());
|
---|
193 |
|
---|
194 | executingDialog = true;
|
---|
195 | if (errorDialog.exec() == QDialog::Accepted)
|
---|
196 | socket->ignoreSslErrors();
|
---|
197 | executingDialog = false;
|
---|
198 |
|
---|
199 | // did the socket state change?
|
---|
200 | if (socket->state() != QAbstractSocket::ConnectedState)
|
---|
201 | socketStateChanged(socket->state());
|
---|
202 | }
|
---|
203 |
|
---|
204 | void SslClient::displayCertificateInfo()
|
---|
205 | {
|
---|
206 | CertificateInfo *info = new CertificateInfo(this);
|
---|
207 | info->setCertificateChain(socket->peerCertificateChain());
|
---|
208 | info->exec();
|
---|
209 | info->deleteLater();
|
---|
210 | }
|
---|
211 |
|
---|
212 | void SslClient::appendString(const QString &line)
|
---|
213 | {
|
---|
214 | QTextCursor cursor(form->sessionOutput->textCursor());
|
---|
215 | cursor.movePosition(QTextCursor::End);
|
---|
216 | cursor.insertText(line);
|
---|
217 | form->sessionOutput->verticalScrollBar()->setValue(form->sessionOutput->verticalScrollBar()->maximum());
|
---|
218 | }
|
---|