1## OpenCA::CRR
2##
3## Copyright (C) 1998-1999 Massimiliano Pala (madwolf@openca.org)
4## All rights reserved.
5##
6## This library is free for commercial and non-commercial use as long as
7## the following conditions are aheared to.  The following conditions
8## apply to all code found in this distribution, be it the RC4, RSA,
9## lhash, DES, etc., code; not just the SSL code.  The documentation
10## included with this distribution is covered by the same copyright terms
11##
12## Copyright remains Massimiliano Pala's, and as such any Copyright notices
13## in the code are not to be removed.
14## If this package is used in a product, Massimiliano Pala should be given
15## attribution as the author of the parts of the library used.
16## This can be in the form of a textual message at program startup or
17## in documentation (online or textual) provided with the package.
18##
19## Redistribution and use in source and binary forms, with or without
20## modification, are permitted provided that the following conditions
21## are met:
22## 1. Redistributions of source code must retain the copyright
23##    notice, this list of conditions and the following disclaimer.
24## 2. Redistributions in binary form must reproduce the above copyright
25##    notice, this list of conditions and the following disclaimer in the
26##    documentation and/or other materials provided with the distribution.
27## 3. All advertising materials mentioning features or use of this software
28##    must display the following acknowledgement:
29##    "This product includes OpenCA software written by Massimiliano Pala
30##     (madwolf@openca.org) and the OpenCA Group (www.openca.org)"
31## 4. If you include any Windows specific code (or a derivative thereof) from
32##    some directory (application code) you must include an acknowledgement:
33##    "This product includes OpenCA software (www.openca.org)"
34##
35## THIS SOFTWARE IS PROVIDED BY OPENCA DEVELOPERS ``AS IS'' AND
36## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38## ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45## SUCH DAMAGE.
46##
47## The licence and distribution terms for any publically available version or
48## derivative of this code cannot be changed.  i.e. this code cannot simply be
49## copied and put under another distribution licence
50## [including the GNU Public Licence.]
51##
52package OpenCA::CRR;
53
54$VERSION = '0.0.2';
55
56my %params = {
57	crr => undef,
58	parsedCRR => undef,
59	signature => undef,
60	body => undef,
61};
62
63sub new {
64	my $that = shift;
65	my $class = ref($that) || $that;
66
67	my $self = {
68		%params,
69	};
70
71	bless $self, $class;
72
73	my $keys = { @_ };
74
75	$self->{crr} = $keys->{DATA} || $_[0];
76	$self->{parsedCRR} = $self->getParsed( $self->{crr} );
77
78	return if ( (not $self->{crr}) or (not $self->{parsedCRR}) );
79
80	$self->{signature} = $self->{parsedCRR}->{SIGNATURE};
81	$self->{body} = $self->{parsedCRR}->{BODY};
82
83	return $self;
84}
85sub parseCRR {
86	my $self = shift;
87	my @keys = @_;
88
89	my $crr = $keys[0];
90	my $beginCRR = "-----BEGIN CRR-----";
91	my $endCRR   = "-----END CRR-----";
92	my $beginSig = "-----BEGIN PKCS7-----";
93	my $endSig   = "-----END PKCS7-----";
94
95	my $line, $dn, $serial, $notBefore, $notAfter, $issuer;
96	my $signature = "";
97	my $body = "";
98	my $isSignature = 0;
99	my $isCRR = 0;
100
101	return if (not $crr);
102
103	my @lines = split ( /\n/, $crr );
104	foreach $line ( @lines ) {
105		$isCRR = 1 if( $line =~ /$beginCRR/ );
106		$isCRR = 0 if( $line =~ /$endCRR/ );
107
108		$isSignature = 1 if( $line =~ /$beginSig/ );
109		$isSignature = 0 if( $line =~ /$endSig/ );
110
111		if( $isCRR ) {
112			$body .= "$line\n";
113
114			if ($line =~ /Submitted on:/) {
115				( $date ) =
116					( $line =~ /Submitted on:[\s]*(.*)/i );
117			}
118
119			if ($line =~ /DN:/) {
120				( $dn ) = ( $line =~ /DN:[\s]*(.*)/ );
121			}
122
123                	if ($line =~ /Issued by:/) {
124                    		($issuer) =
125					( $line=~ /Issued by:[\s]*(.*)/i );
126                	};
127
128                	if ($line =~ /Not After[\s]*:/) {
129                    		($notAfter) =
130					($line =~ /Not After:[\s]*(.*)/i );
131                	};
132                	if ($line =~ /Not Before:/) {
133                        	($notBefore) =
134					( $line=~ /Not Before:[\s]*(.*)/i );
135                	};
136
137                	if ($line =~ /Serial:/) {
138                        	($serial) =
139					( $line =~ /Serial:[\s]*([0-9A-F]+)/i);
140
141				if( length( $serial ) % 2 ) {
142					$serial = "0" . $serial;
143				}
144                	}
145
146		} elsif ( $isSignature ) {
147			$signature .= "$line\n";
148		}
149	}
150
151	my $ret = {
152		    SUBMIT_DATE => $date,
153		    BODY => $body,
154		    SIGNATURE => $signature,
155                    CERTIFICATE_DN => $dn,
156                    CERTIFICATE_NOT_BEFORE => $notbefore,
157                    CERTIFICATE_NOT_AFTER => $notafter,
158                    CERTIFICATE_SERIAL => $serial,
159                    CERTIFICATE_ISSUER => $issuer,
160        };
161
162	return $ret;
163}
164
165sub getParsed {
166	my $self = shift;
167
168	return if ( not $self->{parsedCRR} );
169	return $self->{parsedCRR};
170}
171
172sub getSignature {
173	my $self = shift;
174
175	return if ( not $self->{signature} );
176	return $self->{signature};
177}
178
179sub getBody {
180	my $self = shift;
181
182	return if ( not $self->{body} );
183	return $self->{body};
184}
185
186sub getCRR {
187	my $self = shift;
188
189	return if ( not $self->{crr} );
190	return $self->{crr};
191}
192
193
1941;
195__END__
196# Below is the stub of documentation for your module. You better edit it!
197
198=head1 NAME
199
200OpenCA::CRR - Perl extention to handle CRR objects.
201
202=head1 SYNOPSIS
203
204  use OpenCA::CRR;
205
206=head1 DESCRIPTION
207
208This class handles CRR (Certificate Revoking Request) objects. Them can
209be signed or not depending on the implementation. CRR objects begin and
210end with boundaries:
211
212	-----BEGIN CRR-----
213	-----END CRR-----
214
215Currently implemented functions are:
216
217	new          - Creates a new instance of the class.
218	getParsed    - Returns a parsed version of the object.
219	getSignature - Returns the signature (if present).
220	getBody      - Get Signed Text (boundaries included).
221	getCRR	     - Returns passed CRR (sig. incl.).
222
223=head1 FUNCTIONS
224
225=head2 sub new () - Creates a new instance of the class.
226
227	This function creates a new instance of the class. You have
228	to provide a valid CRR data as argument.
229
230	EXAMPLE:
231
232		my $CRR = new OpenCA::CRR( $crrData );
233
234=head2 sub getParsed () - Returns a parsed CRR.
235
236	This function returns a parsed CRR as an HASH object. The
237	returned object has the following structure:
238
239		my $ret = {
240		    SUBMIT_DATE => $date,
241		    BODY => $body,
242		    SIGNATURE => $signature,
243                    CERTIFICATE_DN => $dn,
244                    CERTIFICATE_NOT_BEFORE => $notbefore,
245                    CERTIFICATE_NOT_AFTER => $notafter,
246                    CERTIFICATE_SERIAL => $serial,
247                    CERTIFICATE_ISSUER => $issuer,
248        	};
249
250=head2 sub getSignature() - Returns signature.
251
252	Use this function to retrieve the signature. Remember the
253	signature is intended to be PKCS7 and returned value includes
254	boundaries.
255
256	EXAMPLE:
257
258		print $CRR->getSignature();
259
260
261
262
263=head1 AUTHOR
264
265Massimiliano Pala <madwolf@openca.org>
266
267=head1 SEE ALSO
268
269perl(1).
270
271=cut
272