Move the PKCS#12 support from QSslSocket to QSslCertificate.

Discussed with Peter and agreed that it's a slightly better fit there.

Change-Id: If8db777336e2273670a23d75d8542b30c07e0d7b
Reviewed-by: Daniel Molkentin <daniel@molkentin.de>
Reviewed-by: Peter Hartmann <phartmann@blackberry.com>
This commit is contained in:
Richard J. Moore 2014-05-12 21:16:20 +01:00 committed by The Qt Project
parent b861c43395
commit 1a8788d966
13 changed files with 149 additions and 74 deletions

View File

@ -979,6 +979,26 @@ QList<QSslError> QSslCertificate::verify(QList<QSslCertificate> certificateChain
return QSslSocketBackendPrivate::verify(certificateChain, hostName);
}
/*!
\since 5.4
Imports a PKCS#12 (pfx) file from the specified \a device. A PKCS#12
file is a bundle that can contain a number of certificates and keys.
This method reads a single \a key, its \a certificate and any
associated \a caCertificates from the bundle. If a \a passPhrase is
specified then this will be used to decrypt the bundle. Returns
\c true if the PKCS#12 file was successfully loaded.
\note The \a device must be open and ready to be read from.
*/
bool QSslCertificate::importPKCS12(QIODevice *device,
QSslKey *key, QSslCertificate *certificate,
QList<QSslCertificate> *caCertificates,
const QByteArray &passPhrase)
{
return QSslSocketBackendPrivate::importPKCS12(device, key, certificate, caCertificates, passPhrase);
}
void QSslCertificatePrivate::init(const QByteArray &data, QSsl::EncodingFormat format)
{
if (!data.isEmpty()) {

View File

@ -142,6 +142,11 @@ public:
static QList<QSslError> verify(QList<QSslCertificate> certificateChain, const QString &hostName = QString());
static bool importPKCS12(QIODevice *device,
QSslKey *key, QSslCertificate *cert,
QList<QSslCertificate> *caCertificates=0,
const QByteArray &passPhrase=QByteArray());
Qt::HANDLE handle() const;
private:

View File

@ -1455,26 +1455,6 @@ QList<QSslCertificate> QSslSocket::systemCaCertificates()
return QSslSocketPrivate::systemCaCertificates();
}
/*!
\since 5.4
Imports a PKCS#12 (pfx) file from the specified \a device. A PKCS#12
file is a bundle that can contain a number of certificates and keys.
This method reads a single \a key, it's \a certificate and any
associated \a caCertificates from the bundle. If a \a passPhrase is
specified then this will be used to decrypt the bundle. Returns
\c true if the PKCS#12 file was successfully loaded.
\note The \a device must be open and ready to be read from.
*/
bool QSslSocket::importPKCS12(QIODevice *device,
QSslKey *key, QSslCertificate *certificate,
QList<QSslCertificate> *caCertificates,
const QByteArray &passPhrase)
{
return QSslSocketBackendPrivate::importPKCS12(device, key, certificate, caCertificates, passPhrase);
}
/*!
Waits until the socket is connected, or \a msecs milliseconds,
whichever happens first. If the connection has been established,

View File

@ -172,11 +172,6 @@ public:
static QList<QSslCertificate> defaultCaCertificates();
static QList<QSslCertificate> systemCaCertificates();
static bool importPKCS12(QIODevice *device,
QSslKey *key, QSslCertificate *cert,
QList<QSslCertificate> *caCertificates=0,
const QByteArray &passPhrase=QByteArray());
bool waitForConnected(int msecs = 30000);
bool waitForEncrypted(int msecs = 30000);
bool waitForReadyRead(int msecs = 30000);

View File

@ -1,4 +1,5 @@
The PKCS#12 bundle was created by running:
The PKCS#12 bundle was created by running the following on
in the qsslsocket/certs directory:
openssl pkcs12 -export -in leaf.crt -inkey leaf.key \
-out leaf.p12 \

View File

@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDvDCCAnSgAwIBAgIQO+uZxerYC10Ll11PBnVL4TANBgkqhkiG9w0BAQUFADA8
MQswCQYDVQQGEwJHQjEZMBcGA1UEChMQV2VzdHBvaW50IENBIEtleTESMBAGA1UE
ChMJV2VzdHBvaW50MB4XDTEzMDIxNjE2NTMwOFoXDTIzMDIxNjE2NTMwOFowMjEL
MAkGA1UEBhMCR0IxIzAhBgNVBAoTGldlc3Rwb2ludCBJbnRlcm1lZGlhdGUgS2V5
MIIBUjANBgkqhkiG9w0BAQEFAAOCAT8AMIIBOgKCATEAsR4tRskg2IFfQFMfGBJ1
eqlrNejANw0oM6k5HlEB8uFA9qeyAzmflwQUPoJ55KRQ/gVHTOBdWrtgGgPMiekF
1Q36Ry1elwbAl4a+LZ6qsc9ASipvk8HirKpt1v5L9hG+aI4yDxyvjNztFtg5R4P5
zqsh/WwhCgsYmE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7KNaa
4Ygid1upnX6hk4CW5WZcoxGFacrFRpInKh5Ng8lEIHGp0VSzOBVDR0L5sAxutFuX
6N9C0CuH80vD101mOloNnfT5KHZMI5RXqP6sDGUFlwak2XybDL1qOAza3gZAy25H
vS/ll1BneBavikR5j+zxoTztAoGZAOJOJ5RyOrqpNuhiWZylah5LIFT9N1lCF4Hl
ZbFIjUZ4jcApJ7JxkMXNQ4RU/3AiKCC1xr5ib7dd/qyjKXhdMo4SnLoKhapx5R9G
3XOsQMahiCD/Zcymv9tmk8MxxzbLxhZYhEPzIP/NFkua3CHiX+d1e6fkzFLF/EiX
ZGQOgRcFKrlzUeBputRQRXAkKJH+kMClgAWvy28zAoGYKyaMXhG9DV+4xjzMBhIW
iijfsgbz+6AMRU+OIK1qmZa+ARsdNMXYf54noLVxvETOg0ZB+SGizwvZitO3lE4Q
NKWx3fTaeNMcMJ1rLkrN2UZ5M8/PT24muoAxWu8aGbURzmKuO3bTYwT7z0OvbayC
dYw36tG8/knXX6Vub6GdVGG9LKFB2nceiQnUVT0EK/wXwebYBoUvT/ECgZgF9qdG
Wyg/CPyAbS8NWLKOL86fTrjpqjsyWhgu7smCROT/XlZEdoepHrqbvx2oF85U5lVh
aPimrVxrsjUCjfoqEkV9BY/2KOAvzc9CIBTo5xLOQ8yr8uz1XCOiriogwIfsyNJb
dAm3k/D1dxQ79FowoEDs8LONrtfyFcM4e8VdFO7GSkqrDj41IBRkWx+SkVHBMdtI
yxQiTwKBmQCWym2iDCJg1ZZq4/lVwRudMhVmHoD0yoCAwADYHjjAi8QBplM0vfdd
CESKsnBhlcrPGB279BKVJyZHehKZG+/dfnFs+to14l6A3IqU2d6+pu3EyFNX34HS
xo+64QxMeF0akWnSaIPfUJfk36phjCvLBr4eLXN1i4jW3RdGFwF1THXt29VSSGmU
q/hM51H0bsQ13AIVUSdNHA==
-----END RSA PRIVATE KEY-----

View File

@ -6,5 +6,6 @@ SOURCES += tst_qsslcertificate.cpp
QT = core network testlib
TARGET = tst_qsslcertificate
DEFINES += SRCDIR=\\\"$$PWD/\\\"
TESTDATA += certificates/* more-certificates/* verify-certs/*
TESTDATA += certificates/* more-certificates/* verify-certs/* pkcs12/*

View File

@ -114,6 +114,7 @@ private slots:
void threadSafeConstMethods();
void version_data();
void version();
void pkcs12();
// helper for verbose test failure messages
QString toString(const QList<QSslError>&);
@ -1229,6 +1230,48 @@ void tst_QSslCertificate::version()
QCOMPARE(certificate.version(), result);
}
void tst_QSslCertificate::pkcs12()
{
if (!QSslSocket::supportsSsl()) {
qWarning("SSL not supported, skipping test");
return;
}
QFile f(QLatin1String(SRCDIR "pkcs12/leaf.p12"));
bool ok = f.open(QIODevice::ReadOnly);
QVERIFY(ok);
QSslKey key;
QSslCertificate cert;
QList<QSslCertificate> caCerts;
ok = QSslCertificate::importPKCS12(&f, &key, &cert, &caCerts);
QVERIFY(ok);
f.close();
QList<QSslCertificate> leafCert = QSslCertificate::fromPath(QLatin1String( SRCDIR "pkcs12/leaf.crt"));
QVERIFY(!leafCert.isEmpty());
QCOMPARE(cert, leafCert.first());
QFile f2(QLatin1String(SRCDIR "pkcs12/leaf.key"));
ok = f2.open(QIODevice::ReadOnly);
QVERIFY(ok);
QSslKey leafKey(&f2, QSsl::Rsa);
f2.close();
QVERIFY(!leafKey.isNull());
QCOMPARE(key, leafKey);
QList<QSslCertificate> caCert = QSslCertificate::fromPath(QLatin1String(SRCDIR "pkcs12/inter.crt"));
QVERIFY(!caCert.isEmpty());
QVERIFY(!caCerts.isEmpty());
QCOMPARE(caCerts.first(), caCert.first());
QCOMPARE(caCerts, caCert);
}
#endif // QT_NO_SSL
QTEST_MAIN(tst_QSslCertificate)

View File

@ -194,7 +194,6 @@ private slots:
void qtbug18498_peek2();
void dhServer();
void ecdhServer();
void pkcs12();
void setEmptyDefaultConfiguration(); // this test should be last
static void exitLoop()
@ -2735,52 +2734,6 @@ void tst_QSslSocket::ecdhServer()
QVERIFY(client->state() == QAbstractSocket::ConnectedState);
}
void tst_QSslSocket::pkcs12()
{
if (!QSslSocket::supportsSsl()) {
qWarning("SSL not supported, skipping test");
return;
}
QFETCH_GLOBAL(bool, setProxy);
if (setProxy)
return;
QFile f(QLatin1String(SRCDIR "certs/leaf.p12"));
bool ok = f.open(QIODevice::ReadOnly);
QVERIFY(ok);
QSslKey key;
QSslCertificate cert;
QList<QSslCertificate> caCerts;
ok = QSslSocket::importPKCS12(&f, &key, &cert, &caCerts);
QVERIFY(ok);
f.close();
QList<QSslCertificate> leafCert = QSslCertificate::fromPath(SRCDIR "certs/leaf.crt");
QVERIFY(!leafCert.isEmpty());
QCOMPARE(cert, leafCert.first());
QFile f2(QLatin1String(SRCDIR "certs/leaf.key"));
ok = f2.open(QIODevice::ReadOnly);
QVERIFY(ok);
QSslKey leafKey(&f2, QSsl::Rsa);
f2.close();
QVERIFY(!leafKey.isNull());
QCOMPARE(key, leafKey);
QList<QSslCertificate> caCert = QSslCertificate::fromPath(SRCDIR "certs/inter.crt");
QVERIFY(!caCert.isEmpty());
QVERIFY(!caCerts.isEmpty());
QCOMPARE(caCerts.first(), caCert.first());
QCOMPARE(caCerts, caCert);
}
void tst_QSslSocket::setEmptyDefaultConfiguration() // this test should be last, as it has some side effects
{
// used to produce a crash in QSslConfigurationPrivate::deepCopyDefaultConfiguration, QTBUG-13265