Skip to content
Permalink
Browse files

[auth] Simplify code and add a *.* filter for certs and keys selection

  • Loading branch information
elpaso committed Nov 15, 2017
1 parent b1eb6c0 commit 2a8ea5dc30baa6d6d6a5f1634c07b2ed912c5607
@@ -63,34 +63,7 @@ bool QgsAuthPkiPathsEdit::validateConfig()
}

// check for issue date validity, then notify status
QSslCertificate cert;
QFile file( certpath );
QFileInfo fileinfo( file );
QString ext( fileinfo.fileName().remove( fileinfo.completeBaseName() ).toLower() );
if ( ext.isEmpty() )
{
writePkiMessage( lePkiPathsMsg, tr( "Certificate file has no extension" ), Invalid );
return validityChange( false );
}

QFile::OpenMode openflags( QIODevice::ReadOnly );
QSsl::EncodingFormat encformat( QSsl::Der );
if ( ext == QLatin1String( ".pem" ) )
{
openflags |= QIODevice::Text;
encformat = QSsl::Pem;
}

if ( file.open( openflags ) )
{
cert = QSslCertificate( file.readAll(), encformat );
file.close();
}
else
{
writePkiMessage( lePkiPathsMsg, tr( "Failed to read certificate file" ), Invalid );
return validityChange( false );
}
QSslCertificate cert( QgsAuthCertUtils::certFromFile( certpath ) );

if ( cert.isNull() )
{
@@ -212,7 +185,7 @@ void QgsAuthPkiPathsEdit::chkPkiPathsPassShow_stateChanged( int state )
void QgsAuthPkiPathsEdit::btnPkiPathsCert_clicked()
{
const QString &fn = QgsAuthGuiUtils::getOpenFileName( this, tr( "Open Client Certificate File" ),
tr( "PEM (*.pem);;DER (*.der)" ) );
tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
if ( !fn.isEmpty() )
{
lePkiPathsCert->setText( fn );
@@ -223,7 +196,7 @@ void QgsAuthPkiPathsEdit::btnPkiPathsCert_clicked()
void QgsAuthPkiPathsEdit::btnPkiPathsKey_clicked()
{
const QString &fn = QgsAuthGuiUtils::getOpenFileName( this, tr( "Open Private Key File" ),
tr( "PEM (*.pem);;DER (*.der)" ) );
tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
if ( !fn.isEmpty() )
{
lePkiPathsKey->setText( fn );
@@ -224,7 +224,7 @@ void QgsAuthImportCertDialog::validateCertificates()

void QgsAuthImportCertDialog::btnImportFile_clicked()
{
const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "PEM (*.pem);;DER (*.der)" ) );
const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
if ( !fn.isEmpty() )
{
leImportFile->setText( fn );
@@ -192,7 +192,7 @@ void QgsAuthImportIdentityDialog::chkPkiPathsPassShow_stateChanged( int state )

void QgsAuthImportIdentityDialog::btnPkiPathsCert_clicked()
{
const QString &fn = getOpenFileName( tr( "Open Client Certificate File" ), tr( "PEM (*.pem);;DER (*.der)" ) );
const QString &fn = getOpenFileName( tr( "Open Client Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
if ( !fn.isEmpty() )
{
lePkiPathsCert->setText( fn );
@@ -202,7 +202,7 @@ void QgsAuthImportIdentityDialog::btnPkiPathsCert_clicked()

void QgsAuthImportIdentityDialog::btnPkiPathsKey_clicked()
{
const QString &fn = getOpenFileName( tr( "Open Private Key File" ), tr( "PEM (*.pem);;DER (*.der)" ) );
const QString &fn = getOpenFileName( tr( "Open Private Key File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
if ( !fn.isEmpty() )
{
lePkiPathsKey->setText( fn );
@@ -287,26 +287,8 @@ bool QgsAuthImportIdentityDialog::validatePkiPaths()
//TODO: set enabled on cert info button, relative to cert validity

// check for valid private key and that any supplied password works
bool keypem = keypath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive );
QByteArray keydata( QgsAuthCertUtils::fileData( keypath ) );

QSslKey clientkey;
QString keypass = lePkiPathsKeyPass->text();
clientkey = QSslKey( keydata,
QSsl::Rsa,
keypem ? QSsl::Pem : QSsl::Der,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
if ( clientkey.isNull() )
{
// try DSA algorithm, since Qt can't seem to determine it otherwise
clientkey = QSslKey( keydata,
QSsl::Dsa,
keypem ? QSsl::Pem : QSsl::Der,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
}

QString keypass( lePkiPathsKeyPass->text() );
QSslKey clientkey( QgsAuthCertUtils::keyFromFile( keypath, keypass ) );
if ( clientkey.isNull() )
{
writeValidation( tr( "Failed to load client private key from file" ), Invalid, true );
@@ -369,7 +369,7 @@ void QgsAuthSslImportDialog::radioFileImportToggled( bool checked )

void QgsAuthSslImportDialog::btnCertPath_clicked()
{
const QString &fn = getOpenFileName( tr( "Open Server Certificate File" ), tr( "PEM (*.pem);;DER (*.der)" ) );
const QString &fn = getOpenFileName( tr( "Open Server Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
if ( !fn.isEmpty() )
{
leCertPath->setText( fn );

0 comments on commit 2a8ea5d

Please sign in to comment.
You can’t perform that action at this time.