| 1 | # Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||||||||||||||||||||||||||||
| 2 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 3 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Convert::ASN1; | ||||||||||||||||||||||||||||
| 6 | 24 | $Convert::ASN1::VERSION = '0.36'; | |||||||||||||||||||||||||||
| 7 | 24 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS @opParts @opName $AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 24 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 24 | ▶ | use constant CHECK_UTF8 => $] > 5.007; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 24 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 15 | 24 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||
| 16 | 24 | ▶ | eval { require bytes and 'bytes'->import }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 24 | if (CHECK_UTF8) { | |||||||||||||||||||||||||||
| 19 | 24 | require Encode; | |||||||||||||||||||||||||||
| 20 | 24 | require utf8; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 24 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 24 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 26 | io => [qw(asn_recv asn_send asn_read asn_write asn_get asn_ready)], | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | debug => [qw(asn_dump asn_hexdump)], | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | const => [qw(ASN_BOOLEAN ASN_INTEGER ASN_BIT_STR ASN_OCTET_STR | ||||||||||||||||||||||||||||
| 31 | ASN_NULL ASN_OBJECT_ID ASN_REAL ASN_ENUMERATED | ||||||||||||||||||||||||||||
| 32 | ASN_SEQUENCE ASN_SET ASN_PRINT_STR ASN_IA5_STR | ||||||||||||||||||||||||||||
| 33 | ASN_UTC_TIME ASN_GENERAL_TIME ASN_RELATIVE_OID | ||||||||||||||||||||||||||||
| 34 | ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE | ||||||||||||||||||||||||||||
| 35 | ASN_PRIMITIVE ASN_CONSTRUCTOR ASN_LONG_LEN ASN_EXTENSION_ID ASN_BIT)], | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | tag => [qw(asn_tag asn_decode_tag2 asn_decode_tag asn_encode_tag asn_decode_length asn_encode_length)] | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 24 | @EXPORT_OK = map { @$_ } values %EXPORT_TAGS; | |||||||||||||||||||||||||||
| 41 | 24 | $EXPORT_TAGS{all} = \@EXPORT_OK; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 24 | @opParts = qw( | |||||||||||||||||||||||||||
| 44 | cTAG cTYPE cVAR cLOOP cOPT cEXT cCHILD cDEFINE | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 24 | @opName = qw( | |||||||||||||||||||||||||||
| 48 | opUNKNOWN opBOOLEAN opINTEGER opBITSTR opSTRING opNULL opOBJID opREAL | ||||||||||||||||||||||||||||
| 49 | opSEQUENCE opEXPLICIT opSET opUTIME opGTIME opUTF8 opANY opCHOICE opROID opBCD | ||||||||||||||||||||||||||||
| 50 | opEXTENSIONS | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 24 | foreach my $l (\@opParts, \@opName) { | |||||||||||||||||||||||||||
| 54 | 48 | my $i = 0; | |||||||||||||||||||||||||||
| 55 | 48 | foreach my $name (@$l) { | |||||||||||||||||||||||||||
| 56 | 648 | my $j = $i++; | |||||||||||||||||||||||||||
| 57 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | *{__PACKAGE__ . '::' . $name} = sub () { $j } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 59 | 648 | } | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 24 | ▶ | sub _internal_syms { | ||||||||||||||||||||||||||
|
Subroutine
_internal_syms: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 24 | my $pkg = caller; | |||||||||||||||||||||||||||
| 65 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 24 | for my $sub (@opParts,@opName,'dump_op') { | |||||||||||||||||||||||||||
| 67 | 672 | *{$pkg . '::' . $sub} = \&{__PACKAGE__ . '::' . $sub}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub ASN_BOOLEAN () { 0x01 } | ||||||||||||||||||||||||||||
| 72 | sub ASN_INTEGER () { 0x02 } | ||||||||||||||||||||||||||||
| 73 | sub ASN_BIT_STR () { 0x03 } | ||||||||||||||||||||||||||||
| 74 | sub ASN_OCTET_STR () { 0x04 } | ||||||||||||||||||||||||||||
| 75 | sub ASN_NULL () { 0x05 } | ||||||||||||||||||||||||||||
| 76 | sub ASN_OBJECT_ID () { 0x06 } | ||||||||||||||||||||||||||||
| 77 | sub ASN_REAL () { 0x09 } | ||||||||||||||||||||||||||||
| 78 | sub ASN_ENUMERATED () { 0x0A } | ||||||||||||||||||||||||||||
| 79 | sub ASN_RELATIVE_OID () { 0x0D } | ||||||||||||||||||||||||||||
| 80 | sub ASN_SEQUENCE () { 0x10 } | ||||||||||||||||||||||||||||
| 81 | sub ASN_SET () { 0x11 } | ||||||||||||||||||||||||||||
| 82 | sub ASN_PRINT_STR () { 0x13 } | ||||||||||||||||||||||||||||
| 83 | sub ASN_IA5_STR () { 0x16 } | ||||||||||||||||||||||||||||
| 84 | sub ASN_UTC_TIME () { 0x17 } | ||||||||||||||||||||||||||||
| 85 | sub ASN_GENERAL_TIME () { 0x18 } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub ASN_UNIVERSAL () { 0x00 } | ||||||||||||||||||||||||||||
| 88 | sub ASN_APPLICATION () { 0x40 } | ||||||||||||||||||||||||||||
| 89 | sub ASN_CONTEXT () { 0x80 } | ||||||||||||||||||||||||||||
| 90 | sub ASN_PRIVATE () { 0xC0 } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub ASN_PRIMITIVE () { 0x00 } | ||||||||||||||||||||||||||||
| 93 | sub ASN_CONSTRUCTOR () { 0x20 } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub ASN_LONG_LEN () { 0x80 } | ||||||||||||||||||||||||||||
| 96 | sub ASN_EXTENSION_ID () { 0x1F } | ||||||||||||||||||||||||||||
| 97 | sub ASN_BIT () { 0x80 } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 33 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 33 | my $pkg = shift; | |||||||||||||||||||||||||||
| 102 | 33 | my $self = bless {}, $pkg; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 33 | $self->configure(@_); | |||||||||||||||||||||||||||
| 105 | 33 | $self; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 40 | ▶ | sub configure { | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=8 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 40 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 40 | my %opt = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 40 | ▶ | $self->{options}{encoding} = uc($opt{encoding} || 'BER'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$opt{'encoding'} || 'BER'
MC/DC100%1/1
$opt{'encoding'} || 'BER'
$opt{'encoding'}
Truth table100%2/2
A
A= $opt{'encoding'}
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 40 | ▶ | unless ($self->{options}{encoding} =~ /^[BD]ER$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | require Carp; | |||||||||||||||||||||||||||
| 117 | 0 | Carp::croak("Unsupported encoding format '$opt{encoding}'"); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # IMPLICIT as default for backwards compatibility, even though it's wrong. | ||||||||||||||||||||||||||||
| 121 | 40 | ▶ | $self->{options}{tagdefault} = uc($opt{tagdefault} || 'IMPLICIT'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$opt{'tagdefault'} || 'IMPLICIT'
MC/DC100%1/1
$opt{'tagdefault'} || 'IMPLICIT'
$opt{'tagdefault'}
Truth table100%2/2
A
A= $opt{'tagdefault'}
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 40 | ▶ | unless ($self->{options}{tagdefault} =~ /^(?:EXPLICIT|IMPLICIT)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | require Carp; | |||||||||||||||||||||||||||
| 125 | 0 | Carp::croak("Default tagging must be EXPLICIT/IMPLICIT. Not $opt{tagdefault}"); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 40 | for my $type (qw(encode decode)) { | |||||||||||||||||||||||||||
| 130 | 80 | ▶ | if (exists $opt{$type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 8 | ▶ | while(my($what,$value) = each %{$opt{$type}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 8 | $self->{options}{"${type}_${what}"} = $value; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 18 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 18 | my $what = shift; | |||||||||||||||||||||||||||
| 143 | 18 | ▶ | return unless exists $self->{tree}{$what}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 18 | my %new = %$self; | |||||||||||||||||||||||||||
| 145 | 18 | $new{script} = $new{tree}->{$what}; | |||||||||||||||||||||||||||
| 146 | 18 | bless \%new, ref($self); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 100 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 100 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 100 | my $asn = shift; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 100 | ▶ | $self = $self->new unless ref($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 100 | my $tree; | |||||||||||||||||||||||||||
| 156 | 100 | ▶ | if( ref($asn) eq 'GLOB' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | local $/ = undef; | |||||||||||||||||||||||||||
| 158 | 0 | my $txt = <$asn>; | |||||||||||||||||||||||||||
| 159 | 0 | $tree = Convert::ASN1::parser::parse($txt,$self->{options}{tagdefault}); | |||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||
| 161 | 100 | $tree = Convert::ASN1::parser::parse($asn,$self->{options}{tagdefault}); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 100 | ▶ | unless ($tree) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | $self->{error} = $@; | |||||||||||||||||||||||||||
| 166 | 0 | return; | |||||||||||||||||||||||||||
| 167 | ### If $self has been set to a new object, not returning | ||||||||||||||||||||||||||||
| 168 | ### this object here will destroy the object, so the caller | ||||||||||||||||||||||||||||
| 169 | ### won't be able to get at the error. | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 100 | $self->{tree} = _pack_struct($tree); | |||||||||||||||||||||||||||
| 173 | 100 | $self->{script} = (values %$tree)[0]; | |||||||||||||||||||||||||||
| 174 | 100 | $self; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub prepare_file { | ||||||||||||||||||||||||||
|
Subroutine
prepare_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | 0 | my $asnp = shift; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | local *ASN; | |||||||||||||||||||||||||||
| 182 | open( ASN, $asnp ) | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | or do{ $self->{error} = $@; return; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | my $ret = $self->prepare( \*ASN ); | |||||||||||||||||||||||||||
| 185 | 0 | close( ASN ); | |||||||||||||||||||||||||||
| 186 | 0 | $ret; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 2 | ▶ | sub registeroid { | ||||||||||||||||||||||||||
|
Subroutine
registeroid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | 2 | my $oid = shift; | |||||||||||||||||||||||||||
| 192 | 2 | my $handler = shift; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 2 | $self->{options}{oidtable}{$oid}=$handler; | |||||||||||||||||||||||||||
| 195 | 2 | $self->{oidtable}{$oid}=$handler; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | sub registertype { | ||||||||||||||||||||||||||
|
Subroutine
registertype: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 200 | 0 | my $def = shift; | |||||||||||||||||||||||||||
| 201 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 202 | 0 | my $handler = shift; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | $self->{options}{handlers}{$def}{$type}=$handler; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # In XS the will convert the tree between perl and C structs | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 100 | ▶ | sub _pack_struct { $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
_pack_struct: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub _unpack_struct { $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
_unpack_struct: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | ## | ||||||||||||||||||||||||||||
| 213 | ## Encoding | ||||||||||||||||||||||||||||
| 214 | ## | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 106 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 217 | 106 | my $self = shift; | |||||||||||||||||||||||||||
| 218 | 106 | ▶ | my $stash = @_ == 1 ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 106 | my $buf = ''; | |||||||||||||||||||||||||||
| 220 | 106 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||
| 221 | 106 | eval { _encode($self->{options}, $self->{script}, $stash, [], $buf) } | |||||||||||||||||||||||||||
| 222 | 106 | ▶ | or do { $self->{error} = $@; undef } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Encode tag value for encoding. | ||||||||||||||||||||||||||||
| 228 | # We assume that the tag has been correctly generated with asn_tag() | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 866 | ▶ | sub asn_encode_tag { | ||||||||||||||||||||||||||
|
Subroutine
asn_encode_tag: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 866 | ▶ | $_[0] >> 8 | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 232 | ? $_[0] & 0x8000 | ||||||||||||||||||||||||||||
| 233 | ? $_[0] & 0x800000 | ||||||||||||||||||||||||||||
| 234 | ? pack("V",$_[0]) | ||||||||||||||||||||||||||||
| 235 | : substr(pack("V",$_[0]),0,3) | ||||||||||||||||||||||||||||
| 236 | : pack("v", $_[0]) | ||||||||||||||||||||||||||||
| 237 | : pack("C",$_[0]); | ||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Encode a length. If < 0x80 then encode as a byte. Otherwise encode | ||||||||||||||||||||||||||||
| 242 | # 0x80 | num_bytes followed by the bytes for the number. top end | ||||||||||||||||||||||||||||
| 243 | # bytes of all zeros are not encoded | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 198 | ▶ | sub asn_encode_length { | ||||||||||||||||||||||||||
|
Subroutine
asn_encode_length: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 198 | ▶ | if($_[0] >> 7) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 6 | my $lenlen = &num_length; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 6 | return pack("Ca*", $lenlen | 0x80, substr(pack("N",$_[0]), -$lenlen)); | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 192 | return pack("C", $_[0]); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | ## | ||||||||||||||||||||||||||||
| 258 | ## Decoding | ||||||||||||||||||||||||||||
| 259 | ## | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 142 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 262 | 142 | my $self = shift; | |||||||||||||||||||||||||||
| 263 | 142 | my $ret; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 142 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||
| 266 | eval { | ||||||||||||||||||||||||||||
| 267 | 142 | my (%stash, $result); | |||||||||||||||||||||||||||
| 268 | 142 | my $script = $self->{script}; | |||||||||||||||||||||||||||
| 269 | 142 | my $stash = \$result; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 142 | ▶ | while ($script) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 189 | ▶ | my $child = $script->[0] or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 189 | ▶ | if (@$script > 1 or defined $child->[cVAR]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$script > 1 or defined $child->[2]
MC/DC100%2/2
@$script > 1 or defined $child->[2]
@$script > 1
defined $child->[2]
Truth table100%3/3
A or B
A= @$script > 1B= defined $child->[2]
| |||||||||||||||||||||||||||||
| 274 | 122 | $result = $stash = \%stash; | |||||||||||||||||||||||||||
| 275 | 122 | last; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | 67 | ▶ | last if $child->[cTYPE] == opCHOICE or $child->[cLOOP]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$child->[1] == 15 or $child->[3]
MC/DC100%2/2
$child->[1] == 15 or $child->[3]
$child->[1] == 15
$child->[3]
Truth table100%3/3
A or B
A= $child->[1] == 15B= $child->[3]
| |||||||||||||||||||||||||||||
| 278 | 54 | $script = $child->[cCHILD]; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | _decode( | ||||||||||||||||||||||||||||
| 282 | $self->{options}, | ||||||||||||||||||||||||||||
| 283 | $self->{script}, | ||||||||||||||||||||||||||||
| 284 | 142 | $stash, | |||||||||||||||||||||||||||
| 285 | 0, | ||||||||||||||||||||||||||||
| 286 | length $_[0], | ||||||||||||||||||||||||||||
| 287 | undef, | ||||||||||||||||||||||||||||
| 288 | {}, | ||||||||||||||||||||||||||||
| 289 | $_[0]); | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 139 | $ret = $result; | |||||||||||||||||||||||||||
| 292 | 139 | ▶ | 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'Unknown error'
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||
| 293 | 142 | ▶ | } or $self->{'error'} = $@ || 'Unknown error'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 142 | $ret; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 9 | ▶ | sub asn_decode_length { | ||||||||||||||||||||||||||
|
Subroutine
asn_decode_length: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 9 | ▶ | return unless length $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 9 | my $len = unpack("C",$_[0]); | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 9 | ▶ | if($len & 0x80) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 4 | ▶ | $len &= 0x7f or return (1,-1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 4 | ▶ | return if $len >= length $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 4 | return (1+$len, unpack("N", "\0" x (4 - $len) . substr($_[0],1,$len))); | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | 5 | return (1, $len); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 11 | ▶ | sub asn_decode_tag { | ||||||||||||||||||||||||||
|
Subroutine
asn_decode_tag: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 316 | 11 | ▶ | return unless length $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 9 | my $tag = unpack("C", $_[0]); | |||||||||||||||||||||||||||
| 319 | 9 | my $n = 1; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 9 | ▶ | if(($tag & 0x1f) == 0x1f) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 4 | my $b; | |||||||||||||||||||||||||||
| 323 | 4 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 6 | ▶ | return if $n >= length $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 6 | $b = unpack("C",substr($_[0],$n,1)); | |||||||||||||||||||||||||||
| 326 | 6 | $tag |= $b << (8 * $n++); | |||||||||||||||||||||||||||
| 327 | } while($b & 0x80); | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 9 | ($n, $tag); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 0 | ▶ | sub asn_decode_tag2 { | ||||||||||||||||||||||||||
|
Subroutine
asn_decode_tag2: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 334 | 0 | ▶ | return unless length $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | my $tag = unpack("C",$_[0]); | |||||||||||||||||||||||||||
| 337 | 0 | my $num = $tag & 0x1f; | |||||||||||||||||||||||||||
| 338 | 0 | my $len = 1; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 0 | ▶ | if($num == 0x1f) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | 0 | $num = 0; | |||||||||||||||||||||||||||
| 342 | 0 | my $b; | |||||||||||||||||||||||||||
| 343 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 0 | ▶ | return if $len >= length $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 0 | $b = unpack("C",substr($_[0],$len++,1)); | |||||||||||||||||||||||||||
| 346 | 0 | $num = ($num << 7) + ($b & 0x7f); | |||||||||||||||||||||||||||
| 347 | } while($b & 0x80); | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | 0 | ($len, $tag, $num); | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | ## | ||||||||||||||||||||||||||||
| 354 | ## Utilities | ||||||||||||||||||||||||||||
| 355 | ## | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # How many bytes are needed to encode a number | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 66 | ▶ | sub num_length { | ||||||||||||||||||||||||||
|
Subroutine
num_length: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 360 | 66 | ▶ | $_[0] >> 8 | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 361 | ? $_[0] >> 16 | ||||||||||||||||||||||||||||
| 362 | ? $_[0] >> 24 | ||||||||||||||||||||||||||||
| 363 | ? 4 | ||||||||||||||||||||||||||||
| 364 | : 3 | ||||||||||||||||||||||||||||
| 365 | : 2 | ||||||||||||||||||||||||||||
| 366 | : 1 | ||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | # Convert from a bigint to an octet string | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 12 | ▶ | sub i2osp { | ||||||||||||||||||||||||||
|
Subroutine
i2osp: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 372 | 12 | my($num, $biclass) = @_; | |||||||||||||||||||||||||||
| 373 | 12 | ▶ | eval "use $biclass"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 374 | 12 | $num = $biclass->new($num); | |||||||||||||||||||||||||||
| 375 | 12 | ▶ | my $neg = $num < 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | and $num = abs($num+1); | ||||||||||||||||||||||||||||
| 377 | 12 | my $base = $biclass->new(256); | |||||||||||||||||||||||||||
| 378 | 12 | my $result = ''; | |||||||||||||||||||||||||||
| 379 | 12 | ▶ | while($num != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 101 | my $r = $num % $base; | |||||||||||||||||||||||||||
| 381 | 101 | $num = ($num-$r) / $base; | |||||||||||||||||||||||||||
| 382 | 101 | $result .= pack("C",$r); | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | 12 | ▶ | $result ^= pack("C",255) x length($result) if $neg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | 12 | return scalar reverse $result; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # Convert from an octet string to a bigint | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 16 | ▶ | sub os2ip { | ||||||||||||||||||||||||||
|
Subroutine
os2ip: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 391 | 16 | my($os, $biclass) = @_; | |||||||||||||||||||||||||||
| 392 | 16 | eval "require $biclass"; | |||||||||||||||||||||||||||
| 393 | 16 | my $base = $biclass->new(256); | |||||||||||||||||||||||||||
| 394 | 16 | my $result = $biclass->new(0); | |||||||||||||||||||||||||||
| 395 | 16 | ▶ | my $neg = unpack("C",$os) >= 0x80 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | and $os ^= pack("C",255) x length($os); | ||||||||||||||||||||||||||||
| 397 | 16 | for (unpack("C*",$os)) { | |||||||||||||||||||||||||||
| 398 | 171 | $result = ($result * $base) + $_; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | 16 | ▶ | return $neg ? ($result + 1) * -1 : $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | # Given a class and a tag, calculate an integer which when encoded | ||||||||||||||||||||||||||||
| 404 | # will become the tag. This means that the class bits are always | ||||||||||||||||||||||||||||
| 405 | # in the bottom byte, so are the tag bits if tag < 30. Otherwise | ||||||||||||||||||||||||||||
| 406 | # the tag is in the upper 3 bytes. The upper bytes are encoded | ||||||||||||||||||||||||||||
| 407 | # with bit8 representing that there is another byte. This | ||||||||||||||||||||||||||||
| 408 | # means the max tag we can do is 0x1fffff | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 98 | ▶ | sub asn_tag { | ||||||||||||||||||||||||||
|
Subroutine
asn_tag: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 411 | 98 | my($class,$value) = @_; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 98 | ▶ | die sprintf "Bad tag class 0x%x",$class | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | if $class & ~0xe0; | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 98 | ▶ | unless ($value & ~0x1f or $value == 0x1f) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value & '18446744073709551584' or $value == 31
MC/DC50%1/2
$value & '18446744073709551584' or $value == 31
$value & '18446744073709551584'
$value == 31
Truth table66%2/3
A or B
A= $value & '18446744073709551584'B= $value == 31
| |||||||||||||||||||||||||||||
| 417 | 92 | return (($class & 0xe0) | $value); | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 6 | ▶ | die sprintf "Tag value 0x%08x too big\n",$value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | if $value & 0xffe00000; | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 6 | $class = ($class | 0x1f) & 0xff; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 6 | my @t = ($value & 0x7f); | |||||||||||||||||||||||||||
| 426 | 6 | ▶ | unshift @t, (0x80 | ($value & 0x7f)) while $value >>= 7; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 6 | unpack("V",pack("C4",$class,@t,0,0)); | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 24 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 432 | # When we have XS &_encode will be defined by the XS code | ||||||||||||||||||||||||||||
| 433 | # so will all the subs in these required packages | ||||||||||||||||||||||||||||
| 434 | 24 | ▶ | unless (defined &_encode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | 24 | require Convert::ASN1::_decode; | |||||||||||||||||||||||||||
| 436 | 24 | require Convert::ASN1::_encode; | |||||||||||||||||||||||||||
| 437 | 24 | require Convert::ASN1::IO; | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 24 | require Convert::ASN1::parser; | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 444 | 0 | ▶ | require Convert::ASN1::Debug if $AUTOLOAD =~ /dump/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | 0 | ▶ | goto &{$AUTOLOAD} if defined &{$AUTOLOAD}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | 0 | require Carp; | |||||||||||||||||||||||||||
| 447 | 0 | ▶ | my $pkg = ref($_[0]) || ($_[0] =~ /^[\w\d]+(?:::[\w\d]+)*$/)[0]; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $_[0] || ($_[0] =~ /^[\w\d]+(?:::[\w\d]+)*$/)[0]
MC/DC0%0/2
ref $_[0] || ($_[0] =~ /^[\w\d]+(?:::[\w\d]+)*$/)[0]
ref $_[0]
($_[0] =~ /^[\w\d]+(?:::[\w\d]+)*$/)[0]
Truth table0%0/3
A || B
A= ref $_[0]B= ($_[0] =~ /^[\w\d]+(?:::[\w\d]+)*$/)[0]
| |||||||||||||||||||||||||||||
| 448 | 0 | ▶ | if ($pkg and UNIVERSAL::isa($pkg, __PACKAGE__)) { # guess it was a method call | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pkg and UNIVERSAL::isa($pkg, __PACKAGE__)
MC/DC0%0/2
$pkg and UNIVERSAL::isa($pkg, __PACKAGE__)
$pkg
UNIVERSAL::isa($pkg, __PACKAGE__)
Truth table0%0/3
A and B
A= $pkgB= UNIVERSAL::isa($pkg, __PACKAGE__)
| |||||||||||||||||||||||||||||
| 449 | 0 | $AUTOLOAD =~ s/.*:://; | |||||||||||||||||||||||||||
| 450 | 0 | Carp::croak(sprintf q{Can't locate object method "%s" via package "%s"},$AUTOLOAD,$pkg); | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | else { | ||||||||||||||||||||||||||||
| 453 | 0 | Carp::croak(sprintf q{Undefined subroutine &%s called}, $AUTOLOAD); | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 6 | ▶ | sub error { $_[0]->{error} } | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 460 | 24 | 1; | |||||||||||||||||||||||||||