summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-26 01:31:54 +0200
committero <o@immerda.ch>2011-06-26 01:31:54 +0200
commit5852310dc002323728b9e06ad647f413dbea28ec (patch)
treec1e2eac1418fb3e5dca1b3b3dedc76f08bee9973
parent02d6f605f3738753c96ba6bf86c9f5b90216c9fe (diff)
encrypted messages (SymmetricEncryptedAndIntegrityProtectedData) apparently use new header format and partial size headers :/ -> added all that stuff
gpg mixes old and new header format ...
-rw-r--r--lib/fpg/packets/public_key_encrypted_session_key.rb6
-rw-r--r--lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb6
-rw-r--r--lib/fpg/parse/packet.rb52
-rw-r--r--lib/fpg/parse/packet_header.rb9
-rw-r--r--lib/fpg/parse/parser.rb9
-rw-r--r--lib/fpg/parse/public_key_encrypted_session_key.rb10
-rw-r--r--lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb10
-rw-r--r--spec/fixtures/messages/11
-rw-r--r--spec/fixtures/messages/1_enc.gpgbin0 -> 2696 bytes
-rw-r--r--spec/fixtures/messages/1_enc_sig.gpgbin0 -> 3476 bytes
-rw-r--r--spec/packet_parsing_spec.rb12
11 files changed, 100 insertions, 15 deletions
diff --git a/lib/fpg/packets/public_key_encrypted_session_key.rb b/lib/fpg/packets/public_key_encrypted_session_key.rb
new file mode 100644
index 0000000..bcd4738
--- /dev/null
+++ b/lib/fpg/packets/public_key_encrypted_session_key.rb
@@ -0,0 +1,6 @@
+module FPG
+ module Packets
+ class PublicKeyEncryptedSessionKey < Packet
+ 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
new file mode 100644
index 0000000..2629652
--- /dev/null
+++ b/lib/fpg/packets/symmetric_encrypted_and_integrity_protected_data.rb
@@ -0,0 +1,6 @@
+module FPG
+ module Packets
+ class SymmetricEncryptedAndIntegrityProtectedData < Packet
+ end
+ end
+end
diff --git a/lib/fpg/parse/packet.rb b/lib/fpg/parse/packet.rb
index d4b27d0..2f35828 100644
--- a/lib/fpg/parse/packet.rb
+++ b/lib/fpg/parse/packet.rb
@@ -1,22 +1,60 @@
module FPG
module Parse
module PacketParser
- def parse( stream )
- size= packet_size( stream )
- stream.seek(size,IO::SEEK_CUR)
+ attr_accessor :partial_length
+ attr_accessor :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)
+ while partial_length? do
+ self.byte_size += self.partial_byte_size = new_packet_size(stream)
+ parse_content(stream)
+ end
return self
end
- def packet_size( str )
- tag = size_tag(str.getbyte)
+ def parse_content(stream)
+ stream.seek(partial_byte_size,IO::SEEK_CUR)
+ end
+ def packet_size(str)
+ header = str.getbyte
+ if new_packet?(header)
+ new_packet_size(str)
+ else
+ tag = old_size_tag(header)
+ old_packet_size(tag,str)
+ end
+ end
+ def new_packet_size(str)
+ self.partial_length= false
+ first = str.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
+ 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
- raise "Partial Body lengths are not supported"
+ #TODO this seems a bit cumbersome
+ current_pos = str.pos
+ stream.seek(0,IO::SEEK_END)
+ size = 1+stream.pos - current_pos
+ stream.pos= current_pos
+ return size
end
- def size_tag( header )
+ def old_size_tag( header )
#size tag = two lowest bits
header & 3
end
+ def new_packet?(byte)
+ byte>>6 == 3
+ end
end
class Packet < Packets::Packet
include PacketParser
diff --git a/lib/fpg/parse/packet_header.rb b/lib/fpg/parse/packet_header.rb
index f388d8b..d1c9ff4 100644
--- a/lib/fpg/parse/packet_header.rb
+++ b/lib/fpg/parse/packet_header.rb
@@ -9,14 +9,17 @@ module FPG
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
+ 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-(1<<7))>>6 == 1
+ byte>>6 == 3
end
end
end
diff --git a/lib/fpg/parse/parser.rb b/lib/fpg/parse/parser.rb
index 8eb47e8..87fe1ce 100644
--- a/lib/fpg/parse/parser.rb
+++ b/lib/fpg/parse/parser.rb
@@ -4,11 +4,10 @@ module FPG
def self.parse( stream )
packets = []
until stream.eof? do
- begin
- packets << PacketHeader.new.parse(stream)
- rescue Exception => e
- raise 'parsing the message failed at position '+stream.pos.to_s+'. reason: '+ e.message
- end
+ packets << PacketHeader.new.parse(stream)
+ #rescue Exception => e
+ # raise 'parsing the message failed at position '+stream.pos.to_s+'. reason: '+ e.message
+ #end
end
packets
end
diff --git a/lib/fpg/parse/public_key_encrypted_session_key.rb b/lib/fpg/parse/public_key_encrypted_session_key.rb
new file mode 100644
index 0000000..99cc05e
--- /dev/null
+++ b/lib/fpg/parse/public_key_encrypted_session_key.rb
@@ -0,0 +1,10 @@
+module FPG
+ module Parse
+ module PublicKeyEncryptedSessionKeyParser
+ include PacketParser
+ end
+ class PublicKeyEncryptedSessionKey < Packets::PublicKeyEncryptedSessionKey
+ include PublicKeyEncryptedSessionKeyParser
+ 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
new file mode 100644
index 0000000..9185516
--- /dev/null
+++ b/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
@@ -0,0 +1,10 @@
+module FPG
+ module Parse
+ module SymmetricEncryptedAndIntegrityProtectedDataParser
+ include PacketParser
+ end
+ class SymmetricEncryptedAndIntegrityProtectedData < Packets::SymmetricEncryptedAndIntegrityProtectedData
+ include SymmetricEncryptedAndIntegrityProtectedDataParser
+ end
+ end
+end
diff --git a/spec/fixtures/messages/1 b/spec/fixtures/messages/1
new file mode 100644
index 0000000..6cc0a30
--- /dev/null
+++ b/spec/fixtures/messages/1
@@ -0,0 +1 @@
+gievuisooquainohpaesuzohquieveeyeufiedociefezohceeshooreneiyeechaeliecaiyaepovuphusaefiexaegheeyaphaichahnaidahghiexigaeneemeiphohghaicahpahvoarifathiezitudaiyieheidefaetheifiepeiwaepouquaephoemaewohfuotahjimeipheochimilaelohphoeneiciefoonaexutaisiteingeiparuayiekeiyughoofaechihohyooshifapaechoonohshupielahsedeethaxogeitesabohwoliepeeregaebeisoafeemialaofoofaaghuethiemierabiyiericeigeepulahqueirebiethahceifohghetosheiquathoodoongeeyooquuvaechooneinoopejailahbohgeenuyahmaesheegomiuchuachahzugoochohxaeghieraivoemogeishethaiphaexechubahdahhikocuupoitheiyaiyoovooxiengooyeirahsohniigeecheiyeidoniemeegaitocahgeiraevuquaetheowahrohjaizuliebohsheijayohtayeiwaesheedaeweihowaechaepaizahvudijeethohzaicenaihaemeecathoolacizosaicongomainaechungahdeeduteeceithoreiriyoozaejaegahjeiyaecoijahpohthahcoonohpuseiphiexooquexeexoshoozuujohxiegeiniengookisiemingeithoxooraelaijahjufeemucaechabohnoojeojohngashahngaijeimuphengeethaighugheequiithaitiedailoofahgheiquuteetuxahvootucohnoothochaithofaeviecaeghiesaizobahthizoocechuojiepheelaeboishodohghietucahmohphurailiiyuunesojohraefunohngigousooweeshaseiquoopeesiaghaineiwaiwajughaingorooghahgeicurieteenahphahquiacahzahuthuchaejaigeishegiequeiphieluchechoicuquawaikodemootoshaenazodeeyahzuzeithiesaemaefeixohnaiwaikuithooghahphuishohdorohthooyoofeenabohquooceechiubeijeipeicopohrijeerohdeleetureepoopeejaichoniajaeviquohnoodaaziengoosahrivirahruchiaghooxeraeyeinivahmaevubeezeuseeyouzesejohwaexieceinguediezashimahzoofaefeiphietaesoamohseefeceizeishohuveifohchieciequieboocaiquiseitohxaatheicheeleeviuyoolopheepahzooleexiabucahkifeseiquahitouvuluavuobooyeengiokeethuiquahheeheebiwaepithahkierahcahpahhaiwahshorahzeethaimalaichiuveiqueenaesiengaifiecahngolieriesaciahahveiphahthooghiephaesaithahdahchaedouheigaelaikohbapahquaezahmeihaishoevutiexihahshoocohcahwijeiraelaisecoghiejahfishoocheiyiehoneecivichoogiuheekohraejahciehaebamiejahyeelahdaiveshaequekuheechahmaenohneekeimiwaejeebaefaimahghaepuequooghahnuseiyoojaemeshiseesheifahbirohmeikoobaigehavaiphoophuijaidachiepohmeiviejohxeisohleeroquiedeiyaighaazefoocaereinaichozahhainaeshaizahgeemeiquaiheiquiemoogahgiephoopeewahduficeipheequievapiajuufooshaefaexaifotioghaecheushaevohngohxeipeeliithihahmaeshajulaeceizukahxooyaiphoocheisaotheuvasheivawoohaiphiekaoxeerukeefiepiedaidoosaihohahrienaebiebeisiephaejeihiahebeevohzeigaevaevezeilohciesohwuuquohbiapaudooneihaidahxaipeimecahdeeshocheowaethaetheikegineitiefaegeyieziewoazupheechoosaecheishohwohbeengeashoahieraejerooyohiesheimingoorefaihooquichaekainaixothielopegaequeicomeeshoingahkeegoofooboazotohseepahnifiemavaisoopahcheipohtoodeichouchohloakeiphutiiyeesiekipeiwaujikaemaimoothimeidoveenughiexohwaejumoavunohshofieshaishoilookamaeneekohnuzobaebohngaafeengiejeizieyaeyiugophopahghupudiangoovohrixeigeebeicaedujiukieraikeibeisheipahroeweehashuthetezoxangegainohmahjiewadiujeghooleemeefeiqueenajeetooweehahhohvoopheiceithioweeweizietiehaefoogaivohlaiqueimaghaegoogiyoobagohceihahxenaifieyohyoogheojohpheuphouvoephaejaraabeigaipaicheivaegohphailiethowahboodufeediesoafiohaiziedaikeiquainuheighahjieyomiekainooxoozaeyaezoajojohpahtebuimeisheedaequiegiequathaikoochaengepaibangahpaesahnulahyisiideixeitoongayaewohseipaeshohcheetohxuukeewozaebaedohngaquoofiemeehozoomohvoseeghaeweehureibuhudahnighaesahhaepheghaokahloghaebaecuiqueinohmaihoocheequeiraebielielavohngataichairichaeceiyuthahdoothoweejietaengeivoofahtaichaidufuighooxikaiquaipuwohyienoomeimeshaequohkeichojahqueitiedierikiengaibaetohcaetievaesaeriithiekutedoocholahshaeleicooyooripahxaiwochooxeefiemaimuoquaoweejuuseipheejughookeeghughaeniaheingeetaeheoyiachanoojogeengaetucavusooyeichohthaepausaitheiyohzahmathooyiquohngeirahbaxeuphailimaebeechooceegheeweengahmotaemaigaijoodisaesairefoothuechiemiraemifohghuxiekaiphaejisoothoucaebonaziemaikajijooxahgoghaerahpediekoohuwiepohmiewohtheeleizaefeeghivekilooweequaeleuvuorimahzuekeosahkieshichurohchafesheefiengoshaikeibiebeiyayashoucheeghithooboopheufoomeiyeipheitucheiveyeingaghiexawahshaifoojuphookoochohwaifoochiejiejaequahjahwunahwaeghohquahsh
diff --git a/spec/fixtures/messages/1_enc.gpg b/spec/fixtures/messages/1_enc.gpg
new file mode 100644
index 0000000..94742e7
--- /dev/null
+++ b/spec/fixtures/messages/1_enc.gpg
Binary files differ
diff --git a/spec/fixtures/messages/1_enc_sig.gpg b/spec/fixtures/messages/1_enc_sig.gpg
new file mode 100644
index 0000000..aef1b99
--- /dev/null
+++ b/spec/fixtures/messages/1_enc_sig.gpg
Binary files differ
diff --git a/spec/packet_parsing_spec.rb b/spec/packet_parsing_spec.rb
index 8f68207..bc31691 100644
--- a/spec/packet_parsing_spec.rb
+++ b/spec/packet_parsing_spec.rb
@@ -8,6 +8,8 @@ describe Parser do
before(:each) do
@binary_pub = File.open "spec/fixtures/test.bin.pub", 'rb'
@binary_sec = File.open "spec/fixtures/test.bin.sec", 'rb'
+ @msg_enc = File.open "spec/fixtures/messages/1_enc.gpg", 'rb'
+ @msg_enc_sig = File.open "spec/fixtures/messages/1_enc_sig.gpg", 'rb'
end
it "should find all the packets in a publickey" do
@@ -24,5 +26,15 @@ describe Parser do
parsed.any?{|packet| packet.is_a? SecretSubkey}.should be_true
parsed.any?{|packet| packet.is_a? SecretKey}.should be_true
end
+ it "should find all the packets in an encrypted message" do
+ parsed = Parser.parse(@msg_enc)
+ parsed.any?{|packet| packet.is_a? PublicKeyEncryptedSessionKey}.should be_true
+ parsed.any?{|packet| packet.is_a? SymmetricEncryptedAndIntegrityProtectedData}.should be_true
+ end
+ it "should find all the packets in an encrypted and signed message" do
+ parsed = Parser.parse(@msg_enc_sig)
+ parsed.any?{|packet| packet.is_a? PublicKeyEncryptedSessionKey}.should be_true
+ parsed.any?{|packet| packet.is_a? SymmetricEncryptedAndIntegrityProtectedData}.should be_true
+ end
end