18 #include "ui_qgsauthimportidentitydialog.h" 21 #include <QFileDialog> 22 #include <QPushButton> 40 openflags |= QIODevice::Text;
41 bool ret = file.
open( openflags );
55 , mIdentityType( CertIdentity )
58 , mAuthNotifyLayout( nullptr )
59 , mAuthNotify( nullptr )
67 mAuthNotifyLayout->
addWidget( mAuthNotify );
75 mIdentityType = identitytype;
77 populateIdentityType();
103 void QgsAuthImportIdentityDialog::populateIdentityType()
107 stkwBundleType->setVisible(
true );
109 cmbIdentityTypes->addItem(
tr(
"PKI PEM/DER Certificate Paths" ),
111 cmbIdentityTypes->addItem(
tr(
"PKI PKCS#12 Certificate Bundle" ),
114 connect( cmbIdentityTypes, SIGNAL( currentIndexChanged(
int ) ),
115 stkwBundleType, SLOT( setCurrentIndex(
int ) ) );
116 connect( stkwBundleType, SIGNAL( currentChanged(
int ) ),
117 cmbIdentityTypes, SLOT( setCurrentIndex(
int ) ) );
119 connect( cmbIdentityTypes, SIGNAL( currentIndexChanged(
int ) ),
120 this, SLOT( validateIdentity() ) );
121 connect( stkwBundleType, SIGNAL( currentChanged(
int ) ),
122 this, SLOT( validateIdentity() ) );
124 cmbIdentityTypes->setCurrentIndex( 0 );
125 stkwBundleType->setCurrentIndex( 0 );
130 void QgsAuthImportIdentityDialog::validateIdentity()
135 ok = validateBundle();
140 bool QgsAuthImportIdentityDialog::validateBundle()
146 mCertBundle = qMakePair( emptycert, emptykey );
149 QWidget *curpage = stkwBundleType->currentWidget();
150 if ( curpage == pagePkiPaths )
152 return validatePkiPaths();
154 else if ( curpage == pagePkiPkcs12 )
156 return validatePkiPkcs12();
162 void QgsAuthImportIdentityDialog::clearValidation()
164 teValidation->clear();
165 teValidation->setStyleSheet(
"" );
168 void QgsAuthImportIdentityDialog::writeValidation(
const QString &msg,
178 txt =
tr(
"Valid: %1" ).
arg( msg );
182 txt =
tr(
"Invalid: %1" ).
arg( msg );
189 teValidation->setStyleSheet( ss );
192 teValidation->
append( txt );
196 teValidation->setText( txt );
198 teValidation->moveCursor( QTextCursor::Start );
201 void QgsAuthImportIdentityDialog::on_lePkiPathsKeyPass_textChanged(
const QString &pass )
207 void QgsAuthImportIdentityDialog::on_chkPkiPathsPassShow_stateChanged(
int state )
209 lePkiPathsKeyPass->setEchoMode(( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
212 void QgsAuthImportIdentityDialog::on_btnPkiPathsCert_clicked()
214 const QString& fn = getOpenFileName(
tr(
"Open Client Certificate File" ),
tr(
"PEM (*.pem);;DER (*.der)" ) );
217 lePkiPathsCert->setText( fn );
222 void QgsAuthImportIdentityDialog::on_btnPkiPathsKey_clicked()
224 const QString& fn = getOpenFileName(
tr(
"Open Private Key File" ),
tr(
"PEM (*.pem);;DER (*.der)" ) );
227 lePkiPathsKey->setText( fn );
232 void QgsAuthImportIdentityDialog::on_lePkiPkcs12KeyPass_textChanged(
const QString &pass )
238 void QgsAuthImportIdentityDialog::on_chkPkiPkcs12PassShow_stateChanged(
int state )
240 lePkiPkcs12KeyPass->setEchoMode(( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
243 void QgsAuthImportIdentityDialog::on_btnPkiPkcs12Bundle_clicked()
245 const QString& fn = getOpenFileName(
tr(
"Open PKCS#12 Certificate Bundle" ),
tr(
"PKCS#12 (*.p12 *.pfx)" ) );
248 lePkiPkcs12Bundle->setText( fn );
253 bool QgsAuthImportIdentityDialog::validatePkiPaths()
255 bool isvalid =
false;
258 QString certpath( lePkiPathsCert->text() );
259 QString keypath( lePkiPathsKey->text() );
264 fileFound( certpath.isEmpty() || certfound, lePkiPathsCert );
265 fileFound( keypath.isEmpty() || keyfound, lePkiPathsKey );
267 if ( !certfound || !keyfound )
269 writeValidation(
tr(
"Missing components" ),
Invalid );
283 writeValidation(
tr(
"Failed to read client certificate from file" ),
Invalid );
287 if ( clientcert.
isNull() )
289 writeValidation(
tr(
"Failed to load client certificate from file" ),
Invalid );
295 teValidation->append(
tr(
"Extra certificates found with identity" ) );
299 isvalid = clientcert.
isValid();
303 writeValidation(
tr(
"%1 thru %2" ).arg( startdate.toString(), enddate.toString() ),
308 bool keypem = keypath.endsWith(
".pem", Qt::CaseInsensitive );
312 QString keypass = lePkiPathsKeyPass->text();
315 keypem ? QSsl::Pem : QSsl::Der,
323 keypem ? QSsl::Pem : QSsl::Der,
330 writeValidation(
tr(
"Failed to load client private key from file" ), Invalid,
true );
333 writeValidation(
tr(
"Private key password may not match" ), Invalid,
true );
339 isvalid = isvalid &&
true;
344 mCertBundle = qMakePair( clientcert, clientkey );
353 bool QgsAuthImportIdentityDialog::validatePkiPkcs12()
356 QString bundlepath( lePkiPkcs12Bundle->text() );
358 fileFound( bundlepath.isEmpty() || bundlefound, lePkiPkcs12Bundle );
362 writeValidation(
tr(
"Missing components" ),
Invalid );
366 if ( !QCA::isSupported(
"pkcs12" ) )
368 writeValidation(
tr(
"QCA library has no PKCS#12 support" ),
Invalid );
373 QCA::SecureArray passarray;
374 QString keypass = QString::null;
375 if ( !lePkiPkcs12KeyPass->text().isEmpty() )
377 passarray = QCA::SecureArray( lePkiPkcs12KeyPass->text().toUtf8() );
378 keypass = lePkiPkcs12KeyPass->text();
381 QCA::ConvertResult res;
382 QCA::KeyBundle bundle( QCA::KeyBundle::fromFile( bundlepath, passarray, &res,
QString(
"qca-ossl" ) ) );
384 if ( res == QCA::ErrorFile )
386 writeValidation(
tr(
"Failed to read bundle file" ),
Invalid );
389 else if ( res == QCA::ErrorPassphrase )
391 writeValidation(
tr(
"Incorrect bundle password" ),
Invalid );
392 lePkiPkcs12KeyPass->setPlaceholderText(
QString(
"Required passphrase" ) );
395 else if ( res == QCA::ErrorDecode )
397 writeValidation(
tr(
"Failed to decode (try entering password)" ),
Invalid );
401 if ( bundle.isNull() )
403 writeValidation(
tr(
"Bundle empty or can not be loaded" ),
Invalid );
408 QCA::Certificate cert( bundle.certificateChain().primary() );
411 writeValidation(
tr(
"Bundle client cert can not be loaded" ),
Invalid );
416 QDateTime startdate( cert.notValidBefore() );
417 QDateTime enddate( cert.notValidAfter() );
419 bool bundlevalid = ( now >= startdate && now <= enddate );
421 writeValidation(
tr(
"%1 thru %2" ).arg( startdate.toString(), enddate.toString() ),
430 clientcert = certs.
first();
432 if ( clientcert.
isNull() )
434 writeValidation(
tr(
"Qt cert could not be created from QCA cert" ), Invalid,
true );
438 clientkey =
QSslKey( bundle.privateKey().toRSA().toPEM().toAscii(), QSsl::Rsa );
441 writeValidation(
tr(
"Qt private key could not be created from QCA key" ), Invalid,
true );
445 QCA::CertificateChain cert_chain( bundle.certificateChain() );
447 if ( cert_chain.size() > 1 )
449 Q_FOREACH (
const QCA::Certificate& ca_cert, cert_chain )
451 if ( ca_cert != cert_chain.primary() )
458 mCertBundle = qMakePair( clientcert, clientkey );
459 mPkiBundle =
QgsPkiBundle( clientcert, clientkey, ca_certs );
465 void QgsAuthImportIdentityDialog::fileFound(
bool found,
QWidget *widget )
479 QString QgsAuthImportIdentityDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
491 settings.
setValue(
"UI/lastAuthImportBundleOpenFileDir",
QFileInfo( f ).absoluteDir().path() );
496 QPushButton *QgsAuthImportIdentityDialog::okButton()
498 return buttonBox->button( QDialogButtonBox::Ok );
QDateTime effectiveDate() const
QString & append(QChar ch)
static QByteArray fileData_(const QString &path, bool astext=false)
IdentityType
Type of identity being imported.
static QgsAuthManager * instance()
Enforce singleton pattern.
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Return list of concatenated certs from a PEM or DER formatted file.
QgsAuthImportIdentityDialog::IdentityType identityType()
Get identity type.
QString tr(const char *sourceText, const char *disambiguation, int n)
Storage set for PKI bundle: SSL certificate, key, optional CA cert chain.
void setValue(const QString &key, const QVariant &value)
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc.
QgsAuthImportIdentityDialog(QgsAuthImportIdentityDialog::IdentityType identitytype, QWidget *parent=nullptr)
Construct a dialog for importing identities.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
static QList< QSslCertificate > certsFromString(const QString &pemtext)
Return list of concatenated certs from a PEM Base64 text block.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc.
QVariant value(const QString &key, const QVariant &defaultValue) const
QDateTime currentDateTime()
QDateTime expiryDate() const
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
~QgsAuthImportIdentityDialog()
const QPair< QSslCertificate, QSslKey > certBundleToImport()
Get certificate/key bundle to be imported.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Validity
Type of certificate/bundle validity output.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QByteArray toUtf8() const