blib/lib/OpenCA/PKCS7.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 264 CC 28 SCAR 40.8
CC
28
Cov
0%
parseDepth
156.0
new
30.0
initSignature
20.0
CRAP 59.1
1 ## OpenCA::PKCS7
2 ##
3 ## Copyright (C) 1998-1999 Massimiliano Pala (madwolf@openca.org)
4 ## All rights reserved.
5 ##
6 ## This library is free for commercial and non-commercial use as long as
7 ## the following conditions are aheared to. The following conditions
8 ## apply to all code found in this distribution, be it the RC4, RSA,
9 ## lhash, DES, etc., code; not just the SSL code. The documentation
10 ## included with this distribution is covered by the same copyright terms
11 ##
12 ## Copyright remains Massimiliano Pala's, and as such any Copyright notices
13 ## in the code are not to be removed.
14 ## If this package is used in a product, Massimiliano Pala should be given
15 ## attribution as the author of the parts of the library used.
16 ## This can be in the form of a textual message at program startup or
17 ## in documentation (online or textual) provided with the package.
18 ##
19 ## Redistribution and use in source and binary forms, with or without
20 ## modification, are permitted provided that the following conditions
21 ## are met:
22 ## 1. Redistributions of source code must retain the copyright
23 ## notice, this list of conditions and the following disclaimer.
24 ## 2. Redistributions in binary form must reproduce the above copyright
25 ## notice, this list of conditions and the following disclaimer in the
26 ## documentation and/or other materials provided with the distribution.
27 ## 3. All advertising materials mentioning features or use of this software
28 ## must display the following acknowledgement:
29 ## "This product includes OpenCA software written by Massimiliano Pala
30 ## (madwolf@openca.org) and the OpenCA Group (www.openca.org)"
31 ## 4. If you include any Windows specific code (or a derivative thereof) from
32 ## some directory (application code) you must include an acknowledgement:
33 ## "This product includes OpenCA software (www.openca.org)"
34 ##
35 ## THIS SOFTWARE IS PROVIDED BY OPENCA DEVELOPERS ``AS IS'' AND
36 ## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 ## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39 ## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 ## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 ## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 ## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 ## SUCH DAMAGE.
46 ##
47 ## The licence and distribution terms for any publically available version or
48 ## derivative of this code cannot be changed. i.e. this code cannot simply be
49 ## copied and put under another distribution licence
50 ## [including the GNU Public Licence.]
51 ##
52  
53 ## the module's errorcode is 79
54 ##
55 ## functions
56 ##
57 ## new 11
58 ## initSignature 12
59 ## getParsed 21
60 ## getSigner 22
61 ## verifyChain 31
62 ## parseDepth 32
63 ## getSignature 23
64  
65 use strict;
66 use X500::DN;
67  
68 package OpenCA::PKCS7;
69  
70 our ($errno, $errval);
71  
72 ($OpenCA::PKCS7::VERSION = '$Revision: 1.13 $' )=~ s/(?:^.*: (\d+))|(?:\s+\$$)/defined $1?"0\.9":""/eg;
73  
74 my %params = (
75 inFile => undef,
76 signature => undef,
77 dataFile => undef,
78 caCert => undef,
79 caDir => undef,
80 parsed => undef,
81 context => undef,
82 backend => undef,
83 status => undef,
84 nochain => undef,
85 );
86  
87 sub setError {
88 my $self = shift;
89  
90 if (scalar (@_) == 4) {
91 my $keys = { @_ };
92 $errval = $keys->{ERRVAL};
93 $errno = $keys->{ERRNO};
94 } else {
95 $errno = $_[0];
96 $errval = $_[1];
97 }
98  
99 ## support for: return $self->setError (1234, "Something fails.") if (not $xyz);
100 return undef;
101 }
102  
103 ## Create an instance of the Class
104 sub new {
105 my $that = shift;
106 my $class = ref($that) || $that;
107  
108 my $self = {
109 %params,
110 };
111  
112 bless $self, $class;
113  
114 my $keys = { @_ };
115 my $tmp;
116  
117 $self->{caCert} = $keys->{CA_CERT};
118 $self->{caDir} = $keys->{CA_DIR};
119  
120 $self->{dataFile} = $keys->{DATAFILE};
121 $self->{data} = $keys->{DATA};
122  
123 $self->{inFile} = $keys->{INFILE};
124 $self->{signature} = $keys->{SIGNATURE};
125  
126 $self->{backend} = $keys->{SHELL};
127  
128 $self->{nochain} = $keys->{NOCHAIN};
129  
130 if( ($self->{inFile}) and ( -e "$self->{inFile}") ) {
131 $self->{signature} = "";
132  
133 open(FD, "<$self->{inFile}" )
134 or return $self->setError (7911021, "OpenCA::PKCS7->new: Cannot open infile ".
135 $self->{inFile}." for reading.");
136 while ( $tmp = <FD> ) {
137 $self->{signature} .= $tmp;
138 }
139 close(FD);
140 };
141  
142 if( ($self->{dataFile}) and ( -e "$self->{dataFile}") ) {
143 $self->{data} = "";
144  
145 open(FD, "<$self->{dataFile}" )
146 or return $self->setError (7911023, "OpenCA::PKCS7->new: Cannot open datafile ".
147 $self->{dataFile}." for reading.");
148 while ( $tmp = <FD> ) {
149 $self->{data} .= $tmp;
150 }
151 close(FD);
152 };
153  
154 if (not $self->{data} and $self->{inFile} and ( -e "$self->{inFile}")) {
155 $self->{data} = "";
156  
157 open(FD, "<$self->{inFile}" )
158 or return $self->setError (7911025, "OpenCA::PKCS7->new: Cannot open infile ".
159 $self->{inFile}." for reading.");
160 while ( $tmp = <FD> ) {
161 $self->{data} .= $tmp;
162 }
163 close(FD);
164 };
165  
166 return $self->setError (7911031, "OpenCA::PKCS7->new: Cannot initialize signature ($errno)\n$errval")
167 if (not $self->initSignature() );
168  
169 return $self;
170 }
171  
172 sub initSignature {
173 my $self = shift;
174 my $keys = { @_ };
175 my $tmp;
176  
177 return $self->setError (7912011, "OpenCA::PKCS7->initSignature: No signature specified.")
178 if (not $self->{signature});
179 return $self->setError (7912012, "OpenCA::PKCS7->initSignature: No data specified.")
180 if (not $self->{data});
181  
182 if( $self->getParsed() ) {
183 return 1;
184 } else {
185 return $self->setError (7912021, "OpenCA::PKCS7->initSignature: Cannot parse signature ($errno)\n$errval.")
186 }
187 }
188  
189 sub getParsed {
190 my $self = shift;
191 my $keys = { @_ };
192  
193 my ( $ret, $tmp );
194  
195 $tmp = $self->{backend}->verify( SIGNATURE=>$self->{signature},
196 ## old: DATA_FILE=>$self->{dataFile},
197 DATA => $self->{data},
198 CA_CERT => $self->{caCert},
199 CA_DIR => $self->{caDir},
200 VERBOSE => 1,
201 NOCHAIN => $self->{nochain} );
202  
203 ## why should verify the signature twice?
204 #$tmp = $self->{backend}->verify( SIGNATURE=>$self->{signature},
205 # DATA_FILE=>$self->{dataFile},
206 # NOCHAIN=>1,
207 # VERBOSE=>1 );
208  
209 if (not $tmp) {
210 $self->{status} = $OpenCA::OpenSSL::errno;
211 return $self->setError (7921021, "OpenCA::PKCS7->getParsed: The crypto-backend cannot verify the signature ".
212 "(".$OpenCA::OpenSSL::errno.")\n".$OpenCA::OpenSSL::errval);
213 }
214  
215  
216 if ( not $ret = $self->parseDepth( DEPTH=>"0", DATA=>$tmp ) ) {
217 $self->{status} = $OpenCA::OpenSSL::errno;
218 return $self->setError (7921031, "OpenCA::PKCS7->getParsed: Cannot parse the signer ($errno)\n$errval");
219 }
220  
221 $self->{parsed}->{SIGNER} = $ret->{0};
222  
223 if ( ( $tmp ) and ( $ret = $self->parseDepth( DATA=>$tmp )) ) {
224 $self->{parsed}->{CHAIN} = $ret;
225 }
226  
227 $self->{parsed}->{SIGNER}->{CERTIFICATE} =
228 $self->{backend}->pkcs7Certs( PKCS7=>$self->{signature});
229  
230 $self->{parsed}->{SIGNATURE} = $self->{signature};
231  
232 return $self->{parsed};
233 }
234  
235 sub status {
236 my $self = shift;
237  
238 return $self->{status};
239 }
240  
241 sub getSigner {
242 my $self = shift;
243  
244 my $keys = { @_ };
245 my ( $tmp, $ret );
246  
247 return $self->setError (7922011, "OpenCA::PKCS7->getSigner: The signature was not parsed.")
248 if( not $self->{parsed} );
249 return $self->{parsed}->{SIGNER};
250 }
251  
252 sub verifyChain {
253 my $self = shift;
254  
255 my $keys = { @_ };
256 my ( $tmp, $ret );
257  
258 #unnecessary because the signature was already loaded
259 #if ( $self->{inFile} ) {
260 # $tmp=$self->{backend}->verify( SIGNATURE_FILE => $self->{inFile},
261 # DATA => $self->{data},
262 # CA_CERT => $self->{caCert},
263 # CA_DIR => $self->{caDir},
264 # VERBOSE => 1 );
265 #} else {
266 $tmp=$self->{backend}->verify( SIGNATURE => $self->{signature},
267 DATA => $self->{data},
268 CA_CERT => $self->{caCert},
269 CA_DIR => $self->{caDir},
270 VERBOSE => 1 );
271 #};
272  
273 return $self->setError (7931021, "OpenCA::PKCS7->verifyChain: The crypto-backend fails ".
274 "(".$OpenCA::OpenSSL::errno.")\n".$OpenCA::OpenSSL::errval)
275 if (not $tmp);
276  
277 ## Returns if signature is not valid (verify returned an error)
278 return $self->setError (7931022, "OpenCA::PKCS7->verifyChain: The crypto-backend fails.")
279 if( $? != 0 );
280  
281 if ( not $ret = $self->parseDepth( DEPTH=>"0", DATA=>$tmp ) ) {
282 return $self->setError (7931031, "OpenCA::PKCS7->verifyChain: Cannot parse the signer ($errno)\n$errval");
283 }
284  
285 return $ret;
286 }
287  
288 sub parseDepth {
289  
290 my $self = shift;
291 my $keys = { @_ };
292  
293 my $depth = $keys->{DEPTH};
294 my $data = $keys->{DATA};
295 my @dnList = ();
296 my @ouList = ();
297  
298 my ( $serial, $dn, $email, $cn, @ou, $o, $c );
299 my ( $currentDepth, $subject, $tmp, $line, $ret );
300
301 return $self->setError (7932011, "OpenCA::PKCS7->parseDepth: No data specified.")
302 if (not $data);
303  
304 my @lines = split ( /(\n|\r)/ , $data );
305  
306 while( $line = shift @lines ) {
307  
308 if ($line =~ /^\s*error:20:/i) {
309 $self->setError (7932021, "OpenCA::PKCS7->parseDepth: The chain is not complete.");
310 $self->{status} = 20;
311 } elsif ($line =~ /^\s*error:18:/i) {
312 if (!$self->{nochain}) {
313 $self->setError (7932023, "OpenCA::PKCS7->parseDepth: Selfsigned certificate at depth 0.");
314 $self->{status} = 18;
315 }
316 } elsif ($line =~ /^\s*error:/i) {
317 $self->setError (7932039, "OpenCA::PKCS7->parseDepth: ".
318 "There is a problem with the verification of the chain ($line).");
319 ($self->{status}) = ( $line =~ /^\s*error:([^:]*):/ );
320 }
321  
322 next if( $line != /^depth/i );
323  
324 ( $currentDepth, $serial, $dn ) =
325 ( $line =~ /depth:([\d]+) serial:([a-f\d]+) subject:(.*)/ );
326 $ret->{$currentDepth}->{SERIAL} = hex ($serial) ;
327 $ret->{$currentDepth}->{DN} = $dn;
328  
329 ## Split the Subject into separate fields
330 @dnList = split( /[\,\/]+/, $dn );
331 @ouList = ();
332  
333 my $tmpOU;
334  
335 ## load the differnt parts of the DN into DN_HASH
336 print "OpenCA::PKCS7->parseDepth: DN: ".$dn."<br>\n" if ($self->{DEBUG});
337 my $x500_dn = X500::DN->ParseRFC2253 ($dn);
338 if (not $x500_dn) {
339 print "OpenCA::PKCS7->parseDepth: X500::DN failed<br>\n" if ($self->{DEBUG});
340 return $self->setError (7932081, "OpenCA::PKCS7->parseDepth: X500::DN failed.");
341 return undef;
342 }
343 my $rdn;
344 foreach $rdn ($x500_dn->getRDNs()) {
345 next if ($rdn->isMultivalued());
346 my @attr_types = $rdn->getAttributeTypes();
347 my $type = $attr_types[0];
348 my $value = $rdn->getAttributeValue ($type);
349 push (@{$ret->{$currentDepth}->{DN_HASH}->{uc($type)}}, $value);
350 print "OpenCA::PKCS7->parseDepth: DN_HASH: $type=$value<br>\n" if ($self->{DEBUG});
351 }
352  
353 }
354  
355 return $ret;
356 }
357  
358 sub getSignature {
359 my $self = shift;
360  
361 return $self->setError (7923011, "OpenCA::PKCS7->getSignature: There is no signature present.")
362 if( not $self->{signature} );
363 return $self->{signature};
364 }
365  
366 sub getSignerCert {
367 my $self = shift;
368  
369 my $keys = { @_ };
370  
371  
372 }
373  
374 # Autoload methods go after =cut, and are processed by the autosplit program.
375  
376 1;