diff options
author | Stephen Paul Weber <singpolyma@singpolyma.net> | 2012-04-25 11:17:44 -0500 |
---|---|---|
committer | Stephen Paul Weber <singpolyma@singpolyma.net> | 2012-04-25 11:17:44 -0500 |
commit | b3a00a8206490fdf92762b1db86a06348582b4f7 (patch) | |
tree | 08148a3a4f621c4d156a755dfe3ccf486a68bf6d | |
parent | da82b6a356e6a1571047fdea15d26ec10c869fa4 (diff) |
s2k values are really optional, not undefined
Should they be grouped in some way?
Should encrypted_data also be grouped with them in some way?
-rw-r--r-- | Data/OpenPGP.hs | 32 | ||||
-rw-r--r-- | tests/suite.hs | 2 |
2 files changed, 19 insertions, 15 deletions
diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs index d950570..b77b36d 100644 --- a/Data/OpenPGP.hs +++ b/Data/OpenPGP.hs | |||
@@ -48,6 +48,7 @@ module Data.OpenPGP ( | |||
48 | import Control.Monad | 48 | import Control.Monad |
49 | import Data.Bits | 49 | import Data.Bits |
50 | import Data.Word | 50 | import Data.Word |
51 | import Data.Maybe | ||
51 | import Data.Map (Map, (!)) | 52 | import Data.Map (Map, (!)) |
52 | import qualified Data.Map as Map | 53 | import qualified Data.Map as Map |
53 | import qualified Data.ByteString.Lazy as LZ | 54 | import qualified Data.ByteString.Lazy as LZ |
@@ -93,12 +94,12 @@ data Packet = | |||
93 | timestamp::Word32, | 94 | timestamp::Word32, |
94 | key_algorithm::KeyAlgorithm, | 95 | key_algorithm::KeyAlgorithm, |
95 | key::Map Char MPI, | 96 | key::Map Char MPI, |
96 | s2k_useage::Word8, | 97 | s2k_useage::Word8, -- determines if the Maybes are Just or Nothing |
97 | symmetric_type::Word8, | 98 | symmetric_type::Maybe Word8, |
98 | s2k_type::Word8, | 99 | s2k_type::Maybe Word8, |
99 | s2k_hash_algorithm::HashAlgorithm, | 100 | s2k_hash_algorithm::Maybe HashAlgorithm, |
100 | s2k_salt::Word64, | 101 | s2k_salt::Maybe Word64, |
101 | s2k_count::Word32, | 102 | s2k_count::Maybe Word32, |
102 | encrypted_data::LZ.ByteString, | 103 | encrypted_data::LZ.ByteString, |
103 | private_hash::Maybe LZ.ByteString -- the hash may be in the encrypted data | 104 | private_hash::Maybe LZ.ByteString -- the hash may be in the encrypted data |
104 | } | | 105 | } | |
@@ -253,9 +254,11 @@ put_packet (SecretKeyPacket { version = version, timestamp = timestamp, | |||
253 | encrypted_data = encrypted_data }) = | 254 | encrypted_data = encrypted_data }) = |
254 | (LZ.concat $ [p, encode s2k_useage] ++ | 255 | (LZ.concat $ [p, encode s2k_useage] ++ |
255 | (if s2k_useage `elem` [255, 254] then | 256 | (if s2k_useage `elem` [255, 254] then |
256 | [encode symmetric_type, encode s2k_type, encode s2k_hash_algo] ++ | 257 | [encode $ fromJust symmetric_type, encode s2k_t, |
257 | (if s2k_type `elem` [1, 3] then [encode s2k_salt] else []) ++ | 258 | encode $ fromJust s2k_hash_algo] ++ |
258 | if s2k_type == 3 then [encode $ encode_s2k_count s2k_count] else [] | 259 | (if s2k_t `elem` [1,3] then [encode $ fromJust s2k_salt] else []) ++ |
260 | if s2k_t == 3 then | ||
261 | [encode $ encode_s2k_count $ fromJust s2k_count] else [] | ||
259 | else []) ++ | 262 | else []) ++ |
260 | (if s2k_useage > 0 then | 263 | (if s2k_useage > 0 then |
261 | [encrypted_data] | 264 | [encrypted_data] |
@@ -268,6 +271,7 @@ put_packet (SecretKeyPacket { version = version, timestamp = timestamp, | |||
268 | LZ.foldl (\c i -> (c + fromIntegral i) `mod` 65536) | 271 | LZ.foldl (\c i -> (c + fromIntegral i) `mod` 65536) |
269 | (0::Integer) (LZ.concat s) :: Word16)]), 5) | 272 | (0::Integer) (LZ.concat s) :: Word16)]), 5) |
270 | where | 273 | where |
274 | (Just s2k_t) = s2k_type | ||
271 | p = fst (put_packet $ PublicKeyPacket version timestamp algorithm key | 275 | p = fst (put_packet $ PublicKeyPacket version timestamp algorithm key |
272 | :: (LZ.ByteString, Integer)) -- Supress warning | 276 | :: (LZ.ByteString, Integer)) -- Supress warning |
273 | s = map (encode . (key !)) (secret_key_fields algorithm) | 277 | s = map (encode . (key !)) (secret_key_fields algorithm) |
@@ -363,13 +367,13 @@ parse_packet 5 = do | |||
363 | else return undefined | 367 | else return undefined |
364 | s2k_count <- if s2k_type == 3 then fmap decode_s2k_count get else | 368 | s2k_count <- if s2k_type == 3 then fmap decode_s2k_count get else |
365 | return undefined | 369 | return undefined |
366 | return (k symmetric_type s2k_type s2k_hash_algorithm | 370 | return (k (Just symmetric_type) (Just s2k_type) |
367 | s2k_salt s2k_count) | 371 | (Just s2k_hash_algorithm) (Just s2k_salt) (Just s2k_count)) |
368 | _ | s2k_useage > 0 -> | 372 | _ | s2k_useage > 0 -> |
369 | -- s2k_useage is symmetric_type in this case | 373 | -- s2k_useage is symmetric_type in this case |
370 | return (k s2k_useage undefined undefined undefined undefined) | 374 | return (k (Just s2k_useage) Nothing Nothing Nothing Nothing) |
371 | _ -> | 375 | _ -> |
372 | return (k undefined undefined undefined undefined undefined) | 376 | return (k Nothing Nothing Nothing Nothing Nothing) |
373 | if s2k_useage > 0 then do { | 377 | if s2k_useage > 0 then do { |
374 | encrypted <- getRemainingLazyByteString; | 378 | encrypted <- getRemainingLazyByteString; |
375 | return (k' encrypted Nothing) | 379 | return (k' encrypted Nothing) |
@@ -378,7 +382,7 @@ parse_packet 5 = do | |||
378 | mpi <- get :: Get MPI | 382 | mpi <- get :: Get MPI |
379 | return $ Map.insert f mpi m) key (secret_key_fields algorithm) | 383 | return $ Map.insert f mpi m) key (secret_key_fields algorithm) |
380 | private_hash <- getRemainingLazyByteString | 384 | private_hash <- getRemainingLazyByteString |
381 | return ((k' undefined (Just private_hash)) {key = key}) | 385 | return ((k' LZ.empty (Just private_hash)) {key = key}) |
382 | -- PublicKeyPacket, http://tools.ietf.org/html/rfc4880#section-5.5.2 | 386 | -- PublicKeyPacket, http://tools.ietf.org/html/rfc4880#section-5.5.2 |
383 | parse_packet 6 = do | 387 | parse_packet 6 = do |
384 | version <- get :: Get Word8 | 388 | version <- get :: Get Word8 |
diff --git a/tests/suite.hs b/tests/suite.hs index 17ab3cb..7ea5e57 100644 --- a/tests/suite.hs +++ b/tests/suite.hs | |||
@@ -139,8 +139,8 @@ tests secring = | |||
139 | testCase "000076-007.secret_subkey" (testSerialization "000076-007.secret_subkey"), | 139 | testCase "000076-007.secret_subkey" (testSerialization "000076-007.secret_subkey"), |
140 | testCase "000077-002.sig" (testSerialization "000077-002.sig"), | 140 | testCase "000077-002.sig" (testSerialization "000077-002.sig"), |
141 | testCase "000078-012.ring_trust" (testSerialization "000078-012.ring_trust"), | 141 | testCase "000078-012.ring_trust" (testSerialization "000078-012.ring_trust"), |
142 | -- Issue #11 -- testCase "secring.gpg" (testSerialization "secring.gpg"), | ||
143 | testCase "pubring.gpg" (testSerialization "pubring.gpg"), | 142 | testCase "pubring.gpg" (testSerialization "pubring.gpg"), |
143 | testCase "secring.gpg" (testSerialization "secring.gpg"), | ||
144 | testCase "compressedsig.gpg" (testSerialization "compressedsig.gpg"), | 144 | testCase "compressedsig.gpg" (testSerialization "compressedsig.gpg"), |
145 | testCase "compressedsig-zlib.gpg" (testSerialization "compressedsig-zlib.gpg"), | 145 | testCase "compressedsig-zlib.gpg" (testSerialization "compressedsig-zlib.gpg"), |
146 | testCase "compressedsig-bzip2.gpg" (testSerialization "compressedsig-bzip2.gpg"), | 146 | testCase "compressedsig-bzip2.gpg" (testSerialization "compressedsig-bzip2.gpg"), |