ACME.pm

filter: Statementstmt 85.4% 106 / 124 Branchbran 37.5% 12 / 32 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.5% 25 / 27 Podpod 37.5% 3 / 8 totaltotal 74.1% 149 / 201 CC 19 SCAR 15.0
CC
19
Cov
75%
get_terms_of_service
20.0
get_certificate
10.6
delete_authz
3.2
CRAP 4.5
↑ Summary ACME/Utils.pm »
1 package Net::ACME;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::ACME - Client for the ACME protocol (e.g., L<Let’s Encrypt|http://letsencrypt.org>)
8  
9 X<Lets Encrypt> X<Let's Encrypt> X<letsencrypt>
10  
11 =head1 SYNOPSIS
12  
13 package MyACME::SomeService;
14  
15 use constant _HOST => ...; #the name of the ACME host
16  
17 #See below for full examples.
18  
19 =head1 DESCRIPTION
20  
21 This module implements client logic (including SSL certificate issuance)
22 for the “draft” version of the ACME protocol,
23 the system for automated issuance of SSL certificates used by
24 L<Let’s Encrypt|http://letsencrypt.org>.
25  
26 (For support of the L<IETF|http://ietf.org>-standard version of this
27 protocol, look at L<Net::ACME2>.)
28  
29 The methods of this class return objects that correspond to the
30 respective ACME resource:
31  
32 =over 4
33  
34 =item * C<register()>: C<Net::ACME::Registration>
35  
36 =item * C<start_domain_authz()>: C<Net::ACME::Authorization::Pending>
37  
38 =item * C<get_certificate()>: C<Net::ACME::Certificate> or C<Net::ACME::Certificate::Pending>
39  
40 =back
41  
42 =head1 WHY USE THIS MODULE?
43  
44 =over 4
45  
46 =item * Closely based on cPanel’s widely-used Let’s Encrypt plugin.
47  
48 =item * Support for both RSA and ECDSA encryption (via L<Crypt::Perl>).
49  
50 =item * Thorough error-checking: any deviation from what the ACME protocol
51 expects is reported immediately via an exception.
52  
53 =item * Well-defined object system, including typed, queryable exceptions.
54  
55 =item * Extensive test coverage.
56  
57 =item * Light memory footprint - no Moose/Moo/etc.
58  
59 =item * No careless overwriting of globals like C<$@>, C<$!>, and C<$?>.
60 (Hopefully your code isn’t susceptible to this anyway, but it’s just a good
61 precaution.)
62  
63 =item * This is a pure-Perl solution. Most of its dependencies are
64 either core modules or pure Perl themselves. XS is necessary to
65 communicate with the ACME server via TLS; however, most Perl installations
66 already include the necessary logic (i.e., L<Net::SSLeay>) for TLS.
67  
68 In short, Net::ACME will run anywhere that Perl can speak TLS, which is
69 I<almost> everywhere that Perl runs.
70  
71 =back
72  
73 =head1 STATUS
74  
75 This module is now well-tested and should be safe for use in your application.
76  
77 =head1 CUSTOMIZATION
78  
79 B<HTTPS options>: This module uses C<HTTP::Tiny> for its network operations.
80 In some instances it is desirable to specify custom C<SSL_options> in that
81 module’s constructor; to do this, populate
82 C<@Net::ACME::HTTP_Tiny::SSL_OPTIONS>.
83  
84 =head1 URI vs. URL
85  
86 This module uses “uri” for ACME-related objects and “url” for
87 HTTP-related ones. This apparent conflict is a result of maintaining
88 consistency with both the ACME specification (“uri”) and L<HTTP::Tiny> (“url”).
89  
90 =head1 EXAMPLES
91  
92 See the C<examples> directory in the distribution for complete, interactive
93 example scripts that also illustrate a bit of how ACME works.
94  
95 See below for cut-paste-y examples.
96  
97 =head1 EXAMPLE: REGISTRATION
98  
99 my $tos_url = Net::ACME::LetsEncrypt->get_terms_of_service();
100  
101 my $acme = Net::ACME::LetsEncrypt->new( key => $reg_rsa_pem );
102  
103 #Use this method any time you want to update contact information,
104 #not just when you set up a new account.
105 my $reg = $acme->register('mailto:me@example.com', 'mailto:who@example.com');
106  
107 $acme->accept_tos( $reg->uri(), $tos_url );
108  
109 =head1 EXAMPLE: DOMAIN AUTHORIZATION & CERTIFICATE PROCUREMENT
110  
111 for my $domain (@domains) {
112 my $authz_p = $acme->start_domain_authz($domain);
113  
114 for my $cmb_ar ( $authz_p->combinations() ) {
115  
116 #$cmb_ar is a set of challenges that the ACME server will
117 #accept as proof of domain control. As of November 2016, these
118 #sets all contain exactly one challenge each: “http-01”, etc.
119  
120 #Each member of @$cmb_ar is an instance of
121 #Net::ACME::Challenge::Pending--maybe a subclass thereof such as
122 #Net::ACME::Challenge::Pending::http_01.
123  
124 #At this point, you examine $cmb_ar and determine if this
125 #combination is one that you’re interested in. You might try
126 #something like:
127 #
128 # next if @$cmb_ar > 1;
129 # next if $cmb_ar->[0]->type() ne 'http-01';
130  
131 #Once you’ve examined $cmb_ar and set up the appropriate response(s),
132 #it’s time to tell the ACME server to send its challenge query.
133 $acme->do_challenge($_) for @$cmb_ar;
134  
135 while (1) {
136 if ( $authz_p->is_time_to_poll() ) {
137 my $poll = $authz_p->poll();
138  
139 last if $poll->status() eq 'valid';
140  
141 if ( $poll->status() eq 'invalid' ) {
142 my @failed = map { $_->error() } $poll->challenges();
143  
144 warn $_->to_string() . $/ for @failed;
145  
146 die "Failed authorization for “$domain”!";
147 }
148  
149 }
150  
151 sleep 1;
152 }
153 }
154 }
155  
156 #Make a key and CSR.
157 #Creation of CSRs is well-documented so won’t be discussed here.
158  
159 my $cert = $acme->get_certificate($csr_pem);
160  
161 #This shouldn’t actually be necessary for Let’s Encrypt,
162 #but the ACME protocol describes it.
163 while ( !$cert->pem() ) {
164 sleep 1;
165 next if !$cert->is_time_to_poll();
166 $cert = $cert->poll() || $cert;
167 }
168  
169 =head1 TODO
170  
171 =over 4
172  
173 =item * Once the L<ACME specification|https://tools.ietf.org/html/draft-ietf-acme-acme>
174 is finalized, update this module to take advantage of the full specification.
175 As Let’s Encrypt’s L<Boulder|https://github.com/letsencrypt/boulder> is currently
176 the only widely-used ACME server, and that software is compatible with
177 L<the first draft of the ACME spec|https://tools.ietf.org/html/draft-ietf-acme-acme-01>,
178 there’s little reason to update for the time being.
179  
180 =back
181  
182 =head1 THANKS
183  
184 =over 4
185  
186 =item * cPanel, Inc. for permission to adapt their ACME framework for
187 public consumption.
188  
189 =item * Stephen Ludin for developing and maintaining L<Protocol::ACME>, from which
190 this module took its inspiration.
191  
192 =back
193  
194 =head1 SEE ALSO
195  
196 For support of the forthcoming (as of this writing)
197 L<IETF|http://ietf.org>-standard version of this protocol, look at
198 L<Net::ACME2>.
199  
200 I am aware of the following additional CPAN modules that implement
201 the draft ACME protocol:
202  
203 =over 4
204  
205 =item * L<Protocol::ACME>
206  
207 =item * L<Crypt::LE>
208  
209 =item * L<WWW::LetsEncrypt>
210  
211 =item * L<Mojo::ACME>
212  
213 =back
214  
215 =head1 REPOSITORY (FEEDBACK/BUGS)
216  
217 L<https://github.com/FGasper/p5-Net-ACME>
218  
219 =head1 AUTHOR
220  
221 Felipe Gasper (FELIPE)
222  
223 =head1 LICENSE
224  
225 This module is licensed under the same terms as Perl.
226  
227 =cut
228  
229 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 7 ▶use Crypt::Format ();
Subroutine BEGIN: called CC=1 SCAR=0.0
233 7 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
234 7 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
235  
236 7 ▶use Net::ACME::Authorization::Pending ();
Subroutine BEGIN: called CC=1 SCAR=0.0
237 7 ▶use Net::ACME::Certificate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
238 7 ▶use Net::ACME::Certificate::Pending ();
Subroutine BEGIN: called CC=1 SCAR=0.0
239 7 ▶use Net::ACME::Constants ();
Subroutine BEGIN: called CC=1 SCAR=0.0
240 7 ▶use Net::ACME::Challenge::Pending::http_01 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
241 7 ▶use Net::ACME::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
242 7 ▶use Net::ACME::Registration ();
Subroutine BEGIN: called CC=1 SCAR=0.0
243 7 ▶use Net::ACME::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
244 7 ▶use Net::ACME::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 7 our $VERSION = '0.13-TRIAL1';
247  
248 7 *_to_base64url = \&MIME::Base64::encode_base64url;
249  
250 sub new {
251 5 ▶ my ( $class, %opts ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
252  
253 my $self = {
254 _host => $class->_HOST(),
255 5 _key => $opts{'key'},
256 };
257  
258 5 bless $self, $class;
259  
260 5 $self->_set_ua();
261  
262 5 return $self;
263 }
264  
265 0 ▶sub _HOST { die 'Not Implemented!' }
Subroutine _HOST: not called CC=1 SCAR=6.9
266  
267 sub accept_tos {
268 1 ▶ my ( $self, $reg_uri, $tos_url ) = @_;
Subroutine accept_tos: called CC=2 SCAR=7.0
Pod undocumented
269  
270 1 my $resp = $self->_post_url(
271 $reg_uri,
272 {
273 resource => 'reg',
274 agreement => $tos_url,
275 },
276 );
277  
278 1 ▶ $resp->die_because_unexpected() if $resp->status() != 202;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->status != 202 0 1 1
279  
280 1 return;
281 }
282  
283 #Returns a Net::ACME::Registration instance whose
284 #terms_of_service() will be current/useful.
285 sub register {
286 1 ▶ my ( $self, @contacts ) = @_;
Subroutine register: called CC=3 SCAR=11.2
Pod documented
287  
288 1 my $payload = {
289 resource => 'new-reg',
290 };
291  
292 1 ▶ if (@contacts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@contacts) 1 0 1
293 1 $payload->{'contact'} = \@contacts;
294 }
295  
296 1 my ( $resp, $reg_uri );
297  
298 1 $resp = $self->_post( 'new-reg', $payload );
299  
300 1 ▶ if ( $resp->status() != 201 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->status != 201) 0 1 1
301 0 $resp->die_because_unexpected();
302 }
303  
304 1 $reg_uri = $resp->header('location');
305  
306 #We don’t save the terms-of-service here because the terms
307 #of service might be updated between now and the next time we
308 #load this data. It’s better to make the caller call
309 #get_terms_of_service() each time.
310 my @metadata = (
311 uri => $reg_uri,
312 1 %{ $resp->content_struct() },
313 );
314  
315 #Even though we didn’t save the “terms-of-service” URL from
316 #this registration object, we might as well hold onto it
317 #for the current process to save a call to get_terms_of_service().
318 return Net::ACME::Registration->new(
319 @metadata,
320 1 terms_of_service => { $resp->links() }->{'terms-of-service'},
321 );
322 }
323  
324 #NOTE: This doesn’t actually seem to work with Let’s Encrypt.
325 #The POST keeps coming back with a 202 status rather than 200.
326 #(Looks like Boulder doesn’t handle this function yet?)
327 #sub rollover_key {
328 # my ($self, $reg_uri) = @_;
329 #
330 # my $new_key = $self->create_key_pem();
331 #
332 # my $sub_payload = {
333 # resource => 'reg',
334 # oldKey => $self->jwk_thumbprint(),
335 # };
336 #
337 # my $resp = $self->_post_url(
338 # $reg_uri,
339 # {
340 # resource => 'reg',
341 # newKey => Net::ACME::Utils::get_jws_data(
342 # $new_key,
343 # undef,
344 # JSON::encode_json($sub_payload),
345 # ),
346 # },
347 # );
348 #
349 # if ($resp->status() != 200) {
350 # die "Incorrect status: " . $resp->status() . $/ . $resp->content();
351 # }
352 #
353 # $self->{'_account_key'} = $new_key;
354 # $self->_set_ua();
355 #
356 # return $new_key;
357 #}
358  
359 sub start_domain_authz {
360 1 ▶ my ( $self, $domain_name ) = @_;
Subroutine start_domain_authz: called CC=3 SCAR=11.0
Pod documented
361  
362 1 my $resp = $self->_post(
363 'new-authz',
364 {
365 resource => 'new-authz',
366 identifier => {
367 type => 'dns',
368 value => $domain_name,
369 },
370 },
371 );
372  
373 1 ▶ $resp->die_because_unexpected() if $resp->status() != 201;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->status != 201 0 1 1
374  
375 1 my $content = $resp->content_struct();
376  
377 return Net::ACME::Authorization::Pending->new(
378 uri => $resp->header('location'),
379 combinations => $content->{'combinations'},
380 challenges => [
381 map {
382 2 my $class = 'Net::ACME::Challenge::Pending';
383 2 ▶ if ( $_->{'type'} eq 'http-01' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"type"} eq "http-01") 1 1 2
384 1 $class .= '::http_01';
385 }
386 2 $class->new(%$_);
387 1 } @{ $content->{'challenges'} },
388 ],
389 );
390 }
391  
392 #NOTE: This doesn’t actually work with Boulder (Let’s Encrypt) because
393 #that server implements acme-01. Deletion of an authz was added in acme-02.
394 #
395 #It is critical, though, that when this doesn’t work we still request the
396 #challenge against the authz so that the LE account doesn’t exceed a rate
397 #limit. (cf. COBRA-3273)
398 sub delete_authz {
399 1 ▶ my ( $self, $authz ) = @_;
Subroutine delete_authz: called CC=3 SCAR=11.8
Pod undocumented
400  
401 #sanity
402 1 ▶ if ( !Net::ACME::Utils::thing_isa($authz, 'Net::ACME::Authorization::Pending') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Net::ACME::Utils::thing_isa($authz, "Net::ACME::Authorization::Pending")) 0 1 1
403 0 die "Must be a pending authz object, not “$authz”!";
404 }
405  
406 1 my $resp = $self->_post_url(
407 $authz->uri(),
408 {
409 resource => 'authz',
410 delete => JSON::true(),
411 },
412 );
413  
414 1 ▶ $resp->die_because_unexpected() if $resp->status() != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->status != 200 0 1 1
415  
416 1 return;
417 }
418  
419 sub do_challenge {
420 1 ▶ my ( $self, $challenge_obj ) = @_;
Subroutine do_challenge: called CC=3 SCAR=11.3
Pod undocumented
421  
422 1 my ( $token, $uri ) = map { $challenge_obj->$_() } qw( token uri );
423  
424 1 my $key_obj = Net::ACME::Crypt::parse_key($self->{'_key'});
425  
426 1 ▶ $self->{'_key_jwk'} ||= $key_obj->get_struct_for_public_jwk();
Condition33%1/3
$self->{"_key_jwk"} ||= $key_obj->get_struct_for_public_jwk
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_key_jwk"} ||= $key_obj->get_struct_for_public_jwk
$self->{"_key_jwk"} $key_obj->get_struct_for_public_jwk
Truth table33%1/3
A ||= B
A= $self->{"_key_jwk"}B= $key_obj->get_struct_for_public_jwk
ABresult
1X1
011
000
427  
428 my $resp = $self->_post_url(
429 $uri,
430 {
431 resource => 'challenge',
432 1 keyAuthorization => $challenge_obj->make_key_authz( $self->{'_key_jwk'} ),
433 },
434 );
435  
436 1 ▶ $resp->die_because_unexpected() if $resp->status() != 202;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->status != 202 0 1 1
437  
438 1 return;
439 }
440  
441 sub get_certificate {
442 1 ▶ my ( $self, $csr_pem ) = @_;
Subroutine get_certificate: called CC=5 SCAR=23.6
Pod documented
443  
444 1 my $csr_der = Crypt::Format::pem2der($csr_pem);
445  
446 1 my $resp = $self->_post(
447 'new-cert',
448 {
449 resource => 'new-cert',
450 csr => _to_base64url($csr_der),
451 },
452 );
453  
454 1 my $status = $resp->status();
455  
456 #NB: Let’s Encrypt doesn’t seem to need this,
457 #but per the ACME spec it *could* work this way.
458 1 ▶ if ( $status == 202 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 202) 0 1 1
459 0 my $pcert = Net::ACME::Certificate::Pending->new(
460 uri => $resp->header('location'),
461 retry_after => $resp->header('retry-after'),
462 );
463  
464 0 while (1) {
465 0 ▶ if ( $pcert->is_time_to_poll() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pcert->is_time_to_poll) 0 0 0
466 0 my $c = $pcert->poll();
467 0 ▶ return $c if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
468 }
469 0 sleep 1;
470 }
471 }
472  
473 1 ▶ if ( $status == 201 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 201) 1 0 1
474 return Net::ACME::Certificate->new(
475 content => $resp->content(),
476 type => $resp->header('content-type'),
477 1 issuer_cert_uri => { $resp->links() }->{'up'},
478 );
479 }
480  
481 0 $resp->die_because_unexpected();
482  
483 0 return;
484 }
485  
486 sub get_terms_of_service {
487 0 ▶ my ($self) = @_;
Subroutine get_terms_of_service: not called CC=4 SCAR=30.0
Pod undocumented
488  
489 #We want to be able to call this as a class method.
490 0 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
491 0 $self = $self->new();
492 }
493  
494 0 my $dir = $self->_get_directory();
495 0 ▶ my $url = $self->_get_directory()->{'meta'} or die 'No “meta” in directory!';
Branch: 1 branch
BranchTrueFalseTotal
my $url = $self->_get_directory->{"meta"} or die "No \342\200\234meta\342\200\235 in directory!" 0 0 0
496 0 ▶ $url = $url->{'terms-of-service'} or die 'No “terms-of-service” in directory metadata!';
Branch: 1 branch
BranchTrueFalseTotal
$url = $url->{"terms-of-service"} or die "No \342\200\234terms-of-service\342\200\235 in directory metadata!" 0 0 0
497  
498 0 return $url;
499 }
500  
501 #----------------------------------------------------------------------
502  
503 sub _set_ua {
504 5 ▶ my ($self) = @_;
Subroutine _set_ua: called CC=1 SCAR=0.0
505 $self->{'_ua'} = Net::ACME::HTTP->new(
506 5 key => $self->{'_key'},
507 );
508  
509 5 return;
510 }
511  
512 #TODO: cache
513 sub _get_directory {
514 9 ▶ my ($self) = @_;
Subroutine _get_directory: called CC=2 SCAR=7.1
515  
516 9 ▶ return $self->{'_directory'} ||= $self->{'_ua'}->get("https://$self->{'_host'}/directory")->content_struct();
Condition66%2/3
$self->{"_directory"} ||= $self->{"_ua"}->get(qq[https://$self->{"_host"}/directory])->content_struct
l!l&&r!l&&!r
450
MC/DC0%0/2
$self->{"_directory"} ||= $self->{"_ua"}->get(qq[https://$self->{"_host"}/directory])->content_struct
$self->{"_directory"} $self->{"_ua"}->get(qq[https://$self->{"_host"}/directory])->content_struct
Truth table66%2/3
A ||= B
A= $self->{"_directory"}B= $self->{"_ua"}->get(qq[https://$self->{"_host"}/directory])->content_struct
ABresult
1X1
011
000
517 }
518  
519 sub _post {
520 3 ▶ my ( $self, $link_name, $data ) = @_;
Subroutine _post: called CC=2 SCAR=7.1
521  
522 3 ▶ my $url = $self->_get_directory()->{$link_name} or die "Unknown link name: “$link_name”";
Branch: 1 branch
BranchTrueFalseTotal
my $url = $self->_get_directory->{$link_name} or die "Unknown link name: \342\200\234$link_name\342\200\235" 0 3 3
523  
524 3 return $self->_post_url( $url, $data );
525 }
526  
527 #mocked in tests
528 sub _post_url {
529 6 ▶ my ( $self, $url, $data ) = @_;
Subroutine _post_url: called CC=1 SCAR=0.0
530  
531 #Do this in case we haven’t initialized the directory yet.
532 #Initializing the directory is necessary to get a nonce.
533 6 $self->_get_directory();
534  
535 6 return $self->{'_ua'}->post( $url, $data );
536 }
537  
538 7 1;
↑ Summary ACME/Utils.pm »