SSL/Utils.pm

filter: Statementstmt 44.9% 111 / 247 Branchbran 27.7% 40 / 144 Conditioncond 32.6% 30 / 92 MC/DCmcdc 1.9% 1 / 51 Subroutinesub 47.8% 11 / 23 Podpod 100% 15 / 15 totaltotal 36.3% 208 / 572 CC 117 SCAR 56.8
CC
117
Cov
37%
CERT_asHash
756.0
CERT_create
320.8
__ANON__
42.0
CRAP 292.6
« SSL.pm ↑ Summary SSL/PublicSuffix.pm »
1  
2 package IO::Socket::SSL::Utils;
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Net::SSLeay;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # old versions of Exporter do not export 'import' yet
9 14 require Exporter;
10 14 *import = \&Exporter::import;
11  
12 14 our $VERSION = '2.015';
13 14 our @EXPORT = qw(
14 PEM_file2cert PEM_file2certs PEM_string2cert PEM_cert2file PEM_certs2file PEM_cert2string
15 PEM_file2key PEM_string2key PEM_key2file PEM_key2string
16 KEY_free CERT_free
17 KEY_create_rsa CERT_asHash CERT_create
18 );
19  
20 sub PEM_file2cert {
21 10 ▶ my $file = shift;
Subroutine PEM_file2cert: called CC=3 SCAR=11.2
Pod documented
22 10 ▶ my $bio = Net::SSLeay::BIO_new_file($file,'r') or
Branch: 1 branch
BranchTrueFalseTotal
my $bio = &Net::SSLeay::BIO_new_file($file, "r") or croak("cannot read ${file}: $!") 0 10 10
23 croak "cannot read $file: $!";
24 10 my $cert = Net::SSLeay::PEM_read_bio_X509($bio);
25 10 Net::SSLeay::BIO_free($bio);
26 10 ▶ $cert or croak "cannot parse $file as PEM X509 cert: ".
Branch: 1 branch
BranchTrueFalseTotal
$cert or croak("cannot parse $file as PEM X509 cert: " . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error())) 0 10 10
27 Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
28 10 return $cert;
29 }
30  
31 sub PEM_cert2file {
32 0 ▶ my ($cert,$file) = @_;
Subroutine PEM_cert2file: not called CC=3 SCAR=24.8
Pod documented
33 0 ▶ my $string = Net::SSLeay::PEM_get_string_X509($cert)
Branch: 1 branch
BranchTrueFalseTotal
my $string = &Net::SSLeay::PEM_get_string_X509($cert) or croak("cannot get string from cert") 0 0 0
34 or croak("cannot get string from cert");
35 0 ▶ open( my $fh,'>',$file ) or croak("cannot write $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("cannot write ${file}: $!") 0 0 0
36 0 print $fh $string;
37 }
38  
39 14 ▶use constant PEM_R_NO_START_LINE => 108;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 sub PEM_file2certs {
41 0 ▶ my $file = shift;
Subroutine PEM_file2certs: not called CC=5 SCAR=34.0
Pod documented
42 0 ▶ my $bio = Net::SSLeay::BIO_new_file($file,'r') or
Branch: 1 branch
BranchTrueFalseTotal
my $bio = &Net::SSLeay::BIO_new_file($file, "r") or croak("cannot read ${file}: $!") 0 0 0
43 croak "cannot read $file: $!";
44 0 my @certs;
45 0 while (1) {
46 0 ▶ if (my $cert = Net::SSLeay::PEM_read_bio_X509($bio)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cert = &Net::SSLeay::PEM_read_bio_X509($bio)) { } 0 0 0
47 0 push @certs, $cert;
48 } else {
49 0 Net::SSLeay::BIO_free($bio);
50 0 my $error = Net::SSLeay::ERR_get_error();
51 0 ▶ last if ($error & 0xfff) == PEM_R_NO_START_LINE && @certs;
Branch: 1 branch
BranchTrueFalseTotal
if ($error & 4095) == 108 and @certs 0 0 0
Condition0%0/3
($error & 4095) == 108 and @certs
!ll&&!rl&&r
000
MC/DC0%0/2
($error & 4095) == 108 and @certs
($error & 4095) == 108 @certs
Truth table0%0/3
A and B
A= ($error & 4095) == 108B= @certs
ABresult
0X0
100
111
52 0 croak "cannot parse $file as PEM X509 cert: " .
53 Net::SSLeay::ERR_error_string($error);
54 }
55 }
56 0 return @certs;
57 }
58  
59 sub PEM_certs2file {
60 0 ▶ my $file = shift;
Subroutine PEM_certs2file: not called CC=4 SCAR=30.0
Pod documented
61 0 ▶ open( my $fh,'>',$file ) or croak("cannot write $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("cannot write ${file}: $!") 0 0 0
62 0 for my $cert (@_) {
63 0 ▶ my $string = Net::SSLeay::PEM_get_string_X509($cert)
Branch: 1 branch
BranchTrueFalseTotal
my $string = &Net::SSLeay::PEM_get_string_X509($cert) or croak("cannot get string from cert") 0 0 0
64 or croak("cannot get string from cert");
65 0 print $fh $string;
66 }
67 }
68  
69  
70 sub PEM_string2cert {
71 484 ▶ my $string = shift;
Subroutine PEM_string2cert: called CC=2 SCAR=7.0
Pod documented
72 484 my $bio = Net::SSLeay::BIO_new( Net::SSLeay::BIO_s_mem());
73 484 Net::SSLeay::BIO_write($bio,$string);
74 484 my $cert = Net::SSLeay::PEM_read_bio_X509($bio);
75 484 Net::SSLeay::BIO_free($bio);
76 484 ▶ $cert or croak "cannot parse string as PEM X509 cert: ".
Branch: 1 branch
BranchTrueFalseTotal
$cert or croak('cannot parse string as PEM X509 cert: ' . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error())) 0 484 484
77 Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
78 484 return $cert;
79 }
80  
81 sub PEM_cert2string {
82 0 ▶ my $cert = shift;
Subroutine PEM_cert2string: not called CC=2 SCAR=17.9
Pod documented
83 0 ▶ return Net::SSLeay::PEM_get_string_X509($cert)
Condition0%0/3
&Net::SSLeay::PEM_get_string_X509($cert) || croak('cannot get string from cert')
l!l&&r!l&&!r
000
MC/DC0%0/2
&Net::SSLeay::PEM_get_string_X509($cert) || croak('cannot get string from cert')
&Net::SSLeay::PEM_get_string_X509($cert) croak('cannot get string from cert')
Truth table0%0/3
A || B
A= &Net::SSLeay::PEM_get_string_X509($cert)B= croak('cannot get string from cert')
ABresult
1X1
011
000
84 || croak("cannot get string from cert");
85 }
86  
87 sub PEM_file2key {
88 0 ▶ my $file = shift;
Subroutine PEM_file2key: not called CC=3 SCAR=24.8
Pod documented
89 0 ▶ my $bio = Net::SSLeay::BIO_new_file($file,'r') or
Branch: 1 branch
BranchTrueFalseTotal
my $bio = &Net::SSLeay::BIO_new_file($file, "r") or croak("cannot read ${file}: $!") 0 0 0
90 croak "cannot read $file: $!";
91 0 my $key = Net::SSLeay::PEM_read_bio_PrivateKey($bio);
92 0 Net::SSLeay::BIO_free($bio);
93 0 ▶ $key or croak "cannot parse $file as PEM private key: ".
Branch: 1 branch
BranchTrueFalseTotal
$key or croak("cannot parse $file as PEM private key: " . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error())) 0 0 0
94 Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
95 0 return $key;
96 }
97  
98 sub PEM_key2file {
99 0 ▶ my ($key,$file) = @_;
Subroutine PEM_key2file: not called CC=3 SCAR=24.8
Pod documented
100 0 ▶ my $string = Net::SSLeay::PEM_get_string_PrivateKey($key)
Branch: 1 branch
BranchTrueFalseTotal
my $string = &Net::SSLeay::PEM_get_string_PrivateKey($key) or croak("cannot get string from key") 0 0 0
101 or croak("cannot get string from key");
102 0 ▶ open( my $fh,'>',$file ) or croak("cannot write $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("cannot write ${file}: $!") 0 0 0
103 0 print $fh $string;
104 }
105  
106 sub PEM_string2key {
107 0 ▶ my $string = shift;
Subroutine PEM_string2key: not called CC=2 SCAR=17.9
Pod documented
108 0 my $bio = Net::SSLeay::BIO_new( Net::SSLeay::BIO_s_mem());
109 0 Net::SSLeay::BIO_write($bio,$string);
110 0 my $key = Net::SSLeay::PEM_read_bio_PrivateKey($bio);
111 0 Net::SSLeay::BIO_free($bio);
112 0 ▶ $key or croak "cannot parse string as PEM private key: ".
Branch: 1 branch
BranchTrueFalseTotal
$key or croak('cannot parse string as PEM private key: ' . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error())) 0 0 0
113 Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
114 0 return $key;
115 }
116  
117 sub PEM_key2string {
118 0 ▶ my $key = shift;
Subroutine PEM_key2string: not called CC=2 SCAR=17.9
Pod documented
119 0 ▶ return Net::SSLeay::PEM_get_string_PrivateKey($key)
Condition0%0/3
&Net::SSLeay::PEM_get_string_PrivateKey($key) || croak('cannot get string from key')
l!l&&r!l&&!r
000
MC/DC0%0/2
&Net::SSLeay::PEM_get_string_PrivateKey($key) || croak('cannot get string from key')
&Net::SSLeay::PEM_get_string_PrivateKey($key) croak('cannot get string from key')
Truth table0%0/3
A || B
A= &Net::SSLeay::PEM_get_string_PrivateKey($key)B= croak('cannot get string from key')
ABresult
1X1
011
000
120 || croak("cannot get string from key");
121 }
122  
123 sub CERT_free {
124 78 ▶ Net::SSLeay::X509_free($_) for @_;
Subroutine CERT_free: called CC=2 SCAR=6.9
Pod documented
125 }
126  
127 sub KEY_free {
128 0 ▶ Net::SSLeay::EVP_PKEY_free($_) for @_;
Subroutine KEY_free: not called CC=2 SCAR=17.9
Pod documented
129 }
130  
131 sub KEY_create_rsa {
132 16 ▶ my $bits = shift || 2048;
Subroutine KEY_create_rsa: called CC=2 SCAR=7.0
Pod documented
Condition50%1/2
shift() || 2048
l!l
016
MC/DC0%0/1
shift() || 2048
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
133 16 my $key = Net::SSLeay::EVP_PKEY_new();
134 16 my $rsa = Net::SSLeay::RSA_generate_key($bits, 0x10001); # 0x10001 = RSA_F4
135 16 Net::SSLeay::EVP_PKEY_assign_RSA($key,$rsa);
136 16 return $key;
137 }
138  
139 14 ▶if (defined &Net::SSLeay::EC_KEY_generate_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Net::SSLeay::EC_KEY_generate_key) 14 0 14
140 14 push @EXPORT,'KEY_create_ec';
141 *KEY_create_ec = sub {
142 0 ▶ my $curve = shift || 'prime256v1';
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
shift() || 'prime256v1'
l!l
00
MC/DC0%0/1
shift() || 'prime256v1'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
143 0 my $key = Net::SSLeay::EVP_PKEY_new();
144 0 my $ec = Net::SSLeay::EC_KEY_generate_key($curve);
145 0 Net::SSLeay::EVP_PKEY_assign_EC_KEY($key,$ec);
146 0 return $key;
147 }
148 14 }
149  
150 # extract information from cert
151 14 my %gen2i = qw( OTHERNAME 0 EMAIL 1 DNS 2 X400 3 DIRNAME 4 EDIPARTY 5 URI 6 IP 7 RID 8 );
152 14 my %i2gen = reverse %gen2i;
153 sub CERT_asHash {
154 0 ▶ my $cert = shift;
Subroutine CERT_asHash: not called CC=27 SCAR=66.3
Pod documented
155 0 ▶ my $digest_name = shift || 'sha256';
Condition0%0/2
shift() || 'sha256'
l!l
00
MC/DC0%0/1
shift() || 'sha256'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
156  
157 0 my %hash = (
158 version => Net::SSLeay::X509_get_version($cert),
159 not_before => _asn1t2t(Net::SSLeay::X509_get_notBefore($cert)),
160 not_after => _asn1t2t(Net::SSLeay::X509_get_notAfter($cert)),
161 serial => Net::SSLeay::P_ASN1_INTEGER_get_dec(
162 Net::SSLeay::X509_get_serialNumber($cert)),
163 signature_alg => Net::SSLeay::OBJ_obj2txt (
164 Net::SSLeay::P_X509_get_signature_alg($cert)),
165 crl_uri => [ Net::SSLeay::P_X509_get_crl_distribution_points($cert) ],
166 keyusage => [ Net::SSLeay::P_X509_get_key_usage($cert) ],
167 extkeyusage => {
168 oid => [ Net::SSLeay::P_X509_get_ext_key_usage($cert,0) ],
169 nid => [ Net::SSLeay::P_X509_get_ext_key_usage($cert,1) ],
170 sn => [ Net::SSLeay::P_X509_get_ext_key_usage($cert,2) ],
171 ln => [ Net::SSLeay::P_X509_get_ext_key_usage($cert,3) ],
172 },
173 "pubkey_digest_$digest_name" => Net::SSLeay::X509_pubkey_digest(
174 $cert,_digest($digest_name)),
175 "x509_digest_$digest_name" => Net::SSLeay::X509_digest(
176 $cert,_digest($digest_name)),
177 "fingerprint_$digest_name" => Net::SSLeay::X509_get_fingerprint(
178 $cert,$digest_name),
179 );
180  
181 0 for([ subject => Net::SSLeay::X509_get_subject_name($cert) ],
182 [ issuer => Net::SSLeay::X509_get_issuer_name($cert) ]) {
183 0 my ($what,$subj) = @$_;
184 0 my %subj;
185 0 for ( 0..Net::SSLeay::X509_NAME_entry_count($subj)-1 ) {
186 0 my $e = Net::SSLeay::X509_NAME_get_entry($subj,$_);
187 0 my $k = Net::SSLeay::OBJ_obj2txt(
188 Net::SSLeay::X509_NAME_ENTRY_get_object($e));
189 0 my $v = Net::SSLeay::P_ASN1_STRING_get(
190 Net::SSLeay::X509_NAME_ENTRY_get_data($e));
191 0 ▶ if (!exists $subj{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $subj{$k}) { } 0 0 0
elsif (not ref $subj{$k}) { } 0 0 0
192 0 $subj{$k} = $v;
193 } elsif (!ref $subj{$k}) {
194 0 $subj{$k} = [ $subj{$k}, $v ];
195 } else {
196 0 push @{$subj{$k}}, $v;
197 }
198 }
199 0 $hash{$what} = \%subj;
200 }
201  
202  
203 0 ▶ if ( my @names = Net::SSLeay::X509_get_subjectAltNames($cert) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@names) = &Net::SSLeay::X509_get_subjectAltNames($cert)) 0 0 0
204 0 my $alt = $hash{subjectAltNames} = [];
205 0 ▶ while (my ($t,$v) = splice(@names,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@names, 0, 2)) 0 0 0
206 0 ▶ $t = $i2gen{$t} || die "unknown type $t in subjectAltName";
Condition0%0/2
$i2gen{$t} || die("unknown type $t in subjectAltName")
l!l
00
MC/DC0%0/1
$i2gen{$t} || die("unknown type $t in subjectAltName")
$i2gen{$t}
Truth table0%0/2
A
A= $i2gen{$t}
Aresult
11
00
207 0 ▶ if ( $t eq 'IP' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "IP") 0 0 0
208 0 ▶ if (length($v) == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $v == 4) { } 0 0 0
elsif (length $v == 16) { } 0 0 0
209 0 $v = join('.',unpack("CCCC",$v));
210 } elsif ( length($v) == 16 ) {
211 0 my @v = unpack("nnnnnnnn",$v);
212 0 my ($best0,$last0);
213 0 ▶ for(my $i=0;$i<@v;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @v) 0 0 0
214 0 ▶ if ($v[$i] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v[$i] == 0) { } 0 0 0
215 0 ▶ if ($last0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last0) { } 0 0 0
216 0 $last0->[1] = $i;
217 0 $last0->[2]++;
218 0 ▶ $best0 = $last0 if ++$last0->[2]>$best0->[2];
Branch: 1 branch
BranchTrueFalseTotal
if ++$last0->[2] > $best0->[2] 0 0 0
219 } else {
220 0 $last0 = [ $i,$i,0 ];
221 0 ▶ $best0 ||= $last0;
Condition0%0/3
$best0 ||= $last0
l!l&&r!l&&!r
000
MC/DC0%0/2
$best0 ||= $last0
$best0 $last0
Truth table0%0/3
A ||= B
A= $best0B= $last0
ABresult
1X1
011
000
222 }
223 } else {
224 0 $last0 = undef;
225 }
226 }
227 0 ▶ if ($best0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best0) { } 0 0 0
228 0 $v = '';
229 0 ▶ $v .= join(':', map { sprintf( "%x",$_) } @v[0..$best0->[0]-1]) if $best0->[0]>0;
Branch: 1 branch
BranchTrueFalseTotal
if $best0->[0] > 0 0 0 0
230 0 $v .= '::';
231 0 ▶ $v .= join(':', map { sprintf( "%x",$_) } @v[$best0->[1]+1..$#v]) if $best0->[1]<$#v;
Branch: 1 branch
BranchTrueFalseTotal
if $best0->[1] < $#v 0 0 0
232 } else {
233 0 $v = join(':', map { sprintf( "%x",$_) } @v);
234 }
235 }
236 }
237 0 push @$alt,[$t,$v]
238 }
239 }
240  
241 0 my @ext;
242 0 for( 0..Net::SSLeay::X509_get_ext_count($cert)-1 ) {
243 0 my $e = Net::SSLeay::X509_get_ext($cert,$_);
244 0 my $o = Net::SSLeay::X509_EXTENSION_get_object($e);
245 0 my $nid = Net::SSLeay::OBJ_obj2nid($o);
246 0 ▶ push @ext, {
Branch: 2 branches
BranchTrueFalseTotal
$nid > 0 ? : 0 0 0
$nid > 0 ? : 0 0 0
247 oid => Net::SSLeay::OBJ_obj2txt($o),
248 nid => ( $nid > 0 ) ? $nid : undef,
249 sn => ( $nid > 0 ) ? Net::SSLeay::OBJ_nid2sn($nid) : undef,
250 critical => Net::SSLeay::X509_EXTENSION_get_critical($e),
251 data => Net::SSLeay::X509V3_EXT_print($e),
252 }
253 }
254 0 $hash{ext} = \@ext;
255  
256 0 ▶ if ( defined(&Net::SSLeay::P_X509_get_ocsp_uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Net::SSLeay::P_X509_get_ocsp_uri) { } 0 0 0
257 0 $hash{ocsp_uri} = [ Net::SSLeay::P_X509_get_ocsp_uri($cert) ];
258 } else {
259 0 $hash{ocsp_uri} = [];
260 0 for( @ext ) {
261 0 ▶ $_->{sn} or next;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"sn"} or next 0 0 0
262 0 ▶ $_->{sn} eq 'authorityInfoAccess' or next;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"sn"} eq "authorityInfoAccess" or next 0 0 0
263 0 push @{ $hash{ocsp_uri}}, $_->{data} =~m{\bOCSP - URI:(\S+)}g;
264 }
265 }
266  
267 0 return \%hash;
268 }
269  
270 sub CERT_create {
271 120 ▶ my %args = @_%2 ? %{ shift() } : @_;
Subroutine CERT_create: called CC=61 SCAR=57.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 120 120
272  
273 120 my $cert = Net::SSLeay::X509_new();
274 120 ▶ my $digest_name = delete $args{digest} || 'sha256';
Condition50%1/2
delete $args{'digest'} || 'sha256'
l!l
0120
MC/DC0%0/1
delete $args{'digest'} || 'sha256'
delete $args{'digest'}
Truth table50%1/2
A
A= delete $args{'digest'}
Aresult
11
00
275  
276 Net::SSLeay::ASN1_INTEGER_set(
277 Net::SSLeay::X509_get_serialNumber($cert),
278 120 ▶ delete $args{serial} || rand(2**32),
Condition33%1/3
delete $args{'serial'} || rand 4294967296.0
l!l&&r!l&&!r
01200
MC/DC0%0/2
delete $args{'serial'} || rand 4294967296.0
delete $args{'serial'} rand 4294967296.0
Truth table33%1/3
A || B
A= delete $args{'serial'}B= rand 4294967296.0
ABresult
1X1
011
000
279 );
280  
281 # version default to 2 (V3)
282 Net::SSLeay::X509_set_version($cert,
283 120 ▶ delete $args{version} || 2 );
Condition50%1/2
delete $args{'version'} || 2
l!l
0120
MC/DC0%0/1
delete $args{'version'} || 2
delete $args{'version'}
Truth table50%1/2
A
A= delete $args{'version'}
Aresult
11
00
284  
285 # not_before default to now
286 Net::SSLeay::ASN1_TIME_set(
287 Net::SSLeay::X509_get_notBefore($cert),
288 delete $args{not_before} || time()
289 120 ▶ );
Condition33%1/3
delete $args{'not_before'} || time
l!l&&r!l&&!r
01200
MC/DC0%0/2
delete $args{'not_before'} || time
delete $args{'not_before'} time
Truth table33%1/3
A || B
A= delete $args{'not_before'}B= time
ABresult
1X1
011
000
290  
291 # not_after default to now+365 days
292 Net::SSLeay::ASN1_TIME_set(
293 Net::SSLeay::X509_get_notAfter($cert),
294 120 ▶ delete $args{not_after} || time() + 365*86400
Condition33%1/3
delete $args{'not_after'} || time + 31536000
l!l&&r!l&&!r
01200
MC/DC0%0/2
delete $args{'not_after'} || time + 31536000
delete $args{'not_after'} time + 31536000
Truth table33%1/3
A || B
A= delete $args{'not_after'}B= time + 31536000
ABresult
1X1
011
000
295 );
296  
297 # set subject
298 120 my $subj_e = Net::SSLeay::X509_get_subject_name($cert);
299 my $subj = delete $args{subject} || {
300 120 ▶ organizationName => 'IO::Socket::SSL',
Condition100%2/2
delete $args{'subject'} || {'organizationName', 'IO::Socket::SSL', 'commonName', 'IO::Socket::SSL Test'}
l!l
1173
MC/DC100%1/1
delete $args{'subject'} || {'organizationName', 'IO::Socket::SSL', 'commonName', 'IO::Socket::SSL Test'}
delete $args{'subject'}
Truth table100%2/2
A
A= delete $args{'subject'}
Aresult
11
00
301 commonName => 'IO::Socket::SSL Test'
302 };
303  
304 120 ▶ while ( my ($k,$v) = each %$subj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$subj) 116 120 236
305 # Not everything we get is nice - try with MBSTRING_UTF8 first and if it
306 # fails try V_ASN1_T61STRING and finally V_ASN1_OCTET_STRING
307 116 ▶ for (ref($v) ? @$v : ($v)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 116 116
308 116 ▶ Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e,$k,0x1000,$_,-1,0)
Branch: 1 branch
BranchTrueFalseTotal
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $_, -1, 0) or croak("failed to add entry for $k - " . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error())) 0 116 116
Condition33%2/6
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $_, -1, 0)
l!l&&r!l&&!r
11600
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $_, -1, 0)
l!l&&r!l&&!r
11600
MC/DC0%0/3
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $_, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $_, -1, 0)
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $_, -1, 0) &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $_, -1, 0) &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $_, -1, 0)
Truth table25%1/4
A or B or C
A= &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $_, -1, 0)B= &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $_, -1, 0)C= &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $_, -1, 0)
ABCresult
1XX1
01X1
0011
0000
309 or Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e,$k,20,$_,-1,0)
310 or Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e,$k,4,$_,-1,0)
311 or croak("failed to add entry for $k - ".
312 Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()));
313 }
314 }
315  
316 120 my @ext = (
317 &Net::SSLeay::NID_subject_key_identifier => 'hash',
318 &Net::SSLeay::NID_authority_key_identifier => 'keyid',
319 );
320 120 ▶ if ( my $altsubj = delete $args{subjectAltNames} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $altsubj = delete $args{"subjectAltNames"}) 22 98 120
321 push @ext,
322 &Net::SSLeay::NID_subject_alt_name =>
323 22 join(',', map { "$_->[0]:$_->[1]" } @$altsubj)
324 }
325  
326 120 ▶ my $key = delete $args{key} || KEY_create_rsa();
Condition66%2/3
delete $args{'key'} || KEY_create_rsa()
l!l&&r!l&&!r
104160
MC/DC0%0/2
delete $args{'key'} || KEY_create_rsa()
delete $args{'key'} KEY_create_rsa()
Truth table66%2/3
A || B
A= delete $args{'key'}B= KEY_create_rsa()
ABresult
1X1
011
000
327 120 Net::SSLeay::X509_set_pubkey($cert,$key);
328  
329 120 my $is = delete $args{issuer};
330 120 ▶ my $issuer_cert = delete $args{issuer_cert} || $is && $is->[0] || $cert;
Condition77%7/9
delete $args{'issuer_cert'} || $is && $is->[0] || $cert
l!l&&r!l&&!r
11280
delete $args{"issuer_cert"} or $is and $is->[0]
l!l&&r!l&&!r
78348
$is and $is->[0]
!ll&&!rl&&r
8034
MC/DC0%0/4
delete $args{'issuer_cert'} || $is && $is->[0] || $cert
delete $args{"issuer_cert"} $is $is->[0] $cert
Truth table50%3/6
A or B and C || D
A= delete $args{"issuer_cert"}B= $isC= $is->[0]D= $cert
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
331 120 ▶ my $issuer_key = delete $args{issuer_key} || $is && $is->[1] || $key;
Condition77%7/9
delete $args{'issuer_key'} || $is && $is->[1] || $key
l!l&&r!l&&!r
11280
delete $args{"issuer_key"} or $is and $is->[1]
l!l&&r!l&&!r
78348
$is and $is->[1]
!ll&&!rl&&r
8034
MC/DC0%0/4
delete $args{'issuer_key'} || $is && $is->[1] || $key
delete $args{"issuer_key"} $is $is->[1] $key
Truth table50%3/6
A or B and C || D
A= delete $args{"issuer_key"}B= $isC= $is->[1]D= $key
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
332  
333 120 my %purpose;
334 120 ▶ if (my $p = delete $args{purpose}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = delete $args{"purpose"}) 2 118 120
335 2 ▶ if (!ref($p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $p) { } 0 2 2
elsif (ref $p eq 'ARRAY') { } 0 2 2
336 0 ▶ $purpose{lc($2)} = (!$1 || $1 eq '+') ? 1:0
Branch: 2 branches
BranchTrueFalseTotal
$p =~ /([+-]?)(\w+)/g and do { $purpose{lc $2} = !$1 || $1 eq "+" ? 1 : 0 } 0 0 0
!$1 || $1 eq '+' ? : 0 0 0
Condition0%0/3
not $1 or $1 eq "+"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $1 or $1 eq "+"
not $1 $1 eq "+"
Truth table0%0/3
not(A) or B
A= not $1B= $1 eq "+"
ABresult
1X1
011
000
337 while $p =~m{([+-]?)(\w+)}g;
338 } elsif (ref($p) eq 'ARRAY') {
339 0 for(@$p) {
340 0 ▶ m{^([+-]?)(\w+)$} or die "invalid entry in purpose: $_";
Branch: 1 branch
BranchTrueFalseTotal
/^([+-]?)(\w+)$/ or die "invalid entry in purpose: $_" 0 0 0
341 0 ▶ $purpose{lc($2)} = (!$1 || $1 eq '+') ? 1:0
Branch: 1 branch
BranchTrueFalseTotal
!$1 || $1 eq '+' ? : 0 0 0
Condition0%0/3
not $1 or $1 eq "+"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $1 or $1 eq "+"
not $1 $1 eq "+"
Truth table0%0/3
not(A) or B
A= not $1B= $1 eq "+"
ABresult
1X1
011
000
342 }
343 } else {
344 2 ▶ while( my ($k,$v) = each %$p) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$p) 2 2 4
345 2 ▶ $purpose{lc($k)} = ($v && $v ne '-')?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$v && $v ne '-' ? : 2 0 2
Condition33%1/3
$v and $v ne "-"
!ll&&!rl&&r
002
MC/DC0%0/2
$v and $v ne "-"
$v $v ne "-"
Truth table33%1/3
A and B
A= $vB= $v ne "-"
ABresult
0X0
100
111
346 }
347 }
348 }
349 120 ▶ if (delete $args{CA}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args{"CA"}) 10 110 120
350 # add defaults for CA
351 10 %purpose = (
352 ca => 1, sslca => 1, emailca => 1, objca => 1,
353 %purpose
354 );
355 }
356 120 ▶ if (!%purpose) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%purpose) 108 12 120
357 108 %purpose = (server => 1, client => 1);
358 }
359  
360 120 my (%key_usage,%ext_key_usage,%cert_type,%basic_constraints);
361  
362 120 my %dS = ( digitalSignature => \%key_usage );
363 120 my %kE = ( keyEncipherment => \%key_usage );
364 120 my %CA = ( 'CA:TRUE' => \%basic_constraints, %dS, keyCertSign => \%key_usage );
365 120 my @disable;
366 120 for(
367 [ client => { %dS, %kE, clientAuth => \%ext_key_usage, client => \%cert_type } ],
368 [ server => { %dS, %kE, serverAuth => \%ext_key_usage, server => \%cert_type } ],
369 [ email => { %dS, %kE, emailProtection => \%ext_key_usage, email => \%cert_type } ],
370 [ objsign => { %dS, %kE, codeSigning => \%ext_key_usage, objsign => \%cert_type } ],
371  
372 [ CA => { %CA }],
373 [ sslCA => { %CA, sslCA => \%cert_type }],
374 [ emailCA => { %CA, emailCA => \%cert_type }],
375 [ objCA => { %CA, objCA => \%cert_type }],
376  
377 [ emailProtection => { %dS, %kE, emailProtection => \%ext_key_usage, email => \%cert_type } ],
378 [ codeSigning => { %dS, %kE, codeSigning => \%ext_key_usage, objsign => \%cert_type } ],
379  
380 [ timeStamping => { timeStamping => \%ext_key_usage } ],
381 [ digitalSignature => { digitalSignature => \%key_usage } ],
382 [ nonRepudiation => { nonRepudiation => \%key_usage } ],
383 [ keyEncipherment => { keyEncipherment => \%key_usage } ],
384 [ dataEncipherment => { dataEncipherment => \%key_usage } ],
385 [ keyAgreement => { keyAgreement => \%key_usage } ],
386 [ keyCertSign => { keyCertSign => \%key_usage } ],
387 [ cRLSign => { cRLSign => \%key_usage } ],
388 [ encipherOnly => { encipherOnly => \%key_usage } ],
389 [ decipherOnly => { decipherOnly => \%key_usage } ],
390 [ clientAuth => { clientAuth => \%ext_key_usage } ],
391 [ serverAuth => { serverAuth => \%ext_key_usage } ],
392 ) {
393 2640 ▶ exists $purpose{lc($_->[0])} or next;
Branch: 1 branch
BranchTrueFalseTotal
exists $purpose{lc $_->[0]} or next 2382 258 2640
394 258 ▶ if (delete $purpose{lc($_->[0])}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $purpose{lc $_->[0]}) { } 258 0 258
395 258 ▶ while (my($k,$h) = each %{$_->[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$_[1];}) 1022 258 1280
396 1022 $h->{$k} = 1;
397 }
398 } else {
399 0 push @disable, $_->[1];
400 }
401 }
402 120 ▶ die "unknown purpose ".join(",",keys %purpose) if %purpose;
Branch: 1 branch
BranchTrueFalseTotal
if %purpose 0 120 120
403 120 for(@disable) {
404 0 ▶ while (my($k,$h) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 0 0 0
405 0 delete $h->{$k};
406 }
407 }
408  
409 120 ▶ if (%basic_constraints) {
Branch: 1 branch
BranchTrueFalseTotal
if (%basic_constraints) { } 10 110 120
410 10 push @ext,&Net::SSLeay::NID_basic_constraints,
411 => join(",",'critical', sort keys %basic_constraints);
412 } else {
413 110 push @ext, &Net::SSLeay::NID_basic_constraints => 'critical,CA:FALSE';
414 }
415 120 ▶ push @ext,&Net::SSLeay::NID_key_usage
Branch: 1 branch
BranchTrueFalseTotal
if %key_usage 120 0 120
416 => join(",",'critical', sort keys %key_usage) if %key_usage;
417 120 ▶ push @ext,&Net::SSLeay::NID_netscape_cert_type
Branch: 1 branch
BranchTrueFalseTotal
if %cert_type 120 0 120
418 => join(",",sort keys %cert_type) if %cert_type;
419 120 ▶ push @ext,&Net::SSLeay::NID_ext_key_usage
Branch: 1 branch
BranchTrueFalseTotal
if %ext_key_usage 110 10 120
420 => join(",",sort keys %ext_key_usage) if %ext_key_usage;
421 120 Net::SSLeay::P_X509_add_extensions($cert, $issuer_cert, @ext);
422  
423 120 my %have_ext;
424 120 ▶ for(my $i=0;$i<@ext;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ext) 732 120 852
425 732 $have_ext{ $ext[$i] }++
426 }
427 120 ▶ for my $ext (@{ delete $args{ext} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
delete $args{"ext"} or [] 120 0 120
428 my $nid = $ext->{nid}
429 || $ext->{sn} && Net::SSLeay::OBJ_sn2nid($ext->{sn})
430 0 ▶ || croak "cannot determine NID of extension";
Condition0%0/9
$ext->{'nid'} || $ext->{'sn'} && &Net::SSLeay::OBJ_sn2nid($ext->{'sn'}) || croak('cannot determine NID of extension')
l!l&&r!l&&!r
000
$ext->{"nid"} or $ext->{"sn"} and &Net::SSLeay::OBJ_sn2nid($ext->{"sn"})
l!l&&r!l&&!r
000
$ext->{"sn"} and &Net::SSLeay::OBJ_sn2nid($ext->{"sn"})
!ll&&!rl&&r
000
MC/DC0%0/4
$ext->{'nid'} || $ext->{'sn'} && &Net::SSLeay::OBJ_sn2nid($ext->{'sn'}) || croak('cannot determine NID of extension')
$ext->{"nid"} $ext->{"sn"} &Net::SSLeay::OBJ_sn2nid($ext->{"sn"}) croak('cannot determine NID of extension')
Truth table0%0/6
A or B and C || D
A= $ext->{"nid"}B= $ext->{"sn"}C= &Net::SSLeay::OBJ_sn2nid($ext->{"sn"})D= croak('cannot determine NID of extension')
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
431 0 ▶ $have_ext{$nid} and next;
Branch: 1 branch
BranchTrueFalseTotal
$have_ext{$nid} and next 0 0 0
432 0 my $val = $ext->{data};
433 0 ▶ if ($nid == 177) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nid == 177) { } 0 0 0
434 # authorityInfoAccess:
435 # OpenSSL i2v does not output the same way as expected by i2v :(
436 0 for (split(/\n/,$val)) {
437 0 s{ - }{;}; # "OCSP - URI:..." -> "OCSP;URI:..."
438 0 ▶ $_ = "critical,$_" if $ext->{critical};
Branch: 1 branch
BranchTrueFalseTotal
if $ext->{"critical"} 0 0 0
439 0 Net::SSLeay::P_X509_add_extensions($cert,$issuer_cert,$nid,$_);
440 }
441 } else {
442 0 ▶ $val = "critical,$val" if $ext->{critical};
Branch: 1 branch
BranchTrueFalseTotal
if $ext->{"critical"} 0 0 0
443 0 Net::SSLeay::P_X509_add_extensions($cert, $issuer_cert, $nid, $val);
444 }
445 }
446  
447 die "unknown arguments: ". join(" ", sort keys %args)
448 120 ▶ if !delete $args{ignore_invalid_args} && %args;
Branch: 1 branch
BranchTrueFalseTotal
if not delete $args{"ignore_invalid_args"} and %args 0 120 120
Condition33%1/3
not delete $args{"ignore_invalid_args"} and %args
!ll&&!rl&&r
01200
MC/DC0%0/2
not delete $args{"ignore_invalid_args"} and %args
not delete $args{"ignore_invalid_args"} %args
Truth table33%1/3
not(A) and B
A= not delete $args{"ignore_invalid_args"}B= %args
ABresult
0X0
100
111
449  
450 120 Net::SSLeay::X509_set_issuer_name($cert,
451 Net::SSLeay::X509_get_subject_name($issuer_cert));
452 120 Net::SSLeay::X509_sign($cert,$issuer_key,_digest($digest_name));
453  
454 120 return ($cert,$key);
455 }
456  
457  
458  
459 14 ▶if ( defined &Net::SSLeay::ASN1_TIME_timet ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Net::SSLeay::ASN1_TIME_timet) { } 14 0 14
460 14 *_asn1t2t = \&Net::SSLeay::ASN1_TIME_timet
461 } else {
462 0 require Time::Local;
463 0 my %mon2i = qw(
464 Jan 0 Feb 1 Mar 2 Apr 3 May 4 Jun 5
465 Jul 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11
466 );
467 *_asn1t2t = sub {
468 0 ▶ my $t = Net::SSLeay::P_ASN1_TIME_put2string( shift );
Subroutine __ANON__: not called CC=6 SCAR=37.4
469 0 my ($mon,$d,$h,$m,$s,$y,$tz) = split(/[\s:]+/,$t);
470 0 ▶ defined( $mon = $mon2i{$mon} ) or die "invalid month in $t";
Branch: 1 branch
BranchTrueFalseTotal
defined($mon = $mon2i{$mon}) or die "invalid month in $t" 0 0 0
471 0 ▶ $tz ||= $y =~s{^(\d+)([A-Z]\S*)}{$1} && $2;
Condition0%0/6
$tz ||= $y =~ s/^(\d+)([A-Z]\S*)/$1/ && $2
l!l&&r!l&&!r
000
$y =~ s/^(\d+)([A-Z]\S*)/$1/ && $2
!ll&&!rl&&r
000
MC/DC0%0/3
$tz ||= $y =~ s/^(\d+)([A-Z]\S*)/$1/ && $2
$tz $y =~ s/^(\d+)([A-Z]\S*)/$1/ $2
Truth table0%0/4
A ||= B && C
A= $tzB= $y =~ s/^(\d+)([A-Z]\S*)/$1/C= $2
ABCresult
1XX1
0111
00X0
0100
472 0 ▶ if ( ! $tz ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $tz) { } 0 0 0
elsif ($tz eq 'GMT') { } 0 0 0
473 0 return Time::Local::timelocal($s,$m,$h,$d,$mon,$y)
474 } elsif ( $tz eq 'GMT' ) {
475 0 return Time::Local::timegm($s,$m,$h,$d,$mon,$y)
476 } else {
477 0 die "unexpected TZ $tz from ASN1_TIME_print";
478 }
479 }
480 0 }
481  
482 {
483 14 my %digest;
484 sub _digest {
485 120 ▶ my $digest_name = shift;
Subroutine _digest: called CC=3 SCAR=11.3
486 120 ▶ return $digest{$digest_name} ||= do {
Condition66%2/3
$digest{$digest_name} ||= do { &Net::SSLeay::SSLeay_add_ssl_algorithms(); &Net::SSLeay::EVP_get_digestbyname($digest_name) or die "Digest algorithm $digest_name is not available" }
l!l&&r!l&&!r
11460
MC/DC0%0/2
$digest{$digest_name} ||= do { &Net::SSLeay::SSLeay_add_ssl_algorithms(); &Net::SSLeay::EVP_get_digestbyname($digest_name) or die "Digest algorithm $digest_name is not available" }
$digest{$digest_name} do { &Net::SSLeay::SSLeay_add_ssl_algorithms(); &Net::SSLeay::EVP_get_digestbyname($digest_name) or die "Digest algorithm $digest_name is not available" }
Truth table66%2/3
A ||= B
A= $digest{$digest_name}B= do { &Net::SSLeay::SSLeay_add_ssl_algorithms(); &Net::SSLeay::EVP_get_digestbyname($digest_name) or die "Digest algorithm $digest_name is not available" }
ABresult
1X1
011
000
487 6 Net::SSLeay::SSLeay_add_ssl_algorithms();
488 6 ▶ Net::SSLeay::EVP_get_digestbyname($digest_name)
Branch: 1 branch
BranchTrueFalseTotal
&Net::SSLeay::EVP_get_digestbyname($digest_name) or die "Digest algorithm $digest_name is not available" 0 6 6
489 or die "Digest algorithm $digest_name is not available";
490 };
491 }
492 }
493  
494  
495 14 1;
496  
497 __END__
« SSL.pm ↑ Summary SSL/PublicSuffix.pm »