From b60db331eff0d40904d6684dd6124280f4b68e7f Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Thu, 21 Apr 2011 22:54:19 -0400 Subject: [PATCH] added Crypt::Monkeysphere::Validator->findall() --- Crypt/Monkeysphere/Validator.pm | 61 ++++++++++++++++++++++++++++ unit-tests/20.validator/10.findall.t | 41 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 unit-tests/20.validator/10.findall.t diff --git a/Crypt/Monkeysphere/Validator.pm b/Crypt/Monkeysphere/Validator.pm index 66fc638..3247f0c 100644 --- a/Crypt/Monkeysphere/Validator.pm +++ b/Crypt/Monkeysphere/Validator.pm @@ -147,6 +147,67 @@ sub lookup { return $self->_tryquery(uid => $opts{uid}, fpr => $opts{fpr}, key => $opts{key}); } +sub valid_binding { + my $self = shift; + my $uid = shift; + my $gpgkey = shift; + + my $validity = '-'; + foreach my $tryuid ($gpgkey->user_ids) { + if ($tryuid->as_string eq $uid) { + return 1 + if $tryuid->validity =~ /^[fu]$/; + } + } + return 0; +} + +=pod + +=head2 findall + +Find all keys with appropriate capabilities and valid bindings to the given uid. + +=cut + +sub findall{ + my $self=shift; + my $uid=shift; + + $self->fetch_uid($uid) if ($self->{kspolicy} eq 'always'); + + my @keys = $self->_findall($uid); + + if (scalar(@keys) == 0 and $self->{kspolicy} eq 'unlessvalid'){ + $self->fetch_uid($uid); + @keys=$self->_findall($uid); + } + + return @keys; +} + +sub _findall { + my $self=shift; + my $uid=shift; + + my @keys; + my $x = 0; + + foreach my $gpgkey ($self->{gnupg}->get_public_keys('='.$uid)) { + if ($self->valid_binding($uid, $gpgkey)) { + foreach my $subkey ($gpgkey, @{$gpgkey->subkeys()}) { + if ($self->test_capable($subkey) ) { + $self->log('verbose', "key 0x%s is capable...\n",$subkey->hex_id); + + push(@keys, $subkey); + } + } + } + } + return @keys; +} + + sub keycomp { my $self=shift; my $rsakey = shift; diff --git a/unit-tests/20.validator/10.findall.t b/unit-tests/20.validator/10.findall.t new file mode 100644 index 0000000..fc316d5 --- /dev/null +++ b/unit-tests/20.validator/10.findall.t @@ -0,0 +1,41 @@ +# -*- perl -*- +use Test::More; + +use Crypt::Monkeysphere::Validator; +use GnuPG::Interface; +use File::Temp qw(tempdir); +use Data::Dumper; + +use strict; + + +my $gpgdir = $ENV{MSTEST_GNUPGHOME}; + +unless (defined $gpgdir && -d $gpgdir){ + plan skip_all => "Preseeded GPGHOME not found"; + goto end; +} + + +my $gnupg = new GnuPG::Interface(); +$gnupg->options->hash_init(homedir=>$gpgdir); + +my $validator=new Crypt::Monkeysphere::Validator(gnupg=>$gnupg, + kspolicy=>'never', + loglevel=>'debug'); + + +plan tests =>2; + +isa_ok($validator,'Crypt::Monkeysphere::Validator'); + +my $uid='Joe Tester '; + +my @keys=$validator->findall($uid); + + + +ok(scalar @keys >= 3); + + +end: -- 2.26.2