File Coverage

blib/lib/Protocol/ACME/Key.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 4 50.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 4 0.0
total 45 52 86.5


line stmt bran cond sub pod time code
1             package Protocol::ACME::Key;
2              
3             # A shim that imitates Crypt::OpenSSL::RSA.
4              
5 5     5   928 use strict;
  5         12  
  5         155  
6 5     5   24 use warnings;
  5         10  
  5         247  
7              
8             our $VERSION = '1.02';
9              
10 5     5   29 use Crypt::RSA::Parse;
  5         10  
  5         115  
11 5     5   4428 use Math::BigInt ();
  5         99633  
  5         256  
12              
13 5     5   49 use Protocol::ACME::Utils;
  5         12  
  5         1695  
14              
15             sub new
16             {
17 15     15 0 79 my ($class, %opts) = @_;
18              
19 15         172 my $key = Crypt::RSA::Parse::private($opts{'keystring'});
20              
21             my $self = {
22             _keystring => $opts{'keystring'},
23 14         4324975 _openssl_bin => $opts{'openssl'},
24             _private_key => $key,
25             e => Math::BigInt->new( $key->publicExponent() ),
26             n => $key->modulus(),
27             };
28              
29 14         1152 return bless $self, $class;
30             }
31              
32             sub use_sha256_hash
33       14 0   {
34             # NOOP for compatibility with Crypt::OpenSSL::RSA
35             }
36              
37             sub get_key_parameters
38             {
39 14     14 0 30 my $self = shift;
40 14         72 return ( $self->{n}, $self->{e} );
41             }
42              
43             sub sign {
44 5     5 0 157 my ($self, $payload) = @_;
45              
46             #TODO: Use an available SHA256-digest module, if any.
47              
48 5   66     34 $self->{'_openssl'} ||= do {
49 3         2217 require Protocol::ACME::OpenSSL;
50 3         36 Protocol::ACME::OpenSSL->new($self->{'_openssl_bin'});
51             };
52              
53 5         58 require File::Temp;
54 5         84 my $fh = File::Temp->new();
55 5         3691 my $kpath = $fh->filename();
56 5 50       45 print {$fh} $self->{'_keystring'} or die "write($kpath) failed: $!";
  5         774  
57 5 50       296 close $fh or die "close($kpath) failed: $!";
58              
59 5         78 return $self->{'_openssl'}->run(
60             command => [
61             'dgst',
62             '-sha256',
63             '-binary',
64             '-sign' => $kpath,
65             ],
66             stdin => $payload,
67             );
68             }
69              
70             1;