-
Notifications
You must be signed in to change notification settings - Fork 0
/
Acme.pm
1128 lines (923 loc) · 32.6 KB
/
Acme.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Acmepl
#
# Acmepl is is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2016 - 2017 Raphaël Gertz <acme@rapsys.eu>
# Acme package
package Acme;
# Best practice
use strict;
use warnings;
# Add acl support to file tests
use filetest qw(access);
# Symbol export
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(ACCOUNT CONFIG MAIL PENDING TERM THUMBPRINT VERSION);
# Load dependancies
use Carp qw(carp confess);
use Date::Parse qw(str2time);
use DateTime;
use Digest::SHA qw(sha256_base64);
use Email::Valid;
use File::Copy qw(copy);
use File::Path qw(make_path);
use File::Slurp qw(read_file write_file);
use File::Spec qw(splitpath);
use File::stat qw(stat);
use File::Temp; # qw( :seekable );
use IPC::System::Simple qw(capturex);
use JSON qw(from_json to_json);
use LWP;
use MIME::Base64 qw(encode_base64url encode_base64);
use Net::DNS qw();
use Net::Domain::TLD qw(tld_exists);
use POSIX qw(EXIT_FAILURE);
use Tie::IxHash;
# Load debug
#use Data::Dumper;
# Documentation links
#XXX: see https://letsencrypt.github.io/acme-spec/ (probably based on https://ietf-wg-acme.github.io/acme/)
#XXX: see jwk rfc http://www.rfc-editor.org/rfc/rfc7517.txt
#XXX: see javascript implementation https://github.com/diafygi/gethttpsforfree/blob/gh-pages/js/index.js
# Set constants
use constant {
# Config infos
ACCOUNT => '/etc/acme/account.pem',
CONFIG => '/etc/acme/config',
PENDING => '/tmp/acme',
THUMBPRINT => '/etc/acme/thumbprint',
TERM => 'https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf',
MAIL => 'webmaster',
# Certificate info
CSR_SUFFIX => '.der',
# Redhat infos
RH_CERTS => '/etc/pki/tls/certs',
RH_PRIVATE => '/etc/pki/tls/private',
RH_SUFFIX => '.pem',
# Debian infos
DEB_CERTS => '/etc/ssl/certs',
DEB_PRIVATE => '/etc/ssl/private',
DEB_CERTS_SUFFIX => '.crt',
DEB_PRIVATE_SUFFIX => '.key',
# Dns infos
DNS_PREFIX => '_acme-challenge.',
DNS_SUFFIX => '.',
# Key infos
KEY_TYPE => 'rsa',
KEY_SIZE => 4096,
# Acme infos
#ACME_CERT => 'https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem',
ACME_DIR => 'https://acme-staging-v02.api.letsencrypt.org/directory',
ACME_PROD_DIR => 'https://acme-v02.api.letsencrypt.org/directory',
# Version
VERSION => '2.0.4',
# Timeout
TIMEOUT => 300
};
# User agent object
our $ua;
# Strerr backup
our $_stderr;
# JSON Web Key (JWK)
#XXX: tie to Tie::IxHash to keep a stable ordering of hash keys
#our %jwk = (
# pubkey => undef,
# jwk => {
# alg => 'RS256',
# jwk => {
# # Exponent
# e => undef,
# # Key type
# kty => uc(KEY_TYPE),
# # Modulus
# n => undef
# }
# },
# thumbprint => undef
#);
tie(our %jwk, 'Tie::IxHash', pubkey => undef, jwk => undef, thumbprint => undef);
tie(%{$jwk{jwk}}, 'Tie::IxHash', alg => 'RS256', jwk => undef);
#XXX: strict ordering only really needed here for thumbprint sha256 digest
tie(%{$jwk{jwk}{jwk}}, 'Tie::IxHash', e => undef, kty => uc(KEY_TYPE), n => undef);
# Constructor
sub new {
# Extract params
my ($class, $verbose, $domain, $config) = @_;
# Create self hash
my $self = {};
# Link self to package
bless($self, $class);
# Save verbose
$self->{verbose} = $verbose;
# Save domain
$self->{domain} = $domain;
# Save config
$self->{config} = $config;
# Save domains
my @domains = ($domain->{domain}, @{$domain->{domains}});
# Show error if check fail
unless (defined $self->{domain}{mail}) {
confess('Missing mail');
}
# Transform mail in an array
unless (ref($self->{domain}{mail}) eq 'ARRAY') {
$self->{domain}{mail} = [ $self->{domain}{mail} ];
}
# Add extra check to mail validity
#XXX: mxcheck fail if there is only a A record on the domain
my $ev = Email::Valid->new(-fqdn => 1, -tldcheck => 1, -mxcheck => 1);
# Loop on each mail
map {
# Checke address
if (! defined $ev->address($_)) {
map { carp 'failed check: '.$_ if ($self->{verbose}) } $ev->details();
confess('Validate '.$_.' mail address failed');
}
} @{$self->{domain}{mail}};
# Check domains
map {
my $tld;
# Extract tld
unless (($tld) = $_ =~ m/\.(\w+)$/) {
confess('Extract '.$_.' tld failed');
}
# Check if tld exists
unless(Net::Domain::TLD::tld_exists($tld)) {
confess('Extracted '.$_.' tld '.$tld.' do not exists');
}
# Search a record
my $a = Net::DNS::Resolver->new->search($_, 'A', 'IN');
# Search aaaa record
my $aaaa = Net::DNS::Resolver->new->search($_, 'AAAA', 'IN');
# Trigger error for unresolvable domain
unless (
# Check if either has a A or AAAA record
scalar map {
($_->type eq 'A' or $_->type eq 'AAAA') ? 1 : ();
}
# Merge both answer
(
(defined $a and defined $a->answer) ? $a->answer : (),
(defined $aaaa and defined $aaaa->answer) ? $aaaa->answer : ()
)
) {
confess('Resolve '.$_.' to an A or AAAA record failed');
}
} @domains;
# Return class reference
return $self;
}
# Prepare environement
sub prepare {
my ($self) = @_;
# Extract cert directory and filename
my ($certFile, $certDir) = File::Spec->splitpath($self->{domain}{cert});
# Extract key directory and filename
my ($keyFile, $keyDir) = File::Spec->splitpath($self->{domain}{key});
# Extract account directory and filename
my ($accountFile, $accountDir) = File::Spec->splitpath($self->{domain}{account});
# Create all paths
{
make_path($certDir, $keyDir, $accountDir, $self->{config}{pending}, {error => \my $err});
if (@$err) {
map {
my ($file, $msg) = %{$_};
carp 'Mkdir '.($file ? $file.' ' : '').'failed: '.$msg if ($self->{verbose});
} @$err;
confess('Make path failed');
}
}
# Create user agent
$ua = LWP::UserAgent->new;
$ua->agent(__PACKAGE__.'/'.VERSION);
# Check that certificate is writable
unless (-w $certDir || -w $self->{domain}{cert}) {
confess('Directory '.$certDir.' or file '.$self->{domain}{cert}.' must be writable: '.$!);
}
# Check that key is readable or parent directory is writable
unless (-r $self->{domain}{key} || -w $keyDir) {
confess('File '.$self->{domain}{key}.' must be readable or directory '.$keyDir.' must be writable: '.$!);
}
# Check that account key is readable or parent directory is writable
unless (-r $self->{domain}{account} || -w $accountDir) {
confess('File '.$self->{domain}{account}.' must be readable or directory '.$accountDir.' must be writable: '.$!);
}
# Backup old certificate if possible
if (-w $certDir && -f $self->{domain}{cert}) {
my ($dt, $suffix) = undef;
# Extract datetime suffix
$suffix = ($dt = DateTime->from_epoch(epoch => stat($self->{domain}{cert})->mtime))->ymd('').$dt->hms('');
# Rename old certificate
unless(copy($self->{domain}{cert}, $self->{domain}{cert}.'.'.$suffix)) {
carp('Copy '.$self->{domain}{cert}.' to '.$self->{domain}{cert}.'.'.$suffix.' failed: '.$!);
}
}
}
# Drop stderr
sub _dropStdErr {
# Save stderr
open($_stderr, '>&STDERR') or die $!;
# Close it
close(STDERR) or die $!;
# Send to /dev/null
open(STDERR, '>', '/dev/null') or die $!;
}
# Restore stderr
sub _restoreStdErr {
# Close stderr
close(STDERR);
# Open it back
open(STDERR, '>&', $_stderr) or die $!;
}
# Generate required keys
sub genKeys {
my ($self) = @_;
# Generate account and server key if required
map {
# Check key existence
if (! -f $_) {
# Drop stderr
_dropStdErr();
# Generate key
#XXX: we drop stderr here because openssl can't be quiet on this command
capturex('openssl', ('genrsa', '-out', $_, KEY_SIZE));
# Restore stderr
_restoreStdErr();
}
} ($self->{domain}{account}, $self->{domain}{key});
# Extract modulus and publicExponent jwk
#XXX: tie to Tie::IxHash to keep a stable ordering of hash keys
tie(%{$self->{account}}, 'Tie::IxHash', %jwk);
map {
if (/^Modulus=([0-9A-F]+)$/) {
# Extract to binary from hex and convert to base64 url
$self->{account}{jwk}{jwk}{n} = encode_base64url(pack("H*", $1) =~ s/^\0+//r);
} elsif (/^publicExponent:\s([0-9]+)\s\(0x[0-1]+\)$/) {
# Extract to binary from int, trim leading zeros and convert to base64 url
chomp ($self->{account}{jwk}{jwk}{e} = encode_base64url(pack("N", $1) =~ s/^\0+//r));
}
} capturex('openssl', ('rsa', '-text', '-in', $self->{domain}{account}, '-noout', '-modulus'));
# Drop stderr
_dropStdErr();
# Extract account public key
$self->{account}{pubkey} = join('', map { chomp; $_; } capturex('openssl', ('rsa', '-in', $self->{domain}{account}, '-pubout')));
# Restore stderr
_restoreStdErr();
# Store thumbprint
#XXX: convert base64 to base64 url
$self->{account}{thumbprint} = (sha256_base64(to_json($self->{account}{jwk}{jwk})) =~ s/=+\z//r) =~ tr[+/][-_]r;
}
# Directory call
sub directory {
my ($self) = @_;
# Set time
my $time = time;
# Set directory
my $dir = $self->{domain}{prod} ? ACME_PROD_DIR : ACME_DIR;
# Create a request
my $req = HTTP::Request->new(GET => $dir.'?'.$time);
# Get request
my $res = $ua->request($req);
# Handle error
unless ($res->is_success) {
confess('GET '.$dir.'?'.$time.' failed: '.$res->status_line);
}
# Init content
my %content;
# Extract content
unless (%content = %{from_json($res->content)}) {
confess('GET '.$dir.'?'.$time.' from_json failed: '.$res->status_line);
}
# Merge uris in self content
$self->{req}{dir} = $dir;
$self->{req}{keyChange} = $content{keyChange};
$self->{req}{newNonce} = $content{newNonce};
$self->{req}{newAccount} = $content{newAccount};
$self->{req}{revokeCert} = $content{revokeCert};
$self->{req}{newOrder} = $content{newOrder};
# Check term
unless ($self->{config}{term} eq $content{meta}{termsOfService}) {
confess('GET '.$dir.'?'.$time.' term: '.$content{meta}{termsOfService}.' differ from config: '.$self->{config}{term});
}
}
# Nonce call
sub nonce {
my ($self) = @_;
# Set time
my $time = time;
# Create a request
my $req = HTTP::Request->new(HEAD => $self->{req}{newNonce}.'?'.$time);
# Get request
my $res = $ua->request($req);
# Handle error
unless ($res->is_success) {
confess('HEAD '.$self->{req}{newNonce}.'?'.$time.' failed: '.$res->status_line);
}
# Save nonce
$self->{req}{nonce} = $res->headers->{'replay-nonce'};
}
# Post request
sub _post {
my ($self, $uri, $payload) = @_;
# Init protected
#XXX: tie to Tie::IxHash to keep a stable ordering of hash keys
#XXX: strict ordering only really needed here for thumbprint sha256 digest
tie(my %protected, 'Tie::IxHash', alg => $self->{account}{jwk}{alg}, jwk => $self->{account}{jwk}{jwk}, nonce => $self->{req}{nonce}, url => $uri);
# We have a kid
if (defined($self->{req}{kid})) {
# Replace jwk entry with it
#XXX: when kid is available all request with jwk are rejected by the api
%protected = (alg => $self->{account}{jwk}{alg}, kid => $self->{req}{kid}, nonce => $self->{req}{nonce}, url => $uri);
}
# Encode protected
my $protected = encode_base64url(to_json(\%protected));
# Encode payload
$payload = encode_base64url(to_json($payload)) unless ($payload eq '');
# Sign temp file
my $stf = File::Temp->new();
# Append protect.payload to stf
print $stf $protected.'.'.$payload;
# Close stf
close($stf);
# Generate digest of stf
my $signature = encode_base64url(join('', capturex('openssl', ('dgst', '-sha256', '-binary', '-sign', $self->{domain}{account}, $stf->filename))) =~ s/^\0+//r);
# Create a request
my $req = HTTP::Request->new(POST => $uri);
# Set request header
$req->header('Content-Type' => 'application/jose+json');
# Set new-reg request content
$req->content(to_json({
protected => $protected,
payload => $payload,
signature => $signature
}));
# Post request
my $res = $ua->request($req);
# Save nonce
if (defined $res->headers->{'replay-nonce'}) {
$self->{req}{nonce} = $res->headers->{'replay-nonce'};
}
# Return res object
return $res;
}
# Resolve dns and check content
#XXX: see https://community.centminmod.com/threads/looks-like-letsencrypt-dns-01-is-ready.5845/#12 for example
sub _dnsCheck {
my ($self, $domain, $token) = @_;
# Generate signature from content
my $signature = ((sha256_base64($token.'.'.$self->{account}{thumbprint})) =~ s/=+\z//r) =~ tr[+/][-_]r;
# Search txt record
my $txt = Net::DNS::Resolver->new->search(DNS_PREFIX.$domain.DNS_SUFFIX, 'TXT', 'IN');
# Check that we have a txt record
unless (defined $txt and defined $txt->answer and scalar map { $_->type eq 'TXT' ? 1 : (); } $txt->answer) {
carp 'Resolve '.DNS_PREFIX.$domain.DNS_SUFFIX.' to a TXT record failed' if ($self->{verbose});
return;
}
# Check that txt record data match signature
unless (scalar map { ($_->type eq 'TXT' and $_->txtdata eq $signature) ? 1 : (); } $txt->answer) {
# Check verbose
if ($self->{verbose}) {
# Loop on each answer
map {
# Check if we have a TXT record with different value
if ($_->type eq 'TXT' and $_->txtdata ne $signature) {
carp 'Resolved '.DNS_PREFIX.$domain.DNS_SUFFIX.' with "'.$_->txtdata.'" instead of "'.$signature.'"';
}
} $txt->answer;
}
return;
}
# Return success
return 1;
}
# Get uri and check content
sub _httpCheck {
my ($self, $domain, $token) = @_;
# Create a request
my $req = HTTP::Request->new(GET => 'http://'.$domain.'/.well-known/acme-challenge/'.$token);
# Check if thumbprint is writeable
if (-w $self->{config}{thumbprint}) {
# Try to write thumbprint
write_file($self->{config}{thumbprint}, $self->{account}{thumbprint});
}
# Get request
my $res = $ua->request($req);
# Handle error
unless ($res->is_success) {
carp 'Fetch http://'.$domain.'/.well-known/acme-challenge/'.$token.' failed: '.$res->status_line if ($self->{verbose});
return;
}
# Handle invalid content
unless($res->content =~ /^$token.$self->{account}{thumbprint}\s*$/) {
carp 'Fetched http://'.$domain.'/.well-known/acme-challenge/'.$token.' with "'.$res->content.'" instead of "'.$token.'.'.$self->{account}{thumbprint}.'"' if ($self->{verbose});
return;
}
# Return success
return 1;
}
# Register account
#XXX: see doc at https://ietf-wg-acme.github.io/acme/#rfc.section.6.3
sub account {
my ($self) = @_;
# Init pending directory
$self->{req}{pending} = $self->{config}{pending}.'/'.encode_base64url($self->{req}{dir}).'/'.encode_base64url(join(',', @{$self->{domain}{mail}}));
# Create pending directory
{
make_path($self->{req}{pending}, {error => \my $err});
if (@$err) {
map {
my ($file, $msg) = %{$_};
carp 'Mkdir '.($file ? $file.' ' : '').'failed: '.$msg if ($self->{verbose});
} @$err;
confess('Make path failed');
}
}
# Init file
#XXX: we use this file to store the fetched account
my $file = $self->{req}{pending}.'/'.(((sha256_base64(join(',', @{$self->{domain}{mail}}))) =~ s/=+\z//r) =~ tr[+/][-_]r);
# Init content
my $content = undef;
# Load account content or post a new one
if (
#XXX: use eval to workaround a fatal in from_json
! defined eval {
# Check that file exists
-f $file &&
# Read it
($content = read_file($file)) &&
# Decode it
($content = from_json($content))
}
) {
# Init tied payload
#XXX: tie to Tie::IxHash to keep a stable ordering of hash keys
tie(my %payload, 'Tie::IxHash', termsOfServiceAgreed => JSON::true, contact => []);
# Loop on mails
map {
# Append mail to payload
$payload{contact}[scalar @{$payload{contact}}] = 'mailto:'.$_;
} @{$self->{domain}{mail}};
# Post newAccount request
# TODO: change contact field in config to contain directly the array [mailto:example@example.com,...] ???
#XXX: contact array may contain a tel:+33612345678 for example (supported ???)
my $res = $self->_post($self->{req}{'newAccount'}, \%payload);
# Handle error
unless ($res->is_success) {
confess('POST '.$self->{req}{'newAccount'}.' failed: '.$res->status_line)
}
# Store kid from header location
$content = {
'kid' => $res->headers->{location},
};
# Write to file
write_file($file, to_json($content));
}
# Set kid from content
$self->{req}{kid} = $content->{kid};
}
# Authorize domains
sub order {
my ($self) = @_;
# Init file
#XXX: we use this file to store the requested domains on our side
#XXX: see bug https://github.com/letsencrypt/boulder/issues/3335 and https://community.letsencrypt.org/t/acmev2-orders-list/51662
my $file = $self->{req}{pending}.'/'.(((sha256_base64(join(',', ($self->{domain}{domain}, @{$self->{domain}{domains}})))) =~ s/=+\z//r) =~ tr[+/][-_]r);
# Init content
my $content = undef;
# Load account content or post a new one
if (
#XXX: use eval to workaround a fatal in from_json
! defined eval {
# Check that file exists
-f $file &&
# Read it
($content = read_file($file)) &&
# Decode it
($content = from_json($content))
# Check expiration
} || (str2time($content->{expires}) <= time()+3600)
) {
# Init tied payload
#XXX: tie to Tie::IxHash to keep a stable ordering of hash keys
#XXX: https://www.perlmonks.org/?node_id=1215976
#XXX: optional notBefore, notAfter, see https://ietf-wg-acme.github.io/acme/draft-ietf-acme-acme.html#applying-for-certificate-issuance
tie(my %payload, 'Tie::IxHash', identifiers => []);
# Loop on domains
map {
# Tie in a stable hash and append to identifiers array
#XXX: tie to Tie::IxHash to keep a stable ordering of hash keys
tie(%{$payload{identifiers}[scalar @{$payload{identifiers}}]}, 'Tie::IxHash', type => 'dns', value => $_);
} ($self->{domain}{domain}, @{$self->{domain}{domains}});
# Post new order request
my $res = $self->_post($self->{req}{'newOrder'}, \%payload);
# Handle error
unless ($res->is_success) {
confess('POST '.$self->{req}{'newOrder'}.' failed: '.$res->status_line);
}
# Handle error
unless ($res->content) {
confess('POST '.$self->{req}{'newOrder'}.' empty content: '.$res->status_line);
}
# Handle error
unless ($res->headers->{location}) {
confess('POST '.$self->{req}{'newOrder'}.' missing location: '.$res->status_line);
}
# Extract content
$content = from_json($res->content);
# Write to file
write_file($file, to_json($content));
}
# Save the authorizations
$self->{req}{authorizations} = [ keys %{{ map { $_ => undef } @{$content->{authorizations}} }} ];
# Save the finalize uri
$self->{req}{finalize} = $content->{finalize};
# Create challenges hash
%{$self->{req}{challenges}} = ();
# Extract authorizations
map {
# Init uri
my $uri = $_;
# Init content
my $content = undef;
# Init file
#XXX: tmpdir.'/'.<orderuri>.'/'.<authuri>
my $file = $self->{req}{pending}.'/'.encode_base64url($uri);
# Load auth request content or post a new one
#TODO: add more check on cache file ???
if (
#XXX: use eval to workaround a fatal in from_json
! defined eval {
# Check that file exists
-f $file &&
# Read it
($content = read_file($file)) &&
# Decode it
($content = from_json($content))
# Check expiration
} || (str2time($content->{expires}) <= time()+3600)
) {
# Post new-authz request
my $res = $self->_post($uri, '');
# Handle error
unless ($res->is_success) {
confess('POST '.$uri.' failed: '.$res->status_line);
}
# Decode content
$content = from_json($res->content);
# Check identifier
unless (
defined $content->{identifier} and
defined $content->{identifier}{type} and
defined $content->{identifier}{value}
) {
confess('POST '.$uri.' missing identifier: '.$res->status_line);
} else {
unless (
$content->{identifier}{type} eq 'dns' and
$content->{identifier}{value}
) {
confess('POST '.$uri.' invalid identifier: '.$res->status_line);
}
}
# Check status
unless ($content->{status} eq 'valid' or $content->{status} eq 'pending') {
confess('POST '.$uri.' for '.$content->{identifier}{value}.' failed: '.$res->status_line);
}
# Write to file
write_file($file, to_json($content));
}
# Add challenge
%{$self->{req}{challenges}{$content->{identifier}{value}}} = (
status => $content->{status},
expires => $content->{expires},
challenges => {},
polls => {}
);
# Extract challenges
map {
# Save if valid
if ($_->{status} eq 'valid') {
$self->{req}{challenges}{$content->{identifier}{value}}{status} = $_->{status};
# Check is still polling
} elsif ($content->{status} eq 'pending') {
# Add to challenges list for later use
$self->{req}{challenges}{$content->{identifier}{value}}{challenges}{$_->{type}} = {
status => $_->{status},
token => $_->{token},
url => $_->{url}
};
}
} @{$content->{challenges}};
# Set identifier
my $identifier = $content->{identifier}{value};
# Save pending data
if ($self->{req}{challenges}{$identifier}{status} eq 'pending') {
# Check challenges
map {
# One test already validated this auth request
unless($self->{req}{challenges}{$identifier}{status} eq 'valid') {
# One challenge validated
if ($self->{req}{challenges}{$identifier}{challenges}{$_}{status} eq 'valid') {
$self->{req}{challenges}{$identifier}{status} = $self->{req}{challenges}{$identifier}{challenges}{$_}{status};
# This challenge is to be validated
} elsif ($self->{req}{challenges}{$identifier}{challenges}{$_}{status} eq 'pending') {
#TODO: implement tls-alpn-01 challenge someday if possible
if (
($_ eq 'http-01' and $self->_httpCheck($identifier, $self->{req}{challenges}{$identifier}{challenges}{$_}{token})) or
($_ eq 'dns-01' and $self->_dnsCheck($identifier, $self->{req}{challenges}{$identifier}{challenges}{$_}{token}))
) {
# Init file
#XXX: tmpdir.'/'.<orderuri>.'/'.<authuri>
my $file = $self->{req}{pending}.'/'.encode_base64url($self->{req}{challenges}{$identifier}{challenges}{$_}{url});
# Reset content
$content = undef;
# Load auth request content or post a new one
#TODO: add more check on cache file ???
if (
#XXX: use eval to workaround a fatal in from_json
! defined eval {
# Check that file exists
-f $file &&
# Read it
($content = read_file($file)) &&
# Decode it
($content = from_json($content))
#TODO: Check file modification time ? There is no expires field in json answer
}# || (str2time($content->{expires}) <= time()+3600)
) {
# Post challenge request
my $res = $self->_post(
$self->{req}{challenges}{$identifier}{challenges}{$_}{url},
{keyAuthorization => $self->{req}{challenges}{$identifier}{challenges}{$_}{token}.'.'.$self->{account}{thumbprint}}
);
# Handle error
unless ($res->is_success) {
confess('POST '.$self->{req}{challenges}{$identifier}{challenges}{$_}{url}.' failed: '.$res->status_line);
}
# Extract content
$content = from_json($res->content);
# Write to file
write_file($file, to_json($content));
}
# Save if valid
if ($content->{status} eq 'valid') {
$self->{req}{challenges}{$identifier}{status} = $content->{status};
# Check is still polling
} elsif ($content->{status} eq 'pending') {
# Add to poll list for later use
$self->{req}{challenges}{$identifier}{polls}{$content->{type}} = 1;
}
}
}
}
} keys %{$self->{req}{challenges}{$identifier}{challenges}};
# Check if check is challenge still in pending and no polls
if ($self->{req}{challenges}{$identifier}{status} eq 'pending' && scalar keys %{$self->{req}{challenges}{$identifier}{polls}} == 0) {
# Loop on all remaining challenges
map {
#TODO: implement tls-alpn-01 challenge someday if possible
# Display help for http-01 check
if ($_ eq 'http-01') {
print STDERR 'Require URI http://'.$identifier.'/.well-known/acme-challenge/'.$self->{req}{challenges}{$identifier}{challenges}{$_}{token}.' with "'.$self->{req}{challenges}{$identifier}{challenges}{$_}{token}.'.'.$self->{account}{thumbprint}.'"'."\n";
# Display help for dns-01 check
} elsif ($_ eq 'dns-01') {
print STDERR 'Require TXT record _acme-challenge.'.$identifier.'. with "'.(((sha256_base64($self->{req}{challenges}{$identifier}{challenges}{$_}{token}.'.'.$self->{account}{thumbprint})) =~ s/=+\z//r) =~ tr[+/][-_]r).'"'."\n";
}
} keys %{$self->{req}{challenges}{$identifier}{challenges}};
}
}
} @{$self->{req}{authorizations}};
# Init max run
my $remaining = TIMEOUT;
# Poll pending
while (--$remaining >= 0 and scalar map { ($_->{status} eq 'pending' and scalar keys %{$_->{polls}}) ? 1 : (); } values %{$self->{req}{challenges}}) {
# Sleep
sleep(1);
# Poll remaining pending
map {
# Init identifier
my $identifier = $_;
# Poll remaining polls
map {
# Post challenge request
#XXX: no cache here we force update
my $res = $self->_post(
$self->{req}{challenges}{$identifier}{challenges}{$_}{url},
{keyAuthorization => $self->{req}{challenges}{$identifier}{challenges}{$_}{token}.'.'.$self->{account}{thumbprint}}
);
# Handle error
unless ($res->is_success) {
confess('POST '.$self->{req}{challenges}{$identifier}{challenges}{$_}{url}.' failed: '.$res->status_line);
}
# Extract content
$content = from_json($res->content);
# Init file
#XXX: tmpdir.'/'.<orderuri>.'/'.<authuri>
my $file = $self->{req}{pending}.'/'.encode_base64url($self->{req}{challenges}{$identifier}{challenges}{$_}{url});
# Write to file
write_file($file, to_json($content));
# Save status
if ($content->{status} ne 'pending') {
$self->{req}{challenges}{$identifier}{status} = $content->{status};
}
} keys %{$self->{req}{challenges}{$identifier}{polls}};
} map { $self->{req}{challenges}{$_}{status} eq 'pending' ? $_ : (); } keys %{$self->{req}{challenges}};
}
# Check if thumbprint is writeable
if (-w $self->{config}{thumbprint}) {
# Try to write thumbprint
write_file($self->{config}{thumbprint}, '');
}
# Stop here with remaining challenge
if (scalar map { $_->{status} ne 'valid' ? 1 : (); } values %{$self->{req}{challenges}}) {
#TODO: Deactivate all activated domains ?
#XXX: see if implemented by letsencrypt ACMEv2
#map {
# # Post deactivation request
# my $res = $self->_post($self->{challenges}{$_}{http_uri}, {resource => 'authz', status => 'deactivated'});
# # Handle error
# unless ($res->is_success) {
# confess('POST '.$self->{challenges}{$_}{http_uri}.' failed: '.$res->status_line);
# }
#} map { $self->{challenges}{$_}{status} eq 'valid' ? $_ : () } keys %{$self->{challenges}};
# Stop here as a domain of csr list failed authorization
if ($self->{verbose}) {
my @domains = map { $self->{req}{challenges}{$_}{status} ne 'valid' ? $_ : (); } keys %{$self->{req}{challenges}};
#my @domains = map { ! defined $self->{challenges}{$_}{status} or $self->{challenges}{$_}{status} ne 'valid' ? $_ : (); } keys %{$self->{challenges}};
carp 'Fix challenge'.(scalar @domains > 1?'s':'').' for: '.join(', ', @domains);
}
exit EXIT_FAILURE;
}
}
# Generate certificate request
sub genCsr {
my ($self) = @_;
# Init csr file
#XXX: tmpdir.'/'.<orderuri>.'/'.<thumbprint>.':'.<mail>.':'.join(',', @domains).'.<prodstaging>.'.CSR_SUFFIX
$self->{req}{csr} = $self->{req}{pending}.'/'.(((sha256_base64(join(',', ($self->{domain}{domain}, @{$self->{domain}{domains}})))) =~ s/=+\z//r) =~ tr[+/][-_]r).CSR_SUFFIX;
# Reuse certificate request file without domain/mail change
if (! -f $self->{req}{csr}) {
# Openssl config template
my $oct = File::Temp->new(UNLINK => 0);
# Save data start position
my $pos = tell DATA;
# Init counter
my $i = 0;
# Prepare mail
my $mail = join("\n", map { $i++.'.emailAddress'."\t\t\t".'= '.$_; } @{$self->{domain}{mail}});
# Load template from data
map { s/__EMAIL_ADDRESS__/$mail/; s/__COMMON_NAME__/$self->{domain}{domain}/; print $oct $_; } <DATA>;
# Reseek data
seek(DATA, $pos, 0);
# Append domain names
$i = 0;
map { print $oct 'DNS.'.$i++.' = '.$_."\n"; } ($self->{domain}{domain}, @{$self->{domain}{domains}});
# Generate csr
#XXX: read certificate request with: openssl req -inform DER -in $self->{req}{csr} -text
capturex('openssl', ('req', '-new', '-outform', 'DER', '-key', $self->{domain}{key}, '-config', $oct->filename, '-out', $self->{req}{csr}));
# Close oct
close($oct);
}
}
# Issue certificate
sub issue {
my ($self) = @_;
# Open csr file
open(my $fh, '<', $self->{req}{csr}) or die $!;
# Load csr
my $csr = encode_base64url(join('', <$fh>) =~ s/^\0+//r);
# Close csr file
close($fh) or die $!;
# Init file
#XXX: tmpdir.'/'.<orderuri>.'/'.<finalizeuri>
my $file = $self->{req}{pending}.'/'.encode_base64url($self->{req}{finalize});
# Init content
my $content = undef;
# Init res
my $res = undef;
# Load auth request content or post a new one
#TODO: add more check on cache file ???
if (
#XXX: use eval to workaround a fatal in from_json
! defined eval {
# Check that file exists
-f $file &&
# Read it
($content = read_file($file)) &&
# Decode it
($content = from_json($content))
# Check file modification time ? There is no expires field in json answer