| 1 | package App::GroupSecret::Crypt; | ||||||||||
| 2 | # ABSTRACT: Collection of crypto-related subroutines | ||||||||||
| 3 | |||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | our $VERSION = '0.304'; # VERSION | |||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use File::Temp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use IPC::Open2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use IPC::Open3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use Symbol qw(gensym); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use namespace::clean -except => [qw(import)]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | our @EXPORT_OK = qw( | |||||||||
| 17 | generate_secure_random_bytes | ||||||||||
| 18 | read_openssh_public_key | ||||||||||
| 19 | read_openssh_key_fingerprint | ||||||||||
| 20 | decrypt_rsa | ||||||||||
| 21 | encrypt_rsa | ||||||||||
| 22 | decrypt_aes_256_cbc | ||||||||||
| 23 | encrypt_aes_256_cbc | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | 4 | our $OPENSSL = 'openssl'; | |||||||||
| 27 | 4 | our $SSH_KEYGEN = 'ssh-keygen'; | |||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub _croak { require Carp; Carp::croak(@_) } | ||||||||
|
Subroutine
_croak: not called CC=1 SCAR=6.9
| |||||||||||
| 30 | 0 | ▶ | sub _usage { _croak("Usage: @_\n") } | ||||||||
|
Subroutine
_usage: not called CC=1 SCAR=6.9
| |||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | sub generate_secure_random_bytes { | ||||||||
|
Subroutine
generate_secure_random_bytes: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 34 | 0 | ▶ | my $size = shift or _usage(q{generate_secure_random_bytes($num_bytes)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 0 | my @cmd = ($OPENSSL, 'rand', $size); | |||||||||
| 37 | |||||||||||
| 38 | 0 | my $out; | |||||||||
| 39 | 0 | my $pid = open2($out, undef, @cmd); | |||||||||
| 40 | |||||||||||
| 41 | 0 | waitpid($pid, 0); | |||||||||
| 42 | 0 | my $status = $?; | |||||||||
| 43 | |||||||||||
| 44 | 0 | my $exit_code = $status >> 8; | |||||||||
| 45 | 0 | ▶ | _croak 'Failed to generate secure random bytes' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 0 | return do { local $/; <$out> }; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub read_openssh_public_key { | ||||||||
|
Subroutine
read_openssh_public_key: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 52 | 0 | ▶ | my $filepath = shift or _usage(q{read_openssh_public_key($filepath)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | my @cmd = ($SSH_KEYGEN, qw{-e -m PKCS8 -f}, $filepath); | |||||||||
| 55 | |||||||||||
| 56 | 0 | my $out; | |||||||||
| 57 | 0 | my $pid = open2($out, undef, @cmd); | |||||||||
| 58 | |||||||||||
| 59 | 0 | waitpid($pid, 0); | |||||||||
| 60 | 0 | my $status = $?; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $exit_code = $status >> 8; | |||||||||
| 63 | 0 | ▶ | _croak 'Failed to read OpenSSH public key' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 0 | return do { local $/; <$out> }; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub read_openssh_key_fingerprint { | ||||||||
|
Subroutine
read_openssh_key_fingerprint: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 70 | 0 | ▶ | my $filepath = shift or _usage(q{read_openssh_key_fingerprint($filepath)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | # try with the -E flag first | ||||||||||
| 73 | 0 | my @cmd = ($SSH_KEYGEN, qw{-l -E md5 -f}, $filepath); | |||||||||
| 74 | |||||||||||
| 75 | 0 | my $out; | |||||||||
| 76 | 0 | my $err = gensym; | |||||||||
| 77 | 0 | my $pid = open3(undef, $out, $err, @cmd); | |||||||||
| 78 | |||||||||||
| 79 | 0 | waitpid($pid, 0); | |||||||||
| 80 | 0 | my $status = $?; | |||||||||
| 81 | |||||||||||
| 82 | 0 | my $exit_code = $status >> 8; | |||||||||
| 83 | 0 | ▶ | if ($exit_code != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | my $error_str = do { local $/; <$err> }; | |||||||||
| 85 | 0 | ▶ | _croak 'Failed to read SSH2 key fingerprint' if $error_str !~ /unknown option -- E/s; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 0 | @cmd = ($SSH_KEYGEN, qw{-l -f}, $filepath); | |||||||||
| 88 | |||||||||||
| 89 | 0 | undef $out; | |||||||||
| 90 | 0 | $pid = open2($out, undef, @cmd); | |||||||||
| 91 | |||||||||||
| 92 | 0 | waitpid($pid, 0); | |||||||||
| 93 | 0 | $status = $?; | |||||||||
| 94 | |||||||||||
| 95 | 0 | $exit_code = $status >> 8; | |||||||||
| 96 | 0 | ▶ | _croak 'Failed to read SSH2 key fingerprint' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 0 | my $line = do { local $/; <$out> }; | |||||||||
| 100 | 0 | chomp $line; | |||||||||
| 101 | |||||||||||
| 102 | 0 | my ($bits, $fingerprint, $comment, $type) = $line =~ m!^(\d+) (?:MD5:)?([^ ]+) (.*) \(([^\)]+)\)$!; | |||||||||
| 103 | |||||||||||
| 104 | 0 | $fingerprint =~ s/://g; | |||||||||
| 105 | |||||||||||
| 106 | return { | ||||||||||
| 107 | 0 | bits => $bits, | |||||||||
| 108 | fingerprint => $fingerprint, | ||||||||||
| 109 | comment => $comment, | ||||||||||
| 110 | type => lc($type), | ||||||||||
| 111 | }; | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | 0 | ▶ | sub decrypt_rsa { | ||||||||
|
Subroutine
decrypt_rsa: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 116 | 0 | ▶ | my $filepath = shift or _usage(q{decrypt_rsa($filepath, $keypath)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | my $privkey = shift or _usage(q{decrypt_rsa($filepath, $keypath)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | my $outfile = shift; | |||||||||
| 119 | |||||||||||
| 120 | 0 | my $temp; | |||||||||
| 121 | 0 | ▶ | if (ref $filepath eq 'SCALAR') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | $temp = File::Temp->new(UNLINK => 1); | |||||||||
| 123 | 0 | print $temp $$filepath; | |||||||||
| 124 | 0 | close $temp; | |||||||||
| 125 | 0 | $filepath = $temp->filename; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 0 | my @cmd = ($OPENSSL, qw{rsautl -decrypt -oaep -in}, $filepath, '-inkey', $privkey); | |||||||||
| 129 | 0 | ▶ | push @cmd, ('-out', $outfile) if $outfile; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | 0 | my $out; | |||||||||
| 132 | 0 | my $pid = open2($out, undef, @cmd); | |||||||||
| 133 | |||||||||||
| 134 | 0 | waitpid($pid, 0); | |||||||||
| 135 | 0 | my $status = $?; | |||||||||
| 136 | |||||||||||
| 137 | 0 | my $exit_code = $status >> 8; | |||||||||
| 138 | 0 | ▶ | _croak 'Failed to decrypt ciphertext' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | |||||||||||
| 140 | 0 | return do { local $/; <$out> }; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | 0 | ▶ | sub encrypt_rsa { | ||||||||
|
Subroutine
encrypt_rsa: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 145 | 0 | ▶ | my $filepath = shift or _usage(q{encrypt_rsa($filepath, $keypath)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | ▶ | my $pubkey = shift or _usage(q{encrypt_rsa($filepath, $keypath)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 0 | my $outfile = shift; | |||||||||
| 148 | |||||||||||
| 149 | 0 | my $temp1; | |||||||||
| 150 | 0 | ▶ | if (ref $filepath eq 'SCALAR') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 0 | $temp1 = File::Temp->new(UNLINK => 1); | |||||||||
| 152 | 0 | print $temp1 $$filepath; | |||||||||
| 153 | 0 | close $temp1; | |||||||||
| 154 | 0 | $filepath = $temp1->filename; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | 0 | my $key = read_openssh_public_key($pubkey); | |||||||||
| 158 | |||||||||||
| 159 | 0 | my $temp2 = File::Temp->new(UNLINK => 1); | |||||||||
| 160 | 0 | print $temp2 $key; | |||||||||
| 161 | 0 | close $temp2; | |||||||||
| 162 | 0 | my $keypath = $temp2->filename; | |||||||||
| 163 | |||||||||||
| 164 | 0 | my @cmd = ($OPENSSL, qw{rsautl -encrypt -oaep -pubin -inkey}, $keypath, '-in', $filepath); | |||||||||
| 165 | 0 | ▶ | push @cmd, ('-out', $outfile) if $outfile; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | |||||||||||
| 167 | 0 | my $out; | |||||||||
| 168 | 0 | my $pid = open2($out, undef, @cmd); | |||||||||
| 169 | |||||||||||
| 170 | 0 | waitpid($pid, 0); | |||||||||
| 171 | 0 | my $status = $?; | |||||||||
| 172 | |||||||||||
| 173 | 0 | my $exit_code = $status >> 8; | |||||||||
| 174 | 0 | ▶ | _croak 'Failed to encrypt plaintext' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | |||||||||||
| 176 | 0 | return do { local $/; <$out> }; | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | |||||||||||
| 180 | 0 | ▶ | sub decrypt_aes_256_cbc { | ||||||||
|
Subroutine
decrypt_aes_256_cbc: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 181 | 0 | ▶ | my $filepath = shift or _usage(q{decrypt_aes_256_cbc($ciphertext, $secret)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | 0 | ▶ | my $secret = shift or _usage(q{decrypt_aes_256_cbc($ciphertext, $secret)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 0 | my $outfile = shift; | |||||||||
| 184 | |||||||||||
| 185 | 0 | my $temp; | |||||||||
| 186 | 0 | ▶ | if (ref $filepath eq 'SCALAR') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | 0 | $temp = File::Temp->new(UNLINK => 1); | |||||||||
| 188 | 0 | print $temp $$filepath; | |||||||||
| 189 | 0 | close $temp; | |||||||||
| 190 | 0 | $filepath = $temp->filename; | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | 0 | my @cmd = ($OPENSSL, qw{aes-256-cbc -d -pass stdin -md sha256 -in}, $filepath); | |||||||||
| 194 | 0 | ▶ | push @cmd, ('-out', $outfile) if $outfile; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | |||||||||||
| 196 | 0 | my ($in, $out); | |||||||||
| 197 | 0 | my $pid = open2($out, $in, @cmd); | |||||||||
| 198 | |||||||||||
| 199 | 0 | print $in $secret; | |||||||||
| 200 | 0 | close($in); | |||||||||
| 201 | |||||||||||
| 202 | 0 | waitpid($pid, 0); | |||||||||
| 203 | 0 | my $status = $?; | |||||||||
| 204 | |||||||||||
| 205 | 0 | my $exit_code = $status >> 8; | |||||||||
| 206 | 0 | ▶ | _croak 'Failed to decrypt ciphertext' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 207 | |||||||||||
| 208 | 0 | return do { local $/; <$out> }; | |||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | |||||||||||
| 212 | 0 | ▶ | sub encrypt_aes_256_cbc { | ||||||||
|
Subroutine
encrypt_aes_256_cbc: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 213 | 0 | ▶ | my $filepath = shift or _usage(q{encrypt_aes_256_cbc($plaintext, $secret)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 0 | ▶ | my $secret = shift or _usage(q{encrypt_aes_256_cbc($plaintext, $secret)}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | 0 | my $outfile = shift; | |||||||||
| 216 | |||||||||||
| 217 | 0 | my $temp; | |||||||||
| 218 | 0 | ▶ | if (ref $filepath eq 'SCALAR') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 219 | 0 | $temp = File::Temp->new(UNLINK => 1); | |||||||||
| 220 | 0 | print $temp $$filepath; | |||||||||
| 221 | 0 | close $temp; | |||||||||
| 222 | 0 | $filepath = $temp->filename; | |||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | 0 | my @cmd = ($OPENSSL, qw{aes-256-cbc -pass stdin -md sha256 -in}, $filepath); | |||||||||
| 226 | 0 | ▶ | push @cmd, ('-out', $outfile) if $outfile; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 227 | |||||||||||
| 228 | 0 | my ($in, $out); | |||||||||
| 229 | 0 | my $pid = open2($out, $in, @cmd); | |||||||||
| 230 | |||||||||||
| 231 | 0 | print $in $secret; | |||||||||
| 232 | 0 | close($in); | |||||||||
| 233 | |||||||||||
| 234 | 0 | waitpid($pid, 0); | |||||||||
| 235 | 0 | my $status = $?; | |||||||||
| 236 | |||||||||||
| 237 | 0 | my $exit_code = $status >> 8; | |||||||||
| 238 | 0 | ▶ | _croak 'Failed to encrypt plaintext' if $exit_code != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 239 | |||||||||||
| 240 | 0 | return do { local $/; <$out> }; | |||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | 4 | 1; | |||||||||
| 244 | |||||||||||
| 245 | __END__ | ||||||||||