aheinecke@468: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
aheinecke@468:  * Software engineering by Intevation GmbH
aheinecke@468:  *
aheinecke@468:  * This file is Free Software under the GNU GPL (v>=2)
aheinecke@468:  * and comes with ABSOLUTELY NO WARRANTY!
aheinecke@468:  * See LICENSE.txt for details.
aheinecke@468:  */
aheinecke@468: #include "createcertlisttest.h"
aheinecke@468: #include "sslhelp.h"
aheinecke@468: #include "createcertlistdialog.h"
aheinecke@468: #include "certificatelist.h"
aheinecke@468: 
aheinecke@468: #include <polarssl/pk.h>
aheinecke@468: 
aheinecke@468: #include <QDateTime>
aheinecke@468: #include <QTemporaryFile>
aheinecke@468: #include <QTest>
aheinecke@468: 
aheinecke@468: void CreateCertListTest::testListCreation() {
aheinecke@468:     QTemporaryFile tmpFile, outputFile, keyFile;
aheinecke@468: 
aheinecke@468:     /* Get a valid list */
aheinecke@468:     QFile res(":/list-valid-signed.txt");
aheinecke@468:     res.open(QIODevice::ReadOnly);
aheinecke@468:     tmpFile.open();
aheinecke@468:     tmpFile.write(res.readAll());
aheinecke@468:     tmpFile.close();
aheinecke@468:     CertificateList validList = CertificateList(tmpFile.fileName().toLocal8Bit().data());
aheinecke@468:     QVERIFY(validList.isValid());
aheinecke@468: 
aheinecke@468:     /* Get a key */
aheinecke@468:     QFile keyRes(":/testkey-priv.pem");
aheinecke@468:     keyRes.open(QIODevice::ReadOnly);
aheinecke@468:     keyFile.open();
aheinecke@468:     keyFile.write(keyRes.readAll());
aheinecke@468:     keyFile.close();
aheinecke@468:     pk_context * pk = new pk_context;
aheinecke@468:     pk_init(pk);
aheinecke@468:     int ret = pk_parse_keyfile(pk, keyFile.fileName().toLocal8Bit().constData(), "");
aheinecke@468:     QVERIFY(ret == 0);
aheinecke@468: 
aheinecke@468:     /* Write the certificates from that list to another file */
aheinecke@468:     outputFile.open();
aheinecke@468:     QDateTime current = QDateTime::currentDateTimeUtc();
aheinecke@468:     QVERIFY(CreateCertListDialog::writeList(validList.getCertificates(),
aheinecke@468:                 outputFile.fileName(), current, pk));
aheinecke@468:     pk_free(pk);
aheinecke@468: 
aheinecke@468:     CertificateList outputList = CertificateList(outputFile.fileName().toLocal8Bit().data());
aheinecke@468:     QVERIFY(outputList.isValid());
aheinecke@468:     QVERIFY(outputList.getCertificates() == validList.getCertificates());
aheinecke@474:     QVERIFY(outputList.date().date() == current.date());
aheinecke@474:     QVERIFY(outputList.date().time().hour() == current.time().hour());
aheinecke@474:     QVERIFY(outputList.date().time().minute() == current.time().minute());
aheinecke@474:     QVERIFY(outputList.date().time().second() == current.time().second());
aheinecke@468: }
aheinecke@468: 
aheinecke@468: void CreateCertListTest::testSha256Sum()
aheinecke@468: {
aheinecke@470:     QByteArray input = "foo\n";
aheinecke@468:     QByteArray output = sha256sum(input);
aheinecke@471:     QVERIFY(output == QByteArray::fromHex("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"));
aheinecke@468: }
aheinecke@468: 
aheinecke@468: void CreateCertListTest::testSignature()
aheinecke@468: {
aheinecke@471:     QByteArray hash = QByteArray::fromHex("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c");
aheinecke@468: 
aheinecke@468:     QTemporaryFile keyFile;
aheinecke@468:     QFile keyRes(":/testkey-priv.pem");
aheinecke@468:     keyRes.open(QIODevice::ReadOnly);
aheinecke@468:     keyFile.open();
aheinecke@468:     keyFile.write(keyRes.readAll());
aheinecke@468:     keyFile.close();
aheinecke@468:     pk_context * pk = new pk_context;
aheinecke@468:     pk_init(pk);
aheinecke@468:     int ret = pk_parse_keyfile(pk, keyFile.fileName().toLocal8Bit().constData(), "");
aheinecke@468:     QVERIFY(ret == 0);
aheinecke@468: 
aheinecke@468:     QByteArray signature = rsaSignSHA256Hash(hash, pk);
aheinecke@468:     pk_free(pk);
aheinecke@468: 
aheinecke@468:     QVERIFY(signature.size() == 3072 / 8);
aheinecke@468:     QVERIFY(signature.toBase64() == QByteArray("KMOni98NWbt6SWd13H0JlGA1B7hBlXWH84e883s7gMrWeCCj0fUyHmdsNCyY0rmosu+o9mo2K847S3CdnxFPPJcjbfcmILZWRw0hHMtUYta1i9jypHJbz4oznuDctgXz59L4SQzzliCNUzItNoe6UpUznkS5gja4ZHbzqIj3qDVX3H86Z+qOdLICw+LXKlTs5ghsq+SdhZRAFFpHnt+URICWHjEIQKRlmIGEUIh1NgEHInHB/teFLqNGJMu1khi0MTsWDzesAEF5LQTM7Fo3fKmVxEUSbHKupluZrX1XSfnp5w3MaxBQK/t5nFvkVVdFrdEWvb68FIkMt21XqCvjyCPG2oWNh9jjfx3/R+eQ8kFbXzgUIhlZNxbB7bOCVDe2fYNxlXhy+HAqfHsIDP8qegHU+ngLck7tJHScC5dZwTCBDL6sxAvaeGyb3m6FraqaipNI+SGLii63ou9H7PlH5xWOTY9JvJDXGpfjN9U0UrZ6X5hPutOa/llT7s0pmoQb"));
aheinecke@468: }
aheinecke@468: 
aheinecke@468: QTEST_GUILESS_MAIN (CreateCertListTest);