| 1 |
# Before `make install' is performed this script should be runnable with |
| 2 |
# `make test'. After `make install' it should work as `perl Lemonldap-NG-Common.t' |
| 3 |
######################### |
| 4 |
use Time::Fake; |
| 5 |
|
| 6 |
# Must subclass TOTP because it uses $self->logger etc. |
| 7 |
package TestableTotp; |
| 8 |
use Test::More; |
| 9 |
use Mouse; |
| 10 |
use Lemonldap::NG::Common::TOTP; |
| 11 |
use Lemonldap::NG::Common::Logger::Null; |
| 12 |
extends 'Lemonldap::NG::Common::TOTP'; |
| 13 |
has logger => ( is => "ro", lazy => 1, builder => '_null_logger' ); |
| 14 |
has userLogger => ( is => "ro", lazy => 1, builder => '_null_logger' ); |
| 15 |
|
| 16 |
sub _null_logger { |
| 17 |
return Lemonldap::NG::Common::Logger::Null->new; |
| 18 |
} |
| 19 |
|
| 20 |
package main; |
| 21 |
use Test::More tests => 17; |
| 22 |
|
| 23 |
BEGIN { |
| 24 |
use_ok('Lemonldap::NG::Common::TOTP'); |
| 25 |
} |
| 26 |
use strict; |
| 27 |
|
| 28 |
### WARNING FOR DEVELOPPERS ### |
| 29 |
# These constants are not to be messed with. If this unit test breaks, do NOT |
| 30 |
# modify them, fix the code instead. |
| 31 |
# |
| 32 |
# In particular, if the $stored_secret no longer decrypts to $cleartext_secret, |
| 33 |
# it means that users will lose their encrypted TOTP secrets on the next |
| 34 |
# upgrade. |
| 35 |
# If you need to change the cryptographic algorithm, make sure you remain |
| 36 |
# compatible with existing stored values |
| 37 |
|
| 38 |
my $timestamp = 1633009395; |
| 39 |
my $totp_for_timestamp = 766039; |
| 40 |
my $cleartext_secret = "ggtoch5x6naorymli6nh72ku4khwd4jr"; |
| 41 |
my $key = "azert"; |
| 42 |
my $encrypted_secret = |
| 43 |
"{llngcrypt}TdEcd2vkmn4j0D8+str3v2D8zt0Dbm3sZ8TwlzdOKcang+qUmLraTQBztSrESRHDpAh+pQCKvDozuz9va7GxhHIkaKI3EZxOCWJ0rQCun/I="; |
| 44 |
|
| 45 |
######################### |
| 46 |
|
| 47 |
subtest "RFC6238 test vectors" => sub { |
| 48 |
|
| 49 |
my $keys = { |
| 50 |
"SHA1" => "12345678901234567890", |
| 51 |
"SHA256" => "12345678901234567890123456789012", |
| 52 |
"SHA512" => |
| 53 |
"1234567890123456789012345678901234567890123456789012345678901234", |
| 54 |
}; |
| 55 |
|
| 56 |
my @vectors = ( |
| 57 |
[ 59, "94287082", "SHA1" ], |
| 58 |
[ 59, "46119246", "SHA256" ], |
| 59 |
[ 59, "90693936", "SHA512" ], |
| 60 |
[ 1111111109, "07081804", "SHA1" ], |
| 61 |
[ 1111111109, "68084774", "SHA256" ], |
| 62 |
[ 1111111109, "25091201", "SHA512" ], |
| 63 |
[ 1111111111, "14050471", "SHA1" ], |
| 64 |
[ 1111111111, "67062674", "SHA256" ], |
| 65 |
[ 1111111111, "99943326", "SHA512" ], |
| 66 |
[ 1234567890, "89005924", "SHA1" ], |
| 67 |
[ 1234567890, "91819424", "SHA256" ], |
| 68 |
[ 1234567890, "93441116", "SHA512" ], |
| 69 |
[ 2000000000, "69279037", "SHA1" ], |
| 70 |
[ 2000000000, "90698825", "SHA256" ], |
| 71 |
[ 2000000000, "38618901", "SHA512" ], |
| 72 |
[ 20000000000, "65353130", "SHA1" ], |
| 73 |
[ 20000000000, "77737706", "SHA256" ], |
| 74 |
[ 20000000000, "47863826", "SHA512" ], |
| 75 |
); |
| 76 |
my $t = TestableTotp->new(); |
| 77 |
for my $vector (@vectors) { |
| 78 |
|
| 79 |
my $alg = $vector->[2]; |
| 80 |
my $expected_code = $vector->[1]; |
| 81 |
my $timestamp = $vector->[0]; |
| 82 |
my $key = $keys->{$alg}; |
| 83 |
|
| 84 |
is( $t->_code( $key, 0, 30, 8, lc($alg), $timestamp ), |
| 85 |
$expected_code, |
| 86 |
"$alg TOTP at timestamp $timestamp is $expected_code" ); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
######################### |
| 91 |
|
| 92 |
# Insert your test code below, the Test::More module is used here so read |
| 93 |
# its man page ( perldoc Test::More ) for help writing this test script. |
| 94 |
|
| 95 |
my $t = TestableTotp->new( key => $key, encryptSecret => 0 ); |
| 96 |
|
| 97 |
# Verification with no offset |
| 98 |
Time::Fake->offset($timestamp); |
| 99 |
is( $t->verifyCode( 30, 0, 6, $cleartext_secret, $totp_for_timestamp ), |
| 100 |
1, "TOTP code is valid" ); |
| 101 |
|
| 102 |
Time::Fake->offset( $timestamp + 30 ); |
| 103 |
is( $t->verifyCode( 30, 0, 6, $cleartext_secret, $totp_for_timestamp ), |
| 104 |
0, "TOTP code is no longer valid" ); |
| 105 |
|
| 106 |
Time::Fake->offset( $timestamp - 30 ); |
| 107 |
is( $t->verifyCode( 30, 0, 6, $cleartext_secret, $totp_for_timestamp ), |
| 108 |
0, "TOTP code is not valid yet" ); |
| 109 |
|
| 110 |
# Verification with offset 2 allows +1m and -1m |
| 111 |
Time::Fake->offset( $timestamp + 45 ); |
| 112 |
is( $t->verifyCode( 30, 2, 6, $cleartext_secret, $totp_for_timestamp ), |
| 113 |
1, "TOTP code is valid" ); |
| 114 |
|
| 115 |
Time::Fake->offset( $timestamp - 45 ); |
| 116 |
is( $t->verifyCode( 30, 2, 6, $cleartext_secret, $totp_for_timestamp ), |
| 117 |
1, "TOTP code is valid" ); |
| 118 |
|
| 119 |
Time::Fake->offset( $timestamp + 95 ); |
| 120 |
is( $t->verifyCode( 30, 2, 6, $cleartext_secret, $totp_for_timestamp ), |
| 121 |
0, "TOTP code is no longer valid" ); |
| 122 |
|
| 123 |
Time::Fake->offset( $timestamp - 95 ); |
| 124 |
is( $t->verifyCode( 30, 2, 6, $cleartext_secret, $totp_for_timestamp ), |
| 125 |
0, "TOTP code is not valid yet" ); |
| 126 |
|
| 127 |
# TOTP encryption tests |
| 128 |
|
| 129 |
$t = TestableTotp->new( key => $key, encryptSecret => 0 ); |
| 130 |
Time::Fake->offset($timestamp); |
| 131 |
is( $t->verifyCode( 30, 0, 6, $encrypted_secret, $totp_for_timestamp ), |
| 132 |
1, "TOTP is valid with encrypted secret and encryption disabled" ); |
| 133 |
|
| 134 |
$t = TestableTotp->new( key => $key, encryptSecret => 1 ); |
| 135 |
Time::Fake->offset($timestamp); |
| 136 |
is( $t->verifyCode( 30, 0, 6, $encrypted_secret, $totp_for_timestamp ), |
| 137 |
1, "TOTP is valid with encrypted secret and encryption enabled" ); |
| 138 |
Time::Fake->offset($timestamp); |
| 139 |
is( $t->verifyCode( 30, 0, 6, $cleartext_secret, $totp_for_timestamp ), |
| 140 |
1, "TOTP is valid with cleartext secret and encryption enabled" ); |
| 141 |
|
| 142 |
# Encryption of TOTP secret, wrong key |
| 143 |
$t = TestableTotp->new( key => "idunno", encryptSecret => 0 ); |
| 144 |
is( $t->verifyCode( 30, 0, 6, $encrypted_secret, $totp_for_timestamp ), |
| 145 |
-1, "TOTP code fails to verify" ); |
| 146 |
|
| 147 |
# Do not encrypt new secrets unless we configured it |
| 148 |
$t = TestableTotp->new( key => $key, encryptSecret => 0 ); |
| 149 |
is( $t->get_storable_secret($cleartext_secret), |
| 150 |
$cleartext_secret, |
| 151 |
"TOTP secret is stored as-is when encryption is disabled" ); |
| 152 |
|
| 153 |
# Encrypt new secrets if we configured it |
| 154 |
$t = TestableTotp->new( key => $key, encryptSecret => 1 ); |
| 155 |
my $new = $t->get_storable_secret($cleartext_secret); |
| 156 |
like( $new, qr/^{llngcrypt}/, "Secret looks encrypted" ); |
| 157 |
unlike( $new, qr/$cleartext_secret/, "Secret looks encrypted" ); |
| 158 |
|
| 159 |
Time::Fake->offset($timestamp); |
| 160 |
is( $t->verifyCode( 30, 0, 6, $new, $totp_for_timestamp ), |
| 161 |
1, "get_storable_secret produces working secret" ); |