| 1 | package Net::ACME2::HTTP; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Net::ACME2::HTTP - transport logic for C<Net::ACME2>. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | my $resp = Net::ACME2::HTTP->new()->get('https://url/to/endpoint'); | ||
| 12 | |||
| 13 | my $http_authn = Net::ACME2::HTTP->new( key => $account_key ); | ||
| 14 | |||
| 15 | my $post_resp = $http_authn->post( | ||
| 16 | 'https://url/to/endpoint', | ||
| 17 | { foo => 4 }, #i.e., the payload to send | ||
| 18 | %opts, #cf. HTTP::Tiny::request | ||
| 19 | ); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This module handles communication with an ACME server at the HTTP level. | ||
| 24 | It handles the wrapping of POSTs in JWSes (JSON Web Signatures). | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | |||
| 31 | use Crypt::Perl::PK (); | ||
| 32 | use JSON (); | ||
| 33 | |||
| 34 | use Net::ACME2::Error (); | ||
| 35 | use Net::ACME2::HTTP_Tiny (); | ||
| 36 | use Net::ACME2::HTTP::Response (); | ||
| 37 | use Net::ACME2::X (); | ||
| 38 | |||
| 39 | use constant _CONTENT_TYPE => 'application/jose+json'; | ||
| 40 | |||
| 41 | #accessed from tests | ||
| 42 | our $_NONCE_HEADER = 'replay-nonce'; | ||
| 43 | |||
| 44 | #Used in testing | ||
| 45 | our $verify_SSL = 1; | ||
| 46 | |||
| 47 | #NB: “key” isn’t needed if we’re just doing GETs. | ||
| 48 | sub new { | ||
| 49 | my ( $class, %opts ) = @_; | ||
| 50 | |||
| 51 | die 'need “key”!' if !$opts{'key'}; | ||
| 52 | |||
| 53 | my $ua = Net::ACME2::HTTP_Tiny->new( verify_SSL => $verify_SSL ); | ||
| 54 | |||
| 55 | my $self = bless { | ||
| 56 | _ua => $ua, | ||
| 57 | _acme_key => $opts{'key'}, | ||
| 58 | _key_id => $opts{'key_id'}, | ||
| 59 | _jws_format => $opts{'jws_format'}, | ||
| 60 | }, $class; | ||
| 61 | |||
| 62 | return bless $self, $class; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub set_key_id { | ||
| 66 | my ($self, $key_id) = @_; | ||
| 67 | |||
| 68 | $self->{'_key_id'} = $key_id; | ||
| 69 | |||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub set_new_nonce_url { | ||
| 74 | my ($self, $url) = @_; | ||
| 75 | |||
| 76 | $self->{'_nonce_url'} = $url; | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | #GETs submit no data and thus are not signed. | ||
| 82 | sub get { | ||
| 83 | my ( $self, $url ) = @_; | ||
| 84 | |||
| 85 | return $self->_request( 'GET', $url ); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub post_full_jwt { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | return $self->_post( 'create_full_jws', @_ ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub post_key_id { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | return $self->_post( | ||
| 98 | 'create_key_id_jws', | ||
| 99 | @_, | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | #---------------------------------------------------------------------- | ||
| 104 | |||
| 105 | #POSTs are signed. | ||
| 106 | sub _post { | ||
| 107 | my ( $self, $jwt_method, $url, $data, $opts_hr ) = @_; | ||
| 108 | |||
| 109 | die "Constructor needed “key” to do POST! ($url)" if !$self->{'_acme_key'}; | ||
| 110 | |||
| 111 | my $jws = $self->_create_jwt( $jwt_method, $url, $data ); | ||
| 112 | |||
| 113 | local $opts_hr->{'headers'}{'Content-Type'} = 'application/jose+json'; | ||
| 114 | |||
| 115 | return $self->_request_and_set_last_nonce( | ||
| 116 | 'POST', | ||
| 117 | $url, | ||
| 118 | { | ||
| 119 | content => $jws, | ||
| 120 | headers => { | ||
| 121 | 'content-type' => _CONTENT_TYPE, | ||
| 122 | }, | ||
| 123 | }, | ||
| 124 | $opts_hr || (), | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _ua_request { | ||
| 129 | my ( $self, $type, @args ) = @_; | ||
| 130 | |||
| 131 | return $self->{'_ua'}->request( $type, @args ); | ||
| 132 | } | ||
| 133 | |||
| 134 | #overridden in tests | ||
| 135 | sub _request { | ||
| 136 | my ( $self, $type, @args ) = @_; | ||
| 137 | |||
| 138 | my $resp; | ||
| 139 | |||
| 140 | #cf. eval_bug.readme | ||
| 141 | my $eval_err = $@; | ||
| 142 | |||
| 143 | eval { $resp = $self->_ua_request( $type, @args ); }; | ||
| 144 | |||
| 145 | if (ref $@) { | ||
| 146 | my $exc = $@; | ||
| 147 | |||
| 148 | if ( eval { $exc->isa('Net::ACME2::X::HTTP::Protocol') } ) { | ||
| 149 | my $_nonce_header_lc = $_NONCE_HEADER; | ||
| 150 | $_nonce_header_lc =~ tr<A-Z><a-z>; | ||
| 151 | |||
| 152 | my $nonce = $exc->get('headers')->{$_nonce_header_lc}; | ||
| 153 | $self->{'_last_nonce'} = $nonce if $nonce; | ||
| 154 | |||
| 155 | #If the exception is able to be made into a Net::ACME2::Error, | ||
| 156 | #then do so to get a nicer error message. | ||
| 157 | my $acme_error = eval { | ||
| 158 | Net::ACME2::Error->new( | ||
| 159 | %{ JSON::decode_json( $exc->get('content') ) }, | ||
| 160 | ); | ||
| 161 | }; | ||
| 162 | |||
| 163 | if ($acme_error) { | ||
| 164 | die Net::ACME2::X->create( | ||
| 165 | 'ACME', | ||
| 166 | { | ||
| 167 | http => $exc, | ||
| 168 | acme => $acme_error, | ||
| 169 | }, | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | $@ = $exc; | ||
| 175 | die; | ||
| 176 | } | ||
| 177 | |||
| 178 | $@ = $eval_err; | ||
| 179 | |||
| 180 | return Net::ACME2::HTTP::Response->new($resp); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub _request_and_set_last_nonce { | ||
| 184 | my ( $self, $type, $url, @args ) = @_; | ||
| 185 | |||
| 186 | my $resp = $self->_request( $type, $url, @args ); | ||
| 187 | |||
| 188 | #NB: ACME’s replay protection works thus: | ||
| 189 | # - each server response includes a nonce | ||
| 190 | # - each request must include ONE of the nonces that have been sent | ||
| 191 | # - once used, a nonce can’t be reused | ||
| 192 | # | ||
| 193 | #This is subtly different from what was originally in mind (i.e., that | ||
| 194 | #each request must use the most recently sent nonce). It implies that GETs | ||
| 195 | #do not need to send nonces, though each GET will *receive* a nonce that | ||
| 196 | #may be used. | ||
| 197 | if ( my $nonce = $resp->header($_NONCE_HEADER) ) { | ||
| 198 | $self->{'_last_nonce'} = $nonce; | ||
| 199 | } | ||
| 200 | else { | ||
| 201 | warn "Received no $_NONCE_HEADER from $url!"; | ||
| 202 | } | ||
| 203 | |||
| 204 | return $resp; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub _get_first_nonce { | ||
| 208 | my ($self) = @_; | ||
| 209 | |||
| 210 | my $url = $self->{'_nonce_url'} or die "Need newNonce URL!"; | ||
| 211 | |||
| 212 | $self->_request_and_set_last_nonce( 'HEAD', $url ); | ||
| 213 | |||
| 214 | if (!$self->{'_nonce_url'}) { | ||
| 215 | die "Didn’t get a new nonce from “$self->{'_nonce_url'}”!"; | ||
| 216 | } | ||
| 217 | |||
| 218 | return; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub _create_jwt { | ||
| 222 | my ( $self, $jwt_method, $url, $data ) = @_; | ||
| 223 | |||
| 224 | $self->_get_first_nonce() if !$self->{'_last_nonce'}; | ||
| 225 | |||
| 226 | $self->{'_jwt_maker'} ||= do { | ||
| 227 | my $class; | ||
| 228 | |||
| 229 | if ($self->{'_acme_key'}->isa('Crypt::Perl::RSA::PrivateKey')) { | ||
| 230 | $class = 'Net::ACME2::JWTMaker::RSA'; | ||
| 231 | } | ||
| 232 | elsif ($self->{'_acme_key'}->isa('Crypt::Perl::ECDSA::PrivateKey')) { | ||
| 233 | $class = 'Net::ACME2::JWTMaker::ECC'; | ||
| 234 | } | ||
| 235 | else { | ||
| 236 | die "Unknown key “$self->{'_acme_key'}”"; | ||
| 237 | } | ||
| 238 | |||
| 239 | if (!$class->can('new')) { | ||
| 240 | require Module::Load; | ||
| 241 | Module::Load::load($class); | ||
| 242 | } | ||
| 243 | |||
| 244 | $class->new( | ||
| 245 | key => $self->{'_acme_key'}, | ||
| 246 | format => $self->{'_jws_format'}, | ||
| 247 | ); | ||
| 248 | }; | ||
| 249 | |||
| 250 | return $self->{'_jwt_maker'}->$jwt_method( | ||
| 251 | key_id => $self->{'_key_id'}, | ||
| 252 | payload => $data, | ||
| 253 | extra_headers => { | ||
| 254 | nonce => $self->{'_last_nonce'}, | ||
| 255 | url => $url, | ||
| 256 | }, | ||
| 257 | ); | ||
| 258 | } | ||
| 259 | |||
| 260 | 1; |