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