lib/Web/ID/Certificate/Generator.pm

filter: Statementstmt 87.0% 87 / 100 Branchbran 46.4% 13 / 28 Conditioncond 38.4% 5 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 81.8% 18 / 22 Podpod 100% 1 / 1 totaltotal 72.6% 125 / 172 CC 24 SCAR 32.8
CC
24
Cov
74%
generate
48.6
__ANON__
2.0
__ANON__
2.0
CRAP 26.4
↑ Summary blib/lib/Web/ID/Util.pm »
1 package Web::ID::Certificate::Generator;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $Web::ID::Certificate::Generator::AUTHORITY = 'cpan:TOBYINK';
8 1 $Web::ID::Certificate::Generator::VERSION = '1.927';
9 }
10  
11 1 ▶use Moose::Util qw(apply_all_roles);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use RDF::Trine qw(statement blank iri literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Web::ID::Certificate;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Web::ID::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Web::ID::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub import
23 1 ▶{
Subroutine import: called CC=1 SCAR=0.0
24 1 apply_all_roles("Web::ID::Certificate", __PACKAGE__);
25 }
26  
27 sub _openssl_path
28 5 ▶{
Subroutine _openssl_path: called CC=1 SCAR=0.0
29 5 require Web::ID::Util::FindOpenSSL;
30 5 path( Web::ID::Util::FindOpenSSL::find_openssl() );
31 }
32  
33 sub generate
34 5 ▶{
Subroutine generate: called CC=24 SCAR=38.8
Pod documented
35 5 my ($class, %options) = @_;
36
37 5 ▶ my $openssl = (delete $options{openssl_path}) // $class->_openssl_path;
Condition33%1/3
delete $options{"openssl_path"} // $class->_openssl_path
l!l&&r!l&&!r
050
MC/DC0%0/2
delete $options{"openssl_path"} // $class->_openssl_path
delete $options{"openssl_path"} $class->_openssl_path
Truth table33%1/3
A // B
A= delete $options{"openssl_path"}B= $class->_openssl_path
ABresult
1X1
011
000
38 my $passphrase = (delete $options{passphrase})
39 5 ▶ or confess "need to provide passphrase option";
Branch: 1 branch
BranchTrueFalseTotal
my $passphrase = delete $options{"passphrase"} or confess("need to provide passphrase option") 5 0 5
40 5 ▶ my $key_size = (delete $options{key_size}) // 1024;
Condition50%1/2
delete $options{"key_size"} // 1024
l!l
05
MC/DC0%0/1
delete $options{"key_size"} // 1024
delete $options{"key_size"}
Truth table50%1/2
A
A= delete $options{"key_size"}
Aresult
11
00
41 my $sans = (delete $options{subject_alt_names})
42 5 ▶ or confess "need to provide subject_alt_names option";
Branch: 1 branch
BranchTrueFalseTotal
my $sans = delete $options{"subject_alt_names"} or confess("need to provide subject_alt_names option") 5 0 5
43 5 my $not_after = (delete $options{not_after});
44 my $dest = (delete $options{cert_output})
45 5 ▶ or confess "need to provide cert_output option";
Branch: 1 branch
BranchTrueFalseTotal
my $dest = delete $options{"cert_output"} or confess("need to provide cert_output option") 5 0 5
46 my $rdf_sink = (delete $options{rdf_output})
47 5 ▶ or confess "need to provide rdf_output option";
Branch: 1 branch
BranchTrueFalseTotal
my $rdf_sink = delete $options{"rdf_output"} or confess("need to provide rdf_output option") 5 0 5
48
49 my %subject = (
50 C => delete $options{subject_country},
51 ST => delete $options{subject_region},
52 L => delete $options{subject_locality},
53 O => delete $options{subject_org},
54 CN => delete $options{subject_cn},
55 5 );
56
57 5 ▶ confess "need to provide subject_cn option" unless $subject{CN};
Branch: 1 branch
BranchTrueFalseTotal
unless $subject{"CN"} 0 5 5
58
59 5 ▶ confess "unsupported options: ".(join q(, ), sort keys %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 5 5
60
61 5 ▶ my $days = $not_after
Branch: 1 branch
BranchTrueFalseTotal
$not_after ? : 0 5 5
62 ? $not_after->delta_days( DateTime->coerce('now') )->days
63 : 365;
64
65 5 my $tempdir = path( File::Temp->newdir );
66 5 $tempdir->mkpath;
67
68 5 my $config = $tempdir->child('openssl.cnf')->openw;
69 5 say $config $_ for
70 q([req]),
71 q(default_bits = 1024),
72 q(default_keyfile = privkey.pem),
73 q(distinguished_name = req_distinguished_name),
74 q(x509_extensions = v3_ca),
75 q(prompt = no),
76 q(),
77 q([v3_ca]);
78
79 say $config
80 q(subjectAltName = ) .
81 join q(,),
82 map {
83 5 my $value = $_->value;
84 my $type = {
85 rfc822Name => 'email',
86 uniformResourceIdentifier => 'URI',
87 5 }->{ $_->type };
88 5 ▶ $type ? (join q(:), $type, $value) : ();
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 5 0 5
89 } @$sans;
90
91 5 say $config $_ for
92 q(),
93 q([req_distinguished_name]);
94
95 5 foreach (qw(C ST L O CN))
96 {
97 25 ▶ next unless (defined $subject{$_} and length $subject{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject{$_} and length $subject{$_} 20 5 25
Condition66%2/3
defined $subject{$_} and length $subject{$_}
!ll&&!rl&&r
2005
MC/DC50%1/2
defined $subject{$_} and length $subject{$_}
defined $subject{$_} length $subject{$_}
Truth table66%2/3
A and B
A= defined $subject{$_}B= length $subject{$_}
ABresult
0X0
100
111
98 5 say $config "$_ = ", $subject{$_};
99 }
100
101 5 close $config;
102
103 5 system(
104 $openssl,
105 "req",
106 "-newkey" => "rsa:".$key_size,
107 "-x509",
108 "-days" => $days,
109 "-config" => $tempdir->child('openssl.cnf'),
110 "-out" => $tempdir->child('cert.pem'),
111 "-keyout" => $tempdir->child('privkey.pem'),
112 "-passout" => "pass:".$passphrase,
113 );
114
115 system(
116 $openssl,
117 "pkcs12",
118 "-export",
119 "-in" => $tempdir->child('cert.pem'),
120 "-inkey" => $tempdir->child('privkey.pem'),
121 "-out" => $tempdir->child('cert.p12'),
122 5 ▶ "-name" => sprintf('%s <%s>', ($subject{CN}//'Unnamed'), $sans->[0]->value),
Condition50%1/2
$subject{"CN"} // "Unnamed"
l!l
50
MC/DC0%0/1
$subject{"CN"} // "Unnamed"
$subject{"CN"}
Truth table50%1/2
A
A= $subject{"CN"}
Aresult
11
00
123 "-passin" => "pass:".$passphrase,
124 "-passout" => "pass:".$passphrase,
125 );
126
127 5 ▶ if (ref $dest eq 'SCALAR')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dest eq 'SCALAR') { } 5 0 5
elsif (ref $dest =~ /^IO/) { } 0 0 0
128 {
129 5 $$dest = $tempdir->child('cert.p12')->slurp;
130 }
131 elsif (ref $dest =~ m/^IO/)
132 {
133 0 my $p12 = $tempdir->child('cert.p12')->slurp;
134 0 print $dest $p12;
135 }
136 else
137 {
138 0 my $p12 = $tempdir->child('cert.p12')->slurp;
139 0 my $fh = path($dest)->openw;
140 0 print $fh $p12;
141 }
142
143 5 ▶ my ($on_triple, $on_done) = (sub {}, sub {});
Subroutine __ANON__: not called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
144 5 ▶ if (ref $rdf_sink eq 'SCALAR')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rdf_sink eq 'SCALAR') { } 5 0 5
elsif (builtin::blessed($rdf_sink) and $rdf_sink->isa('RDF::Trine::Model')) { } 0 0 0
145 {
146 5 $$rdf_sink = Model->new;
147 5 ▶ $on_triple = sub { $$rdf_sink->add_statement(statement(@_)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
148 }
149 ▶ elsif (blessed($rdf_sink) and $rdf_sink->isa('RDF::Trine::Model'))
Condition0%0/3
builtin::blessed($rdf_sink) and $rdf_sink->isa("RDF::Trine::Model")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($rdf_sink) and $rdf_sink->isa("RDF::Trine::Model")
builtin::blessed($rdf_sink) $rdf_sink->isa("RDF::Trine::Model")
Truth table0%0/3
A and B
A= builtin::blessed($rdf_sink)B= $rdf_sink->isa("RDF::Trine::Model")
ABresult
0X0
100
111
150 {
151 0 ▶ $on_triple = sub { $rdf_sink->add_statement(statement(@_)) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 }
153 else
154 {
155 0 my $model = Model->new;
156 0 my $fh = path($rdf_sink)->openw;
157 0 ▶ $on_triple = sub { $model->add_statement(statement(@_)) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 ▶ $on_done = sub { "RDF::Trine::Serializer"->new('RDFXML')->serialize_model_to_file($fh, $model) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 }
160
161 5 my $pem = $tempdir->child('cert.pem')->slurp;
162 5 my $cert = $class->new(pem => $pem);
163
164 5 ▶ my $hex = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 5 (my $h = shift->as_hex) =~ s/^0x//;
166 5 $h;
167 5 };
168
169 5 my $k = blank();
170 5 $on_triple->($k, u('rdf:type'), u('cert:RSAPublicKey'));
171 5 $on_triple->($k, u('cert:modulus'), literal($cert->modulus->$hex, undef, uu('xsd:hexBinary')));
172 5 $on_triple->($k, u('cert:exponent'), literal($cert->exponent->bstr, undef, uu('xsd:integer')));
173 5 foreach my $san (@$sans)
174 {
175 5 ▶ next unless $san->type eq 'uniformResourceIdentifier';
Branch: 1 branch
BranchTrueFalseTotal
unless $san->type eq "uniformResourceIdentifier" 0 5 5
176 5 $on_triple->(iri($san->value), u('cert:key'), $k);
177 }
178 5 $on_done->();
179
180 5 $tempdir->remove_tree;
181
182 5 return $cert;
183 }
184  
185 1 __PACKAGE__
186 __END__
↑ Summary blib/lib/Web/ID/Util.pm »