File indexing completed on 2024-05-05 04:45:30

0001 /**
0002  * Copyright (C)  2005, 2006  Brad Hards <bradh@frogmouth.net>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright
0009  *   notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *   notice, this list of conditions and the following disclaimer in the
0012  *   documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include <QtCrypto>
0027 #include <QtTest/QtTest>
0028 
0029 #ifdef QT_STATICPLUGIN
0030 #include "import_plugins.h"
0031 #endif
0032 
0033 class DSAUnitTest : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void initTestCase();
0039     void cleanupTestCase();
0040     void testdsa();
0041 
0042 private:
0043     QCA::Initializer *m_init;
0044 };
0045 
0046 void DSAUnitTest::initTestCase()
0047 {
0048     m_init = new QCA::Initializer;
0049 }
0050 
0051 void DSAUnitTest::cleanupTestCase()
0052 {
0053     delete m_init;
0054 }
0055 
0056 void DSAUnitTest::testdsa()
0057 {
0058     if (!QCA::isSupported("pkey") || !QCA::PKey::supportedTypes().contains(QCA::PKey::DSA) ||
0059         !QCA::PKey::supportedIOTypes().contains(QCA::PKey::DSA)) {
0060         QSKIP("DSA not supported!");
0061     }
0062 
0063     if (!QCA::DLGroup::supportedGroupSets().contains(QCA::DSA_1024)) {
0064         QSKIP("DSA_1024 discrete logarithm group sets not supported!");
0065     }
0066 
0067     QCA::KeyGenerator keygen;
0068     QCOMPARE(keygen.isBusy(), false);
0069     QCOMPARE(keygen.blockingEnabled(), true);
0070     QCA::DLGroup group = keygen.createDLGroup(QCA::DSA_1024);
0071     QCOMPARE(group.isNull(), false);
0072 
0073     QCA::PrivateKey dsaKey = keygen.createDSA(group);
0074     QCOMPARE(dsaKey.isNull(), false);
0075     QCOMPARE(dsaKey.isRSA(), false);
0076     QCOMPARE(dsaKey.isDSA(), true);
0077     QCOMPARE(dsaKey.isDH(), false);
0078     QCOMPARE(dsaKey.isPrivate(), true);
0079     QCOMPARE(dsaKey.isPublic(), false);
0080     QCOMPARE(dsaKey.canSign(), true);
0081     QCOMPARE(dsaKey.canDecrypt(), false);
0082 
0083     QCOMPARE(dsaKey.bitSize(), 1024);
0084     QCA::DSAPrivateKey dsaPrivKey = dsaKey.toDSA();
0085     QCOMPARE(dsaPrivKey.bitSize(), 1024);
0086 
0087     QCA::SecureArray dsaDER = dsaKey.toDER();
0088     QCOMPARE(dsaDER.isEmpty(), false);
0089 
0090     QString dsaPEM = dsaKey.toPEM();
0091     QCOMPARE(dsaPEM.isEmpty(), false);
0092 
0093     QCA::ConvertResult checkResult;
0094     QCA::PrivateKey    fromPEMkey = QCA::PrivateKey::fromPEM(dsaPEM, QCA::SecureArray(), &checkResult);
0095     QCOMPARE(checkResult, QCA::ConvertGood);
0096     QCOMPARE(fromPEMkey.isNull(), false);
0097     QCOMPARE(fromPEMkey.isRSA(), false);
0098     QCOMPARE(fromPEMkey.isDSA(), true);
0099     QCOMPARE(fromPEMkey.isDH(), false);
0100     QCOMPARE(fromPEMkey.isPrivate(), true);
0101     QCOMPARE(fromPEMkey.isPublic(), false);
0102     QVERIFY(dsaKey == fromPEMkey);
0103 
0104     QCA::PrivateKey fromDERkey = QCA::PrivateKey::fromDER(dsaDER, QCA::SecureArray(), &checkResult);
0105     QCOMPARE(checkResult, QCA::ConvertGood);
0106     QCOMPARE(fromDERkey.isNull(), false);
0107     QCOMPARE(fromDERkey.isRSA(), false);
0108     QCOMPARE(fromDERkey.isDSA(), true);
0109     QCOMPARE(fromDERkey.isDH(), false);
0110     QCOMPARE(fromDERkey.isPrivate(), true);
0111     QCOMPARE(fromDERkey.isPublic(), false);
0112     QVERIFY(dsaKey == fromDERkey);
0113 }
0114 
0115 QTEST_MAIN(DSAUnitTest)
0116 
0117 #include "dsaunittest.moc"