From a901da5942b29e0a3f1ce358b6c14f56984934e7 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Sat, 16 May 2020 08:17:38 -0400 Subject: Encode v5 signatures (draft-ietf-openpgp-rfc4880bis-09). --- Data/OpenPGP.hs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs index cc97cd6..dc6fb78 100644 --- a/Data/OpenPGP.hs +++ b/Data/OpenPGP.hs @@ -383,14 +383,14 @@ secret_key_fields alg = fromMaybe (error $ "Unknown secret fields for "++show a -- Need this seperate for trailer calculation signature_packet_start :: Packet -> B.ByteString signature_packet_start (SignaturePacket { - version = 4, + version = v, signature_type = signature_type, key_algorithm = key_algorithm, hash_algorithm = hash_algorithm, hashed_subpackets = hashed_subpackets -}) = +}) | v==4 || v==5 = B.concat [ - encode (0x04 :: Word8), + encode (v :: Word8), encode signature_type, encode key_algorithm, encode hash_algorithm, @@ -416,12 +416,16 @@ calculate_signature_trailer (SignaturePacket { version = v, Just (SignatureCreationTimePacket creation_time) = find isCreation unhashed_subpackets isCreation (SignatureCreationTimePacket {}) = True isCreation _ = False -calculate_signature_trailer p@(SignaturePacket {version = 4}) = +calculate_signature_trailer p@(SignaturePacket {version = v}) | v==4 || v==5 = B.concat [ signature_packet_start p, - encode (0x04 :: Word8), + -- TODO: v5 document signatures (type 0x00 or 0x01) hash more fields here. + encode (v :: Word8), encode (0xff :: Word8), - encode (fromIntegral (B.length $ signature_packet_start p) :: Word32) + if v==4 + then encode (fromIntegral (B.length $ signature_packet_start p) :: Word32) + else encode (fromIntegral (B.length $ signature_packet_start p) :: Word64) + ] calculate_signature_trailer x = error ("Trying to calculate signature trailer for: " ++ show x) @@ -544,21 +548,23 @@ put_packet (SignaturePacket { version = v, Just (IssuerPacket keyidS) = find isIssuer unhashed_subpackets isIssuer (IssuerPacket {}) = True isIssuer _ = False -put_packet (SymmetricSessionKeyPacket version salgo s2k encd) = - (B.concat [encode version, encode salgo, encode s2k, encd], 3) -put_packet (SignaturePacket { version = 4, +put_packet (SignaturePacket { version = v, unhashed_subpackets = unhashed_subpackets, hash_head = hash_head, signature = signature, trailer = trailer }) = (B.concat $ [ - trailer_top, + B.take n trailer, encode (fromIntegral $ B.length unhashed :: Word16), unhashed, encode hash_head ] ++ map encode signature, 2) where - trailer_top = B.reverse $ B.drop 6 $ B.reverse trailer + n = case B.length trailer - (if v==5 then 10 else 6) of + x | x >=0 -> x + | otherwise -> 0 -- Should never happen. unhashed = B.concat $ map encode unhashed_subpackets +put_packet (SymmetricSessionKeyPacket version salgo s2k encd) = + (B.concat [encode version, encode salgo, encode s2k, encd], 3) put_packet (OnePassSignaturePacket { version = version, signature_type = signature_type, hash_algorithm = hash_algorithm, -- cgit v1.2.3