Skip to content

Commit

Permalink
[auth] Moved the PEM/DER sniffing to a common private function
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 14, 2017
1 parent c09c96b commit b1eb6c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/core/auth/qgsauthcertutils.cpp
Expand Up @@ -125,8 +125,8 @@ QByteArray QgsAuthCertUtils::fileData( const QString &path )
QList<QSslCertificate> QgsAuthCertUtils::certsFromFile( const QString &certspath )
{
QList<QSslCertificate> certs;
bool pem = certspath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive );
certs = QSslCertificate::fromData( QgsAuthCertUtils::fileData( certspath ), pem ? QSsl::Pem : QSsl::Der );
const QByteArray payload( QgsAuthCertUtils::fileData( certspath ) );
certs = QSslCertificate::fromData( payload, sniffEncoding( payload ) );
if ( certs.isEmpty() )
{
QgsDebugMsg( QString( "Parsed cert(s) EMPTY for path: %1" ).arg( certspath ) );
Expand Down Expand Up @@ -193,9 +193,7 @@ QSslKey QgsAuthCertUtils::keyFromFile( const QString &keypath,
QByteArray keydata( QgsAuthCertUtils::fileData( keypath ) );
QSslKey clientkey;

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

const std::vector<QSsl::KeyAlgorithm> algs
{
Expand Down Expand Up @@ -652,6 +650,13 @@ void QgsAuthCertUtils::appendDirSegment_( QStringList &dirname,
}
}

QSsl::EncodingFormat QgsAuthCertUtils::sniffEncoding( const QByteArray &payload )
{
return payload.contains( QByteArrayLiteral( "-----BEGIN " ) ) ?
QSsl::Pem :
QSsl::Der;
}

QString QgsAuthCertUtils::getCertDistinguishedName( const QSslCertificate &qcert,
const QCA::Certificate &acert,
bool issuer )
Expand Down
2 changes: 2 additions & 0 deletions src/core/auth/qgsauthcertutils.h
Expand Up @@ -373,6 +373,8 @@ class CORE_EXPORT QgsAuthCertUtils

private:
static void appendDirSegment_( QStringList &dirname, const QString &segment, QString value );

static QSsl::EncodingFormat sniffEncoding( const QByteArray &payload );
};

#endif // QGSAUTHCERTUTILS_H

0 comments on commit b1eb6c0

Please sign in to comment.