| 1 | package Crypt::OpenSSL::X509; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | ▶ | use Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | ▶ | use Convert::ASN1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 4 | ▶ | use version; our $VERSION = version->declare('2.1.5'); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | our @EXPORT_OK = qw( | |||||||||||||
| 14 | FORMAT_UNDEF FORMAT_ASN1 FORMAT_TEXT FORMAT_PEM | ||||||||||||||
| 15 | FORMAT_PKCS12 FORMAT_SMIME FORMAT_ENGINE FORMAT_IISSGC OPENSSL_VERSION_NUMBER | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 2 | ▶ | sub Crypt::OpenSSL::X509::has_extension_oid { | ||||||||||||
|
Subroutine
has_extension_oid: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 19 | 2 | my $x509 = shift; | |||||||||||||
| 20 | 2 | my $oid = shift; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 2 | ▶ | return ${$x509->extensions_by_oid}{$oid} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 3 | ▶ | sub Crypt::OpenSSL::X509::Extension::is_critical { | ||||||||||||
|
Subroutine
is_critical: called CC=2 SCAR=6.9
| |||||||||||||||
| 26 | 3 | my $ext = shift; | |||||||||||||
| 27 | 3 | my $crit = $ext->critical(); | |||||||||||||
| 28 | |||||||||||||||
| 29 | 3 | ▶ | return $crit ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | # return a hash for the values of keyUsage or nsCertType | ||||||||||||||
| 33 | 1 | ▶ | sub Crypt::OpenSSL::X509::Extension::hash_bit_string { | ||||||||||||
|
Subroutine
hash_bit_string: called CC=3 SCAR=12.0
| |||||||||||||||
| 34 | 1 | my $ext = shift; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 1 | my @bits = split(//, $ext->bit_string); | |||||||||||||
| 37 | 1 | my $len = @bits; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 1 | my %bit_str_hash = (); | |||||||||||||
| 40 | |||||||||||||||
| 41 | 1 | ▶ | if ($len == 9) { # bits for keyUsage | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 1 | %bit_str_hash = ( | |||||||||||||
| 44 | 'Digital Signature' => $bits[0], | ||||||||||||||
| 45 | 'Non Repudiation' => $bits[1], | ||||||||||||||
| 46 | 'Key Encipherment' => $bits[2], | ||||||||||||||
| 47 | 'Data Encipherment' => $bits[3], | ||||||||||||||
| 48 | 'Key Agreement' => $bits[4], | ||||||||||||||
| 49 | 'Certificate Sign' => $bits[5], | ||||||||||||||
| 50 | 'CRL Sign' => $bits[6], | ||||||||||||||
| 51 | 'Encipher Only' => $bits[7], | ||||||||||||||
| 52 | 'Decipher Only' => $bits[8],); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | } elsif ($len == 8) { #bits for nsCertType | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | %bit_str_hash = ( | |||||||||||||
| 57 | 'SSL Client' => $bits[0], | ||||||||||||||
| 58 | 'SSL Server' => $bits[1], | ||||||||||||||
| 59 | 'S/MIME' => $bits[2], | ||||||||||||||
| 60 | 'Object Signing' => $bits[3], | ||||||||||||||
| 61 | 'Unused' => $bits[4], | ||||||||||||||
| 62 | 'SSL CA' => $bits[5], | ||||||||||||||
| 63 | 'S/MIME CA' => $bits[6], | ||||||||||||||
| 64 | 'Object Signing CA' => $bits[7],); | ||||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 1 | return %bit_str_hash; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | ▶ | sub Crypt::OpenSSL::X509::Extension::extKeyUsage { | ||||||||||||
|
Subroutine
extKeyUsage: not called CC=1 SCAR=6.9
| |||||||||||||||
| 71 | 0 | my $ext = shift; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | my @vals = split(/ /, $ext->extendedKeyUsage); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | return @vals; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 1 | ▶ | sub Crypt::OpenSSL::X509::is_selfsigned { | ||||||||||||
|
Subroutine
is_selfsigned: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 79 | 1 | my $x509 = shift; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | return $x509->subject eq $x509->issuer; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 8 | ▶ | sub Crypt::OpenSSL::X509::subjectaltname { | ||||||||||||
|
Subroutine
subjectaltname: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 85 | 8 | my $x509 = shift; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 8 | my $SUBJECT_ALT_NAME_OID = '2.5.29.17'; | |||||||||||||
| 88 | 8 | my $ext; | |||||||||||||
| 89 | 8 | eval { | |||||||||||||
| 90 | # extensions_by_oid croaks of no extensions found | ||||||||||||||
| 91 | # we don't care we will return an empty array | ||||||||||||||
| 92 | 8 | $ext = $x509->extensions_by_oid(); | |||||||||||||
| 93 | }; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # Determine whether the SubjectAltName exist | ||||||||||||||
| 96 | 8 | ▶ | if (! defined ${$ext}{$SUBJECT_ALT_NAME_OID}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | # Simply return a reference to an empty array if it does not exist | ||||||||||||||
| 98 | 5 | my @tmp = (); | |||||||||||||
| 99 | 5 | return \@tmp; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 3 | my $pdu = ${$ext}{$SUBJECT_ALT_NAME_OID}->value(); | |||||||||||||
| 103 | |||||||||||||||
| 104 | # remove leading '#' from the value returned | ||||||||||||||
| 105 | 3 | $pdu =~ s/^#//g; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 3 | my $bin_data = join '', pack 'H*', $pdu; | |||||||||||||
| 108 | 3 | my $asn = Convert::ASN1->new(); | |||||||||||||
| 109 | |||||||||||||||
| 110 | 3 | my $ok = $asn->prepare(q< | |||||||||||||
| 111 | AnotherName ::= SEQUENCE { | ||||||||||||||
| 112 | type OBJECT IDENTIFIER, | ||||||||||||||
| 113 | value [0] EXPLICIT ANY } --DEFINED BY type-id } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | EDIPartyName ::= SEQUENCE { | ||||||||||||||
| 116 | nameAssigner [0] DirectoryString OPTIONAL, | ||||||||||||||
| 117 | partyName [1] DirectoryString } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | -- Directory string type -- | ||||||||||||||
| 120 | |||||||||||||||
| 121 | DirectoryString ::= CHOICE { | ||||||||||||||
| 122 | teletexString TeletexString, --(SIZE (1..MAX)), | ||||||||||||||
| 123 | printableString PrintableString, --(SIZE (1..MAX)), | ||||||||||||||
| 124 | bmpString BMPString, --(SIZE (1..MAX)), | ||||||||||||||
| 125 | universalString UniversalString, --(SIZE (1..MAX)), | ||||||||||||||
| 126 | utf8String UTF8String, --(SIZE (1..MAX)), | ||||||||||||||
| 127 | ia5String IA5String --added for EmailAddress | ||||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | AttributeType ::= OBJECT IDENTIFIER | ||||||||||||||
| 131 | |||||||||||||||
| 132 | AttributeValue ::= DirectoryString --ANY | ||||||||||||||
| 133 | |||||||||||||||
| 134 | AttributeTypeAndValue ::= SEQUENCE { | ||||||||||||||
| 135 | type AttributeType, | ||||||||||||||
| 136 | value AttributeValue | ||||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | -- naming data types -- | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Name ::= CHOICE { -- only one possibility for now | ||||||||||||||
| 142 | rdnSequence RDNSequence | ||||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | RDNSequence ::= SEQUENCE OF RelativeDistinguishedName | ||||||||||||||
| 146 | |||||||||||||||
| 147 | DistinguishedName ::= RDNSequence | ||||||||||||||
| 148 | |||||||||||||||
| 149 | RelativeDistinguishedName ::= | ||||||||||||||
| 150 | SET OF AttributeTypeAndValue --SET SIZE (1 .. MAX) OF | ||||||||||||||
| 151 | |||||||||||||||
| 152 | SubjectAltName ::= GeneralNames | ||||||||||||||
| 153 | |||||||||||||||
| 154 | GeneralNames ::= SEQUENCE OF GeneralName | ||||||||||||||
| 155 | |||||||||||||||
| 156 | GeneralName ::= CHOICE { | ||||||||||||||
| 157 | rfc822Name [1] IA5String, | ||||||||||||||
| 158 | dNSName [2] IA5String, | ||||||||||||||
| 159 | x400Address [3] ANY, --ORAddress, | ||||||||||||||
| 160 | directoryName [4] Name, | ||||||||||||||
| 161 | ediPartyName [5] EDIPartyName, | ||||||||||||||
| 162 | uniformResourceIdentifier [6] IA5String, | ||||||||||||||
| 163 | iPAddress [7] OCTET STRING, | ||||||||||||||
| 164 | registeredID [8] OBJECT IDENTIFIER | ||||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | >); | ||||||||||||||
| 168 | 3 | ▶ | die '*** Could not prepare definition: '.$asn->error() | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | if !$ok; | ||||||||||||||
| 170 | |||||||||||||||
| 171 | # This is an important bit - if you don't do the find the decode | ||||||||||||||
| 172 | # will randomly fail/succeed. This is required to work | ||||||||||||||
| 173 | 3 | ▶ | my $asn_node = $asn->find('SubjectAltName') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | or die $asn->error; | ||||||||||||||
| 175 | |||||||||||||||
| 176 | 3 | ▶ | my $san = $asn_node->decode($bin_data) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 177 | or die 'Unable to decode SubjectAltName: '.$asn_node->error; | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 3 | return $san; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 4 | ▶ | use XSLoader; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 183 | 4 | XSLoader::load 'Crypt::OpenSSL::X509', $VERSION; | |||||||||||||
| 184 | |||||||||||||||
| 185 | 4 | ▶ | END { | ||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||
| 186 | 4 | __PACKAGE__->__X509_cleanup; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | |||||||||||||||
| 189 | 4 | 1; | |||||||||||||
| 190 | |||||||||||||||
| 191 | __END__ | ||||||||||||||