summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Paul Weber <singpolyma@singpolyma.net>2013-01-01 13:27:34 -0500
committerStephen Paul Weber <singpolyma@singpolyma.net>2013-01-01 13:27:43 -0500
commite2f93583af0942b855b451c74ffe11dcf702ae7e (patch)
treeeb19c64bd174a3442c83b4ce269b3f91bc58b5a9
parente5d953a676a9077ff03fd9222083ab696de711a3 (diff)
SymmetricSessionKeyPacket
-rw-r--r--Data/OpenPGP.hs31
-rw-r--r--openpgp.cabal1
-rw-r--r--tests/data/symmetrically_encryptedbin0 -> 528 bytes
-rw-r--r--tests/suite.hs1
4 files changed, 26 insertions, 7 deletions
diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs
index 266cd9b..e71e48a 100644
--- a/Data/OpenPGP.hs
+++ b/Data/OpenPGP.hs
@@ -8,6 +8,7 @@ module Data.OpenPGP (
8 Packet( 8 Packet(
9 AsymmetricSessionKeyPacket, 9 AsymmetricSessionKeyPacket,
10 OnePassSignaturePacket, 10 OnePassSignaturePacket,
11 SymmetricSessionKeyPacket,
11 PublicKeyPacket, 12 PublicKeyPacket,
12 SecretKeyPacket, 13 SecretKeyPacket,
13 CompressedDataPacket, 14 CompressedDataPacket,
@@ -185,6 +186,13 @@ data Packet =
185 trailer::B.ByteString 186 trailer::B.ByteString
186 } | 187 } |
187 -- ^ <http://tools.ietf.org/html/rfc4880#section-5.2> 188 -- ^ <http://tools.ietf.org/html/rfc4880#section-5.2>
189 SymmetricSessionKeyPacket {
190 version::Word8,
191 symmetric_algorithm::SymmetricAlgorithm,
192 s2k::S2K,
193 encrypted_data::B.ByteString
194 } |
195 -- ^ <http://tools.ietf.org/html/rfc4880#section-5.3>
188 OnePassSignaturePacket { 196 OnePassSignaturePacket {
189 version::Word8, 197 version::Word8,
190 signature_type::Word8, 198 signature_type::Word8,
@@ -209,7 +217,7 @@ data Packet =
209 key_algorithm::KeyAlgorithm, 217 key_algorithm::KeyAlgorithm,
210 key::[(Char,MPI)], 218 key::[(Char,MPI)],
211 s2k_useage::Word8, 219 s2k_useage::Word8,
212 s2k::Maybe S2K, 220 s2k::S2K, -- ^ This is meaningless if symmetric_algorithm == Unencrypted
213 symmetric_algorithm::SymmetricAlgorithm, 221 symmetric_algorithm::SymmetricAlgorithm,
214 encrypted_data::B.ByteString, 222 encrypted_data::B.ByteString,
215 private_hash::Maybe B.ByteString, -- ^ the hash may be in the encrypted data 223 private_hash::Maybe B.ByteString, -- ^ the hash may be in the encrypted data
@@ -403,6 +411,8 @@ put_packet (SignaturePacket { version = v,
403 Just (IssuerPacket keyidS) = find isIssuer unhashed_subpackets 411 Just (IssuerPacket keyidS) = find isIssuer unhashed_subpackets
404 isIssuer (IssuerPacket {}) = True 412 isIssuer (IssuerPacket {}) = True
405 isIssuer _ = False 413 isIssuer _ = False
414put_packet (SymmetricSessionKeyPacket version salgo s2k encd) =
415 (B.concat [encode version, encode salgo, encode s2k, encd], 3)
406put_packet (SignaturePacket { version = 4, 416put_packet (SignaturePacket { version = 4,
407 unhashed_subpackets = unhashed_subpackets, 417 unhashed_subpackets = unhashed_subpackets,
408 hash_head = hash_head, 418 hash_head = hash_head,
@@ -435,9 +445,10 @@ put_packet (SecretKeyPacket { version = version, timestamp = timestamp,
435 encrypted_data = encrypted_data, 445 encrypted_data = encrypted_data,
436 is_subkey = is_subkey }) = 446 is_subkey = is_subkey }) =
437 (B.concat $ p : 447 (B.concat $ p :
438 (case s2k of 448 (if s2k_useage `elem` [254,255] then
439 Just s2k -> [encode s2k_useage, encode symmetric_algorithm, encode s2k] 449 [encode s2k_useage, encode symmetric_algorithm, encode s2k]
440 Nothing -> [encode symmetric_algorithm] 450 else
451 [encode symmetric_algorithm]
441 ) ++ 452 ) ++
442 (if symmetric_algorithm /= Unencrypted then 453 (if symmetric_algorithm /= Unencrypted then
443 [encrypted_data] 454 [encrypted_data]
@@ -552,6 +563,12 @@ parse_packet 2 = do
552 trailer = B.concat [encode version, encode signature_type, encode key_algorithm, encode hash_algorithm, encode (fromIntegral hashed_size :: Word16), hashed_data, B.pack [4, 0xff], encode ((6 + fromIntegral hashed_size) :: Word32)] 563 trailer = B.concat [encode version, encode signature_type, encode key_algorithm, encode hash_algorithm, encode (fromIntegral hashed_size :: Word16), hashed_data, B.pack [4, 0xff], encode ((6 + fromIntegral hashed_size) :: Word32)]
553 } 564 }
554 x -> fail $ "Unknown SignaturePacket version " ++ show x ++ "." 565 x -> fail $ "Unknown SignaturePacket version " ++ show x ++ "."
566-- SymmetricSessionKeyPacket, http://tools.ietf.org/html/rfc4880#section-5.3
567parse_packet 3 = SymmetricSessionKeyPacket
568 <$> (assertProp (==4) =<< get)
569 <*> get
570 <*> get
571 <*> getRemainingByteString
555-- OnePassSignaturePacket, http://tools.ietf.org/html/rfc4880#section-5.4 572-- OnePassSignaturePacket, http://tools.ietf.org/html/rfc4880#section-5.4
556parse_packet 4 = do 573parse_packet 4 = do
557 version <- get 574 version <- get
@@ -580,12 +597,12 @@ parse_packet 5 = do
580 s2k_useage <- get :: Get Word8 597 s2k_useage <- get :: Get Word8
581 let k = SecretKeyPacket version timestamp algorithm key s2k_useage 598 let k = SecretKeyPacket version timestamp algorithm key s2k_useage
582 (symmetric_algorithm, s2k) <- case () of 599 (symmetric_algorithm, s2k) <- case () of
583 _ | s2k_useage `elem` [255, 254] -> (,) <$> get <*> fmap Just get 600 _ | s2k_useage `elem` [255, 254] -> (,) <$> get <*> get
584 _ | s2k_useage > 0 -> 601 _ | s2k_useage > 0 ->
585 -- s2k_useage is symmetric_type in this case 602 -- s2k_useage is symmetric_type in this case
586 return (decode $ encode s2k_useage, Just $ SimpleS2K MD5) 603 return (decode $ encode s2k_useage, SimpleS2K MD5)
587 _ -> 604 _ ->
588 return (Unencrypted, Nothing) 605 return (Unencrypted, S2K 100 B.empty)
589 if symmetric_algorithm /= Unencrypted then do { 606 if symmetric_algorithm /= Unencrypted then do {
590 encrypted <- getRemainingByteString; 607 encrypted <- getRemainingByteString;
591 return (k s2k symmetric_algorithm encrypted Nothing False) 608 return (k s2k symmetric_algorithm encrypted Nothing False)
diff --git a/openpgp.cabal b/openpgp.cabal
index 9a92e22..3a1e054 100644
--- a/openpgp.cabal
+++ b/openpgp.cabal
@@ -120,6 +120,7 @@ extra-source-files:
120 tests/data/compressedsig.gpg, 120 tests/data/compressedsig.gpg,
121 tests/data/compressedsig-zlib.gpg, 121 tests/data/compressedsig-zlib.gpg,
122 tests/data/onepass_sig, 122 tests/data/onepass_sig,
123 tests/data/symmetrically_encrypted,
123 tests/data/pubring.gpg, 124 tests/data/pubring.gpg,
124 tests/data/secring.gpg, 125 tests/data/secring.gpg,
125 tests/data/uncompressed-ops-dsa.gpg, 126 tests/data/uncompressed-ops-dsa.gpg,
diff --git a/tests/data/symmetrically_encrypted b/tests/data/symmetrically_encrypted
new file mode 100644
index 0000000..129155a
--- /dev/null
+++ b/tests/data/symmetrically_encrypted
Binary files differ
diff --git a/tests/suite.hs b/tests/suite.hs
index feb5fe6..4bee6d6 100644
--- a/tests/suite.hs
+++ b/tests/suite.hs
@@ -141,6 +141,7 @@ tests =
141 testCase "compressedsig-zlib.gpg" (testSerialization "compressedsig-zlib.gpg"), 141 testCase "compressedsig-zlib.gpg" (testSerialization "compressedsig-zlib.gpg"),
142 testCase "compressedsig-bzip2.gpg" (testSerialization "compressedsig-bzip2.gpg"), 142 testCase "compressedsig-bzip2.gpg" (testSerialization "compressedsig-bzip2.gpg"),
143 testCase "onepass_sig" (testSerialization "onepass_sig"), 143 testCase "onepass_sig" (testSerialization "onepass_sig"),
144 testCase "symmetrically_encrypted" (testSerialization "symmetrically_encrypted"),
144 testCase "uncompressed-ops-dsa.gpg" (testSerialization "uncompressed-ops-dsa.gpg"), 145 testCase "uncompressed-ops-dsa.gpg" (testSerialization "uncompressed-ops-dsa.gpg"),
145 testCase "uncompressed-ops-dsa-sha384.txt.gpg" (testSerialization "uncompressed-ops-dsa-sha384.txt.gpg"), 146 testCase "uncompressed-ops-dsa-sha384.txt.gpg" (testSerialization "uncompressed-ops-dsa-sha384.txt.gpg"),
146 testCase "uncompressed-ops-rsa.gpg" (testSerialization "uncompressed-ops-rsa.gpg"), 147 testCase "uncompressed-ops-rsa.gpg" (testSerialization "uncompressed-ops-rsa.gpg"),