| 1 | package Crypt::Perl::X509v3; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Crypt::Perl::X509v3 - TLS/SSL Certificates | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | my $cert = Crypt::Perl::X509v3->new( | ||||||||||||||||||||
| 15 | key => $crypt_perl_public_key_obj, | ||||||||||||||||||||
| 16 | issuer => [ | ||||||||||||||||||||
| 17 | [ commonName => 'Foo', surname => 'theIssuer' ], | ||||||||||||||||||||
| 18 | [ givenName => 'separate RDNs' ], | ||||||||||||||||||||
| 19 | ], | ||||||||||||||||||||
| 20 | subject => \@subject, #same format as issuer | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | not_before => $unixtime, | ||||||||||||||||||||
| 23 | not_after => $unixtime, | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # The same structure as in Crypt::Perl::PKCS10 … | ||||||||||||||||||||
| 26 | extensions => [ | ||||||||||||||||||||
| 27 | [ keyUsage => 'keyCertSign', 'keyEncipherment' ], | ||||||||||||||||||||
| 28 | [ $extn_name => @extn_args ], | ||||||||||||||||||||
| 29 | # .. | ||||||||||||||||||||
| 30 | ], | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | serial_number => 12345, | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | issuer_unique_id => '..', | ||||||||||||||||||||
| 35 | subject_unique_id => '..', | ||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | $cert->sign( $crypt_perl_private_key_obj, 'sha256' ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | my $pem = $cert->to_pem(); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 STATUS | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | This module is B<experimental>! The API may change between versions. | ||||||||||||||||||||
| 45 | If you’re going to build something off of it, ensure that you check | ||||||||||||||||||||
| 46 | Crypt::Perl’s changelog before updating this module. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | This module can create TLS/SSL certificates. The caller has full control | ||||||||||||||||||||
| 51 | over all certificate components, and anything not specified is not assumed. | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | There currently is not a parsing interface. Hopefully that can be remedied. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 1 | ▶ | use parent qw( Crypt::Perl::ASN1::Encodee ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 1 | ▶ | use Digest::SHA (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | ▶ | use Crypt::Perl::ASN1::Signatures (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 1 | ▶ | use Crypt::Perl::X509::Extensions (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 1 | ▶ | use Crypt::Perl::X509::Name (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | ▶ | use Crypt::Perl::X (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | #TODO: refactor | ||||||||||||||||||||
| 68 | 1 | *to_der = __PACKAGE__->can('encode'); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | sub to_pem { | ||||||||||||||||||||
| 71 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
to_pem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 6 | return Crypt::Format::der2pem( $self->to_der(), 'CERTIFICATE' ); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | ▶ | use constant ASN1 => <<END; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | X509v3 ::= SEQUENCE { | ||||||||||||||||||||
| 78 | tbsCertificate ANY, | ||||||||||||||||||||
| 79 | signatureAlgorithm SigIdentifier, | ||||||||||||||||||||
| 80 | signature BIT STRING | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | SigIdentifier ::= SEQUENCE { | ||||||||||||||||||||
| 84 | algorithm OBJECT IDENTIFIER, | ||||||||||||||||||||
| 85 | parameters ANY OPTIONAL | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | TBSCertificate ::= SEQUENCE { | ||||||||||||||||||||
| 89 | version [0] Version, | ||||||||||||||||||||
| 90 | serialNumber INTEGER, | ||||||||||||||||||||
| 91 | signature SigIdentifier, | ||||||||||||||||||||
| 92 | issuer ANY, -- Name | ||||||||||||||||||||
| 93 | validity Validity, | ||||||||||||||||||||
| 94 | subject ANY, -- Name | ||||||||||||||||||||
| 95 | subjectPublicKeyInfo ANY, | ||||||||||||||||||||
| 96 | issuerUniqueID [1] IMPLICIT BIT STRING OPTIONAL, | ||||||||||||||||||||
| 97 | -- If present, version MUST be v2 or v3 | ||||||||||||||||||||
| 98 | subjectUniqueID [2] IMPLICIT BIT STRING OPTIONAL, | ||||||||||||||||||||
| 99 | -- If present, version MUST be v2 or v3 | ||||||||||||||||||||
| 100 | extensions [3] Extensions OPTIONAL | ||||||||||||||||||||
| 101 | -- If present, version MUST be v3 -- | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | Version ::= SEQUENCE { | ||||||||||||||||||||
| 105 | version INTEGER | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Validity ::= SEQUENCE { | ||||||||||||||||||||
| 109 | notBefore Time, | ||||||||||||||||||||
| 110 | notAfter Time | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | Time ::= CHOICE { | ||||||||||||||||||||
| 114 | -- utcTime UTCTime, -- Y2K problem … wtf?!? | ||||||||||||||||||||
| 115 | generalTime GeneralizedTime | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | Extensions ::= SEQUENCE { | ||||||||||||||||||||
| 119 | extensions ANY | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | END | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | sub new { | ||||||||||||||||||||
| 124 | 6 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 6 | my @missing = grep { !$opts{$_} } qw( subject key not_after ); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 6 | ▶ | if (@missing) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | die Crypt::Perl::X::create('Generic', "Missing: @missing"); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 6 | ▶ | $opts{'extensions'} &&= Crypt::Perl::X509::Extensions->new(@{ $opts{'extensions'} }); | ||||||||||||||||||
|
Condition33%1/3
$opts{"extensions"} &&= "Crypt::Perl::X509::Extensions"->new(@{$opts{"extensions"};})
MC/DC0%0/2
$opts{"extensions"} &&= "Crypt::Perl::X509::Extensions"->new(@{$opts{"extensions"};})
$opts{"extensions"}
"Crypt::Perl::X509::Extensions"->new(@{$opts{"extensions"};})
Truth table33%1/3
A &&= B
A= $opts{"extensions"}B= "Crypt::Perl::X509::Extensions"->new(@{$opts{"extensions"};})
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 6 | my $subj = Crypt::Perl::X509::Name->new( @{ $opts{'subject'} } ); | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 6 | my $issuer; | |||||||||||||||||||
| 137 | 6 | ▶ | if ($opts{'issuer'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 6 | $issuer = Crypt::Perl::X509::Name->new( @{ $opts{'issuer'} } ); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||
| 141 | 0 | $issuer = $subj; #self-signed | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 6 | ▶ | $opts{'serial_number'} ||= 0; | ||||||||||||||||||
|
Condition50%1/2
$opts{"serial_number"} ||= 0
MC/DC0%0/1
$opts{"serial_number"} ||= 0
$opts{"serial_number"}
Truth table50%1/2
A
A= $opts{"serial_number"}
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | my %self = ( | ||||||||||||||||||||
| 147 | _subject => $subj, | ||||||||||||||||||||
| 148 | _issuer => $issuer, | ||||||||||||||||||||
| 149 | _not_before => $opts{'not_before'} || time, | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 6 | ▶ | ( map { ( "_$_" => $opts{$_} ) } qw( | ||||||||||||||||||
|
Condition33%1/3
$opts{'not_before'} || time
MC/DC0%0/2
$opts{'not_before'} || time
$opts{'not_before'}
time
Truth table33%1/3
A || B
A= $opts{'not_before'}B= time
| |||||||||||||||||||||
| 152 | key | ||||||||||||||||||||
| 153 | not_after | ||||||||||||||||||||
| 154 | extensions | ||||||||||||||||||||
| 155 | serial_number | ||||||||||||||||||||
| 156 | subject_unique_id | ||||||||||||||||||||
| 157 | issuer_unique_id | ||||||||||||||||||||
| 158 | ) ), | ||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 6 | return bless \%self, $class; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | sub sign { | ||||||||||||||||||||
| 165 | 6 | ▶ | my ($self, $signer_key, $digest_algorithm) = @_; | ||||||||||||||||||
|
Subroutine
sign: called CC=5 SCAR=16.5
Pod
undocumented
| |||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | #This validates the digest algorithm. | ||||||||||||||||||||
| 168 | 6 | my $tbs = $self->_encode_tbs_certificate($signer_key, $digest_algorithm); | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 6 | my ($sig_alg, $sig_func, $signature); | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 6 | ▶ | $digest_algorithm =~ m<([0-9]+)\z> or die "huh? ($digest_algorithm)"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 6 | my $digest_length = $1; | |||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 6 | ▶ | if ($signer_key->isa('Crypt::Perl::ECDSA::PrivateKey')) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 176 | #$sig_alg = $signer_key->get_public_key()->algorithm_identifier_with_curve_name(); | ||||||||||||||||||||
| 177 | 3 | $sig_alg = "ecdsa-with-SHA$digest_length"; | |||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 3 | $signature = $signer_key->sign( Digest::SHA->can($digest_algorithm)->($tbs) ); | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | elsif ($signer_key->isa('Crypt::Perl::RSA::PrivateKey')) { | ||||||||||||||||||||
| 182 | #$sig_alg = $signer_key->get_public_key()->algorithm_identifier(); | ||||||||||||||||||||
| 183 | 3 | $sig_alg = "sha${digest_length}WithRSAEncryption"; | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 3 | ▶ | my $sign_cr = $signer_key->can("sign_RS$digest_length") or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | 0 | die "Unsupported digest for RSA: $digest_algorithm"; | |||||||||||||||||||
| 187 | }; | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 3 | $signature = $sign_cr->($signer_key, $tbs); | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | else { | ||||||||||||||||||||
| 192 | 0 | die "Key ($signer_key) is not a recognized private key object!"; | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | $sig_alg = { | ||||||||||||||||||||
| 196 | 6 | algorithm => $Crypt::Perl::ASN1::Signatures::OID{$sig_alg}, | |||||||||||||||||||
| 197 | }; | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 6 | $self->{'_signed'} = { | |||||||||||||||||||
| 200 | tbsCertificate => $tbs, | ||||||||||||||||||||
| 201 | signatureAlgorithm => $sig_alg, | ||||||||||||||||||||
| 202 | signature => $signature, | ||||||||||||||||||||
| 203 | }; | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 6 | return $self; | |||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | sub _encode_params { | ||||||||||||||||||||
| 209 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_encode_params: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 6 | ▶ | if (!$self->{'_signed'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 212 | 0 | die Crypt::Perl::X::create('Generic', 'Call sign() first!'); | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 6 | return $self->{'_signed'}; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | sub _encode_tbs_certificate { | ||||||||||||||||||||
| 219 | 6 | ▶ | my ($self, $signing_key, $digest_algorithm) = @_; | ||||||||||||||||||
|
Subroutine
_encode_tbs_certificate: called CC=6 SCAR=18.4
| |||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 6 | ▶ | $digest_algorithm =~ m<\Asha(224|256|384|512)\z> or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | 0 | die Crypt::Perl::X::create('Generic', "Unknown digest algorithm: “$digest_algorithm”"); | |||||||||||||||||||
| 223 | }; | ||||||||||||||||||||
| 224 | 6 | my $digest_length = $1; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 6 | my $sig_alg; | |||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | my $pubkey_der; | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 6 | ▶ | if ($self->{'_key'}->isa('Crypt::Perl::ECDSA::PublicKey')) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 231 | 3 | $pubkey_der = $self->{'_key'}->to_der_with_curve_name(); | |||||||||||||||||||
| 232 | #$sig_alg = 'ecPublicKey'; #"ecdsa-with-SHA$digest_length"; | ||||||||||||||||||||
| 233 | 3 | $sig_alg = "ecdsa-with-SHA$digest_length"; | |||||||||||||||||||
| 234 | } | ||||||||||||||||||||
| 235 | elsif ($self->{'_key'}->isa('Crypt::Perl::RSA::PublicKey')) { | ||||||||||||||||||||
| 236 | 3 | $pubkey_der = $self->{'_key'}->to_subject_der(); | |||||||||||||||||||
| 237 | #$sig_alg = 'rsaEncryption'; #"sha${digest_length}WithRSAEncryption"; | ||||||||||||||||||||
| 238 | 3 | $sig_alg = "sha${digest_length}WithRSAEncryption"; | |||||||||||||||||||
| 239 | } | ||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||
| 241 | 0 | die "Key ($self->{'_key'}) is not a recognized public key object!"; | |||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 6 | my $extns_bin; | |||||||||||||||||||
| 245 | 6 | ▶ | if ($self->{'_extensions'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 246 | 6 | $extns_bin = $self->{'_extensions'}->encode(); | |||||||||||||||||||
| 247 | } | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | my $params_hr = { | ||||||||||||||||||||
| 250 | version => { version => 2 }, | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | serialNumber => $self->{'_serial_number'}, | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | issuerUniqueID => $self->{'_issuer_unique_id'}, | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | subjectUniqueID => $self->{'_subject_unique_id'}, | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | subject => $self->{'_subject'}->encode(), | ||||||||||||||||||||
| 259 | issuer => $self->{'_issuer'}->encode(), | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | validity => { | ||||||||||||||||||||
| 262 | notBefore => { generalTime => $self->{'_not_before'} }, | ||||||||||||||||||||
| 263 | notAfter => { generalTime => $self->{'_not_after'} }, | ||||||||||||||||||||
| 264 | }, | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | subjectPublicKeyInfo => $pubkey_der, | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | signature => { | ||||||||||||||||||||
| 269 | 6 | ▶ | algorithm => $Crypt::Perl::ASN1::Signatures::OID{$sig_alg}, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 270 | }, | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | ( $extns_bin ? ( extensions => { extensions => $extns_bin } ) : () ), | ||||||||||||||||||||
| 273 | }; | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | 6 | my $asn1 = Crypt::Perl::ASN1->new()->prepare($self->ASN1()); | |||||||||||||||||||
| 276 | 6 | $asn1 = $asn1->find('TBSCertificate'); | |||||||||||||||||||
| 277 | 6 | $asn1->configure( encode => { time => 'utctime' } ); | |||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | 6 | return $asn1->encode($params_hr); | |||||||||||||||||||
| 280 | } | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | #sub _get_GeneralizedTime { | ||||||||||||||||||||
| 283 | # my ($epoch) = @_; | ||||||||||||||||||||
| 284 | # | ||||||||||||||||||||
| 285 | # my @smhdmy = (gmtime $epoch)[0 .. 5]; | ||||||||||||||||||||
| 286 | # $smhdmy[4]++; #month | ||||||||||||||||||||
| 287 | # $smhdmy[5] += 1900; #year | ||||||||||||||||||||
| 288 | # | ||||||||||||||||||||
| 289 | # return sprintf '%04d%02d%02d%02d%02d%02dZ', reverse @smhdmy; | ||||||||||||||||||||
| 290 | #} | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | 1 | 1; | |||||||||||||||||||