summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-27 16:44:23 +0200
committero <o@immerda.ch>2011-06-27 16:44:23 +0200
commite879a280c3c3af2f87eee37a189fd3a8ae8c6106 (patch)
tree442d60f50f16fad2a3add37008d0600030204813
parentebde938e0d391192bd9a6f343ee52d2dc315719c (diff)
various tweaks. the correct parsing modules are now loaded as soon as a
packet receives a parse message
-rw-r--r--lib/fpg/packets/packet.rb6
-rw-r--r--lib/fpg/packets/public_key.rb3
-rw-r--r--lib/fpg/packets/public_key_encrypted_session_key.rb3
-rw-r--r--lib/fpg/packets/public_subkey.rb3
-rw-r--r--lib/fpg/packets/secret_key.rb3
-rw-r--r--lib/fpg/packets/secret_subkey.rb3
-rw-r--r--lib/fpg/packets/signature.rb3
-rw-r--r--lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb3
-rw-r--r--lib/fpg/packets/user_id.rb3
-rw-r--r--lib/fpg/parse/key_material.rb21
-rw-r--r--lib/fpg/parse/packet.rb41
-rw-r--r--lib/fpg/parse/parser.rb16
-rw-r--r--lib/fpg/parse/public_key.rb8
-rw-r--r--lib/fpg/parse/public_key_encrypted_session_key.rb7
-rw-r--r--lib/fpg/parse/public_subkey.rb7
-rw-r--r--lib/fpg/parse/secret_key.rb8
-rw-r--r--lib/fpg/parse/secret_subkey.rb7
-rw-r--r--lib/fpg/parse/signature.rb8
-rw-r--r--lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb8
-rw-r--r--lib/fpg/parse/user_id.rb8
-rw-r--r--spec/packet_parsing_spec.rb1
21 files changed, 85 insertions, 85 deletions
diff --git a/lib/fpg/packets/packet.rb b/lib/fpg/packets/packet.rb
index 8b4ba14..75ee063 100644
--- a/lib/fpg/packets/packet.rb
+++ b/lib/fpg/packets/packet.rb
@@ -1,7 +1,13 @@
module FPG
module Packets
class Packet
+ include TaggedSubclasses
attr_accessor :byte_size
+ def << stream
+ self.class.send(:include, Parse::PacketParser)
+ self.class.send(:include, Parse.const_get("#{self.class.name.split('::').last}Parser"))
+ parse(stream)
+ end
end
end
end
diff --git a/lib/fpg/packets/public_key.rb b/lib/fpg/packets/public_key.rb
index c454871..14e9612 100644
--- a/lib/fpg/packets/public_key.rb
+++ b/lib/fpg/packets/public_key.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class PublicKey < Packet
+ def self.tag
+ 6
+ end
end
end
end
diff --git a/lib/fpg/packets/public_key_encrypted_session_key.rb b/lib/fpg/packets/public_key_encrypted_session_key.rb
index c5a73c9..f4e0db3 100644
--- a/lib/fpg/packets/public_key_encrypted_session_key.rb
+++ b/lib/fpg/packets/public_key_encrypted_session_key.rb
@@ -2,6 +2,9 @@ module FPG
module Packets
class PublicKeyEncryptedSessionKey < Packet
attr_accessor :key_id, :encryption_algo, :session_key
+ def self.tag
+ 1
+ end
end
end
end
diff --git a/lib/fpg/packets/public_subkey.rb b/lib/fpg/packets/public_subkey.rb
index 537c0bd..c0ae875 100644
--- a/lib/fpg/packets/public_subkey.rb
+++ b/lib/fpg/packets/public_subkey.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class PublicSubkey < PublicKey
+ def self.tag
+ 14
+ end
end
end
end
diff --git a/lib/fpg/packets/secret_key.rb b/lib/fpg/packets/secret_key.rb
index 3c61f25..b5a0558 100644
--- a/lib/fpg/packets/secret_key.rb
+++ b/lib/fpg/packets/secret_key.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class SecretKey < Packet
+ def self.tag
+ 5
+ end
end
end
end
diff --git a/lib/fpg/packets/secret_subkey.rb b/lib/fpg/packets/secret_subkey.rb
index e13ffeb..a8cd6a3 100644
--- a/lib/fpg/packets/secret_subkey.rb
+++ b/lib/fpg/packets/secret_subkey.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class SecretSubkey < SecretKey
+ def self.tag
+ 7
+ end
end
end
end
diff --git a/lib/fpg/packets/signature.rb b/lib/fpg/packets/signature.rb
index b521cd4..1d250e2 100644
--- a/lib/fpg/packets/signature.rb
+++ b/lib/fpg/packets/signature.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class Signature < Packet
+ def self.tag
+ 2
+ end
end
end
end
diff --git a/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb b/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb
index 2629652..ae507f6 100644
--- a/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb
+++ b/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class SymmetricEncryptedAndIntegrityProtectedData < Packet
+ def self.tag
+ 18
+ end
end
end
end
diff --git a/lib/fpg/packets/user_id.rb b/lib/fpg/packets/user_id.rb
index 6d12c01..78d267d 100644
--- a/lib/fpg/packets/user_id.rb
+++ b/lib/fpg/packets/user_id.rb
@@ -1,6 +1,9 @@
module FPG
module Packets
class UserId < Packet
+ def self.tag
+ 13
+ end
end
end
end
diff --git a/lib/fpg/parse/key_material.rb b/lib/fpg/parse/key_material.rb
new file mode 100644
index 0000000..96d57db
--- /dev/null
+++ b/lib/fpg/parse/key_material.rb
@@ -0,0 +1,21 @@
+module FPG
+ module Parse
+ module KeyMaterialParser
+ def parse_content
+ parse_version
+ parse_creation
+ parse_algo
+ @stream.seek(@byte_size-6,IO::SEEK_CUR)
+ end
+ def parse_creation
+ @created= stream.read(4).unpack("N1")
+ end
+ def parse_algo
+ @algo= PublicKeyAlgos::Algo.with_tag(stream.getbyte)
+ end
+ def parse_version
+ raise "only key material packets with version 4 are supported" unless @stream.getbyte == 4
+ end
+ end
+ end
+end
diff --git a/lib/fpg/parse/packet.rb b/lib/fpg/parse/packet.rb
index 499ecae..c24d5d6 100644
--- a/lib/fpg/parse/packet.rb
+++ b/lib/fpg/parse/packet.rb
@@ -1,41 +1,19 @@
module FPG
module Parse
module PacketParser
- module PacketParserKlass
- 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)
- with_tag(packet_number(header)).parse(stream)
- end
- def packet_number(byte)
- if new_packet?(byte)
- byte-(1<<7)-(1<<6)
- else
- (byte-(1<<7))>>2
- end
- end
- def packet_header?(byte)
- byte>>7 == 1
- end
- def new_packet?(byte)
- byte>>6 == 3
- end
- end
- def self.included(base)
- base.extend PacketParserKlass
- end
-
attr_accessor :stream, :partial_length
- def parse(stream)
- @stream = stream
+ def parse_header
@byte_size = packet_size
@byte_size += strip_partials if partial_length?
+ end
+ def parse(stream)
+ @stream = stream
+ parse_header
parse_content
return self
end
- def parse_content
+ def skip
stream.seek(byte_size,IO::SEEK_CUR)
end
@@ -101,12 +79,5 @@ module FPG
byte>>6 == 3
end
end
- class Packet < Packets::Packet
- include PacketParser
- include TaggedSubclasses
- def self.subclasses
- Packets::Packet.subclasses - [self]
- end
- end
end
end
diff --git a/lib/fpg/parse/parser.rb b/lib/fpg/parse/parser.rb
index d729655..aefe4c2 100644
--- a/lib/fpg/parse/parser.rb
+++ b/lib/fpg/parse/parser.rb
@@ -4,13 +4,27 @@ module FPG
def self.parse( stream )
packets = []
until stream.eof? do
- packets << Packet.parse(stream)
+ num = packet_number(stream.getbyte)
+ stream.seek(-1,IO::SEEK_CUR)
+ packets << (Packets::Packet.with_tag(num) << stream)
+
#rescue Exception => e
# raise 'parsing the message failed at position '+stream.pos.to_s+'. reason: '+ e.message
#end
end
packets
end
+ def self.packet_number(byte)
+ raise "this is not a valid packet header" unless byte>>7 == 1
+ if new_packet?(byte)
+ byte-(1<<7)-(1<<6)
+ else
+ (byte-(1<<7))>>2
+ end
+ end
+ def self.new_packet?(byte)
+ byte>>6 == 3
+ end
end
end
end
diff --git a/lib/fpg/parse/public_key.rb b/lib/fpg/parse/public_key.rb
index e2f859d..8e93a7e 100644
--- a/lib/fpg/parse/public_key.rb
+++ b/lib/fpg/parse/public_key.rb
@@ -1,13 +1,7 @@
module FPG
module Parse
module PublicKeyParser
- include PacketParser
- end
- class PublicKey < Packets::PublicKey
- def self.tag
- 6
- end
- include PublicKeyParser
+ include KeyMaterialParser
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 536a7fc..68360c7 100644
--- a/lib/fpg/parse/public_key_encrypted_session_key.rb
+++ b/lib/fpg/parse/public_key_encrypted_session_key.rb
@@ -1,7 +1,6 @@
module FPG
module Parse
module PublicKeyEncryptedSessionKeyParser
- include PacketParser
def parse_content
parse_version
parse_keyid
@@ -21,11 +20,5 @@ module FPG
self.session_key= stream.read(byte_size-10)
end
end
- class PublicKeyEncryptedSessionKey < Packets::PublicKeyEncryptedSessionKey
- def self.tag
- 1
- end
- include PublicKeyEncryptedSessionKeyParser
- end
end
end
diff --git a/lib/fpg/parse/public_subkey.rb b/lib/fpg/parse/public_subkey.rb
index 0661a48..0b9e152 100644
--- a/lib/fpg/parse/public_subkey.rb
+++ b/lib/fpg/parse/public_subkey.rb
@@ -1,10 +1,7 @@
module FPG
module Parse
- class PublicSubkey < Packets::PublicSubkey
- def self.tag
- 14
- end
- include PublicKeyParser
+ module PublicSubkeyParser
+ include KeyMaterialParser
end
end
end
diff --git a/lib/fpg/parse/secret_key.rb b/lib/fpg/parse/secret_key.rb
index e7d3743..204fd2e 100644
--- a/lib/fpg/parse/secret_key.rb
+++ b/lib/fpg/parse/secret_key.rb
@@ -1,13 +1,7 @@
module FPG
module Parse
module SecretKeyParser
- include PacketParser
- end
- class SecretKey < Packets::SecretKey
- def self.tag
- 5
- end
- include SecretKeyParser
+ include KeyMaterialParser
end
end
end
diff --git a/lib/fpg/parse/secret_subkey.rb b/lib/fpg/parse/secret_subkey.rb
index ba2d04e..5b4698c 100644
--- a/lib/fpg/parse/secret_subkey.rb
+++ b/lib/fpg/parse/secret_subkey.rb
@@ -1,10 +1,7 @@
module FPG
module Parse
- class SecretSubkey < Packets::SecretSubkey
- def self.tag
- 7
- end
- include SecretKeyParser
+ module SecretSubkeyParser
+ include KeyMaterialParser
end
end
end
diff --git a/lib/fpg/parse/signature.rb b/lib/fpg/parse/signature.rb
index 4e88deb..b0f043a 100644
--- a/lib/fpg/parse/signature.rb
+++ b/lib/fpg/parse/signature.rb
@@ -1,13 +1,9 @@
module FPG
module Parse
module SignatureParser
- include PacketParser
- end
- class Signature < Packets::Signature
- def self.tag
- 2
+ def parse_content
+ skip
end
- include SignatureParser
end
end
end
diff --git a/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb b/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
index ed710b6..ef12aab 100644
--- a/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
+++ b/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
@@ -1,13 +1,9 @@
module FPG
module Parse
module SymmetricEncryptedAndIntegrityProtectedDataParser
- include PacketParser
- end
- class SymmetricEncryptedAndIntegrityProtectedData < Packets::SymmetricEncryptedAndIntegrityProtectedData
- def self.tag
- 18
+ def parse_content
+ skip
end
- include SymmetricEncryptedAndIntegrityProtectedDataParser
end
end
end
diff --git a/lib/fpg/parse/user_id.rb b/lib/fpg/parse/user_id.rb
index 9a95808..9bda28d 100644
--- a/lib/fpg/parse/user_id.rb
+++ b/lib/fpg/parse/user_id.rb
@@ -1,13 +1,9 @@
module FPG
module Parse
module UserIdParser
- include PacketParser
- end
- class UserId < Packets::UserId
- def self.tag
- 13
+ def parse_content
+ skip
end
- include UserIdParser
end
end
end
diff --git a/spec/packet_parsing_spec.rb b/spec/packet_parsing_spec.rb
index bc31691..376de22 100644
--- a/spec/packet_parsing_spec.rb
+++ b/spec/packet_parsing_spec.rb
@@ -3,6 +3,7 @@ require File.join(File.dirname(__FILE__), %w[spec_helper])
require 'stringio'
include FPG::Parse
+include FPG::Packets
describe Parser do
before(:each) do