CrlPublish/CrlInfo.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 166 CC 19 SCAR 21.6
CC
19
Cov
0%
importIssuingUrl
20.0
new
12.0
retrieve
12.0
CRAP 8.7
1 package EJBCA::CrlPublish::CrlInfo;
2 use warnings;
3 use strict;
4 #
5 # crlpublish
6 #
7 # Copyright (C) 2014, Kevin Cody-Little <kcody@cpan.org>
8 #
9 # Portions derived from crlpublisher.sh, original copyright follows:
10 #
11 # Copyright (C) 2011, Branko Majic <branko@majic.rs>
12 #
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #
26 ###############################################################################
27  
28 =head1 NAME
29  
30 EJBCA::CrlPublish::CrlInfo
31  
32 =head1 SYNOPSIS
33  
34 Retrives details from a CRL file and presents them as accessor methods.
35  
36 Calls the openssl binary and parses the output to get its job done.
37  
38 =cut
39  
40  
41 ###############################################################################
42 # Library Dependencies
43  
44 use EJBCA::CrlPublish::CrlInfo::Parse;
45 use EJBCA::CrlPublish::Logging;
46  
47 our $VERSION = '0.60';
48  
49  
50 ###############################################################################
51  
52 =head1 CONSTRUCTOR
53  
54 =head2 EJBCA::CrlPublish::CrlInfo->new( $crlFile )
55  
56 Argument must be a path to a plain, readable CRL file in DER or PEM format.
57  
58 Returns a blessed, populated object reference, or undef on failure.
59  
60 =cut
61  
62 sub new {
63 my ( $class, $crlFile ) = @_;
64  
65 bless my $self = {}, $class;
66  
67 msgDebug "Analyzing CRL file $crlFile";
68  
69 $self->crlFile( $crlFile );
70  
71 unless ( $self->importIssuerDn ) {
72 msgError "Could not parse issuerDn from CRL.";
73 return undef;
74 }
75  
76 unless ( $self->importIssuingUrl ) {
77 msgError "Could not retrieve CRL information.";
78 return undef;
79 };
80  
81 msgDebug "CRL file is in ", $self->crlFormat, " format.";
82  
83 return $self;
84 }
85  
86 sub retrieve {
87 my ( $class, $crlFileHandle ) = @_;
88  
89 bless my $self = {}, $class;
90  
91 msgDebug "Analyzing remote CRL file";
92  
93 my $t = EJBCA::CrlPublish::CrlInfo::Parse->new( $crlFileHandle );
94  
95 unless ( $t ) {
96 msgError "Failed to parse CRL.";
97 return undef;
98 }
99  
100 unless ( $t->apply( $self ) ) {
101 msgError "Failed to apply parsed CRL values.";
102 return undef;
103 }
104  
105 return $self;
106 }
107  
108  
109 ###############################################################################
110 # Data Import Methods
111  
112 sub importIssuerDn {
113 my ( $self ) = @_;
114 my ( $f, $s );
115  
116 my $crlFile = $self->crlFile;
117  
118 $f = 'openssl crl -issuer -inform %s -noout -in "%s" 2>/dev/null';
119  
120 $s = sprintf $f, 'DER', $crlFile;
121 return 1 if $self->tryImportIssuerDn( 'DER', $s );
122  
123 $s = sprintf $f, 'PEM', $crlFile;
124 return 1 if $self->tryImportIssuerDn( 'PEM', $s );
125  
126 msgError "Invalid CRL file '$crlFile'\n";
127  
128 return 0;
129 }
130  
131 sub tryImportIssuerDn {
132 my ( $self, $crlFormat, $string ) = @_;
133  
134 return 0 unless my $rawIssuer = `$string`;
135 chomp $rawIssuer;
136  
137 $self->crlFormat( $crlFormat );
138  
139 $rawIssuer =~ s/^issuer=\///;
140 $rawIssuer =~ s/\//,/g;
141  
142 $self->issuerDn( $rawIssuer );
143  
144 return 1;
145 }
146  
147 sub importIssuingUrl {
148 my ( $self ) = @_;
149  
150 my $s = 'openssl crl'
151 . ' -in ' . $self->crlFile
152 . ' -inform ' . $self->crlFormat
153 . ' -noout -text'
154 . ' 2> /dev/null';
155  
156 my $fh;
157 unless ( open $fh, "$s |" ) {
158 msgError "open(openssl|): $!";
159 return undef;
160 }
161  
162 my $t = EJBCA::CrlPublish::CrlInfo::Parse->new( $fh );
163  
164 unless ( $t ) {
165 msgError "Failed to parse CRL.";
166 return undef;
167 }
168  
169 unless ( $t->apply( $self ) ) {
170 msgError "Failed to apply parsed CRL values.";
171 return undef;
172 }
173  
174 return 1;
175 }
176  
177  
178 ###############################################################################
179  
180 =head1 ACCESSOR METHODS
181  
182 =head2 $self->crlFile
183  
184 Returns the CRL filename supplied to the constructor.
185  
186 =cut
187  
188 sub crlFile {
189 my ( $self, $crlFile ) = @_;
190  
191 if ( defined $crlFile ) {
192 $self->{crlFile} = $crlFile;
193 }
194  
195 return $self->{crlFile};
196 }
197  
198 =head2 $self->crlFormat
199  
200 Returns 'PEM' or 'DER'.
201  
202 =cut
203  
204 sub crlFormat {
205 my ( $self, $crlFormat ) = @_;
206  
207 if ( defined $crlFormat ) {
208 $self->{crlFormat} = $crlFormat;
209 }
210  
211 return $self->{crlFormat};
212 }
213  
214 =head2 $self->crlNumber
215  
216 Returns the integer crlNumber from the CRL.
217  
218 =cut
219  
220 sub crlNumber {
221 my ( $self, $crlNumber ) = @_;
222  
223 if ( defined $crlNumber ) {
224 $self->{crlNumber} = $crlNumber;
225 }
226  
227 return $self->{crlNumber};
228 }
229  
230 =head2 $self->issuerDn
231  
232 Returns the CRL issuer distinguished name.
233  
234 =cut
235  
236 sub issuerDn {
237 my ( $self, $issuerDn ) = @_;
238  
239 if ( defined $issuerDn ) {
240 $self->{issuerDn} = $issuerDn;
241 }
242  
243 return $self->{issuerDn};
244 }
245  
246 =head2 $self->issuingFile
247  
248 Returns the file portion of the issuing distribution point URL.
249  
250 =cut
251  
252 sub issuingFile {
253 my ( $self, $issuingFile ) = @_;
254  
255 if ( defined $issuingFile ) {
256 $self->{issuingFile} = $issuingFile;
257 }
258  
259 return $self->{issuingFile};
260 }
261  
262 =head2 $self->issuingPath
263  
264 Returns the path portion of the issuing distribution point URL.
265  
266 =cut
267  
268 sub issuingPath {
269 my ( $self, $issuingPath ) = @_;
270  
271 if ( defined $issuingPath ) {
272 $self->{issuingPath} = $issuingPath;
273 }
274  
275 return $self->{issuingPath};
276 }
277  
278 =head2 $self->issuingHost
279  
280 Returns the host portion of the issuing distribution point URL.
281  
282 =cut
283  
284 sub issuingHost {
285 my ( $self, $issuingHost ) = @_;
286  
287 if ( defined $issuingHost ) {
288 $self->{issuingHost} = $issuingHost;
289 }
290  
291 return $self->{issuingHost};
292 }
293  
294 =head2 $self->issuingUrl
295  
296 Returns the entire issuing distribution point URL.
297  
298 =cut
299  
300 sub issuingUrl {
301 my ( $self, $issuingUrl ) = @_;
302  
303 if ( defined $issuingUrl ) {
304 $self->{issuingUrl} = $issuingUrl;
305 }
306  
307 return $self->{issuingUrl};
308 }
309  
310  
311 ###############################################################################
312  
313 =head1 AUTHOR
314  
315 Kevin Cody-Little <kcody@cpan.org>
316  
317 =cut
318  
319  
320 ###############################################################################
321 ####################################### EOF ###################################
322 ###############################################################################
323 1;