From caa385652073534bb61169636ca5bc022faa9035 Mon Sep 17 00:00:00 2001 From: Jacob Bachmeyer Date: Mon, 26 Jun 2023 19:00:19 -0500 Subject: [PATCH] Move authentication and authorization checks to packet objects --- gatekeeper.pl | 105 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 36 deletions(-) diff --git a/gatekeeper.pl b/gatekeeper.pl index 816277e..ab485e9 100755 --- a/gatekeeper.pl +++ b/gatekeeper.pl @@ -1651,7 +1651,10 @@ sub find_package { sub target_filename; sub target_filepair; # always FILE, FILE.sig, in that order + sub auth_keyrings; + sub parse; + sub auth_check; } { @@ -1692,13 +1695,75 @@ sub find_package { return $filename, $filename.'.sig'; } + sub auth_keyrings { + my $self = shift; + + # Shortcut: do we already have a keyring list? + return @{$self->{auth_keyrings}} if $self->{auth_keyrings}; + + # Check that we have a keyring for this package: + my @keyrings = ::directory_keyrings($self->target_directory); + unless (@keyrings) { + my $package = ::directory_package_name($self->target_directory); + throw package_configuration => package_name => $package, + summary => "no keyring for package $package" + } + + # Store the keyring list in the packet object + $self->{auth_keyrings} = \@keyrings; + + return @keyrings; + } + + sub auth_check { + my $self = shift; + + my $dsig_info = $self->{auth_directive_signature_info} = + ::verify_clearsigned_message($self->{directive_text}, $self->auth_keyrings); + + throw signature_error => sig_info => $dsig_info, + summary => "gpg verify of directive file failed" + if $dsig_info->{exitcode} != 0 || defined $dsig_info->{TILT}; + throw signature_error => sig_info => $dsig_info, + summary => "gpg verification problem: could not extract timestamp" + unless defined $dsig_info->{sig_creation}; + ::check_signature_timestamp(directive => $dsig_info->{sig_creation}); + + ::check_replay($self->{oplist}, $dsig_info->{sig_creation}); + } + } { package Local::Packet::Directive::Upload; {our @ISA = qw(Local::Packet::Directive)} + BEGIN { *throw = \&::throw } + sub has_uploaded_file { return 1 } + + sub auth_check { + my $self = shift; + + # first check the directive itself + $self->SUPER::auth_check; + + # now check the detached signature on the uploaded file + my $fsig_info = $self->{auth_file_signature_info} = + ::verify_detached_signature + (map(File::Spec->catfile(::CONF_DIR_Scratch, $_), + $self->target_filepair), + $self->auth_keyrings); + + throw signature_error => sig_info => $fsig_info, + summary => + 'gpg verify of upload file ('.$self->target_filename.') failed' + if $fsig_info->{exitcode} != 0 || defined $fsig_info->{TILT}; + throw signature_error => sig_info => $fsig_info, + summary => "gpg verification problem: could not extract timestamp" + unless defined $fsig_info->{sig_creation}; + ::check_signature_timestamp(file => $fsig_info->{sig_creation}); + } } @@ -3214,43 +3279,11 @@ foreach my $packet (@packets) { # each list element is an array reference $Phase = 'AA'; - # Check that we have a keyring for this package: - my @keyrings = directory_keyrings($packet->target_directory); - unless (@keyrings) { - my $package = directory_package_name($packet->target_directory); - throw package_configuration => package_name => $package, - summary => "no keyring for package $package" - } - - $dsig_info = verify_clearsigned_message($directive_text, @keyrings); + $packet->auth_check; - throw signature_error => sig_info => $dsig_info, - summary => "gpg verify of directive file failed" - if $dsig_info->{exitcode} != 0 || defined $dsig_info->{TILT}; - throw signature_error => sig_info => $dsig_info, - summary => "gpg verification problem: could not extract timestamp" - unless defined $dsig_info->{sig_creation}; - check_signature_timestamp(directive => $dsig_info->{sig_creation}); - - if (find_directive_elements($directive, 'filename')) { - # There is a file associated with this upload; verify its signature now. - - $fsig_info = verify_detached_signature - (map(File::Spec->catfile(CONF_DIR_Scratch, $_), - $packet->target_filepair), - @keyrings); - - throw signature_error => sig_info => $fsig_info, - summary => - 'gpg verify of upload file ('.$packet->target_filename.') failed' - if $fsig_info->{exitcode} != 0 || defined $fsig_info->{TILT}; - throw signature_error => sig_info => $fsig_info, - summary => "gpg verification problem: could not extract timestamp" - unless defined $fsig_info->{sig_creation}; - check_signature_timestamp(file => $fsig_info->{sig_creation}); - } - - check_replay($oplist, $dsig_info->{sig_creation}); + # scaffolding to be cleaned up later + $dsig_info = $packet->{auth_directive_signature_info}; + $fsig_info = $packet->{auth_file_signature_info}; $Phase = 'VL'; -- 2.25.1