Cloner.pm

filter: Statementstmt 97.9% 142 / 145 Branchbran 75.0% 21 / 28 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 16 / 16 Podpod 33.3% 1 / 3 totaltotal 92.0% 184 / 200 CC 18 SCAR 16.4
CC
18
Cov
93%
clone_cert
11.1
load_ca
4.1
new
4.0
CRAP 5.2
↑ Summary Cloner/x509asn1.pm »
1 package Crypt::OpenSSL::Cloner;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Crypt::OpenSSL::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Slurp qw(read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Convert::ASN1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #use Encoding::BER::DER;
14  
15 1 ▶use Crypt::OpenSSL::Cloner::x509asn1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $PREFERRED_ALG = "sha1";
18 1 our $PREFERRED_KEYLENGTH = 1024;
19 1 our $CA_BASENAME = "CA";
20 1 our $VERSION = 0.04;
21  
22 1 my $ASN = Convert::ASN1->new();
23 1 ▶$ASN->prepare($Crypt::OpenSSL::Cloner::x509asn1::ASN_DEF,
Branch: 1 branch
BranchTrueFalseTotal
$ASN->prepare($Crypt::OpenSSL::Cloner::x509asn1::ASN_DEF, "encoding", "DER") or die "GRRR" 1 0 1
24 encoding => "DER") or die "GRRR";
25  
26 1 my %PARSERS = map { $_, $ASN->find($_) } qw(
27 SubjectKeyIdentifier
28 BasicConstraints
29 KeyUsage
30 CertificatePolicies
31 SubjectAltName
32 );
33  
34 1 my %oid_2_ext = (
35 '2.5.29.14' => "SubjectKeyIdentifier",
36 '2.5.29.17' => "SubjectAltName",
37 '2.5.29.37' => "KeyUsage",
38 '2.5.29.32' => 'CertificatePolicies',
39 '2.5.29.19' => "BasicConstraints",
40
41 );
42  
43 1 my @FIELDS = qw(
44 PATH
45 CA_OBJ
46 PRIVKEY_OBJ
47 AUTH_KEY_ID
48 );
49  
50 #To make sure we don't get a timestamp serial collision (unlikely but possible)
51 1 my %TS_SERIALS = ();
52  
53 2 ▶sub load_ca {
Subroutine load_ca: called CC=4 SCAR=14.0
Pod undocumented
54 2 my ($self) = @_;
55 2 my $path = $self->{PATH};
56 2 my ($privkey_obj,$privkey_string,$ca_obj,$pem);
57 2 eval {
58 2 $privkey_string = read_file($path . "/$CA_BASENAME.key");
59 1 $pem = read_file($path."/$CA_BASENAME.pem");
60 };
61 2 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
62
63 1 $privkey_obj = Crypt::OpenSSL::CA::PrivateKey->parse($privkey_string);
64 1 $ca_obj = Crypt::OpenSSL::CA::X509->parse($pem);
65
66 1 ▶ return unless ($privkey_obj && $ca_obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $privkey_obj and $ca_obj 0 1 1
Condition33%1/3
$privkey_obj and $ca_obj
!ll&&!rl&&r
001
MC/DC0%0/2
$privkey_obj and $ca_obj
$privkey_obj $ca_obj
Truth table33%1/3
A and B
A= $privkey_objB= $ca_obj
ABresult
0X0
100
111
67 1 return [$ca_obj,$privkey_obj];
68 }
69  
70 1 ▶sub _gen_new_ca {
Subroutine _gen_new_ca: called CC=2 SCAR=6.9
71 1 my ($self,$dn_hash) = @_;
72 1 my $rsa = Crypt::OpenSSL::RSA->generate_key($PREFERRED_KEYLENGTH);
73 1 my $privkey = Crypt::OpenSSL::CA::PrivateKey->parse(
74 $rsa->get_private_key_string
75 );
76 1 my $ca = Crypt::OpenSSL::CA::X509->new($privkey->get_public_key);
77 1 my $dn = Crypt::OpenSSL::CA::X509_NAME->new(%$dn_hash);
78 1 my $keyid = $privkey->get_public_key->get_openssl_keyid();
79 1 ▶ die "Need Distinguished Name for CA" if !$dn_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $dn_hash 0 1 1
80 1 $ca->set_serial("0x1");
81 1 $ca->set_notBefore("20080204101500Z");
82 1 $ca->set_notAfter("20220204101500Z");
83 1 $ca->set_subject_DN($dn);
84 1 $ca->set_issuer_DN($dn);
85 1 $ca->set_extension("subjectKeyIdentifier", $keyid);
86 1 $ca->set_extension("authorityKeyIdentifier", {
87 keyid => $keyid,
88 issuer => $dn,
89 serial => "0x1"
90 });
91
92 1 $ca->set_extension("basicConstraints", "CA:TRUE", -critical => 1);
93 #$ca->set_extension("keyUsage" =>
94 # "digitalSignature, nonRepudiation,".
95 # "keyEncipherment, dataEncipherment, keyAgreement,".
96 # "keyCertSign, cRLSign");
97 1 my $crt_text = $ca->sign($privkey, $PREFERRED_ALG);
98 1 return [$ca,$privkey,$crt_text,$rsa->get_private_key_string];
99 }
100  
101 2 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
102 2 my ($cls,%opts) = @_;
103 2 my $self = {};
104 2 bless ($self, $cls);
105 2 lock_keys(%$self, @FIELDS);
106 2 ▶ my $path = delete $opts{path} or die "Must have CA path";
Branch: 1 branch
BranchTrueFalseTotal
my $path = delete $opts{"path"} or die "Must have CA path" 2 0 2
107 2 my $dn_hash = delete $opts{dn};
108 2 ▶ $dn_hash ||= {
Condition100%2/2
$dn_hash ||= {"C", "GB", "O", "CertOnTheFly", "OU", "CertOnTheFly Certificate Generation", "CN", "CertOnTheFly Root"}
l!l
11
MC/DC100%1/1
$dn_hash ||= {"C", "GB", "O", "CertOnTheFly", "OU", "CertOnTheFly Certificate Generation", "CN", "CertOnTheFly Root"}
$dn_hash
Truth table100%2/2
A
A= $dn_hash
Aresult
11
00
109 C => 'GB',
110 O => 'CertOnTheFly',
111 OU => "CertOnTheFly Certificate Generation",
112 CN => 'CertOnTheFly Root'
113 };
114
115 2 mkpath($path);
116 2 $self->{PATH} = $path;
117
118 2 my ($ca_obj,$privkey_obj);
119 2 my $res = $self->load_ca();
120 2 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 1 1 2
121 1 ($ca_obj,$privkey_obj) = @$res;
122 } else {
123 1 my ($pem,$keytxt);
124 1 ($ca_obj,$privkey_obj,$pem,$keytxt) = @{ $self->_gen_new_ca($dn_hash) };
125 1 write_file($path . "/$CA_BASENAME.pem", $pem);
126 1 write_file($path . "/$CA_BASENAME.key", $keytxt);
127 }
128 2 $self->{CA_OBJ} = $ca_obj;
129 2 $self->{PRIVKEY_OBJ} = $privkey_obj;
130 2 return $self;
131 }
132  
133  
134 2 ▶sub clone_cert {
Subroutine clone_cert: called CC=11 SCAR=24.1
Pod undocumented
135 2 my ($self,$pem,$domain_name) = @_;
136 2 my $keystr = Crypt::OpenSSL::RSA->generate_key(1024)->get_private_key_string();
137 2 my $privkey = Crypt::OpenSSL::CA::PrivateKey->parse($keystr);
138 2 my $new_cert = Crypt::OpenSSL::CA::X509->new($privkey->get_public_key);
139
140 2 ▶ my $alt_name_string = ($domain_name) ? "DNS:$domain_name" : "";
Branch: 1 branch
BranchTrueFalseTotal
$domain_name ? : 0 2 2
141
142 2 $new_cert->set_subject_DN(Crypt::OpenSSL::CA::X509->parse($pem)->get_subject_DN);
143 2 $new_cert->set_issuer_DN($self->{CA_OBJ}->get_issuer_DN);
144 2 $new_cert->set_notBefore("20080204114600Z");
145 2 $new_cert->set_notAfter("20220204114600Z");
146 $new_cert->set_extension("authorityKeyIdentifier",
147 2 { keyid => $self->{CA_OBJ}->get_subject_keyid });
148 2 my $serial = time();
149 2 $serial .= $TS_SERIALS{$serial}++;
150 2 $serial = "0x$serial";
151 2 $new_cert->set_serial($serial);
152 2 my %extracted;
153 2 my $blob = $pem;
154 2 $blob =~ s/-----(BEGIN|END)\sCERTIFICATE-----//msg;
155 2 $blob = decode_base64($blob);
156 2 my $rootparse = $ASN->find("Certificate");
157 2 my $extensions = $rootparse->decode($blob);
158 2 $extensions = $extensions->{tbsCertificate}->{extensions};
159 2 foreach my $ext (@$extensions) {
160 13 my $oid = $ext->{extnID};
161 13 my $extname = $oid_2_ext{$oid};
162 13 ▶ next if !$extname;
Branch: 1 branch
BranchTrueFalseTotal
unless $extname 6 7 13
163 7 my $der = $ext->{extnValue};
164 7 my $parser = $PARSERS{$extname};
165 7 my $decoded = $parser->decode($der);
166 7 ▶ if ($extname eq 'SubjectKeyIdentifier') {
Branch: 3 branches
BranchTrueFalseTotal
if ($extname eq 'SubjectKeyIdentifier') { } 1 6 7
elsif ($extname eq 'KeyUsage') { } 2 4 6
elsif ($extname eq 'SubjectAltName') { } 1 3 4
167 1 $new_cert->set_extension(
168 "subjectKeyIdentifier", unpack('H*', $decoded));
169 } elsif ($extname eq 'KeyUsage') {
170 #Then try to figure that out, too..
171 #Apparently this module has a different way of doing things...
172 # Our sample cert doesn't seem to conform to this.. and using
173 # A parse of another module, seems to be using OIDs for
174 # ExtendedKeyUsage?
175 } elsif ($extname eq 'SubjectAltName') {
176 #ASN:
177 #
178 #GeneralName ::= CHOICE {
179 #otherName [0] AnotherName,
180 #rfc822Name [1] IA5String,
181 #dNSName [2] IA5String,
182 #x400Address [3] ANY, --ORAddress,
183 #directoryName [4] Name,
184 #ediPartyName [5] EDIPartyName,
185 #uniformResourceIdentifier [6] IA5String,
186 #iPAddress [7] OCTET STRING,
187 #registeredID [8] OBJECT IDENTIFIER }
188  
189 1 my %asn2openssl = (
190 otherName => "otherName",
191 rfc822name => "email",
192 dNSName => "DNS",
193 x400Address => "dirName",
194 #ediPartyName => "what's this?",
195 directoryName => "dirName",
196 uniformResourceIdentifier => "URI",
197 iPAddress => "IP",
198 registeredID => "RID",
199 );
200 1 my @altnames;
201 1 my $altname = "";
202 1 foreach my $h (@$decoded) {
203 2 my ($k,$v) = (%$h);
204 2 my $new_k = $asn2openssl{$k};
205 2 ▶ if (!$new_k) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_k) 0 2 2
206 0 warn "Found ASN.1 X509 field $k which doesn't have an OpenSSL mapping";
207 0 next;
208 }
209 2 $k = $new_k;
210 2 push @altnames, "$k:$v";
211 }
212 1 $altname = join(",", @altnames);
213 1 ▶ if ($alt_name_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alt_name_string) { } 0 1 1
214 0 $alt_name_string .= ",$altname";
215 } else {
216 1 $alt_name_string = $altname;
217 }
218 }
219 }
220 2 ▶ $new_cert->set_extension("subjectAltName", $alt_name_string) if $alt_name_string;
Branch: 1 branch
BranchTrueFalseTotal
if $alt_name_string 1 1 2
221 2 my $new_pem = $new_cert->sign($self->{PRIVKEY_OBJ}, $PREFERRED_ALG);
222 2 return ($new_pem, $keystr);
223 }
224  
225 1 1;
226  
227 __END__
↑ Summary Cloner/x509asn1.pm »