LetsEncrypt/Message/Certificate.pm

filter: Statementstmt 75.0% 48 / 64 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 70.5% 67 / 95 CC 11 SCAR 14.6
CC
11
Cov
69%
_revocation_step
12.0
_prep_revocation_step
6.0
_prep_step
5.1
CRAP 4.3
« LetsEncrypt/Message.pm ↑ Summary LetsEncrypt/Message/Authorization.pm »
1 package WWW::LetsEncrypt::Message::Certificate;
2 2 $WWW::LetsEncrypt::Message::Certificate::VERSION = '0.001';
3 2 ▶use HTTP::Status qw(RC_OK RC_CREATED RC_ACCEPTED RC_FORBIDDEN);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 extends 'WWW::LetsEncrypt::Message';
8  
9 =pod
10  
11 =head1 NAME
12  
13 WWW::LetsEncrypt::Message::Certificate - ACME messages
14  
15 =head1 SYNOPSIS
16  
17 use WWW::LetsEncrypt::JWK;
18 use WWW::LetsEncrypt::Message::Certificate;
19  
20 my $JWK = ...;
21 my $DER_encoded_ssl_cert_string = ...;
22  
23 my $CertMsg = WWW::LetsEncrypt::Message::Certificate->new({
24 cert => $DER_encoded_csr_string,
25 JWK => $JWK,
26 nonce => 'nonce_string',
27 });
28  
29 my $result_ref = $CertMsg->do_request();
30 if ($result_ref->{successful}) {
31 if ($result_ref->{finished}) {
32 my $DER_encoded_cert_string = $result_ref->{cert};
33 # do a thing with ^
34 } else {
35 sleep $CertMsg->retry_time;
36 # while !sucessful
37 $result_ref = $CertMsg->do_request();
38 # then do a thing with $result_ref->{cert}
39 # it contains the DER encoded signed certificate
40 }
41 }
42  
43 ---------------------------
44  
45 my $CertMsg = WWW::LetsEncrypt::Message::Certificate->new({
46 cert => $DER_encoded_cert_string,
47 JWK => $JWK,
48 nonce => 'nonce_string',
49 revoke => 1,
50 });
51  
52 my $result_ref = $CertMsg->do_request();
53 # Check successful if it will be revoked.
54  
55 =head1 DESCRIPTION
56  
57 This module implements certificate requests and revocation messages for the ACME protocol.
58  
59 =head2 Attributes
60  
61 =over 4
62  
63 =item cert
64  
65 a scalar string that is the DER encoded CSR for certificate
66 requests OR DER encoded CERT for certificate revocation. Note: This MUST be a
67 DER encoded string. PEM is not going to cut it. This attribute is required.
68  
69 =item revoke
70  
71 a scalar boolean that causes the message to perform revocation.
72  
73 =back
74  
75 =cut
76  
77 2 has 'cert' => (
78 is => 'rw',
79 isa => 'Str',
80 required => 1,
81 );
82  
83 2 has 'revoke' => (
84 is => 'rw',
85 isa => 'Bool',
86 );
87  
88  
89 sub _process_response {
90 6 ▶ my ($self, $Response) = @_;
Subroutine _process_response: called CC=1 SCAR=0.0
91 6 my $step_function = "_" . $self->_step() . "_step";
92 6 return $self->$step_function($Response);
93 }
94  
95 sub _prep_step {
96 6 ▶ my ($self) = @_;
Subroutine _prep_step: called CC=3 SCAR=16.3
97 6 ▶ if ($self->_step) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_step) { } 6 0 6
elsif ($self->revoke) { } 0 0 0
98 6 my $step = "_prep_" . $self->_step . "_step";
99 6 return $self->$step();
100 } elsif ($self->revoke) {
101 0 my $step = "_prep_revocation_step";
102 0 $self->_step('revocation');
103 0 return $self->$step;
104 }
105 0 $self->_step('submit');
106 0 return $self->_prep_submit_step();
107 }
108  
109 sub _prep_revocation_step {
110 0 ▶ my ($self) = @_;
Subroutine _prep_revocation_step: not called CC=2 SCAR=17.9
111  
112 0 ▶ confess 'A Certificate must be provided for revocation!'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cert 0 0 0
113 if !$self->cert;
114  
115 0 my $uri = $self->acme_base_url() . "/acme/revoke-cert";
116 0 $self->_Request(HTTP::Request->new(POST => $uri));
117  
118 0 $self->_payload({
119 resource => 'revoke-cert',
120 certificate => $self->cert,
121 });
122 0 return 1;
123 }
124  
125 # $Obj->_revocation_step($Response)
126 #
127 #Internal function that processes revocation messages.
128 #
129 #Input
130 # $Response - HTTP::Response object reference
131 #
132 #Output
133 # # if revocation was successful
134 # \%hash_ref = {
135 # successful => 1,
136 # finished => 1,
137 # }
138 #
139 # # if it has been revoked already
140 # \%hash_ref = {
141 # successful => 0,
142 # finished => 1,
143 # }
144 #
145 # # Else, an error \%hashref
146  
147 sub _revocation_step {
148 0 ▶ my ($self, $Response) = @_;
Subroutine _revocation_step: not called CC=3 SCAR=24.8
149 0 ▶ if ($Response->code() == RC_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Response->code == 200) { } 0 0 0
elsif ($Response->code == 403) { } 0 0 0
150 return {
151 0 successful => 1,
152 finished => 1,
153 };
154 } elsif ($Response->code() == RC_FORBIDDEN) {
155 return {
156 0 successful => 0,
157 finished => 1,
158 already_revoked => 1,
159 };
160 }
161 0 return {error => 1}
162 }
163  
164 sub _prep_submit_step {
165 3 ▶ my ($self) = @_;
Subroutine _prep_submit_step: called CC=2 SCAR=7.0
166  
167 3 my $uri = $self->acme_base_url() . "/acme/new-cert";
168 3 $self->_Request(HTTP::Request->new(POST => $uri));
169  
170 3 ▶ confess 'CSR must be provided for certificate submission!'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cert 0 3 3
171 if !$self->cert;
172  
173 3 $self->_payload({
174 resource => 'new-cert',
175 csr => $self->cert,
176 });
177 3 return 1;
178 }
179  
180 # $Obj->_submit_step($Response)
181 #
182 #Internal function that handles CSR submission
183 #
184 #Input
185 # $Response - HTTP::Response object reference
186 #
187 #Output
188 # # If the certificate is signed upon request
189 # \%hash_ref = {
190 # sucessful => 1,
191 # finished => 1,
192 # cert => scalar string that is the DER encoded certificate,
193 # }
194 #
195 # # If polling will be necessary
196 # \$hash_ref = {
197 # sucessful => 1,
198 # finished => 0,
199 # }
200  
201 sub _submit_step {
202 3 ▶ my ($self, $Response) = @_;
Subroutine _submit_step: called CC=3 SCAR=11.0
203 3 ▶ if ($Response->code() == RC_ACCEPTED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Response->code == 202) { } 1 2 3
elsif ($Response->code == 201) { } 1 1 2
204 1 my $polling_url = $Response->header('location');
205 1 $self->_step('poll');
206 return {
207 1 successful => 1,
208 finished => 0,
209 };
210 } elsif ($Response->code() == RC_CREATED) {
211 return {
212 1 successful => 1,
213 finished => 1,
214 cert => $Response->content(),
215 }
216 }
217 1 return {error => 1};
218 }
219  
220 sub _prep_poll_step {
221 3 ▶ my ($self) = @_;
Subroutine _prep_poll_step: called CC=1 SCAR=0.0
222 3 $self->_payload({});
223 3 my $Request = HTTP::Request->new(GET => $self->_url);
224 3 $Request->header('Accept-Encoding' => 'application/x-pem-file');
225 3 $self->_Request($Request);
226 3 return 1;
227 }
228  
229 # $Obj->_poll_step($Response)
230 #
231 #Internal function that polls the ACME server for the certificate
232 #
233 #Input
234 # $Response - HTTP::Response object reference
235 #
236 #Output
237 # # If the certificate is signed upon request
238 # \%hash_ref = {
239 # sucessful => 1,
240 # finished => 1,
241 # cert => scalar string that is the DER encoded certificate,
242 # }
243 #
244 # # If polling will be necessary
245 # \$hash_ref = {
246 # sucessful => 1,
247 # finished => 0,
248 # }
249  
250 sub _poll_step {
251 3 ▶ my ($self, $Response) = @_;
Subroutine _poll_step: called CC=3 SCAR=11.0
252 3 ▶ if ($Response->code() == RC_ACCEPTED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Response->code == 202) { } 1 2 3
elsif ($Response->code == 200) { } 1 1 2
253 1 my $wait_time = $Response->header('Retry-After');
254 1 $self->retry_time($wait_time);
255 return {
256 1 successful => 1,
257 finished => 0,
258 };
259 } elsif ($Response->code() == RC_OK) {
260 1 my $cert = $Response->content();
261 return {
262 1 successful => 1,
263 finished => 1,
264 cert => $cert,
265 };
266 } else {
267 1 return {error => 1};
268 }
269 }
270  
271 =back
272  
273 =cut
274  
275 2 __PACKAGE__->meta->make_immutable;
« LetsEncrypt/Message.pm ↑ Summary LetsEncrypt/Message/Authorization.pm »