66 #include "ui_qgsauthsslimporterrors.h" 69 #include <QFileDialog> 71 #include <QPushButton> 75 #include <QToolButton> 89 mAuthNotifyLayout =
new QVBoxLayout;
90 this->setLayout( mAuthNotifyLayout );
92 mAuthNotifyLayout->addWidget( mAuthNotify );
97 connect( btnCertPath, &QToolButton::clicked,
this, &QgsAuthSslImportDialog::btnCertPath_clicked );
98 QStyle *style = QApplication::style();
99 lblWarningIcon->setPixmap( style->standardIcon( QStyle::SP_MessageBoxWarning ).pixmap( 48, 48 ) );
100 lblWarningIcon->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
102 closeButton()->setDefault(
false );
103 saveButton()->setEnabled(
false );
105 leServer->setSelection( 0, leServer->text().size() );
106 pteSessionStatus->setReadOnly(
true );
107 spinbxTimeout->setValue( 15 );
109 grpbxServer->setCollapsed(
false );
110 radioServerImport->setChecked(
true );
111 frameServerImport->setEnabled(
true );
112 radioFileImport->setChecked(
false );
113 frameFileImport->setEnabled(
false );
115 connect( radioServerImport, &QAbstractButton::toggled,
116 this, &QgsAuthSslImportDialog::radioServerImportToggled );
117 connect( radioFileImport, &QAbstractButton::toggled,
118 this, &QgsAuthSslImportDialog::radioFileImportToggled );
120 connect( leServer, &QLineEdit::textChanged,
121 this, &QgsAuthSslImportDialog::updateEnabledState );
122 connect( btnConnect, &QAbstractButton::clicked,
123 this, &QgsAuthSslImportDialog::secureConnect );
124 connect( leServer, &QLineEdit::returnPressed,
125 btnConnect, &QAbstractButton::click );
127 connect( buttonBox, &QDialogButtonBox::accepted,
129 connect( buttonBox, &QDialogButtonBox::rejected,
130 this, &QDialog::reject );
133 this, &QgsAuthSslImportDialog::widgetReadyToSaveChanged );
134 wdgtSslConfig->setEnabled(
false );
142 wdgtSslConfig->saveSslCertConfig();
146 void QgsAuthSslImportDialog::updateEnabledState()
148 leServer->setStyleSheet( QLatin1String(
"" ) );
150 bool unconnected = !mSocket || mSocket->state() == QAbstractSocket::UnconnectedState;
152 leServer->setReadOnly( !unconnected );
153 spinbxPort->setReadOnly( !unconnected );
154 spinbxTimeout->setReadOnly( !unconnected );
156 leServer->setFocusPolicy( unconnected ? Qt::StrongFocus : Qt::NoFocus );
157 btnConnect->setEnabled( unconnected && !leServer->text().isEmpty() );
159 bool connected = mSocket && mSocket->state() == QAbstractSocket::ConnectedState;
160 if ( connected && !mSocket->peerName().isEmpty() )
162 appendString( tr(
"Connected to %1: %2" ).arg( mSocket->peerName() ).arg( mSocket->peerPort() ) );
166 void QgsAuthSslImportDialog::secureConnect()
168 if ( leServer->text().isEmpty() )
173 leServer->setStyleSheet( QLatin1String(
"" ) );
174 clearStatusCertificateConfig();
178 mSocket =
new QSslSocket(
this );
179 connect( mSocket, &QAbstractSocket::stateChanged,
180 this, &QgsAuthSslImportDialog::socketStateChanged );
181 connect( mSocket, &QAbstractSocket::connected,
182 this, &QgsAuthSslImportDialog::socketConnected );
183 connect( mSocket, &QAbstractSocket::disconnected,
184 this, &QgsAuthSslImportDialog::socketDisconnected );
185 connect( mSocket, &QSslSocket::encrypted,
186 this, &QgsAuthSslImportDialog::socketEncrypted );
187 connect( mSocket,
static_cast<void ( QAbstractSocket::* )( QAbstractSocket::SocketError )
>( &QAbstractSocket::error ),
188 this, &QgsAuthSslImportDialog::socketError );
189 connect( mSocket,
static_cast<void ( QSslSocket::* )(
const QList<QSslError> & )
>( &QSslSocket::sslErrors ),
190 this, &QgsAuthSslImportDialog::sslErrors );
191 connect( mSocket, &QIODevice::readyRead,
192 this, &QgsAuthSslImportDialog::socketReadyRead );
195 mSocket->setCaCertificates( mTrustedCAs );
199 mTimer =
new QTimer(
this );
200 connect( mTimer, &QTimer::timeout,
this, &QgsAuthSslImportDialog::destroySocket );
202 mTimer->start( spinbxTimeout->value() * 1000 );
204 mSocket->connectToHost( leServer->text(), spinbxPort->value() );
205 updateEnabledState();
208 void QgsAuthSslImportDialog::socketStateChanged( QAbstractSocket::SocketState state )
210 if ( mExecErrorsDialog )
215 updateEnabledState();
216 if ( state == QAbstractSocket::UnconnectedState )
218 leServer->setFocus();
223 void QgsAuthSslImportDialog::socketConnected()
225 appendString( tr(
"Socket CONNECTED" ) );
226 mSocket->startClientEncryption();
229 void QgsAuthSslImportDialog::socketDisconnected()
231 appendString( tr(
"Socket DISCONNECTED" ) );
234 void QgsAuthSslImportDialog::socketEncrypted()
240 appendString( tr(
"Socket ENCRYPTED" ) );
242 appendString( QStringLiteral(
"%1: %2" ).arg( tr(
"Protocol" ),
245 QSslCipher ciph = mSocket->sessionCipher();
246 QString cipher = QStringLiteral(
"%1: %2, %3 (%4/%5)" )
247 .arg( tr(
"Session cipher" ), ciph.authenticationMethod(), ciph.name() )
248 .arg( ciph.usedBits() ).arg( ciph.supportedBits() );
249 appendString( cipher );
253 wdgtSslConfig->setEnabled(
true );
254 QString hostport( QStringLiteral(
"%1:%2" ).arg( mSocket->peerName() ).arg( mSocket->peerPort() ) );
255 wdgtSslConfig->setSslCertificate( mSocket->peerCertificate(), hostport.trimmed() );
256 if ( !mSslErrors.isEmpty() )
258 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
270 void QgsAuthSslImportDialog::socketError( QAbstractSocket::SocketError err )
275 appendString( QStringLiteral(
"%1: %2" ).arg( tr(
"Socket ERROR" ), mSocket->errorString() ) );
279 void QgsAuthSslImportDialog::socketReadyRead()
281 appendString( QString::fromUtf8( mSocket->readAll() ) );
284 void QgsAuthSslImportDialog::destroySocket()
290 if ( !mSocket->isEncrypted() )
292 appendString( tr(
"Socket unavailable or not encrypted" ) );
294 mSocket->disconnectFromHost();
295 mSocket->deleteLater();
299 void QgsAuthSslImportDialog::sslErrors(
const QList<QSslError> &errors )
301 if ( !mTimer->isActive() )
307 QDialog errorDialog(
this );
309 ui.setupUi( &errorDialog );
310 Q_FOREACH (
const QSslError &error, errors )
312 ui.sslErrorList->addItem( error.errorString() );
315 mExecErrorsDialog =
true;
316 if ( errorDialog.exec() == QDialog::Accepted )
318 mSocket->ignoreSslErrors();
321 mExecErrorsDialog =
false;
326 if ( mSocket->state() != QAbstractSocket::ConnectedState )
327 socketStateChanged( mSocket->state() );
330 void QgsAuthSslImportDialog::showCertificateInfo()
332 QList<QSslCertificate> peerchain( mSocket->peerCertificateChain() );
334 if ( !peerchain.isEmpty() )
336 QSslCertificate cert = peerchain.takeFirst();
337 if ( !cert.isNull() )
346 void QgsAuthSslImportDialog::widgetReadyToSaveChanged(
bool cansave )
348 saveButton()->setEnabled( cansave );
351 void QgsAuthSslImportDialog::checkCanSave()
353 saveButton()->setEnabled( wdgtSslConfig->readyToSave() );
354 saveButton()->setDefault(
false );
355 closeButton()->setDefault(
false );
358 void QgsAuthSslImportDialog::radioServerImportToggled(
bool checked )
360 frameServerImport->setEnabled( checked );
361 clearStatusCertificateConfig();
364 void QgsAuthSslImportDialog::radioFileImportToggled(
bool checked )
366 frameFileImport->setEnabled( checked );
367 clearStatusCertificateConfig();
370 void QgsAuthSslImportDialog::btnCertPath_clicked()
372 const QString &fn = getOpenFileName( tr(
"Open Server Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
375 leCertPath->setText( fn );
380 void QgsAuthSslImportDialog::clearCertificateConfig()
382 wdgtSslConfig->resetSslCertConfig();
383 wdgtSslConfig->setEnabled(
false );
386 void QgsAuthSslImportDialog::clearStatusCertificateConfig()
389 pteSessionStatus->clear();
390 saveButton()->setEnabled(
false );
391 clearCertificateConfig();
394 void QgsAuthSslImportDialog::loadCertFromFile()
396 clearStatusCertificateConfig();
399 if ( certs.isEmpty() )
401 appendString( tr(
"Could not load any certs from file" ) );
405 QSslCertificate cert( certs.first() );
408 appendString( tr(
"Could not load server cert from file" ) );
414 appendString( tr(
"Certificate does not appear for be for an SSL server. " 415 "You can still add a configuration, if you know it is the correct certificate." ) );
418 wdgtSslConfig->setEnabled(
true );
419 wdgtSslConfig->setSslHost( QLatin1String(
"" ) );
420 wdgtSslConfig->setSslCertificate( cert );
421 if ( !mSslErrors.isEmpty() )
423 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
429 void QgsAuthSslImportDialog::appendString(
const QString &line )
431 QTextCursor cursor( pteSessionStatus->textCursor() );
432 cursor.movePosition( QTextCursor::End );
433 cursor.insertText( line +
'\n' );
437 QPushButton *QgsAuthSslImportDialog::saveButton()
439 return buttonBox->button( QDialogButtonBox::Save );
442 QPushButton *QgsAuthSslImportDialog::closeButton()
444 return buttonBox->button( QDialogButtonBox::Close );
447 QString QgsAuthSslImportDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
450 QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportSslOpenFileDir" ), QDir::homePath() ).toString();
451 QString
f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
455 this->activateWindow();
459 settings.
setValue( QStringLiteral(
"UI/lastAuthImportSslOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
This class is a composition of two QSettings instances:
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Return list of concatenated certs from a PEM or DER formatted file.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Dialog wrapper for widget displaying detailed info on a certificate and its hierarchical trust chain...
QgsAuthSslImportDialog(QWidget *parent=nullptr)
Construct dialog for importing certificates.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
static bool certificateIsSslServer(const QSslCertificate &cert)
Get whether a certificate is probably used for a SSL server.
static QString getSslProtocolName(QSsl::SslProtocol protocol)
SSL Protocol name strings per enum.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
const QList< QSslCertificate > trustedCaCertsCache()
trustedCaCertsCache cache of trusted certificate authorities, ready for network connections ...