lib/Net/SSLeay/CA.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« script/san.pl ↑ Summary lib/Net/SSLeay/CA/CSR.pm »
1 0 ▶use Object::Pad ':experimental(:all)';
Subroutine BEGIN: called CC=1 SCAR=2.6
2  
3 package Net::SSLeay::CA;
4  
5 class Net::SSLeay::CA : does(Net::SSLeay::CA::Config) :
6 does(Net::SSLeay::CA::Base);
7  
8 use Path::Tiny;
9 use Net::SSLeay;
10 use Cwd;
11 use Const::Fast;
12 use List::Util 'first';
13 use IPC::Nosh;
14  
15 use utf8;
16 use v5.40;
17  
18 our $VERSION = "0.01";
19  
20 field $OPENSSL = [];
21 field $REQ = [];
22 field $NEWCERT = "";
23 field $env;
24 field $what;
25 field $extra;
26 field $CA = [];
27 field $POLICY = [];
28 field $CATOP = $$env{catop};
29 field $CAKEY = $$env{cakey};
30 field $CACERT;
31 field $CADAYS;
32 field $CACRL;
33 field $EXTENSIONS = [];
34 field $CAREQ;
35 field $PKCS12 = [];
36 field $NEWP12;
37 field $NEWKEY;
38 field $NEWREQ;
39 field $VERIFY;
40 field $X509;
41  
42 field $ret : reader;
43  
44 method touch ( $file, %opts ) {
45 $opts{iolayer} //= '';
46 $opts{close} //= 1;
47  
48 open my $fh, ">$opts{iolayer}", $file;
49 close $fh if $opts{close};
50 path($file);
51 }
52  
53 # See if reason for a CRL entry is valid; exit if not.
54 method crl_reason_ok ($r) {
55 if ( $r eq 'unspecified'
56 || $r eq 'keyCompromise'
57 || $r eq 'CACompromise'
58 || $r eq 'affiliationChanged'
59 || $r eq 'superseded'
60 || $r eq 'cessationOfOperation'
61 || $r eq 'certificateHold'
62 || $r eq 'removeFromCRL' )
63 {
64 return 1;
65 }
66 warn "Invalid CRL reason; must be one of:\n";
67 warn " unspecified, keyCompromise, CACompromise,\n";
68 warn " affiliationChanged, superseded, cessationOfOperation\n";
69 warn " certificateHold, removeFromCRL";
70  
71 1;
72 }
73  
74 method copy_pemfile ( $infile, $outfile, $bound, %opts ) {
75 my $found = 0;
76  
77 $opts{iolayer} //= "";
78  
79 open( my $IN, '<', $opts{iolayer}, $infile )
80 || Net::SSLeay::CA::err( "Cannot open '$infile' for reading: $!", $? );
81 open( my $OUT, '>', "$outfile" )
82 || Net::SSLeay::CA::err( "Cannot write to '$outfile': $!", $? );
83  
84 while ( my $line = <$IN> ) {
85 $found = 1 if $line =~ /^-----BEGIN.*$bound/;
86 print $OUT $line if $found;
87 $found = 2, last if $line = !/^-----END.*$bound/;
88 }
89  
90 close $IN;
91 close $OUT;
92  
93 $found == 2 ? 0 : 1;
94 }
95  
96 method run ( $cmd, %opts ) {
97 $App::OpenSSL::CA::run::read_stdin //= 1;
98 my $read_stdin = $opts{stdin} // $App::OpenSSL::CA::run::read_stdin // 1;
99  
100 my $bin = shift @$cmd;
101 say "====\n$bin " . join ' ', @$cmd if $$env{verbose};
102  
103 my $run3ret = run3(
104 [ $bin, @$cmd ],
105 (
106 $read_stdin == 1 ? undef
107 : $read_stdin == 0 ? \undef
108 : undef
109 ),
110  
111 $opts{outh} // undef,
112 $opts{errh} // undef
113 );
114  
115 my $status = $? // 0;
116 say "==> $status\n====" if $$env{verbose};
117  
118 $status >> 8;
119 }
120  
121 method newcert ( $certout, $keyout, %opts ) {
122 $self->run(
123 [
124 @$REQ, qw(-new -x509 -keyout),
125 $keyout, "-out",
126 $certout, $opts{days},
127 $$extra{req}->@*
128 ]
129 );
130 }
131  
132 method precert ( $certout, $keyout, %opts ) {
133  
134 # create a pre-certificate
135 $ret = $self->run(
136 [
137 @$REQ, qw(-x509 -precert -keyout),
138 $keyout, "-out",
139 $certout, ( $opts{days} // $$env{days} ),
140 $$extra{req}->@*
141 ]
142 );
143  
144 say "Pre-cert is in $certout, private key is in $keyout" if $ret == 0;
145 }
146  
147 method newreq ( $csrout, $keyout ) {
148 my ($nodes) = ( $what =~ /^\-newreq(\-nodes)?$/ );
149  
150 # create a certificate request
151 $ret = $self->run(
152 [
153 @$REQ, "-new", ( defined $1 ? ( $1, ) : () ),
154 "-keyout", $keyout, "-out", $csrout, $$extra{req}->@*
155 ]
156 );
157  
158 say "Request is in $csrout, private key is in $keyout" if $ret == 0;
159 }
160  
161 method newca ( $CAcertout, $CAkeyout, %opts ) {
162  
163 # create the directory hierarchy
164 my @dirs = (
165 "$CATOP", "$CATOP/certs",
166 "$CATOP/crl", "$CATOP/newcerts",
167 "$CATOP/private"
168 );
169  
170 if (
171 my $fileexists =
172 first { -f $_ } map { "$CATOP/$_" } qw(index.txt serial)
173 )
174 {
175 Net::SSLeay::CA::err(
176 "'$fileexists' exists.\nRemove old sub-tree to proceed.", $? );
177 }
178  
179 foreach my $d (@dirs) {
180 -d $d
181 ? warn "Directory $d exists"
182 : mkdir $d
183 or Net::SSLeay::CA::err(
184 "Can't make directory at $d:\n> mkdir exited with $? - $!", $? );
185 }
186  
187 $self->touch("$CATOP/index.txt");
188  
189 open my $out, '>', "$CATOP/crlnumber";
190 say $out "01";
191 close $out;
192  
193 # ask user for existing CA certificate
194 say "CA certificate filename (or enter to create)";
195  
196 my $FILE;
197  
198 $FILE = "" unless defined( $FILE = <STDIN> );
199 $FILE =~ s{\R$}{};
200  
201 if ( $FILE ne "" ) {
202 $self->copy_pemfile( "$CATOP/$FILE", "$CATOP/private/$CAKEY",
203 "PRIVATE" );
204 $self->copy_pemfile( "$CATOP/$FILE", "$CATOP/$CACERT", "CERTIFICATE" );
205 }
206 else {
207 say "Making CA certificate...";
208  
209 my $ret = $self->run(
210 [
211 @$REQ, qw(-new -keyout),
212 "$CATOP/private/$CAKEY", "-out",
213 "$CATOP/$CAREQ", $$extra{req}->@*
214 ]
215 );
216  
217 warn $@ if $? != 0;
218  
219 $ret = $self->run(
220 [
221 @$CA, qw(-create_serial -out),
222 "$CATOP/$CACERT", @$CADAYS,
223 qw(-batch -keyfile), "$CATOP/private/$CAKEY",
224 "-selfsign", @$EXTENSIONS,
225 "-infiles", "$CATOP/$CAREQ",
226 $$extra{ca}->@*
227 ]
228 );
229  
230 warn $@ if $? != 0;
231 say "CA certificate is in $CATOP/$CACERT" if $? == 0;
232 }
233 }
234  
235 #elsif ( $WHAT eq '-pkcs12' ) {
236 method pkcs12 {
237 my $cname = $ARGV[0];
238 $cname = "My Certificate" unless defined $cname;
239  
240 $ret = $self->run(
241 [
242 @$PKCS12, "-in",
243 $NEWCERT, "-inkey",
244 $NEWKEY, "-certfile",
245 "$CATOP/$CACERT", "-out",
246 $NEWP12, qw(-export -name),
247 $cname, $$extra{pkcs12}->@*
248 ]
249 );
250  
251 say "PKCS#12 file is in $NEWP12" if $ret == 0;
252 }
253  
254 method xsign {
255 $ret =
256 $self->run( [ @$CA, @$POLICY, "-infiles", $NEWREQ, $$extra{ca}->@* ] );
257 }
258  
259 method sign {
260 $ret = $self->run(
261 [
262 @$CA, @$POLICY, "-out", $NEWCERT,
263 "-infiles", $NEWREQ, $$extra{ca}->@*
264 ]
265 );
266  
267 say "Signed certificate is in $NEWCERT" if $ret == 0;
268 }
269  
270 method signCA {
271 $ret = $self->run(
272 [
273 @$CA, @$POLICY, "-out", $NEWCERT,
274 @$EXTENSIONS, "-infiles", $NEWREQ, $$extra{ca}->@*
275 ]
276 );
277  
278 say "Signed CA certificate is in $NEWCERT" if $ret == 0;
279 }
280  
281 method signcert {
282 $ret = $self->run(
283 [
284 @$X509, qw(-x509toreq -in),
285 $NEWREQ, "-signkey",
286 $NEWKEY, qw(-out tmp.pem),
287 $$extra{x509}->@*
288 ]
289 );
290 $ret = $self->run(
291 [
292 @$CA, @$POLICY,
293 "-out", $NEWCERT,
294 qw(-infiles tmp.pem), $$extra{ca}->@*
295 ]
296 ) if $ret == 0;
297  
298 say "Signed certificate is in $NEWCERT" if $ret == 0;
299 }
300  
301 method verify {
302 my @files = @ARGV ? @ARGV : ($NEWCERT);
303  
304 foreach my $file (@files) {
305 my $status = $self->run(
306 [
307 @$VERIFY, "-CAfile",
308 "$CATOP/$CACERT", $file,
309 $$extra{verify}->@*
310 ]
311 );
312 $ret = $status if $status != 0;
313 }
314 }
315  
316 method crl {
317 $ret =
318 $self->run(
319 [ @$CA, qw(-gencrl -out), "$CATOP/crl/$CACRL", $$extra{ca}->@* ] );
320 say "Generated CRL is in $CATOP/crl/$CACRL" if $ret == 0;
321 }
322  
323 method revoke ( $cmake, $crl_reason ) {
324 my $cname = $ARGV[0];
325  
326 if ( !defined $cname ) {
327 say "Certificate filename is required; reason optional.";
328 exit 1;
329 }
330  
331 my @reason;
332 @reason = ( "-crl_reason", $ARGV[1] )
333 if defined $ARGV[1] && $self->crl_reason_ok( $ARGV[1] );
334  
335 $ret = $self->run( [ @$CA, "-revoke", $cname, @reason, $$extra{ca}->@* ] );
336 }
337  
338 method unknown_arg {
339 warn "Unknown arg \"$what\"\n";
340 warn "Use -help for help.\n";
341 exit 1;
342 }
343  
344 =encoding utf-8
345  
346 =head1 NAME
347  
348 Net::SSLeay::CA - It's new $module
349  
350 =head1 SYNOPSIS
351  
352 use Net::SSLeay::CA;
353  
354 =head1 DESCRIPTION
355  
356 Net::SSLeay::CA is ...
357  
358 =head1 LICENSE
359  
360 Copyright (C) Ian P Bradley.
361  
362 This library is free software; you can redistribute it and/or modify
363 it under the same terms as Perl itself.
364  
365 =head1 AUTHOR
366  
367 Ian P Bradley E<lt>ian.bradley@studiocrabapple.comE<gt>
368  
369 =cut
« script/san.pl ↑ Summary lib/Net/SSLeay/CA/CSR.pm »