| 1 | package VOMS::Lite::ASN1Helper; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Math::BigInt lib => 'GMP'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 9 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 1 | %EXPORT_TAGS = ( ); | |||||||||||||||||||||||||||
| 12 | 1 | @EXPORT_OK = qw( Hex DecToHex ASN1BitStr ASN1Wrap ASN1UnwrapHex ASNLenStr ASN1Index ASN1Unwrap ASN1OIDtoOID OIDtoASN1OID); | |||||||||||||||||||||||||||
| 13 | 1 | @EXPORT = ( ); | |||||||||||||||||||||||||||
| 14 | 1 | $VERSION = '0.20'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | ################################################## | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 89 | ▶ | sub Hex { #Converts a binary string to a padded string of hex values | ||||||||||||||||||||||||||
|
Subroutine
Hex: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 89 | my $data=shift; | |||||||||||||||||||||||||||
| 20 | 89 | ▶ | return undef if ( ! defined $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 89 | my $str=unpack('H*',$data); | |||||||||||||||||||||||||||
| 22 | 89 | ▶ | return ((length($str) & 1)?"0":"").$str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | ################################################## | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 39 | ▶ | sub DecToHex { #Converts an 'integer' to a padded string of hex values | ||||||||||||||||||||||||||
|
Subroutine
DecToHex: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 39 | my $data=shift; | |||||||||||||||||||||||||||
| 29 | 39 | ▶ | return undef if ( ! defined $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 39 | ▶ | return "NaN" if ( $data !~ /^-?[0-9]+$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 39 | my $num=Math::BigInt->new("$data"); | |||||||||||||||||||||||||||
| 32 | 39 | ▶ | $num->binc() if ( $data =~ /^-/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 39 | my $str=$num->as_hex(); | |||||||||||||||||||||||||||
| 34 | 39 | $str =~ s/^-?0x//; # strip 0x and negative sign | |||||||||||||||||||||||||||
| 35 | 39 | $str =~ s/^.(..)*$/0$&/; # even up str | |||||||||||||||||||||||||||
| 36 | 39 | $str =~ s/^[89a-f]/00$&/; # convert to 2's complement as if positive | |||||||||||||||||||||||||||
| 37 | 39 | ▶ | $str =~ y/0-9a-f/fedcba9876543210/ if ( $data =~ /^-/ ); # if negative, negate | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 39 | return $str; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | ################################################## | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 75 | ▶ | sub ASN1OIDtoOID { | ||||||||||||||||||||||||||
|
Subroutine
ASN1OIDtoOID: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 75 | my ($val,$OIDstr)=(0,""); | |||||||||||||||||||||||||||
| 45 | 75 | foreach (split(//,shift)) { # Tackle OID one byte at a time | |||||||||||||||||||||||||||
| 46 | 225 | $val=($val*128)+ord($_&"\x7f"); # effectively shift $val 7 bits left add last 7 bits of $_ | |||||||||||||||||||||||||||
| 47 | 225 | ▶ | if (($_&"\x80") ne "\x80") { # If bit 8 is 0 then write append the value to the oid string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 225 | ▶ | $OIDstr .= ( ( length($OIDstr) ) ? ".$val" : (int($val/40).".".$val%40) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 225 | $val=0; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 75 | return $OIDstr; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | sub OIDtoASN1OID { | ||||||||||||||||||||||||||
|
Subroutine
OIDtoASN1OID: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( $_[0] !~ /^[0-9]+(?:\.[0-9]+)*$/ ) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 9 | my @nums=split /\./,$_[0]; | |||||||||||||||||||||||||||
| 58 | 9 | unshift(@nums,shift(@nums)*40+shift(@nums)); | |||||||||||||||||||||||||||
| 59 | 9 | my $OIDASN1str=""; | |||||||||||||||||||||||||||
| 60 | 9 | foreach (@nums) { | |||||||||||||||||||||||||||
| 61 | 27 | my $str=""; | |||||||||||||||||||||||||||
| 62 | 27 | ▶ | while ( $_ > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 27 | my $m = $_ % 128; | |||||||||||||||||||||||||||
| 64 | 27 | $_ -= $m; | |||||||||||||||||||||||||||
| 65 | 27 | ▶ | $m += 128 if ($str ne ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 27 | $str = pack('C',$m).$str; | |||||||||||||||||||||||||||
| 67 | 27 | $_/=128; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 27 | $OIDASN1str .= $str; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 9 | return $OIDASN1str; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | ################################################## | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 9 | ▶ | sub ASN1BitStr { #Converts a hex representation of a bit string to an ASN1 bitstring primitive | ||||||||||||||||||||||||||
|
Subroutine
ASN1BitStr: called CC=3 SCAR=13.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 9 | my ($data,$length)=@_; | |||||||||||||||||||||||||||
| 78 | 9 | ▶ | return undef if ( ! defined $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 9 | ▶ | if ( defined $length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $wholebytes = int($length/8); | |||||||||||||||||||||||||||
| 81 | 0 | my $mask = Hex(pack('C',(2**((8-$length)%8)-1))); | |||||||||||||||||||||||||||
| 82 | 0 | return $mask.substr($data,0,$wholebytes*2); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 9 | return "00".$data; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ################################################## | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 220 | ▶ | sub ASN1Wrap { #wraps an ASN1 structure with its ASN1 headers | ||||||||||||||||||||||||||
|
Subroutine
ASN1Wrap: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 220 | my $Header=shift; | |||||||||||||||||||||||||||
| 91 | 220 | my $Data=shift; | |||||||||||||||||||||||||||
| 92 | 220 | ▶ | return undef if (! defined $Header || ! defined $Data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $Header and defined $Data
MC/DC0%0/2
defined $Header and defined $Data
defined $Header
defined $Data
Truth table33%1/3
A and B
A= defined $HeaderB= defined $Data
| |||||||||||||||||||||||||||||
| 93 | 220 | return $Header.ASNLenStr($Data).$Data; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | ################################################## | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub ASN1UnwrapHex { | ||||||||||||||||||||||||||
|
Subroutine
ASN1UnwrapHex: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 0 | my $data=shift; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | return undef if ( ! defined $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $data=~ s/(..)/pack("C",hex($&))/ge; | |||||||||||||||||||||||||||
| 102 | 0 | return Hex(scalar ASN1Unwrap($data)); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1109 | ▶ | sub ASN1Unwrap { | ||||||||||||||||||||||||||
|
Subroutine
ASN1Unwrap: called CC=13 SCAR=27.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 1109 | my $BER=shift; | |||||||||||||||||||||||||||
| 107 | 1109 | ▶ | return (wantarray ? (0,0,undef,undef,undef,"") : "") if (! defined $BER ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 1109 | my $inheader=1; | |||||||||||||||||||||||||||
| 109 | 1109 | my ($Class,$Constructed,$Tag)=(0,0,0); | |||||||||||||||||||||||||||
| 110 | 1109 | my ($headlen,$reallen,$lenlen)=(0,0,0); | |||||||||||||||||||||||||||
| 111 | # my $i; | ||||||||||||||||||||||||||||
| 112 | 1109 | ▶ | for ( my $i = 0 ; $i <= length($BER) ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 3453 | my $C=substr $BER,$i,1; | |||||||||||||||||||||||||||
| 114 | 3453 | my @B=split(//, unpack("B*", $C)); | |||||||||||||||||||||||||||
| 115 | 3453 | ▶ | if ( $inheader==1 ) { # ID First Byte | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 116 | 1109 | $headlen=1; | |||||||||||||||||||||||||||
| 117 | 1109 | $Class= (shift @B)*2 + shift @B; | |||||||||||||||||||||||||||
| 118 | 1109 | $Constructed=shift @B; | |||||||||||||||||||||||||||
| 119 | 1109 | $Tag=unpack("N", pack("B32",substr("0" x 32 . join("",@B), -32 ))); | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($Tag==31) { $inheader=2; $Tag=0; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1109 | else { $inheader=3; } | |||||||||||||||||||||||||||
| 122 | } elsif ( $inheader==2 ) { # ID Subsequent Bytes | ||||||||||||||||||||||||||||
| 123 | 0 | $headlen++; | |||||||||||||||||||||||||||
| 124 | 0 | $inheader+=shift @B; | |||||||||||||||||||||||||||
| 125 | 0 | $Tag <<= 7; | |||||||||||||||||||||||||||
| 126 | 0 | $Tag=unpack("N", pack("B32",substr("0" x 32 . join("",@B), -32 ))); | |||||||||||||||||||||||||||
| 127 | } elsif ( $inheader==3 ) { # Length First Byte | ||||||||||||||||||||||||||||
| 128 | 1109 | $headlen++; | |||||||||||||||||||||||||||
| 129 | 1109 | ▶ | if ( shift @B ) { $inheader=4; $reallen=0; $lenlen = unpack("N", pack("B32",substr("0" x 32 . join("",@B), -32 )));} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 1021 | else { $inheader=-1; $lenlen=0; $reallen = unpack("N", pack("B32",substr("0" x 32 . join("",@B), -32 )));} | |||||||||||||||||||||||||||
| 131 | } elsif ( $inheader==4 ) { # Length Subsequent Bytes | ||||||||||||||||||||||||||||
| 132 | 126 | $headlen++; $lenlen--; | |||||||||||||||||||||||||||
| 133 | 126 | $reallen+=(unpack("N", pack("B32",substr("0" x 32 . join("",@B), -32 ))))*(256**$lenlen); | |||||||||||||||||||||||||||
| 134 | 126 | ▶ | if ( $lenlen == 0 ) { $inheader=-1;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } else { #What's left: Primative or Construction | ||||||||||||||||||||||||||||
| 136 | 1109 | ▶ | return wantarray ? ($headlen,$reallen,$Class,$Constructed,$Tag,substr($BER,$i,$reallen)) : substr($BER,$i,$reallen); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return wantarray ? (0,0,undef,undef,undef,"") : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | ################################################## | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 220 | ▶ | sub ASNLenStr { #expects Hex String returns ASN1 length header | ||||||||||||||||||||||||||
|
Subroutine
ASNLenStr: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 220 | my $data=shift; | |||||||||||||||||||||||||||
| 146 | 220 | ▶ | return undef if ( ! defined $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 220 | my $len=length($data)/2; | |||||||||||||||||||||||||||
| 149 | 220 | ▶ | if ($len <= 127) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 195 | return unpack("H2",pack("i",$len)); | |||||||||||||||||||||||||||
| 151 | } else { | ||||||||||||||||||||||||||||
| 152 | 25 | my $lenlen=sprintf "%0x",$len; | |||||||||||||||||||||||||||
| 153 | 25 | ▶ | if ( length($lenlen) & 1 ) { $lenlen='0'.$lenlen; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 25 | return sprintf("%0x%s",((length($lenlen)/2)+128),$lenlen); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | ################################################## | ||||||||||||||||||||||||||||
| 159 | # Subroutine to find structure of DER ASN.1 | ||||||||||||||||||||||||||||
| 160 | ################################################## | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 38 | ▶ | sub ASN1Index { | ||||||||||||||||||||||||||
|
Subroutine
ASN1Index: called CC=11 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 38 | my $data=shift; | |||||||||||||||||||||||||||
| 164 | 38 | ▶ | return () if ( ! defined $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 38 | my @ContentStart=(0); | |||||||||||||||||||||||||||
| 167 | 38 | my $datalength=length($data); | |||||||||||||||||||||||||||
| 168 | 38 | my @ContentStop=($datalength); | |||||||||||||||||||||||||||
| 169 | 38 | my $pointer=0; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 38 | my @total=ASN1Unwrap(substr($data,$pointer,($ContentStop[-1]-$ContentStart[-1]))); | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | if ( $total[0]+$total[1] != $datalength ) { return (); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 38 | my @data; | |||||||||||||||||||||||||||
| 175 | 38 | ▶ | while ( defined $ContentStop[-1] && $pointer < $ContentStop[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ContentStop[-1] and $pointer < $ContentStop[0]
MC/DC50%1/2
defined $ContentStop[-1] and $pointer < $ContentStop[0]
defined $ContentStop[-1]
$pointer < $ContentStop[0]
Truth table66%2/3
A and B
A= defined $ContentStop[-1]B= $pointer < $ContentStop[0]
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 996 | my ($headlen,$reallen,$Class,$Constructed,$Tag,$Data) = ASN1Unwrap(substr($data,$pointer,($ContentStop[-1]-$ContentStart[-1]))); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( ! defined $Class ) { return (); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 996 | push @ContentStart,($pointer+$headlen); | |||||||||||||||||||||||||||
| 181 | 996 | push @ContentStop,($pointer+$headlen+$reallen); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 996 | push @data, [$Class,$Constructed,$Tag,$pointer,$headlen,$reallen]; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 996 | $pointer+=$headlen; | |||||||||||||||||||||||||||
| 186 | 996 | ▶ | $pointer+=$reallen if ($Constructed==0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 996 | ▶ | while ( defined $ContentStop[-1] && $pointer == $ContentStop[-1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ContentStop[-1] and $pointer == $ContentStop[-1]
MC/DC100%2/2
defined $ContentStop[-1] and $pointer == $ContentStop[-1]
defined $ContentStop[-1]
$pointer == $ContentStop[-1]
Truth table100%3/3
A and B
A= defined $ContentStop[-1]B= $pointer == $ContentStop[-1]
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ( ! defined pop @ContentStop) { return (); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if ( ! defined pop @ContentStart) { return (); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 38 | return @data; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | 1; | |||||||||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||||||||||