Skip to content

Commit 368b0df

Browse files
committedOct 26, 2017
[auth] Add certificate chain validation routine
1 parent e20e076 commit 368b0df

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
 

‎python/core/auth/qgsauthcertutils.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ Get short strings describing an SSL error
275275
%End
276276

277277

278+
static QList<QSslError> validateCertChain( const QList<QSslCertificate> &certificateChain, const QString &hostName = QString(), bool addRootCa = false ) ;
279+
%Docstring
280+
validateCertChain validates the given ``certificateChain``
281+
\param certificateChain list of certificates to be checked, with leaf first and with optional root CA last
282+
\param addRootCa if true the CA will be added to the trusted CAs for this validation check
283+
:return: list of QSslError, if the list is empty then the cert chain is valid
284+
:rtype: list of QSslError
285+
%End
286+
278287
};
279288

280289
/************************************************************************

‎src/core/auth/qgsauthcertutils.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,25 @@ QList<QPair<QSslError::SslError, QString> > QgsAuthCertUtils::sslErrorEnumString
10181018
QgsAuthCertUtils::sslErrorEnumString( QSslError::CertificateBlacklisted ) );
10191019
return errenums;
10201020
}
1021+
1022+
QList<QSslError> QgsAuthCertUtils::validateCertChain( const QList<QSslCertificate> &certificateChain, const QString &hostName, bool addRootCa )
1023+
{
1024+
QList<QSslError> results;
1025+
// Merge in the root CA if present and asked for
1026+
if ( addRootCa && certificateChain.count() > 1 && certificateChain.last().isSelfSigned() )
1027+
{
1028+
static QMutex sMutex;
1029+
QMutexLocker lock( &sMutex );
1030+
QSslConfiguration oldSslConfig( QSslConfiguration::defaultConfiguration() );
1031+
QSslConfiguration sslConfig( oldSslConfig );
1032+
sslConfig.setCaCertificates( casMerge( sslConfig.caCertificates(), QList<QSslCertificate>() << certificateChain.last() ) );
1033+
QSslConfiguration::setDefaultConfiguration( sslConfig );
1034+
results = QSslCertificate::verify( certificateChain, hostName );
1035+
QSslConfiguration::setDefaultConfiguration( oldSslConfig );
1036+
}
1037+
else
1038+
{
1039+
results = QSslCertificate::verify( certificateChain, hostName );
1040+
}
1041+
return results;
1042+
}

‎src/core/auth/qgsauthcertutils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ class CORE_EXPORT QgsAuthCertUtils
296296
*/
297297
static QList<QPair<QSslError::SslError, QString> > sslErrorEnumStrings() SIP_SKIP;
298298

299+
/**
300+
* \brief validateCertChain validates the given \a certificateChain
301+
* \param certificateChain list of certificates to be checked, with leaf first and with optional root CA last
302+
* \param addRootCa if true the CA will be added to the trusted CAs for this validation check
303+
* \return list of QSslError, if the list is empty then the cert chain is valid
304+
*/
305+
static QList<QSslError> validateCertChain( const QList<QSslCertificate> &certificateChain, const QString &hostName = QString(), bool addRootCa = false ) ;
306+
299307
private:
300308
static void appendDirSegment_( QStringList &dirname, const QString &segment, QString value );
301309
};

0 commit comments

Comments
 (0)
Please sign in to comment.