65#include "moc_qgsauthsslimportdialog.cpp"
67#include "ui_qgsauthsslimporterrors.h"
90 mAuthNotifyLayout =
new QVBoxLayout;
91 this->setLayout( mAuthNotifyLayout );
93 mAuthNotifyLayout->addWidget( mAuthNotify );
98 connect( btnCertPath, &QToolButton::clicked,
this, &QgsAuthSslImportDialog::btnCertPath_clicked );
99 QStyle *style = QApplication::style();
100 lblWarningIcon->setPixmap( style->standardIcon( QStyle::SP_MessageBoxWarning ).pixmap( 48, 48 ) );
101 lblWarningIcon->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
103 closeButton()->setDefault(
false );
104 saveButton()->setEnabled(
false );
106 leServer->setSelection( 0, leServer->text().size() );
107 pteSessionStatus->setReadOnly(
true );
108 spinbxTimeout->setClearValue( 15 );
109 spinbxTimeout->setValue( 15 );
110 spinbxPort->setClearValue( 443 );
112 grpbxServer->setCollapsed(
false );
113 radioServerImport->setChecked(
true );
114 frameServerImport->setEnabled(
true );
115 radioFileImport->setChecked(
false );
116 frameFileImport->setEnabled(
false );
118 connect( radioServerImport, &QAbstractButton::toggled,
this, &QgsAuthSslImportDialog::radioServerImportToggled );
119 connect( radioFileImport, &QAbstractButton::toggled,
this, &QgsAuthSslImportDialog::radioFileImportToggled );
121 connect( leServer, &QLineEdit::textChanged,
this, &QgsAuthSslImportDialog::updateEnabledState );
122 connect( btnConnect, &QAbstractButton::clicked,
this, &QgsAuthSslImportDialog::secureConnect );
123 connect( leServer, &QLineEdit::returnPressed, btnConnect, &QAbstractButton::click );
126 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
129 wdgtSslConfig->setEnabled(
false );
137 wdgtSslConfig->saveSslCertConfig();
141void QgsAuthSslImportDialog::updateEnabledState()
143 leServer->setStyleSheet( QString() );
145 const bool unconnected = !mSocket || mSocket->state() == QAbstractSocket::UnconnectedState;
147 leServer->setReadOnly( !unconnected );
148 spinbxPort->setReadOnly( !unconnected );
149 spinbxTimeout->setReadOnly( !unconnected );
151 leServer->setFocusPolicy( unconnected ? Qt::StrongFocus : Qt::NoFocus );
152 btnConnect->setEnabled( unconnected && !leServer->text().isEmpty() );
154 const bool connected = mSocket && mSocket->state() == QAbstractSocket::ConnectedState;
155 if ( connected && !mSocket->peerName().isEmpty() )
157 appendString( tr(
"Connected to %1: %2" ).arg( mSocket->peerName() ).arg( mSocket->peerPort() ) );
161void QgsAuthSslImportDialog::secureConnect()
163 if ( leServer->text().isEmpty() )
168 leServer->setStyleSheet( QString() );
169 clearStatusCertificateConfig();
173 mSocket =
new QSslSocket(
this );
174 connect( mSocket, &QAbstractSocket::stateChanged,
this, &QgsAuthSslImportDialog::socketStateChanged );
175 connect( mSocket, &QAbstractSocket::connected,
this, &QgsAuthSslImportDialog::socketConnected );
176 connect( mSocket, &QAbstractSocket::disconnected,
this, &QgsAuthSslImportDialog::socketDisconnected );
177 connect( mSocket, &QSslSocket::encrypted,
this, &QgsAuthSslImportDialog::socketEncrypted );
178#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
179 connect( mSocket,
static_cast<void ( QAbstractSocket::* )( QAbstractSocket::SocketError )
>( &QAbstractSocket::error ),
this, &QgsAuthSslImportDialog::socketError );
181 connect( mSocket, &QAbstractSocket::errorOccurred,
this, &QgsAuthSslImportDialog::socketError );
183 connect( mSocket,
static_cast<void ( QSslSocket::* )(
const QList<QSslError> & )
>( &QSslSocket::sslErrors ),
this, &QgsAuthSslImportDialog::sslErrors );
184 connect( mSocket, &QIODevice::readyRead,
this, &QgsAuthSslImportDialog::socketReadyRead );
187 QSslConfiguration sslConfig = mSocket->sslConfiguration();
188 sslConfig.setCaCertificates( mTrustedCAs );
189 mSocket->setSslConfiguration( sslConfig );
193 mTimer =
new QTimer(
this );
194 connect( mTimer, &QTimer::timeout,
this, &QgsAuthSslImportDialog::destroySocket );
196 mTimer->start( spinbxTimeout->value() * 1000 );
198 mSocket->connectToHost( leServer->text(), spinbxPort->value() );
199 updateEnabledState();
202void QgsAuthSslImportDialog::socketStateChanged( QAbstractSocket::SocketState state )
204 if ( mExecErrorsDialog )
209 updateEnabledState();
210 if ( state == QAbstractSocket::UnconnectedState )
212 leServer->setFocus();
217void QgsAuthSslImportDialog::socketConnected()
219 appendString( tr(
"Socket CONNECTED" ) );
220 mSocket->startClientEncryption();
223void QgsAuthSslImportDialog::socketDisconnected()
225 appendString( tr(
"Socket DISCONNECTED" ) );
228void QgsAuthSslImportDialog::socketEncrypted()
234 appendString( tr(
"Socket ENCRYPTED" ) );
238 const QSslCipher ciph = mSocket->sessionCipher();
239 const QString cipher = QStringLiteral(
"%1: %2, %3 (%4/%5)" )
240 .arg( tr(
"Session cipher" ), ciph.authenticationMethod(), ciph.name() )
241 .arg( ciph.usedBits() )
242 .arg( ciph.supportedBits() );
243 appendString( cipher );
246 wdgtSslConfig->setEnabled(
true );
247 const QString hostport( QStringLiteral(
"%1:%2" ).arg( mSocket->peerName() ).arg( mSocket->peerPort() ) );
248 wdgtSslConfig->setSslCertificate( mSocket->peerCertificate(), hostport.trimmed() );
249 if ( !mSslErrors.isEmpty() )
251 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
263void QgsAuthSslImportDialog::socketError( QAbstractSocket::SocketError err )
268 appendString( QStringLiteral(
"%1: %2" ).arg( tr(
"Socket ERROR" ), mSocket->errorString() ) );
272void QgsAuthSslImportDialog::socketReadyRead()
274 appendString( QString::fromUtf8( mSocket->readAll() ) );
277void QgsAuthSslImportDialog::destroySocket()
283 if ( !mSocket->isEncrypted() )
285 appendString( tr(
"Socket unavailable or not encrypted" ) );
287 mSocket->disconnectFromHost();
288 mSocket->deleteLater();
292void QgsAuthSslImportDialog::sslErrors(
const QList<QSslError> &errors )
294 if ( !mTimer->isActive() )
300 QDialog errorDialog(
this );
302 ui.setupUi( &errorDialog );
303 const auto constErrors = errors;
304 for (
const QSslError &error : constErrors )
306 ui.sslErrorList->addItem( error.errorString() );
309 mExecErrorsDialog =
true;
310 if ( errorDialog.exec() == QDialog::Accepted )
312 mSocket->ignoreSslErrors();
315 mExecErrorsDialog =
false;
320 if ( mSocket->state() != QAbstractSocket::ConnectedState )
321 socketStateChanged( mSocket->state() );
324void QgsAuthSslImportDialog::showCertificateInfo()
326 QList<QSslCertificate> peerchain( mSocket->peerCertificateChain() );
328 if ( !peerchain.isEmpty() )
330 const QSslCertificate cert = peerchain.takeFirst();
331 if ( !cert.isNull() )
340void QgsAuthSslImportDialog::widgetReadyToSaveChanged(
bool cansave )
342 saveButton()->setEnabled( cansave );
345void QgsAuthSslImportDialog::checkCanSave()
347 saveButton()->setEnabled( wdgtSslConfig->readyToSave() );
348 saveButton()->setDefault(
false );
349 closeButton()->setDefault(
false );
352void QgsAuthSslImportDialog::radioServerImportToggled(
bool checked )
354 frameServerImport->setEnabled( checked );
355 clearStatusCertificateConfig();
358void QgsAuthSslImportDialog::radioFileImportToggled(
bool checked )
360 frameFileImport->setEnabled( checked );
361 clearStatusCertificateConfig();
364void QgsAuthSslImportDialog::btnCertPath_clicked()
366 const QString &fn = getOpenFileName( tr(
"Open Server Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
369 leCertPath->setText( fn );
374void QgsAuthSslImportDialog::clearCertificateConfig()
376 wdgtSslConfig->resetSslCertConfig();
377 wdgtSslConfig->setEnabled(
false );
380void QgsAuthSslImportDialog::clearStatusCertificateConfig()
383 pteSessionStatus->clear();
384 saveButton()->setEnabled(
false );
385 clearCertificateConfig();
388void QgsAuthSslImportDialog::loadCertFromFile()
390 clearStatusCertificateConfig();
393 if ( certs.isEmpty() )
395 appendString( tr(
"Could not load any certs from file" ) );
399 const QSslCertificate cert( certs.first() );
402 appendString( tr(
"Could not load server cert from file" ) );
408 appendString( tr(
"Certificate does not appear for be for an SSL server. "
409 "You can still add a configuration, if you know it is the correct certificate." ) );
412 wdgtSslConfig->setEnabled(
true );
413 wdgtSslConfig->setSslHost( QString() );
414 wdgtSslConfig->setSslCertificate( cert );
415 if ( !mSslErrors.isEmpty() )
417 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
423void QgsAuthSslImportDialog::appendString(
const QString &line )
425 QTextCursor cursor( pteSessionStatus->textCursor() );
426 cursor.movePosition( QTextCursor::End );
427 cursor.insertText( line +
'\n' );
431QPushButton *QgsAuthSslImportDialog::saveButton()
433 return buttonBox->button( QDialogButtonBox::Save );
436QPushButton *QgsAuthSslImportDialog::closeButton()
438 return buttonBox->button( QDialogButtonBox::Close );
441QString QgsAuthSslImportDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
444 const QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportSslOpenFileDir" ), QDir::homePath() ).toString();
445 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
449 this->activateWindow();
453 settings.
setValue( QStringLiteral(
"UI/lastAuthImportSslOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Dialog wrapper for widget displaying detailed info on a certificate and its hierarchical trust chain.
static QString getSslProtocolName(QSsl::SslProtocol protocol)
SSL Protocol name strings per enum.
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Returns a list of concatenated certs from a PEM or DER formatted file.
static bool certificateIsSslServer(const QSslCertificate &cert)
Gets whether a certificate is probably used for a SSL server.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
const QList< QSslCertificate > trustedCaCertsCache()
trustedCaCertsCache cache of trusted certificate authorities, ready for network connections
QgsAuthSslImportDialog(QWidget *parent=nullptr)
Construct dialog for importing certificates.
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.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsgLevel(str, level)