/[pdpsoft]/trunk/fetchcrl/TrustAnchor.pm
ViewVC logotype

Annotation of /trunk/fetchcrl/TrustAnchor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2188 - (hide annotations) (download) (as text)
Sun Feb 13 18:41:25 2011 UTC (10 years, 9 months ago) by davidg
File MIME type: application/x-perl
File size: 23908 byte(s)
Now work even for CESNET

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 davidg 2188 (defined $self->{"preserve_warnings"} and defined $self->{"preserve_errors"})
50     or die "Internal error: restoreLogMode called without previous save\n";
51 davidg 1758 $::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 davidg 1901 STATEHUNT: foreach my $output ( ( $::cnf->{_}->{"output"},
333     $::cnf->{_}->{"output_der"}, $::cnf->{_}->{"output_pem"},
334     $::cnf->{_}->{"output_nss"}, $::cnf->{_}->{"output_openssl"}) ) {
335     defined $output and $output or next;
336 davidg 1758 foreach my $file (
337     $self->{"nametemplate_der"},
338     $self->{"nametemplate_pem"},
339     $self->{"alias"}.".r\@R\@",
340     $self->{"anchorname"}.".r\@R\@",
341     ) {
342     next unless $file;
343     $file =~ s/\@R\@/$i/g;
344     $file = join "/", $output, $file;
345     next if ! -e $file;
346     $mtime = (stat(_))[9];
347     last STATEHUNT;
348     }
349     }
350     $::log->verb(3,"Inferred mtime for",$self->{"alias"},"is",$mtime) if $mtime;
351     $self->{"crl"}[$i]{"state"}{"mtime"} = $mtime if $mtime;
352     }
353 davidg 1901
354     # as a last resort, set mtime to curren time
355     $self->{"crl"}[$i]{"state"}{"mtime"} ||= time;
356    
357 davidg 1758 }
358     return 1;
359     }
360    
361     sub saveState($$) {
362     my $self = shift;
363     my $fallbackmode = shift;
364    
365     $self->{"statedir"} and -d $self->{"statedir"} and -w $self->{"statedir"} or
366     return 0;
367    
368     $self->{"crlurls"} or
369     $::log->err("loading state for uninitialised list of CRLs") and return 0;
370     $self->{"alias"} or
371     $::log->err("loading state for uninitialised trust anchor") and return 0;
372    
373     # of state, mtime is set based on CRL write in $output and filled there
374     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
375     if ( defined $self->{"statedir"} and
376     -d $self->{"statedir"}
377     ) {
378     my $state = ConfigTiny->new;
379     foreach my $key ( keys %{$self->{"crl"}[$i]{"state"}} ) {
380     $state->{$self->{"alias"}}->{$key} = $self->{"crl"}[$i]{"state"}{$key};
381     }
382     $state->write(
383     $self->{"statedir"}.'/'.$self->{"alias"}.'.'.$i.'.state' );
384     $::log->verb(5,"State saved in",
385     $self->{"statedir"}.'/'.$self->{"alias"}.'.'.$i.'.state');
386     }
387    
388     }
389     return 1;
390     }
391    
392     sub retrieveHTTP($$) {
393     my $self = shift;
394     my $idx = shift;
395     my $url = shift;
396     my %metadata;
397     my $data;
398    
399     $url =~ /^(http:|https:|ftp:)/ or die "retrieveHTTP: non-http URL $url\n";
400    
401     $::log->verb(3,"Downloading data from $url");
402     my $ua = LWP::UserAgent->new;
403     $ua->agent('fetch-crl/'.$::cnf->{_}->{version} . ' ('.
404     $ua->agent . '; '.$::cnf->{_}->{packager} . ')'
405     );
406     $ua->timeout($self->{"httptimeout"});
407     $ua->use_eval(0);
408     if ( $self->{"http_proxy"} ) {
409     if ( $self->{"http_proxy"} =~ /^ENV/i ) {
410     $ua->env_proxy();
411     } else {
412     $ua->proxy("http", $self->{"http_proxy"});
413     }
414     }
415    
416    
417     # see with a HEAD request if we can get by with old data
418     # but to assess that we need Last-Modified from the previous request
419     # (so if the CA did not send that: too bad)
420     if ( $self->{"crl"}[$idx]{"state"}{"lastmod"} and
421     $self->{"crl"}[$idx]{"state"}{"b64data"}
422     ) {
423     $::log->verb(4,"Lastmod set to",$self->{"crl"}[$idx]{"state"}{"lastmod"});
424     $::log->verb(4,"Attemping HEAD retrieval of $url");
425    
426     my $response;
427     eval {
428     local $SIG{ALRM}=sub{die "timed out after ".$self->{"httptimeout"}."s\n";};
429     alarm $self->{"httptimeout"};
430     $response = $ua->head($url);
431     alarm 0;
432     };
433     alarm 0; # make sure the alarm stops ticking, regardless of the eval
434    
435     if ( $@ ) {
436     $::log->verb(2,"HEAD error $url:", $@);
437     return undef;
438     }
439    
440     # try get if head fails anyway
441     if ( ( ! $@ ) and
442     $response->is_success and
443     $response->header("Last-Modified") ) {
444    
445     my $lastmod = HTTP::Date::str2time($response->header("Last-Modified"));
446     if ( $lastmod == $self->{"crl"}[$idx]{"state"}{"lastmod"}) {
447     $::log->verb(4,"HEAD lastmod unchanged, using cache");
448     $data = base64::b64decode($self->{"crl"}[$idx]{"state"}{"b64data"});
449     %metadata = (
450     "freshuntil" => $response->fresh_until(heuristic_expiry=>0)||time,
451     "lastmod" => $self->{"crl"}[$idx]{"state"}{"lastmod"} || time,
452     "sourceurl" => $self->{"crl"}[$idx]{"state"}{"sourceurl"} || $url
453     );
454     return ($data,%metadata) if wantarray;
455     return $data;
456    
457     } elsif ( $lastmod < $self->{"crl"}[$idx]{"state"}{"lastmod"} ) {
458     # retrieve again, but print warning abount this wierd behaviour
459     $::log->warn("Retrieved HEAD Last-Modified is older than cache: ".
460     "cache invalidated, GET issued");
461     }
462     }
463     }
464    
465     # try get if head fails anyway
466    
467     my $response;
468     eval {
469     local $SIG{ALRM}=sub{die "timed out after ".$self->{"httptimeout"}."s\n";};
470     alarm $self->{"httptimeout"};
471     $response = $ua->get($url);
472     alarm 0;
473     };
474     alarm 0; # make sure the alarm stops ticking, regardless of the eval
475    
476     if ( $@ ) {
477     chomp($@);
478     $::log->verb(0,"Download error $url:", $@);
479     return undef;
480     }
481    
482     if ( ! $response->is_success ) {
483     $::log->verb(0,"Download error $url:",$response->status_line);
484     return undef;
485     }
486    
487     $data = $response->content;
488    
489     $metadata{"freshuntil"}=$response->fresh_until(heuristic_expiry=>0)||time;
490     if ( my $lastmod = $response->header("Last-Modified") ) {
491     $metadata{"lastmod"} = HTTP::Date::str2time($lastmod);
492     }
493     $metadata{"sourceurl"} = $url;
494    
495     return ($data,%metadata) if wantarray;
496     return $data;
497     }
498    
499     sub retrieveFile($$) {
500     my $self = shift;
501     my $idx = shift;
502     my $url = shift;
503     $url =~ /^file:\/*(\/.*)$/ or die "retrieveFile: non-file URL $url\n";
504     $::log->verb(4,"Retrieving data from $url");
505    
506     # for files the previous state does not matter, we retrieve it
507     # anyway
508    
509     my $data;
510     {
511     open CRLFILE,$1 or do {
512     $! = "Cannot open $1: $!";
513     return undef;
514     };
515     binmode CRLFILE;
516     local $/;
517     $data = <CRLFILE>;
518     close CRLFILE;
519     }
520    
521     my %metadata;
522     $metadata{"lastmod"} = (stat($1))[9];
523     $metadata{"freshuntil"} = time;
524     $metadata{"sourceurl"} = $url;
525    
526     return ($data,%metadata) if wantarray;
527     return $data;
528     }
529    
530     sub retrieve($) {
531     my $self = shift;
532    
533     $self->{"crlurls"} or
534     $::log->err("Retrieving uninitialised list of CRL URLs") and return 0;
535    
536     $::log->verb(2,"Retrieving CRLs for",$self->{"alias"});
537    
538     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
539     my ($result,%response);
540    
541     $::log->verb(3,"Retrieving CRL for",$self->{"alias"},"index $i");
542    
543     # within the list of CRL URLs for a specific index, all entries
544     # are considered equivalent. I.e., if we get one, the metadata will
545     # be used for all (like Last-Modified, and cache control data)
546    
547     # if we have a cached piece of fresh data, return that one
548 davidg 1763 if ( !$self->{"nocache"} and
549     ($self->{"crl"}[$i]{"state"}{"freshuntil"} || 0) > time and
550 davidg 2085 ($self->{"crl"}[$i]{"state"}{"nextupdate"} || time) >= time and
551 davidg 1758 $self->{"crl"}[$i]{"state"}{"b64data"} ) {
552     $::log->verb(3,"Using cached content for",$self->{"alias"},"index",$i);
553     $::log->verb(4,"Content dated",
554     scalar gmtime($self->{"crl"}[$i]{"state"}{"lastmod"}),
555     "valid until",
556     scalar gmtime($self->{"crl"}[$i]{"state"}{"freshuntil"}),
557     "UTC");
558     $result = base64::b64decode($self->{"crl"}[$i]{"state"}{"b64data"});
559     %response = (
560     "freshuntil" => $self->{"crl"}[$i]{"state"}{"freshuntil"} || time,
561     "lastmod" => $self->{"crl"}[$i]{"state"}{"lastmod"} || time,
562     "sourceurl" => $self->{"crl"}[$i]{"state"}{"sourceurl"} || "null:"
563     );
564     } else {
565     foreach my $url ( split(//,$self->{"crlurls"}[$i]) ) {
566     # of these, the first one wins
567     $url =~ /^(http:|https:|ftp:)/ and
568     ($result,%response) = $self->retrieveHTTP($i,$url);
569     $url =~ /^(file:)/ and
570     ($result,%response) = $self->retrieveFile($i,$url);
571     last if $result;
572     }
573     }
574    
575     # check if result is there, otherwise invoke agingtolerance clause
576     # before actually raising this as an error
577     # note that agingtolerance stats counting only AFTER the freshness
578     # of the cache control directives has passed ...
579    
580     if ( ! $result ) {
581    
582     $::log->verb(1,"CRL retrieval for",
583     $self->{"alias"},($i?"[$i] ":"")."failed from all URLs");
584    
585     if ( $self->{"agingtolerance"} && $self->{"crl"}[$i]{"state"}{"mtime"} ) {
586     if ( ( time - $self->{"crl"}[$i]{"state"}{"mtime"} ) <
587     3600*$self->{"agingtolerance"}) {
588     $::log->warn("CRL retrieval for",
589     $self->{"alias"},($i?"[$i] ":"")."failed,",
590     int((3600*$self->{"agingtolerance"}+
591     $self->{"crl"}[$i]{"state"}{"mtime"}-
592     time )/3600).
593     " left of ".$self->{"agingtolerance"}."h, retry later.");
594     } else {
595     $::log->err("CRL retrieval for",
596     $self->{"alias"},($i?"[$i] ":"")."failed.",
597     $self->{"agingtolerance"}."h grace expired.",
598     "CRL not updated");
599     }
600     } else { # direct errors, no tolerance anymore
601     $::log->err("CRL retrieval for",
602     $self->{"alias"},($i?"[$i] ":"")."failed,",
603     "CRL not updated");
604     }
605     next; # next subindex CRL for same CA, no further action on this one
606     }
607    
608     # now data for $i is loaded in $result;
609     # for freshness checks, take a sum (SysV style)
610     my $sum = unpack("%32C*",$result) % 65535;
611    
612     $::log->verb(4,"Got",length($result),"bytes of data (sum=$sum)");
613    
614     $self->{"crl"}[$i]{"data"} = $result;
615     $self->{"crl"}[$i]{"state"}{"alias"} = $self->{"alias"};
616     $self->{"crl"}[$i]{"state"}{"index"} = $i;
617     $self->{"crl"}[$i]{"state"}{"sum"} = $sum;
618     ($self->{"crl"}[$i]{"state"}{"b64data"} =
619     base64::b64encode($result)) =~ s/\s+//gm;
620    
621     $self->{"crl"}[$i]{"state"}{"retrievaltime"} = time;
622     $self->{"crl"}[$i]{"state"}{"sourceurl"} = $response{"sourceurl"}||"null:";
623 davidg 1763 $self->{"crl"}[$i]{"state"}{"freshuntil"} = $response{"freshuntil"}||time;
624 davidg 1758 $self->{"crl"}[$i]{"state"}{"lastmod"} = $response{"lastmod"}||time;
625    
626     }
627     return 1;
628     }
629    
630     sub verifyAndConvertCRLs($) {
631     my $self = shift;
632     $self->{"crlurls"} or
633     $::log->err("Verifying uninitialised list of CRLs impossible") and return 0;
634    
635     # all CRLs must be valid in order to proceed
636     # or we would end up shifting the relative ordering around and
637     # possibly creatiing holes (or overwriting good local copies of
638     # CRLs that have gone bad on the remote end
639    
640     for ( my $i = 0; $i <= $#{$self->{"crlurls"}} ; $i++ ) { # all indices
641     $self->{"crlurls"}[$i] or
642     $::log->verb(3,"CRL",$self->getAnchorName."/".$i,"ignored (no valid URL)")
643     and next;
644     $self->{"crl"}[$i]{"data"} or
645     $::log->verb(3,"CRL",$self->getAnchorName."/".$i,"ignored (no new data)")
646     and next;
647     $::log->verb(4,"Verifying CRL $i for",$self->getAnchorName);
648    
649     my $crl = CRL->new($self->getAnchorName."/$i",$self->{"crl"}[$i]{"data"});
650     my @verifyMessages= $crl->verify(@{$self->{"cafile"}});
651    
652     # do additional checks on correlation between download and current
653     # lastUpdate of current file? have to guess the current file
654     # unless we are stateful!
655     my $oldlastupdate = $self->{"crl"}[$i]{"state"}{"lastupdate"} || undef;
656     $oldlastupdate or do {
657     $::log->verb(6,"Attempting to extract lastUpdate of previous D/L");
658     CRLSTATEHUNT: foreach my $output ( @{$::cnf->{_}->{"output_"}} ,
659     $self->{"infodir"}
660     ) {
661     foreach my $file (
662     $self->{"nametemplate_der"},
663     $self->{"nametemplate_pem"},
664     $self->{"alias"}.".r\@R\@",
665     $self->{"anchorname"}.".r\@R\@",
666     ) {
667     next unless $file;
668     (my $thisfile = $file ) =~ s/\@R\@/$i/g;
669     $thisfile = join "/", $output, $thisfile;
670     $::log->verb(6,"Trying guess $file for old CRL");
671     next if ! -e $thisfile;
672     my $oldcrldata; {
673     open OCF,$thisfile and do {
674     binmode OCF;
675     local $/;
676     $oldcrldata = <OCF>;
677     close OCF;
678     }
679     }
680     my $oldcrl = CRL->new($thisfile,$oldcrldata);
681     $oldlastupdate = $oldcrl->getLastUpdate;
682     last CRLSTATEHUNT;
683     }
684     }
685     $::log->verb(3,"Inferred lastupdate for",$self->{"alias"},"is",
686     $oldlastupdate) if $oldlastupdate;
687     };
688    
689     if ( ! $crl->getLastUpdate ) {
690     push @verifyMessages,"downloaded CRL lastUpdate could not be derived";
691     } elsif ( $oldlastupdate and ($crl->getLastUpdate < $oldlastupdate) and
692 davidg 1901 ($self->{"crl"}[$i]{"state"}{"mtime"} <= time)
693 davidg 1758 ) {
694     push @verifyMessages,"downloaded CRL lastUpdate predates installed CRL,",
695     "and current version has sane timestamp";
696     } elsif ( defined $oldlastupdate and $oldlastupdate > time ) {
697     $::log->warn($self->{"anchorname"}."/$i:","replaced with downloaded CRL",
698     "since current one has lastUpdate in the future");
699     }
700    
701     $#verifyMessages >= 0 and do {
702     $::log->err("CRL verification failed for",$self->{"anchorname"}."/$i",
703     "(".$self->{"alias"}.")");
704     foreach my $m ( @verifyMessages ) {
705     $::log->verb(0,$self->{"anchorname"}."/$i:",$m);
706     }
707     return 0;
708     };
709    
710     $self->{"crl"}[$i]{"pemdata"} = $crl->getPEMdata();
711     foreach my $key ( qw/ lastupdate nextupdate sha1fp issuer / ) {
712     $self->{"crl"}[$i]{"state"}{$key} = $crl->getAttribute($key) || "";
713     }
714     }
715     return 1;
716     }
717    
718    
719     1;
720    

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