summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-26 02:11:43 +0200
committero <o@immerda.ch>2011-06-26 02:11:43 +0200
commit39260e5764332d0e3989c75dcbdff4448809ad6b (patch)
treed74625b29aef928d4f91c45624ecf9b2195c87cd
parent5852310dc002323728b9e06ad647f413dbea28ec (diff)
fixed some stuff
-rw-r--r--lib/fpg/packets/public_key_encrypted_session_key.rb1
-rw-r--r--lib/fpg/parse/packet.rb43
-rw-r--r--lib/fpg/parse/packet_header.rb15
-rw-r--r--lib/fpg/parse/public_key_encrypted_session_key.rb18
4 files changed, 55 insertions, 22 deletions
diff --git a/lib/fpg/packets/public_key_encrypted_session_key.rb b/lib/fpg/packets/public_key_encrypted_session_key.rb
index bcd4738..c5a73c9 100644
--- a/lib/fpg/packets/public_key_encrypted_session_key.rb
+++ b/lib/fpg/packets/public_key_encrypted_session_key.rb
@@ -1,6 +1,7 @@
module FPG
module Packets
class PublicKeyEncryptedSessionKey < Packet
+ attr_accessor :key_id, :encryption_algo, :session_key
end
end
end
diff --git a/lib/fpg/parse/packet.rb b/lib/fpg/parse/packet.rb
index 2f35828..e421a80 100644
--- a/lib/fpg/parse/packet.rb
+++ b/lib/fpg/parse/packet.rb
@@ -1,54 +1,55 @@
module FPG
module Parse
module PacketParser
- attr_accessor :partial_length
- attr_accessor :partial_byte_size
+ attr_accessor :stream, :partial_length, :partial_byte_size
def partial_length?
partial_length
end
def parse(stream)
- self.byte_size = self.partial_byte_size = packet_size(stream)
- parse_content(stream)
+ self.stream = stream
+ self.byte_size = self.partial_byte_size = packet_size
+ parse_content
while partial_length? do
- self.byte_size += self.partial_byte_size = new_packet_size(stream)
- parse_content(stream)
+ self.byte_size += self.partial_byte_size = new_packet_size
+ parse_content
end
return self
end
- def parse_content(stream)
+ def parse_content
stream.seek(partial_byte_size,IO::SEEK_CUR)
end
- def packet_size(str)
- header = str.getbyte
+ def packet_size
+ header = stream.getbyte
if new_packet?(header)
- new_packet_size(str)
+ new_packet_size
else
tag = old_size_tag(header)
- old_packet_size(tag,str)
+ old_packet_size(tag)
end
end
- def new_packet_size(str)
+ def new_packet_size
self.partial_length= false
- first = str.getbyte
+ first = stream.getbyte
return first if first < 192
- return ((first-192)<<8)+str.getbyte+192 if first < 224
- return (str.getbyte<<24)|(str.getbyte<<16)|(str.getbyte<<8)|str.getbyte if first == 255
+ return ((first-192)<<8)+stream.getbyte+192 if first < 224
+ return (first+stream.gets(3)).unpack("N")[0] if first == 255
self.partial_length= true
return 1<<(first&0x1f)
end
- def old_packet_size(tag,str)
- return str.getbyte if tag == 0
- return str.gets(2).unpack("n")[0] if tag == 1
- return str.gets(4).unpack("N")[0] if tag == 2
+ def old_packet_size(tag)
+ return stream.getbyte if tag == 0
+ return stream.gets(2).unpack("n")[0] if tag == 1
+ return stream.gets(4).unpack("N")[0] if tag == 2
+ #tag==3 => size is until eof
#TODO this seems a bit cumbersome
- current_pos = str.pos
+ current_pos = stream.pos
stream.seek(0,IO::SEEK_END)
size = 1+stream.pos - current_pos
stream.pos= current_pos
return size
end
- def old_size_tag( header )
+ def old_size_tag(header)
#size tag = two lowest bits
header & 3
end
diff --git a/lib/fpg/parse/packet_header.rb b/lib/fpg/parse/packet_header.rb
index d1c9ff4..5d4a8c1 100644
--- a/lib/fpg/parse/packet_header.rb
+++ b/lib/fpg/parse/packet_header.rb
@@ -5,7 +5,9 @@ module FPG
def parse( stream )
header = stream.getbyte
stream.seek(-1,IO::SEEK_CUR)
- raise 'i expected a packet header but there is none' unless packet_header?(header)
+ unless packet_header?(header)
+ debug_header_search(stream)
+ end
packet_for(packet_number(header)).parse(stream)
end
def packet_number(byte)
@@ -21,6 +23,17 @@ module FPG
def new_packet?(byte)
byte>>6 == 3
end
+ def debug_header_search(stream)
+ current_pos = stream.pos+1
+ stream.seek(-2,IO::SEEK_CUR)
+ 4.times do
+ puts packet_header?(stream.getbyte)
+ if packet_header?(stream.getbyte) then
+ raise "i expected a packet header but found none. it could to be off by #{current_pos-stream.pos}"
+ end
+ end
+ raise "i expected a packet header but there is none"
+ end
end
end
end
diff --git a/lib/fpg/parse/public_key_encrypted_session_key.rb b/lib/fpg/parse/public_key_encrypted_session_key.rb
index 99cc05e..a25ce82 100644
--- a/lib/fpg/parse/public_key_encrypted_session_key.rb
+++ b/lib/fpg/parse/public_key_encrypted_session_key.rb
@@ -2,6 +2,24 @@ module FPG
module Parse
module PublicKeyEncryptedSessionKeyParser
include PacketParser
+ def parse_content
+ parse_version
+ parse_keyid
+ parse_public_key_algo
+ parse_session_key
+ end
+ def parse_version
+ raise "PublicKeyEncryptedSessionKeyParser Version must be 3" if stream.getbyte != 3
+ end
+ def parse_keyid
+ self.key_id= stream.read(8)
+ end
+ def parse_public_key_algo
+ self.encryption_algo= stream.getbyte
+ end
+ def parse_session_key
+ self.session_key= stream.read(byte_size-10)
+ end
end
class PublicKeyEncryptedSessionKey < Packets::PublicKeyEncryptedSessionKey
include PublicKeyEncryptedSessionKeyParser