/[pdpsoft]/nl.nikhef.pdp.fetchcrl/trunk/TrustAnchor.pm
ViewVC logotype

Annotation of /nl.nikhef.pdp.fetchcrl/trunk/TrustAnchor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1878 - (hide annotations) (download) (as text)
Tue Aug 10 07:03:43 2010 UTC (11 years, 2 months ago) by davidg
Original Path: trunk/fetchcrl/TrustAnchor.pm
File MIME type: application/x-perl
File size: 23527 byte(s)
upgrade to RC4, see changelog

1 davidg 1758 #
2     # @(#)$Id$
3     #
4     # ###########################################################################
5     #
6     #
7     package TrustAnchor;
8     use strict;
9     use File::Basename;
10     use LWP;
11     require ConfigTiny and import ConfigTiny unless defined &ConfigTiny::new;
12     require CRL and import CRL unless defined &CRL::new;
13     require base64 and import base64 unless defined &base64::b64encode;
14     use vars qw/ $log $cnf /;
15    
16     sub new {
17     my $obref = {}; bless $obref;
18     my $self = shift;
19     $self = $obref;
20     my $name = shift;
21    
22     $self->{"infodir"} = $cnf->{_}->{infodir};
23     $self->{"suffix"} = "info";
24    
25     $self->loadAnchor($name) if defined $name;
26    
27     return $self;
28     }
29    
30     sub saveLogMode($) {
31     my $self = shift;
32     return 0 unless defined $self;
33     $self->{"preserve_warnings"} = $::log->getwarnings;
34     $self->{"preserve_errors"} = $::log->geterrors;
35     return 1;
36     }
37    
38     sub setLogMode($) {
39     my $self = shift;
40     return 0 unless defined $self;
41     $self->{"nowarnings"} and $::log->setwarnings(0);
42     $self->{"noerrors"} and $::log->seterrors(0);
43     return 1;
44     }
45    
46     sub restoreLogMode($) {
47     my $self = shift;
48     return 0 unless defined $self;
49     $self->{"preserve_warnings"} and $self->{"preserve_errors"} or
50     die "Internal error: restoreLogMode called without previous save\n";
51     $::log->setwarnings($self->{"preserve_warnings"});
52     $::log->seterrors($self->{"preserve_errors"});
53     return 1;
54     }
55    
56     sub getInfodir($$) {
57     my $self = shift;
58     my ($path) = shift;
59     return 0 unless defined $self;
60    
61     return $self->{"infodir"};
62     }
63    
64     sub setInfodir($$) {
65     my $self = shift;
66     my ($path) = shift;
67     return 0 unless defined $path and defined $self;
68    
69     -e $path or
70     $::log->err("setInfodir: path $path does not exist") and return 0;
71     -d $path or
72     $::log->err("setInfodir: path $path is not a directory") and return 0;
73    
74     $self->{"infodir"} = $path;
75    
76     return 1;
77     }
78    
79    
80     sub loadAnchor($$) {
81     my $self = shift;
82     my ($name) = @_;
83     return 0 unless defined $name;
84    
85     $::log->verb(1,"Initializing trust anchor $name");
86    
87     my ( $basename, $path, $suffix) = fileparse($name,('.info','.crl_url'));
88    
89     $path = "" if $path eq "./" and substr($name,0,length($path)) ne $path ;
90    
91     $::log->err("Invalid name of trust anchor $name") and return 0
92     unless $basename;
93    
94     $self->{"infodir"} = $path if $path ne "";
95     $path = $self->{"infodir"} || "";
96     $path and $path .= "/" unless $path =~ /\/$/;
97    
98     if ( $suffix ) {
99     -e $name or
100     $::log->err("Trust anchor data $name not found") and return 0;
101     } else { # try and guess which suffix should be used
102     ($suffix eq "" and -e $path.$basename.".info" ) and $suffix = ".info";
103     ($suffix eq "" and -e $path.$basename.".crl_url" ) and $suffix = ".crl_url";
104     $suffix or
105     $::log->err("No trust anchor metadata for $basename in '$path'")
106     and return 0;
107     }
108    
109     if ( $suffix eq ".crl_url" ) {
110    
111     $self->{"alias"} = $basename;
112     @{$self->{"crlurls"}} = ();
113     open CRLURL,"$path$basename$suffix" or
114     $::log->err("Error reading crl_url $path$basename$suffix: $!") and return 0;
115     my $urllist;
116     while (<CRLURL>) {
117     /^\s*([^#\n]+).*$/ and my $url = $1 or next;
118     $url =~ s/\s*$//; # trailing whitespace is ignored
119    
120     $url =~ /^\w+:\/\/.*$/ or
121     $::log->err("File $path$basename$suffix contains a non-URL entry")
122     and close CRLURL and return 0;
123    
124     $urllist and $urllist .= "";
125     $urllist .= $url;
126     }
127     close CRLURL;
128     push @{$self->{"crlurls"}}, $urllist;
129     $self->{"status"} ||= "unknown";
130    
131     } else {
132    
133     my $info = ConfigTiny->new();
134     $info->read( $path . $basename . $suffix ) or
135     $::log->err("Error reading info $path$basename$suffix", $info->errstr)
136     and return 0;
137    
138     $info->{_}->{"crl_url"} and $info->{_}->{"crl_url.0"} and
139     $::log->err("Invalid info for $basename: crl_url and .0 duplicate") and
140     return 0;
141     $info->{_}->{"crl_url"} and
142     $info->{_}->{"crl_url.0"} = $info->{_}->{"crl_url"};
143    
144     # only do something when there is actually a CRL to process
145     $info->{_}->{"crl_url.0"} or
146     $::log->verb(1,"Trust anchor $basename does not have a CRL") and return 0;
147    
148     $info->{_}->{"alias"} or
149     $::log->err("Invalid info for $basename: no alias") and
150     return 0;
151     $self->{"alias"} = $info->{_}->{"alias"};
152    
153     @{$self->{"crlurls"}} = ();
154     for ( my $i=0 ; defined $info->{_}{"crl_url.".$i} ; $i++ ) {
155     $info->{_}{"crl_url.".$i} =~ s/[;\s]+//g;
156     $info->{_}{"crl_url.".$i} =~ s/^\s*([^\s]*)\s*$/$1/;
157    
158     $info->{_}{"crl_url.".$i} =~ /^\w+:\/\// or
159     $::log->err("File $path$basename$suffix contains a non-URL entry",
160     $info->{_}{"crl_url.".$i})
161     and close CRLURL and return 0;
162    
163     push @{$self->{"crlurls"}} , $info->{_}{"crl_url.".$i};
164     }
165    
166     foreach my $field ( qw/email ca_url status/ ) {
167     $self->{$field} = $info->{_}->{$field} if $info->{_}->{$field};
168     }
169    
170     # status of CA is only knwon for info-file based CAs
171     $self->{"status"} ||= "local";
172    
173     }
174    
175     # preserve basename of file for config and diagnostics
176     $self->{"anchorname"} = $basename;
177    
178     #
179     # set defaults for common values
180     foreach my $key ( qw /
181     prepend_url postpend_url agingtolerance
182     httptimeout proctimeout
183     nowarnings noerrors nocache http_proxy
184     nametemplate_der nametemplate_pem
185     cadir catemplate statedir
186     / ) {
187 davidg 1767 $self->{$key} = $self->{$key} ||
188 davidg 1758 $::cnf->{$self->{"alias"}}->{$key} ||
189     $::cnf->{$self->{"anchorname"}}->{$key} ||
190     $::cnf->{_}->{$key} or delete $self->{$key};
191     defined $self->{$key} and do {
192     $self->{$key} =~ s/\@ANCHORNAME\@/$self->{"anchorname"}/g;
193     $self->{$key} =~ s/\@STATUS\@/$self->{"status"}/g;
194     $self->{$key} =~ s/\@ALIAS\@/$self->{"alias"}/g;
195     };
196     }
197     # reversible toggle options
198     foreach my $key ( qw / warnings errors cache / ) {
199     delete $self->{"no$key"} if $::cnf->{$self->{"alias"}}->{$key} or
200     $::cnf->{$self->{"anchorname"}}->{$key} or
201     $::cnf->{_}->{$key};
202     }
203     foreach my $key ( qw / nohttp_proxy noprepend_url nopostpend_url
204     nostatedir / ) {
205     (my $nokey = $key) =~ s/^no//;
206     delete $self->{"$nokey"} if $::cnf->{$self->{"alias"}}->{$key} or
207     $::cnf->{$self->{"anchorname"}}->{$key} or
208     $::cnf->{_}->{$key};
209     }
210    
211     # overriding of the URLs (alias takes precedence over anchorname
212     foreach my $section ( qw / anchorname alias / ) {
213     my $i = 0;
214     while ( defined ($::cnf->{$self->{$section}}->{"crl_url.".$i}) ) {
215     my $urls;
216     ($urls=$::cnf->{$self->{$section}}->{"crl_url.".$i} )=~s/[;\s]+//g;
217     ${$self->{"crlurls"}}[$i] = $urls;
218     $i++;
219     }
220     }
221    
222     # templates to construct a CA name may still have other separators
223     $self->{"catemplate"} =~ s/[;\s]+//g;
224    
225     # select only http/https/ftp/file URLs
226     # also transform the URLs using the base patterns and prepend any
227     # local URL patterns (@ANCHORNAME@, @ALIAS@, and @R@)
228     for ( my $i=0; $i <= $#{$self->{"crlurls"}} ; $i++ ) {
229     my $urlstring = @{$self->{"crlurls"}}[$i];
230     my @urls = split(//,$urlstring);
231     $urlstring="";
232     foreach my $url ( @urls ) {
233     if ( $url =~ /^(http:|https:|ftp:|file:)/ ) {
234     $urlstring.="" if $urlstring; $urlstring.=$url;
235     } else {
236     $::log->verb(0,"URL $url in $basename$suffix unsupported, ignored");
237     }
238     }
239     if ( my $purl = $self->{"prepend_url"} ) {
240     $purl =~ s/\@R\@/$i/g;
241     $urlstring = join "" , $purl , $urlstring;
242     }
243     if ( my $purl = $self->{"postpend_url"} ) {
244     $purl =~ s/\@R\@/$i/g;
245     $urlstring = join "" , $urlstring, $purl;
246     }
247     if ( ! $urlstring ) {
248     $::log->err("No usable CRL URLs for",$self->getAnchorName);
249     $self->{"crlurls"}[$i] = "";
250     } else {
251     $self->{"crlurls"}[$i] = $urlstring;
252     }
253     }
254    
255     return 1;
256     }
257    
258     sub getAnchorName($) {
259     my $self = shift;
260     return ($self->{"anchorname"} || undef);
261     }
262    
263     sub printAnchorName($) {
264     my $self = shift;
265     print "" . ($self->{"anchorname"} || "undefined") ."\n";
266     }
267    
268     sub loadCAfiles($) {
269     my $self = shift;
270     my $idx = 0;
271    
272 davidg 1767 # try to find a CA dir, whatever it takes, almost
273     my $cadir = $self->{"cadir"} || $self->{"infodir"};
274    
275     -d $cadir or
276     $::log->err("CA directory",$cadir,"does not exist") and
277 davidg 1758 return 0;
278    
279     @{$self->{"cafile"}} = ();
280     do {
281     my $cafile;
282     foreach my $catpl ( split //, $self->{"catemplate"} ) {
283     $catpl =~ s/\@R\@/$idx/g;
284 davidg 1767 -e $cadir.'/'.$catpl and
285     $cafile = $cadir.'/'.$catpl and last;
286 davidg 1758 }
287     defined $cafile or do {
288     $idx or do $::log->err("Cannot find any CA for",
289 davidg 1767 $self->{"alias"},"in",$cadir);
290 davidg 1758 return $idx?1:0;
291     };
292 davidg 1878 # is the new one any different from the previous (i.e. is the CA indexed?)
293     $#{$self->{"cafile"}} >= 0 and
294     $cafile eq $self->{"cafile"}[$#{$self->{"cafile"}}] and return 1;
295 davidg 1758 push @{$self->{"cafile"}}, $cafile;
296     $::log->verb(3,"Added CA file $idx: $cafile");
297     } while(++$idx);
298     return 0; # you never should come here
299     }
300    
301    
302     sub loadState($$) {
303     my $self = shift;
304     my $fallbackmode = shift;
305    
306     $self->{"crlurls"} or
307     $::log->err("loading state for uninitialised list of CRLs") and return 0;
308     $self->{"alias"} or
309     $::log->err("loading state for uninitialised trust anchor") and return 0;
310    
311     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
312     if ( $self->{"statedir"} and
313     -e $self->{"statedir"}.'/'.$self->{"alias"}.'.'.$i.'.state'
314     ) {
315     my $state = ConfigTiny->new();
316     $state->read($self->{"statedir"}.'/'.$self->{"alias"}.'.'.$i.'.state')
317     or $::log->err("Cannot read existing state file",
318     $self->{"statedir"}.'/'.$self->{"alias"}.'.$i.state',
319     " - ",$state->errstr) and return 0;
320     foreach my $key ( keys %{$state->{$self->{"alias"}}} ) {
321     $self->{"crl"}[$i]{"state"}{$key} = $state->{$self->{"alias"}}->{$key};
322     }
323     }
324    
325     # fine, but we should find at least an mtime if at all possible
326     # make sure it is there:
327     # try to retrieve state from installed files in @output_
328     # where the first look-alike CRL will win. NSS databases
329     # are NOT supported for this heuristic
330     if ( ! defined $self->{"crl"}[$i]{"state"}{"mtime"} ) {
331     my $mtime;
332     STATEHUNT: foreach my $output ( @{$::cnf->{_}->{"output_"}} ) {
333     foreach my $file (
334     $self->{"nametemplate_der"},
335     $self->{"nametemplate_pem"},
336     $self->{"alias"}.".r\@R\@",
337     $self->{"anchorname"}.".r\@R\@",
338     ) {
339     next unless $file;
340     $file =~ s/\@R\@/$i/g;
341     $file = join "/", $output, $file;
342     next if ! -e $file;
343     $mtime = (stat(_))[9];
344     last STATEHUNT;
345     }
346     }
347     $::log->verb(3,"Inferred mtime for",$self->{"alias"},"is",$mtime) if $mtime;
348     $self->{"crl"}[$i]{"state"}{"mtime"} = $mtime if $mtime;
349     }
350     }
351     return 1;
352     }
353    
354     sub saveState($$) {
355     my $self = shift;
356     my $fallbackmode = shift;
357    
358     $self->{"statedir"} and -d $self->{"statedir"} and -w $self->{"statedir"} or
359     return 0;
360    
361     $self->{"crlurls"} or
362     $::log->err("loading state for uninitialised list of CRLs") and return 0;
363     $self->{"alias"} or
364     $::log->err("loading state for uninitialised trust anchor") and return 0;
365    
366     # of state, mtime is set based on CRL write in $output and filled there
367     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
368     if ( defined $self->{"statedir"} and
369     -d $self->{"statedir"}
370     ) {
371     my $state = ConfigTiny->new;
372     foreach my $key ( keys %{$self->{"crl"}[$i]{"state"}} ) {
373     $state->{$self->{"alias"}}->{$key} = $self->{"crl"}[$i]{"state"}{$key};
374     }
375     $state->write(
376     $self->{"statedir"}.'/'.$self->{"alias"}.'.'.$i.'.state' );
377     $::log->verb(5,"State saved in",
378     $self->{"statedir"}.'/'.$self->{"alias"}.'.'.$i.'.state');
379     }
380    
381     }
382     return 1;
383     }
384    
385     sub retrieveHTTP($$) {
386     my $self = shift;
387     my $idx = shift;
388     my $url = shift;
389     my %metadata;
390     my $data;
391    
392     $url =~ /^(http:|https:|ftp:)/ or die "retrieveHTTP: non-http URL $url\n";
393    
394     $::log->verb(3,"Downloading data from $url");
395     my $ua = LWP::UserAgent->new;
396     $ua->agent('fetch-crl/'.$::cnf->{_}->{version} . ' ('.
397     $ua->agent . '; '.$::cnf->{_}->{packager} . ')'
398     );
399     $ua->timeout($self->{"httptimeout"});
400     $ua->use_eval(0);
401     if ( $self->{"http_proxy"} ) {
402     if ( $self->{"http_proxy"} =~ /^ENV/i ) {
403     $ua->env_proxy();
404     } else {
405     $ua->proxy("http", $self->{"http_proxy"});
406     }
407     }
408    
409    
410     # see with a HEAD request if we can get by with old data
411     # but to assess that we need Last-Modified from the previous request
412     # (so if the CA did not send that: too bad)
413     if ( $self->{"crl"}[$idx]{"state"}{"lastmod"} and
414     $self->{"crl"}[$idx]{"state"}{"b64data"}
415     ) {
416     $::log->verb(4,"Lastmod set to",$self->{"crl"}[$idx]{"state"}{"lastmod"});
417     $::log->verb(4,"Attemping HEAD retrieval of $url");
418    
419     my $response;
420     eval {
421     local $SIG{ALRM}=sub{die "timed out after ".$self->{"httptimeout"}."s\n";};
422     alarm $self->{"httptimeout"};
423     $response = $ua->head($url);
424     alarm 0;
425     };
426     alarm 0; # make sure the alarm stops ticking, regardless of the eval
427    
428     if ( $@ ) {
429     $::log->verb(2,"HEAD error $url:", $@);
430     return undef;
431     }
432    
433     # try get if head fails anyway
434     if ( ( ! $@ ) and
435     $response->is_success and
436     $response->header("Last-Modified") ) {
437    
438     my $lastmod = HTTP::Date::str2time($response->header("Last-Modified"));
439     if ( $lastmod == $self->{"crl"}[$idx]{"state"}{"lastmod"}) {
440     $::log->verb(4,"HEAD lastmod unchanged, using cache");
441     $data = base64::b64decode($self->{"crl"}[$idx]{"state"}{"b64data"});
442     %metadata = (
443     "freshuntil" => $response->fresh_until(heuristic_expiry=>0)||time,
444     "lastmod" => $self->{"crl"}[$idx]{"state"}{"lastmod"} || time,
445     "sourceurl" => $self->{"crl"}[$idx]{"state"}{"sourceurl"} || $url
446     );
447     return ($data,%metadata) if wantarray;
448     return $data;
449    
450     } elsif ( $lastmod < $self->{"crl"}[$idx]{"state"}{"lastmod"} ) {
451     # retrieve again, but print warning abount this wierd behaviour
452     $::log->warn("Retrieved HEAD Last-Modified is older than cache: ".
453     "cache invalidated, GET issued");
454     }
455     }
456     }
457    
458     # try get if head fails anyway
459    
460     my $response;
461     eval {
462     local $SIG{ALRM}=sub{die "timed out after ".$self->{"httptimeout"}."s\n";};
463     alarm $self->{"httptimeout"};
464     $response = $ua->get($url);
465     alarm 0;
466     };
467     alarm 0; # make sure the alarm stops ticking, regardless of the eval
468    
469     if ( $@ ) {
470     chomp($@);
471     $::log->verb(0,"Download error $url:", $@);
472     return undef;
473     }
474    
475     if ( ! $response->is_success ) {
476     $::log->verb(0,"Download error $url:",$response->status_line);
477     return undef;
478     }
479    
480     $data = $response->content;
481    
482     $metadata{"freshuntil"}=$response->fresh_until(heuristic_expiry=>0)||time;
483     if ( my $lastmod = $response->header("Last-Modified") ) {
484     $metadata{"lastmod"} = HTTP::Date::str2time($lastmod);
485     }
486     $metadata{"sourceurl"} = $url;
487    
488     return ($data,%metadata) if wantarray;
489     return $data;
490     }
491    
492     sub retrieveFile($$) {
493     my $self = shift;
494     my $idx = shift;
495     my $url = shift;
496     $url =~ /^file:\/*(\/.*)$/ or die "retrieveFile: non-file URL $url\n";
497     $::log->verb(4,"Retrieving data from $url");
498    
499     # for files the previous state does not matter, we retrieve it
500     # anyway
501    
502     my $data;
503     {
504     open CRLFILE,$1 or do {
505     $! = "Cannot open $1: $!";
506     return undef;
507     };
508     binmode CRLFILE;
509     local $/;
510     $data = <CRLFILE>;
511     close CRLFILE;
512     }
513    
514     my %metadata;
515     $metadata{"lastmod"} = (stat($1))[9];
516     $metadata{"freshuntil"} = time;
517     $metadata{"sourceurl"} = $url;
518    
519     return ($data,%metadata) if wantarray;
520     return $data;
521     }
522    
523     sub retrieve($) {
524     my $self = shift;
525    
526     $self->{"crlurls"} or
527     $::log->err("Retrieving uninitialised list of CRL URLs") and return 0;
528    
529     $::log->verb(2,"Retrieving CRLs for",$self->{"alias"});
530    
531     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
532     my ($result,%response);
533    
534     $::log->verb(3,"Retrieving CRL for",$self->{"alias"},"index $i");
535    
536     # within the list of CRL URLs for a specific index, all entries
537     # are considered equivalent. I.e., if we get one, the metadata will
538     # be used for all (like Last-Modified, and cache control data)
539    
540     # if we have a cached piece of fresh data, return that one
541 davidg 1763 if ( !$self->{"nocache"} and
542     ($self->{"crl"}[$i]{"state"}{"freshuntil"} || 0) > time and
543 davidg 1758 $self->{"crl"}[$i]{"state"}{"b64data"} ) {
544     $::log->verb(3,"Using cached content for",$self->{"alias"},"index",$i);
545     $::log->verb(4,"Content dated",
546     scalar gmtime($self->{"crl"}[$i]{"state"}{"lastmod"}),
547     "valid until",
548     scalar gmtime($self->{"crl"}[$i]{"state"}{"freshuntil"}),
549     "UTC");
550     $result = base64::b64decode($self->{"crl"}[$i]{"state"}{"b64data"});
551     %response = (
552     "freshuntil" => $self->{"crl"}[$i]{"state"}{"freshuntil"} || time,
553     "lastmod" => $self->{"crl"}[$i]{"state"}{"lastmod"} || time,
554     "sourceurl" => $self->{"crl"}[$i]{"state"}{"sourceurl"} || "null:"
555     );
556     } else {
557     foreach my $url ( split(//,$self->{"crlurls"}[$i]) ) {
558     # of these, the first one wins
559     $url =~ /^(http:|https:|ftp:)/ and
560     ($result,%response) = $self->retrieveHTTP($i,$url);
561     $url =~ /^(file:)/ and
562     ($result,%response) = $self->retrieveFile($i,$url);
563     last if $result;
564     }
565     }
566    
567     # check if result is there, otherwise invoke agingtolerance clause
568     # before actually raising this as an error
569     # note that agingtolerance stats counting only AFTER the freshness
570     # of the cache control directives has passed ...
571    
572     if ( ! $result ) {
573    
574     $::log->verb(1,"CRL retrieval for",
575     $self->{"alias"},($i?"[$i] ":"")."failed from all URLs");
576    
577     if ( $self->{"agingtolerance"} && $self->{"crl"}[$i]{"state"}{"mtime"} ) {
578     if ( ( time - $self->{"crl"}[$i]{"state"}{"mtime"} ) <
579     3600*$self->{"agingtolerance"}) {
580     $::log->warn("CRL retrieval for",
581     $self->{"alias"},($i?"[$i] ":"")."failed,",
582     int((3600*$self->{"agingtolerance"}+
583     $self->{"crl"}[$i]{"state"}{"mtime"}-
584     time )/3600).
585     " left of ".$self->{"agingtolerance"}."h, retry later.");
586     } else {
587     $::log->err("CRL retrieval for",
588     $self->{"alias"},($i?"[$i] ":"")."failed.",
589     $self->{"agingtolerance"}."h grace expired.",
590     "CRL not updated");
591     }
592     } else { # direct errors, no tolerance anymore
593     $::log->err("CRL retrieval for",
594     $self->{"alias"},($i?"[$i] ":"")."failed,",
595     "CRL not updated");
596     }
597     next; # next subindex CRL for same CA, no further action on this one
598     }
599    
600     # now data for $i is loaded in $result;
601     # for freshness checks, take a sum (SysV style)
602     my $sum = unpack("%32C*",$result) % 65535;
603    
604     $::log->verb(4,"Got",length($result),"bytes of data (sum=$sum)");
605    
606     $self->{"crl"}[$i]{"data"} = $result;
607     $self->{"crl"}[$i]{"state"}{"alias"} = $self->{"alias"};
608     $self->{"crl"}[$i]{"state"}{"index"} = $i;
609     $self->{"crl"}[$i]{"state"}{"sum"} = $sum;
610     ($self->{"crl"}[$i]{"state"}{"b64data"} =
611     base64::b64encode($result)) =~ s/\s+//gm;
612    
613     $self->{"crl"}[$i]{"state"}{"retrievaltime"} = time;
614     $self->{"crl"}[$i]{"state"}{"sourceurl"} = $response{"sourceurl"}||"null:";
615 davidg 1763 $self->{"crl"}[$i]{"state"}{"freshuntil"} = $response{"freshuntil"}||time;
616 davidg 1758 $self->{"crl"}[$i]{"state"}{"lastmod"} = $response{"lastmod"}||time;
617    
618     }
619     return 1;
620     }
621    
622     sub verifyAndConvertCRLs($) {
623     my $self = shift;
624     $self->{"crlurls"} or
625     $::log->err("Verifying uninitialised list of CRLs impossible") and return 0;
626    
627     # all CRLs must be valid in order to proceed
628     # or we would end up shifting the relative ordering around and
629     # possibly creatiing holes (or overwriting good local copies of
630     # CRLs that have gone bad on the remote end
631    
632     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
633     $self->{"crlurls"}[$i] or
634     $::log->verb(3,"CRL",$self->getAnchorName."/".$i,"ignored (no valid URL)")
635     and next;
636     $self->{"crl"}[$i]{"data"} or
637     $::log->verb(3,"CRL",$self->getAnchorName."/".$i,"ignored (no new data)")
638     and next;
639     $::log->verb(4,"Verifying CRL $i for",$self->getAnchorName);
640    
641     my $crl = CRL->new($self->getAnchorName."/$i",$self->{"crl"}[$i]{"data"});
642     my @verifyMessages= $crl->verify(@{$self->{"cafile"}});
643    
644     # do additional checks on correlation between download and current
645     # lastUpdate of current file? have to guess the current file
646     # unless we are stateful!
647     my $oldlastupdate = $self->{"crl"}[$i]{"state"}{"lastupdate"} || undef;
648     $oldlastupdate or do {
649     $::log->verb(6,"Attempting to extract lastUpdate of previous D/L");
650     CRLSTATEHUNT: foreach my $output ( @{$::cnf->{_}->{"output_"}} ,
651     $self->{"infodir"}
652     ) {
653     foreach my $file (
654     $self->{"nametemplate_der"},
655     $self->{"nametemplate_pem"},
656     $self->{"alias"}.".r\@R\@",
657     $self->{"anchorname"}.".r\@R\@",
658     ) {
659     next unless $file;
660     (my $thisfile = $file ) =~ s/\@R\@/$i/g;
661     $thisfile = join "/", $output, $thisfile;
662     $::log->verb(6,"Trying guess $file for old CRL");
663     next if ! -e $thisfile;
664     my $oldcrldata; {
665     open OCF,$thisfile and do {
666     binmode OCF;
667     local $/;
668     $oldcrldata = <OCF>;
669     close OCF;
670     }
671     }
672     my $oldcrl = CRL->new($thisfile,$oldcrldata);
673     $oldlastupdate = $oldcrl->getLastUpdate;
674     last CRLSTATEHUNT;
675     }
676     }
677     $::log->verb(3,"Inferred lastupdate for",$self->{"alias"},"is",
678     $oldlastupdate) if $oldlastupdate;
679     };
680    
681     if ( ! $crl->getLastUpdate ) {
682     push @verifyMessages,"downloaded CRL lastUpdate could not be derived";
683     } elsif ( $oldlastupdate and ($crl->getLastUpdate < $oldlastupdate) and
684     ($self->{"crl"}[$i]{"state"}{"mtime"} < time)
685     ) {
686     push @verifyMessages,"downloaded CRL lastUpdate predates installed CRL,",
687     "and current version has sane timestamp";
688     } elsif ( defined $oldlastupdate and $oldlastupdate > time ) {
689     $::log->warn($self->{"anchorname"}."/$i:","replaced with downloaded CRL",
690     "since current one has lastUpdate in the future");
691     }
692    
693     $#verifyMessages >= 0 and do {
694     $::log->err("CRL verification failed for",$self->{"anchorname"}."/$i",
695     "(".$self->{"alias"}.")");
696     foreach my $m ( @verifyMessages ) {
697     $::log->verb(0,$self->{"anchorname"}."/$i:",$m);
698     }
699     return 0;
700     };
701    
702     $self->{"crl"}[$i]{"pemdata"} = $crl->getPEMdata();
703     foreach my $key ( qw/ lastupdate nextupdate sha1fp issuer / ) {
704     $self->{"crl"}[$i]{"state"}{$key} = $crl->getAttribute($key) || "";
705     }
706     }
707     return 1;
708     }
709    
710    
711     1;
712    

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28