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( QString() );
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( QString() );
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()
236 QgsDebugMsg( QStringLiteral(
"socketEncrypted entered" ) );
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 const auto constErrors = errors;
311 for (
const QSslError &error : constErrors )
313 ui.sslErrorList->addItem( error.errorString() );
316 mExecErrorsDialog =
true;
317 if ( errorDialog.exec() == QDialog::Accepted )
319 mSocket->ignoreSslErrors();
322 mExecErrorsDialog =
false;
327 if ( mSocket->state() != QAbstractSocket::ConnectedState )
328 socketStateChanged( mSocket->state() );
331 void QgsAuthSslImportDialog::showCertificateInfo()
333 QList<QSslCertificate> peerchain( mSocket->peerCertificateChain() );
335 if ( !peerchain.isEmpty() )
337 QSslCertificate cert = peerchain.takeFirst();
338 if ( !cert.isNull() )
347 void QgsAuthSslImportDialog::widgetReadyToSaveChanged(
bool cansave )
349 saveButton()->setEnabled( cansave );
352 void QgsAuthSslImportDialog::checkCanSave()
354 saveButton()->setEnabled( wdgtSslConfig->readyToSave() );
355 saveButton()->setDefault(
false );
356 closeButton()->setDefault(
false );
359 void QgsAuthSslImportDialog::radioServerImportToggled(
bool checked )
361 frameServerImport->setEnabled( checked );
362 clearStatusCertificateConfig();
365 void QgsAuthSslImportDialog::radioFileImportToggled(
bool checked )
367 frameFileImport->setEnabled( checked );
368 clearStatusCertificateConfig();
371 void QgsAuthSslImportDialog::btnCertPath_clicked()
373 const QString &fn = getOpenFileName( tr(
"Open Server Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
376 leCertPath->setText( fn );
381 void QgsAuthSslImportDialog::clearCertificateConfig()
383 wdgtSslConfig->resetSslCertConfig();
384 wdgtSslConfig->setEnabled(
false );
387 void QgsAuthSslImportDialog::clearStatusCertificateConfig()
390 pteSessionStatus->clear();
391 saveButton()->setEnabled(
false );
392 clearCertificateConfig();
395 void QgsAuthSslImportDialog::loadCertFromFile()
397 clearStatusCertificateConfig();
400 if ( certs.isEmpty() )
402 appendString( tr(
"Could not load any certs from file" ) );
406 QSslCertificate cert( certs.first() );
409 appendString( tr(
"Could not load server cert from file" ) );
415 appendString( tr(
"Certificate does not appear for be for an SSL server. " 416 "You can still add a configuration, if you know it is the correct certificate." ) );
419 wdgtSslConfig->setEnabled(
true );
420 wdgtSslConfig->setSslHost( QString() );
421 wdgtSslConfig->setSslCertificate( cert );
422 if ( !mSslErrors.isEmpty() )
424 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
430 void QgsAuthSslImportDialog::appendString(
const QString &line )
432 QTextCursor cursor( pteSessionStatus->textCursor() );
433 cursor.movePosition( QTextCursor::End );
434 cursor.insertText( line +
'\n' );
438 QPushButton *QgsAuthSslImportDialog::saveButton()
440 return buttonBox->button( QDialogButtonBox::Save );
443 QPushButton *QgsAuthSslImportDialog::closeButton()
445 return buttonBox->button( QDialogButtonBox::Close );
448 QString QgsAuthSslImportDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
451 QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportSslOpenFileDir" ), QDir::homePath() ).toString();
452 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
456 this->activateWindow();
460 settings.
setValue( QStringLiteral(
"UI/lastAuthImportSslOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Returns a list of concatenated certs from a PEM or DER formatted file.
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)
Gets 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.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
const QList< QSslCertificate > trustedCaCertsCache()
trustedCaCertsCache cache of trusted certificate authorities, ready for network connections ...