X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fgroupsecret;a=blobdiff_plain;f=lib%2FApp%2FGroupSecret%2FCrypt.pm;h=e64fa40e1c0d8535a84a5ffd6c69c0e3eea010aa;hp=90163486fe1db33d8bb520f39d4ef2745ec8c396;hb=3bc3c5b0b3648ae001e38ee72e65dc7880dec9bd;hpb=d2b7194430bfb422a9860c574b80fbfbbaebdf81;ds=sidebyside diff --git a/lib/App/GroupSecret/Crypt.pm b/lib/App/GroupSecret/Crypt.pm index 9016348..e64fa40 100644 --- a/lib/App/GroupSecret/Crypt.pm +++ b/lib/App/GroupSecret/Crypt.pm @@ -21,6 +21,9 @@ our @EXPORT_OK = qw( encrypt_aes_256_cbc ); +our $OPENSSL = 'openssl'; +our $SSH_KEYGEN = 'ssh-keygen'; + sub _croak { require Carp; Carp::croak(@_) } sub _usage { _croak("Usage: @_\n") } @@ -35,12 +38,11 @@ Get a certain number of secure random bytes. sub generate_secure_random_bytes { my $size = shift or _usage(q{generate_secure_random_bytes($num_bytes)}); - my @cmd = (qw{openssl rand}, $size); + my @cmd = ($OPENSSL, 'rand', $size); - my ($in, $out); - my $pid = open2($out, $in, @cmd); + my $out; + my $pid = open2($out, undef, @cmd); - close($in); waitpid($pid, 0); my $status = $?; @@ -61,12 +63,10 @@ Read a RFC4716 (SSH2) public key from a file, converting it to PKCS8 (PEM). sub read_openssh_public_key { my $filepath = shift or _usage(q{read_openssh_public_key($filepath)}); - my @cmd = (qw{ssh-keygen -e -m PKCS8 -f}, $filepath); + my @cmd = ($SSH_KEYGEN, qw{-e -m PKCS8 -f}, $filepath); - my ($in, $out); - my $pid = open2($out, $in, @cmd); - - close($in); + my $out; + my $pid = open2($out, undef, @cmd); waitpid($pid, 0); my $status = $?; @@ -88,7 +88,7 @@ Get the fingerprint of an OpenSSH private or public key. sub read_openssh_key_fingerprint { my $filepath = shift or _usage(q{read_openssh_key_fingerprint($filepath)}); - my @cmd = (qw{ssh-keygen -l -E md5 -f}, $filepath); + my @cmd = ($SSH_KEYGEN, qw{-l -E md5 -f}, $filepath); my $out; my $pid = open2($out, undef, @cmd); @@ -138,13 +138,11 @@ sub decrypt_rsa { $filepath = $temp->filename; } - my @cmd = (qw{openssl rsautl -decrypt -oaep -in}, $filepath, '-inkey', $privkey); + my @cmd = ($OPENSSL, qw{rsautl -decrypt -oaep -in}, $filepath, '-inkey', $privkey); push @cmd, ('-out', $outfile) if $outfile; - my ($in, $out); - my $pid = open2($out, $in, @cmd); - - close($in); + my $out; + my $pid = open2($out, undef, @cmd); waitpid($pid, 0); my $status = $?; @@ -186,13 +184,11 @@ sub encrypt_rsa { close $temp2; my $keypath = $temp2->filename; - my @cmd = (qw{openssl rsautl -encrypt -oaep -pubin -inkey}, $keypath, '-in', $filepath); + my @cmd = ($OPENSSL, qw{rsautl -encrypt -oaep -pubin -inkey}, $keypath, '-in', $filepath); push @cmd, ('-out', $outfile) if $outfile; - my ($in, $out); - my $pid = open2($out, $in, @cmd); - - close($in); + my $out; + my $pid = open2($out, undef, @cmd); waitpid($pid, 0); my $status = $?; @@ -227,7 +223,7 @@ sub decrypt_aes_256_cbc { $filepath = $temp->filename; } - my @cmd = (qw{openssl aes-256-cbc -d -pass stdin -md sha256 -in}, $filepath); + my @cmd = ($OPENSSL, qw{aes-256-cbc -d -pass stdin -md sha256 -in}, $filepath); push @cmd, ('-out', $outfile) if $outfile; my ($in, $out); @@ -269,7 +265,7 @@ sub encrypt_aes_256_cbc { $filepath = $temp->filename; } - my @cmd = (qw{openssl aes-256-cbc -pass stdin -md sha256 -in}, $filepath); + my @cmd = ($OPENSSL, qw{aes-256-cbc -pass stdin -md sha256 -in}, $filepath); push @cmd, ('-out', $outfile) if $outfile; my ($in, $out);