| 1 | package EJBCA::CrlPublish::CrlInfo::Parse; | ||
| 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::Parse | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | Parses the openssl command line tool's output to glean details about the CRL. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | |||
| 39 | ############################################################################### | ||
| 40 | # Library Dependencies | ||
| 41 | |||
| 42 | use EJBCA::CrlPublish::Logging; | ||
| 43 | |||
| 44 | our $VERSION = '0.60'; | ||
| 45 | |||
| 46 | |||
| 47 | ############################################################################### | ||
| 48 | |||
| 49 | =head1 CONSTRUCTOR | ||
| 50 | |||
| 51 | =head2 EJBCA::CrlPublish::CrlInfo::Publish->new( $fileHandle ) | ||
| 52 | |||
| 53 | Argument must be an opened filehandle to openssl's output. | ||
| 54 | |||
| 55 | Returns a blessed, populated object reference, or undef on failure. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my ( $class, $fileHandle ) = @_; | ||
| 61 | |||
| 62 | bless my $self = {}, $class; | ||
| 63 | |||
| 64 | while ( my $txt = <$fileHandle> ) { | ||
| 65 | chomp( $txt ); | ||
| 66 | return undef unless $self->parse( $txt ); | ||
| 67 | } | ||
| 68 | |||
| 69 | return $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | ############################################################################### | ||
| 74 | # One Line Input Method | ||
| 75 | |||
| 76 | sub parse { | ||
| 77 | my ( $self, $txt ) = @_; | ||
| 78 | |||
| 79 | $txt =~ s/^\s+//; | ||
| 80 | $txt =~ s/\s+$//; | ||
| 81 | |||
| 82 | if ( my $value = $self->{_nextValue} ) { | ||
| 83 | delete $self->{_nextValue}; | ||
| 84 | $self->{$value} = $txt; | ||
| 85 | msgDebug "crlInfo parser found $value = $txt"; | ||
| 86 | goto success; | ||
| 87 | } | ||
| 88 | |||
| 89 | if ( my $check = $self->{_nextCheck} ) { | ||
| 90 | delete $self->{_nextCheck}; | ||
| 91 | unless ( $txt eq $check ) { | ||
| 92 | msgError "CrlInfo parser expected $check, got $txt"; | ||
| 93 | return 0; | ||
| 94 | } | ||
| 95 | $self->{_lastValue} = $txt; | ||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | if ( $txt =~ /Issuer:/ ) { | ||
| 100 | $txt =~ s/^Issuer: \///; | ||
| 101 | $txt =~ s/\//,/g; | ||
| 102 | $self->{issuerDn} = $txt; | ||
| 103 | msgDebug "crlInfo parser found issuerDn = $txt"; | ||
| 104 | } | ||
| 105 | |||
| 106 | if ( $txt =~ /X509v3 CRL Number:/ ) { | ||
| 107 | $self->{_nextValue} = 'crlNumber'; | ||
| 108 | goto success; | ||
| 109 | } | ||
| 110 | |||
| 111 | if ( $txt =~ /X509v3 Issuing Distr(i|u)bution Point/ ) { | ||
| 112 | $self->{_nextCheck} = 'Full Name:'; | ||
| 113 | goto success; | ||
| 114 | } | ||
| 115 | |||
| 116 | if ( $txt =~ /URI:/ ) { | ||
| 117 | my $last = $self->{_lastValue}; | ||
| 118 | unless ( $last and $last eq 'Full Name:' ) { | ||
| 119 | msgError "CrlInfo encountered URI out of place."; | ||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | $self->parseIssuingUrl( $txt ); | ||
| 123 | msgDebug "crlInfo parser found $txt"; | ||
| 124 | goto success; | ||
| 125 | } | ||
| 126 | |||
| 127 | success: | ||
| 128 | delete $self->{_lastValue}; | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | ############################################################################### | ||
| 134 | # Pick Apart the Issuing URL | ||
| 135 | |||
| 136 | sub parseIssuingUrl { | ||
| 137 | my ( $self, $whole ) = @_; | ||
| 138 | |||
| 139 | my ( $dum0, $prot, $unc ) = split /:/, $whole, 3; | ||
| 140 | |||
| 141 | my ( $dum1, $dum2, $host, $path ) = split /\//, $unc, 4; | ||
| 142 | |||
| 143 | my @part = split /\//, $path; | ||
| 144 | my $file = pop @part; | ||
| 145 | $path = join( '/', @part ); | ||
| 146 | |||
| 147 | $self->{issuingFile} = $file; | ||
| 148 | $self->{issuingPath} = $path; | ||
| 149 | $self->{issuingHost} = $host; | ||
| 150 | $self->{issuingUrl} = $prot . ';' . $unc; | ||
| 151 | |||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | ############################################################################### | ||
| 157 | # Push Contents Onto Object | ||
| 158 | |||
| 159 | sub apply { | ||
| 160 | my ( $self, $target ) = @_; | ||
| 161 | |||
| 162 | foreach my $key ( keys %$self ) { | ||
| 163 | next if $key =~ /^_/; | ||
| 164 | $target->$key( $self->{$key} ); | ||
| 165 | } | ||
| 166 | |||
| 167 | return 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | ############################################################################### | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Kevin Cody-Little <kcody@cpan.org> | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | |||
| 180 | ############################################################################### | ||
| 181 | ####################################### EOF ################################### | ||
| 182 | ############################################################################### | ||
| 183 | 1; |