ACME.pm

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