script/verifycert.pl untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 224 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 use VOMS::Lite::PEMHelper;
4 use VOMS::Lite::CertKeyHelper qw(buildchain);
5  
6 my $cert;
7 my @CAdirs=();
8 my @CAfiles=();
9 my $quiet=0;
10 my $summary=0;
11  
12 while ($_=shift @ARGV) {
13 if ( /^(--?cert)$/ ) {
14 $cert=shift @ARGV;
15 die "$1 requires an argument" if ( ! defined $cert );
16 die "cannot open certificate file $cert" if ( ! -r $cert );
17 }
18 elsif ( /^(--?(?:ca|CA)(?:path|dir))$/ ) {
19 my $dir=shift @ARGV;
20 die "$1 requires an argument" if ( ! defined $dir );
21 die "$dir is not a directory" if ( ! -d $dir );
22 push @CAdirs, $dir;
23 }
24 elsif ( /^--?(?:ca|CA)file$/ ) {
25 my $file=shift @ARGV;
26 die "$1 requires an argument" if ( ! defined $file );
27 die "cannot open $file" if ( ! -r $file );
28 push @CAfiles, $file;
29 }
30 elsif ( /^--?q(?:uiet)?$/ ) { $quiet=1; }
31 elsif ( /^--?s(?:ummary)?$/ ) { $summary=1; }
32 else { die "Unrecognised option \"$_\"\nUsage: $0 [-q] [-s] -cert <path to cert.pem> [ -cadir /path/to/CA/dir ] [ -cafile /path/to/a/CA.pem ]"; }
33 }
34  
35 die "Usage: $0 [-q] [-s] -cert <path to cert.pem> [ -cadir /path/to/CA/dir ] [ -cafile /path/to/a/CA.pem ]" if ( ! defined $cert );
36 my @certs=VOMS::Lite::PEMHelper::readCert($cert);
37  
38 my @CAcerts=();
39 foreach (@CAfiles) { print "$_\n"; push @CAcerts, VOMS::Lite::PEMHelper::readCert($_); }
40  
41 my %Chain = %{ buildchain( { trustedCAdirs => \@CAdirs,
42 suppliedcerts => \@certs,
43 trustedCAs => \@CAcerts } ) };
44  
45 my @returnedCerts = @{ $Chain{Certs} };
46 my @IHash = @{ $Chain{IssuerHashes} };
47 my @Hash = @{ $Chain{SubjectHashes} };
48 my @SKID = @{ $Chain{SubjectKeyIdentifiers} };
49 my @AKID = @{ $Chain{AuthorityKeyIdentifiersSKIDs} };
50 my @DNs = @{ $Chain{DistinguishedNames} };
51 my @IDNs = @{ $Chain{IssuerDistinguishedNames} };
52 my @Trust = @{ $Chain{TrustedCA} };
53 my $self = $Chain{SelfSignedInChain};
54 my @GSI = @{ $Chain{GSIType} };
55 my $EECDN = $Chain{EndEntityDN};
56 my $EECIDN = $Chain{EndEntityIssuerDN};
57 my $EEC = $Chain{EndEntityCert};
58 my @LifeTime = @{ $Chain{Lifetimes} };
59 my @Errors = @{ $Chain{Errors} };
60 my $Err = 0;
61 my $Trust = 0;
62 my $comltstr;
63 my $comlifetime = $LifeTime[0];
64  
65 while ( $#returnedCerts >= 0 ) {
66 my $timeleft = shift @LifeTime;
67 my $tl = $timeleft;
68 my $y = int( $tl / 31556736 ); $tl %= 31556736;
69 my $d = int( $tl / 86400 ); $tl %= 86400;
70 my $h = int( $tl / 3600 ); $tl %= 3600;
71 my $m = int( $tl / 60 ); $tl %= 60;
72 my $s = $tl;
73 my $timeleftstr = (($timeleft>0) ? (( ($y>0) ? "$y years, " : "").
74 ( ($d>0||$y>0) ? "$d days, " : "").
75 ( ($h>0||$d>0||$y>0) ? "$h hours, " : "").
76 ( ($m>0||$h>0||$d>0||$y>0) ? "$m minutes, " : "").
77 ( ($s>0||$m>0||$h>0||$d>0||$y>0) ? "$s seconds, " : ""))
78 : "0" );
79 if ( $timeleft <= $comlifetime ) { $comlifetime = $timeleft; $comltstr=$timeleftstr; }
80 $Trust=1 if ($Trust[-1]);
81 my @certErrors=@{ shift @Errors };
82 my $errstr="";
83 if ( $#certErrors >=0 ) { $Err=1; foreach (@certErrors) { $errstr .= "ERROR: $_\n"; } }
84  
85 my $status = "DN ".(shift @DNs)."\n".
86 "Hash ".(shift @Hash)."\n".
87 "Issuer DN ".(shift @IDNs)."\n".
88 "Issuer Hash ".(shift @IHash)."\n".
89 "GSI Status ".(shift @GSI)."\n".
90 "Time remaining $timeleftstr\n".
91 "Trusted ".(((shift @Trust) == 1)?"yes\n":"no\n").
92 "$errstr".
93 VOMS::Lite::PEMHelper::encodeCert(shift @returnedCerts) . "\n";
94  
95 print $status if ( ! $summary && ! $quiet );
96 }
97  
98 if ( ! $quiet ) {
99 print "Over all remaining time: $comltstr\n";
100 print "Chain is".(( $self == 1 )?"":" NOT")." complete.\n";
101 print "Chain ".(( $Trust == 1 )?"contains":"does NOT contain")." a local trust anchor.\n";
102 print "There were errors in the validation.\n" if ( $Err == 1 );
103 }
104  
105 my $ret=0;
106 $ret+=1 if ( $Trust == 0 );
107 $ret+=2 if ( $self == 0 );
108 $ret+=4 if ( $Err == 1 );
109 exit($ret);
110  
111 __END__