summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-25 23:41:53 +0200
committero <o@immerda.ch>2011-06-25 23:41:53 +0200
commit1fd5965d6c2687a26c4d32cd3199448755598b8d (patch)
treedae431fea6d0e934540b8fd2eb88806f22d388ed
parentf8eb36b0eb9c7c3848109c59d992f32f76733d33 (diff)
fixed parsing of headers
apparently gpg uses old header format. so for now just implement this. the size type is encoded in the two lsb of the header we can now already find all the packets in: FPG::Parse::Parser.parse( File.open( "spec/fixtures/test.bin.pub" ) )
-rw-r--r--lib/fpg/packets/packet.rb (renamed from lib/fpg/packets/new_packet.rb)2
-rw-r--r--lib/fpg/packets/public_key.rb6
-rw-r--r--lib/fpg/packets/public_subkey.rb6
-rw-r--r--lib/fpg/packets/signature.rb2
-rw-r--r--lib/fpg/packets/user_id.rb6
-rw-r--r--lib/fpg/parse/_packet_header_numbers.rb (renamed from lib/fpg/parse/_packet_headers.rb)8
-rw-r--r--lib/fpg/parse/new_packet.rb21
-rw-r--r--lib/fpg/parse/new_packet_header.rb23
-rw-r--r--lib/fpg/parse/packet.rb26
-rw-r--r--lib/fpg/parse/packet_header.rb23
-rw-r--r--lib/fpg/parse/parser.rb9
-rw-r--r--lib/fpg/parse/public_key.rb10
-rw-r--r--lib/fpg/parse/public_subkey.rb10
-rw-r--r--lib/fpg/parse/signature.rb2
-rw-r--r--lib/fpg/parse/user_id.rb10
15 files changed, 111 insertions, 53 deletions
diff --git a/lib/fpg/packets/new_packet.rb b/lib/fpg/packets/packet.rb
index 417c737..8b4ba14 100644
--- a/lib/fpg/packets/new_packet.rb
+++ b/lib/fpg/packets/packet.rb
@@ -1,6 +1,6 @@
module FPG
module Packets
- class NewPacket
+ class Packet
attr_accessor :byte_size
end
end
diff --git a/lib/fpg/packets/public_key.rb b/lib/fpg/packets/public_key.rb
new file mode 100644
index 0000000..c454871
--- /dev/null
+++ b/lib/fpg/packets/public_key.rb
@@ -0,0 +1,6 @@
+module FPG
+ module Packets
+ class PublicKey < Packet
+ end
+ end
+end
diff --git a/lib/fpg/packets/public_subkey.rb b/lib/fpg/packets/public_subkey.rb
new file mode 100644
index 0000000..b369589
--- /dev/null
+++ b/lib/fpg/packets/public_subkey.rb
@@ -0,0 +1,6 @@
+module FPG
+ module Packets
+ class PublicSubkey < Packet
+ end
+ end
+end
diff --git a/lib/fpg/packets/signature.rb b/lib/fpg/packets/signature.rb
index f8f4aef..b521cd4 100644
--- a/lib/fpg/packets/signature.rb
+++ b/lib/fpg/packets/signature.rb
@@ -1,6 +1,6 @@
module FPG
module Packets
- class Signature < NewPacket
+ class Signature < Packet
end
end
end
diff --git a/lib/fpg/packets/user_id.rb b/lib/fpg/packets/user_id.rb
new file mode 100644
index 0000000..6d12c01
--- /dev/null
+++ b/lib/fpg/packets/user_id.rb
@@ -0,0 +1,6 @@
+module FPG
+ module Packets
+ class UserId < Packet
+ end
+ end
+end
diff --git a/lib/fpg/parse/_packet_headers.rb b/lib/fpg/parse/_packet_header_numbers.rb
index a016a04..560d9e7 100644
--- a/lib/fpg/parse/_packet_headers.rb
+++ b/lib/fpg/parse/_packet_header_numbers.rb
@@ -1,10 +1,10 @@
module FPG
module Parse
- module PacketHeaders
+ module PacketHeaderNumbers
def packet_for( byte )
case byte
when 0
- raise Error, "a packet tag must not be 0"
+ raise "a packet tag must not be 0"
when 1
PublicKeyEncryptedSessionKey.new
when 2
@@ -40,9 +40,9 @@ module FPG
when 19
MudificationDetectionCode.new
when 60..63
- raise Error, 'Private or Experimental Header detected'
+ raise 'Private or Experimental Header detected'
else
- nil
+ raise byte.to_s+' is no valid Header'
end
end
end
diff --git a/lib/fpg/parse/new_packet.rb b/lib/fpg/parse/new_packet.rb
deleted file mode 100644
index f4e7658..0000000
--- a/lib/fpg/parse/new_packet.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module FPG
- module Parse
- module NewPacketParser
- def parse( stream )
- size= packet_size( stream )
- puts "i'm size "+size.to_s
- stream.seek( size, IO::SEEK_CUR )
- end
- def packet_size( str )
- tag = str.getbyte
- return tag if tag < 192
- return ((tag-192)<<8) + str.getbyte + 192 if tag < 224
- return (str.getbyte<<24) | (str.getbyte<<16) | (str.getbyte<<8) | str.getbyte if tag == 255
- raise Error, "Partial Body lengths are not supported"
- end
- end
- class NewPacket < Packets::NewPacket
- include NewPacketParser
- end
- end
-end
diff --git a/lib/fpg/parse/new_packet_header.rb b/lib/fpg/parse/new_packet_header.rb
deleted file mode 100644
index a65a82e..0000000
--- a/lib/fpg/parse/new_packet_header.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-module FPG
- module Parse
- class NewPacketHeader
- include PacketHeaders
- def parse( stream )
- stream.each_byte do |byte|
- current_packet = packet_for( byte )
- unless current_packet.nil? then
- puts current_packet.inspect
- return current_packet.parse( stream )
- end
- end
- end
- def packet_size( str )
- tag = str.getbyte
- return tag if tag < 192
- return ((tag-192)<<8) + str.getbyte + 192 if tag < 224
- return (str.getbyte<<24) | (str.getbyte<<16) | (str.getbyte<<8) | str.getbyte if tag == 255
- raise Error, "Partial Body lengths are not supported"
- end
- end
-end
-end
diff --git a/lib/fpg/parse/packet.rb b/lib/fpg/parse/packet.rb
new file mode 100644
index 0000000..b216491
--- /dev/null
+++ b/lib/fpg/parse/packet.rb
@@ -0,0 +1,26 @@
+module FPG
+ module Parse
+ module PacketParser
+ def parse( stream )
+ size= packet_size( stream )
+ puts self
+ puts "i'm size "+size.to_s
+ stream.seek(size,IO::SEEK_CUR)
+ end
+ def packet_size( str )
+ tag = size_tag(str.getbyte)
+ 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
+ raise "Partial Body lengths are not supported"
+ end
+ def size_tag( header )
+ #size tag = two lowest bits
+ header & 3
+ end
+ end
+ class Packet < Packets::Packet
+ include PacketParser
+ end
+ end
+end
diff --git a/lib/fpg/parse/packet_header.rb b/lib/fpg/parse/packet_header.rb
new file mode 100644
index 0000000..f388d8b
--- /dev/null
+++ b/lib/fpg/parse/packet_header.rb
@@ -0,0 +1,23 @@
+module FPG
+ module Parse
+ class PacketHeader
+ include PacketHeaderNumbers
+ 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)
+ packet_for(packet_number(header)).parse(stream)
+ end
+ def packet_number(byte)
+ raise 'sorry i can''t parse new packet headers yet' if new_packet?(byte)
+ (byte-(1<<7))>>2
+ end
+ def packet_header?(byte)
+ byte>>7 == 1
+ end
+ def new_packet?(byte)
+ (byte-(1<<7))>>6 == 1
+ end
+ end
+ end
+end
diff --git a/lib/fpg/parse/parser.rb b/lib/fpg/parse/parser.rb
index 3d0cd26..37a0d44 100644
--- a/lib/fpg/parse/parser.rb
+++ b/lib/fpg/parse/parser.rb
@@ -2,8 +2,13 @@ module FPG
module Parse
class Parser
def self.parse( stream )
- #TODO: parse until stream at end
- NewPacketHeader.new.parse( stream )
+ until stream.eof? do
+ begin
+ PacketHeader.new.parse( stream )
+ rescue Exception => e
+ raise 'parsing the message failed at position '+stream.pos.to_s+'. reason: '+ e.message
+ end
+ end
end
end
end
diff --git a/lib/fpg/parse/public_key.rb b/lib/fpg/parse/public_key.rb
new file mode 100644
index 0000000..55032a3
--- /dev/null
+++ b/lib/fpg/parse/public_key.rb
@@ -0,0 +1,10 @@
+module FPG
+ module Parse
+ module PublicKeyParser
+ include PacketParser
+ end
+ class PublicKey < Packets::PublicKey
+ include PublicKeyParser
+ end
+ end
+end
diff --git a/lib/fpg/parse/public_subkey.rb b/lib/fpg/parse/public_subkey.rb
new file mode 100644
index 0000000..cb3c2e8
--- /dev/null
+++ b/lib/fpg/parse/public_subkey.rb
@@ -0,0 +1,10 @@
+module FPG
+ module Parse
+ module PublicSubkeyParser
+ include PacketParser
+ end
+ class PublicSubkey < Packets::PublicSubkey
+ include PublicSubkeyParser
+ end
+ end
+end
diff --git a/lib/fpg/parse/signature.rb b/lib/fpg/parse/signature.rb
index 41bee87..727092e 100644
--- a/lib/fpg/parse/signature.rb
+++ b/lib/fpg/parse/signature.rb
@@ -1,7 +1,7 @@
module FPG
module Parse
module SignatureParser
- include NewPacketParser
+ include PacketParser
end
class Signature < Packets::Signature
include SignatureParser
diff --git a/lib/fpg/parse/user_id.rb b/lib/fpg/parse/user_id.rb
new file mode 100644
index 0000000..8849ea1
--- /dev/null
+++ b/lib/fpg/parse/user_id.rb
@@ -0,0 +1,10 @@
+module FPG
+ module Parse
+ module UserIdParser
+ include PacketParser
+ end
+ class UserId < Packets::UserId
+ include UserIdParser
+ end
+ end
+end