Skip to content
Permalink
Browse files

[auth] Also parse EC keys and get encoding from content

  • Loading branch information
elpaso committed Nov 10, 2017
1 parent 87117a8 commit f52dbeba630b25643a98ace822f769ed54cc7bf6
Showing with 42 additions and 44 deletions.
  1. +42 −44 src/core/auth/qgsauthcertutils.cpp
@@ -191,51 +191,49 @@ QSslKey QgsAuthCertUtils::keyFromFile( const QString &keypath,
{
// The approach here is to try all possible encodings and algorithms
QByteArray keydata( QgsAuthCertUtils::fileData( keypath ) );

QSslKey clientkey;
clientkey = QSslKey( keydata,
QSsl::Rsa,
QSsl::Pem,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
if ( ! clientkey.isNull() )
{
if ( algtype )
*algtype = QStringLiteral( "rsa" );
return clientkey;
}
clientkey = QSslKey( keydata,
QSsl::Dsa,
QSsl::Pem,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
if ( ! clientkey.isNull() )
{
if ( algtype )
*algtype = QStringLiteral( "dsa" );
return clientkey;
}
clientkey = QSslKey( keydata,
QSsl::Rsa,
QSsl::Der,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
if ( ! clientkey.isNull() )
{
if ( algtype )
*algtype = QStringLiteral( "rsa" );
return clientkey;
}
clientkey = QSslKey( keydata,
QSsl::Dsa,
QSsl::Der,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
if ( ! clientkey.isNull() )
{
if ( algtype )
*algtype = QStringLiteral( "dsa" );
return clientkey;

QSsl::EncodingFormat keyEncoding( keydata.contains( QByteArrayLiteral( "-----BEGIN " ) ) ?
QSsl::Pem :
QSsl::Der );

const std::vector<QSsl::KeyAlgorithm> algs
{
QSsl::KeyAlgorithm::Rsa,
QSsl::KeyAlgorithm::Dsa,
QSsl::KeyAlgorithm::Ec,
QSsl::KeyAlgorithm::Opaque
};

for ( const auto &alg : algs )
{
clientkey = QSslKey( keydata,
alg,
keyEncoding,
QSsl::PrivateKey,
!keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
if ( ! clientkey.isNull() )
{
if ( algtype )
{
switch ( alg )
{
case QSsl::KeyAlgorithm::Rsa:
*algtype = QStringLiteral( "rsa" );
break;
case QSsl::KeyAlgorithm::Dsa:
*algtype = QStringLiteral( "dsa" );
break;
case QSsl::KeyAlgorithm::Ec:
*algtype = QStringLiteral( "ec" );
break;
case QSsl::KeyAlgorithm::Opaque:
*algtype = QStringLiteral( "opaque" );
break;
}
}
return clientkey;
}
}
return QSslKey();
}

0 comments on commit f52dbeb

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