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   701 use strict;
  5         8  
  5         115  
6 5     5   16 use warnings;
  5         8  
  5         155  
7              
8             our $VERSION = '0.16';
9              
10 5     5   18 use Crypt::RSA::Parse;
  5         5  
  5         94  
11 5     5   3731 use Math::BigInt ();
  5         62370  
  5         141  
12              
13 5     5   41 use Protocol::ACME::Utils;
  5         8  
  5         1199  
14              
15             sub new
16             {
17 15     15 0 50 my ($class, %opts) = @_;
18              
19 15         111 my $key = Crypt::RSA::Parse::private($opts{'keystring'});
20              
21             my $self = {
22             _keystring => $opts{'keystring'},
23 14         2256788 _openssl_bin => $opts{'openssl'},
24             _private_key => $key,
25             e => Math::BigInt->new( $key->publicExponent() ),
26             n => $key->modulus(),
27             };
28              
29 14         521 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 26 my $self = shift;
40 14         50 return ( $self->{n}, $self->{e} );
41             }
42              
43             sub sign {
44 5     5 0 153 my ($self, $payload) = @_;
45              
46             #TODO: Use an available SHA256-digest module, if any.
47              
48 5   66     30 $self->{'_openssl'} ||= do {
49 3         2061 require Protocol::ACME::OpenSSL;
50 3         42 Protocol::ACME::OpenSSL->new($self->{'_openssl_bin'});
51             };
52              
53 5         63 require File::Temp;
54 5         70 my $fh = File::Temp->new();
55 5         3323 my $kpath = $fh->filename();
56 5 50       36 print {$fh} $self->{'_keystring'} or die "write($kpath) failed: $!";
  5         1201  
57 5 50       275 close $fh or die "close($kpath) failed: $!";
58              
59 5         55 return $self->{'_openssl'}->run(
60             command => [
61             'dgst',
62             '-sha256',
63             '-binary',
64             '-sign' => $kpath,
65             ],
66             stdin => $payload,
67             );
68             }
69              
70             1;