2010-03-13 06:20:07 +01:00
|
|
|
require 'stringio'
|
|
|
|
|
2010-02-09 23:46:49 +01:00
|
|
|
module Net
|
|
|
|
module BER
|
|
|
|
module BERParser
|
2010-03-16 01:16:12 +01:00
|
|
|
VERSION = '0.1.0'
|
|
|
|
|
2010-02-09 23:46:49 +01:00
|
|
|
# The order of these follows the class-codes in BER.
|
|
|
|
# Maybe this should have been a hash.
|
|
|
|
TagClasses = [:universal, :application, :context_specific, :private]
|
|
|
|
|
|
|
|
BuiltinSyntax = Net::BER.compile_syntax( {
|
|
|
|
:universal => {
|
|
|
|
:primitive => {
|
|
|
|
1 => :boolean,
|
|
|
|
2 => :integer,
|
|
|
|
4 => :string,
|
|
|
|
5 => :null,
|
|
|
|
6 => :oid,
|
|
|
|
10 => :integer,
|
|
|
|
13 => :string # (relative OID)
|
|
|
|
},
|
|
|
|
:constructed => {
|
|
|
|
16 => :array,
|
|
|
|
17 => :array
|
|
|
|
}
|
|
|
|
},
|
|
|
|
:context_specific => {
|
|
|
|
:primitive => {
|
|
|
|
10 => :integer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
def read_ber syntax=nil
|
2010-02-12 11:59:46 +01:00
|
|
|
# TODO: clean this up so it works properly with partial
|
|
|
|
# packets coming from streams that don't block when
|
|
|
|
# we ask for more data (like StringIOs). At it is,
|
|
|
|
# this can throw TypeErrors and other nasties.
|
2010-02-12 14:39:57 +01:00
|
|
|
|
|
|
|
id = getbyte or return nil # don't trash this value, we'll use it later
|
2010-02-09 23:46:49 +01:00
|
|
|
|
2010-02-12 14:39:57 +01:00
|
|
|
n = getbyte
|
2010-02-10 19:13:59 +01:00
|
|
|
lengthlength,contentlength = if n <= 127
|
|
|
|
[1,n]
|
|
|
|
else
|
|
|
|
# Replaced the inject because it profiles hot.
|
2010-02-12 11:59:46 +01:00
|
|
|
# j = (0...(n & 127)).inject(0) {|mem,x| mem = (mem << 8) + getc}
|
2010-02-10 19:13:59 +01:00
|
|
|
j = 0
|
|
|
|
read( n & 127 ).each_byte {|n1| j = (j << 8) + n1}
|
|
|
|
[1 + (n & 127), j]
|
|
|
|
end
|
2010-02-09 23:46:49 +01:00
|
|
|
|
2010-02-12 14:39:57 +01:00
|
|
|
newobj = read contentlength
|
2010-02-10 19:13:59 +01:00
|
|
|
|
|
|
|
# This exceptionally clever and clear bit of code is verrrry slow.
|
|
|
|
objtype = (syntax && syntax[id]) || BuiltinSyntax[id]
|
|
|
|
|
|
|
|
# == is expensive so sort this if/else so the common cases are at the top.
|
|
|
|
obj = if objtype == :string
|
|
|
|
#(newobj || "").dup
|
|
|
|
s = BerIdentifiedString.new( newobj || "" )
|
|
|
|
s.ber_identifier = id
|
|
|
|
s
|
|
|
|
elsif objtype == :integer
|
|
|
|
j = 0
|
|
|
|
newobj.each_byte {|b| j = (j << 8) + b}
|
|
|
|
j
|
|
|
|
elsif objtype == :oid
|
|
|
|
# cf X.690 pgh 8.19 for an explanation of this algorithm.
|
|
|
|
# Potentially not good enough. We may need a BerIdentifiedOid
|
|
|
|
# as a subclass of BerIdentifiedArray, to get the ber identifier
|
|
|
|
# and also a to_s method that produces the familiar dotted notation.
|
|
|
|
oid = newobj.unpack("w*")
|
|
|
|
f = oid.shift
|
|
|
|
g = if f < 40
|
2010-02-09 23:46:49 +01:00
|
|
|
[0, f]
|
2010-02-10 19:13:59 +01:00
|
|
|
elsif f < 80
|
2010-02-09 23:46:49 +01:00
|
|
|
[1, f-40]
|
2010-02-10 19:13:59 +01:00
|
|
|
else
|
2010-02-09 23:46:49 +01:00
|
|
|
[2, f-80] # f-80 can easily be > 80. What a weird optimization.
|
2010-02-10 19:13:59 +01:00
|
|
|
end
|
|
|
|
oid.unshift g.last
|
|
|
|
oid.unshift g.first
|
|
|
|
oid
|
|
|
|
elsif objtype == :array
|
|
|
|
#seq = []
|
|
|
|
seq = BerIdentifiedArray.new
|
|
|
|
seq.ber_identifier = id
|
|
|
|
sio = StringIO.new( newobj || "" )
|
|
|
|
# Interpret the subobject, but note how the loop
|
|
|
|
# is built: nil ends the loop, but false (a valid
|
|
|
|
# BER value) does not!
|
|
|
|
while (e = sio.read_ber(syntax)) != nil
|
|
|
|
seq << e
|
|
|
|
end
|
|
|
|
seq
|
|
|
|
elsif objtype == :boolean
|
|
|
|
newobj != "\000"
|
|
|
|
elsif objtype == :null
|
|
|
|
n = BerIdentifiedNull.new
|
|
|
|
n.ber_identifier = id
|
|
|
|
n
|
|
|
|
else
|
|
|
|
raise BerError.new( "unsupported object type: id=#{id}" )
|
2010-02-09 23:46:49 +01:00
|
|
|
end
|
2010-02-12 11:59:46 +01:00
|
|
|
|
2010-02-10 19:13:59 +01:00
|
|
|
obj
|
|
|
|
end
|
2010-02-09 23:46:49 +01:00
|
|
|
end
|
|
|
|
end
|
2010-03-13 06:20:07 +01:00
|
|
|
end
|