Age/Stanza.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 61 / 61 CC 6 SCAR 9.9
CC
6
Cov
100%
decode_base64_no_padding
5.0
to_string
2.0
BEGIN
1.0
CRAP 2.7
« Age/Keys.pm ↑ Summary Age/Stanza/X25519.pm »
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
BranchTrueFalseTotal
if $encoded =~ /=/ 2 5616 5618
45 if $encoded =~ m{=};
46 5616 ▶ croak "Invalid base64: character outside the RFC 4648 section 4 alphabet"
Branch: 1 branch
BranchTrueFalseTotal
if $encoded =~ m[[^A-Za-z0-9+/]] 4 5612 5616
47 if $encoded =~ m{[^A-Za-z0-9+/]};
48 5612 ▶ croak "Invalid base64: length is not a valid unpadded encoding"
Branch: 1 branch
BranchTrueFalseTotal
if length($encoded) % 4 == 1 1 5611 5612
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
BranchTrueFalseTotal
unless encode_base64_no_padding($decoded) eq $encoded 5 5606 5611
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
BranchTrueFalseTotal
if (length $body_b64 >= 64) 8 1055 1063
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__
« Age/Keys.pm ↑ Summary Age/Stanza/X25519.pm »