/[pdpsoft]/nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.10-1/TrustAnchor.pm
ViewVC logotype

Contents of /nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.10-1/TrustAnchor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1767 - (show annotations) (download) (as text)
Fri Jun 11 19:44:35 2010 UTC (11 years, 5 months ago) by davidg
Original Path: trunk/fetchcrl/TrustAnchor.pm
File MIME type: application/x-perl
File size: 23341 byte(s)
work harded to find a CA dir when individual info files are given as arguments

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

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