66 #include "ui_qgsauthsslimporterrors.h" 69 #include <QFileDialog> 71 #include <QPushButton> 75 #include <QToolButton> 86 , mExecErrorsDialog( false )
90 , mAuthNotifyLayout( nullptr )
91 , mAuthNotify( nullptr )
98 mAuthNotifyLayout->
addWidget( mAuthNotify );
104 lblWarningIcon->setPixmap( style->
standardIcon( QStyle::SP_MessageBoxWarning ).
pixmap( 48, 48 ) );
105 lblWarningIcon->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
110 leServer->setSelection( 0, leServer->text().size() );
111 pteSessionStatus->setReadOnly(
true );
112 spinbxTimeout->setValue( 15 );
114 grpbxServer->setCollapsed(
false );
115 radioServerImport->setChecked(
true );
116 frameServerImport->setEnabled(
true );
117 radioFileImport->setChecked(
false );
118 frameFileImport->setEnabled(
false );
120 connect( radioServerImport, SIGNAL( toggled(
bool ) ),
121 this, SLOT( radioServerImportToggled(
bool ) ) );
122 connect( radioFileImport, SIGNAL( toggled(
bool ) ),
123 this, SLOT( radioFileImportToggled(
bool ) ) );
126 this, SLOT( updateEnabledState() ) );
127 connect( btnConnect, SIGNAL( clicked() ),
128 this, SLOT( secureConnect() ) );
129 connect( leServer, SIGNAL( returnPressed() ),
130 btnConnect, SLOT( click() ) );
137 connect( wdgtSslConfig, SIGNAL( readyToSaveChanged(
bool ) ),
138 this, SLOT( widgetReadyToSaveChanged(
bool ) ) );
139 wdgtSslConfig->setEnabled(
false );
151 wdgtSslConfig->saveSslCertConfig();
155 void QgsAuthSslImportDialog::updateEnabledState()
157 leServer->setStyleSheet(
"" );
159 bool unconnected = !mSocket || mSocket->
state() == QAbstractSocket::UnconnectedState;
161 leServer->setReadOnly( !unconnected );
162 spinbxPort->setReadOnly( !unconnected );
163 spinbxTimeout->setReadOnly( !unconnected );
165 leServer->setFocusPolicy( unconnected ? Qt::StrongFocus : Qt::NoFocus );
166 btnConnect->setEnabled( unconnected && !leServer->text().isEmpty() );
168 bool connected = mSocket && mSocket->
state() == QAbstractSocket::ConnectedState;
171 appendString(
tr(
"Connected to %1:%2" ).arg( mSocket->
peerName() ).arg( mSocket->
peerPort() ) );
175 void QgsAuthSslImportDialog::secureConnect()
177 if ( leServer->text().isEmpty() )
182 leServer->setStyleSheet(
"" );
183 clearStatusCertificateConfig();
188 connect( mSocket, SIGNAL( stateChanged( QAbstractSocket::SocketState ) ),
189 this, SLOT( socketStateChanged( QAbstractSocket::SocketState ) ) );
190 connect( mSocket, SIGNAL( connected() ),
191 this, SLOT( socketConnected() ) );
192 connect( mSocket, SIGNAL( disconnected() ),
193 this, SLOT( socketDisconnected() ) );
194 connect( mSocket, SIGNAL( encrypted() ),
195 this, SLOT( socketEncrypted() ) );
196 connect( mSocket, SIGNAL( error( QAbstractSocket::SocketError ) ),
197 this, SLOT( socketError( QAbstractSocket::SocketError ) ) );
200 connect( mSocket, SIGNAL( readyRead() ),
201 this, SLOT( socketReadyRead() ) );
208 mTimer =
new QTimer(
this );
209 connect( mTimer, SIGNAL( timeout() ),
this, SLOT( destroySocket() ) );
211 mTimer->
start( spinbxTimeout->value() * 1000 );
213 mSocket->
connectToHost( leServer->text(), spinbxPort->value() );
214 updateEnabledState();
217 void QgsAuthSslImportDialog::socketStateChanged( QAbstractSocket::SocketState state )
219 if ( mExecErrorsDialog )
224 updateEnabledState();
225 if ( state == QAbstractSocket::UnconnectedState )
227 leServer->setFocus();
232 void QgsAuthSslImportDialog::socketConnected()
234 appendString(
tr(
"Socket CONNECTED" ) );
238 void QgsAuthSslImportDialog::socketDisconnected()
240 appendString(
tr(
"Socket DISCONNECTED" ) );
243 void QgsAuthSslImportDialog::socketEncrypted()
249 appendString(
tr(
"Socket ENCRYPTED" ) );
251 appendString(
QString(
"%1: %2" ).arg(
tr(
"Protocol" ),
256 .
arg(
tr(
"Session cipher" ), ciph.authenticationMethod(), ciph.name() )
257 .arg( ciph.usedBits() ).arg( ciph.supportedBits() );
258 appendString( cipher );
262 wdgtSslConfig->setEnabled(
true );
267 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
279 void QgsAuthSslImportDialog::socketError( QAbstractSocket::SocketError err )
288 void QgsAuthSslImportDialog::socketReadyRead()
293 void QgsAuthSslImportDialog::destroySocket()
301 appendString(
tr(
"Socket unavailable or not encrypted" ) );
318 ui.setupUi( &errorDialog );
319 connect( ui.certificateChainButton, SIGNAL( clicked() ),
320 this, SLOT( showCertificateInfo() ) );
322 Q_FOREACH (
const QSslError &error, errors )
327 mExecErrorsDialog =
true;
328 if ( errorDialog.
exec() == QDialog::Accepted )
333 mExecErrorsDialog =
false;
338 if ( mSocket->
state() != QAbstractSocket::ConnectedState )
339 socketStateChanged( mSocket->
state() );
342 void QgsAuthSslImportDialog::showCertificateInfo()
346 if ( !peerchain.isEmpty() )
358 void QgsAuthSslImportDialog::widgetReadyToSaveChanged(
bool cansave )
363 void QgsAuthSslImportDialog::checkCanSave()
365 saveButton()->
setEnabled( wdgtSslConfig->readyToSave() );
370 void QgsAuthSslImportDialog::radioServerImportToggled(
bool checked )
372 frameServerImport->setEnabled( checked );
373 clearStatusCertificateConfig();
376 void QgsAuthSslImportDialog::radioFileImportToggled(
bool checked )
378 frameFileImport->setEnabled( checked );
379 clearStatusCertificateConfig();
382 void QgsAuthSslImportDialog::on_btnCertPath_clicked()
384 const QString& fn = getOpenFileName(
tr(
"Open Server Certificate File" ),
tr(
"PEM (*.pem);;DER (*.der)" ) );
387 leCertPath->setText( fn );
392 void QgsAuthSslImportDialog::clearCertificateConfig()
394 wdgtSslConfig->resetSslCertConfig();
395 wdgtSslConfig->setEnabled(
false );
398 void QgsAuthSslImportDialog::clearStatusCertificateConfig()
401 pteSessionStatus->clear();
403 clearCertificateConfig();
406 void QgsAuthSslImportDialog::loadCertFromFile()
408 clearStatusCertificateConfig();
413 appendString(
tr(
"Could not load any certs from file" ) );
420 appendString(
tr(
"Could not load server cert from file" ) );
426 appendString(
tr(
"Certificate does not appear for be for an SSL server. " 427 "You can still add a configuration, if you know it is the correct certificate." ) );
430 wdgtSslConfig->setEnabled(
true );
431 wdgtSslConfig->setSslHost(
"" );
432 wdgtSslConfig->setSslCertificate( cert );
435 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
441 void QgsAuthSslImportDialog::appendString(
const QString &line )
444 cursor.movePosition( QTextCursor::End );
445 cursor.insertText( line +
'\n' );
451 return buttonBox->button( QDialogButtonBox::Save );
456 return buttonBox->button( QDialogButtonBox::Close );
471 settings.
setValue(
"UI/lastAuthImportSslOpenFileDir",
QFileInfo( f ).absoluteDir().path() );
QSslCertificate peerCertificate() const
~QgsAuthSslImportDialog()
void ignoreSslErrors(const QList< QSslError > &errors)
static QgsAuthManager * instance()
Enforce singleton pattern.
SocketState state() const
QString errorString() const
void startClientEncryption()
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Return list of concatenated certs from a PEM or DER formatted file.
QString tr(const char *sourceText, const char *disambiguation, int n)
QPixmap pixmap(const QSize &size, Mode mode, State state) const
void connectToHost(const QString &hostName, quint16 port, QFlags< QIODevice::OpenModeFlag > openMode)
void setValue(const QString &key, const QVariant &value)
QString fromUtf8(const char *str, int size)
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.
const QList< QSslCertificate > getTrustedCaCertsCache()
Get cache of trusted certificate authorities, ready for network connections.
QSslCipher sessionCipher() const
QList< QSslCertificate > peerCertificateChain() const
static bool certificateIsSslServer(const QSslCertificate &cert)
Get whether a certificate is probably used for a SSL server.
QString errorString() const
QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const
void disconnectFromHost()
static QString getSslProtocolName(QSsl::SslProtocol protocol)
SSL Protocol name strings per enum.
QVariant value(const QString &key, const QVariant &defaultValue) const
QSsl::SslProtocol protocol() const
void setCaCertificates(const QList< QSslCertificate > &certificates)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void accept() override
Overridden slot of base dialog.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const