Skip to content

Commit 2a8ea5d

Browse files
committed
[auth] Simplify code and add a *.* filter for certs and keys selection
1 parent b1eb6c0 commit 2a8ea5d

File tree

4 files changed

+9
-54
lines changed

4 files changed

+9
-54
lines changed

src/auth/pkipaths/qgsauthpkipathsedit.cpp

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,7 @@ bool QgsAuthPkiPathsEdit::validateConfig()
6363
}
6464

6565
// check for issue date validity, then notify status
66-
QSslCertificate cert;
67-
QFile file( certpath );
68-
QFileInfo fileinfo( file );
69-
QString ext( fileinfo.fileName().remove( fileinfo.completeBaseName() ).toLower() );
70-
if ( ext.isEmpty() )
71-
{
72-
writePkiMessage( lePkiPathsMsg, tr( "Certificate file has no extension" ), Invalid );
73-
return validityChange( false );
74-
}
75-
76-
QFile::OpenMode openflags( QIODevice::ReadOnly );
77-
QSsl::EncodingFormat encformat( QSsl::Der );
78-
if ( ext == QLatin1String( ".pem" ) )
79-
{
80-
openflags |= QIODevice::Text;
81-
encformat = QSsl::Pem;
82-
}
83-
84-
if ( file.open( openflags ) )
85-
{
86-
cert = QSslCertificate( file.readAll(), encformat );
87-
file.close();
88-
}
89-
else
90-
{
91-
writePkiMessage( lePkiPathsMsg, tr( "Failed to read certificate file" ), Invalid );
92-
return validityChange( false );
93-
}
66+
QSslCertificate cert( QgsAuthCertUtils::certFromFile( certpath ) );
9467

9568
if ( cert.isNull() )
9669
{
@@ -212,7 +185,7 @@ void QgsAuthPkiPathsEdit::chkPkiPathsPassShow_stateChanged( int state )
212185
void QgsAuthPkiPathsEdit::btnPkiPathsCert_clicked()
213186
{
214187
const QString &fn = QgsAuthGuiUtils::getOpenFileName( this, tr( "Open Client Certificate File" ),
215-
tr( "PEM (*.pem);;DER (*.der)" ) );
188+
tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
216189
if ( !fn.isEmpty() )
217190
{
218191
lePkiPathsCert->setText( fn );
@@ -223,7 +196,7 @@ void QgsAuthPkiPathsEdit::btnPkiPathsCert_clicked()
223196
void QgsAuthPkiPathsEdit::btnPkiPathsKey_clicked()
224197
{
225198
const QString &fn = QgsAuthGuiUtils::getOpenFileName( this, tr( "Open Private Key File" ),
226-
tr( "PEM (*.pem);;DER (*.der)" ) );
199+
tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
227200
if ( !fn.isEmpty() )
228201
{
229202
lePkiPathsKey->setText( fn );

src/gui/auth/qgsauthimportcertdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void QgsAuthImportCertDialog::validateCertificates()
224224

225225
void QgsAuthImportCertDialog::btnImportFile_clicked()
226226
{
227-
const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "PEM (*.pem);;DER (*.der)" ) );
227+
const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
228228
if ( !fn.isEmpty() )
229229
{
230230
leImportFile->setText( fn );

src/gui/auth/qgsauthimportidentitydialog.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void QgsAuthImportIdentityDialog::chkPkiPathsPassShow_stateChanged( int state )
192192

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

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

289289
// check for valid private key and that any supplied password works
290-
bool keypem = keypath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive );
291-
QByteArray keydata( QgsAuthCertUtils::fileData( keypath ) );
292-
293-
QSslKey clientkey;
294-
QString keypass = lePkiPathsKeyPass->text();
295-
clientkey = QSslKey( keydata,
296-
QSsl::Rsa,
297-
keypem ? QSsl::Pem : QSsl::Der,
298-
QSsl::PrivateKey,
299-
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
300-
if ( clientkey.isNull() )
301-
{
302-
// try DSA algorithm, since Qt can't seem to determine it otherwise
303-
clientkey = QSslKey( keydata,
304-
QSsl::Dsa,
305-
keypem ? QSsl::Pem : QSsl::Der,
306-
QSsl::PrivateKey,
307-
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
308-
}
309-
290+
QString keypass( lePkiPathsKeyPass->text() );
291+
QSslKey clientkey( QgsAuthCertUtils::keyFromFile( keypath, keypass ) );
310292
if ( clientkey.isNull() )
311293
{
312294
writeValidation( tr( "Failed to load client private key from file" ), Invalid, true );

src/gui/auth/qgsauthsslimportdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ void QgsAuthSslImportDialog::radioFileImportToggled( bool checked )
369369

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

0 commit comments

Comments
 (0)