lib/VOMS/Lite/CertKeyHelper.pm

filter: Statementstmt 88.3% 204 / 231 Branchbran 46.6% 42 / 90 Conditioncond 34.9% 22 / 63 MC/DCmcdc 2.7% 1 / 36 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 7 totaltotal 64.2% 284 / 442 CC 73 SCAR 44.6
CC
73
Cov
70%
verifychain
167.5
buildchain
28.3
digestTBS
12.3
CRAP 86.1
« lib/VOMS/Lite.pm ↑ Summary lib/VOMS/Lite/PEMHelper.pm »
1 package VOMS::Lite::CertKeyHelper;
2  
3 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use VOMS::Lite::ASN1Helper qw(ASN1Index ASN1Unwrap ASN1Wrap Hex ASN1OIDtoOID);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use VOMS::Lite::RSAHelper qw(rsasign rsaverify);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use VOMS::Lite::PEMHelper qw(readCert);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use VOMS::Lite::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 @ISA = qw(Exporter);
14 1 %EXPORT_TAGS = ( );
15 1 @EXPORT_OK = qw(buildchain digestSign OIDtoDNattrib DNattribToOID);
16 1 @EXPORT = ( );
17 1 $VERSION = '0.20';
18 ##################################################
19  
20 # Define some common OIDs used in Distunguished names NB we're using UID and Email not UserID and emailAddress
21 1 my %DNOIDs=( '2.5.4.49' => 'DN',
22 '0.9.2342.19200300.100.1.1' => 'UID',
23 '0.9.2342.19200300.100.1.25' => 'DC',
24 '1.2.840.113549.1.9.1' => 'Email',
25 '2.5.4.3' => 'CN',
26 '2.5.4.4' => 'SN',
27 '2.5.4.5' => 'serialNumber',
28 '2.5.4.6' => 'C',
29 '2.5.4.7' => 'L',
30 '2.5.4.8' => 'ST',
31 '2.5.4.9' => 'street',
32 '2.5.4.12' => 'title',
33 '2.5.4.16' => 'postalAddress',
34 '2.5.4.17' => 'postalCode',
35 '2.5.4.18' => 'postOfficeBox',
36 '2.5.4.26' => 'registeredAddress',
37 '2.5.4.11' => 'OU',
38 '2.5.4.41' => 'name',
39 '2.5.4.10' => 'O',
40 '2.5.4.42' => 'givenName',
41 '2.5.4.43' => 'initials',
42 '2.5.6.3' => 'locality',
43 '2.5.6.4' => 'organization');
44  
45 1 my %DNAttribs=( reverse %DNOIDs,
46 commonName => '2.5.4.3',
47 serialNumber => '2.5.4.4',
48 countryName => '2.5.4.6',
49 localityName => '2.5.4.7',
50 stateOrProvinceName => '2.5.4.8',
51 organizationName => '2.5.4.10',
52 organizationalUnitName => '2.5.4.11',
53 emailAddress => '1.2.840.113549.1.9.1',
54 UserID => '0.9.2342.19200300.100.1.1',
55 domainComponent => '0.9.2342.19200300.100.1.25'
56 );
57  
58 30 ▶sub OIDtoDNattrib {
Subroutine OIDtoDNattrib: called CC=2 SCAR=7.6
Pod undocumented
59 30 ▶ return (defined $DNOIDs{$_[0]})?$DNOIDs{$_[0]}:$_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $DNOIDs{$_[0]} ? : 30 0 30
60 }
61  
62 9 ▶sub DNattribToOID {
Subroutine DNattribToOID: called CC=2 SCAR=7.6
Pod undocumented
63 9 ▶ return (defined $DNAttribs{$_[0]})?$DNAttribs{$_[0]}:undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $DNAttribs{$_[0]} ? : 9 0 9
64 }
65  
66 ##################################################
67  
68 1 ▶sub verifychain {
Subroutine verifychain: called CC=40 SCAR=51.2
Pod undocumented
69 1 my ($lastExp,$lastMod,$lastCAPurpose,$lastKeyCertSign,$lastkeyUsageDigitalSignature,$EECDN,$EECIDN,$EEC);
70 1 my @Self;
71 1 my @Time;
72 1 my @LifeTime;
73 1 my @Signed;
74 1 my @SignerPurposeCA;
75 1 my @SignerCertSignPurpose;
76 1 my @PathLen;
77 1 my @GSIType;
78 1 my @ProxyPathLen;
79 1 my $now=time();
80  
81 # Loop over certificate starting at 'root-most'
82 1 foreach ( reverse @_ ) {
83 2 my %CI = %{ VOMS::Lite::X509::Examine( $_ , { SubjectDN=>"",
84 IssuerDN=>"",
85 Start=>"",
86 End=>"",
87 SignatureType=>"",
88 SignatureValue=>"",
89 X509TBSCert=>"",
90 keyUsage=>"",
91 basicConstraints=>"",
92 KeypublicExponent=>"",
93 Keymodulus=>"",
94 authorityKeyIdentifier=>"",
95 subjectKeyIdentifier=>"",
96 ProxyInfo=>""
97 }) };
98  
99 # Check if selfsigned, if first in chain set 'last' values to these values
100 2 push @Self,0;
101 2 ▶ if ( $CI{IssuerDN} eq $CI{SubjectDN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CI{"IssuerDN"} eq $CI{"SubjectDN"}) 1 1 2
102 1 ▶ if ( ! defined $CI{authorityKeyIdentifierSkid} || $CI{subjectKeyIdentifier} eq $CI{authorityKeyIdentifierSkid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $CI{"authorityKeyIdentifierSkid"} or $CI{"subjectKeyIdentifier"} eq $CI{"authorityKeyIdentifierSkid"}) 1 0 1
Condition33%1/3
not defined $CI{"authorityKeyIdentifierSkid"} or $CI{"subjectKeyIdentifier"} eq $CI{"authorityKeyIdentifierSkid"}
l!l&&r!l&&!r
010
MC/DC0%0/2
not defined $CI{"authorityKeyIdentifierSkid"} or $CI{"subjectKeyIdentifier"} eq $CI{"authorityKeyIdentifierSkid"}
not defined $CI{"authorityKeyIdentifierSkid"} $CI{"subjectKeyIdentifier"} eq $CI{"authorityKeyIdentifierSkid"}
Truth table33%1/3
not(A) or B
A= not defined $CI{"authorityKeyIdentifierSkid"}B= $CI{"subjectKeyIdentifier"} eq $CI{"authorityKeyIdentifierSkid"}
ABresult
1X1
011
000
103 1 $Self[-1]=1;
104 1 ▶ if ( ! defined $lastMod ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lastMod) 1 0 1
105 1 $lastExp = Hex($CI{KeypublicExponent});
106 1 $lastMod = Hex($CI{Keymodulus});
107 1 $lastCAPurpose = $CI{basicConstraintsCA};
108 1 $lastKeyCertSign = $CI{keyUsageKeyCertSign};
109 1 $lastkeyUsageDigitalSignature = $CI{keyUsageDigitalSignature};
110 } } }
111  
112 # Check Validity of Time, signature, Issuer's purpose and key usage, and path length
113 2 ▶ push @Time, ( $CI{Start} < $now && $CI{End} > $now ) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$CI{'Start'} < $now && $CI{'End'} > $now ? : 2 0 2
Condition33%1/3
$CI{"Start"} < $now and $CI{"End"} > $now
!ll&&!rl&&r
002
MC/DC0%0/2
$CI{"Start"} < $now and $CI{"End"} > $now
$CI{"Start"} < $now $CI{"End"} > $now
Truth table33%1/3
A and B
A= $CI{"Start"} < $nowB= $CI{"End"} > $now
ABresult
0X0
100
111
114 2 ▶ push @PathLen, ( defined $CI{basicConstraintsPathLen} && $CI{basicConstraintsPathLen} < ($#_-$#PathLen) ) ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
defined $CI{'basicConstraintsPathLen'} && $CI{'basicConstraintsPathLen'} < $#_ - $#PathLen ? : 0 2 2
Condition33%1/3
defined $CI{"basicConstraintsPathLen"} and $CI{"basicConstraintsPathLen"} < $#_ - $#PathLen
!ll&&!rl&&r
200
MC/DC0%0/2
defined $CI{"basicConstraintsPathLen"} and $CI{"basicConstraintsPathLen"} < $#_ - $#PathLen
defined $CI{"basicConstraintsPathLen"} $CI{"basicConstraintsPathLen"} < $#_ - $#PathLen
Truth table33%1/3
A and B
A= defined $CI{"basicConstraintsPathLen"}B= $CI{"basicConstraintsPathLen"} < $#_ - $#PathLen
ABresult
0X0
100
111
115 2 push @Signed, verifySignature($CI{SignatureType},Hex($CI{SignatureValue}),$CI{X509TBSCert},$lastExp,$lastMod);
116 2 push @SignerPurposeCA, $lastCAPurpose;
117 2 push @SignerCertSignPurpose, $lastKeyCertSign;
118 # TODO Check CRL: 1 find CRL 2 In Date 3 CRL Signed 4 CA has keyUsageCRLSign 5 Cert not in List
119  
120 # Check GSIness
121 2 ▶ if ( ! $SignerPurposeCA[-1] && ! $SignerCertSignPurpose[-1] && $lastkeyUsageDigitalSignature) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $SignerPurposeCA[-1] || $SignerCertSignPurpose[-1] and $lastkeyUsageDigitalSignature) { } 0 2 2
Condition33%2/6
not $SignerPurposeCA[-1] || $SignerCertSignPurpose[-1] and $lastkeyUsageDigitalSignature
!ll&&!rl&&r
200
$SignerPurposeCA[-1] || $SignerCertSignPurpose[-1]
l!l&&r!l&&!r
200
MC/DC0%0/3
not $SignerPurposeCA[-1] || $SignerCertSignPurpose[-1] and $lastkeyUsageDigitalSignature
$SignerPurposeCA[-1] $SignerCertSignPurpose[-1] $lastkeyUsageDigitalSignature
Truth table25%1/4
not(A || B) and C
A= $SignerPurposeCA[-1]B= $SignerCertSignPurpose[-1]C= $lastkeyUsageDigitalSignature
ABCresult
1XX0
01X0
0000
0011
122 0 ▶ if ( $CI{SubjectDN} eq "$CI{IssuerDN}/CN=proxy" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($CI{'SubjectDN'} eq "$CI{'IssuerDN'}/CN=proxy") { } 0 0 0
elsif ($CI{'SubjectDN'} eq "$CI{'IssuerDN'}/CN=limited proxy") { } 0 0 0
elsif ($CI{'ProxyInfo'} eq 'RFC' or $CI{'ProxyInfo'} eq 'Pre-RFC') { } 0 0 0
123 0 ▶ if ( $CI{IssuerDN} =~ /\/CN=limited proxy$/ ) { push @GSIType,"Bad Legacy proxy: issuer was limited" }
Branch: 1 branch
BranchTrueFalseTotal
if ($CI{'IssuerDN'} =~ m[/CN=limited proxy$]) { } 0 0 0
124 0 else { push @GSIType,"Legacy Proxy" }
125 }
126 0 elsif ( $CI{SubjectDN} eq "$CI{IssuerDN}/CN=limited proxy" ) { push @GSIType,"Limited Proxy"; }
127 ▶ elsif ( $CI{ProxyInfo} eq 'RFC' || $CI{ProxyInfo} eq 'Pre-RFC' ) {
Condition0%0/3
$CI{"ProxyInfo"} eq "RFC" or $CI{"ProxyInfo"} eq "Pre-RFC"
l!l&&r!l&&!r
000
MC/DC0%0/2
$CI{"ProxyInfo"} eq "RFC" or $CI{"ProxyInfo"} eq "Pre-RFC"
$CI{"ProxyInfo"} eq "RFC" $CI{"ProxyInfo"} eq "Pre-RFC"
Truth table0%0/3
A or B
A= $CI{"ProxyInfo"} eq "RFC"B= $CI{"ProxyInfo"} eq "Pre-RFC"
ABresult
1X1
011
000
128 0 ▶ if ( $CI{SubjectDN} !~ /^$CI{IssuerDN}\/CN=[0-9]+$/ ) { push @GSIType,"Bad RFC proxy: issuer name mismatch" }
Branch: 2 branches
BranchTrueFalseTotal
if (not $CI{'SubjectDN'} =~ m[^$CI{'IssuerDN'}/CN=[0-9]+$]) { } 0 0 0
elsif ($CI{'IssuerDN'} =~ m[/CN=(?:limited )?proxy$]) { } 0 0 0
129 0 elsif ( $CI{IssuerDN} =~ /\/CN=(?:limited )?proxy$/ ) { push @GSIType,"Bad legacy proxy: issuer was legacy" }
130 0 else { push @GSIType,"$CI{ProxyInfo} Proxy"; }
131 }
132 0 else { push @GSIType,"Bad proxy"; }
133 }
134 else {
135 2 ▶ if ( $CI{basicConstraintsCA} || $CI{keyUsageKeyCertSign} ) { push @GSIType,"CA" }
Branch: 1 branch
BranchTrueFalseTotal
if ($CI{'basicConstraintsCA'} or $CI{'keyUsageKeyCertSign'}) { } 1 1 2
Condition66%2/3
$CI{"basicConstraintsCA"} or $CI{"keyUsageKeyCertSign"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$CI{"basicConstraintsCA"} or $CI{"keyUsageKeyCertSign"}
$CI{"basicConstraintsCA"} $CI{"keyUsageKeyCertSign"}
Truth table66%2/3
A or B
A= $CI{"basicConstraintsCA"}B= $CI{"keyUsageKeyCertSign"}
ABresult
1X1
011
000
136 1 else { push @GSIType, "EEC";
137 1 ($EECDN,$EECIDN,$EEC) = ($CI{SubjectDN},$CI{IssuerDN},$_);
138 }
139 }
140  
141 # GSI PathLen
142 2 ▶ push @ProxyPathLen,(defined $CI{"ProxyPathlen$CI{ProxyInfo}"} && $CI{"ProxyPathlen$CI{ProxyInfo}"}<($#_-$#ProxyPathLen))?0:1;
Branch: 1 branch
BranchTrueFalseTotal
defined $CI{"ProxyPathlen$CI{'ProxyInfo'}"} && $CI{"ProxyPathlen$CI{'ProxyInfo'}"} < $#_ - $#ProxyPathLen ? : 0 2 2
Condition33%1/3
defined $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} and $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} < $#_ - $#ProxyPathLen
!ll&&!rl&&r
200
MC/DC0%0/2
defined $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} and $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} < $#_ - $#ProxyPathLen
defined $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} < $#_ - $#ProxyPathLen
Truth table33%1/3
A and B
A= defined $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]}B= $CI{qq[ProxyPathlen$CI{"ProxyInfo"}]} < $#_ - $#ProxyPathLen
ABresult
0X0
100
111
143  
144 # Populate remaining times array
145 2 push @LifeTime, ( $CI{End} - $now );
146  
147 # Remember relevant parts of this certificate as issuer for next
148 2 $lastExp = Hex($CI{KeypublicExponent});
149 2 $lastMod = Hex($CI{Keymodulus});
150 2 $lastCAPurpose = $CI{basicConstraintsCA};
151 2 $lastKeyCertSign = $CI{keyUsageKeyCertSign};
152 2 $lastkeyUsageDigitalSignature = $CI{keyUsageDigitalSignature};
153 }
154  
155 1 my $i=0;
156 1 my @returnErrors;
157 1 ▶ while ( defined $Time[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Time[$i]) 2 1 3
158 2 my @errors=();
159 0 ▶ if ( $i > 0 && $GSIType[$i] =~ /^Bad/ ) { push @errors, $GSIType[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $GSIType[$i] =~ /^Bad/) 0 2 2
Condition66%2/3
$i > 0 and $GSIType[$i] =~ /^Bad/
!ll&&!rl&&r
110
MC/DC0%0/2
$i > 0 and $GSIType[$i] =~ /^Bad/
$i > 0 $GSIType[$i] =~ /^Bad/
Truth table66%2/3
A and B
A= $i > 0B= $GSIType[$i] =~ /^Bad/
ABresult
0X0
100
111
160 0 ▶ if ( ! $Time[$i] ) { push @errors, "Time error in certificate $i"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($Time[$i]) 0 2 2
161 0 ▶ if ( ! $Signed[$i] && ( $i!=0 || $Self[$i] )) { push @errors, "Bad signature on certificate $i"; }
Branch: 1 branch
BranchTrueFalseTotal
if (not $Signed[$i] and $i != 0 || $Self[$i]) 0 2 2
Condition16%1/6
not $Signed[$i] and $i != 0 || $Self[$i]
!ll&&!rl&&r
200
$i != 0 or $Self[$i]
l!l&&r!l&&!r
000
MC/DC0%0/3
not $Signed[$i] and $i != 0 || $Self[$i]
not $Signed[$i] $i != 0 $Self[$i]
Truth table25%1/4
not(A) and B or C
A= not $Signed[$i]B= $i != 0C= $Self[$i]
ABCresult
0XX0
1000
11X1
1011
162 0 ▶ if ( $i!=0 && $Self[$i] ) { push @errors, "Self signed certificate in at $i the chain"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0 and $Self[$i]) 0 2 2
Condition66%2/3
$i != 0 and $Self[$i]
!ll&&!rl&&r
110
MC/DC0%0/2
$i != 0 and $Self[$i]
$i != 0 $Self[$i]
Truth table66%2/3
A and B
A= $i != 0B= $Self[$i]
ABresult
0X0
100
111
163 2 ▶ if ( $GSIType[$i] !~ /^(?:Lega[sc]y|Limited|RFC|Pre-RFC) Proxy$/ && ( $i!=0 || $Self[$i] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $GSIType[$i] =~ /^(?:Lega[sc]y|Limited|RFC|Pre-RFC) Proxy$/ and $i != 0 || $Self[$i]) 2 0 2
Condition50%3/6
not $GSIType[$i] =~ /^(?:Lega[sc]y|Limited|RFC|Pre-RFC) Proxy$/ and $i != 0 || $Self[$i]
!ll&&!rl&&r
002
$i != 0 or $Self[$i]
l!l&&r!l&&!r
110
MC/DC0%0/3
not $GSIType[$i] =~ /^(?:Lega[sc]y|Limited|RFC|Pre-RFC) Proxy$/ and $i != 0 || $Self[$i]
not $GSIType[$i] =~ /^(?:Lega[sc]y|Limited|RFC|Pre-RFC) Proxy$/ $i != 0 $Self[$i]
Truth table50%2/4
not(A) and B or C
A= not $GSIType[$i] =~ /^(?:Lega[sc]y|Limited|RFC|Pre-RFC) Proxy$/B= $i != 0C= $Self[$i]
ABCresult
0XX0
1000
11X1
1011
164 0 ▶ if ( ! $SignerPurposeCA[$i] ) { push @errors, "Signer of certificate $i is not a CA"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($SignerPurposeCA[$i]) 0 2 2
165 0 ▶ if ( ! $SignerCertSignPurpose[$i] ) { push @errors, "Signer of certificate $i may not sign certificates"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($SignerCertSignPurpose[$i]) 0 2 2
166 }
167 0 ▶ if ( ! $PathLen[$i] ) { push @errors, "Path Length exceeded at certificate $i"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($PathLen[$i]) 0 2 2
168 0 ▶ if ( ! $ProxyPathLen[$i] ) { push @errors, "Proxy Path Length exceeded at certificate $i"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ProxyPathLen[$i]) 0 2 2
169 2 push @returnErrors,\@errors;
170 2 $i++;
171 }
172 1 return ( \@returnErrors, \@GSIType, \@LifeTime, $EECDN, $EECIDN, $EEC );
173 }
174  
175 ##################################################
176  
177 1 ▶sub buildchain {
Subroutine buildchain: called CC=24 SCAR=33.4
Pod undocumented
178 1 my $inref = shift;
179 1 my %in = %{$inref};
180  
181 1 ▶ my @CAdirs = (defined $in{'trustedCAdirs'})? @{$in{'trustedCAdirs'}}:();
Branch: 1 branch
BranchTrueFalseTotal
defined $in{'trustedCAdirs'} ? : 1 0 1
182 1 ▶ my @certs = (defined $in{'suppliedcerts'})? @{$in{'suppliedcerts'}}:();
Branch: 1 branch
BranchTrueFalseTotal
defined $in{'suppliedcerts'} ? : 1 0 1
183 0 ▶ my @CAcerts = (defined $in{'trustedCAs'})? @{$in{'trustedCAs'}}:();
Branch: 1 branch
BranchTrueFalseTotal
defined $in{'trustedCAs'} ? : 0 1 1
184  
185 1 my %cert; my %hash; my %ihash; my %skid; my %akid; my %dn; my %idn; my %dir; my %trust;
186 1 my $CertHashTemplate={authorityKeyIdentifier=>"", subjectKeyIdentifier=>"", keyUsage=>"", basicConstraints=>"",IHash=>"",Hash=>"",SubjectDN=>"",IssuerDN=>""};
187  
188 # Load Leaf certificate information
189 1 my @returnCerts = (shift @certs); # First one is treated as the leaf certificate/gsi-certificate
190 1 my $CertInfoRef = VOMS::Lite::X509::Examine( $returnCerts[0] ,$CertHashTemplate);
191 1 my %CERTINFO = %$CertInfoRef;
192 1 my @IHash = ($CERTINFO{IHash});
193 1 my @Hash = ($CERTINFO{Hash});
194 1 my @SKID = ($CERTINFO{subjectKeyIdentifier});
195 1 my @AKID = ($CERTINFO{authorityKeyIdentifierSkid});
196 1 my @DNs = ($CERTINFO{SubjectDN});
197 1 my @IDNs = ($CERTINFO{IssuerDN});
198 1 my @Trust = (0);
199  
200 #Make place holder for CA certs which reside in supplied directories (only load them if needed)
201 1 my @cas;
202 1 foreach my $dir (reverse @CAdirs) { # First directory takes presidence
203 1 opendir(GRIDSECURITYDIR,$dir);
204 1 push @cas, grep(/\.[0-9]+$/, readdir(GRIDSECURITYDIR));
205 1 closedir(GRIDSECURITYDIR);
206 1 foreach (@cas) {
207 1 $hash{$_}="$_";
208 1 $cert{$_}="";
209 1 $dir{$_}=$dir;
210 }
211 }
212  
213 #Load locally supplied CAcert info and peer supplied CAcert info
214 1 my $remainingtrusted=$#CAcerts;
215 1 foreach (@CAcerts,@certs) {
216 1 my $CertInfoRef=VOMS::Lite::X509::Examine($_,$CertHashTemplate);
217 1 my %CERTINFO=%$CertInfoRef;
218 1 my $index=0;
219 1 ▶ until ( ! defined $hash{"$CERTINFO{Hash}.$index"} ) { $index++; }
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $hash{qq[$CERTINFO{"Hash"}.$index]}) 1 1 2
220 1 $cert{"$CERTINFO{Hash}.$index"} = $_;
221 1 $hash{"$CERTINFO{Hash}.$index"} = $CERTINFO{Hash};
222 1 $ihash{"$CERTINFO{Hash}.$index"} = $CERTINFO{IHash};
223 1 $skid{"$CERTINFO{Hash}.$index"} = $CERTINFO{subjectKeyIdentifier};
224 1 $akid{"$CERTINFO{Hash}.$index"} = $CERTINFO{authorityKeyIdentifierSkid};
225 1 $dn{"$CERTINFO{Hash}.$index"} = $CERTINFO{SubjectDN};
226 1 $idn{"$CERTINFO{Hash}.$index"} = $CERTINFO{IssuerDN};
227 1 ▶ $trust{"$CERTINFO{Hash}.$index"} = ( $remainingtrusted > -1 ) ? 1:0; #trust only locally supplied certs
Branch: 1 branch
BranchTrueFalseTotal
$remainingtrusted > -1 ? : 0 1 1
228 1 $remainingtrusted--;
229 }
230  
231 #Loop round all certificate authorities until the chain is constructed or there are no found issuers.
232 1 my $found;
233 1 my $self=0;
234 1 for(;;) {
235 1 $found="no";
236 1 my @a;
237 1 ▶ foreach (keys(%cert)) { if ($_ =~ /^$IHash[-1].[0-9]+$/ ) { push @a, $_;} }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^$IHash[-1].[0-9]+$/) 2 0 2
238 1 my @b=sort(@a);
239 1 foreach my $file ( @b ) {
240 1 ▶ if ( $cert{$file} eq "" && defined $dir{$file} ) { # Load in CA certificate as required
Branch: 1 branch
BranchTrueFalseTotal
if ($cert{$file} eq "" and defined $dir{$file}) 1 0 1
Condition33%1/3
$cert{$file} eq "" and defined $dir{$file}
!ll&&!rl&&r
001
MC/DC0%0/2
$cert{$file} eq "" and defined $dir{$file}
$cert{$file} eq "" defined $dir{$file}
Truth table33%1/3
A and B
A= $cert{$file} eq ""B= defined $dir{$file}
ABresult
0X0
100
111
241 1 my $certder = readCert("$dir{$file}/$file");
242 1 my $CertInfoRef = VOMS::Lite::X509::Examine($certder,$CertHashTemplate);
243 1 my %CERTINFO = %$CertInfoRef;
244 1 $cert{$file} = $certder;
245 1 $hash{$file} = $CERTINFO{Hash};
246 1 $ihash{$file} = $CERTINFO{IHash};
247 1 $skid{$file} = $CERTINFO{subjectKeyIdentifier};
248 1 $akid{$file} = $CERTINFO{authorityKeyIdentifierSkid};
249 1 $dn{$file} = $CERTINFO{SubjectDN};
250 1 $idn{$file} = $CERTINFO{IssuerDN};
251 1 $trust{$file} = 1;
252 }
253  
254 1 ▶ if($IDNs[-1] eq $dn{$file} && ( ! defined $AKID[-1] || $AKID[-1] eq $skid{$file} ) ) { #Issuer names match and Key ID's match
Branch: 1 branch
BranchTrueFalseTotal
if ($IDNs[-1] eq $dn{$file} and !defined($AKID[-1]) || $AKID[-1] eq $skid{$file}) 1 0 1
Condition33%2/6
$IDNs[-1] eq $dn{$file} and !defined($AKID[-1]) || $AKID[-1] eq $skid{$file}
!ll&&!rl&&r
001
not defined $AKID[-1] or $AKID[-1] eq $skid{$file}
l!l&&r!l&&!r
010
MC/DC0%0/3
$IDNs[-1] eq $dn{$file} and !defined($AKID[-1]) || $AKID[-1] eq $skid{$file}
$IDNs[-1] eq $dn{$file} not defined $AKID[-1] $AKID[-1] eq $skid{$file}
Truth table25%1/4
A and not(B) or C
A= $IDNs[-1] eq $dn{$file}B= not defined $AKID[-1]C= $AKID[-1] eq $skid{$file}
ABCresult
0XX0
1000
11X1
1011
255 1 $found="yes";
256 1 ▶ if ( $DNs[-1] eq $IDNs[-1] && ( ! defined $AKID[-1] || $SKID[-1] eq $AKID[-1] )) { #Check last cert not self-signed
Branch: 1 branch
BranchTrueFalseTotal
if ($DNs[-1] eq $IDNs[-1] and !defined($AKID[-1]) || $SKID[-1] eq $AKID[-1]) { } 0 1 1
Condition16%1/6
$DNs[-1] eq $IDNs[-1] and !defined($AKID[-1]) || $SKID[-1] eq $AKID[-1]
!ll&&!rl&&r
100
not defined $AKID[-1] or $SKID[-1] eq $AKID[-1]
l!l&&r!l&&!r
000
MC/DC0%0/3
$DNs[-1] eq $IDNs[-1] and !defined($AKID[-1]) || $SKID[-1] eq $AKID[-1]
$DNs[-1] eq $IDNs[-1] not defined $AKID[-1] $SKID[-1] eq $AKID[-1]
Truth table25%1/4
A and not(B) or C
A= $DNs[-1] eq $IDNs[-1]B= not defined $AKID[-1]C= $SKID[-1] eq $AKID[-1]
ABCresult
0XX0
1000
11X1
1011
257 0 $self=1;
258 0 $Trust[-1]=1;
259 0 last;
260 }
261 else {
262 1 push @returnCerts, $cert{$file};
263 1 push @IHash, $ihash{$file};
264 1 push @Hash, $hash{$file};
265 1 push @SKID, $skid{$file};
266 1 push @AKID, $akid{$file};
267 1 push @DNs, $dn{$file};
268 1 push @IDNs, $idn{$file};
269 1 push @Trust, $trust{$file};
270 1 last;
271 }
272 }
273 }
274 # Stop looping if no signer found or certificate is self signed
275 1 ▶ last if ( $found eq "no" );
Branch: 1 branch
BranchTrueFalseTotal
if $found eq "no" 0 1 1
276 1 ▶ if ( $DNs[-1] eq $IDNs[-1] && ( !defined $AKID[-1] || $SKID[-1] eq $AKID[-1] )) { $self=1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DNs[-1] eq $IDNs[-1] and !defined($AKID[-1]) || $SKID[-1] eq $AKID[-1]) 1 0 1
Condition33%2/6
$DNs[-1] eq $IDNs[-1] and !defined($AKID[-1]) || $SKID[-1] eq $AKID[-1]
!ll&&!rl&&r
001
not defined $AKID[-1] or $SKID[-1] eq $AKID[-1]
l!l&&r!l&&!r
010
MC/DC0%0/3
$DNs[-1] eq $IDNs[-1] and !defined($AKID[-1]) || $SKID[-1] eq $AKID[-1]
$DNs[-1] eq $IDNs[-1] not defined $AKID[-1] $SKID[-1] eq $AKID[-1]
Truth table25%1/4
A and not(B) or C
A= $DNs[-1] eq $IDNs[-1]B= not defined $AKID[-1]C= $SKID[-1] eq $AKID[-1]
ABCresult
0XX0
1000
11X1
1011
277 }
278 1 my @verify = verifychain(@returnCerts);
279 1 my @Errors = reverse @{ $verify[0] };
280 1 my @GSI = reverse @{ $verify[1] };
281 1 my @LifeTime = reverse @{ $verify[2] };
282 1 my $EECDN = $verify[3];
283 1 my $EECIDN = $verify[4];
284 1 my $EEC = $verify[5];
285  
286 1 return { Certs => \@returnCerts,
287 IssuerHashes => \@IHash,
288 SubjectHashes => \@Hash,
289 SubjectKeyIdentifiers => \@SKID,
290 AuthorityKeyIdentifiersSKIDs => \@AKID,
291 DistinguishedNames => \@DNs,
292 IssuerDistinguishedNames => \@IDNs,
293 TrustedCA => \@Trust,
294 SelfSignedInChain => $self,
295 GSIType => \@GSI,
296 EndEntityDN => $EECDN,
297 EndEntityIssuerDN => $EECIDN,
298 EndEntityCert => $EEC,
299 Lifetimes => \@LifeTime,
300 Errors => \@Errors
301 }
302 }
303  
304 ################################################################
305  
306 # Need type of digestType, BinaryData, HexKey, HexModulus,
307 7 ▶sub digestTBS {
Subroutine digestTBS: called CC=7 SCAR=25.1
Pod undocumented
308 7 my ($type,$Data) = @_;
309  
310 7 ▶ if ( $type eq "md5WithRSA" ) { # 128 bit digest
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'md5WithRSA') { } 2 5 7
elsif ($type eq 'sha1WithRSA') { } 5 0 5
elsif ($type eq 'md2WithRSA') { } 0 0 0
311 2 ▶ if ( eval "require Digest::MD5" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require Digest::MD5") 2 0 2
312 # 1.2.840.113549.2.5 for use with 1.840.113549.1.1.4 for RSA signatures
313 2 return ASN1Wrap("30","300c06082a864886f70d02050500".ASN1Wrap("04",Digest::MD5::md5_hex($Data)));
314 }
315 }
316 elsif ( $type eq "sha1WithRSA" ) { # 160-bit
317 5 ▶ if ( eval "require Digest::SHA1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require Digest::SHA1") 5 0 5
318 # 1.3.14.3.2.26 for use with 1.840.113549.1.1.5 for RSA signatures
319 5 return ASN1Wrap("30","300906052b0e03021a0500".ASN1Wrap("04",Digest::SHA1::sha1_hex($Data)));
320 }
321 }
322 elsif ( $type eq "md2WithRSA" ) { # 128 bit
323 0 ▶ if ( eval "require Digest::MD2" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require Digest::MD2") 0 0 0
324 # 1.840.113549.2.2 for use with 1.840.113549.1.1.2 for RSA signatures
325 0 return ASN1Wrap("30","300c06082a864886f70d02020500".ASN1Wrap("04",Digest::MD2::md2_hex($Data)));
326 }
327 }
328 # elsif ( $type eq "md4WithRSA" ) { #128 bit
329 # if ( eval "require Digest::MD4" ) {
330 # # 1.840.113549.2.4 for use with 1.840.113549.1.1.3 for RSA signatures
331 # return ASN1Wrap("30","300c06082a864886f70d02040500".ASN1Wrap("04",Digest::MD4::md4_hex($Data)));
332 # }
333 # }
334 0 return undef;
335 }
336  
337 ################################################################
338 #digestSign("md5WithRSA",$Data,$chex,$nhex);
339 5 ▶sub digestSign {
Subroutine digestSign: called CC=2 SCAR=7.4
Pod undocumented
340 5 my ($type,$Data,$chex,$nhex) = @_;
341 5 my $digestTBS=digestTBS($type,$Data);
342  
343 5 ▶ if ( defined $digestTBS ) { return rsasign($digestTBS,$chex,$nhex); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $digestTBS) 5 0 5
344 0 return "";
345 }
346  
347 ################################################################
348  
349 2 ▶sub verifySignature {
Subroutine verifySignature: called CC=2 SCAR=8.1
Pod undocumented
350 2 my ($digestType,$SignedInfo,$TBS,$chex,$nhex)=@_;
351 2 ▶ return 1 if ( digestTBS($digestType,$TBS) eq rsaverify($SignedInfo,$chex,$nhex) );
Branch: 1 branch
BranchTrueFalseTotal
if digestTBS($digestType, $TBS) eq rsaverify($SignedInfo, $chex, $nhex) 2 0 2
352 0 return 0;
353 }
354  
355 ################################################################
356  
357 1 1;
358 __END__
« lib/VOMS/Lite.pm ↑ Summary lib/VOMS/Lite/PEMHelper.pm »