Skip to content

Commit

Permalink
[auth] Simplify code and add a *.* filter for certs and keys selection
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 15, 2017
1 parent b1eb6c0 commit 2a8ea5d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 54 deletions.
33 changes: 3 additions & 30 deletions src/auth/pkipaths/qgsauthpkipathsedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/auth/qgsauthimportcertdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
26 changes: 4 additions & 22 deletions src/gui/auth/qgsauthimportidentitydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/auth/qgsauthsslimportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 2a8ea5d

Please sign in to comment.