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

filter: Statementstmt 86.1% 56 / 65 Branchbran 45.8% 11 / 24 Conditioncond 18.1% 4 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 60.1% 83 / 138 CC 23 SCAR 31.2
CC
23
Cov
64%
can_decrypt
72.0
decrypt_data_key
9.8
encrypt_data_key
6.8
CRAP 22.5
↑ Summary blib/lib/File/SOPS/Metadata.pm »
1 package File::SOPS::Backend::Age;
2 # ABSTRACT: age encryption backend for SOPS
3 82 our $VERSION = '0.003';
4 82 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 82 ▶use Crypt::Age;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 82 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 82 ▶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 82 our @CARP_NOT = qw( File::SOPS );
15  
16  
17 sub encrypt_data_key {
18 686 ▶ my ($class, %args) = @_;
Subroutine encrypt_data_key: called CC=6 SCAR=19.2
Pod documented
19 686 ▶ 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
68600
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 686 ▶ my $recipients = $args{recipients} // croak "recipients required";
Condition33%1/3
$args{"recipients"} // croak("recipients required")
l!l&&r!l&&!r
68600
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 686 ▶ croak "recipients must be an array ref" unless ref($recipients) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $recipients eq "ARRAY" 0 686 686
23 686 ▶ croak "at least one recipient required" unless @$recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless @$recipients 0 686 686
24  
25 686 my @encrypted_keys;
26  
27 686 for my $recipient (@$recipients) {
28 687 my $encrypted = Crypt::Age->encrypt(
29 plaintext => $data_key,
30 recipients => [$recipient],
31 );
32  
33 # Armor the output for SOPS compatibility
34 686 my $armored = _armor($encrypted);
35  
36 686 push @encrypted_keys, {
37 recipient => $recipient,
38 enc => $armored,
39 };
40 }
41  
42 685 return \@encrypted_keys;
43 }
44  
45  
46 sub decrypt_data_key {
47 519 ▶ my ($class, %args) = @_;
Subroutine decrypt_data_key: called CC=9 SCAR=22.9
Pod documented
48 519 ▶ 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
51900
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 519 ▶ my $identities = $args{identities} // croak "identities required";
Condition33%1/3
$args{"identities"} // croak("identities required")
l!l&&r!l&&!r
51900
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 519 ▶ 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 519 519
52 519 ▶ croak "identities must be an array ref" unless ref($identities) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $identities eq "ARRAY" 0 519 519
53  
54 # The data key the SOPS data path consumes is exactly 32 bytes -- the
55 # AES-256 key every value in the document is encrypted under. A short
56 # return is silently accepted by CryptX as a working AES-128/192 key
57 # (k52, the same defect class as the data-key / IV checks in
58 # Encrypted::_random_bytes); a long return is not a valid AES key and
59 # dies inside CryptX, attributed to gcm and naming neither the CSPRNG
60 # nor the age layer. Crypt::Age 0.001 happens to return 32 bytes; we
61 # cannot rely on that, and we cannot see the inner values (file key,
62 # nonce, ephemeral key) that produced the result.
63 519 my $EXPECTED_DATA_KEY_LEN = 32;
64  
65 519 for my $key_info (@$age_keys) {
66 520 my $encrypted = $key_info->{enc};
67 520 ▶ next unless defined $encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encrypted 0 520 520
68  
69 # Dearmor if needed
70 520 my $ciphertext = _dearmor($encrypted);
71  
72 520 my $data_key = eval {
73 520 Crypt::Age->decrypt(
74 ciphertext => $ciphertext,
75 identities => $identities,
76 );
77 };
78  
79 520 ▶ next unless defined $data_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_key 2 518 520
80  
81 518 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if length $data_key != $EXPECTED_DATA_KEY_LEN 6 512 518
82 "Crypt::Age returned a data key of %d bytes, expected %d",
83 length($data_key),
84 $EXPECTED_DATA_KEY_LEN,
85 ) if length($data_key) != $EXPECTED_DATA_KEY_LEN;
86  
87 512 return $data_key;
88 }
89  
90 1 croak "Could not decrypt data key with any of the provided identities";
91 }
92  
93  
94 sub can_decrypt {
95 0 ▶ my ($class, %args) = @_;
Subroutine can_decrypt: not called CC=8 SCAR=42.8
Pod documented
96 0 ▶ my $age_keys = $args{age_keys} // return 0;
Condition0%0/2
$args{"age_keys"} // (return 0)
l!l
00
MC/DC0%0/1
$args{"age_keys"} // (return 0)
$args{"age_keys"}
Truth table0%0/2
A
A= $args{"age_keys"}
Aresult
11
00
97 0 ▶ my $identities = $args{identities} // return 0;
Condition0%0/2
$args{"identities"} // (return 0)
l!l
00
MC/DC0%0/1
$args{"identities"} // (return 0)
$args{"identities"}
Truth table0%0/2
A
A= $args{"identities"}
Aresult
11
00
98  
99 0 ▶ return 0 unless ref($age_keys) eq 'ARRAY' && @$age_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $age_keys eq "ARRAY" and @$age_keys 0 0 0
Condition0%0/3
ref $age_keys eq "ARRAY" and @$age_keys
!ll&&!rl&&r
000
MC/DC0%0/2
ref $age_keys eq "ARRAY" and @$age_keys
ref $age_keys eq "ARRAY" @$age_keys
Truth table0%0/3
A and B
A= ref $age_keys eq "ARRAY"B= @$age_keys
ABresult
0X0
100
111
100 0 ▶ return 0 unless ref($identities) eq 'ARRAY' && @$identities;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $identities eq "ARRAY" and @$identities 0 0 0
Condition0%0/3
ref $identities eq "ARRAY" and @$identities
!ll&&!rl&&r
000
MC/DC0%0/2
ref $identities eq "ARRAY" and @$identities
ref $identities eq "ARRAY" @$identities
Truth table0%0/3
A and B
A= ref $identities eq "ARRAY"B= @$identities
ABresult
0X0
100
111
101  
102 0 my $data_key = eval {
103 0 $class->decrypt_data_key(
104 age_keys => $age_keys,
105 identities => $identities,
106 );
107 };
108  
109 0 ▶ return defined $data_key ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $data_key ? : 0 0 0
110 }
111  
112  
113 sub _armor {
114 686 ▶ my ($data) = @_;
Subroutine _armor: called CC=1 SCAR=0.0
115  
116 686 my $encoded = encode_base64($data, '');
117 # Split into 64-character lines
118 686 $encoded =~ s/(.{64})/$1\n/g;
119 686 $encoded =~ s/\n$//;
120  
121 686 return "-----BEGIN AGE ENCRYPTED FILE-----\n"
122 . $encoded . "\n"
123 . "-----END AGE ENCRYPTED FILE-----\n";
124 }
125  
126 sub _dearmor {
127 520 ▶ my ($armored) = @_;
Subroutine _dearmor: called CC=3 SCAR=11.6
128  
129 # If it's already raw (starts with age-encryption.org), return as-is
130 520 ▶ return $armored if $armored =~ /^age-encryption\.org/;
Branch: 1 branch
BranchTrueFalseTotal
if $armored =~ /^age-encryption\.org/ 0 520 520
131  
132 # Strip PEM headers and decode
133 520 ▶ 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) 520 0 520
134 520 my $encoded = $1;
135 520 $encoded =~ s/\s//g;
136 520 return decode_base64($encoded);
137 }
138  
139 # Assume it's raw if we can't parse it
140 0 return $armored;
141 }
142  
143  
144 82 1;
145  
146 __END__
↑ Summary blib/lib/File/SOPS/Metadata.pm »