summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-30 20:50:22 +0200
committero <o@immerda.ch>2011-06-30 20:50:22 +0200
commitce318f2b4aa01c7957fa06c4f788fc904753e96b (patch)
tree5458bd638ff51c92ee1d4a754362dbc197c69569
parent3fe777cbfd99bd6a12240273cdfc3600993775ca (diff)
added some tests for secret keys and key uid
-rw-r--r--lib/fpg/algos/hash.rb7
-rw-r--r--lib/fpg/algos/sha1.rb9
-rw-r--r--lib/fpg/fields/s2k_specifier.rb5
-rw-r--r--lib/fpg/packets/secret_key.rb11
-rw-r--r--lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb3
-rw-r--r--lib/fpg/packets/user_id.rb2
-rw-r--r--spec/secret_key_spec.rb23
-rw-r--r--spec/user_id_spec.rb19
8 files changed, 73 insertions, 6 deletions
diff --git a/lib/fpg/algos/hash.rb b/lib/fpg/algos/hash.rb
new file mode 100644
index 0000000..1388124
--- /dev/null
+++ b/lib/fpg/algos/hash.rb
@@ -0,0 +1,7 @@
+module FPG
+ module Algos
+ class Hash
+ include TaggedSubclasses
+ end
+ end
+end
diff --git a/lib/fpg/algos/sha1.rb b/lib/fpg/algos/sha1.rb
new file mode 100644
index 0000000..a47ce87
--- /dev/null
+++ b/lib/fpg/algos/sha1.rb
@@ -0,0 +1,9 @@
+module FPG
+ module Algos
+ class Sha1 < Hash
+ def self.tag
+ 2
+ end
+ end
+ end
+end
diff --git a/lib/fpg/fields/s2k_specifier.rb b/lib/fpg/fields/s2k_specifier.rb
index 68fb0e2..9b3a00c 100644
--- a/lib/fpg/fields/s2k_specifier.rb
+++ b/lib/fpg/fields/s2k_specifier.rb
@@ -3,10 +3,13 @@ module FPG
class S2kSpecifier < BinData::Record
endian :big
uint8 :id
- uint8 :hash_algo
+ uint8 :h_algo
uint64 :salt, :onlyif => lambda { id > 0 }
uint8 :iterations, :onlyif => lambda { id > 2 }
+ def hash_algo
+ Algos::Hash.with_tag(h_algo)
+ end
end
end
end
diff --git a/lib/fpg/packets/secret_key.rb b/lib/fpg/packets/secret_key.rb
index c83a67b..16d834f 100644
--- a/lib/fpg/packets/secret_key.rb
+++ b/lib/fpg/packets/secret_key.rb
@@ -14,6 +14,14 @@ module FPG
uint16 :checksum, :onlyif => lambda{ ! long_checksum? }
uint160 :lchecksum, :onlyif => :long_checksum?
+
+ def hash_algo
+ self.s2k_specifier.hash_algo
+ end
+
+ def s2k_specifier_given?
+ s2k >= 254
+ end
def secret_key_bytes
header[:header_size] + byte_size - (encrypted_secret_key_material.offset) - checksum_size
end
@@ -23,9 +31,6 @@ module FPG
def s2k_algo
@s2k_algo ||= Algos::Symmetric.with_tag( s2k_specifier_given? ? s2k_al : s2k )
end
- def s2k_specifier_given?
- s2k >= 254
- end
def is_encrypted?
s2k > 0
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 2307e02..b66c5e2 100644
--- a/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb
+++ b/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb
@@ -1,7 +1,8 @@
module FPG
module Packets
class SymmetricEncryptedAndIntegrityProtectedData < Packet
- string :skip_this, :read_length => :byte_size
+ uint8 :version, :check_value => lambda { value == 1 }
+ string :data, :read_length => lambda { byte_size - 1 }
def self.tag
18
end
diff --git a/lib/fpg/packets/user_id.rb b/lib/fpg/packets/user_id.rb
index ec4cde3..c343a42 100644
--- a/lib/fpg/packets/user_id.rb
+++ b/lib/fpg/packets/user_id.rb
@@ -1,7 +1,7 @@
module FPG
module Packets
class UserId < Packet
- string :skip_this, :read_length => :byte_size
+ string :user_id, :read_length => :byte_size
def self.tag
13
end
diff --git a/spec/secret_key_spec.rb b/spec/secret_key_spec.rb
new file mode 100644
index 0000000..4268829
--- /dev/null
+++ b/spec/secret_key_spec.rb
@@ -0,0 +1,23 @@
+require File.join(File.dirname(__FILE__), %w[spec_helper])
+
+require 'stringio'
+
+include Parse
+include Packets
+include Algos
+
+describe SecretKey do
+ before(:each) do
+ @binary_sec = File.open "spec/fixtures/test.bin.sec", 'rb'
+ end
+
+ it "should parse a privatekey packet in a publickey" do
+ parsed = Parser.parse(@binary_sec)
+ pk = parsed.select{|packet| packet.is_a? SecretKey}.first
+ pk.algo.is_a?( Rsa ).should be_true
+ pk.key_material.n.bit_len.should == 4096
+ pk.hash_algo.is_a?( Sha1 ).should be_true
+
+ end
+end
+
diff --git a/spec/user_id_spec.rb b/spec/user_id_spec.rb
new file mode 100644
index 0000000..db7ef83
--- /dev/null
+++ b/spec/user_id_spec.rb
@@ -0,0 +1,19 @@
+require File.join(File.dirname(__FILE__), %w[spec_helper])
+
+require 'stringio'
+
+include FPG::Parse
+include FPG::Packets
+
+describe UserId do
+ before(:each) do
+ @binary_pub = File.open "spec/fixtures/test.bin.pub", 'rb'
+ end
+
+ it "should find the user id in a publickey" do
+ parsed = Parser.parse(@binary_pub)
+ uid = parsed.select{|packet| packet.is_a? UserId}.first
+ uid.user_id.should == "Test FPG (friendlier) <bla@bla.com>"
+ end
+end
+