18#include "moc_qgsauthimportidentitydialog.cpp"
19#include "ui_qgsauthimportidentitydialog.h"
37 , mIdentityType( CertIdentity )
45 mAuthNotifyLayout =
new QVBoxLayout;
46 this->setLayout( mAuthNotifyLayout );
48 mAuthNotifyLayout->addWidget( mAuthNotify );
53 connect( lePkiPathsKeyPass, &QLineEdit::textChanged,
this, &QgsAuthImportIdentityDialog::lePkiPathsKeyPass_textChanged );
54 connect( chkPkiPathsPassShow, &QCheckBox::stateChanged,
this, &QgsAuthImportIdentityDialog::chkPkiPathsPassShow_stateChanged );
55 connect( btnPkiPathsCert, &QToolButton::clicked,
this, &QgsAuthImportIdentityDialog::btnPkiPathsCert_clicked );
56 connect( btnPkiPathsKey, &QToolButton::clicked,
this, &QgsAuthImportIdentityDialog::btnPkiPathsKey_clicked );
57 connect( lePkiPkcs12KeyPass, &QLineEdit::textChanged,
this, &QgsAuthImportIdentityDialog::lePkiPkcs12KeyPass_textChanged );
58 connect( chkPkiPkcs12PassShow, &QCheckBox::stateChanged,
this, &QgsAuthImportIdentityDialog::chkPkiPkcs12PassShow_stateChanged );
59 connect( btnPkiPkcs12Bundle, &QToolButton::clicked,
this, &QgsAuthImportIdentityDialog::btnPkiPkcs12Bundle_clicked );
60 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QWidget::close );
61 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
63 mIdentityType = identitytype;
65 populateIdentityType();
82 return qMakePair( QSslCertificate(), QSslKey() );
87void QgsAuthImportIdentityDialog::populateIdentityType()
91 stkwBundleType->setVisible(
true );
93 cmbIdentityTypes->addItem( tr(
"PKI PEM/DER Certificate Paths" ),
95 cmbIdentityTypes->addItem( tr(
"PKI PKCS#12 Certificate Bundle" ),
98 connect( cmbIdentityTypes,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
99 stkwBundleType, &QStackedWidget::setCurrentIndex );
100 connect( stkwBundleType, &QStackedWidget::currentChanged,
101 cmbIdentityTypes, &QComboBox::setCurrentIndex );
103 connect( cmbIdentityTypes,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
104 this, [ = ] { validateIdentity(); } );
105 connect( stkwBundleType, &QStackedWidget::currentChanged,
106 this, &QgsAuthImportIdentityDialog::validateIdentity );
108 cmbIdentityTypes->setCurrentIndex( 0 );
109 stkwBundleType->setCurrentIndex( 0 );
114void QgsAuthImportIdentityDialog::validateIdentity()
119 ok = validateBundle();
121 okButton()->setEnabled( ok );
124bool QgsAuthImportIdentityDialog::validateBundle()
128 const QSslCertificate emptycert;
129 const QSslKey emptykey;
130 mCertBundle = qMakePair( emptycert, emptykey );
133 QWidget *curpage = stkwBundleType->currentWidget();
134 if ( curpage == pagePkiPaths )
136 return validatePkiPaths();
138 else if ( curpage == pagePkiPkcs12 )
140 return validatePkiPkcs12();
146void QgsAuthImportIdentityDialog::clearValidation()
148 teValidation->clear();
149 teValidation->setStyleSheet( QString() );
152void QgsAuthImportIdentityDialog::writeValidation(
const QString &msg,
162 txt = tr(
"Valid: %1" ).arg( msg );
166 txt = tr(
"Invalid: %1" ).arg( msg );
171 teValidation->setStyleSheet( ss );
174 teValidation->append( txt );
178 teValidation->setText( txt );
180 teValidation->moveCursor( QTextCursor::Start );
183void QgsAuthImportIdentityDialog::lePkiPathsKeyPass_textChanged(
const QString &pass )
189void QgsAuthImportIdentityDialog::chkPkiPathsPassShow_stateChanged(
int state )
191 lePkiPathsKeyPass->setEchoMode( ( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
194void QgsAuthImportIdentityDialog::btnPkiPathsCert_clicked()
196 const QString &fn = getOpenFileName( tr(
"Open Client Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
199 lePkiPathsCert->setText( fn );
204void QgsAuthImportIdentityDialog::btnPkiPathsKey_clicked()
206 const QString &fn = getOpenFileName( tr(
"Open Private Key File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
209 lePkiPathsKey->setText( fn );
214void QgsAuthImportIdentityDialog::lePkiPkcs12KeyPass_textChanged(
const QString &pass )
220void QgsAuthImportIdentityDialog::chkPkiPkcs12PassShow_stateChanged(
int state )
222 lePkiPkcs12KeyPass->setEchoMode( ( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
225void QgsAuthImportIdentityDialog::btnPkiPkcs12Bundle_clicked()
227 const QString &fn = getOpenFileName( tr(
"Open PKCS#12 Certificate Bundle" ), tr(
"PKCS#12 (*.p12 *.pfx)" ) );
230 lePkiPkcs12Bundle->setText( fn );
235bool QgsAuthImportIdentityDialog::validatePkiPaths()
237 bool isvalid =
false;
240 const QString certpath( lePkiPathsCert->text() );
241 const QString keypath( lePkiPathsKey->text() );
243 const bool certfound = QFile::exists( certpath );
244 const bool keyfound = QFile::exists( keypath );
246 fileFound( certpath.isEmpty() || certfound, lePkiPathsCert );
247 fileFound( keypath.isEmpty() || keyfound, lePkiPathsKey );
249 if ( !certfound || !keyfound )
251 writeValidation( tr(
"Missing components" ),
Invalid );
256 QSslCertificate clientcert;
258 QList<QSslCertificate> ca_certs;
259 if ( !certs.isEmpty() )
261 clientcert = certs.takeFirst();
265 writeValidation( tr(
"Failed to read client certificate from file" ),
Invalid );
269 if ( clientcert.isNull() )
271 writeValidation( tr(
"Failed to load client certificate from file" ),
Invalid );
275 if ( !certs.isEmpty() )
277 teValidation->append( tr(
"Extra certificates found with identity" ) );
283 const QDateTime startdate( clientcert.effectiveDate() );
284 const QDateTime enddate( clientcert.expiryDate() );
286 writeValidation( tr(
"%1 thru %2" ).arg( startdate.toString(), enddate.toString() ),
291 const QString keypass( lePkiPathsKeyPass->text() );
293 if ( clientkey.isNull() )
295 writeValidation( tr(
"Failed to load client private key from file" ),
Invalid,
true );
296 if ( !keypass.isEmpty() )
298 writeValidation( tr(
"Private key password may not match" ),
Invalid,
true );
305 mCertBundle = qMakePair( clientcert, clientkey );
314bool QgsAuthImportIdentityDialog::validatePkiPkcs12()
317 const QString bundlepath( lePkiPkcs12Bundle->text() );
318 const bool bundlefound = QFile::exists( bundlepath );
319 fileFound( bundlepath.isEmpty() || bundlefound, lePkiPkcs12Bundle );
323 writeValidation( tr(
"Missing components" ),
Invalid );
327 if ( !QCA::isSupported(
"pkcs12" ) )
329 writeValidation( tr(
"QCA library has no PKCS#12 support" ),
Invalid );
334 QCA::SecureArray passarray;
335 QString keypass = QString();
336 if ( !lePkiPkcs12KeyPass->text().isEmpty() )
338 passarray = QCA::SecureArray( lePkiPkcs12KeyPass->text().toUtf8() );
339 keypass = lePkiPkcs12KeyPass->text();
342 QCA::ConvertResult res;
343 const QCA::KeyBundle bundle( QCA::KeyBundle::fromFile( bundlepath, passarray, &res, QStringLiteral(
"qca-ossl" ) ) );
345 if ( res == QCA::ErrorFile )
347 writeValidation( tr(
"Failed to read bundle file" ),
Invalid );
350 else if ( res == QCA::ErrorPassphrase )
352 writeValidation( tr(
"Incorrect bundle password" ),
Invalid );
353 lePkiPkcs12KeyPass->setPlaceholderText( QStringLiteral(
"Required passphrase" ) );
356 else if ( res == QCA::ErrorDecode )
358 writeValidation( tr(
"Failed to decode (try entering password)" ),
Invalid );
362 if ( bundle.isNull() )
364 writeValidation( tr(
"Bundle empty or can not be loaded" ),
Invalid );
369 const QCA::Certificate cert( bundle.certificateChain().primary() );
372 writeValidation( tr(
"Bundle client cert can not be loaded" ),
Invalid );
377 const QDateTime startdate( cert.notValidBefore() );
378 const QDateTime enddate( cert.notValidAfter() );
379 const QDateTime now( QDateTime::currentDateTime() );
380 const bool bundlevalid = ( now >= startdate && now <= enddate );
382 writeValidation( tr(
"%1 thru %2" ).arg( startdate.toString(), enddate.toString() ),
387 QSslCertificate clientcert;
389 if ( !certs.isEmpty() )
391 clientcert = certs.first();
393 if ( clientcert.isNull() )
395 writeValidation( tr(
"Qt cert could not be created from QCA cert" ),
Invalid,
true );
399 clientkey = QSslKey( bundle.privateKey().toRSA().toPEM().toLatin1(), QSsl::Rsa );
400 if ( clientkey.isNull() )
402 writeValidation( tr(
"Qt private key could not be created from QCA key" ),
Invalid,
true );
406 const QCA::CertificateChain cert_chain( bundle.certificateChain() );
407 QList<QSslCertificate> ca_certs;
408 if ( cert_chain.size() > 1 )
410 const auto constCert_chain = cert_chain;
411 for (
const QCA::Certificate &ca_cert : constCert_chain )
413 if ( ca_cert != cert_chain.primary() )
415 ca_certs << QSslCertificate( ca_cert.toPEM().toLatin1() );
420 mCertBundle = qMakePair( clientcert, clientkey );
421 mPkiBundle =
QgsPkiBundle( clientcert, clientkey, ca_certs );
427void QgsAuthImportIdentityDialog::fileFound(
bool found, QWidget *widget )
432 widget->setToolTip( tr(
"File not found" ) );
436 widget->setStyleSheet( QString() );
437 widget->setToolTip( QString() );
441QString QgsAuthImportIdentityDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
444 const QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportBundleOpenFileDir" ), QDir::homePath() ).toString();
445 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
449 this->activateWindow();
453 settings.
setValue( QStringLiteral(
"UI/lastAuthImportBundleOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
458QPushButton *QgsAuthImportIdentityDialog::okButton()
460 return buttonBox->button( QDialogButtonBox::Ok );
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static bool certIsViable(const QSslCertificate &cert)
certIsViable checks for viability errors of cert and whether it is NULL
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Returns a list of concatenated certs from a PEM or DER formatted file.
static QSslKey keyFromFile(const QString &keypath, const QString &keypass=QString(), QString *algtype=nullptr)
Returns non-encrypted key from a PEM or DER formatted file.
static QList< QSslCertificate > certsFromString(const QString &pemtext)
Returns a list of concatenated certs from a PEM Base64 text block.
static bool certIsCurrent(const QSslCertificate &cert)
certIsCurrent checks if cert is viable for its not before and not after dates
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc. certificate.
QgsAuthImportIdentityDialog::IdentityType identityType()
Gets identity type.
const QPair< QSslCertificate, QSslKey > certBundleToImport()
Gets certificate/key bundle to be imported.
Validity
Type of certificate/bundle validity output.
IdentityType
Type of identity being imported.
QgsAuthImportIdentityDialog(QgsAuthImportIdentityDialog::IdentityType identitytype, QWidget *parent=nullptr)
Construct a dialog for importing identities.
Storage set for PKI bundle: SSL certificate, key, optional CA cert chain.
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.