2006-03-31 10:42:02 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2005-2006 Brad Hards <bradh@frogmouth.net>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2007-04-02 06:12:51 +00:00
|
|
|
|
|
|
|
#include <QtCrypto>
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
|
|
|
class RSAUnitTest : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
void testrsa();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QCA::Initializer* m_init;
|
|
|
|
};
|
2006-03-31 10:42:02 +00:00
|
|
|
|
|
|
|
void RSAUnitTest::initTestCase()
|
|
|
|
{
|
|
|
|
m_init = new QCA::Initializer;
|
|
|
|
#include "../fixpaths.include"
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSAUnitTest::cleanupTestCase()
|
|
|
|
{
|
|
|
|
delete m_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSAUnitTest::testrsa()
|
|
|
|
{
|
|
|
|
QStringList providersToTest;
|
|
|
|
providersToTest.append("qca-openssl");
|
|
|
|
// providersToTest.append("qca-gcrypt");
|
|
|
|
|
2007-04-02 06:12:51 +00:00
|
|
|
foreach(const QString provider, providersToTest) {
|
2006-03-31 10:42:02 +00:00
|
|
|
if(!QCA::isSupported("pkey", provider) ||
|
|
|
|
!QCA::PKey::supportedTypes(provider).contains(QCA::PKey::RSA) ||
|
|
|
|
!QCA::PKey::supportedIOTypes(provider).contains(QCA::PKey::RSA))
|
|
|
|
QWARN(QString("RSA not supported for "+provider).toLocal8Bit());
|
|
|
|
else {
|
|
|
|
QCA::KeyGenerator keygen;
|
|
|
|
QCOMPARE( keygen.isBusy(), false );
|
2007-06-08 20:31:50 +00:00
|
|
|
QCOMPARE( keygen.blockingEnabled(), true );
|
2006-03-31 10:42:02 +00:00
|
|
|
|
|
|
|
QList<int> keySizes;
|
|
|
|
keySizes << 512 << 1024 << 768 << 2048;
|
|
|
|
foreach( int keysize, keySizes ) {
|
|
|
|
QCA::PrivateKey rsaKey = keygen.createRSA(keysize, 65537, provider);
|
|
|
|
QCOMPARE( rsaKey.isNull(), false );
|
|
|
|
QCOMPARE( rsaKey.isRSA(), true );
|
|
|
|
QCOMPARE( rsaKey.isDSA(), false );
|
|
|
|
QCOMPARE( rsaKey.isDH(), false );
|
|
|
|
QCOMPARE( rsaKey.isPrivate(), true );
|
|
|
|
QCOMPARE( rsaKey.isPublic(), false );
|
|
|
|
QCOMPARE( rsaKey.canSign(), true);
|
|
|
|
QCOMPARE( rsaKey.canDecrypt(), true);
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2006-03-31 10:42:02 +00:00
|
|
|
QCA::RSAPrivateKey rsaPrivKey = rsaKey.toRSA();
|
|
|
|
QCOMPARE( rsaPrivKey.bitSize(), keysize );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2006-03-31 10:42:02 +00:00
|
|
|
QString rsaPEM = rsaKey.toPEM();
|
|
|
|
QCOMPARE( rsaPEM.isEmpty(), false );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2006-03-31 10:42:02 +00:00
|
|
|
QCA::ConvertResult checkResult;
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::PrivateKey fromPEMkey = QCA::PrivateKey::fromPEM(rsaPEM, QCA::SecureArray(), &checkResult);
|
2006-03-31 10:42:02 +00:00
|
|
|
QCOMPARE( checkResult, QCA::ConvertGood );
|
|
|
|
QCOMPARE( fromPEMkey.isNull(), false );
|
|
|
|
QCOMPARE( fromPEMkey.isRSA(), true );
|
|
|
|
QCOMPARE( fromPEMkey.isDSA(), false );
|
|
|
|
QCOMPARE( fromPEMkey.isDH(), false );
|
|
|
|
QCOMPARE( fromPEMkey.isPrivate(), true );
|
|
|
|
QCOMPARE( fromPEMkey.isPublic(), false );
|
|
|
|
QCOMPARE( rsaKey == fromPEMkey, true );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray rsaDER = rsaKey.toDER(QCA::SecureArray("foo"));
|
2006-03-31 10:42:02 +00:00
|
|
|
QCOMPARE( rsaDER.isEmpty(), false );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::PrivateKey fromDERkey = QCA::PrivateKey::fromDER(rsaDER, QCA::SecureArray("foo"), &checkResult);
|
2006-03-31 10:42:02 +00:00
|
|
|
QCOMPARE( checkResult, QCA::ConvertGood );
|
|
|
|
QCOMPARE( fromDERkey.isNull(), false );
|
|
|
|
QCOMPARE( fromDERkey.isRSA(), true );
|
|
|
|
QCOMPARE( fromDERkey.isDSA(), false );
|
|
|
|
QCOMPARE( fromDERkey.isDH(), false );
|
|
|
|
QCOMPARE( fromDERkey.isPrivate(), true );
|
|
|
|
QCOMPARE( fromDERkey.isPublic(), false );
|
|
|
|
QCOMPARE( rsaKey == fromDERkey, true );
|
|
|
|
|
|
|
|
// same test, without passphrase
|
|
|
|
rsaDER = rsaKey.toDER();
|
|
|
|
QCOMPARE( rsaDER.isEmpty(), false );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
fromDERkey = QCA::PrivateKey::fromDER(rsaDER, QCA::SecureArray(), &checkResult);
|
2006-03-31 10:42:02 +00:00
|
|
|
QCOMPARE( checkResult, QCA::ConvertGood );
|
|
|
|
QCOMPARE( fromDERkey.isNull(), false );
|
|
|
|
QCOMPARE( fromDERkey.isRSA(), true );
|
|
|
|
QCOMPARE( fromDERkey.isDSA(), false );
|
|
|
|
QCOMPARE( fromDERkey.isDH(), false );
|
|
|
|
QCOMPARE( fromDERkey.isPrivate(), true );
|
|
|
|
QCOMPARE( fromDERkey.isPublic(), false );
|
|
|
|
QCOMPARE( rsaKey == fromDERkey, true );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2006-03-31 10:42:02 +00:00
|
|
|
QCA::PublicKey pubKey = rsaKey.toPublicKey();
|
|
|
|
QCOMPARE( pubKey.isNull(), false );
|
|
|
|
QCOMPARE( pubKey.isRSA(), true );
|
|
|
|
QCOMPARE( pubKey.isDSA(), false );
|
|
|
|
QCOMPARE( pubKey.isDH(), false );
|
|
|
|
QCOMPARE( pubKey.isPrivate(), false );
|
|
|
|
QCOMPARE( pubKey.isPublic(), true );
|
2007-04-02 06:12:51 +00:00
|
|
|
|
2006-03-31 10:42:02 +00:00
|
|
|
QCA::RSAPublicKey RSApubKey = pubKey.toRSA();
|
2007-04-13 19:19:18 +00:00
|
|
|
QCOMPARE( RSApubKey.e(), QCA::BigInteger(65537) );
|
2006-03-31 10:42:02 +00:00
|
|
|
QCOMPARE( RSApubKey.isNull(), false );
|
|
|
|
QCOMPARE( RSApubKey.isRSA(), true );
|
|
|
|
QCOMPARE( RSApubKey.isDSA(), false );
|
|
|
|
QCOMPARE( RSApubKey.isDH(), false );
|
|
|
|
QCOMPARE( RSApubKey.isPrivate(), false );
|
|
|
|
QCOMPARE( RSApubKey.isPublic(), true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTEST_MAIN(RSAUnitTest)
|
2007-04-02 06:12:51 +00:00
|
|
|
|
|
|
|
#include "rsaunittest.moc"
|