Punk-Push

 view release on metacpan or  search on metacpan

t/04-encrypt.t  view on Meta::CPAN

#!perl
use 5.010;
use strict;
use warnings;
use Test::More;
use MIME::Base64 qw/encode_base64url decode_base64url/;
use Crypt::PK::ECC;
use Crypt::KeyDerivation qw(hkdf);
use Crypt::AuthEnc::GCM qw(gcm_decrypt_verify);
use File::Raw::JSON qw(file_json_decode);
use VAPID ();
use Punk ();
use Punk::Plugin::Push ();

# RFC 8291's own worked example is asserted in VAPID's suite, where the
# arithmetic lives. What is proved here is the WIRING: that this plugin hands
# VAPID the right subscription keys and puts the resulting record on the wire
# unaltered.
#
# So the test plays the receiver. It generates a subscription whose private key
# it keeps, takes the body the plugin produced, and decrypts it the way a
# browser would. If the plugin passed the wrong key, swapped the two public
# keys in the context, or touched the bytes afterwards, this cannot pass.

our ($PUB, $PRIV) = VAPID::generate_vapid_keys();
our %OPTS = (subject => 'mailto:ops@example.com',
             public_key => $PUB, private_key => $PRIV,
             guard => sub { 1 });

{
    package TFake::Res;    sub new { bless { %{$_[1]} }, $_[0] } sub status { 201 }
    package TFake::Future; sub new { bless {}, shift } sub get { TFake::Res->new({}) }
    package TFake::UA;
    sub new  { bless { calls => [] }, shift }
    sub post { my ($s,$u,%o)=@_; push @{$s->{calls}}, {url=>$u,%o}; TFake::Future->new }
    sub last_body { $_[0]{calls}[-1]{body} }
}

eval <<'PERL' or die $@;
package PushEnc;
use Punk;
host 'https://example.com';
plugin 'Push' => { %main::OPTS };
1;
PERL
PushEnc->to_app;
my $app = PushEnc->punk_app;

my $ua = TFake::UA->new;
Punk::Plugin::Push->config_for($app)->{ua} = $ua;

# A subscription we hold both halves of, so we can play the browser.
my $ua_key = Crypt::PK::ECC->new;
$ua_key->generate_key('prime256v1');
my $auth_secret = join '', map { chr rand 256 } 1 .. 16;

my $subscription = {
    endpoint => 'https://push.example.net/p/1',
    keys => { p256dh => encode_base64url($ua_key->export_key_raw('public')),
              auth   => encode_base64url($auth_secret) },
};

# What a browser does with the body, per RFC 8291 section 3.4 over RFC 8188.
sub decrypt {
    my ($body) = @_;

    my $salt   = substr $body, 0, 16;
    my $rs     = unpack 'N', substr($body, 16, 4);
    my $idlen  = unpack 'C', substr($body, 20, 1);
    my $as_pub = substr $body, 21, $idlen;
    my $sealed = substr $body, 21 + $idlen;

    my $as = Crypt::PK::ECC->new->import_key_raw($as_pub, 'prime256v1');
    my $ecdh = $ua_key->shared_secret($as);

    my $ikm = hkdf($ecdh, $auth_secret, 'SHA256', 32,
        "WebPush: info\x00" . $ua_key->export_key_raw('public') . $as_pub);
    my $cek   = hkdf($ikm, $salt, 'SHA256', 16, "Content-Encoding: aes128gcm\x00");
    my $nonce = hkdf($ikm, $salt, 'SHA256', 12, "Content-Encoding: nonce\x00");

    my $ct  = substr($sealed, 0, length($sealed) - 16);
    my $tag = substr($sealed, -16);
    my $plain = gcm_decrypt_verify('AES', $cek, $nonce, '', $ct, $tag);
    return ($plain, $rs, $idlen);
}

# ---- the record a browser receives -----------------------------------------



( run in 1.308 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )