| 1 | package AWS::Signature::V4::Chunker; | ||
| 2 | use v5.24; | ||
| 3 | use Moo; | ||
| 4 | use AWS::Signature::V4::Error qw< fail shown >; | ||
| 5 | use Digest::SHA qw< sha256_hex hmac_sha256_hex >; | ||
| 6 | use experimental qw< signatures >; | ||
| 7 | use namespace::clean; | ||
| 8 | |||
| 9 | use AWS::Signature::V4::Checksum (); | ||
| 10 | |||
| 11 | # The chunker is created by AWS::Signature::V4::sign(), which knows all of | ||
| 12 | # this: the arguments are not a public interface. | ||
| 13 | has signed => (is => 'ro', required => 1); | ||
| 14 | has amzdate => (is => 'ro'); | ||
| 15 | has scope => (is => 'ro'); | ||
| 16 | |||
| 17 | # The signing key, for signed chunks only, can sign any request for the same | ||
| 18 | # service, region and day: it is kept in a closure, so that no accessor gives | ||
| 19 | # it back and dumping the object does not show it. The closure can still sign | ||
| 20 | # with it, so the chunker is no safer to share than the signer. | ||
| 21 | has _mac => ( | ||
| 22 | is => 'ro', | ||
| 23 | init_arg => 'key', | ||
| 24 | coerce => sub ($key) { | ||
| 25 | ref $key eq 'CODE' ? $key : sub ($data) { hmac_sha256_hex($data, $key) }; | ||
| 26 | }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | # signature of the previous chunk: the chain must not be changed from outside | ||
| 30 | has _previous => (is => 'rw', init_arg => 'previous'); | ||
| 31 | has expected => (is => 'ro', required => 1); # declared size of the data | ||
| 32 | has checksum => (is => 'ro'); # name of the built-in checksum, if any | ||
| 33 | has trailer_names => (is => 'ro', default => sub { [] }); | ||
| 34 | |||
| 35 | has _seen => (is => 'rw', init_arg => undef, default => 0); | ||
| 36 | has _done => (is => 'rw', init_arg => undef, default => 0); | ||
| 37 | has _sum => (is => 'lazy', init_arg => undef); | ||
| 38 | |||
| 39 | sub BUILD ($self, $args) { | ||
| 40 | fail 500, 'signed chunks need a "key"' if $self->signed && !$self->_mac; | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _build__sum ($self) { | ||
| 45 | my $algo = $self->checksum; | ||
| 46 | return defined $algo ? AWS::Signature::V4::Checksum->new($algo) : undef; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _sign ($self, $data_ref) { | ||
| 50 | my $string_to_sign = join "\n", 'AWS4-HMAC-SHA256-PAYLOAD', | ||
| 51 | $self->amzdate, $self->scope, $self->_previous, | ||
| 52 | sha256_hex(''), sha256_hex($$data_ref); | ||
| 53 | return $self->_previous($self->_mac->($string_to_sign)); | ||
| 54 | } | ||
| 55 | |||
| 56 | # encoded chunk for the data (a byte string or a reference to one) | ||
| 57 | sub chunk ($self, $data) { | ||
| 58 | fail 400, 'chunker already finished' if $self->_done; | ||
| 59 | fail 400, 'chunk must be a byte string or a reference to one' | ||
| 60 | if ref $data && ref $data ne 'SCALAR'; | ||
| 61 | my $ref = ref $data ? $data : \$data; | ||
| 62 | my $len = length($$ref) // 0; | ||
| 63 | fail 400, 'empty chunk: call finish() to terminate the stream' unless $len; | ||
| 64 | _is_bytes($ref) or fail 400, 'chunk must be a byte string: encode characters first'; | ||
| 65 | my $seen = $self->_seen + $len; | ||
| 66 | fail 400, "more data than the declared @{[ $self->expected ]} bytes" | ||
| 67 | if $seen > $self->expected; | ||
| 68 | $self->_seen($seen); | ||
| 69 | $self->_sum->add($ref) if $self->_sum; | ||
| 70 | my $head = $self->signed | ||
| 71 | ? sprintf('%x;chunk-signature=%s', $len, $self->_sign($ref)) | ||
| 72 | : sprintf('%x', $len); | ||
| 73 | return "$head\r\n" . $$ref . "\r\n"; | ||
| 74 | } | ||
| 75 | |||
| 76 | # the final, empty chunk, followed by the trailers if any; the values of the | ||
| 77 | # trailers that were declared with "trailers" are passed by name, in any case | ||
| 78 | sub finish ($self, %given) { | ||
| 79 | fail 400, 'chunker already finished' if $self->_done; | ||
| 80 | fail 400, "got @{[ $self->_seen ]} bytes instead of the declared @{[ $self->expected ]}" | ||
| 81 | if $self->_seen != $self->expected; | ||
| 82 | my %values; | ||
| 83 | for my $name (sort keys %given) { | ||
| 84 | fail 400, "trailer '" . shown(lc $name) . "' given twice" if exists $values{lc $name}; | ||
| 85 | $values{lc $name} = $given{$name}; | ||
| 86 | } | ||
| 87 | |||
| 88 | # all checks come before the checksum is taken, which is not repeatable, | ||
| 89 | # and before the chain of signatures moves on: after an error, the chunker | ||
| 90 | # is as it was | ||
| 91 | my $computed = $self->_sum ? 'x-amz-checksum-' . $self->checksum : undef; | ||
| 92 | my @pairs; | ||
| 93 | for my $name ($self->trailer_names->@*) { | ||
| 94 | my $value; | ||
| 95 | if (defined $computed && $name eq $computed) { | ||
| 96 | fail 400, "trailer '$name' is computed, not to be given" if exists $values{$name}; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $value = delete $values{$name} // fail 400, "missing value for trailer '$name'"; | ||
| 100 | fail 400, "invalid value for trailer '$name': a byte string, no CR, LF or NUL" | ||
| 101 | if ref $value || $value =~ m{[\r\n\0]} || !_is_bytes(\$value); | ||
| 102 | utf8::downgrade($value); | ||
| 103 | } | ||
| 104 | push @pairs, [$name, $value]; | ||
| 105 | } | ||
| 106 | fail 400, 'undeclared trailers: ' . join(', ', map { shown($_) } sort keys %values) | ||
| 107 | if %values; | ||
| 108 | my @lines = map { "$_->[0]:" . ($_->[1] // $self->_sum->base64) } @pairs; | ||
| 109 | my $trailers = join '', map { "$_\r\n" } @lines; | ||
| 110 | |||
| 111 | my $body; | ||
| 112 | if (!$self->signed) { | ||
| 113 | $body = "0\r\n$trailers\r\n"; | ||
| 114 | } | ||
| 115 | elsif (!@lines) { | ||
| 116 | $body = "0;chunk-signature=@{[ $self->_sign(\'') ]}\r\n\r\n"; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | my $sig = $self->_sign(\''); | ||
| 120 | my $string_to_sign = join "\n", 'AWS4-HMAC-SHA256-TRAILER', | ||
| 121 | $self->amzdate, $self->scope, $self->_previous, | ||
| 122 | sha256_hex(join '', map { "$_\n" } @lines); | ||
| 123 | my $tsig = $self->_mac->($string_to_sign); | ||
| 124 | $body = "0;chunk-signature=$sig\r\n${trailers}x-amz-trailer-signature:$tsig\r\n\r\n"; | ||
| 125 | } | ||
| 126 | $self->_done(1); | ||
| 127 | return $body; | ||
| 128 | } | ||
| 129 | |||
| 130 | # true if the string holds only bytes, even if it is stored as UTF-8; it is | ||
| 131 | # passed by reference, so that big chunks are copied only when they are UTF-8 | ||
| 132 | sub _is_bytes ($ref) { | ||
| 133 | return !utf8::is_utf8($$ref) || utf8::downgrade(my $copy = $$ref, 1); | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; |