| 1 | package UID2::Client::Encryption; | ||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Crypt::Cipher::AES; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Crypt::Mode::CBC; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Crypt::AuthEnc::GCM; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Crypt::Misc qw(encode_b64 decode_b64 decode_b64u); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use UID2::Client::AdvertisingTokenVersion; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use UID2::Client::DecryptionStatus; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use UID2::Client::EncryptionStatus; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use UID2::Client::Timestamp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 39 | ▶ | sub decrypt_token { | ||||||||||||||||||||||||||||||
|
Subroutine
decrypt_token: called CC=12 SCAR=25.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 18 | 39 | my $token = shift; | |||||||||||||||||||||||||||||||
| 19 | 39 | my @args = @_; | |||||||||||||||||||||||||||||||
| 20 | 39 | ▶ | if (length $token < 4) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | 39 | my $result = eval { | |||||||||||||||||||||||||||||||
| 24 | 39 | my $header = substr $token, 0, 4; | |||||||||||||||||||||||||||||||
| 25 | 39 | my $header_bytes; | |||||||||||||||||||||||||||||||
| 26 | 39 | ▶ | if ($header =~ /[\-_]/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 0 | $header_bytes = decode_b64u($header); | |||||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||||||
| 29 | 39 | $header_bytes = decode_b64($header); | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | 39 | ▶ | if (!defined $header_bytes or length $header_bytes < 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $header_bytes or length $header_bytes < 2
MC/DC0%0/2
not defined $header_bytes or length $header_bytes < 2
not defined $header_bytes
length $header_bytes < 2
Truth table33%1/3
not(A) or B
A= not defined $header_bytesB= length $header_bytes < 2
| |||||||||||||||||||||||||||||||||
| 32 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | 39 | ▶ | if (ord(substr($header_bytes, 0, 1)) == 2) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 35 | 9 | my $bytes = decode_b64($token); | |||||||||||||||||||||||||||||||
| 36 | 9 | ▶ | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD) unless defined $bytes; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 7 | return _decrypt_token_v2($bytes, @args); | |||||||||||||||||||||||||||||||
| 38 | } elsif (ord(substr($header_bytes, 1, 1)) == UID2::Client::AdvertisingTokenVersion::V3) { | ||||||||||||||||||||||||||||||||
| 39 | 14 | my $bytes = decode_b64($token); | |||||||||||||||||||||||||||||||
| 40 | 14 | ▶ | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD) unless defined $bytes; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 12 | return _decrypt_token_v3($bytes, @args); | |||||||||||||||||||||||||||||||
| 42 | } elsif (ord(substr($header_bytes, 1, 1)) == UID2::Client::AdvertisingTokenVersion::V4) { | ||||||||||||||||||||||||||||||||
| 43 | # same as V3 but use Base64URL encoding | ||||||||||||||||||||||||||||||||
| 44 | 14 | my $bytes = decode_b64u($token); | |||||||||||||||||||||||||||||||
| 45 | 14 | ▶ | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD) unless defined $bytes; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 13 | return _decrypt_token_v3($bytes, @args); | |||||||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||||||
| 48 | 2 | return _error_response(UID2::Client::DecryptionStatus::VERSION_NOT_SUPPORTED); | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | 39 | ▶ | }; if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 4 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | 35 | $result; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | sub _decrypt_token_v2 { | ||||||||||||||||||||||||||||||
|
Subroutine
_decrypt_token_v2: called CC=10 SCAR=23.6
| |||||||||||||||||||||||||||||||||
| 57 | 7 | my ($bytes, $now, $keys) = @_; | |||||||||||||||||||||||||||||||
| 58 | 7 | ▶ | if (!$keys) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 1 | return _error_response(UID2::Client::DecryptionStatus::NOT_INITIALIZED); | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 6 | ▶ | $now //= UID2::Client::Timestamp->now; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$now //= "UID2::Client::Timestamp"->now
MC/DC0%0/2
$now //= "UID2::Client::Timestamp"->now
$now
"UID2::Client::Timestamp"->now
Truth table66%2/3
A //= B
A= $nowB= "UID2::Client::Timestamp"->now
| |||||||||||||||||||||||||||||||||
| 62 | 6 | ▶ | if (!$keys->is_valid($now)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 1 | return _error_response(UID2::Client::DecryptionStatus::KEYS_NOT_SYNCED); | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 5 | my ($version, $master_key_id, $master_payload_encrypted) = unpack 'a N! a*', $bytes; | |||||||||||||||||||||||||||||||
| 66 | 5 | ▶ | if (ord($version) != 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 0 | return _error_response(UID2::Client::DecryptionStatus::VERSION_NOT_SUPPORTED); | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 5 | my $master_key = $keys->get($master_key_id); | |||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | unless ($master_key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 1 | return _error_response(UID2::Client::DecryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | 4 | my $master_payload = decrypt_cbc($master_payload_encrypted, $master_key->secret); | |||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | unless (defined $master_payload) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 3 | my ($expires, $site_key_id, $identity_encrypted) = unpack 'q> N! a*', $master_payload; | |||||||||||||||||||||||||||||||
| 78 | 3 | my $site_key = $keys->get($site_key_id); | |||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | unless ($site_key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 0 | return _error_response(UID2::Client::DecryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 3 | my $identity = decrypt_cbc($identity_encrypted, $site_key->secret); | |||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | unless (defined $identity) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | 3 | my ($site_id, $id_len) = unpack 'N! N!', $identity; | |||||||||||||||||||||||||||||||
| 87 | 3 | my ($id_str, $established) = unpack "x4 x4 a${id_len} x4 q>", $identity; | |||||||||||||||||||||||||||||||
| 88 | 3 | my $result = { | |||||||||||||||||||||||||||||||
| 89 | site_id => $site_id, | ||||||||||||||||||||||||||||||||
| 90 | site_key_site_id => $site_key->site_id, | ||||||||||||||||||||||||||||||||
| 91 | established => UID2::Client::Timestamp->from_epoch_milli($established), | ||||||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | if ($expires < $now->get_epoch_milli) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 1 | $result->{is_success} = undef; | |||||||||||||||||||||||||||||||
| 95 | 1 | $result->{status} = UID2::Client::DecryptionStatus::EXPIRED_TOKEN; | |||||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||||||
| 97 | 2 | $result->{is_success} = 1; | |||||||||||||||||||||||||||||||
| 98 | 2 | $result->{status} = UID2::Client::DecryptionStatus::SUCCESS; | |||||||||||||||||||||||||||||||
| 99 | 2 | $result->{uid} = $id_str; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | 3 | $result; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 25 | ▶ | sub _decrypt_token_v3 { | ||||||||||||||||||||||||||||||
|
Subroutine
_decrypt_token_v3: called CC=11 SCAR=24.9
| |||||||||||||||||||||||||||||||||
| 105 | 25 | my ($bytes, $now, $keys, $identity_scope) = @_; | |||||||||||||||||||||||||||||||
| 106 | 25 | ▶ | if (!$keys) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 2 | return _error_response(UID2::Client::DecryptionStatus::NOT_INITIALIZED); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 23 | ▶ | $now //= UID2::Client::Timestamp->now; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$now //= "UID2::Client::Timestamp"->now
MC/DC0%0/2
$now //= "UID2::Client::Timestamp"->now
$now
"UID2::Client::Timestamp"->now
Truth table66%2/3
A //= B
A= $nowB= "UID2::Client::Timestamp"->now
| |||||||||||||||||||||||||||||||||
| 110 | 23 | ▶ | if (!$keys->is_valid($now)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 2 | return _error_response(UID2::Client::DecryptionStatus::KEYS_NOT_SYNCED); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | 21 | my ($prefix, $master_key_id, $master_payload_encrypted) = unpack 'a x N! a*', $bytes; | |||||||||||||||||||||||||||||||
| 114 | 21 | ▶ | if (_decode_identity_scope($prefix) != $identity_scope) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_IDENTITY_SCOPE); | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | 21 | my $master_key = $keys->get($master_key_id); | |||||||||||||||||||||||||||||||
| 118 | 21 | ▶ | unless ($master_key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 2 | return _error_response(UID2::Client::DecryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 19 | ▶ | if (length($master_payload_encrypted) > 256) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | 19 | my $master_payload = decrypt_gcm($master_payload_encrypted, $master_key->secret); | |||||||||||||||||||||||||||||||
| 125 | 16 | ▶ | unless (defined $master_payload) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | 16 | my ($expires, $site_key_id, $site_payload_encrypted) = unpack 'q> x8 x4 x x4 x4 N! a*', $master_payload; | |||||||||||||||||||||||||||||||
| 129 | 16 | my $site_key = $keys->get($site_key_id); | |||||||||||||||||||||||||||||||
| 130 | 16 | ▶ | unless ($site_key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 0 | return _error_response(UID2::Client::DecryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | 16 | my $site_payload = decrypt_gcm($site_payload_encrypted, $site_key->secret); | |||||||||||||||||||||||||||||||
| 134 | 16 | ▶ | unless (defined $site_payload) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | 16 | my ($site_id, $established, $identity_bytes) = unpack 'N! x8 x4 x4 q> x8 a*', $site_payload; | |||||||||||||||||||||||||||||||
| 138 | 16 | my $result = { | |||||||||||||||||||||||||||||||
| 139 | site_id => $site_id, | ||||||||||||||||||||||||||||||||
| 140 | site_key_site_id => $site_key->site_id, | ||||||||||||||||||||||||||||||||
| 141 | established => UID2::Client::Timestamp->from_epoch_milli($established), | ||||||||||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||||||||||
| 143 | 16 | ▶ | if ($expires < $now->get_epoch_milli) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 4 | $result->{is_success} = undef; | |||||||||||||||||||||||||||||||
| 145 | 4 | $result->{status} = UID2::Client::DecryptionStatus::EXPIRED_TOKEN; | |||||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||||||
| 147 | 12 | $result->{is_success} = 1; | |||||||||||||||||||||||||||||||
| 148 | 12 | $result->{status} = UID2::Client::DecryptionStatus::SUCCESS; | |||||||||||||||||||||||||||||||
| 149 | 12 | $result->{uid} = encode_b64($identity_bytes); | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | 16 | $result; | |||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 4 | my $EncryptedDataType = 128; | |||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 4 | my $EncryptedDataVersion = 1; | |||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 4 | my $EncryptedDataTypeV3 = 96; | |||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 4 | my $EncryptedDataVersionV3 = 112; | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 4 | my $CBC_IV_LEN = 16; | |||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 4 | my $GCM_IV_LEN = 12; | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 4 | my $GCM_AUTHTAG_LEN = 16; | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 62 | ▶ | sub encrypt_data { | ||||||||||||||||||||||||||||||
|
Subroutine
encrypt_data: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 169 | 62 | my $result = eval { | |||||||||||||||||||||||||||||||
| 170 | 62 | _encrypt_data_v3(@_); | |||||||||||||||||||||||||||||||
| 171 | 62 | ▶ | }; if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | 2 | return _error_response(UID2::Client::EncryptionStatus::ENCRYPTION_FAILURE); | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | 60 | $result; | |||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 62 | ▶ | sub _encrypt_data_v3 { | ||||||||||||||||||||||||||||||
|
Subroutine
_encrypt_data_v3: called CC=15 SCAR=27.7
| |||||||||||||||||||||||||||||||||
| 178 | 62 | my ($data, $request) = @_; | |||||||||||||||||||||||||||||||
| 179 | 62 | my $keys = $request->{keys}; | |||||||||||||||||||||||||||||||
| 180 | 62 | my $key = $request->{key}; | |||||||||||||||||||||||||||||||
| 181 | 62 | ▶ | if ($keys && $key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$keys and $key
MC/DC0%0/2
$keys and $key
$keys
$key
Truth table66%2/3
A and B
A= $keysB= $key
| |||||||||||||||||||||||||||||||||
| 182 | 0 | croak 'only one of keys and key can be specified'; | |||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | 62 | ▶ | my $now = $request->{now} // UID2::Client::Timestamp->now; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$request->{"now"} // "UID2::Client::Timestamp"->now
MC/DC0%0/2
$request->{"now"} // "UID2::Client::Timestamp"->now
$request->{"now"}
"UID2::Client::Timestamp"->now
Truth table66%2/3
A // B
A= $request->{"now"}B= "UID2::Client::Timestamp"->now
| |||||||||||||||||||||||||||||||||
| 185 | 62 | my $site_id; | |||||||||||||||||||||||||||||||
| 186 | 62 | ▶ | if (!$key) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 187 | 42 | ▶ | if (!$keys) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | 0 | return _error_response(UID2::Client::EncryptionStatus::NOT_INITIALIZED); | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | 42 | ▶ | if (!$keys->is_valid($now)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | 0 | return _error_response(UID2::Client::EncryptionStatus::KEYS_NOT_SYNCED); | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | 42 | my $site_key_site_id; | |||||||||||||||||||||||||||||||
| 194 | 42 | ▶ | if (defined $request->{site_id} && length $request->{advertising_token}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $request->{"site_id"} and length $request->{"advertising_token"}
MC/DC100%2/2
defined $request->{"site_id"} and length $request->{"advertising_token"}
defined $request->{"site_id"}
length $request->{"advertising_token"}
Truth table100%3/3
A and B
A= defined $request->{"site_id"}B= length $request->{"advertising_token"}
| |||||||||||||||||||||||||||||||||
| 195 | 2 | croak 'only one of siteId or advertisingToken can be specified'; | |||||||||||||||||||||||||||||||
| 196 | } elsif (defined $request->{site_id}) { | ||||||||||||||||||||||||||||||||
| 197 | 28 | $site_id = $request->{site_id}; | |||||||||||||||||||||||||||||||
| 198 | 28 | $site_key_site_id = $site_id; | |||||||||||||||||||||||||||||||
| 199 | } else { | ||||||||||||||||||||||||||||||||
| 200 | 12 | my $decrypted = decrypt_token($request->{advertising_token}, $now, $keys, $request->{identity_scope}); | |||||||||||||||||||||||||||||||
| 201 | 12 | ▶ | if (!$decrypted->{is_success}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 4 | return _error_response(UID2::Client::EncryptionStatus::TOKEN_DECRYPT_FAILURE); | |||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | 8 | $site_id = $decrypted->{site_id}; | |||||||||||||||||||||||||||||||
| 205 | 8 | $site_key_site_id = $decrypted->{site_key_site_id}; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | 36 | $key = $keys->get_active_site_key($site_key_site_id, $now); | |||||||||||||||||||||||||||||||
| 208 | 36 | ▶ | unless ($key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 10 | return _error_response(UID2::Client::EncryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | } elsif (!$key->is_active($now)) { | ||||||||||||||||||||||||||||||||
| 212 | 8 | return _error_response(UID2::Client::EncryptionStatus::KEY_INACTIVE); | |||||||||||||||||||||||||||||||
| 213 | } else { | ||||||||||||||||||||||||||||||||
| 214 | 12 | $site_id = $key->site_id; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 38 | my $iv = $request->{initialization_vector}; | |||||||||||||||||||||||||||||||
| 218 | 38 | ▶ | if (defined $iv && length($iv) != $GCM_IV_LEN) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $iv and length $iv != $GCM_IV_LEN
MC/DC0%0/2
defined $iv and length $iv != $GCM_IV_LEN
defined $iv
length $iv != $GCM_IV_LEN
Truth table66%2/3
A and B
A= defined $ivB= length $iv != $GCM_IV_LEN
| |||||||||||||||||||||||||||||||||
| 219 | 0 | croak "initialization vector size must be $GCM_IV_LEN"; | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 38 | my $payload = pack 'q> N! a*', $now->get_epoch_milli, $site_id, $data; | |||||||||||||||||||||||||||||||
| 223 | 38 | my $identity = $EncryptedDataTypeV3 | ($request->{identity_scope} << 4) | 0xB; | |||||||||||||||||||||||||||||||
| 224 | 38 | my $version = $EncryptedDataVersionV3; | |||||||||||||||||||||||||||||||
| 225 | 38 | my $res = pack 'C C N! a*', $identity, $version, $key->id, encrypt_gcm($payload, $key->secret, $iv); | |||||||||||||||||||||||||||||||
| 226 | +{ | ||||||||||||||||||||||||||||||||
| 227 | 38 | is_success => 1, | |||||||||||||||||||||||||||||||
| 228 | status => UID2::Client::EncryptionStatus::SUCCESS, | ||||||||||||||||||||||||||||||||
| 229 | encrypted_data => encode_b64($res), | ||||||||||||||||||||||||||||||||
| 230 | }; | ||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 52 | ▶ | sub decrypt_data { | ||||||||||||||||||||||||||||||
|
Subroutine
decrypt_data: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 234 | 52 | my $encrypted_data = shift; | |||||||||||||||||||||||||||||||
| 235 | 52 | my $result = eval { | |||||||||||||||||||||||||||||||
| 236 | 52 | my $bytes = decode_b64($encrypted_data); | |||||||||||||||||||||||||||||||
| 237 | 52 | ▶ | unless (defined $bytes) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 6 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | 46 | ▶ | if ((ord(substr($bytes, 0, 1)) & 224) == $EncryptedDataTypeV3) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | 38 | _decrypt_data_v3($bytes, @_); | |||||||||||||||||||||||||||||||
| 242 | } else { | ||||||||||||||||||||||||||||||||
| 243 | 8 | _decrypt_data_v2($bytes, @_); | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | 52 | ▶ | }; if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 6 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | 46 | $result; | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 8 | ▶ | sub _decrypt_data_v2 { | ||||||||||||||||||||||||||||||
|
Subroutine
_decrypt_data_v2: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 252 | 8 | my ($encrypted_bytes, $keys) = @_; | |||||||||||||||||||||||||||||||
| 253 | 8 | my ($type, $version, $encrypted_at, $site_id, $key_id, $bytes) = unpack 'a a q> N! N! a*', $encrypted_bytes; | |||||||||||||||||||||||||||||||
| 254 | 8 | ▶ | if (ord($type) != $EncryptedDataType) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 255 | 3 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD_TYPE); | |||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||
| 257 | 5 | ▶ | if (ord($version) != $EncryptedDataVersion) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 258 | 1 | return _error_response(UID2::Client::DecryptionStatus::VERSION_NOT_SUPPORTED); | |||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||
| 260 | 4 | my $key = $keys->get($key_id); | |||||||||||||||||||||||||||||||
| 261 | 4 | ▶ | unless ($key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | 1 | return _error_response(UID2::Client::DecryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | 3 | my $data = decrypt_cbc($bytes, $key->secret); | |||||||||||||||||||||||||||||||
| 265 | +{ | ||||||||||||||||||||||||||||||||
| 266 | 1 | is_success => 1, | |||||||||||||||||||||||||||||||
| 267 | status => UID2::Client::DecryptionStatus::SUCCESS, | ||||||||||||||||||||||||||||||||
| 268 | decrypted_data => $data, | ||||||||||||||||||||||||||||||||
| 269 | encrypted_at => UID2::Client::Timestamp->from_epoch_milli($encrypted_at), | ||||||||||||||||||||||||||||||||
| 270 | }; | ||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | 38 | ▶ | sub _decrypt_data_v3 { | ||||||||||||||||||||||||||||||
|
Subroutine
_decrypt_data_v3: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||
| 274 | 38 | my ($encrypted_bytes, $keys, $identity_scope) = @_; | |||||||||||||||||||||||||||||||
| 275 | 38 | my ($identity, $version, $key_id, $bytes) = unpack 'a a N! a*', $encrypted_bytes; | |||||||||||||||||||||||||||||||
| 276 | 38 | ▶ | if (_decode_identity_scope($identity) != $identity_scope) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_IDENTITY_SCOPE); | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | 38 | ▶ | if (ord($version) != $EncryptedDataVersionV3) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 280 | 2 | return _error_response(UID2::Client::DecryptionStatus::VERSION_NOT_SUPPORTED); | |||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | 36 | my $key = $keys->get($key_id); | |||||||||||||||||||||||||||||||
| 283 | 36 | ▶ | unless ($key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 284 | 2 | return _error_response(UID2::Client::DecryptionStatus::NOT_AUTHORIZED_FOR_KEY); | |||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | 34 | my $payload = decrypt_gcm($bytes, $key->secret); | |||||||||||||||||||||||||||||||
| 287 | 30 | ▶ | unless (defined $payload) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | 0 | return _error_response(UID2::Client::DecryptionStatus::INVALID_PAYLOAD); | |||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||
| 290 | 30 | my ($encrypted_at, $data) = unpack 'q> x4 a*', $payload; | |||||||||||||||||||||||||||||||
| 291 | +{ | ||||||||||||||||||||||||||||||||
| 292 | 30 | is_success => 1, | |||||||||||||||||||||||||||||||
| 293 | status => UID2::Client::DecryptionStatus::SUCCESS, | ||||||||||||||||||||||||||||||||
| 294 | decrypted_data => $data, | ||||||||||||||||||||||||||||||||
| 295 | encrypted_at => UID2::Client::Timestamp->from_epoch_milli($encrypted_at), | ||||||||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | 65 | ▶ | sub _error_response { | ||||||||||||||||||||||||||||||
|
Subroutine
_error_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 300 | +{ | ||||||||||||||||||||||||||||||||
| 301 | 65 | is_success => undef, | |||||||||||||||||||||||||||||||
| 302 | status => $_[0], | ||||||||||||||||||||||||||||||||
| 303 | }; | ||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | 59 | ▶ | sub _decode_identity_scope { | ||||||||||||||||||||||||||||||
|
Subroutine
_decode_identity_scope: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 307 | 59 | (ord($_[0]) >> 4) & 1; | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | sub encrypt_cbc { | ||||||||||||||||||||||||||||||
|
Subroutine
encrypt_cbc: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 311 | 0 | my ($data, $secret, $iv) = @_; | |||||||||||||||||||||||||||||||
| 312 | 0 | my $cipher = Crypt::Mode::CBC->new('AES'); | |||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | $iv //= random_bytes($CBC_IV_LEN); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$iv //= &random_bytes($CBC_IV_LEN)
MC/DC0%0/2
$iv //= &random_bytes($CBC_IV_LEN)
$iv
&random_bytes($CBC_IV_LEN)
Truth table0%0/3
A //= B
A= $ivB= &random_bytes($CBC_IV_LEN)
| |||||||||||||||||||||||||||||||||
| 314 | 0 | $iv . $cipher->encrypt($data, $secret, $iv); | |||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 10 | ▶ | sub decrypt_cbc { | ||||||||||||||||||||||||||||||
|
Subroutine
decrypt_cbc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 318 | 10 | my ($data, $secret) = @_; | |||||||||||||||||||||||||||||||
| 319 | 10 | my $iv = substr $data, 0, $CBC_IV_LEN; | |||||||||||||||||||||||||||||||
| 320 | 10 | my $cipher = Crypt::Mode::CBC->new('AES'); | |||||||||||||||||||||||||||||||
| 321 | 10 | my $payload = substr $data, $CBC_IV_LEN; | |||||||||||||||||||||||||||||||
| 322 | 10 | $cipher->decrypt($payload, $secret, $iv); | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | 198 | ▶ | sub encrypt_gcm { | ||||||||||||||||||||||||||||||
|
Subroutine
encrypt_gcm: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 326 | 198 | my ($data, $secret, $iv) = @_; | |||||||||||||||||||||||||||||||
| 327 | 198 | ▶ | $iv //= random_bytes($GCM_IV_LEN); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$iv //= &random_bytes($GCM_IV_LEN)
MC/DC0%0/2
$iv //= &random_bytes($GCM_IV_LEN)
$iv
&random_bytes($GCM_IV_LEN)
Truth table66%2/3
A //= B
A= $ivB= &random_bytes($GCM_IV_LEN)
| |||||||||||||||||||||||||||||||||
| 328 | 198 | my $ae = Crypt::AuthEnc::GCM->new('AES', $secret, $iv); | |||||||||||||||||||||||||||||||
| 329 | 198 | my $ciphertext = $ae->encrypt_add($data); | |||||||||||||||||||||||||||||||
| 330 | 198 | $iv . $ciphertext . $ae->encrypt_done(); | |||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 229 | ▶ | sub decrypt_gcm { | ||||||||||||||||||||||||||||||
|
Subroutine
decrypt_gcm: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 334 | 229 | my ($data, $secret) = @_; | |||||||||||||||||||||||||||||||
| 335 | 229 | my $iv = substr $data, 0, $GCM_IV_LEN; | |||||||||||||||||||||||||||||||
| 336 | 229 | my $ae = Crypt::AuthEnc::GCM->new('AES', $secret, $iv); | |||||||||||||||||||||||||||||||
| 337 | 229 | my $payload = substr $data, $GCM_IV_LEN, -$GCM_AUTHTAG_LEN; | |||||||||||||||||||||||||||||||
| 338 | 229 | my $plaintext = $ae->decrypt_add($payload); | |||||||||||||||||||||||||||||||
| 339 | 229 | my $authtag = substr $data, -$GCM_AUTHTAG_LEN, $GCM_AUTHTAG_LEN; | |||||||||||||||||||||||||||||||
| 340 | 229 | ▶ | $ae->decrypt_done($authtag) or croak 'auth data check failed'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 341 | 222 | $plaintext; | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 4 | 1; | |||||||||||||||||||||||||||||||
| 345 | __END__ | ||||||||||||||||||||||||||||||||