Skip to content

Commit d293e8f

Browse files
committedOct 16, 2017
[auth] Add method to exclude self-signed CAs from a list of certificates
1 parent 032f225 commit d293e8f

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
 

‎python/core/auth/qgsauthcertutils.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ Return list of concatenated certs from a PEM Base64 text block
127127
:rtype: list of QSslCertificate
128128
%End
129129

130+
131+
static QList<QSslCertificate> casRemoveSelfSigned( const QList<QSslCertificate> &caList );
132+
%Docstring
133+
casRemoveSelfSigned remove self-signed CA certificates from ``caList``
134+
\param caList list of CA certificates
135+
:return: a list of non self-signed certificates
136+
:rtype: list of QSslCertificate
137+
%End
138+
130139
static QStringList certKeyBundleToPem( const QString &certpath,
131140
const QString &keypath,
132141
const QString &keypass = QString(),

‎src/core/auth/qgsauthcertutils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ QList<QSslCertificate> QgsAuthCertUtils::certsFromString( const QString &pemtext
224224
return certs;
225225
}
226226

227+
QList<QSslCertificate> QgsAuthCertUtils::casRemoveSelfSigned( const QList<QSslCertificate> &caList )
228+
{
229+
QList<QSslCertificate> certs;
230+
for ( const auto cert : caList )
231+
{
232+
if ( ! cert.isSelfSigned( ) )
233+
{
234+
certs.append( cert );
235+
}
236+
}
237+
return certs;
238+
}
239+
227240
QStringList QgsAuthCertUtils::certKeyBundleToPem( const QString &certpath,
228241
const QString &keypath,
229242
const QString &keypass,

‎src/core/auth/qgsauthcertutils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ class CORE_EXPORT QgsAuthCertUtils
136136
//! Return list of concatenated certs from a PEM Base64 text block
137137
static QList<QSslCertificate> certsFromString( const QString &pemtext );
138138

139+
140+
/**
141+
* \brief casRemoveSelfSigned remove self-signed CA certificates from \a caList
142+
* \param caList list of CA certificates
143+
* \return a list of non self-signed certificates
144+
*/
145+
static QList<QSslCertificate> casRemoveSelfSigned( const QList<QSslCertificate> &caList );
146+
139147
/**
140148
* Return list of certificate, private key and algorithm (as PEM text) from file path components
141149
* \param certpath File path to certificate

‎tests/src/python/test_qgsauthsystem.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,21 @@ def test_130_cas_merge(self):
603603

604604
self.assertTrue(trusted[0] in merged)
605605

606+
def test_140_cas_remove_self_signed(self):
607+
"""Test CAs merge """
608+
extra_path = PKIDATA + '/alice-cert_w-chain.pem'
609+
610+
extra = QgsAuthCertUtils.casFromFile(extra_path)
611+
filtered = QgsAuthCertUtils.casRemoveSelfSigned(extra)
612+
613+
self.assertEqual(len(filtered), 1)
614+
self.assertEqual(len(extra), 2)
615+
616+
self.assertTrue(extra[1].isSelfSigned())
617+
618+
for c in filtered:
619+
self.assertFalse(c.isSelfSigned())
620+
606621

607622
if __name__ == '__main__':
608623
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.