| 1 | package Crypt::Age::Stanza; | ||||||||||
| 2 | # ABSTRACT: Base class for age recipient stanzas | ||||||||||
| 3 | 7 | our $VERSION = '0.005'; | |||||||||
| 4 | 7 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 7 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | 7 | has type => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | required => 1, | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | has args => ( | ||||||||||
| 17 | is => 'ro', | ||||||||||
| 18 | 2 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 7 | ); | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 7 | has body => ( | |||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | default => '', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 7758 | ▶ | sub encode_base64_no_padding { | ||||||||
|
Subroutine
encode_base64_no_padding: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | 7758 | my ($data) = @_; | |||||||||
| 30 | 7758 | my $encoded = encode_base64($data, ''); | |||||||||
| 31 | 7758 | $encoded =~ s/=+$//; # Remove padding | |||||||||
| 32 | 7758 | return $encoded; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 5618 | ▶ | sub decode_base64_no_padding { | ||||||||
|
Subroutine
decode_base64_no_padding: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 36 | 5618 | my ($encoded) = @_; | |||||||||
| 37 | |||||||||||
| 38 | # "decoders MUST reject non-canonical encodings and encodings ending with | ||||||||||
| 39 | # '=' padding characters" -- c2sp.org/age. MIME::Base64 does neither: it | ||||||||||
| 40 | # silently skips characters outside the alphabet, drops a stray trailing | ||||||||||
| 41 | # character, and ignores the unused bits of the last group. Every check | ||||||||||
| 42 | # below is therefore ours, and none of them names $encoded in its message -- | ||||||||||
| 43 | # a stanza body is wrapped key material. | ||||||||||
| 44 | 5618 | ▶ | croak "Invalid base64: '=' padding is not allowed in the age format" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | if $encoded =~ m{=}; | ||||||||||
| 46 | 5616 | ▶ | croak "Invalid base64: character outside the RFC 4648 section 4 alphabet" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | if $encoded =~ m{[^A-Za-z0-9+/]}; | ||||||||||
| 48 | 5612 | ▶ | croak "Invalid base64: length is not a valid unpadded encoding" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | if length($encoded) % 4 == 1; | ||||||||||
| 50 | |||||||||||
| 51 | 5611 | my $pad = (4 - length($encoded) % 4) % 4; | |||||||||
| 52 | 5611 | my $decoded = decode_base64($encoded . ('=' x $pad)); | |||||||||
| 53 | |||||||||||
| 54 | # Re-encode and compare. This is what catches non-canonical trailing bits: | ||||||||||
| 55 | # the unused low bits of the final group must be zero, so the canonical | ||||||||||
| 56 | # encoding of the decoded bytes has to be byte-identical to the input. | ||||||||||
| 57 | 5611 | ▶ | croak "Invalid base64: non-canonical encoding" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | unless encode_base64_no_padding($decoded) eq $encoded; | ||||||||||
| 59 | |||||||||||
| 60 | 5606 | return $decoded; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1055 | ▶ | sub to_string { | ||||||||
|
Subroutine
to_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 64 | 1055 | my ($self) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 1055 | my @parts = ('->', $self->type, @{$self->args}); | |||||||||
| 67 | 1055 | my $header_line = join(' ', @parts); | |||||||||
| 68 | |||||||||||
| 69 | 1055 | my $body_b64 = encode_base64_no_padding($self->body); | |||||||||
| 70 | |||||||||||
| 71 | # Split into 64-char lines. The ABNF is | ||||||||||
| 72 | # stanza = arg-line *full-line final-line | ||||||||||
| 73 | # final-line = *63base64char LF | ||||||||||
| 74 | # so the final line is at most 63 characters and a body whose encoding is an | ||||||||||
| 75 | # exact multiple of 64 MUST be followed by an empty final line -- hence >= | ||||||||||
| 76 | # and not >. Header::parse's matching `last if $len < 64` depends on it, and | ||||||||||
| 77 | # so does the header MAC. | ||||||||||
| 78 | 1055 | my @lines = ($header_line); | |||||||||
| 79 | 1055 | ▶ | while (length($body_b64) >= 64) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 8 | push @lines, substr($body_b64, 0, 64, ''); | |||||||||
| 81 | } | ||||||||||
| 82 | 1055 | push @lines, $body_b64; # final line, empty when the body ended flush at 64 | |||||||||
| 83 | |||||||||||
| 84 | 1055 | return join("\n", @lines); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | 7 | 1; | |||||||||
| 92 | |||||||||||
| 93 | __END__ | ||||||||||