| 1 | package Crypt::Perl::X509::Extensions; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 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 | use Try::Tiny; | ||
| 51 | |||
| 52 | use Module::Load (); | ||
| 53 | |||
| 54 | use Crypt::Perl::ASN1 (); | ||
| 55 | |||
| 56 | use parent qw( | ||
| 57 | Crypt::Perl::ASN1::Encodee | ||
| 58 | ); | ||
| 59 | |||
| 60 | use constant OID => '1.2.840.113549.1.9.14'; | ||
| 61 | |||
| 62 | use constant ASN1 => <<END; | ||
| 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 | my $EXT_BASE = 'Crypt::Perl::X509::Extension'; | ||
| 73 | |||
| 74 | sub new { | ||
| 75 | my ($class, @extensions) = @_; | ||
| 76 | |||
| 77 | if (!@extensions) { | ||
| 78 | die Crypt::Perl::X::create('Generic', "Empty “extensions”!"); | ||
| 79 | } | ||
| 80 | |||
| 81 | for my $ext (@extensions) { | ||
| 82 | if (!try { $ext->isa($EXT_BASE) }) { | ||
| 83 | if ( 'HASH' eq ref $ext ) { | ||
| 84 | if ( !try { $ext->{'extension'}->isa($EXT_BASE) }) { | ||
| 85 | if ( 'ARRAY' ne ref $ext->{'extension'} ) { | ||
| 86 | 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 | 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 | return bless \@extensions, $class; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _new_parse_arrayref { | ||
| 100 | my ($ext) = @_; | ||
| 101 | my $module = $ext->[0]; | ||
| 102 | |||
| 103 | # For the acmeValdation-v1 extension … | ||
| 104 | $module =~ tr<-><_>; | ||
| 105 | |||
| 106 | my $class = "Crypt::Perl::X509::Extension::$module"; | ||
| 107 | Module::Load::load($class); | ||
| 108 | return $class->new( @{$ext}[ 1 .. $#$ext ] ); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _encode_params { | ||
| 112 | my ($self) = @_; | ||
| 113 | |||
| 114 | my @exts_asn1; | ||
| 115 | |||
| 116 | for my $ext ( @$self ) { | ||
| 117 | my ($critical, $real_ext); | ||
| 118 | if ('HASH' eq ref $ext) { | ||
| 119 | ($critical, $real_ext) = @{$ext}{ qw(critical extension) }; | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | $real_ext = $ext; | ||
| 123 | } | ||
| 124 | |||
| 125 | if ('ARRAY' eq ref $real_ext) { | ||
| 126 | $real_ext = _new_parse_arrayref($real_ext); | ||
| 127 | } | ||
| 128 | |||
| 129 | if (!defined $critical) { | ||
| 130 | $critical = $real_ext->can('CRITICAL'); | ||
| 131 | $critical &&= $critical->(); | ||
| 132 | } | ||
| 133 | |||
| 134 | push @exts_asn1, { | ||
| 135 | extnID => $real_ext->OID(), | ||
| 136 | ($critical ? (critical => Crypt::Perl::ASN1->ASN_BOOLEAN()) : ()), | ||
| 137 | extnValue => $real_ext->encode(), | ||
| 138 | }, | ||
| 139 | }; | ||
| 140 | |||
| 141 | return \@exts_asn1; | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; |