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