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