-
Notifications
You must be signed in to change notification settings - Fork 9
/
letsencrypt.sh
executable file
·1489 lines (1251 loc) · 46.2 KB
/
letsencrypt.sh
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
#!/bin/sh
# letsencrypt.sh - a simple shell implementation for the acme protocol
# Copyright (C) 2015 Gerhard Heift
# Copyright (C) 2016-2024 Attila Bruncsak
#
# This program 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
CADIR="https://api.test4.buypass.no/acme/directory"
CADIR="https://acme-staging-v02.api.letsencrypt.org/directory"
# Prefix the following line with "# letsencrypt-production-server #", to use
# the staging server of letsencrypt. The staging server has lower rate limits,
# but does not issue valid certificates. To automatically remove the comment
# again on commiting the file, add the filter to your git config by running
# git config filter.production-server.clean misc/filter-production-server
CADIR="https://api.buypass.com/acme/directory"
CADIR="https://acme-v02.api.letsencrypt.org/directory"
# global variables:
# base64url encoded JSON nonce, generated from Replay-Nonce header
# see gen_protected()
PROTECTED=
# base64url encoded JSON request object
PAYLOAD=
# base64url encoded signature of PROTECTED and PAYLOAD
# see also gen_signature()
SIGNATURE=
# the account key used to send the requests and to verify the domain challenges
ACCOUNT_KEY=
# the JSON Web Key is the representation of the key as JSON object
ACCOUNT_JWK=
# the JSON object to specify the signature format
ACCOUNT_ID=
# the thumbprint is the checksum of the JWK and is used for the challenges
ACCOUNT_THUMB=
# the private key, which should be signed by the CA
SERVER_KEY=
# the certificate signing request, which sould be used
SERVER_CSR=
# the location, where the certificate should be stored
SERVER_CERT=
# the location, where the certificate with the signing certificate(s) should be stored
SERVER_FULL_CHAIN=
# the location, where the signing certificate(s) should be stored
SERVER_SIGNING_CHAIN=
# selection of the signing chain
SIGNING_CHAIN_SELECTION=0
# the e-mail address to be used with the account key, only needed if account
# key is not yet registred
ACCOUNT_EMAIL=
# a list of domains, which should be assigned to the certificate
DOMAINS=
# a list of domains, challenge uri, token and authorization uri
DOMAIN_DATA=
# the directory, where to push the response
# $DOMAIN or ${DOMAIN} will be replaced with the actual domain
WEBDIR=
# the script to be called to push the response to a remote server
PUSH_TOKEN=
# the script to be called to push the response to a remote server needs the commit feature
PUSH_TOKEN_COMMIT=
# set the option of the preferred IP family for connecting to the ACME server
IPV_OPTION=
# the challenge type, can be dns-01 or http-01 (default)
CHALLENGE_TYPE="http-01"
# the date of the that version
VERSION_DATE="2024-07-09"
# The meaningful User-Agent to help finding related log entries in the ACME server log
USER_AGENT="bruncsak/ght-acme.sh $VERSION_DATE"
LOGLEVEL=1
# utility functions
tolower() {
printf '%s' "$*" | tr A-Z a-z
}
HexadecimalStringToOctalEscapeSequence() {
tr '[A-F]' '[a-f]' "$@" | tr -d '\r\n' |
sed -e 's/[^0-9a-f]//g; s/^\(\(..\)\{0,\}\).$/\1/;
s/\([0-9a-f]\)\([0-9a-f]\)/\1_\2/g; s/$/\\c/;
s/_0/o0/g; s/_1/o1/g; s/_2/o2/g; s/_3/o3/g;
s/_4/o4/g; s/_5/o5/g; s/_6/o6/g; s/_7/o7/g;
s/_8/i0/g; s/_9/i1/g; s/_a/i2/g; s/_b/i3/g;
s/_c/i4/g; s/_d/i5/g; s/_e/i6/g; s/_f/i7/g;
s/0o/\\000/g; s/0i/\\001/g; s/1o/\\002/g; s/1i/\\003/g;
s/2o/\\004/g; s/2i/\\005/g; s/3o/\\006/g; s/3i/\\007/g;
s/4o/\\010/g; s/4i/\\011/g; s/5o/\\012/g; s/5i/\\013/g;
s/6o/\\014/g; s/6i/\\015/g; s/7o/\\016/g; s/7i/\\017/g;
s/8o/\\020/g; s/8i/\\021/g; s/9o/\\022/g; s/9i/\\023/g;
s/ao/\\024/g; s/ai/\\025/g; s/bo/\\026/g; s/bi/\\027/g;
s/co/\\030/g; s/ci/\\031/g; s/do/\\032/g; s/di/\\033/g;
s/eo/\\034/g; s/ei/\\035/g; s/fo/\\036/g; s/fi/\\037/g;
'
}
hex2bin() {
xxd -r -p
# echo $ECHOESCFLAG "`HexadecimalStringToOctalEscapeSequence`"
}
base64url() {
openssl base64 | tr '+/' '-_' | tr -d '\r\n='
}
log() {
if [ "$LOGLEVEL" -gt 0 ]; then
echo "$@" >& 2
fi
}
dbgmsg() {
if [ "$LOGLEVEL" -gt 1 ]; then
echo "$@" >& 2
fi
}
errmsg() {
echo "$@" >& 2
}
err_exit() {
RETCODE="$?"
[ -n "$2" ] && RETCODE="$2"
[ -n "$1" ] && printf "%s\n" "$1" >& 2
exit "$RETCODE"
}
required_commands() {
REQUIRED_COMMANDS="basename cat cp rm sed grep egrep fgrep tr mktemp expr tail xxd openssl"
if [ "$USE_WGET" = yes ] ;then
REQUIRED_COMMANDS="$REQUIRED_COMMANDS wget"
else
REQUIRED_COMMANDS="$REQUIRED_COMMANDS curl"
fi
for command in $REQUIRED_COMMANDS ;do
command -v $command > /dev/null || err_exit "The command '$command' is required to run $PROGNAME"
done
}
validate_domain() {
DOMAIN_IN="$1"
if [ "$DOMAIN_IN" = _ ]; then
return 1
fi
DOMAIN_OUT="`printf "%s\n" "$DOMAIN_IN" | sed -e 's/^...$/!/; s/^.\{254,\}$/!/; s/^'"$DOMAIN_EXTRA_PAT"'\([a-zA-Z0-9]\([-a-zA-Z0-9]\{0,61\}[a-zA-Z0-9]\)\{0,1\}\.\)\{1,\}\([a-zA-Z]\([-a-zA-Z0-9]\{0,61\}[a-zA-Z]\)\)$/_/;'`"
if [ "$DOMAIN_OUT" = _ ]; then
return 0
else
return 1
fi
}
handle_wget_exit() {
WGET_EXIT="$1"
WGET_URI="$2"
if [ "$WGET_EXIT" -ne 0 -a "$WGET_EXIT" -ne 8 -o -s "$WGET_OUT" ]; then
echo "error while making a web request to \"$WGET_URI\"" >& 2
echo "wget exit status: $WGET_EXIT" >& 2
case "$WGET_EXIT" in
# see man wget "EXIT STATUS"
4) echo " Network failure" >& 2;;
5) echo " SSL verification failure" >& 2;;
8) echo " Server issued an error response" >& 2;;
esac
cat "$WGET_OUT" >& 2
cat "$RESP_HEABOD" >& 2
exit 1
elif [ "$WGET_EXIT" -eq 8 -a ! -s "$RESP_HEABOD" ] ;then
echo "error while making a web request to \"$WGET_URI\"" >& 2
echo "wget exit status: $WGET_EXIT" >& 2
err_exit "Server issued an error response and no error document returned and no --content-on-error flag available. Upgrade your wget or use curl instead." 1
fi
tr -d '\r' < "$RESP_HEABOD" | sed -e '/^$/,$d' > "$RESP_HEADER"
tr -d '\r' < "$RESP_HEABOD" | sed -e '1,/^$/d' > "$RESP_BODY"
}
curl_return_text()
{
CURL_EXIT="$1"
case "$CURL_EXIT" in
# see man curl "EXIT CODES"
3) TXT=", malformed URI" ;;
6) TXT=", could not resolve host" ;;
7) TXT=", failed to connect" ;;
28) TXT=", operation timeout" ;;
35) TXT=", SSL connect error" ;;
52) TXT=", the server did not reply anything" ;;
56) TXT=", failure in receiving network data" ;;
*) TXT="" ;;
esac
printf "curl return status: %d%s" "$1" "$TXT"
}
curl_loop()
{
CURL_ACTION="$1"; shift
loop_count=0
pluriel=""
while : ;do
dbgmsg "About making a web request to \"$CURL_ACTION\""
curl "$@"
CURL_RETURN_CODE="$?"
[[ "$loop_count" -ge 20 ]] && break
case "$CURL_RETURN_CODE" in
6) ;;
7) ;;
28) ;;
35) ;;
52) ;;
56) ;;
*) break ;;
esac
(( loop_count += 1 ))
dbgmsg "While making a web request to \"$CURL_ACTION\" sleeping $loop_count second$pluriel before retry due to `curl_return_text $CURL_RETURN_CODE`"
sleep "$loop_count"
pluriel="s"
done
if [ "$CURL_RETURN_CODE" -ne 0 ] ;then
errmsg "While making a web request to \"$CURL_ACTION\" error exiting due to `curl_return_text $CURL_RETURN_CODE` (retry number: $loop_count)"
exit "$CURL_RETURN_CODE"
else
dbgmsg "While making a web request to \"$CURL_ACTION\" continuing due to `curl_return_text $CURL_RETURN_CODE`"
fi
}
handle_openssl_exit() {
OPENSSL_EXIT=$1
OPENSSL_ACTION=$2
if [ "$OPENSSL_EXIT" "!=" 0 ]; then
echo "error while $OPENSSL_ACTION" >& 2
echo "openssl exit status: $OPENSSL_EXIT" >& 2
cat "$OPENSSL_ERR" >& 2
exit 1
fi
}
fetch_http_status() {
HTTP_STATUS="`sed -e '/^HTTP\// !d; s/^HTTP\/[0-9.]\{1,\} *\([^ ]*\).*$/\1/' "$RESP_HEADER" | tail -n 1`"
}
check_http_status() {
[ "$HTTP_STATUS" = "$1" ]
}
check_acme_error() {
fgrep -q "urn:ietf:params:acme:error:$1" "$RESP_BODY"
}
unhandled_response() {
echo "unhandled response while $1" >& 2
echo >& 2
cat "$RESP_HEADER" "$RESP_BODY" >& 2
echo >& 2
exit 1
}
show_error() {
if [ -n "$1" ]; then
echo "error while $1" >& 2
fi
ERR_TYPE="`tr -d '\r\n' < "$RESP_BODY" | sed -e 's/.*"type": *"\([^"]*\)".*/\1/'`"
ERR_DETAILS="`tr -d '\r\n' < "$RESP_BODY" | sed -e 's/.*"detail": *"\([^"]*\)".*/\1/'`"
echo " $ERR_DETAILS ($ERR_TYPE)" >& 2
}
header_field_value() {
grep -i -e "^$1:.*$2" "$RESP_HEADER" | sed -e 's/^[^:]*: *//' | tr -d '\r\n'
}
fetch_next_link() {
header_field_value Link ';rel="next"' | sed -s 's/^.*<\(.*\)>.*$/\1/'
}
fetch_alternate_link() {
header_field_value Link ';rel="alternate"' | sed -s 's/^.*<\(.*\)>.*$/\1/'
}
fetch_location() {
header_field_value Location
}
# retrieve the nonce from the response header of the actual request for the forthcomming POST request
extract_nonce() {
new_nonce="`header_field_value Replay-Nonce`"
if [ -n "$new_nonce" ] ;then
# Log if we had unnecesseraily multiple nonces, but use always the latest nonce
[ -n "$NONCE" ] && log "droping unused nonce: $NONCE"
NONCE="$new_nonce"
dbgmsg " new nonce: $NONCE"
else
dbgmsg "no new nonce"
fi
}
retry_after() {
header_field_value Retry-After
}
sleep_retryafter() {
RETRY_AFTER="`retry_after`"
if printf '%s' "$RETRY_AFTER" | egrep -s -q -e '^[1-9][0-9]*$' ;then
if [ "$RETRY_AFTER" -gt 61 ] ;then
log "Too big Retry-After header field value: $RETRY_AFTER"
RETRY_AFTER=61
fi
[[ "$RETRY_AFTER" -eq 1 ]] && pluriel="" || pluriel="s"
log "sleeping $RETRY_AFTER second$pluriel"
sleep $RETRY_AFTER
else
log "Could not retrieve expected Retry-After header field value: $RETRY_AFTER"
sleep 1
fi
}
server_overload() {
if check_http_status 503 && check_acme_error rateLimited ;then
log "busy server rate limit condition"
sleep_retryafter
return 0
else
return 1
fi
}
server_request() {
dbgmsg "server_request: $1 $2"
if [ "$USE_WGET" != yes ] ;then
if [ -n "$2" ] ;then
curl_loop "$1" $CURLEXTRAFLAG -s $IPV_OPTION -A "$USER_AGENT" -D "$RESP_HEADER" -o "$RESP_BODY" -H "Content-type: application/jose+json" -d "$2" "$1"
else
curl_loop "$1" $CURLEXTRAFLAG -s $IPV_OPTION -A "$USER_AGENT" -D "$RESP_HEADER" -o "$RESP_BODY" "$1"
fi
else
if [ -n "$2" ] ;then
wget $WGETEXTRAFLAG -q $IPV_OPTION -U "$USER_AGENT" --retry-connrefused --save-headers $WGETCOEFLAG -O "$RESP_HEABOD" --header="Content-type: application/jose+json" --post-data="$2" "$1" > "$WGET_OUT" 2>& 1
else
wget $WGETEXTRAFLAG -q $IPV_OPTION -U "$USER_AGENT" --retry-connrefused --save-headers $WGETCOEFLAG -O "$RESP_HEABOD" "$1" > "$WGET_OUT" 2>& 1
fi
handle_wget_exit $? "$1"
fi
fetch_http_status
}
request_acme_server() {
while : ;do
server_request "$1" "$2"
extract_nonce
server_overload || return
done
}
# generate the PROTECTED variable, which contains a nonce retrieved from the
# server in the Replay-Nonce header
gen_protected(){
if [ -z "$NONCE" ]; then
dbgmsg "fetch new nonce"
send_get_req "$NEWNONCEURL"
[ -n "$NONCE" ] || err_exit "could not fetch new nonce"
fi
printf '%s' '{"alg":"RS256",'"$ACCOUNT_ID"',"nonce":"'"$NONCE"'","url":"'"$1"'"}'
}
# generate the signature for the request
gen_signature() {
printf '%s' "$1" |
openssl dgst -sha256 -binary -sign "$ACCOUNT_KEY" 2> "$OPENSSL_ERR"
handle_openssl_exit "$?" "signing request"
}
# helper functions to create the json web key object
key_get_modulus(){
openssl rsa -in "$1" -modulus -noout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
handle_openssl_exit $? "extracting account key modulus"
sed -e 's/^Modulus=//' < "$OPENSSL_OUT" \
| hex2bin \
| base64url
}
key_get_exponent(){
openssl rsa -in "$1" -text -noout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
handle_openssl_exit $? "extracting account key exponent"
sed -e '/^publicExponent: / !d; s/^publicExponent: [0-9]* \{1,\}(\(.*\)).*$/\1/;s/^0x\([0-9a-fA-F]\)\(\([0-9a-fA-F][0-9a-fA-F]\)*\)$/0x0\1\2/;s/^0x\(\([0-9a-fA-F][0-9a-fA-F]\)*\)$/\1/' \
< "$OPENSSL_OUT" \
| hex2bin \
| base64url
}
# make a request to the specified URI
# the payload is signed by the ACCOUNT_KEY
# the response header is stored in the file $RESP_HEADER, the body in the file $RESP_BODY
send_req_no_kid(){
URI="$1"
PAYLOAD="`printf '%s' "$2" | base64url`"
while : ;do
PROTECTED="`gen_protected "$URI" | base64url`"
SIGNATURE="`gen_signature $PROTECTED.$PAYLOAD | base64url`"
DATA='{"protected":"'"$PROTECTED"'","payload":"'"$PAYLOAD"'","signature":"'"$SIGNATURE"'"}'
# Use only once a nonce
NONCE=""
request_acme_server "$URI" "$DATA"
if ! check_http_status 400; then
return
elif ! check_acme_error badNonce ;then
return
fi
if [ -z "$BAD_NONCE_MSG" ] ;then
BAD_NONCE_MSG=yes
echo "badNonce warning: other than extrem load on the ACME server," >& 2
echo "this is mostly due to multiple client egress IP addresses," >& 2
echo "including working IPv4 and IPv6 addresses on dual family systems." >& 2
echo "In that case as a workaround please try to restrict the egress" >& 2
echo "IP address with the -4 or -6 command line option on the script." >& 2
echo "This message is just a warning, continuing safely." >& 2
fi
# Bad nonce condition. Here we do not sleep to be nice, just loop immediately.
# The error cannot be on the client side, since it is guaranted that we used the latest available nonce.
done
}
send_req(){
URI="$1"
[ -z "$KID" ] && register_account_key retrieve_kid
send_req_no_kid "$1" "$2"
}
send_get_req(){
request_acme_server "$1"
}
pwncheck(){
server_request "https://v1.pwnedkeys.com/$1"
if check_http_status 404; then
log "pwnedkeys.com claims: $2 is not compromised"
return 0
elif check_http_status 200; then
echo "pwnedkeys.com claims: $2 is compromised, fingerprint: $1" >& 2
return 1
fi
unhandled_response "pwncheck"
}
pkey_hex_digest(){
openssl dgst -sha256 -hex "$1" > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
handle_openssl_exit $? "public key DER hexdigest"
sed -e 's/^.*= *//' "$OPENSSL_OUT"
}
pwnedkey_req_check(){
[ "$PWNEDKEY_CHECK" = no ] && return
openssl req -in "$1" -noout -pubkey > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
handle_openssl_exit $? "extracting request public key"
cp "$OPENSSL_OUT" "$OPENSSL_IN"
if ! openssl pkey -in "$OPENSSL_IN" -pubin -outform der -pubout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR" ;then
# On old openssl there is no EC key. There we default to RSA.
openssl rsa -in "$OPENSSL_IN" -pubin -outform der -pubout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
fi
handle_openssl_exit $? "request public key to DER"
cp "$OPENSSL_OUT" "$OPENSSL_IN"
pwncheck "`pkey_hex_digest "$OPENSSL_IN"`" "$2"
}
pwnedkey_key_check(){
[ "$PWNEDKEY_CHECK" = no ] && return
if ! openssl ec -in "$1" -outform der -pubout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR" ;then
openssl rsa -in "$1" -outform der -pubout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
fi
handle_openssl_exit $? "public key to DER"
cp "$OPENSSL_OUT" "$OPENSSL_IN"
pwncheck "`pkey_hex_digest "$OPENSSL_IN"`" "$2"
}
# account key handling
load_account_key(){
[ -n "$ACCOUNT_KEY" ] || err_exit "no account key specified"
[ -r "$ACCOUNT_KEY" ] || err_exit "could not read account key"
openssl rsa -in "$ACCOUNT_KEY" -noout > "$OPENSSL_OUT" 2> "$OPENSSL_ERR"
handle_openssl_exit $? "opening account key"
ACCOUNT_JWK='{"e":"'"`key_get_exponent $ACCOUNT_KEY`"'","kty":"RSA","n":"'"`key_get_modulus $ACCOUNT_KEY`"'"}'
ACCOUNT_ID='"jwk":'"$ACCOUNT_JWK"
ACCOUNT_THUMB="`printf '%s' "$ACCOUNT_JWK" | openssl dgst -sha256 -binary | base64url`"
if [ -z "$1" ] ;then
if [ "$ACCOUNT_KEY" = "$SERVER_KEY" ] ;then
# We should allow revoking with compromised certificate key too
pwnedkey_key_check "$ACCOUNT_KEY" "server key as account key" || log "revoking certificate with compromised key"
else
pwnedkey_key_check "$ACCOUNT_KEY" "account key" || exit
fi
fi
}
get_one_url(){
if ! egrep -s -q -e '"'"$1"'"' "$RESP_BODY" ;then
cat "$RESP_BODY" >& 2
err_exit "Cannot retrieve URL for $1 ACME protocol function from the directory $CADIR" 1
fi
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/.*"'"$1"'":"\([^"]*\)".*/\1/'
}
get_urls(){
if [ "$USE_WGET" = yes ] ;then
WGETCOEFLAG='--content-on-error'
wget --help | egrep -s -q -e "$WGETCOEFLAG" || WGETCOEFLAG=''
fi
send_get_req "$CADIR"
if ! check_http_status 200 ;then
unhandled_response "fetching directory URLs"
fi
NEWACCOUNTURL="`get_one_url newAccount`"
REVOKECERTURL="`get_one_url revokeCert`"
KEYCHANGEURL="`get_one_url keyChange`"
NEWNONCEURL="`get_one_url newNonce`"
NEWORDERURL="`get_one_url newOrder`"
}
orders_url() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e '/"orders":"/ !d; s/.*"orders":"\([^"]*\)".*/\1/'
}
orders_list() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/^.*"orders":\[\([^]]*\)\].*$/\1/' | tr -d '"' | tr ',' ' '
}
register_account_key(){
[ -n "$NEWACCOUNTURL" ] || get_urls
if [ -n "$ACCOUNT_EMAIL" ] ;then
NEW_REG='{"termsOfServiceAgreed":true,"contact":["mailto:'"$ACCOUNT_EMAIL"'"]}'
else
NEW_REG='{"onlyReturnExisting":true}'
fi
send_req_no_kid "$NEWACCOUNTURL" "$NEW_REG"
if check_http_status 200; then
[ "$1" = "retrieve_kid" ] || err_exit "account already registered"
KID="`fetch_location`"
ACCOUNT_ID='"kid":"'"$KID"'"'
ORDERS_URL="`orders_url`"
return
elif check_http_status 201; then
KID="`fetch_location`"
ACCOUNT_ID='"kid":"'"$KID"'"'
ORDERS_URL="`orders_url`"
return
elif check_http_status 409; then
[ "$1" = "nodie" ] || err_exit "account already exists"
elif check_http_status 400 && check_acme_error accountDoesNotExist ;then
show_error "fetching account information"
exit 1
else
unhandled_response "registering account"
fi
}
clrpenda() {
ORDERS_LIST=""
while [ -n "$ORDERS_URL" ]; do
send_req "$ORDERS_URL" ""
if check_http_status 200; then
ORDERS_LIST="$ORDERS_LIST `orders_list`"
else
unhandled_response "retrieving orders list"
fi
ORDERS_URL="`fetch_next_link`"
done
DOMAIN_AUTHZ_LIST=""
set -- $ORDERS_LIST
for ORDER do
send_req "$ORDER" ""
if check_http_status 200; then
ORDER_STATUS="`order_status`"
if [ "$ORDER_STATUS" = pending ] ;then
DOMAIN_AUTHZ_LIST="$DOMAIN_AUTHZ_LIST `domain_authz_list`"
fi
else
unhandled_response "retrieving order"
fi
done
# All domain should have that challenge type, even wildcard one
CHALLENGE_TYPE=dns-01
set -- $DOMAIN_AUTHZ_LIST
for DOMAIN_AUTHZ do
send_req "$DOMAIN_AUTHZ" ""
if check_http_status 200; then
DOMAIN="`authz_domain`"
AUTHZ_STATUS="`authz_status`"
if [ "$AUTHZ_STATUS" = pending ] ;then
DOMAIN_URI="`authz_domain_uri`"
log "retrieve challenge for $DOMAIN"
request_domain_verification
fi
else
unhandled_response "retrieve challenge for URL: $DOMAIN_AUTHZ"
fi
done
}
delete_account_key(){
log "delete account"
REG='{"resource":"reg","delete":"true"}'
send_req "$REGISTRATION_URI" "$REG"
if check_http_status 200; then
return
else
unhandled_response "deleting account"
fi
}
check_server_domain() {
if [ "$2" = true ] ;then
SERVER_DOMAIN="*.$1"
else
SERVER_DOMAIN="$1"
fi
SERVER_DOMAIN_LOWER="`tolower $SERVER_DOMAIN`"
set -- $DOMAINS
for REQ_DOMAIN do
if [ "$SERVER_DOMAIN_LOWER" = "`tolower $REQ_DOMAIN`" ] ;then
return
fi
done
err_exit "ACME server requested authorization for a rogue domain: $SERVER_DOMAIN" 1
}
authz_status() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/.*"status":"\([^"]*\)".*/\1/'
}
authz_domain() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/.*"identifier":{"type":"dns","value":"\([^"]*\)"}.*/\1/'
}
wildcard_domain() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e '/"wildcard":/ !d; s/^.*"wildcard":\([a-z]*\).*$/\1/'
}
authz_domain_token() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/.*{\([^}]*"type":"'"$CHALLENGE_TYPE"'"[^}]*\)}.*/\1/; s/.*"token":"\([^"]*\)".*/\1/'
}
authz_domain_uri() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/.*{\([^}]*"type":"'"$CHALLENGE_TYPE"'"[^}]*\)}.*/\1/; s/.*"url":"\([^"]*\)".*/\1/'
}
request_challenge_domain(){
send_req "$DOMAIN_AUTHZ" ""
if check_http_status 200; then
DOMAIN="`authz_domain`"
AUTHZ_STATUS="`authz_status`"
case "$AUTHZ_STATUS" in
valid)
log "authorization is valid for $DOMAIN"
;;
pending)
check_server_domain "$DOMAIN" "`wildcard_domain`"
DOMAIN_TOKEN="`authz_domain_token`"
DOMAIN_URI="`authz_domain_uri`"
DOMAIN_DATA="$DOMAIN_DATA $DOMAIN $DOMAIN_URI $DOMAIN_TOKEN $DOMAIN_AUTHZ"
log "retrieve challenge for $DOMAIN"
;;
*)
echo authorization status: "$AUTHZ_STATUS" >& 2
unhandled_response "checking authorization status for domain $DOMAIN"
;;
esac
elif check_http_status 400; then
# account not registred?
show_error "retrieve challenge for URL: $DOMAIN_AUTHZ"
exit 1
elif check_http_status 403; then
# account not registred?
show_error "retrieve challenge for URL: $DOMAIN_AUTHZ"
exit 1
else
unhandled_response "retrieve challenge for URL: $DOMAIN_AUTHZ"
fi
}
domain_authz_list() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/^.*"authorizations":\[\([^]]*\)\].*$/\1/' | tr -d '"' | tr ',' ' '
}
finalize() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/^.*"finalize":"\([^"]*\).*$/\1/'
}
request_challenge(){
log "creating new order"
set -- $DOMAINS
for DOMAIN do
[ -n "$DOMAIN_ORDERS" ] && DOMAIN_ORDERS="$DOMAIN_ORDERS,"
DOMAIN_ORDERS="$DOMAIN_ORDERS"'{"type":"dns","value":"'"$DOMAIN"'"}'
done
[ -n "$NEWORDERURL" ] || get_urls
NEW_ORDER='{"identifiers":['"$DOMAIN_ORDERS"']}'
send_req "$NEWORDERURL" "$NEW_ORDER"
if check_http_status 201; then
DOMAIN_AUTHZ_LIST="`domain_authz_list`"
FINALIZE="`finalize`"
CURRENT_ORDER="`fetch_location`"
else
unhandled_response "requesting new order for $DOMAINS"
fi
set -- $DOMAIN_AUTHZ_LIST
for DOMAIN_AUTHZ do
request_challenge_domain
done
}
domain_commit() {
if [ -n "$PUSH_TOKEN" ] && [ -n "$PUSH_TOKEN_COMMIT" ]; then
log "calling $PUSH_TOKEN commit"
$PUSH_TOKEN commit || err_exit "$PUSH_TOKEN could not commit"
# We cannot know how long the execution of an external command will take.
# Safer to force fetching a new nonce to avoid fatal badNonce error due to nonce validity timeout.
NONCE=""
fi
}
domain_dns_challenge() {
DNS_CHALLENGE="`printf '%s' "$DOMAIN_TOKEN.$ACCOUNT_THUMB" | openssl dgst -sha256 -binary | base64url`"
if [ -n "$PUSH_TOKEN" ]; then
$PUSH_TOKEN "$1" _acme-challenge."$DOMAIN" "$DNS_CHALLENGE" || err_exit "Could not $1 $CHALLENGE_TYPE type challenge token with value $DNS_CHALLENGE for domain $DOMAIN via $PUSH_TOKEN"
else
printf 'update %s _acme-challenge.%s. 300 IN TXT "%s"\n\n' "$1" "$DOMAIN" "$DNS_CHALLENGE" |
nsupdate || err_exit "Could not $1 $CHALLENGE_TYPE type challenge token with value $DNS_CHALLENGE for domain $DOMAIN via nsupdate"
fi
}
push_domain_response() {
log "push response for $DOMAIN"
# do something with DOMAIN, DOMAIN_TOKEN and DOMAIN_RESPONSE
# echo "$DOMAIN_RESPONSE" > "/writeable/location/$DOMAIN/$DOMAIN_TOKEN"
if [ "$CHALLENGE_TYPE" = "http-01" ]; then
if [ -n "$WEBDIR" ]; then
TOKEN_DIR="`printf "%s" $WEBDIR | sed -e 's/\$DOMAIN/'"$DOMAIN"'/g; s/${DOMAIN}/'"$DOMAIN"'/g'`"
SAVED_UMASK="`umask`"
umask 0022
printf "%s\n" "$DOMAIN_TOKEN.$ACCOUNT_THUMB" > "$TOKEN_DIR/$DOMAIN_TOKEN" || exit 1
umask "$SAVED_UMASK"
elif [ -n "$PUSH_TOKEN" ]; then
$PUSH_TOKEN install "$DOMAIN" "$DOMAIN_TOKEN" "$ACCOUNT_THUMB" || err_exit "could not install token for $DOMAIN"
fi
elif [ "$CHALLENGE_TYPE" = "dns-01" ]; then
domain_dns_challenge "add"
else
echo "unsupported challenge type for install token: $CHALLENGE_TYPE" >& 2; exit 1
fi
return
}
remove_domain_response() {
log "remove response for $DOMAIN"
# do something with DOMAIN and DOMAIN_TOKEN
# rm "/writeable/location/$DOMAIN/$DOMAIN_TOKEN"
if [ "$CHALLENGE_TYPE" = "http-01" ]; then
if [ -n "$WEBDIR" ]; then
TOKEN_DIR="`printf "%s" $WEBDIR | sed -e 's/\$DOMAIN/'"$DOMAIN"'/g; s/${DOMAIN}/'"$DOMAIN"'/g'`"
rm -f "$TOKEN_DIR/$DOMAIN_TOKEN"
elif [ -n "$PUSH_TOKEN" ]; then
$PUSH_TOKEN remove "$DOMAIN" "$DOMAIN_TOKEN" "$ACCOUNT_THUMB" || exit 1
fi
elif [ "$CHALLENGE_TYPE" = "dns-01" ]; then
domain_dns_challenge "delete"
else
echo "unsupported challenge type for remove token: $CHALLENGE_TYPE" >& 2; exit 1
fi
return
}
push_response() {
set -- $DOMAIN_DATA
while [ -n "$1" ]; do
DOMAIN="$1"
DOMAIN_URI="$2"
DOMAIN_TOKEN="$3"
DOMAIN_AUTHZ="$4"
shift 4
push_domain_response
done
domain_commit
}
request_domain_verification() {
log request verification of $DOMAIN
send_req $DOMAIN_URI '{}'
dbgmsg "Retry-After value in request_domain_verification: `retry_after`"
if check_http_status 200; then
return
else
unhandled_response "requesting verification of challenge of $DOMAIN"
fi
}
request_verification() {
set -- $DOMAIN_DATA
while [ -n "$1" ]; do
DOMAIN="$1"
DOMAIN_URI="$2"
DOMAIN_TOKEN="$3"
DOMAIN_AUTHZ="$4"
shift 4
request_domain_verification
done
}
domain_status() {
tr -d ' \r\n' < "$RESP_BODY" | sed -e 's/.*"type":"'"$CHALLENGE_TYPE"'",[^{}]*"status":"\([^"]*\)".*/\1/'
}
check_verification() {
ALL_VALID=true
while [ -n "$DOMAIN_DATA" ]; do
sleep 1
set -- $DOMAIN_DATA
DOMAIN_DATA=""
while [ -n "$1" ]; do
DOMAIN="$1"
DOMAIN_URI="$2"
DOMAIN_TOKEN="$3"
DOMAIN_AUTHZ="$4"
shift 4
log check verification of $DOMAIN
send_req "$DOMAIN_AUTHZ" ""
dbgmsg "Retry-After value in check_verification: `retry_after`"
if check_http_status 200; then
DOMAIN_STATUS="`domain_status`"
case "$DOMAIN_STATUS" in
valid)
log $DOMAIN is valid
remove_domain_response
;;
invalid)
echo $DOMAIN: invalid >& 2
show_error
remove_domain_response
ALL_VALID=false
;;
pending)
log $DOMAIN is pending
DOMAIN_DATA="$DOMAIN_DATA $DOMAIN $DOMAIN_URI $DOMAIN_TOKEN $DOMAIN_AUTHZ"
;;
*)
unhandled_response "checking verification status of $DOMAIN: $DOMAIN_STATUS"
;;
esac
else
unhandled_response "checking verification status of $DOMAIN"
fi
done
done
domain_commit
$ALL_VALID || exit 1
log checking order
while : ;do
send_req "$CURRENT_ORDER" ""
if check_http_status 200; then
ORDER_STATUS="`order_status`"
case "$ORDER_STATUS" in
ready)
log order is ready
break
;;
pending)
echo order: "$ORDER_STATUS" >& 2
sleep 1
continue
;;
*)
unhandled_response "checking verification status of order"
;;
esac
else
unhandled_response "requesting order status verification"
fi
done
}
# this function generates the csr from the private server key and list of domains
gen_csr_with_private_key() {
log generate certificate request
set -- $DOMAINS
FIRST_DOM="$1"
validate_domain "$FIRST_DOM" || err_exit "invalid domain: $FIRST_DOM"
ALT_NAME="subjectAltName=DNS:$1"
shift