line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (c) 2000-2005 Graham Barr . 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
|
|
|
|
|
|
|
$Convert::ASN1::VERSION = '0.33'; |
7
|
23
|
|
|
23
|
|
160
|
use strict; |
|
23
|
|
|
|
|
41
|
|
|
23
|
|
|
|
|
687
|
|
8
|
23
|
|
|
23
|
|
13587
|
use Socket; |
|
23
|
|
|
|
|
83029
|
|
|
23
|
|
|
|
|
10232
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
BEGIN { |
11
|
23
|
|
|
23
|
|
133
|
local $SIG{__DIE__}; |
12
|
23
|
50
|
|
|
|
47
|
eval { require bytes } and 'bytes'->import |
|
23
|
|
|
|
|
294
|
|
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub asn_recv { # $socket, $buffer, $flags |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
1
|
10
|
my $peer; |
18
|
|
|
|
|
|
|
my $buf; |
19
|
1
|
|
|
|
|
2
|
my $n = 128; |
20
|
1
|
|
|
|
|
2
|
my $pos = 0; |
21
|
1
|
|
|
|
|
2
|
my $depth = 0; |
22
|
1
|
|
|
|
|
2
|
my $len = 0; |
23
|
1
|
|
|
|
|
2
|
my($tmp,$tb,$lb); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
MORE: |
26
|
1
|
|
|
|
|
18
|
for( |
27
|
|
|
|
|
|
|
$peer = recv($_[0],$buf,$n,MSG_PEEK); |
28
|
|
|
|
|
|
|
defined $peer; |
29
|
|
|
|
|
|
|
$peer = recv($_[0],$buf,$n<<=1,MSG_PEEK) |
30
|
|
|
|
|
|
|
) { |
31
|
|
|
|
|
|
|
|
32
|
1
|
50
|
|
|
|
4
|
if ($depth) { # Are we searching of "\0\0" |
33
|
|
|
|
|
|
|
|
34
|
0
|
0
|
|
|
|
0
|
unless (2+$pos <= length $buf) { |
35
|
0
|
0
|
|
|
|
0
|
next MORE if $n == length $buf; |
36
|
0
|
|
|
|
|
0
|
last MORE; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
0
|
if(substr($buf,$pos,2) eq "\0\0") { |
40
|
0
|
0
|
|
|
|
0
|
unless (--$depth) { |
41
|
0
|
|
|
|
|
0
|
$len = $pos + 2; |
42
|
0
|
|
|
|
|
0
|
last MORE; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# If we can decode a tag and length we can determine the length |
48
|
1
|
|
|
|
|
4
|
($tb,$tmp) = asn_decode_tag(substr($buf,$pos)); |
49
|
1
|
50
|
33
|
|
|
5
|
unless ($tb || $pos+$tb < length $buf) { |
50
|
0
|
0
|
|
|
|
0
|
next MORE if $n == length $buf; |
51
|
0
|
|
|
|
|
0
|
last MORE; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
1
|
50
|
|
|
|
5
|
if (unpack("C",substr($buf,$pos+$tb,1)) == 0x80) { |
55
|
|
|
|
|
|
|
# indefinite length, grrr! |
56
|
0
|
|
|
|
|
0
|
$depth++; |
57
|
0
|
|
|
|
|
0
|
$pos += $tb + 1; |
58
|
0
|
|
|
|
|
0
|
redo MORE; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
4
|
($lb,$len) = asn_decode_length(substr($buf,$pos+$tb)); |
62
|
|
|
|
|
|
|
|
63
|
1
|
50
|
|
|
|
3
|
if ($lb) { |
64
|
1
|
50
|
|
|
|
4
|
if ($depth) { |
65
|
0
|
|
|
|
|
0
|
$pos += $tb + $lb + $len; |
66
|
0
|
|
|
|
|
0
|
redo MORE; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
1
|
|
|
|
|
2
|
$len += $tb + $lb + $pos; |
70
|
1
|
|
|
|
|
11
|
last MORE; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
1
|
50
|
|
|
|
3
|
if (defined $peer) { |
76
|
1
|
50
|
|
|
|
6
|
if ($len > length $buf) { |
|
|
50
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Check we can read the whole element |
78
|
|
|
|
|
|
|
goto error |
79
|
0
|
0
|
|
|
|
0
|
unless defined($peer = recv($_[0],$buf,$len,MSG_PEEK)); |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
0
|
if ($len > length $buf) { |
82
|
|
|
|
|
|
|
# Cannot get whole element |
83
|
0
|
|
|
|
|
0
|
$_[1]=''; |
84
|
0
|
|
|
|
|
0
|
return $peer; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
elsif ($len == 0) { |
88
|
0
|
|
|
|
|
0
|
$_[1] = ''; |
89
|
0
|
|
|
|
|
0
|
return $peer; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
1
|
50
|
|
|
|
22
|
if ($_[2] & MSG_PEEK) { |
|
|
50
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
$_[1] = substr($buf,0,$len); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif (!defined($peer = recv($_[0],$_[1],$len,0))) { |
96
|
0
|
|
|
|
|
0
|
goto error; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
4
|
return $peer; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
error: |
103
|
0
|
|
|
|
|
0
|
$_[1] = undef; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub asn_read { # $fh, $buffer, $offset |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# We need to read one packet, and exactly only one packet. |
109
|
|
|
|
|
|
|
# So we have to read the first few bytes one at a time, until |
110
|
|
|
|
|
|
|
# we have enough to decode a tag and a length. We then know |
111
|
|
|
|
|
|
|
# how many more bytes to read |
112
|
|
|
|
|
|
|
|
113
|
2
|
50
|
|
2
|
1
|
144
|
if ($_[2]) { |
114
|
0
|
0
|
|
|
|
0
|
if ($_[2] > length $_[1]) { |
115
|
0
|
|
|
|
|
0
|
require Carp; |
116
|
0
|
|
|
|
|
0
|
Carp::carp("Offset beyond end of buffer"); |
117
|
0
|
|
|
|
|
0
|
return; |
118
|
|
|
|
|
|
|
} |
119
|
0
|
|
|
|
|
0
|
substr($_[1],$_[2]) = ''; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
else { |
122
|
2
|
|
|
|
|
5
|
$_[1] = ''; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
2
|
|
|
|
|
3
|
my $pos = 0; |
126
|
2
|
|
|
|
|
4
|
my $need = 0; |
127
|
2
|
|
|
|
|
3
|
my $depth = 0; |
128
|
2
|
|
|
|
|
7
|
my $ch; |
129
|
|
|
|
|
|
|
my $n; |
130
|
2
|
|
|
|
|
0
|
my $e; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
2
|
|
|
|
|
3
|
while(1) { |
134
|
266
|
|
100
|
|
|
470
|
$need = ($pos + ($depth * 2)) || 2; |
135
|
|
|
|
|
|
|
|
136
|
266
|
|
|
|
|
460
|
while(($n = $need - length $_[1]) > 0) { |
137
|
196
|
50
|
|
|
|
1545
|
$e = sysread($_[0],$_[1],$n,length $_[1]) or |
138
|
|
|
|
|
|
|
goto READ_ERR; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
266
|
|
|
|
|
615
|
my $tch = unpack("C",substr($_[1],$pos++,1)); |
142
|
|
|
|
|
|
|
# Tag may be multi-byte |
143
|
266
|
100
|
|
|
|
515
|
if(($tch & 0x1f) == 0x1f) { |
144
|
175
|
|
|
|
|
216
|
my $ch; |
145
|
175
|
|
|
|
|
212
|
do { |
146
|
273
|
|
|
|
|
318
|
$need++; |
147
|
273
|
|
|
|
|
452
|
while(($n = $need - length $_[1]) > 0) { |
148
|
273
|
50
|
|
|
|
1965
|
$e = sysread($_[0],$_[1],$n,length $_[1]) or |
149
|
|
|
|
|
|
|
goto READ_ERR; |
150
|
|
|
|
|
|
|
} |
151
|
273
|
|
|
|
|
892
|
$ch = unpack("C",substr($_[1],$pos++,1)); |
152
|
|
|
|
|
|
|
} while($ch & 0x80); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
266
|
|
|
|
|
363
|
$need = $pos + 1; |
156
|
|
|
|
|
|
|
|
157
|
266
|
|
|
|
|
453
|
while(($n = $need - length $_[1]) > 0) { |
158
|
0
|
0
|
|
|
|
0
|
$e = sysread($_[0],$_[1],$n,length $_[1]) or |
159
|
|
|
|
|
|
|
goto READ_ERR; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
266
|
|
|
|
|
425
|
my $len = unpack("C",substr($_[1],$pos++,1)); |
163
|
|
|
|
|
|
|
|
164
|
266
|
100
|
66
|
|
|
656
|
if($len & 0x80) { |
|
|
100
|
|
|
|
|
|
165
|
71
|
50
|
|
|
|
124
|
unless ($len &= 0x7f) { |
166
|
71
|
|
|
|
|
80
|
$depth++; |
167
|
71
|
|
|
|
|
99
|
next; |
168
|
|
|
|
|
|
|
} |
169
|
0
|
|
|
|
|
0
|
$need = $pos + $len; |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
0
|
while(($n = $need - length $_[1]) > 0) { |
172
|
0
|
0
|
|
|
|
0
|
$e = sysread($_[0],$_[1],$n,length $_[1]) or |
173
|
|
|
|
|
|
|
goto READ_ERR; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
0
|
$pos += $len + unpack("N", "\0" x (4 - $len) . substr($_[1],$pos,$len)); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
elsif (!$len && !$tch) { |
179
|
71
|
50
|
|
|
|
117
|
die "Bad ASN PDU" unless $depth; |
180
|
71
|
100
|
|
|
|
107
|
unless (--$depth) { |
181
|
1
|
|
|
|
|
11
|
last; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
else { |
185
|
124
|
|
|
|
|
156
|
$pos += $len; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
194
|
100
|
|
|
|
328
|
last unless $depth; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
2
|
|
|
|
|
7
|
while(($n = $pos - length $_[1]) > 0) { |
192
|
1
|
50
|
|
|
|
11
|
$e = sysread($_[0],$_[1],$n,length $_[1]) or |
193
|
|
|
|
|
|
|
goto READ_ERR; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
2
|
|
|
|
|
9
|
return length $_[1]; |
197
|
|
|
|
|
|
|
|
198
|
0
|
0
|
|
|
|
0
|
READ_ERR: |
199
|
|
|
|
|
|
|
$@ = defined($e) ? "Unexpected EOF" : "I/O Error $!"; # . CORE::unpack("H*",$_[1]); |
200
|
0
|
|
|
|
|
0
|
return undef; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub asn_send { # $sock, $buffer, $flags, $to |
204
|
|
|
|
|
|
|
|
205
|
1
|
50
|
|
1
|
1
|
816
|
@_ == 4 |
206
|
|
|
|
|
|
|
? send($_[0],$_[1],$_[2],$_[3]) |
207
|
|
|
|
|
|
|
: send($_[0],$_[1],$_[2]); |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub asn_write { # $sock, $buffer |
211
|
|
|
|
|
|
|
|
212
|
2
|
|
|
2
|
1
|
195
|
syswrite($_[0],$_[1], length $_[1]); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub asn_get { # $fh |
216
|
|
|
|
|
|
|
|
217
|
2
|
50
|
|
2
|
1
|
58
|
my $fh = ref($_[0]) ? $_[0] : \($_[0]); |
218
|
2
|
|
|
|
|
3
|
my $href = \%{*$fh}; |
|
2
|
|
|
|
|
7
|
|
219
|
|
|
|
|
|
|
|
220
|
2
|
100
|
|
|
|
9
|
$href->{'asn_buffer'} = '' unless exists $href->{'asn_buffer'}; |
221
|
|
|
|
|
|
|
|
222
|
2
|
|
100
|
|
|
9
|
my $need = delete $href->{'asn_need'} || 0; |
223
|
2
|
|
|
|
|
4
|
while(1) { |
224
|
3
|
100
|
|
|
|
9
|
next if $need; |
225
|
2
|
100
|
|
|
|
8
|
my($tb,$tag) = asn_decode_tag($href->{'asn_buffer'}) or next; |
226
|
1
|
50
|
|
|
|
5
|
my($lb,$len) = asn_decode_length(substr($href->{'asn_buffer'},$tb,8)) or next; |
227
|
1
|
|
|
|
|
4
|
$need = $tb + $lb + $len; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
continue { |
230
|
3
|
100
|
66
|
|
|
13
|
if ($need && $need <= length $href->{'asn_buffer'}) { |
231
|
2
|
|
|
|
|
13
|
my $ret = substr($href->{'asn_buffer'},0,$need); |
232
|
2
|
|
|
|
|
5
|
substr($href->{'asn_buffer'},0,$need) = ''; |
233
|
2
|
|
|
|
|
6
|
return $ret; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
1
|
50
|
|
|
|
4
|
my $get = $need > 1024 ? $need : 1024; |
237
|
|
|
|
|
|
|
|
238
|
1
|
50
|
|
|
|
21
|
sysread($_[0], $href->{'asn_buffer'}, $get, length $href->{'asn_buffer'}) |
239
|
|
|
|
|
|
|
or return undef; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub asn_ready { # $fh |
244
|
|
|
|
|
|
|
|
245
|
2
|
50
|
|
2
|
1
|
17
|
my $fh = ref($_[0]) ? $_[0] : \($_[0]); |
246
|
2
|
|
|
|
|
4
|
my $href = \%{*$fh}; |
|
2
|
|
|
|
|
5
|
|
247
|
|
|
|
|
|
|
|
248
|
2
|
50
|
|
|
|
7
|
return 0 unless exists $href->{'asn_buffer'}; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
return $href->{'asn_need'} <= length $href->{'asn_buffer'} |
251
|
2
|
50
|
|
|
|
4
|
if exists $href->{'asn_need'}; |
252
|
|
|
|
|
|
|
|
253
|
2
|
100
|
|
|
|
7
|
my($tb,$tag) = asn_decode_tag($href->{'asn_buffer'}) or return 0; |
254
|
1
|
50
|
|
|
|
5
|
my($lb,$len) = asn_decode_length(substr($href->{'asn_buffer'},$tb,8)) or return 0; |
255
|
|
|
|
|
|
|
|
256
|
1
|
|
|
|
|
4
|
$href->{'asn_need'} = $tb + $lb + $len; |
257
|
|
|
|
|
|
|
|
258
|
1
|
|
|
|
|
3
|
$href->{'asn_need'} <= length $href->{'asn_buffer'}; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
1; |