blib/lib/App/sslmaker.pm

filter: Statementstmt 95.8% 184 / 192 Branchbran 73.8% 62 / 84 Conditioncond 61.3% 27 / 44 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 100% 34 / 34 Podpod 100% 12 / 12 totaltotal 83.3% 325 / 390 CC 65 SCAR 15.0
CC
65
Cov
85%
make_directories
10.3
make_key
7.1
openssl
6.0
CRAP 4.5
« script/sslmaker ↑ Summary
1 package App::sslmaker;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::umask;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use constant DEBUG => $ENV{SSLMAKER_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SSLMAKER_DEBUG'} || 0
l!l
06
MC/DC0%0/1
$ENV{'SSLMAKER_DEBUG'} || 0
$ENV{'SSLMAKER_DEBUG'}
Truth table50%1/2
A
A= $ENV{'SSLMAKER_DEBUG'}
Aresult
11
00
11 6 ▶use constant DEFAULT_BITS => $ENV{SSLMAKER_BITS} || 4096;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SSLMAKER_BITS'} || 4096
l!l
06
MC/DC0%0/1
$ENV{'SSLMAKER_BITS'} || 4096
$ENV{'SSLMAKER_BITS'}
Truth table50%1/2
A
A= $ENV{'SSLMAKER_BITS'}
Aresult
11
00
12 6 ▶use constant DEFAULT_DAYS => $ENV{SSLMAKER_DAYS} || 365;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SSLMAKER_DAYS'} || 365
l!l
06
MC/DC0%0/1
$ENV{'SSLMAKER_DAYS'} || 365
$ENV{'SSLMAKER_DAYS'}
Truth table50%1/2
A
A= $ENV{'SSLMAKER_DAYS'}
Aresult
11
00
13  
14 6 our $VERSION = '0.21';
15 6 ▶our $OPENSSL = $ENV{SSLMAKER_OPENSSL} || 'openssl';
Condition50%1/2
$ENV{'SSLMAKER_OPENSSL'} || 'openssl'
l!l
06
MC/DC0%0/1
$ENV{'SSLMAKER_OPENSSL'} || 'openssl'
$ENV{'SSLMAKER_OPENSSL'}
Truth table50%1/2
A
A= $ENV{'SSLMAKER_OPENSSL'}
Aresult
11
00
16  
17 6 my @CONFIG_TEMPLATE_KEYS = qw(bits cert crl_days days home key);
18  
19 # heavily inspired by Mojo::Loader::_all()
20 6 my %DATA = do {
21 6 seek DATA, 0, 0;
22 6 my $data = join '', <DATA>;
23 6 $data =~ s/^.*\n__DATA__\r?\n/\n/s;
24 6 $data =~ s/\n__END__\r?\n.*$/\n/s;
25 6 $data = [split /^@@\s*(.+?)\s*\r?\n/m, $data];
26 6 shift @$data; # first element is empty string
27 6 @$data;
28 };
29  
30 # need to be defined up front
31 42 ▶sub openssl {
Subroutine openssl: called CC=6 SCAR=17.9
Pod documented
32 42 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : sub { print STDERR $_[1] if DEBUG == 2 and length $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 6 36 42
33 42 ▶ my $self = ref $_[0] ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 40 42
34 42 my $buf = '';
35  
36 6 ▶ use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶ use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 42 $self->_d("\$ $OPENSSL @_") if DEBUG;
39 42 my $OUT = gensym;
40 42 my $pid = open3(undef, $OUT, $OUT, $OPENSSL => @_);
41  
42 42 while (1) {
43 391 my $l = sysread $OUT, my $read, 8096;
44 391 ▶ croak "$OPENSSL: $!" unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 0 391 391
45 391 ▶ last unless $l;
Branch: 1 branch
BranchTrueFalseTotal
unless $l 42 349 391
46 349 $buf .= $read;
47 }
48  
49 42 waitpid $pid, 0;
50 42 ▶ return $self->$cb($buf) unless $?;
Branch: 1 branch
BranchTrueFalseTotal
unless $? 41 1 42
51 1 croak $buf;
52 }
53  
54 4 ▶sub make_cert {
Subroutine make_cert: called CC=5 SCAR=17.3
Pod documented
55 4 my ($self, $args) = @_;
56 4 ▶ my $asset = $args->{cert} ? Path::Tiny->new($args->{cert}) : Path::Tiny->tempfile;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'cert'} ? : 4 0 4
57  
58 4 local $UMASK = 0222; # make files with mode 444
59 4 ▶ croak 'Parameter "subject" is required' unless my $subject = $self->_render_subject($self->subject, $args->{subject});
Branch: 1 branch
BranchTrueFalseTotal
unless my $subject = $self->_render_subject($self->subject, $args->{"subject"}) 0 4 4
60 0 ▶ openssl qw(req -new -sha256 -x509 -extensions v3_ca), (map { (-addext => $_) } grep {length} @{$args->{ext} || []}),
Condition75%3/4
$args->{'ext'} || []
l!l
04
$args->{'days'} || 365
l!l
31
MC/DC0%0/1
$args->{'ext'} || []
$args->{'ext'}
MC/DC100%1/1
$args->{'days'} || 365
$args->{'days'}
Truth table50%1/2
A
A= $args->{'ext'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'days'}
Aresult
11
00
61 -passin => $self->_passphrase($args->{passphrase}),
62 -days => $args->{days} || DEFAULT_DAYS,
63 -key => $args->{key},
64 4 -out => $asset->path,
65 -subj => $subject;
66  
67 4 return $asset;
68 }
69  
70 2 ▶sub make_crl {
Subroutine make_crl: called CC=3 SCAR=11.3
Pod documented
71 2 my ($self, $args) = @_;
72 2 ▶ my $asset = $args->{crl} ? Path::Tiny->new($args->{crl}) : Path::Tiny->tempfile;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'crl'} ? : 2 0 2
73  
74 2 local $UMASK = 0122; # make files with mode 644
75  
76 openssl qw(ca -gencrl),
77 -keyfile => $args->{key},
78 -cert => $args->{cert},
79 2 ▶ $args->{passphrase} ? (-passin => $self->_passphrase($args->{passphrase})) : (), -out => $asset->path;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'passphrase'} ? : 2 0 2
80  
81 2 return $asset;
82 }
83  
84 8 ▶sub make_csr {
Subroutine make_csr: called CC=5 SCAR=16.1
Pod documented
85 8 my ($self, $args) = @_;
86 8 ▶ my $asset = $args->{csr} ? Path::Tiny->new($args->{csr}) : Path::Tiny->tempfile;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'csr'} ? : 7 1 8
87  
88 8 local $UMASK = 0277; # make files with mode 400
89  
90 8 ▶ croak 'Parameter "subject" is required' unless my $subject = $self->_render_subject($self->subject, $args->{subject});
Branch: 1 branch
BranchTrueFalseTotal
unless my $subject = $self->_render_subject($self->subject, $args->{"subject"}) 0 8 8
91 openssl qw(req -new -sha256), $args->{passphrase} ? (-passin => $self->_passphrase($args->{passphrase})) : (),
92 2 ▶ (map { (-addext => $_) } grep {length} @{$args->{ext} || []}),
Condition100%2/2
$args->{'ext'} || []
l!l
26
MC/DC100%1/1
$args->{'ext'} || []
$args->{'ext'}
Truth table100%2/2
A
A= $args->{'ext'}
Aresult
11
00
93 -key => $args->{key},
94 8 ▶ -out => $asset->path,
Branch: 1 branch
BranchTrueFalseTotal
$args->{'passphrase'} ? : 3 5 8
95 -subj => $subject;
96  
97 8 return $asset;
98 }
99  
100 7 ▶sub make_directories {
Subroutine make_directories: called CC=9 SCAR=23.3
Pod documented
101 7 my ($self, $args) = @_;
102 7 my $home = $self->_home($args);
103 7 my $file;
104  
105 7 $home->mkpath;
106 7 ▶ -w $home or croak "Can't write to $home";
Branch: 1 branch
BranchTrueFalseTotal
-w $home or croak("Can't write to $home") 7 0 7
107 7 mkdir $home->child($_) for qw(certs csr crl newcerts private);
108 7 ▶ chmod 0700, $home->child('private') or croak "Couldn't chmod 0700 'private' in $home";
Branch: 1 branch
BranchTrueFalseTotal
chmod 0700, $home->child("private") or croak("Couldn't chmod 0700 'private' in $home") 7 0 7
109  
110 7 ▶ if ($args->{templates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"templates"}) 7 0 7
111 7 local $UMASK = 0122; # make files with mode 644
112 7 ▶ $self->render_to_file('crlnumber', $file, {}) unless -e ($file = $home->child('crlnumber'));
Branch: 1 branch
BranchTrueFalseTotal
unless -e ($file = $home->child("crlnumber")) 7 0 7
113 7 ▶ $self->render_to_file('index.txt', $file, {}) unless -e ($file = $home->child('index.txt'));
Branch: 1 branch
BranchTrueFalseTotal
unless -e ($file = $home->child("index.txt")) 7 0 7
114 7 ▶ $self->render_to_file('index.txt.attr', $file, {}) unless -e ($file = $home->child('index.txt.attr'));
Branch: 1 branch
BranchTrueFalseTotal
unless -e ($file = $home->child("index.txt.attr")) 7 0 7
115 7 ▶ $self->render_to_file('serial', $file, {}) unless -e ($file = $home->child('serial'));
Branch: 1 branch
BranchTrueFalseTotal
unless -e ($file = $home->child("serial")) 7 0 7
116 }
117  
118 7 return $args->{home}; # TBD, but will be true
119 }
120  
121 12 ▶sub make_key {
Subroutine make_key: called CC=7 SCAR=19.6
Pod documented
122 12 my ($self, $args) = @_;
123 12 ▶ my $asset = $args->{key} ? Path::Tiny->new($args->{key}) : Path::Tiny->tempfile;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'key'} ? : 11 1 12
124 12 my $passphrase;
125  
126 12 local $UMASK = 0277; # make files with mode 400
127  
128 12 ▶ if ($passphrase = $args->{passphrase}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passphrase = $args->{"passphrase"}) 7 5 12
129 7 $passphrase = $self->_passphrase($passphrase);
130 7 ▶ Path::Tiny->new($1)->spew({binmode => ':raw'}, $self->_random_passphrase(64))
Branch: 1 branch
BranchTrueFalseTotal
if $passphrase =~ /^file:(.+)/ and not -e $1 7 0 7
Condition33%1/3
$passphrase =~ /^file:(.+)/ and not -e $1
!ll&&!rl&&r
007
MC/DC0%0/2
$passphrase =~ /^file:(.+)/ and not -e $1
$passphrase =~ /^file:(.+)/ not -e $1
Truth table33%1/3
A and not(B)
A= $passphrase =~ /^file:(.+)/B= not -e $1
ABresult
0X0
100
111
131 if $passphrase =~ m!^file:(.+)! and !-e $1;
132 }
133  
134 openssl 'genrsa', $passphrase ? (-aes256 => -passout => $passphrase) : (),
135 -out => $asset->path,
136 12 ▶ $args->{bits} || DEFAULT_BITS;
Branch: 1 branch
BranchTrueFalseTotal
$passphrase ? : 7 5 12
Condition100%2/2
$args->{'bits'} || 4096
l!l
48
MC/DC100%1/1
$args->{'bits'} || 4096
$args->{'bits'}
Truth table100%2/2
A
A= $args->{'bits'}
Aresult
11
00
137  
138 12 return $asset;
139 }
140  
141 # copy/paste from Mojo::Base::new()
142 17 ▶sub new {
Subroutine new: called CC=4 SCAR=16.1
Pod documented
143 17 my $class = shift;
144 0 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 1 16 17
@_ > 1 ? : 1 0 1
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
145 }
146  
147 49 ▶sub render_to_file {
Subroutine render_to_file: called CC=2 SCAR=6.9
Pod documented
148 49 my $stash = pop;
149 49 my ($self, $name, $path) = @_;
150 49 my $template = $self->_render_template($name, $stash);
151 49 my $asset;
152  
153 49 ▶ $asset = $path ? Path::Tiny->new($path) : Path::Tiny->tempfile;
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 29 20 49
154 49 $asset->spew({binmode => ":raw"}, $template);
155 49 $asset;
156 }
157  
158 2 ▶sub revoke_cert {
Subroutine revoke_cert: called CC=3 SCAR=11.8
Pod documented
159 2 my ($self, $args) = @_;
160 2 my $home = $self->_home($args);
161  
162 2 ▶ local $args->{crl} = $args->{crl} || $home->child('crl.pem');
Condition33%1/3
$args->{'crl'} || $home->child('crl.pem')
l!l&&r!l&&!r
020
MC/DC0%0/2
$args->{'crl'} || $home->child('crl.pem')
$args->{'crl'} $home->child('crl.pem')
Truth table33%1/3
A || B
A= $args->{'crl'}B= $home->child('crl.pem')
ABresult
1X1
011
000
163  
164 openssl qw(ca), $args->{passphrase} ? (-passin => $self->_passphrase($args->{passphrase})) : (),
165 2 ▶ -revoke => $args->{revoke};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'passphrase'} ? : 2 0 2
166  
167 2 return $self->make_crl($args); # TBD, but will be true
168 }
169  
170 7 ▶sub sign_csr {
Subroutine sign_csr: called CC=3 SCAR=11.0
Pod documented
171 7 my ($self, $args) = @_;
172 7 ▶ my $asset = $args->{cert} ? Path::Tiny->new($args->{cert}) : Path::Tiny->tempfile;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'cert'} ? : 6 1 7
173  
174 7 local $UMASK = 0222; # make files with mode 444
175  
176 openssl qw(ca -batch -notext -md sha256),
177 -keyfile => $args->{ca_key},
178 -cert => $args->{ca_cert},
179 -passin => $self->_passphrase($args->{passphrase}),
180 -extensions => $args->{extensions} || 'usr_cert',
181 -out => $asset->path,
182 7 ▶ -in => $args->{csr};
Condition50%1/2
$args->{'extensions'} || 'usr_cert'
l!l
70
MC/DC0%0/1
$args->{'extensions'} || 'usr_cert'
$args->{'extensions'}
Truth table50%1/2
A
A= $args->{'extensions'}
Aresult
11
00
183  
184 7 return $asset;
185 }
186  
187 20 ▶sub subject {
Subroutine subject: called CC=4 SCAR=13.9
Pod documented
188 20 my $self = shift;
189 20 ▶ return $self->{subject} // $ENV{SSLMAKER_SUBJECT} // '' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 14 6 20
Condition100%5/5
($self->{"subject"} // $ENV{"SSLMAKER_SUBJECT"}) // ""
l!l
113
$self->{"subject"} // $ENV{"SSLMAKER_SUBJECT"}
l!l&&r!l&&!r
743
MC/DC100%2/2
($self->{"subject"} // $ENV{"SSLMAKER_SUBJECT"}) // ""
$self->{"subject"} $ENV{"SSLMAKER_SUBJECT"}
Truth table100%3/3
A // B
A= $self->{"subject"}B= $ENV{"SSLMAKER_SUBJECT"}
ABresult
1X1
011
000
190 6 $self->{subject} = $self->_render_subject(@_);
191 6 return $self;
192 }
193  
194 28 ▶sub with_config {
Subroutine with_config: called CC=3 SCAR=11.0
Pod documented
195 28 my ($self, $cb, $args) = @_;
196 28 ▶ my $key = join ':', 'config', map { ($_, $args->{$_} // ''); } @CONFIG_TEMPLATE_KEYS;
Condition100%2/2
$args->{$_} // ""
l!l
7098
MC/DC100%1/1
$args->{$_} // ""
$args->{$_}
Truth table100%2/2
A
A= $args->{$_}
Aresult
11
00
197  
198 28 local $args->{home} = $self->_home($args);
199  
200 {
201 28 local $UMASK = 0177; # read/write for current user
202 28 ▶ $self->{$key} ||= $self->render_to_file('openssl.cnf', $args);
Condition66%2/3
$self->{$key} ||= $self->render_to_file("openssl.cnf", $args)
l!l&&r!l&&!r
9190
MC/DC0%0/2
$self->{$key} ||= $self->render_to_file("openssl.cnf", $args)
$self->{$key} $self->render_to_file("openssl.cnf", $args)
Truth table66%2/3
A ||= B
A= $self->{$key}B= $self->render_to_file("openssl.cnf", $args)
ABresult
1X1
011
000
203 }
204  
205 28 local $ENV{OPENSSL_CONF} = $self->{$key}->path;
206 28 return $self->$cb($args);
207 }
208  
209 2 ▶sub _cat {
Subroutine _cat: called CC=4 SCAR=14.1
210 2 my $self = shift;
211 2 my $dest = pop;
212  
213 2 ▶ open my $DEST, '>', $dest or croak "Couldn't write $dest: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $DEST, ">", $dest or croak("Couldn't write ${dest}: $!") 2 0 2
214 2 local @ARGV = @_;
215 2 print $DEST $_ for <>;
216 2 ▶ close $DEST or croak "Couldn't close $dest: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $DEST or croak("Couldn't close ${dest}: $!") 2 0 2
217 2 return $dest;
218 }
219  
220 35 ▶sub _d {
Subroutine _d: called CC=1 SCAR=3.5
221 35 return 0 unless DEBUG;
222 0 my ($self, $msg) = @_;
223 0 print STDERR "$msg\n";
224 0 return 0;
225 }
226  
227 37 ▶sub _home {
Subroutine _home: called CC=4 SCAR=14.1
228 37 my ($self, $args) = @_;
229 37 ▶ return Path::Tiny->new($args->{home}) if exists $args->{home};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"home"} 22 15 37
230 15 ▶ return Path::Tiny->new($args->{ca_key})->parent(2) if $args->{ca_key};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"ca_key"} 3 12 15
231 12 ▶ return Path::Tiny->new($args->{key})->parent(2) if $args->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"key"} 12 0 12
232 0 croak '$SSLMAKER_HOME is required';
233 }
234  
235 25 ▶sub _parse_subject {
Subroutine _parse_subject: called CC=4 SCAR=13.9
236 25 my ($self, $val) = @_;
237 25 ▶ return $val if ref $val eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" 1 24 25
238  
239 # /C=US/ST=Texas/L=Dallas/O=Company/OU=Department/CN=example.com/emailAddress=admin@example.com
240 # Subject: C = US, ST = Texas, L = Dallas, O = Company, OU = Department, CN = superduper
241 24 ▶ my $re = index($val, '/') == 0 ? qr{/([A-Za-z]+)=([^/]*)} : qr{([A-Za-z]+)\s*=\s*([^,]*)};
Branch: 1 branch
BranchTrueFalseTotal
index($val, '/') == 0 ? : 21 3 24
242 24 my %subject;
243 24 ▶ $subject{$1} = $2 while $val =~ /$re/g;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /$re/g and do { $subject{$1} = $2 } 101 24 125
244 24 return \%subject;
245 }
246  
247 25 ▶sub _passphrase {
Subroutine _passphrase: called CC=4 SCAR=15.6
248 25 my ($self, $phrase) = @_;
249  
250 25 ▶ croak 'Parameter "passphrase" is required' unless defined $phrase and length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phrase and length $phrase 0 25 25
Condition33%1/3
defined $phrase and length $phrase
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $phrase and length $phrase
defined $phrase length $phrase
Truth table33%1/3
A and B
A= defined $phraseB= length $phrase
ABresult
0X0
100
111
251 25 ▶ return croak "SCALAR is not yet supported" if ref $phrase eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $phrase eq "SCALAR" 0 25 25
252 25 return "file:$phrase";
253 }
254  
255 8 ▶sub _random_passphrase {
Subroutine _random_passphrase: called CC=1 SCAR=0.0
256 8 my ($self, $length) = @_;
257 8 my @chr = ('a' .. 'z', 'A' .. 'Z', 0 .. 9);
258 8 join '', map { $chr[rand @chr] } 1 .. $length;
259 }
260  
261 3 ▶sub _read_subject_from_cert {
Subroutine _read_subject_from_cert: called CC=1 SCAR=0.2
262 3 my ($self, $cert) = @_;
263 3 my %subject;
264  
265 # Subject: C = US, ST = Texas, L = Dallas, O = Company, OU = Department, CN = superduper
266 3 ▶ return openssl qw(x509 -noout -text -in), $cert => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
267 3 print STDERR $_[1] if DEBUG == 2 and length $_[1];
268 3 ▶ return $self->_parse_subject($1) if $_[1] =~ m!Subject:\s+(.+)!;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /Subject:\s+(.+)/ 3 0 3
269 3 };
270  
271 0 die qq(Could not read subject from "$cert".);
272 }
273  
274 18 ▶sub _render_subject {
Subroutine _render_subject: called CC=5 SCAR=16.1
275 18 my $self = shift;
276  
277 18 my %subject;
278 18 for my $i (@_) {
279 34 ▶ next unless $i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i 9 25 34
280 25 $self->_d(qq(# Subject from @{[-r $i ? 'file' : 'data']} "$i")) if DEBUG == 2;
281 25 ▶ my $s = -r $i ? $self->_read_subject_from_cert($i) : $self->_parse_subject($i);
Branch: 1 branch
BranchTrueFalseTotal
-r $i ? : 3 22 25
282 25 map { $self->_d(sprintf '- %-12s %s', "$_:", "$s->{$_}") } sort keys %$s if DEBUG == 2;
283 25 $subject{$_} = $s->{$_} for keys %$s;
284 }
285  
286 18 return join '/', '', map {"$_=$subject{$_}"} grep { defined $subject{$_} } qw(C ST L O OU CN emailAddress);
287 }
288  
289 # used in script/sslmaker
290 49 ▶sub _render_template {
Subroutine _render_template: called CC=3 SCAR=11.2
291 49 my ($self, $name, $stash) = @_;
292 49 ▶ my $template = $DATA{$name} // confess "No such template: $name";
Condition50%1/2
$DATA{$name} // confess("No such template: $name")
l!l
490
MC/DC0%0/1
$DATA{$name} // confess("No such template: $name")
$DATA{$name}
Truth table50%1/2
A
A= $DATA{$name}
Aresult
11
00
293 49 ▶ $template =~ s!<%=\s*([^%]+)\s*%>!{eval $1 // confess $@}!ges; # super cheap template parser
Condition50%1/2
eval $1 // confess($@)
l!l
2050
MC/DC0%0/1
eval $1 // confess($@)
eval $1
Truth table50%1/2
A
A= eval $1
Aresult
11
00
294 49 $template;
295 }
296  
297 6 1;
298  
299 =encoding utf8
300  
301 =head1 NAME
302  
303 App::sslmaker - Be your own SSL certificate authority
304  
305 =head1 DESCRIPTION
306  
307 L<App::sslmaker> is a module that provide methods for acting as your own
308 L<CA|http://en.wikipedia.org/wiki/Certificate_authority> (certificate authority).
309 It can creating SSL keys, certificates and signing requests. The methods
310 should have good defaults and "just work", so you don't have to worry about
311 the details. "Just work" depends on safe defaults, which will change when
312 new and more secure standards come along.
313  
314 The openssl commands are based on the instructions from
315 L<https://jamielinux.com/docs/openssl-certificate-authority/>.
316  
317 This module is used by the C<sslmaker> command line application, but can also
318 act as a standalone toolkit.
319  
320 =head1 DISCLAIMER
321  
322 This module is based on tips and tricks from online resources, and has been
323 reviewed by security experts. Even so, the L</AUTHOR> of this application or
324 any parts involved cannot be held responsible for the security of your
325 server, application or other parts that use the files generated by this
326 library.
327  
328 =head1 SYNOPSIS
329  
330 $ sslmaker <action> [options]
331  
332 # 1. Initial CA setup
333 # 1a. The CA admin generates root CA key and certificate
334 $ sslmaker root --subject "/C=US/ST=Texas/L=Dallas/O=Company/OU=Department/CN=superduper"
335  
336 # 1b. The CA admin generates intermediate CA key and certificate
337 # Uses the --subject from root CA by default
338 $ sslmaker intermediate
339  
340 # 2. Client certificate setup
341 # 2a. The client generates a server key and certificate signing request
342 # Can be done on any other server
343 # Uses the --subject from intermediate CA if available
344 $ sslmaker generate <cn>
345 $ sslmaker generate www.example.com
346  
347 # 2b. The client sends the signing request file to the CA admin
348  
349 # 3. CA sign and revoke process
350 # 3a. The CA admin signs the certificate request
351 $ sslmaker sign www.example.com.csr.pem
352 $ sslmaker sign www.example.com.csr.pem [outfile]
353  
354 # 3b. The CA admin sends back the signed certificate which the client can use
355  
356 # 3c. The CA can revoke a certificate
357 $ sslmaker revoke <infile>
358 $ sslmaker revoke /etc/ssl/sslmaker/newcerts/1000.pem
359  
360 # 4. Utility commands
361 # 4a. Create dhparam file
362 $ sslmaker dhparam
363 $ sslmaker dhparam /etc/ssl/sslmaker/dhparam.pem 2048
364  
365 # 4b. Show the manual for App::sslmaker
366 $ sslmaker man
367  
368 =head1 ENVIRONMENT VARIABLES
369  
370 =over 2
371  
372 =item * SSLMAKER_BITS
373  
374 Default bits for a generated certificate. Default is 4096.
375  
376 =item * SSLMAKER_DAYS
377  
378 Default days before expiring a generated certificate. Default is 365.
379  
380 =item * SSLMAKER_DEBUG
381  
382 Setting this to "0" will output less debug information from C<sslmaker>.
383  
384 =item * SSLMAKER_HOME
385  
386 Used by the C<sslmaker> script as default home directory. Default is either
387 "/etc/pki/sslmaker" or "/etc/ssl/sslmaker".
388  
389 Directory structure is:
390  
391 # generated by "sslmaker root"
392 $SSLMAKER_HOME/root/ca.cert.pem
393 $SSLMAKER_HOME/root/ca.key.pem
394 $SSLMAKER_HOME/root/crlnumber
395 $SSLMAKER_HOME/root/index.txt
396 $SSLMAKER_HOME/root/index.txt.attr
397 $SSLMAKER_HOME/root/passphrase
398 $SSLMAKER_HOME/root/serial
399  
400 # generated by "sslmaker intermediate"
401 $SSLMAKER_HOME/certs/ca-chain.cert.pem
402 $SSLMAKER_HOME/certs/ca.cert.pem
403 $SSLMAKER_HOME/certs/ca.csr.pem
404 $SSLMAKER_HOME/private/ca.key.pem
405 $SSLMAKER_HOME/private/passphrase
406 $SSLMAKER_HOME/root/newcerts/1000.pem
407 $SSLMAKER_HOME/crlnumber
408 $SSLMAKER_HOME/index.txt
409 $SSLMAKER_HOME/index.txt.attr
410 $SSLMAKER_HOME/serial
411  
412 # generated by "sslmaker sign"
413 $SSLMAKER_HOME/newcerts/1000.pem
414  
415 # generated by "sslmaker dhparam"
416 $SSLMAKER_HOME/dhparam.pem
417  
418 NOTE! After running "sslmaker intermediate", then it is highly suggested to
419 move "$SSLMAKER_HOME/root/" to a safe location, such as a memory stick. You can
420 revoke any of the child certificates if they are compromised, but if you loose
421 the root key, then all is lost.
422  
423 =item * SSLMAKER_OPENSSL
424  
425 Default to "openssl". Can be set to a custom path if "openssl" is not in
426 C<PATH>.
427  
428 =item * SSLMAKER_SUBJECT
429  
430 Used as default subject, unless specified.
431  
432 =back
433  
434 =head2 SEE ALSO
435  
436 =over 4
437  
438 =item * L<https://jamielinux.com/docs/openssl-certificate-authority/>
439  
440 =item * L<https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs>
441  
442 =item * L<http://en.wikipedia.org/wiki/Certificate_authority>
443  
444 =item * L<Easy RSA|https://github.com/OpenVPN/easy-rsa>
445  
446 =back
447  
448 =head1 METHODS
449  
450 =head2 make_cert
451  
452 $asset = $self->make_cert({
453 key => "/path/to/private/input.key.pem",
454 passphrase => "/path/to/passphrase.txt",
455 days => $number_of_days, # default: 365
456 subject => '/C=NO/ST=Oslo', # optional
457 ext => ["subjectAltName = DNS:example.com"], # optional
458 });
459  
460 This method will generate a SSL certificate using a C<key> generated by
461 L</make_key>. C<passphrase> should match the argument given to L</make_key>.
462 An optional C<subject> can be provided. The subject string will be merged with the
463 L</subject> attribute. C<days> can be used to set how many days the certificate
464 should be valid.
465  
466 The returned C<$asset> is a L<Path::Tiny> object which holds the generated certificate
467 file. It is possible to specify the location of this object by passing on C<cert> to
468 this method.
469  
470 =head2 make_crl
471  
472 $asset = $self->make_crl({
473 key => "/path/to/private/input.key.pem",
474 cert => "/path/to/cefrt/input.cert.pem",
475 passphrase => "/path/to/passphrase.txt", # optional
476 });
477  
478 This method will generate a certificate revocation list (CRL) using a C<key> generated
479 by L</make_key>. C<passphrase> should match the argument given to L</make_key>.
480  
481 The returned C<$asset> is a L<Path::Tiny> object which holds the generated certificate
482 file. It is possible to specify the location of this object by passing on C<crl> to
483 this method.
484  
485 You can inspect the generated asset using the command
486 C<openssl crl -in $crl_asset -text>.
487  
488 See also L</revoke_cert>.
489  
490 =head2 make_csr
491  
492 $asset = $self->make_csr({
493 key => "/path/to/private/input.key.pem",
494 passphrase => "/path/to/passphrase.txt",
495 subject => '/C=NO/ST=Oslo',
496 ext => ["subjectAltName=DNS:example.com"], # optional
497 });
498  
499 This method will generate a SSL certificate signing request using a C<key>
500 generated by L</make_key>. C<passphrase> is only required if the C<key> was
501 generated with a C<passphrase>. An optional C<subject> can be provided.
502 The subject string will be merged with the L</subject> attribute.
503  
504 The returned C<$asset> is a L<Path::Tiny> object which holds the generated
505 signing request file. It is possible to specify the location of this object
506 by passing on C<csr> to this method.
507  
508 =head2 make_directories
509  
510 $self->make_directories({
511 home => "/path/to/pki",
512 templates => 1, # default: false
513 });
514  
515 Used to generate a suitable file structure, which reflect what C<openssl.cnf>
516 expects. Set C<$emplates> to a true value to generate L<files|/render_to_file>.
517  
518 $home/ # need to be writable by current user
519 $home/certs/
520 $home/crl/
521 $home/newcerts/
522 $home/private/ # will have mode 700
523 # optional templates
524 $home/index.txt
525 $home/serial
526  
527 =head2 make_key
528  
529 $asset = $self->make_key({
530 passphrase => "/path/to/passphrase.txt", # optional
531 bits => 8192, # default: 4096
532 });
533  
534 This method will generate a SSL key.
535  
536 The key will be protected with C<passphrase> if given as input. In addition
537 if C<passphrase> does not exist, it will be created with a random passphrase.
538  
539 The returned C<$asset> is a L<Path::Tiny> object which holds the generated key.
540 It is possible to specify the location of this object by passing on C<key> to
541 this method.
542  
543 =head2 new
544  
545 $self = App::sslmaker->new(%args);
546 $self = App::sslmaker->new(\%args);
547  
548 Object constructor.
549  
550 =head2 openssl
551  
552 $self->openssl(@args);
553 $self->openssl(@args, sub { ... });
554 App::sslmaker::openssl(@args);
555 App::sslmaker::openssl(@args, sub { ... });
556  
557 Used to run the application C<openssl>. The callback defined at the end is
558 optional, but will be called with the complete output from the openssl
559 command. C<$?> is also available for inspection.
560  
561 The C<openssl> application must exist in path or defined by setting the
562 C<SSLMAKER_OPENSSL> environment variable before loading this module.
563  
564 =head2 render_to_file
565  
566 $asset = $self->render_to_file($template, \%stash);
567 $asset = $self->render_to_file($template, $out_file, \%args);
568  
569 This method can render a C<$template> to either a temp file or C<$out_file>.
570 The C<$template> will have access to C<%stash> and C<$self>.
571  
572 See L</TEMPLATES> for list of valid templates.
573  
574 =head2 revoke_cert
575  
576 $self->with_config(
577 revoke_cert => {
578 key => "/path/to/private/ca.key.pem",
579 cert => "/path/to/certs/ca.cert.pem",
580 crl => "/path/to/crl.pem",
581 revoke => "/path/to/newcerts/1000.pem",
582 },
583 );
584  
585 This method can revoke a certificate. It need to be run either with
586 C<OPENSSL_CONF> or inside L</with_config>.
587  
588 =head2 sign_csr
589  
590 $asset = $self->sign_csr({
591 csr => "/path/to/certs/input.csr.pem",
592 ca_key => "/path/to/private/ca.key.pem",
593 ca_cert => "/path/to/certs/ca.cert.pem",
594 passphrase => "/path/to/passphrase.txt",
595 extensions => "v3_ca", # default: usr_cert
596 });
597  
598 This method will sign a C<csr> file generated by L</make_csr>. C<ca_key> and
599 C<passphrase> is the same values as you would provide L</make_key> and
600 C<ca_cert> is the output from L</make_cert>.
601  
602 The returned C<$asset> is a L<Path::Tiny> object which holds the generated
603 certificate. It is possible to specify the location of this object by
604 passing on C<cert> to this method.
605  
606 =head2 subject
607  
608 $self = $self->subject(@subjects);
609 $self = $self->subject("/C=NO/ST=Oslo/L=Oslo/O=Example/OU=Prime/emailAddress=admin@example.com", ...);
610 $str = $self->subject;
611  
612 Holds the default subject field for the certificate. Can be set by passing in a
613 list of subject strings, hashes or paths to certificate files. The list will
614 get merged, soo the last one overrides the one before.
615  
616 =head2 with_config
617  
618 $any = $self->with_config($method => \%args);
619  
620 Used to call a L<method|/METHODS> with a temp L</openssl.cnf>
621 file. The C<%stash> in the template will be constructed from the C<%args>,
622 which is also passed on to the next C<$method>. Example:
623  
624 $asset = $self->with_config(make_key => {
625 home => "/path/to/pki",
626 passphrase => "/path/to/pki/private/passphrase.txt",
627 bits => 8192,
628 });
629  
630 The config file will be removed when C<$self> go out of scope.
631  
632 An alternative to this method is to set the C<OPENSSL_CONF> environment
633 variable before calling C<$method>:
634  
635 local $ENV{OPENSSL_CONF} = "/path/to/openssl.cnf";
636 $asset = $self->make_key({...});
637  
638 =head1 TEMPLATES
639  
640 L</render_to_file> can render these templates, which is bundled with this module:
641  
642 =over 4
643  
644 =item * crlnumber
645  
646 Creates a file which stores the SSL CRL number. If C<n> is present in
647 C<%stash>, it will be used as the start number, which defaults to 1000.
648  
649 =item * index.txt
650  
651 This is currently just an empty file.
652  
653 =item * nginx.config
654  
655 Used to render an example nginx config. C<%stash> should contain C<cert>,
656 C<client_certificate>, C<crl>, C<key>, C<server_name> and C<verify_client>.
657  
658 =item * openssl.cnf
659  
660 Creates a config file for openssl. TODO: Descrive stash values.
661  
662 =item * serial
663  
664 Creates a file which stores the SSL serial number. If C<n> is present in
665 C<%stash>, it will be used as the start number, which defaults to 1000.
666  
667 =back
668  
669 =head1 COPYRIGHT AND LICENCE
670  
671 =head2 Code
672  
673 Copyright (C) Jan Henning Thorsen
674  
675 The code is free software, you can redistribute it and/or modify it under the
676 terms of the Artistic License version 2.0.
677  
678 =head2 Documentation
679  
680 Documentation is licensed under the terms of Creative Commons
681 Attribution-ShareAlike 3.0 Unported license.
682  
683 The documentation is put together by Jan Henning Thorsen, with citations from
684 Jamie Nguyen's website L<https://jamielinux.com/>.
685  
686 =head1 AUTHOR
687  
688 Jan Henning Thorsen - C<jhthorsen@cpan.org>
689  
690 =cut
691  
692 __DATA__
« script/sslmaker ↑ Summary