| 1 | package Net::ACME::Crypt; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | #---------------------------------------------------------------------- | ||||||||||||||
| 4 | # This module exists because of a desire to do these computations | ||||||||||||||
| 5 | # in environments where a compiler may not be available. | ||||||||||||||
| 6 | # (Otherwise, CryptX would be ideal.) | ||||||||||||||
| 7 | #---------------------------------------------------------------------- | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 14 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 14 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 14 | ▶ | use JSON (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 14 | ▶ | use MIME::Base64 (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 14 | ▶ | use Crypt::Perl::PK (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 14 | ▶ | use Net::ACME::X (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | #As per the ACME spec | ||||||||||||||
| 20 | 14 | ▶ | use constant JWK_THUMBPRINT_DIGEST => 'sha256'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 14 | ▶ | use constant JWT_RSA_SIG => 'RS256'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | 14 | *parse_key = \&Crypt::Perl::PK::parse_key; | |||||||||||||
| 25 | |||||||||||||||
| 26 | 5 | ▶ | sub get_jwk_thumbprint { | ||||||||||||
|
Subroutine
get_jwk_thumbprint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 27 | 5 | my ($jwk_hr) = @_; | |||||||||||||
| 28 | |||||||||||||||
| 29 | #We could generate the thumbprint directly from the JWK, | ||||||||||||||
| 30 | #but there’d be more code to maintain. For now the speed hit | ||||||||||||||
| 31 | #seems acceptable … ? | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 5 | my $key_obj = Crypt::Perl::PK::parse_jwk($jwk_hr); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 5 | return $key_obj->get_jwk_thumbprint(JWK_THUMBPRINT_DIGEST()); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 14 | *_encode_b64u = \&MIME::Base64::encode_base64url; | |||||||||||||
| 39 | |||||||||||||||
| 40 | #expects: | ||||||||||||||
| 41 | # key - object | ||||||||||||||
| 42 | # payload | ||||||||||||||
| 43 | # extra_headers (optional, hashref) | ||||||||||||||
| 44 | 10 | ▶ | sub create_jwt { | ||||||||||||
|
Subroutine
create_jwt: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||
| 45 | 10 | my (%args) = @_; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 10 | ▶ | if ($args{'key'}->isa('Crypt::Perl::RSA::PrivateKey')) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | 9 | return _create_rs256_jwt(%args); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | elsif ($args{'key'}->isa('Crypt::Perl::ECDSA::PrivateKey')) { | ||||||||||||||
| 51 | 1 | return _create_ecc_jwt(%args); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 0 | die "Unrecognized “key”: “$args{'key'}”"; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | #---------------------------------------------------------------------- | ||||||||||||||
| 58 | |||||||||||||||
| 59 | #Based on Crypt::JWT::encode_jwt(), but focused on this particular | ||||||||||||||
| 60 | #protocol’s needs. Note that UTF-8 might get mangled in here, | ||||||||||||||
| 61 | #but that’s not a problem since ACME shouldn’t require sending raw UTF-8. | ||||||||||||||
| 62 | #(Maybe with registration??) | ||||||||||||||
| 63 | 9 | ▶ | sub _create_rs256_jwt { | ||||||||||||
|
Subroutine
_create_rs256_jwt: called CC=1 SCAR=0.0
| |||||||||||||||
| 64 | 9 | my ( %args ) = @_; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 9 | my $alg = JWT_RSA_SIG(); | |||||||||||||
| 67 | |||||||||||||||
| 68 | 9 | my $key = $args{'key'}; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 9 | ▶ | my $signer_cr = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 9 | return $key->can("sign_$alg")->($key, @_); | |||||||||||||
| 72 | 9 | }; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 9 | return _create_jwt( | |||||||||||||
| 75 | %args, | ||||||||||||||
| 76 | alg => $alg, | ||||||||||||||
| 77 | signer_cr => $signer_cr, | ||||||||||||||
| 78 | ); | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | ▶ | sub _create_ecc_jwt { | ||||||||||||
|
Subroutine
_create_ecc_jwt: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | 1 | my (%args) = @_; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 1 | my $key = $args{'key'}; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 1 | ▶ | my $signer_cr = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 87 | 1 | return $key->sign_jwa(@_); | |||||||||||||
| 88 | 1 | }; | |||||||||||||
| 89 | |||||||||||||||
| 90 | 1 | return _create_jwt( | |||||||||||||
| 91 | %args, | ||||||||||||||
| 92 | alg => $key->get_jwa_alg(), | ||||||||||||||
| 93 | signer_cr => $signer_cr, | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 10 | ▶ | sub _create_jwt { | ||||||||||||
|
Subroutine
_create_jwt: called CC=3 SCAR=11.0
| |||||||||||||||
| 98 | 10 | my ( %args ) = @_; | |||||||||||||
| 99 | |||||||||||||||
| 100 | # key | ||||||||||||||
| 101 | 10 | ▶ | die "JWS: missing 'key'" if !$args{key}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 10 | my $payload = $args{payload}; | |||||||||||||
| 104 | 10 | my $alg = $args{'alg'}; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 10 | ▶ | my $header = $args{extra_headers} ? { %{$args{extra_headers}} } : {}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | |||||||||||||||
| 108 | # serialize payload | ||||||||||||||
| 109 | 10 | $payload = _payload_enc($payload); | |||||||||||||
| 110 | |||||||||||||||
| 111 | # encode payload | ||||||||||||||
| 112 | 10 | my $b64u_payload = _encode_b64u($payload); | |||||||||||||
| 113 | |||||||||||||||
| 114 | # prepare header | ||||||||||||||
| 115 | 10 | $header->{alg} = $alg; | |||||||||||||
| 116 | |||||||||||||||
| 117 | # encode header | ||||||||||||||
| 118 | 10 | my $json_header = _encode_json($header); | |||||||||||||
| 119 | 10 | my $b64u_header = _encode_b64u($json_header); | |||||||||||||
| 120 | |||||||||||||||
| 121 | 10 | my $signer_cr = $args{'signer_cr'}; | |||||||||||||
| 122 | |||||||||||||||
| 123 | 10 | my $b64u_signature = _encode_b64u( $signer_cr->("$b64u_header.$b64u_payload", $args{key}) ); | |||||||||||||
| 124 | |||||||||||||||
| 125 | 10 | return join('.', $b64u_header, $b64u_payload, $b64u_signature); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 10 | ▶ | sub _encode_json { | ||||||||||||
|
Subroutine
_encode_json: called CC=1 SCAR=0.0
| |||||||||||||||
| 129 | 10 | my ($payload) = @_; | |||||||||||||
| 130 | |||||||||||||||
| 131 | #Always do a canonical encode so that we can test more easily. | ||||||||||||||
| 132 | #Note that JWS itself does NOT require this. | ||||||||||||||
| 133 | 10 | return JSON->new()->canonical(1)->encode($payload); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | #Taken from Crypt::JWT | ||||||||||||||
| 137 | 10 | ▶ | sub _payload_enc { | ||||||||||||
|
Subroutine
_payload_enc: called CC=3 SCAR=12.0
| |||||||||||||||
| 138 | 10 | my ($payload) = @_; | |||||||||||||
| 139 | |||||||||||||||
| 140 | 10 | ▶ | if (ref($payload) =~ /^(?:HASH|ARRAY)$/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 0 | $payload = _encode_json($payload); | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | else { | ||||||||||||||
| 144 | 10 | ▶ | utf8::downgrade($payload, 1) or die "JWT: payload cannot contain wide character"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 10 | return $payload; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 14 | 1; | |||||||||||||