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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3170 - (hide annotations) (download) (as text)
Mon Mar 20 15:08:49 2017 UTC (4 years, 8 months ago) by davidg
File MIME type: application/x-perl
File size: 10559 byte(s)
Add newline only to PEM files if they are short - resolving report wernig.net

1 davidg 1758 #
2     # @(#)$Id$
3     #
4     # ###########################################################################
5     #
6     #
7     # Syntax:
8     # CRLWriter->new( [name [,index]] );
9     # CRLWriter->setTA( trustanchor );
10     # CRLWriter->setIndex( index );
11     #
12     package CRLWriter;
13     use strict;
14     use File::Basename;
15     use File::Temp qw/ tempfile /;
16     require OSSL and import OSSL unless defined &OSSL::new;
17     require base64 and import base64 unless defined &base64::b64encode;
18     use vars qw/ $log $cnf /;
19    
20     sub new {
21     my $obref = {}; bless $obref;
22     my $self = shift;
23     $self = $obref;
24     my $name = shift;
25     my $index = shift;
26    
27     $self->setTA($name) if defined $name;
28     $self->setIndex($name) if defined $index;
29    
30     return $self;
31     }
32    
33    
34     sub getName($) {
35     my $self = shift;
36     return 0 unless defined $self;
37     return $self->{"ta"}->getAnchorName;
38     }
39    
40     sub setTA($$) {
41     my $self = shift;
42     my ($ta) = shift;
43     return 0 unless defined $ta and defined $self;
44     $ta->{"anchorname"} or
45     $::log->err("CRLWriter::setTA called without uninitialised trust anchor")
46     and return 0;
47     $self->{"ta"} = $ta;
48     return 1;
49     }
50    
51     sub setIndex($$) {
52     my $self = shift;
53     my ($index) = shift;
54     return 0 unless defined $self;
55     $self->{"ta"} or
56     $::log->err("CRLWriter::setIndex called without a loaded TA") and
57     return 0;
58     my $ta = $self->{"ta"};
59    
60     $ta->{"crlurls"} or
61     $::log->err("CRLWriter::setIndex called with uninitialised TA") and
62     return 0;
63    
64     ! defined $index and delete $self->{"index"} and return 1;
65    
66     $index < 0 and
67     $::log->err("CRLWriter::setIndex called with invalid index $index") and
68     return 0;
69     $index > $#{$ta->{"crlurls"}} and
70     $::log->err("CRLWriter::setIndex index $index too large") and
71     return 0;
72    
73     $self->{"index"} = $index;
74    
75     return 1;
76     }
77    
78     sub updatefile($$%) {
79     my $file = shift;
80     my $content = shift;
81     my %flags = @_;
82     $content or return undef;
83     $file or
84     $::log->err("Cannot write content to undefined path") and return undef;
85    
86     my ( $basename, $path, $suffix ) = fileparse($file);
87    
88     # get content and do a comparison. If data identical, touch only
89     # to update mtime (other tools like NGC Nagios use this mtime semantics)
90     #
91     my $olddata;
92     my $mytime;
93     -f $file and do {
94     $mytime = (stat(_))[9];
95     {
96     open OLDFILE,'<',$file or
97     $::log->err("Cannot make backup of $file: $!") and return undef;
98     binmode OLDFILE; local $/;
99     $olddata = <OLDFILE>; close OLDFILE;
100     }
101     };
102     if ( $flags{"BACKUP"} and $olddata ) {
103     if ( -w $path ) {
104     -e "$file~" and ( unlink "$file~" or
105     $::log->warn("Cannot remove old backup $file~: $!") and return undef);
106     if (open BCKFILE,'>',"$file~" ) {
107     print BCKFILE $olddata;
108     close BCKFILE;
109     utime $mytime,$mytime, "$file~";
110     } else {
111     $::log->warn("Cannot reate backup $file~: $!");
112     }
113     } else {
114     $::log->warn("Cannot make backup, $path not writable");
115     }
116     }
117    
118     defined $olddata and $olddata eq $content and do {
119     $::log->verb(4,"$file unchanged - touch only");
120     utime time,time,$file and return 1;
121     $::log->warn("Touch of $file failed, CRL unmodified");
122     return 0;
123     };
124    
125     if ( open FH,'>',$file ) {
126     print FH $content or
127     $::log->err("Write to $file: $!") and return undef;
128     close FH or
129     $::log->err("Close on write of $file: $!") and return undef;
130     } else { # something went wrong in opening the file for write,
131     # so try and restore backup if that was selected
132     $::log->err("Open for write of $file: $!");
133     $flags{"BACKUP"} and ! -s "$file" and -s "$file~" and do {
134     #file has been clobbed, but backup OK
135     unlink "$file" and link "$file~","$file" and unlink "$file~" or
136     $::log->err("Restore of backup $file failed: $!");
137     };
138     return undef;
139     }
140     return 1;
141     }
142    
143     sub writePEM($$$$) {
144     my $self = shift;
145     my $idx = shift;
146     my $data = shift;
147     my $ta = shift;
148     defined $idx and $data and $ta or
149     $::log->err("CRLWriter::writePEM: missing index or data") and return 0;
150    
151     my $output = $::cnf->{_}->{"output"};
152     $output = $::cnf->{_}->{"output_pem"} if defined $::cnf->{_}->{"output_pem"};
153     $output and -d $output or
154     $::log->err("PEM target directory $output invalid") and return 0;
155    
156     my $filename = "$output/".$ta->{"nametemplate_pem"};
157     $filename =~ s/\@R\@/$idx/g;
158    
159     my %flags = ();
160     $::cnf->{_}->{"backups"} and $flags{"BACKUP"} = 1;
161    
162 davidg 3170 if ($data !~ /\n$/sm) {
163     $::log->verb(5,"Appending newline to short PEM file",$filename);
164     $data="$data\n";
165     }
166    
167 davidg 1758 $::log->verb(3,"Writing PEM file",$filename);
168     &updatefile($filename,$data,%flags) or return 0;
169     return 1;
170     }
171    
172     sub writeDER($$$$) {
173     my $self = shift;
174     my $idx = shift;
175     my $data = shift;
176     my $ta = shift;
177     defined $idx and $data and $ta or
178     $::log->err("CRLWriter::writeDER: missing index or data") and return 0;
179    
180     my $output = $::cnf->{_}->{"output"};
181     $output = $::cnf->{_}->{"output_der"} if defined $::cnf->{_}->{"output_der"};
182     $output and -d $output or
183     $::log->err("DER target directory $output invalid") and return 0;
184    
185     my $filename = "$output/".$ta->{"nametemplate_der"};
186     $filename =~ s/\@R\@/$idx/g;
187    
188     my %flags = ();
189     $::cnf->{_}->{"backups"} and $flags{"BACKUP"} = 1;
190    
191     my $openssl=OSSL->new();
192     my ($der,$errors) = $openssl->Exec3($data,qw/crl -inform PEM -outform DER/);
193     $errors or not $der and
194     $::log->err("Data count not be converted to DER: $errors") and return 0;
195    
196     $::log->verb(3,"Writing DER file",$filename);
197     &updatefile($filename,$der,%flags) or return 0;
198     return 1;
199     }
200    
201     sub writeOpenSSL($$$$) {
202     my $self = shift;
203     my $idx = shift;
204     my $data = shift;
205     my $ta = shift;
206     defined $idx and $data and $ta or
207     $::log->err("CRLWriter::writeOpenSSL: missing index, data or ta") and
208     return 0;
209    
210     my $output = $::cnf->{_}->{"output"};
211     $output = $::cnf->{_}->{"output_openssl"} if
212     defined $::cnf->{_}->{"output_openssl"};
213     $output and -d $output or
214     $::log->err("OpenSSL target directory $output invalid") and return 0;
215    
216     my $openssl=OSSL->new();
217    
218     # guess the hash name or names from OpenSSL
219     # if mode is dual (and OpenSSL1 installed) write two files
220     my $opensslversion = $openssl->getVersion() or return 0;
221    
222     my ($cmddata,$errors);
223     my @hashes = ();
224     if ( $opensslversion ge "1" and $::cnf->{_}->{"opensslmode"} eq "dual" ) {
225     $::log->verb(5,"OpenSSL version 1 dual-mode enabled");
226     # this mode needs the ta cafile to get both hashes, since these
227     # can only be extracted by the x509 subcommand from a CA ...
228     ($cmddata,$errors) = $openssl->Exec3(undef,
229     qw/x509 -noout -subject_hash -subject_hash_old -in/,
230     $ta->{"cafile"}[0]);
231     $cmddata or
232     $::log->err("OpenSSL cannot extract hashes from",$ta->{"cafile"}[0]) and
233     return 0;
234     @hashes = split(/[\s\n]+/,$cmddata);
235     } else {
236     $::log->verb(5,"OpenSSL version 1 single-mode or pre-1.0 style");
237     ($cmddata,$errors) = $openssl->Exec3($data,qw/crl -noout -hash/);
238     $cmddata or
239     $::log->err("OpenSSL cannot extract hashes from CRL for",
240     $ta->{"alias"}.'/'.$idx
241     ) and
242     return 0;
243     @hashes = split(/[\s\n]+/,$cmddata);
244     }
245    
246     my %flags = ();
247     $::cnf->{_}->{"backups"} and $flags{"BACKUP"} = 1;
248    
249     foreach my $hash ( @hashes ) {
250     my $filename = "$output/$hash.r$idx";
251     $::log->verb(3,"Writing OpenSSL file",$filename);
252     &updatefile($filename,$data,%flags) or return 0;
253     }
254     return 1;
255     }
256    
257     sub writeNSS($$$$) {
258     my $self = shift;
259     my $idx = shift;
260     my $data = shift;
261     my $ta = shift;
262     defined $idx and $data and $ta or
263     $::log->err("CRLWriter::writeNSS: missing index, data or ta") and return 0;
264    
265     my $output = $::cnf->{_}->{"output"};
266     $output = $::cnf->{_}->{"output_nss"} if defined $::cnf->{_}->{"output_nss"};
267     $output and -d $output or
268     $::log->err("NSS target directory $output invalid") and return 0;
269    
270     my $dbprefix="";
271     $dbprefix = $::cnf->{_}->{"nssdbprefix"}
272     if defined $::cnf->{_}->{"nssdbprefix"};
273    
274     my $filename = "$output/$dbprefix";
275    
276     # the crlutil tool requires the DER formatted cert in a file
277     my $tmpdir = $::cnf->{_}->{exec3tmpdir} || $ENV{"TMPDIR"} || '/tmp';
278     my ($derfh,$dername) = tempfile("fetchcrl3der.XXXXXX",
279     DIR=>$tmpdir, UNLINK=>1);
280     (my $b64data = $data) =~ s/-[^\n]+//gm;
281     $b64data =~ s/\s+//gm;
282     print $derfh base64::b64decode($b64data); # der is decoded PEM :-)
283    
284     my $cmd = "crlutil -I -d \"$output\" -P \"$dbprefix\" ";
285     $::cnf->{_}->{nonssverify} and $cmd .= "-B ";
286     $cmd .= "-n ".$ta->{"alias"}.'.'.$idx." ";
287     $cmd .= "-i \"$dername\"";
288     my $result = `$cmd 2>&1`;
289     unlink $dername;
290     if ( $? != 0 ) {
291     $::log->err("Cannot update NSSDB filename: $result");
292     } else {
293     $::log->verb(3,"WriteNSS: ".$ta->{"alias"}.'.'.$idx." added to $filename");
294     }
295    
296     return 1;
297     }
298    
299    
300     sub writeall($) {
301     my $self = shift;
302     return 0 unless defined $self;
303     $self->{"ta"} or
304     $::log->err("CRLWriter::setIndex called without a loaded TA") and
305     return 0;
306     my $ta = $self->{"ta"};
307     $ta->{"crlurls"} or
308     $::log->err("CRLWriter::setIndex called with uninitialised TA") and
309     return 0;
310    
311     $::log->verb(2,"Writing CRLs for",$ta->{"anchorname"});
312    
313     my $completesuccess = 1;
314     for ( my $idx = 0 ; $idx <= $#{$ta->{"crl"}} ; $idx++ ) {
315     $ta->{"crl"}[$idx]{"pemdata"} or
316     $::log->verb(3,"Ignored CRL $idx skipped") and
317     next; # ignore empty crls, leave these in place
318    
319     my $writeAttempt = 0;
320     my $writeSuccess = 0;
321    
322     ( grep /^pem$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
323     $writeSuccess += $self->writePEM($idx,$ta->{"crl"}[$idx]{"pemdata"},$ta);
324    
325     ( grep /^der$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
326     $writeSuccess += $self->writeDER($idx,$ta->{"crl"}[$idx]{"pemdata"},$ta);
327    
328     ( grep /^openssl$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
329     $writeSuccess += $self->writeOpenSSL($idx,
330     $ta->{"crl"}[$idx]{"pemdata"},$ta);
331    
332     ( grep /^nss$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
333     $writeSuccess += $self->writeNSS($idx,$ta->{"crl"}[$idx]{"pemdata"},$ta);
334    
335     if ( $writeSuccess == $writeAttempt ) {
336     $::log->verb(4,"LastWrite time (mtime) set to current time");
337     $ta->{"crl"}[$idx]{"state"}{"mtime"} = time;
338     } else {
339     $::log->warn("Partial updating ($writeSuccess of $writeAttempt) for",
340     $ta->{"anchorname"},
341     "CRL $idx: mtime not updated");
342     }
343     $completesuccess &&= ($writeSuccess == $writeAttempt);
344     }
345    
346     return $completesuccess;
347     }
348    
349     1;

Properties

Name Value
svn:keywords Id

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