ACME/Certificate.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 57.1% 8 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 82.2% 65 / 79 CC 10 SCAR 11.0
CC
10
Cov
86%
new
5.3
issuers_pem
4.0
_STATIC_die_if_wrong_mime_type
2.1
CRAP 3.0
« ACME/HTTP.pm ↑ Summary ACME/AccessorBase.pm »
1 package Net::ACME::Certificate;
2  
3 =pod
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 Net::ACME::Certificate - resource abstraction for C<Net::ACME>
10  
11 =head1 SYNOPSIS
12  
13 my $has_cert = Net::ACME::Certificate->new(
14 content => $cert_der,
15 type => 'application/pkix-cert',
16 issuer_cert_uri => 'http://uri/to/issuer/cert',
17 );
18  
19 my $cert_pem2 = $has_cert->pem();
20 my (@issuers_pem) = $has_cert->issuers_pem();
21  
22 =head1 DESCRIPTION
23  
24 This module encapsulates interaction with ACME “certificate” resources
25 when the certificate is available.
26  
27 For handling cases of non-availability (i.e., HTTP 202 status from the
28 ACME server), see C<Net::ACME::Certificate::Pending>.
29  
30 =cut
31  
32 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 7 ▶use Call::Context ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use Crypt::Format ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 7 ▶use Net::ACME::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod undocumented
41 4 my ( $class, %opts ) = @_;
42  
43 4 ▶ die 'requires “content”!' if !$opts{'content'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"content"} 0 4 4
44 4 ▶ die 'requires “type”!' if !$opts{'type'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"type"} 0 4 4
45 4 ▶ die 'requires “issuer_cert_uri”!' if !$opts{'issuer_cert_uri'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"issuer_cert_uri"} 0 4 4
46  
47 4 my $self = bless {}, $class;
48  
49 4 $self->{"_$_"} = $opts{$_} for qw(content type issuer_cert_uri);
50  
51 4 return $self;
52 }
53  
54 2 ▶sub issuers_pem {
Subroutine issuers_pem: called CC=4 SCAR=13.9
Pod undocumented
55 2 my ($self) = @_;
56  
57 2 Call::Context::must_be_list();
58  
59 2 my @pems;
60  
61 2 my $uri = $self->{'_issuer_cert_uri'};
62 2 ▶ while ($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) 3 2 5
63 3 my $http = Net::ACME::HTTP->new();
64 3 my $resp = $http->get($uri);
65  
66 #TODO: Check response status code.
67  
68 3 _STATIC_die_if_wrong_mime_type( $resp->header('content-type') );
69  
70 3 push @pems, _STATIC_der_to_pem( $resp->content() );
71  
72 3 my $new_uri = { $resp->links() }->{'up'};
73  
74 #Paranoia: Detect weirdness where an ACME server might consider
75 #a root cert to be its own issuer (cuz it is, really).
76 3 ▶ undef $new_uri if $new_uri && $new_uri eq $uri;
Branch: 1 branch
BranchTrueFalseTotal
if $new_uri and $new_uri eq $uri 0 3 3
Condition66%2/3
$new_uri and $new_uri eq $uri
!ll&&!rl&&r
210
MC/DC0%0/2
$new_uri and $new_uri eq $uri
$new_uri $new_uri eq $uri
Truth table66%2/3
A and B
A= $new_uriB= $new_uri eq $uri
ABresult
0X0
100
111
77  
78 3 $uri = $new_uri;
79 }
80  
81 2 return @pems;
82 }
83  
84 2 ▶sub pem {
Subroutine pem: called CC=2 SCAR=7.4
Pod undocumented
85 2 my ($self) = @_;
86  
87 2 ▶ if ( defined $self->{'_type'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_type"}) 2 0 2
88 2 _STATIC_die_if_wrong_mime_type( $self->{'_type'} );
89  
90 2 return _STATIC_der_to_pem( $self->{'_content'} );
91 }
92  
93 0 return undef;
94 }
95  
96 #----------------------------------------------------------------------
97  
98 5 ▶sub _STATIC_die_if_wrong_mime_type {
Subroutine _STATIC_die_if_wrong_mime_type: called CC=2 SCAR=7.6
99 5 my ($type) = @_;
100  
101 5 ▶ if ( $type ne 'application/pkix-cert' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "application/pkix-cert") 0 5 5
102 0 die "Unrecognized certificate MIME type: “$type”";
103 }
104  
105 5 return;
106 }
107  
108 5 ▶sub _STATIC_der_to_pem {
Subroutine _STATIC_der_to_pem: called CC=1 SCAR=0.0
109 5 my ($der) = @_;
110  
111 5 return Crypt::Format::der2pem( $der, 'CERTIFICATE' );
112 }
113  
114 7 1;
« ACME/HTTP.pm ↑ Summary ACME/AccessorBase.pm »