| 1 | package VAPID; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.006; use strict; use warnings; our $VERSION = '2.00'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Crypt::JWT qw(encode_jwt); use Crypt::PK::ECC; use URI; use MIME::Base64 qw/encode_base64url decode_base64url/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate); use Crypt::KeyDerivation qw(hkdf); use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use HTTP::Request; use LWP::UserAgent; use JSON qw(decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base 'Import::Export'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | our (%EX, $DEFAULT_SECONDS, $MAX_DEFAULT_SECONDS); | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | $DEFAULT_SECONDS = 12 * 60 * 60; # 12 hours | |||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | $MAX_DEFAULT_SECONDS = 24 * 60 * 60; # 24 hours | |||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | %EX = ( | |||||||||||||||||||||||||||||||||||||||||
| 14 | generate_vapid_keys => [qw/all generate/], | ||||||||||||||||||||||||||||||||||||||||||
| 15 | generate_future_expiration_timestamp => [qw/all generate/], | ||||||||||||||||||||||||||||||||||||||||||
| 16 | generate_vapid_header => [qw/all generate/], | ||||||||||||||||||||||||||||||||||||||||||
| 17 | validate_subject => [qw/all validate/], | ||||||||||||||||||||||||||||||||||||||||||
| 18 | validate_public_key => [qw/all validate/], | ||||||||||||||||||||||||||||||||||||||||||
| 19 | validate_private_key => [qw/all validate/], | ||||||||||||||||||||||||||||||||||||||||||
| 20 | validate_expiration => [qw/all validate/], | ||||||||||||||||||||||||||||||||||||||||||
| 21 | validate_subscription => [qw/all validate/], | ||||||||||||||||||||||||||||||||||||||||||
| 22 | encrypt_payload => [qw/all encrypt/], | ||||||||||||||||||||||||||||||||||||||||||
| 23 | build_push_request => [qw/all push/], | ||||||||||||||||||||||||||||||||||||||||||
| 24 | send_push_notification => [qw/all push/], | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | sub generate_vapid_keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_vapid_keys: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | my $curve = Crypt::PK::ECC->new(); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | $curve->generate_key('prime256v1'); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | my $priv = $curve->export_key_raw('private'); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | my $pub = $curve->export_key_raw('public'); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | if (length($priv) < 32) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $padding = 32 - length $priv; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $priv = ("\0" x $padding) . $priv; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | if (length($pub) < 65) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $padding = 65 - length $pub; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $pub = ("\0" x $padding) . $pub; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | encode_base64url($pub), | |||||||||||||||||||||||||||||||||||||||||
| 46 | encode_base64url($priv) | ||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 7 | ▶ | sub generate_vapid_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_vapid_header: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | my ($aud, $subject, $pub, $priv, $expiration, $enc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | if (!$aud) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | die "No audience could be generated for VAPID."; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | if (ref $aud) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | die "The audience value must be a string containing the origin of a push service"; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | my $aud_uri = URI->new($aud); | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | ▶ | if (!$aud_uri->host) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | die "VAPID audience is not a url."; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | validate_subject($subject); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | validate_public_key($pub); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 7 | $priv = validate_private_key($priv); | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | if ($expiration) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | validate_expiration($expiration); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | $expiration = generate_future_expiration_timestamp(); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 7 | my $payload = { | |||||||||||||||||||||||||||||||||||||||||
| 78 | aud => $aud, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | exp => $expiration, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | sub => $subject | ||||||||||||||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 7 | my $key = Crypt::PK::ECC->new | |||||||||||||||||||||||||||||||||||||||||
| 84 | ->import_key_raw($priv, 'prime256v1') | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ->export_key_pem('private'); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | my $jwt_token = encode_jwt( | |||||||||||||||||||||||||||||||||||||||||
| 89 | payload=>$payload, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | extra_headers => { typ => 'JWT' }, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | alg=>'ES256', | ||||||||||||||||||||||||||||||||||||||||||
| 92 | key => \$key | ||||||||||||||||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 7 | ▶ | return $enc | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | ? { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | Authorization => "vapid t=${jwt_token}, k=${pub}" | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | : { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | Authorization => 'WebPush ' . $jwt_token, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 'Crypto-Key' => 'p256ecdsa=' . $pub | ||||||||||||||||||||||||||||||||||||||||||
| 102 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | ▶ | sub generate_future_expiration_timestamp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_future_expiration_timestamp: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | my ($add) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 8 | ▶ | return time + ($add || $DEFAULT_SECONDS); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$add || $VAPID::DEFAULT_SECONDS
MC/DC0%0/2
$add || $VAPID::DEFAULT_SECONDS
$add
$VAPID::DEFAULT_SECONDS
Truth table66%2/3
A || B
A= $addB= $VAPID::DEFAULT_SECONDS
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | ▶ | sub validate_subject { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_subject: called CC=5 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | my ($subject) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 8 | ▶ | if (!$subject) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | die "No subject passed to validate_subject"; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 8 | ▶ | if (ref $subject) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | die "The subject value must be a string containing a URL or 'mailto: address.'"; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 8 | ▶ | unless ($subject =~ m/^mailto\:/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my $uri = URI->new($subject); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (!$uri->host) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | die "VAPID subject is not a url or mailto: address"; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 8 | return $subject; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | ▶ | sub validate_public_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_public_key: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | my ($pub) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | ▶ | if (!$pub) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | die "No public key passed to validate_public_key"; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 8 | ▶ | if (ref $pub) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | die "Vapid public key is must be a URL safe Base 64 encoded string"; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | $pub = decode_base64url($pub); | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 8 | ▶ | if (length $pub != 65) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | die "VAPID public key should be 65 bytes long when decoded."; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 8 | return $pub; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 8 | ▶ | sub validate_private_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_private_key: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 8 | my ($priv) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 8 | ▶ | if (!$priv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | die "No private key passed to validate_private_key"; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 8 | ▶ | if (ref $priv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | die "VAPID private key is must be a URL safe Base 64 encoded string"; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | $priv = decode_base64url($priv); | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 8 | ▶ | if (length $priv != 32) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | die "VAPID private key should be 32 bytes long when decoded."; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 8 | return $priv; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 4 | ▶ | sub validate_expiration { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_expiration: called CC=4 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 4 | my $expiration = shift; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 4 | ▶ | if (!$expiration || $expiration !~ m/^\d+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$expiration and $expiration =~ /^\d+$/
MC/DC0%0/2
$expiration and $expiration =~ /^\d+$/
$expiration
$expiration =~ /^\d+$/
Truth table33%1/3
A and B
A= $expirationB= $expiration =~ /^\d+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | die "expiration value must be a number"; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 4 | my $max = generate_future_expiration_timestamp($MAX_DEFAULT_SECONDS); | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 4 | ▶ | if ($expiration >= $max) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | die "expiration value is greater than maximum of 24 hours"; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | return $expiration; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 18 | ▶ | sub validate_subscription { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_subscription: called CC=11 SCAR=24.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 18 | my ($subscription) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 18 | ▶ | if (!$subscription) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | die "No subscription passed to validate_subscription"; | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 17 | ▶ | if (!ref $subscription || ref $subscription ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $subscription or ref $subscription ne "HASH"
MC/DC50%1/2
not ref $subscription or ref $subscription ne "HASH"
not ref $subscription
ref $subscription ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $subscriptionB= ref $subscription ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | die "Subscription must be a hash reference"; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 16 | ▶ | if (!$subscription->{endpoint}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | die "Subscription must have an endpoint"; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 15 | my $uri = URI->new($subscription->{endpoint}); | |||||||||||||||||||||||||||||||||||||||||
| 203 | 15 | ▶ | if (!$uri->host) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | die "Subscription endpoint is not a valid URL"; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 15 | ▶ | if (!$subscription->{keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | die "Subscription must have keys"; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 14 | ▶ | if (!$subscription->{keys}{p256dh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | die "Subscription must have a p256dh key"; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 13 | ▶ | if (!$subscription->{keys}{auth}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | die "Subscription must have an auth key"; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 12 | ▶ | if (length(decode_base64url($subscription->{keys}{p256dh})) != 65) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | die "Subscription p256dh should be 65 bytes long when decoded"; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 12 | ▶ | if (length(decode_base64url($subscription->{keys}{auth})) != 16) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | die "Subscription auth should be 16 bytes long when decoded"; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 12 | return $subscription; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | ▶ | sub encrypt_payload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt_payload: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | my ($payload, $subscription, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 8 | ▶ | if (!defined $payload) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | die "No payload passed to encrypt_payload"; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 7 | validate_subscription($subscription); | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 7 | my $ua_public = decode_base64url($subscription->{keys}{p256dh}); | |||||||||||||||||||||||||||||||||||||||||
| 240 | 7 | my $auth_secret = decode_base64url($subscription->{keys}{auth}); | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 7 | ▶ | my $salt = $opts{salt} || random_bytes(16); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$opts{'salt'} || &random_bytes(16)
MC/DC0%0/2
$opts{'salt'} || &random_bytes(16)
$opts{'salt'}
&random_bytes(16)
Truth table66%2/3
A || B
A= $opts{'salt'}B= &random_bytes(16)
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 7 | ▶ | if (length $salt != 16) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | die "salt must be 16 bytes"; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 7 | my $local_key = $opts{local_key}; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 7 | ▶ | if (!$local_key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 6 | $local_key = Crypt::PK::ECC->new(); | |||||||||||||||||||||||||||||||||||||||||
| 250 | 6 | $local_key->generate_key('prime256v1'); | |||||||||||||||||||||||||||||||||||||||||
| 251 | } elsif (!ref $local_key) { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | $local_key = Crypt::PK::ECC->new->import_key_raw($local_key, 'prime256v1'); | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 7 | my $as_public = $local_key->export_key_raw('public'); | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 7 | my $ua_key = Crypt::PK::ECC->new->import_key_raw($ua_public, 'prime256v1'); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | my $ecdh_secret = $local_key->shared_secret($ua_key); | |||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 7 | my $key_info = "WebPush: info\x00" . $ua_public . $as_public; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | my $ikm = hkdf($ecdh_secret, $auth_secret, 'SHA256', 32, $key_info); | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | my $cek = hkdf($ikm, $salt, 'SHA256', 16, "Content-Encoding: aes128gcm\x00"); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 7 | my $nonce = hkdf($ikm, $salt, 'SHA256', 12, "Content-Encoding: nonce\x00"); | |||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | ▶ | my $rs = $opts{record_size} || 4096; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'record_size'} || 4096
MC/DC0%0/1
$opts{'record_size'} || 4096
$opts{'record_size'}
Truth table50%1/2
A
A= $opts{'record_size'}
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 7 | my ($ciphertext, $tag) = gcm_encrypt_authenticate( | |||||||||||||||||||||||||||||||||||||||||
| 268 | 'AES', $cek, $nonce, '', $payload . "\x02" | ||||||||||||||||||||||||||||||||||||||||||
| 269 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 7 | my $header = $salt | |||||||||||||||||||||||||||||||||||||||||
| 272 | . pack('N', $rs) | ||||||||||||||||||||||||||||||||||||||||||
| 273 | . pack('C', length $as_public) | ||||||||||||||||||||||||||||||||||||||||||
| 274 | . $as_public; | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 7 | return $header . $ciphertext . $tag; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 5 | ▶ | sub build_push_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_push_request: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 5 | my (%args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 5 | my $subscription = $args{subscription}; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 5 | my $payload = $args{payload}; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 5 | my $vapid_public = $args{vapid_public}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 5 | my $vapid_private = $args{vapid_private}; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 5 | my $subject = $args{subject}; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 5 | ▶ | my $ttl = $args{ttl} // 60; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"ttl"} // 60
MC/DC100%1/1
$args{"ttl"} // 60
$args{"ttl"}
Truth table100%2/2
A
A= $args{"ttl"}
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 5 | my $expiration = $args{expiration}; | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 5 | ▶ | if (defined $args{encoding} && $args{encoding} ne 'aes128gcm') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"encoding"} and $args{"encoding"} ne "aes128gcm"
MC/DC50%1/2
defined $args{"encoding"} and $args{"encoding"} ne "aes128gcm"
defined $args{"encoding"}
$args{"encoding"} ne "aes128gcm"
Truth table66%2/3
A and B
A= defined $args{"encoding"}B= $args{"encoding"} ne "aes128gcm"
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | die "Unsupported encoding '$args{encoding}': VAPID 2.00 sends " | |||||||||||||||||||||||||||||||||||||||||
| 292 | . "RFC 8291 aes128gcm only"; | ||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 4 | validate_subscription($subscription); | |||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 4 | my $endpoint = $subscription->{endpoint}; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 4 | my $uri = URI->new($endpoint); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 4 | my $audience = $uri->scheme . '://' . $uri->host; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 4 | ▶ | if (defined $uri->port && defined $uri->default_port | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $uri->port and defined $uri->default_port and $uri->port != $uri->default_port
defined $uri->port and defined $uri->default_port
MC/DC33%1/3
defined $uri->port and defined $uri->default_port and $uri->port != $uri->default_port
defined $uri->port
defined $uri->default_port
$uri->port != $uri->default_port
Truth table50%2/4
A and B and C
A= defined $uri->portB= defined $uri->default_portC= $uri->port != $uri->default_port
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | && $uri->port != $uri->default_port) { | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | $audience .= ':' . $uri->port; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 4 | my $vapid_headers = generate_vapid_header( | |||||||||||||||||||||||||||||||||||||||||
| 306 | $audience, | ||||||||||||||||||||||||||||||||||||||||||
| 307 | $subject, | ||||||||||||||||||||||||||||||||||||||||||
| 308 | $vapid_public, | ||||||||||||||||||||||||||||||||||||||||||
| 309 | $vapid_private, | ||||||||||||||||||||||||||||||||||||||||||
| 310 | $expiration, | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | ||||||||||||||||||||||||||||||||||||||||||
| 312 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | my $req = HTTP::Request->new(POST => $endpoint); | |||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | $req->header(TTL => $ttl); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 4 | $req->header(Authorization => $vapid_headers->{Authorization}); | |||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | ▶ | if (defined $payload && length $payload) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $payload and length $payload
MC/DC50%1/2
defined $payload and length $payload
defined $payload
length $payload
Truth table66%2/3
A and B
A= defined $payloadB= length $payload
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | $req->header('Content-Encoding' => 'aes128gcm'); | |||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | $req->header('Content-Type' => 'application/octet-stream'); | |||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | $req->content(encrypt_payload($payload, $subscription)); | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 4 | ▶ | $req->header('Content-Length' => length($req->content // '')); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$req->content // ""
MC/DC0%0/1
$req->content // ""
$req->content
Truth table50%1/2
A
A= $req->content
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 4 | return $req; | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | sub send_push_notification { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_push_notification: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | my (%args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $ua = $args{ua} // LWP::UserAgent->new(timeout => 30); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$args{"ua"} // "LWP::UserAgent"->new("timeout", 30)
MC/DC0%0/2
$args{"ua"} // "LWP::UserAgent"->new("timeout", 30)
$args{"ua"}
"LWP::UserAgent"->new("timeout", 30)
Truth table0%0/3
A // B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", 30)
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $req = build_push_request(%args); | |||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | my $resp = $ua->request($req); | |||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | success => $resp->is_success, | |||||||||||||||||||||||||||||||||||||||||
| 339 | status => $resp->code, | ||||||||||||||||||||||||||||||||||||||||||
| 340 | message => $resp->message, | ||||||||||||||||||||||||||||||||||||||||||
| 341 | response => $resp | ||||||||||||||||||||||||||||||||||||||||||
| 342 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | __END__ | ||||||||||||||||||||||||||||||||||||||||||