2017-04-13 06:05:36 -04:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 Weslly Honorato <weslly@protonmail.com>
|
2017-06-09 17:40:36 -04:00
|
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2017-04-13 06:05:36 -04:00
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
|
* version 3 of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "TestTotp.h"
|
|
|
|
|
|
2021-07-11 22:10:29 -04:00
|
|
|
|
#include "core/Entry.h"
|
2023-10-23 23:22:12 -04:00
|
|
|
|
#include "core/Totp.h"
|
2017-04-13 06:05:36 -04:00
|
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
|
|
2021-07-11 22:10:29 -04:00
|
|
|
|
#include <QTest>
|
|
|
|
|
|
2017-04-13 06:05:36 -04:00
|
|
|
|
QTEST_GUILESS_MAIN(TestTotp)
|
|
|
|
|
|
|
|
|
|
void TestTotp::initTestCase()
|
|
|
|
|
{
|
|
|
|
|
QVERIFY(Crypto::init());
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 19:54:19 -04:00
|
|
|
|
void TestTotp::testParseSecret()
|
2017-04-13 06:05:36 -04:00
|
|
|
|
{
|
2018-09-05 16:20:57 -04:00
|
|
|
|
// OTP URL Parsing
|
2017-10-15 00:54:20 -04:00
|
|
|
|
QString secret = "otpauth://totp/"
|
|
|
|
|
"ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm="
|
|
|
|
|
"SHA1&digits=6&period=30";
|
2018-09-05 16:20:57 -04:00
|
|
|
|
auto settings = Totp::parseSettings(secret);
|
|
|
|
|
QVERIFY(!settings.isNull());
|
|
|
|
|
QCOMPARE(settings->key, QString("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"));
|
|
|
|
|
QCOMPARE(settings->custom, false);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(settings->digits, 6u);
|
|
|
|
|
QCOMPARE(settings->step, 30u);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
|
2019-04-10 06:58:27 -04:00
|
|
|
|
|
|
|
|
|
// OTP URL with non-default hash type
|
|
|
|
|
secret = "otpauth://totp/"
|
|
|
|
|
"ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm="
|
|
|
|
|
"SHA512&digits=6&period=30";
|
|
|
|
|
settings = Totp::parseSettings(secret);
|
|
|
|
|
QVERIFY(!settings.isNull());
|
|
|
|
|
QCOMPARE(settings->key, QString("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"));
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->custom, true);
|
|
|
|
|
QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
|
2019-04-10 06:58:27 -04:00
|
|
|
|
QCOMPARE(settings->digits, 6u);
|
|
|
|
|
QCOMPARE(settings->step, 30u);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha512);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
|
2023-02-21 22:26:06 -05:00
|
|
|
|
// Max TOTP step of 24-hours
|
|
|
|
|
secret.replace("period=30", "period=90000");
|
|
|
|
|
settings = Totp::parseSettings(secret);
|
|
|
|
|
QVERIFY(!settings.isNull());
|
|
|
|
|
QCOMPARE(settings->step, 86400u);
|
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
|
// KeeOTP Parsing
|
2019-04-10 06:58:27 -04:00
|
|
|
|
secret = "key=HXDMVJECJJWSRBY%3d&step=25&size=8&otpHashMode=Sha256";
|
2018-09-05 16:20:57 -04:00
|
|
|
|
settings = Totp::parseSettings(secret);
|
|
|
|
|
QVERIFY(!settings.isNull());
|
|
|
|
|
QCOMPARE(settings->key, QString("HXDMVJECJJWSRBY="));
|
|
|
|
|
QCOMPARE(settings->custom, true);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->format, Totp::StorageFormat::KEEOTP);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(settings->digits, 8u);
|
|
|
|
|
QCOMPARE(settings->step, 25u);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha256);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
|
|
|
|
|
// Semi-colon delineated "TOTP Settings"
|
2017-04-13 06:05:36 -04:00
|
|
|
|
secret = "gezdgnbvgy3tqojqgezdgnbvgy3tqojq";
|
2018-09-05 16:20:57 -04:00
|
|
|
|
settings = Totp::parseSettings("30;8", secret);
|
|
|
|
|
QVERIFY(!settings.isNull());
|
|
|
|
|
QCOMPARE(settings->key, QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq"));
|
|
|
|
|
QCOMPARE(settings->custom, true);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->format, Totp::StorageFormat::LEGACY);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(settings->digits, 8u);
|
|
|
|
|
QCOMPARE(settings->step, 30u);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
|
|
|
|
|
// Bare secret (no "TOTP Settings" attribute)
|
|
|
|
|
secret = "gezdgnbvgy3tqojqgezdgnbvgy3tqojq";
|
|
|
|
|
settings = Totp::parseSettings("", secret);
|
|
|
|
|
QVERIFY(!settings.isNull());
|
|
|
|
|
QCOMPARE(settings->key, QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq"));
|
|
|
|
|
QCOMPARE(settings->custom, false);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->format, Totp::StorageFormat::LEGACY);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(settings->digits, 6u);
|
|
|
|
|
QCOMPARE(settings->step, 30u);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
|
2023-10-23 23:22:12 -04:00
|
|
|
|
|
|
|
|
|
// Blank settings (expected failure)
|
|
|
|
|
settings = Totp::parseSettings("", "");
|
|
|
|
|
QVERIFY(settings.isNull());
|
|
|
|
|
|
|
|
|
|
// TOTP Settings with blank secret (expected failure)
|
|
|
|
|
settings = Totp::parseSettings("30;8", "");
|
|
|
|
|
QVERIFY(settings.isNull());
|
2017-04-13 06:05:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestTotp::testTotpCode()
|
|
|
|
|
{
|
|
|
|
|
// Test vectors from RFC 6238
|
|
|
|
|
// https://tools.ietf.org/html/rfc6238#appendix-B
|
2018-09-05 16:20:57 -04:00
|
|
|
|
auto settings = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP);
|
2017-04-13 06:05:36 -04:00
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
|
// Test 6 digit TOTP (default)
|
2017-04-13 06:05:36 -04:00
|
|
|
|
quint64 time = 1234567890;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(Totp::generateTotp(settings, time), QString("005924"));
|
2017-04-13 06:05:36 -04:00
|
|
|
|
|
|
|
|
|
time = 1111111109;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(Totp::generateTotp(settings, time), QString("081804"));
|
2017-04-13 06:05:36 -04:00
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
|
// Test 8 digit TOTP (custom)
|
|
|
|
|
settings->digits = 8;
|
|
|
|
|
settings->custom = true;
|
2017-04-13 06:05:36 -04:00
|
|
|
|
time = 1111111111;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(Totp::generateTotp(settings, time), QString("14050471"));
|
2017-04-13 06:05:36 -04:00
|
|
|
|
|
|
|
|
|
time = 2000000000;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(Totp::generateTotp(settings, time), QString("69279037"));
|
2017-11-20 13:01:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestTotp::testSteamTotp()
|
|
|
|
|
{
|
2018-09-05 16:20:57 -04:00
|
|
|
|
// OTP URL Parsing
|
2017-11-20 13:01:22 -05:00
|
|
|
|
QString secret = "otpauth://totp/"
|
|
|
|
|
"test:test@example.com?secret=63BEDWCQZKTQWPESARIERL5DTTQFCJTK&issuer=Valve&algorithm="
|
|
|
|
|
"SHA1&digits=5&period=30&encoder=steam";
|
2018-09-05 16:20:57 -04:00
|
|
|
|
auto settings = Totp::parseSettings(secret);
|
2017-11-20 13:01:22 -05:00
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(settings->key, QString("63BEDWCQZKTQWPESARIERL5DTTQFCJTK"));
|
|
|
|
|
QCOMPARE(settings->encoder.shortName, Totp::STEAM_SHORTNAME);
|
2019-10-14 09:25:45 -04:00
|
|
|
|
QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(settings->digits, Totp::STEAM_DIGITS);
|
|
|
|
|
QCOMPARE(settings->step, 30u);
|
2017-11-20 13:01:22 -05:00
|
|
|
|
|
|
|
|
|
// These time/value pairs were created by running the Steam Guard function of the
|
|
|
|
|
// Steam mobile app with a throw-away steam account. The above secret was extracted
|
|
|
|
|
// from the Steam app's data for use in testing here.
|
|
|
|
|
quint64 time = 1511200518;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(Totp::generateTotp(settings, time), QString("FR8RV"));
|
2017-11-20 13:01:22 -05:00
|
|
|
|
time = 1511200714;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(Totp::generateTotp(settings, time), QString("9P3VP"));
|
2017-11-20 13:01:22 -05:00
|
|
|
|
}
|
2018-02-01 11:58:15 -05:00
|
|
|
|
|
|
|
|
|
void TestTotp::testEntryHistory()
|
|
|
|
|
{
|
|
|
|
|
Entry entry;
|
2018-09-05 16:20:57 -04:00
|
|
|
|
uint step = 16;
|
|
|
|
|
uint digits = 6;
|
|
|
|
|
auto settings = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", digits, step);
|
|
|
|
|
// Test that entry starts without TOTP
|
2018-02-01 11:58:15 -05:00
|
|
|
|
QCOMPARE(entry.historyItems().size(), 0);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QVERIFY(!entry.hasTotp());
|
|
|
|
|
// Add TOTP to entry
|
|
|
|
|
entry.setTotp(settings);
|
2018-02-01 11:58:15 -05:00
|
|
|
|
QCOMPARE(entry.historyItems().size(), 1);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QVERIFY(entry.hasTotp());
|
|
|
|
|
QCOMPARE(entry.totpSettings()->key, QString("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"));
|
|
|
|
|
// Change key and verify settings changed
|
|
|
|
|
settings->key = "foo";
|
|
|
|
|
entry.setTotp(settings);
|
2018-02-01 11:58:15 -05:00
|
|
|
|
QCOMPARE(entry.historyItems().size(), 2);
|
2018-09-05 16:20:57 -04:00
|
|
|
|
QCOMPARE(entry.totpSettings()->key, QString("foo"));
|
2023-10-23 23:22:12 -04:00
|
|
|
|
// Nullptr Settings (expected reset of TOTP)
|
|
|
|
|
entry.setTotp(nullptr);
|
|
|
|
|
QVERIFY(!entry.hasTotp());
|
|
|
|
|
QCOMPARE(entry.historyItems().size(), 3);
|
2018-02-01 11:58:15 -05:00
|
|
|
|
}
|