| 1 | package HTTP::Message::Signature; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Verify HTTP Message Signatures (RFC 9421) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use v5.24; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Crypt::Digest; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Crypt::PK::Ed25519; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Crypt::PK::RSA; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Hash::Ordered; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use HTTP::Message 6.41; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use HTTP::StructuredField v0.0.5; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use JSON qw( decode_json ); # used by CryptX | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use List::Util 1.29 qw( any first max pairmap ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use MIME::Base64 qw( decode_base64 ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use PerlX::Maybe qw( maybe ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Ref::Util qw( is_blessed_ref is_plain_hashref ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use String::Util qw( trim unquote ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use Syntax::Keyword::Match; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Types::Common qw( Bool HashRef InstanceOf NonEmptyStr PositiveInt ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use Types::URI qw( Uri ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # RECOMMEND PREREQ: Ref::Util::XS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # RECOMMEND PREREQ: Type::Tiny::XS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | use experimental qw( lexical_subs signatures ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | our $VERSION = 'v0.2.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | has message => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => InstanceOf [qw/ HTTP::Message Plack::Request /], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | handles => [qw/ as_string headers /], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | has is_request => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Subroutine
_build_is_request: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $message = $self->message; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return $message->isa("HTTP::Request") || $message->isa("Plack::Request"); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
MC/DC0%0/2
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request')
$message->isa('Plack::Request')
MC/DC0%0/2
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request')
$message->isa('Plack::Request')
Truth table0%0/3
A || B
A= $message->isa('HTTP::Request')B= $message->isa('Plack::Request')
Truth table0%0/3
A || B
A= $message->isa('HTTP::Request')B= $message->isa('Plack::Request')
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | init_arg => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | has is_response => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 9 | ▶ | builder => sub($self) { $self->message->isa("HTTP::Response") }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_is_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | init_arg => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | has request => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | isa => InstanceOf [qw/ HTTP::Request Plack::Request /], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | predicate => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | has user_agent => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | isa => InstanceOf ['LWP::UserAgent'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_user_agent: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return LWP::UserAgent->new( max_size => 8192, timeout => 10 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | has signature_agent => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | isa => NonEmptyStr, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | coerce => sub($uri) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (is_blessed_ref($uri) && $uri->isa("URI")) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")
Ref::Util::XS::is_blessed_ref($uri)
$uri->isa("URI")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($uri)B= $uri->isa("URI")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return $uri->scheme . "://" . $uri->authority; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return "$uri"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_signature_agent: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $agent = join(",", $self->headers->header("signature-agent") // "" ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->headers->header("signature-agent") // ""
MC/DC0%0/1
$self->headers->header("signature-agent") // ""
$self->headers->header("signature-agent")
Truth table0%0/2
A
A= $self->headers->header("signature-agent")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( !$agent && $self->is_response ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $agent and $self->is_response
MC/DC0%0/2
not $agent and $self->is_response
not $agent
$self->is_response
Truth table0%0/3
not(A) and B
A= not $agentB= $self->is_response
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return $self->request->uri; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ( $agent =~ /\A [a-z\*][a-z0-1_\-\.\*]* = /ax ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my ($key) = $self->headers->header("signature-agent-key") || $self->signature_labels; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->headers->header('signature-agent-key') || $self->signature_labels
MC/DC0%0/2
$self->headers->header('signature-agent-key') || $self->signature_labels
$self->headers->header('signature-agent-key')
$self->signature_labels
Truth table0%0/3
A || B
A= $self->headers->header('signature-agent-key')B= $self->signature_labels
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $agents = HTTP::StructuredField->parse_dictionary( $agent ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $agent = $agents->get( unquote($key) )->value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | return $agent; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | has signature_agent_uri => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | isa => Uri, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | coerce => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_signature_agent_uri: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $uri = URI->new( unquote( $self->signature_agent ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | carp "signature-agent header contains a full URL" if $uri->path && $uri->path ne "/"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
$uri->path and $uri->path ne "/"
$uri->path and $uri->path ne "/"
MC/DC0%0/2
$uri->path and $uri->path ne "/"
$uri->path
$uri->path ne "/"
MC/DC0%0/2
$uri->path and $uri->path ne "/"
$uri->path
$uri->path ne "/"
Truth table0%0/3
A and B
A= $uri->pathB= $uri->path ne "/"
Truth table0%0/3
A and B
A= $uri->pathB= $uri->path ne "/"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $uri->path("/.well-known/http-message-signatures-directory"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | return $uri; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | has signature_input => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | isa => InstanceOf ['Hash::Ordered'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_signature_input: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my @input = $self->headers->header("signature-input"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 11 | return HTTP::StructuredField->parse_dictionary( join( ',', @input ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | handles => { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | signature_labels => 'keys', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | has signature_header => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | isa => InstanceOf ['Hash::Ordered'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 8 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 8 | my @input = $self->headers->header("signature"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 8 | return HTTP::StructuredField->parse_dictionary( join( ',', @input ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | has public_keys_json => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | isa => NonEmptyStr, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | state $types = { map { $_ => 1 } qw[ application/http-message-signatures-directory+json application/json ] }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $uri = $self->signature_agent_uri; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | croak "agent_uri should be an https URL" unless $uri->scheme eq "https"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | GET => $uri, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 'Accept' => join( ",", keys $types->%* ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 'User-Agent' => sprintf( 'Perl-%s-%s', __PACKAGE__, $VERSION ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 'Want-Content-Digest' => 'sha-512=10, sha-256=9', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $res = $self->user_agent->request($req); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ( $res->is_success && $types->{ $res->content_type } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$res->is_success and $$types{$res->content_type}
$res->is_success and $$types{$res->content_type}
MC/DC0%0/2
$res->is_success and $$types{$res->content_type}
$res->is_success
$$types{$res->content_type}
MC/DC0%0/2
$res->is_success and $$types{$res->content_type}
$res->is_success
$$types{$res->content_type}
Truth table0%0/3
A and B
A= $res->is_successB= $$types{$res->content_type}
Truth table0%0/3
A and B
A= $res->is_successB= $$types{$res->content_type}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $res->max_body_size( $self->max_public_keys_json_size ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ( $self->verify_public_keys ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ( $res->header("Signature-Input") && $res->header("Signature") ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->header("Signature-Input") and $res->header("Signature")
MC/DC0%0/2
$res->header("Signature-Input") and $res->header("Signature")
$res->header("Signature-Input")
$res->header("Signature")
Truth table0%0/3
A and B
A= $res->header("Signature-Input")B= $res->header("Signature")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my $check = __PACKAGE__->new( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | request => $req, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | response => $res, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | require_tag => "http-message-signatures-directory", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | public_keys_json => $res->decoded_content, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | $check->verify( { check_digest => 1 } ) or croak "signatures-directory was not signed"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | carp "signatures-directory response was unsigned"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | return $res->decoded_content; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | croak "error retrieving ${uri}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 4 | has max_public_keys_json_size => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | isa => PositiveInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | default => 16384, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | has verify_public_keys => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | has public_keys => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | isa => HashRef, # TODO make strict validation optional | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | ▶ | builder => sub($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_public_keys: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | my $pks = decode_json( $self->public_keys_json ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 2 | my $max = $self->max_keys; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | ▶ | if ( defined $pks->{keys}[$max] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | splice( $pks->{keys}->@*, $max ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | carp "public_keys truncated to the first ${max} keys"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # TODO cache keys here, by signature_agent_uri->authority and key id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | return $pks; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | has max_keys => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | isa => PositiveInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | default => 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 4 | has require_tag => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | isa => NonEmptyStr, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | predicate => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | ▶ | sub has_signature($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_signature: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 8 | my $headers = $self->headers; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 8 | ▶ | return $headers->header('signature-input') # check for this first as per RFC | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$headers->header('signature-input') && $headers->header('signature')
MC/DC0%0/2
$headers->header('signature-input') && $headers->header('signature')
$headers->header('signature-input')
$headers->header('signature')
Truth table33%1/3
A && B
A= $headers->header('signature-input')B= $headers->header('signature')
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | && $headers->header('signature'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 12 | ▶ | sub is_expired( $self, $label = undef ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_expired: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 12 | ▶ | my $input = $self->_get_input_from_label( $label // $self->_get_default_label ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$label // $self->_get_default_label
MC/DC0%0/2
$label // $self->_get_default_label
$label
$self->_get_default_label
Truth table33%1/3
A // B
A= $labelB= $self->_get_default_label
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 12 | ▶ | my $expires = $input->params->get("expires") or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 12 | return time > $expires->value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 8 | ▶ | sub verify( $self, $options = undef ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 8 | ▶ | if ( my $err = $self->check_signature($options) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | carp $err; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 8 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 8 | ▶ | sub check_signature( $self, $options = undef ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_signature: called CC=29 SCAR=45.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 8 | ▶ | return "no signature" unless $self->has_signature; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 8 | ▶ | $options //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options //= {}
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 8 | ▶ | $options = { label => $options } unless is_plain_hashref($options); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 8 | ▶ | my $label = $options->{label} //= $self->_get_default_label; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$options->{"label"} //= $self->_get_default_label
MC/DC0%0/2
$options->{"label"} //= $self->_get_default_label
$options->{"label"}
$self->_get_default_label
Truth table33%1/3
A //= B
A= $options->{"label"}B= $self->_get_default_label
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 8 | my $input = $self->_get_input_from_label($label); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 8 | ▶ | if ( $options->{check_expires} //= 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$options->{"check_expires"} //= 1
MC/DC0%0/1
$options->{"check_expires"} //= 1
$options->{"check_expires"}
Truth table50%1/2
A
A= $options->{"check_expires"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 4 | ▶ | return "signature is expired" if $self->is_expired; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 8 | ▶ | if ( $options->{check_created} //= 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$options->{"check_created"} //= 1
MC/DC0%0/1
$options->{"check_created"} //= 1
$options->{"check_created"}
Truth table50%1/2
A
A= $options->{"check_created"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 8 | my $created = $input->params->get("created"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 8 | ▶ | return "signature is postdated" if $created && $created->value > time; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$created and $created->value > time
MC/DC0%0/2
$created and $created->value > time
$created
$created->value > time
Truth table33%1/3
A and B
A= $createdB= $created->value > time
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 8 | ▶ | if ( $self->has_require_tag ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 8 | my $check = $input->params->get("tag"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 8 | my $tag = $self->require_tag; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 8 | ▶ | return "tag is not '${tag}'" unless $check && $check->value eq $tag; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$check and $check->value eq $tag
MC/DC0%0/2
$check and $check->value eq $tag
$check
$check->value eq $tag
Truth table33%1/3
A and B
A= $checkB= $check->value eq $tag
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 8 | state $algs = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | "ed25519" => { class => "Crypt::PK::Ed25519", key => { kty => "OKP", crv => "Ed25519" } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | "rsa-pss-sha512" => { class => "Crypt::PK::RSA", key => { kty => "RSA", alg => "PS512" }, hash => [qw/ SHA512 pss 64 /] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 8 | ▶ | my $alg = eval { $input->params->get("alg")->value } or croak "missing alg parameter"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 8 | ▶ | my $meta = $algs->{ $alg } or croak "unsupported algorithm"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 8 | ▶ | my $keyid = $input->params->get("keyid") or croak "no keyid was found in the signature-input"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 8 | my $kid = unquote( $keyid->value ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 8 | ▶ | my $key = $self->_get_key($kid) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | or return "keyid '${kid}' was not found in the public keys"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # TODO: add support for looking up keys from a cache based on agent URL and key id, and alg | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 8 | for my $field ( keys $meta->{key}->%* ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 16 | ▶ | $key->{$field} eq $meta->{key}{$field} or croak "keyid ${kid} is incompatible with ${alg}"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 8 | my $class = $meta->{class}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 8 | ▶ | my @params = $meta->{hash} ? $meta->{hash}->@* : (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 8 | my $pk = $class->new($key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 8 | my $sig = $self->signature_header->get($label)->value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 8 | my $base = $self->_generate_signature_base($options); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 8 | ▶ | $pk->verify_message( $sig, $base, @params ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | or return "signature failed"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 8 | ▶ | if ( $self->is_response && $options->{check_digest} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->is_response and $options->{"check_digest"}
MC/DC0%0/2
$self->is_response and $options->{"check_digest"}
$self->is_response
$options->{"check_digest"}
Truth table33%1/3
A and B
A= $self->is_responseB= $options->{"check_digest"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if (my @digests = $self->headers->header("content-digest") ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | unless ( any { $_->value eq "content-digest" } $input->value->@* ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | carp "content-digest is not part of the signature-input"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $digests = HTTP::StructuredField->parse_dictionary( join(",", @digests ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | my $alg = first { $digests->exists($_) } qw/ sha-512 sha-256 /; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | my $d = Crypt::Digest->new( uc $alg =~ s/\-//g ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | # Note: RFC 9530 the Content-Digest is of the encoded content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | $d->add( $self->message->content ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | unless ( $d->digest eq $digests->get($alg)->value ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | return "content-digest ${alg} does not match"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 8 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 8 | ▶ | sub _get_key( $self, $kid ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 8 | ▶ | return first { $_->{kid} eq $kid } $self->public_keys->{keys}->@*; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 20 | ▶ | sub _get_default_label($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_default_label: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 20 | my ($label) = $self->signature_labels; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 20 | ▶ | croak "no labels found" unless defined $label; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 20 | return $label; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 31 | ▶ | sub _get_input_from_label( $self, $label ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_input_from_label: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 31 | ▶ | my $input = $self->signature_input->get($label) or croak "signature-input label ${label} was not found"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 31 | return $input; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 11 | ▶ | sub _generate_signature_base( $self, $options ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_signature_base: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 11 | my $label = $options->{label}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 11 | my @buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 11 | my $seen = Hash::Ordered->new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 28 | ▶ | my sub _process_header_field( $field, $value ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_header_field: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | # TODO serialize line as per "component-identifier ABNF rule" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # TODO canonicalize the field value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 28 | ▶ | my $line = sprintf( '%s: %s', lc($field), $value // "" ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$value // ""
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 28 | ▶ | croak "component has already been added to the sginature base" if $seen->exists( lc $field ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 28 | $seen->set( lc $field => 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 28 | push @buffer, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 11 | my $input = $self->_get_input_from_label($label); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 11 | ▶ | croak "label ${label} is not a list" unless $input->is_inner_list; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 11 | for my $field ( $input->value->@* ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 17 | my $name = unquote($field->value, braces => 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 17 | ▶ | if ( substr($name, 0, 1) eq '@' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 11 | _process_header_field( $self->_get_derived_field( $field, $options ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 6 | my $header = $self->headers->header($name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 6 | ▶ | if ($field->has_params) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 2 | my ($key) = $field->params->get("key")->value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 2 | my $dict = HTTP::StructuredField->parse_dictionary($header); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 2 | my $param = $dict->get($key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | # TODO: improve quoting | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 2 | _process_header_field( "\"${name}\";key=\"${key}\"" => sprintf('"%s"', $param->value ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 4 | _process_header_field( "\"${name}\"" => $header ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 11 | my $header = $self->headers->header("signature-input"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 11 | ▶ | croak "unexpected signature-input" unless substr( $header, 0, length($label) + 1 ) eq "${label}="; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 11 | my $params = substr( $header, length($label) + 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 11 | _process_header_field( '"@signature-params"' => $params ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 11 | return join( "\n", @buffer ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 11 | ▶ | sub _get_derived_field( $self, $field, $options ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_derived_field: called CC=5 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 11 | my $name = $field->value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # Note: HTTP::Message has uri_canonical but Plack::Request does not. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | state $handlers = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | ▶ | '@status' => sub( $message, $, $ ) { $message->code }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | ▶ | '@method' => sub( $message, $, $ ) { $message->method }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | '@target-uri' => sub( $message, $, $ ) { $message->uri }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 11 | ▶ | '@authority' => sub( $message, $, $ ) { $message->uri->authority }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | ▶ | '@scheme' => sub( $message, $, $ ) { $message->uri->scheme }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | '@path' => sub( $message, $, $ ) { $message->uri->path }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | '@query' => sub( $message, $, $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my $query = $message->uri->query; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | if ( defined $query ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | return '?' . $query; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | '@query-param' => sub( $message, $, $params ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | if ( my ($key) = $params->keys ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | # Note: RFC 9421 Sec 2.2.8 says named parameters with multiple values must not be included | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | my @values = $message->uri->query_param($key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | croak "multipe query-param values for '${key}'" if @values > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | return $values[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 11 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 11 | ▶ | croak "unsupported field ${name}" unless exists $handlers->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 11 | my $quoted = "\"${name}\""; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 11 | my $message = $self->message; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 11 | my ($param, @extra) = $field->params->keys; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 11 | ▶ | if ( defined $param ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 1 | ▶ | croak "extra parameters in field ${name}" if @extra; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 1 | $quoted .= ";" . unquote($param); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 1 | ▶ | if ( $param eq "req" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 1 | $message = $self->request; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 11 | return ( maybe $quoted => $handlers->{$name}->( $message, $options, $field->params ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 13 | ▶ | sub BUILDARGS( $, @args ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 13 | ▶ | my %args = @args == 1 && is_plain_hashref( $args[0] ) ? $args[0]->%* : @args; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args == 1 and Ref::Util::XS::is_plain_hashref($args[0])
MC/DC0%0/2
@args == 1 and Ref::Util::XS::is_plain_hashref($args[0])
@args == 1
Ref::Util::XS::is_plain_hashref($args[0])
Truth table33%1/3
A and B
A= @args == 1B= Ref::Util::XS::is_plain_hashref($args[0])
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 13 | ▶ | if (my $res = $args{response}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | $args{message} = $res; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | elsif (my $req = $args{request}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 8 | $args{message} = $req; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 13 | my $message = $args{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 13 | ▶ | if ( is_plain_hashref($message) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 1 | require Plack::Request; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 1 | $args{message} = Plack::Request->new($message); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 13 | return \%args; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||