blib/lib/File/SOPS/Backend/Age.pm

filter: Statementstmt 100% 73 / 73 Branchbran 73.3% 22 / 30 Conditioncond 48.1% 13 / 27 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 77.6% 125 / 161 CC 28 SCAR 20.0
CC
28
Cov
83%
decrypt_data_key
11.7
can_decrypt
10.1
encrypt_data_key
6.8
CRAP 7.4
« blib/lib/File/SOPS/Encrypted.pm ↑ Summary blib/lib/File/SOPS/Format/JSON.pm »
1 package File::SOPS::Backend::Age;
2 # ABSTRACT: age encryption backend for SOPS
3 84 our $VERSION = '0.004';
4 84 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 84 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 84 ▶use Crypt::Age;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 84 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 84 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Same reason as in the two format handlers (k71): every frame between a
11 # caller and this backend is File::SOPS's own, so "could not decrypt the data
12 # key" named a line in SOPS.pm rather than the line the caller wrote decrypt()
13 # on. The house rule is that an error reports the caller's line, not ours.
14 84 our @CARP_NOT = qw( File::SOPS );
15  
16  
17 695 ▶sub encrypt_data_key {
Subroutine encrypt_data_key: called CC=6 SCAR=19.2
Pod documented
18 695 my ($class, %args) = @_;
19 695 ▶ my $data_key = $args{data_key} // croak "data_key required";
Condition33%1/3
$args{"data_key"} // croak("data_key required")
l!l&&r!l&&!r
69500
MC/DC0%0/2
$args{"data_key"} // croak("data_key required")
$args{"data_key"} croak("data_key required")
Truth table33%1/3
A // B
A= $args{"data_key"}B= croak("data_key required")
ABresult
1X1
011
000
20 695 ▶ my $recipients = $args{recipients} // croak "recipients required";
Condition33%1/3
$args{"recipients"} // croak("recipients required")
l!l&&r!l&&!r
69500
MC/DC0%0/2
$args{"recipients"} // croak("recipients required")
$args{"recipients"} croak("recipients required")
Truth table33%1/3
A // B
A= $args{"recipients"}B= croak("recipients required")
ABresult
1X1
011
000
21  
22 695 ▶ croak "recipients must be an array ref" unless ref($recipients) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $recipients eq "ARRAY" 0 695 695
23 695 ▶ croak "at least one recipient required" unless @$recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless @$recipients 0 695 695
24  
25 695 my @encrypted_keys;
26  
27 695 for my $recipient (@$recipients) {
28 697 my $encrypted = Crypt::Age->encrypt(
29 plaintext => $data_key,
30 recipients => [$recipient],
31 );
32  
33 # Armor the output for SOPS compatibility
34 696 my $armored = _armor($encrypted);
35  
36 696 push @encrypted_keys, {
37 recipient => $recipient,
38 enc => $armored,
39 };
40 }
41  
42 694 return \@encrypted_keys;
43 }
44  
45  
46 553 ▶sub decrypt_data_key {
Subroutine decrypt_data_key: called CC=11 SCAR=24.6
Pod documented
47 553 my ($class, %args) = @_;
48 553 ▶ my $age_keys = $args{age_keys} // croak "age_keys required";
Condition33%1/3
$args{"age_keys"} // croak("age_keys required")
l!l&&r!l&&!r
55300
MC/DC0%0/2
$args{"age_keys"} // croak("age_keys required")
$args{"age_keys"} croak("age_keys required")
Truth table33%1/3
A // B
A= $args{"age_keys"}B= croak("age_keys required")
ABresult
1X1
011
000
49 553 ▶ my $identities = $args{identities} // croak "identities required";
Condition33%1/3
$args{"identities"} // croak("identities required")
l!l&&r!l&&!r
55300
MC/DC0%0/2
$args{"identities"} // croak("identities required")
$args{"identities"} croak("identities required")
Truth table33%1/3
A // B
A= $args{"identities"}B= croak("identities required")
ABresult
1X1
011
000
50  
51 553 ▶ croak "age_keys must be an array ref" unless ref($age_keys) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $age_keys eq "ARRAY" 0 553 553
52 553 ▶ croak "identities must be an array ref" unless ref($identities) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $identities eq "ARRAY" 0 553 553
53  
54 # k192 / CVE-2026-85783 -- Crypt::Age's per-blob stanza DoS (capped upstream
55 # in 0.004) is amplified on this path: the loop below tries every age entry
56 # in the document, and each try costs an X25519 scalar multiplication per
57 # stanza before the header is authenticated, so a document carrying many age
58 # entries multiplies the cost by entries x stanzas x identities. Nothing
59 # before us bounds the entry count, so bound it here, ahead of any age work.
60 # 64 is a deliberately conservative default -- real documents carry a handful
61 # of recipients; a caller that legitimately has more may raise it.
62 553 ▶ my $max_stanzas = $args{max_stanzas} // 64;
Condition100%2/2
$args{"max_stanzas"} // 64
l!l
18535
MC/DC100%1/1
$args{"max_stanzas"} // 64
$args{"max_stanzas"}
Truth table100%2/2
A
A= $args{"max_stanzas"}
Aresult
11
00
63 553 _check_max_stanzas($args{max_stanzas});
64 544 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if @$age_keys > $max_stanzas 11 533 544
65 "SOPS document has %d age stanzas, exceeding the max_stanzas limit of %d; refusing to decrypt",
66 scalar(@$age_keys),
67 $max_stanzas,
68 ) if @$age_keys > $max_stanzas;
69  
70 # The data key the SOPS data path consumes is exactly 32 bytes -- the
71 # AES-256 key every value in the document is encrypted under. A short
72 # return is silently accepted by CryptX as a working AES-128/192 key
73 # (k52, the same defect class as the data-key / IV checks in
74 # Encrypted::_random_bytes); a long return is not a valid AES key and
75 # dies inside CryptX, attributed to gcm and naming neither the CSPRNG
76 # nor the age layer. Crypt::Age 0.001 happens to return 32 bytes; we
77 # cannot rely on that, and we cannot see the inner values (file key,
78 # nonce, ephemeral key) that produced the result.
79 533 my $EXPECTED_DATA_KEY_LEN = 32;
80  
81 533 for my $key_info (@$age_keys) {
82 535 my $encrypted = $key_info->{enc};
83 535 ▶ next unless defined $encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encrypted 0 535 535
84  
85 # Dearmor if needed
86 535 my $ciphertext = _dearmor($encrypted);
87  
88 535 my $data_key = eval {
89 535 Crypt::Age->decrypt(
90 ciphertext => $ciphertext,
91 identities => $identities,
92 );
93 };
94  
95 535 ▶ next unless defined $data_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_key 3 532 535
96  
97 532 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if length $data_key != $EXPECTED_DATA_KEY_LEN 6 526 532
98 "Crypt::Age returned a data key of %d bytes, expected %d",
99 length($data_key),
100 $EXPECTED_DATA_KEY_LEN,
101 ) if length($data_key) != $EXPECTED_DATA_KEY_LEN;
102  
103 526 return $data_key;
104 }
105  
106 1 croak "Could not decrypt data key with any of the provided identities";
107 }
108  
109  
110 8 ▶sub can_decrypt {
Subroutine can_decrypt: called CC=8 SCAR=23.1
Pod documented
111 8 my ($class, %args) = @_;
112 8 ▶ my $age_keys = $args{age_keys} // return 0;
Condition50%1/2
$args{"age_keys"} // (return 0)
l!l
80
MC/DC0%0/1
$args{"age_keys"} // (return 0)
$args{"age_keys"}
Truth table50%1/2
A
A= $args{"age_keys"}
Aresult
11
00
113 8 ▶ my $identities = $args{identities} // return 0;
Condition50%1/2
$args{"identities"} // (return 0)
l!l
80
MC/DC0%0/1
$args{"identities"} // (return 0)
$args{"identities"}
Truth table50%1/2
A
A= $args{"identities"}
Aresult
11
00
114  
115 8 ▶ return 0 unless ref($age_keys) eq 'ARRAY' && @$age_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $age_keys eq "ARRAY" and @$age_keys 0 8 8
Condition33%1/3
ref $age_keys eq "ARRAY" and @$age_keys
!ll&&!rl&&r
008
MC/DC0%0/2
ref $age_keys eq "ARRAY" and @$age_keys
ref $age_keys eq "ARRAY" @$age_keys
Truth table33%1/3
A and B
A= ref $age_keys eq "ARRAY"B= @$age_keys
ABresult
0X0
100
111
116 8 ▶ return 0 unless ref($identities) eq 'ARRAY' && @$identities;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $identities eq "ARRAY" and @$identities 0 8 8
Condition33%1/3
ref $identities eq "ARRAY" and @$identities
!ll&&!rl&&r
008
MC/DC0%0/2
ref $identities eq "ARRAY" and @$identities
ref $identities eq "ARRAY" @$identities
Truth table33%1/3
A and B
A= ref $identities eq "ARRAY"B= @$identities
ABresult
0X0
100
111
117  
118 # A bad max_stanzas is a caller error, not a "can't decrypt" answer: validate
119 # it here, ahead of the eval below, so it croaks clearly instead of being
120 # swallowed into a false result (k200).
121 8 _check_max_stanzas($args{max_stanzas});
122  
123 4 my $data_key = eval {
124 $class->decrypt_data_key(
125 age_keys => $age_keys,
126 identities => $identities,
127 max_stanzas => $args{max_stanzas},
128 4 );
129 };
130  
131 4 ▶ return defined $data_key ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $data_key ? : 2 2 4
132 }
133  
134  
135 # k200 -- validate max_stanzas the way Crypt::Age does (a positive integer),
136 # ahead of the count guard. Absent/undef means the default and is not an error
137 # here (unlike Crypt::Age); a defined non-positive-integer is. Without this,
138 # max_stanzas => 0/negative refuses every non-empty document with a confusing
139 # "exceeding the limit" message, and a non-numeric value numifies to 0 with a
140 # Perl warning. The message names neither recipients nor blobs.
141 561 ▶sub _check_max_stanzas {
Subroutine _check_max_stanzas: called CC=4 SCAR=13.9
142 561 my ($max_stanzas) = @_;
143 561 ▶ return unless defined $max_stanzas;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_stanzas 537 24 561
144 24 ▶ croak "max_stanzas must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_stanzas =~ /\A[0-9]+\z/ and $max_stanzas > 0 13 11 24
Condition100%3/3
$max_stanzas =~ /\A[0-9]+\z/ and $max_stanzas > 0
!ll&&!rl&&r
10311
MC/DC100%2/2
$max_stanzas =~ /\A[0-9]+\z/ and $max_stanzas > 0
$max_stanzas =~ /\A[0-9]+\z/ $max_stanzas > 0
Truth table100%3/3
A and B
A= $max_stanzas =~ /\A[0-9]+\z/B= $max_stanzas > 0
ABresult
0X0
100
111
145 unless $max_stanzas =~ m{\A[0-9]+\z} && $max_stanzas > 0;
146 11 return;
147 }
148  
149 696 ▶sub _armor {
Subroutine _armor: called CC=1 SCAR=0.0
150 696 my ($data) = @_;
151  
152 696 my $encoded = encode_base64($data, '');
153 # Split into 64-character lines
154 696 $encoded =~ s/(.{64})/$1\n/g;
155 696 $encoded =~ s/\n$//;
156  
157 696 return "-----BEGIN AGE ENCRYPTED FILE-----\n"
158 . $encoded . "\n"
159 . "-----END AGE ENCRYPTED FILE-----\n";
160 }
161  
162 535 ▶sub _dearmor {
Subroutine _dearmor: called CC=3 SCAR=11.0
163 535 my ($armored) = @_;
164  
165 # If it's already raw (starts with age-encryption.org), return as-is
166 535 ▶ return $armored if $armored =~ /^age-encryption\.org/;
Branch: 1 branch
BranchTrueFalseTotal
if $armored =~ /^age-encryption\.org/ 0 535 535
167  
168 # Strip PEM headers and decode
169 535 ▶ if ($armored =~ /-----BEGIN AGE ENCRYPTED FILE-----(.*?)-----END AGE ENCRYPTED FILE-----/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($armored =~ /-----BEGIN AGE ENCRYPTED FILE-----(.*?)-----END AGE ENCRYPTED FILE-----/s) 532 3 535
170 532 my $encoded = $1;
171 532 $encoded =~ s/\s//g;
172 532 return decode_base64($encoded);
173 }
174  
175 # Assume it's raw if we can't parse it
176 3 return $armored;
177 }
178  
179  
180 84 1;
181  
182 __END__
« blib/lib/File/SOPS/Encrypted.pm ↑ Summary blib/lib/File/SOPS/Format/JSON.pm »