| 1 | package Crypt::Perl::X509::Extensions; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Crypt::Perl::X509::Extensions - extensions list for X.509 certificates | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | #Each object passed should be an instance of a subclass of | ||||||||||||||||||||
| 15 | #Crypt::Perl::X509::Extension | ||||||||||||||||||||
| 16 | my $exreq = Crypt::Perl::X509::Extensions->new( @EXTN_OBJS ); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | #...or: | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | my $exreq = Crypt::Perl::X509::Extensions->new( | ||||||||||||||||||||
| 21 | [ $extn_type1 => @args1 ], | ||||||||||||||||||||
| 22 | [ $extn_type2 => @args2 ], | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | #...for example: | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | my $exreq = Crypt::Perl::X509::Extensions->new( | ||||||||||||||||||||
| 28 | [ 'subjectAltName', | ||||||||||||||||||||
| 29 | [ dNSName => 'foo.com' ], | ||||||||||||||||||||
| 30 | [ dNSName => 'haha.tld' ], | ||||||||||||||||||||
| 31 | ], | ||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | Instances of this class represent the list of extensions in an X.509 (SSL) | ||||||||||||||||||||
| 37 | certificate. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | You probably don’t need to | ||||||||||||||||||||
| 40 | instantiate this class directly; instead, you can instantiate it | ||||||||||||||||||||
| 41 | implicitly by listing out arguments to | ||||||||||||||||||||
| 42 | L<Crypt::Perl::X509v3>’s constructor. See that module’s | ||||||||||||||||||||
| 43 | L<SYNOPSIS|Crypt::Perl::X509v3/SYNOPSIS> for an example. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | Look in the L<Crypt::Perl> distribution’s | ||||||||||||||||||||
| 46 | C<Crypt::Perl::X509::Extension> namespace for supported extensions. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 3 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 3 | ▶ | use Module::Load (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 3 | ▶ | use Crypt::Perl::ASN1 (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 3 | use parent qw( | |||||||||||||||||||
| 57 | Crypt::Perl::ASN1::Encodee | ||||||||||||||||||||
| 58 | 3 | ▶ | ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 3 | ▶ | use constant OID => '1.2.840.113549.1.9.14'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 3 | ▶ | use constant ASN1 => <<END; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | Extension ::= SEQUENCE { | ||||||||||||||||||||
| 64 | extnID OBJECT IDENTIFIER, | ||||||||||||||||||||
| 65 | critical BOOLEAN OPTIONAL, | ||||||||||||||||||||
| 66 | extnValue OCTET STRING | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | Extensions ::= SEQUENCE OF Extension | ||||||||||||||||||||
| 70 | END | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 3 | my $EXT_BASE = 'Crypt::Perl::X509::Extension'; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | sub new { | ||||||||||||||||||||
| 75 | 17 | ▶ | my ($class, @extensions) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 17 | ▶ | if (!@extensions) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | die Crypt::Perl::X::create('Generic', "Empty “extensions”!"); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 17 | for my $ext (@extensions) { | |||||||||||||||||||
| 82 | 137 | ▶ | if (!try { $ext->isa($EXT_BASE) }) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 136 | ▶ | if ( 'HASH' eq ref $ext ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 84 | 1 | ▶ | if ( !try { $ext->{'extension'}->isa($EXT_BASE) }) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | ▶ | if ( 'ARRAY' ne ref $ext->{'extension'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | die Crypt::Perl::X::create('Generic', "“extension” in HASH reference must be ARRAY reference or instance of $EXT_BASE, not “$ext”!"); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | elsif ( 'ARRAY' ne ref $ext ) { | ||||||||||||||||||||
| 91 | 0 | die Crypt::Perl::X::create('Generic', "Extension must be HASH reference, ARRAY reference, or instance of $EXT_BASE, not “$ext”!"); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 17 | return bless \@extensions, $class; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub _new_parse_arrayref { | ||||||||||||||||||||
| 100 | 135 | ▶ | my ($ext) = @_; | ||||||||||||||||||
|
Subroutine
_new_parse_arrayref: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 101 | 135 | my $module = $ext->[0]; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # For the acmeValdation-v1 extension … | ||||||||||||||||||||
| 104 | 135 | $module =~ tr<-><_>; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 135 | my $class = "Crypt::Perl::X509::Extension::$module"; | |||||||||||||||||||
| 107 | 135 | Module::Load::load($class); | |||||||||||||||||||
| 108 | 135 | return $class->new( @{$ext}[ 1 .. $#$ext ] ); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub _encode_params { | ||||||||||||||||||||
| 112 | 17 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_encode_params: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 17 | my @exts_asn1; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 17 | for my $ext ( @$self ) { | |||||||||||||||||||
| 117 | 137 | my ($critical, $real_ext); | |||||||||||||||||||
| 118 | 137 | ▶ | if ('HASH' eq ref $ext) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 1 | ($critical, $real_ext) = @{$ext}{ qw(critical extension) }; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||
| 122 | 136 | $real_ext = $ext; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 137 | ▶ | if ('ARRAY' eq ref $real_ext) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 135 | $real_ext = _new_parse_arrayref($real_ext); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 137 | ▶ | if (!defined $critical) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 136 | $critical = $real_ext->can('CRITICAL'); | |||||||||||||||||||
| 131 | 136 | ▶ | $critical &&= $critical->(); | ||||||||||||||||||
|
Condition100%3/3
$critical &&= &$critical()
MC/DC100%2/2
$critical &&= &$critical()
$critical
&$critical()
Truth table100%3/3
A &&= B
A= $criticalB= &$critical()
| |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 137 | ▶ | push @exts_asn1, { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | extnID => $real_ext->OID(), | ||||||||||||||||||||
| 136 | ($critical ? (critical => Crypt::Perl::ASN1->ASN_BOOLEAN()) : ()), | ||||||||||||||||||||
| 137 | extnValue => $real_ext->encode(), | ||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 17 | return \@exts_asn1; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 3 | 1; | |||||||||||||||||||