File: keybundle.cpp

package info (click to toggle)
qca2 2.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,672 kB
  • sloc: cpp: 58,980; ansic: 832; perl: 133; sh: 79; makefile: 22
file content (204 lines) | stat: -rw-r--r-- 7,644 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * Copyright (C)  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.
 */

#include <QtCrypto>
#include <QtTest/QtTest>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

class KeyBundleTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void nullBundle();
    void fromFile();
    void names();
    void certChain();
    void privKey();
    void createBundle();

private:
    QCA::Initializer *m_init;
};

void KeyBundleTest::initTestCase()
{
    m_init = new QCA::Initializer;
}

void KeyBundleTest::cleanupTestCase()
{
    QCA::unloadAllPlugins();
    delete m_init;
}

void KeyBundleTest::nullBundle()
{
    QCA::KeyBundle nullBundle;
    QVERIFY(nullBundle.isNull());
    QCOMPARE(nullBundle.name(), QString());
    QVERIFY(nullBundle.certificateChain().isEmpty());
    QVERIFY(nullBundle.privateKey().isNull());

    QCA::KeyBundle nullCopy = nullBundle; // NOLINT(performance-unnecessary-copy-initialization) This is copied on
                                          // purpose to check the assignment operator
    QVERIFY(nullCopy.isNull());
    QCOMPARE(nullCopy.name(), QString());
    QVERIFY(nullCopy.certificateChain().isEmpty());
    QVERIFY(nullCopy.privateKey().isNull());

    QCA::KeyBundle nullAssigned(nullCopy); // NOLINT(performance-unnecessary-copy-initialization) This is copied on
                                           // purpose to check the copy constructor
    QVERIFY(nullAssigned.isNull());
    QCOMPARE(nullAssigned.name(), QString());
    QVERIFY(nullAssigned.certificateChain().isEmpty());
    QVERIFY(nullAssigned.privateKey().isNull());
}

void KeyBundleTest::fromFile()
{
    if (QCA::isSupported("pkcs12")) {
        // "start" is the passphrase, but you wouldn't normally
        // code it in like this
        QCA::KeyBundle userBundle(QStringLiteral("user2good.p12"), "start");
        QCOMPARE(userBundle.isNull(), false);
        QCOMPARE(userBundle.name(), QString());
        QCOMPARE(userBundle.certificateChain().isEmpty(), false);
        QCOMPARE(userBundle.privateKey().isNull(), false);

        QCA::KeyBundle userBundleCopy = userBundle; // NOLINT(performance-unnecessary-copy-initialization) This is
                                                    // copied on purpose to check the assignment operator
        QCOMPARE(userBundleCopy.isNull(), false);
        QCOMPARE(userBundleCopy.name(), QString());
        QCOMPARE(userBundleCopy.certificateChain().isEmpty(), false);
        QCOMPARE(userBundleCopy.privateKey().isNull(), false);

        QCA::KeyBundle userBundleAssign(userBundleCopy); // NOLINT(performance-unnecessary-copy-initialization) This is
                                                         // copied on purpose to check the copy constructor
        QCOMPARE(userBundleAssign.isNull(), false);
        QCOMPARE(userBundleAssign.name(), QString());
        QCOMPARE(userBundleAssign.certificateChain().isEmpty(), false);
        QCOMPARE(userBundleAssign.privateKey().isNull(), false);
    }
}

void KeyBundleTest::names()
{
    if (QCA::isSupported("pkcs12")) {
        QCA::KeyBundle serverBundle(QStringLiteral("servergood2.p12"), "start");
        QCOMPARE(serverBundle.isNull(), false);
        QCOMPARE(serverBundle.name(), QString());

        serverBundle.setName(QStringLiteral("Some Server Bundle"));
        QCOMPARE(serverBundle.name(), QStringLiteral("Some Server Bundle"));
    }
}

void KeyBundleTest::certChain()
{
    if (QCA::isSupported("pkcs12")) {
        QCA::KeyBundle serverBundle(QStringLiteral("servergood2.p12"), "start");
        QCOMPARE(serverBundle.isNull(), false);
        QCOMPARE(serverBundle.certificateChain().size(), 1);
    }
}

void KeyBundleTest::privKey()
{
    if (QCA::isSupported("pkcs12")) {
        QCA::KeyBundle serverBundle(QStringLiteral("servergood2.p12"), "start");
        QCOMPARE(serverBundle.isNull(), false);
        QCOMPARE(serverBundle.privateKey().isNull(), false);
    }
}
void KeyBundleTest::createBundle()
{
    QScopedPointer<QCA::KeyBundle> newBundle(new QCA::KeyBundle);

    QVERIFY(newBundle->isNull());

    if (!QCA::isSupported("certificate"))
        return;

    QCA::Certificate ca(QStringLiteral("RootCA2cert.pem"));
    QCOMPARE(ca.isNull(), false);

    QCA::Certificate primary(QStringLiteral("user2goodcert.pem"));
    QCOMPARE(primary.isNull(), false);

    QCA::PrivateKey key(QStringLiteral("user2goodkey.pem"));
    QCOMPARE(key.isNull(), false);

    QCA::CertificateChain chain(primary);
    chain.append(ca);

    newBundle->setCertificateChainAndKey(chain, key);
    newBundle->setName(QStringLiteral("My New Key Bundle"));

    QCOMPARE(newBundle->certificateChain(), chain);
    QCOMPARE(newBundle->privateKey(), key);
    QCOMPARE(newBundle->name(), QStringLiteral("My New Key Bundle"));

    // Try round tripping the bundle
    foreach (const QCA::Provider *thisProvider, QCA::providers()) {
        QString provider = thisProvider->name();
        if (QCA::isSupported("pkcs12", provider)) {
            qDebug() << "Testing " << provider;
            QByteArray bundleArray = newBundle->toArray("reel secrut", provider);
            QCOMPARE(bundleArray.isNull(), false);

            QCA::ConvertResult res;
            QCA::KeyBundle     bundleFromArray = QCA::KeyBundle::fromArray(bundleArray, "reel secrut", &res, provider);
            QCOMPARE(res, QCA::ConvertGood);
            QCOMPARE(bundleFromArray.isNull(), false);
            QCOMPARE(bundleFromArray.name(), QStringLiteral("My New Key Bundle"));
            QCOMPARE(bundleFromArray.certificateChain(), chain);
            QCOMPARE(bundleFromArray.privateKey(), key);

            QTemporaryFile tempFile;
            QVERIFY(tempFile.open());

            bool result = newBundle->toFile(tempFile.fileName(), "file passphrase", provider);
            QVERIFY(result);

            QCA::KeyBundle bundleFromFile =
                QCA::KeyBundle::fromFile(tempFile.fileName(), "file passphrase", &res, provider);
            QCOMPARE(res, QCA::ConvertGood);
            QCOMPARE(bundleFromFile.isNull(), false);
            QCOMPARE(bundleFromFile.name(), QStringLiteral("My New Key Bundle"));
            QCOMPARE(bundleFromFile.certificateChain(), chain);
            QCOMPARE(bundleFromFile.privateKey(), key);
        }
    }
}

QTEST_MAIN(KeyBundleTest)

#include "keybundle.moc"