| 1 | package File::SOPS::Backend::Age; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: age encryption backend for SOPS | ||||||||||||||||||||||||||||
| 3 | 85 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||
| 4 | 85 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 85 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 85 | ▶ | use Crypt::Age; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 85 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 85 | ▶ | 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 | 85 | our @CARP_NOT = qw( File::SOPS ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 696 | ▶ | sub encrypt_data_key { | ||||||||||||||||||||||||||
|
Subroutine
encrypt_data_key: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 696 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 19 | 696 | ▶ | my $data_key = $args{data_key} // croak "data_key required"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"data_key"} // croak("data_key required")
MC/DC0%0/1
$args{"data_key"} // croak("data_key required")
$args{"data_key"}
Truth table50%1/2
A
A= $args{"data_key"}
| |||||||||||||||||||||||||||||
| 20 | 696 | ▶ | my $recipients = $args{recipients} // croak "recipients required"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"recipients"} // croak("recipients required")
MC/DC0%0/1
$args{"recipients"} // croak("recipients required")
$args{"recipients"}
Truth table50%1/2
A
A= $args{"recipients"}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 696 | ▶ | croak "recipients must be an array ref" unless ref($recipients) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 696 | ▶ | croak "at least one recipient required" unless @$recipients; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 696 | my @encrypted_keys; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 696 | for my $recipient (@$recipients) { | |||||||||||||||||||||||||||
| 28 | 698 | my $encrypted = Crypt::Age->encrypt( | |||||||||||||||||||||||||||
| 29 | plaintext => $data_key, | ||||||||||||||||||||||||||||
| 30 | recipients => [$recipient], | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Armor the output for SOPS compatibility | ||||||||||||||||||||||||||||
| 34 | 697 | my $armored = _armor($encrypted); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 697 | push @encrypted_keys, { | |||||||||||||||||||||||||||
| 37 | recipient => $recipient, | ||||||||||||||||||||||||||||
| 38 | enc => $armored, | ||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 695 | return \@encrypted_keys; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 565 | ▶ | sub decrypt_data_key { | ||||||||||||||||||||||||||
|
Subroutine
decrypt_data_key: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 565 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 48 | 565 | ▶ | my $age_keys = $args{age_keys} // croak "age_keys required"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"age_keys"} // croak("age_keys required")
MC/DC0%0/1
$args{"age_keys"} // croak("age_keys required")
$args{"age_keys"}
Truth table50%1/2
A
A= $args{"age_keys"}
| |||||||||||||||||||||||||||||
| 49 | 565 | ▶ | my $identities = $args{identities} // croak "identities required"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"identities"} // croak("identities required")
MC/DC0%0/1
$args{"identities"} // croak("identities required")
$args{"identities"}
Truth table50%1/2
A
A= $args{"identities"}
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 565 | ▶ | croak "age_keys must be an array ref" unless ref($age_keys) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 565 | ▶ | croak "identities must be an array ref" unless ref($identities) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 565 | ▶ | my $max_stanzas = $args{max_stanzas} // 64; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"max_stanzas"} // 64
MC/DC100%1/1
$args{"max_stanzas"} // 64
$args{"max_stanzas"}
Truth table100%2/2
A
A= $args{"max_stanzas"}
| |||||||||||||||||||||||||||||
| 63 | 565 | _check_max_stanzas($args{max_stanzas}); | |||||||||||||||||||||||||||
| 64 | 556 | ▶ | croak sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 545 | my $EXPECTED_DATA_KEY_LEN = 32; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 545 | for my $key_info (@$age_keys) { | |||||||||||||||||||||||||||
| 82 | 547 | my $encrypted = $key_info->{enc}; | |||||||||||||||||||||||||||
| 83 | 547 | ▶ | next unless defined $encrypted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Dearmor if needed | ||||||||||||||||||||||||||||
| 86 | 547 | my $ciphertext = _dearmor($encrypted); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 547 | my $data_key = eval { | |||||||||||||||||||||||||||
| 89 | 547 | Crypt::Age->decrypt( | |||||||||||||||||||||||||||
| 90 | ciphertext => $ciphertext, | ||||||||||||||||||||||||||||
| 91 | identities => $identities, | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 547 | ▶ | next unless defined $data_key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 544 | ▶ | croak sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 538 | 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)
MC/DC0%0/1
$args{"age_keys"} // (return 0)
$args{"age_keys"}
Truth table50%1/2
A
A= $args{"age_keys"}
| |||||||||||||||||||||||||||||
| 113 | 8 | ▶ | my $identities = $args{identities} // return 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"identities"} // (return 0)
MC/DC0%0/1
$args{"identities"} // (return 0)
$args{"identities"}
Truth table50%1/2
A
A= $args{"identities"}
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 8 | ▶ | return 0 unless ref($age_keys) eq 'ARRAY' && @$age_keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $age_keys eq "ARRAY" and @$age_keys
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
| |||||||||||||||||||||||||||||
| 116 | 8 | ▶ | return 0 unless ref($identities) eq 'ARRAY' && @$identities; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $identities eq "ARRAY" and @$identities
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 573 | ▶ | sub _check_max_stanzas { | ||||||||||||||||||||||||||
|
Subroutine
_check_max_stanzas: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 142 | 573 | my ($max_stanzas) = @_; | |||||||||||||||||||||||||||
| 143 | 573 | ▶ | return unless defined $max_stanzas; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 24 | ▶ | croak "max_stanzas must be a positive integer" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max_stanzas =~ /\A[0-9]+\z/ and $max_stanzas > 0
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
| |||||||||||||||||||||||||||||
| 145 | unless $max_stanzas =~ m{\A[0-9]+\z} && $max_stanzas > 0; | ||||||||||||||||||||||||||||
| 146 | 11 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 697 | ▶ | sub _armor { | ||||||||||||||||||||||||||
|
Subroutine
_armor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 697 | my ($data) = @_; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 697 | my $encoded = encode_base64($data, ''); | |||||||||||||||||||||||||||
| 153 | # Split into 64-character lines | ||||||||||||||||||||||||||||
| 154 | 697 | $encoded =~ s/(.{64})/$1\n/g; | |||||||||||||||||||||||||||
| 155 | 697 | $encoded =~ s/\n$//; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 697 | return "-----BEGIN AGE ENCRYPTED FILE-----\n" | |||||||||||||||||||||||||||
| 158 | . $encoded . "\n" | ||||||||||||||||||||||||||||
| 159 | . "-----END AGE ENCRYPTED FILE-----\n"; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 547 | ▶ | sub _dearmor { | ||||||||||||||||||||||||||
|
Subroutine
_dearmor: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 163 | 547 | my ($armored) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # If it's already raw (starts with age-encryption.org), return as-is | ||||||||||||||||||||||||||||
| 166 | 547 | ▶ | return $armored if $armored =~ /^age-encryption\.org/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Strip PEM headers and decode | ||||||||||||||||||||||||||||
| 169 | 547 | ▶ | if ($armored =~ /-----BEGIN AGE ENCRYPTED FILE-----(.*?)-----END AGE ENCRYPTED FILE-----/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 544 | my $encoded = $1; | |||||||||||||||||||||||||||
| 171 | 544 | $encoded =~ s/\s//g; | |||||||||||||||||||||||||||
| 172 | 544 | 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 | 85 | 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||