Skip to content

Commit

Permalink
[auth] Also parse EC keys and get encoding from content
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 10, 2017
1 parent 87117a8 commit f52dbeb
Showing 1 changed file with 42 additions and 44 deletions.
86 changes: 42 additions & 44 deletions src/core/auth/qgsauthcertutils.cpp
Expand Up @@ -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();
}
Expand Down

0 comments on commit f52dbeb

Please sign in to comment.