summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-05-10 20:03:30 -0400
committerJoe Crayne <joe@jerkface.net>2020-05-10 20:03:30 -0400
commit47fdd273f68e0af73595daa1f3a9cdff2c8a9320 (patch)
tree0c08b57b5788a813d0dee875ff8ad67d62fc69c5
parent99ee915c0fdbf5bc718dbd59e9f5bbe9f5f7690c (diff)
Compute v5 fingerprints for v4 keys.
-rw-r--r--Data/OpenPGP.hs20
-rw-r--r--Data/OpenPGP/Util.hs1
-rw-r--r--Data/OpenPGP/Util/Fingerprint.hs20
3 files changed, 28 insertions, 13 deletions
diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs
index 3064dc5..bee718a 100644
--- a/Data/OpenPGP.hs
+++ b/Data/OpenPGP.hs
@@ -59,6 +59,8 @@ module Data.OpenPGP (
59 MPI(..), 59 MPI(..),
60 find_key, 60 find_key,
61 fingerprint_material, 61 fingerprint_material,
62 auto_fp_version,
63 fingerprint_materialv,
62 SignatureOver(..), 64 SignatureOver(..),
63 signatures, 65 signatures,
64 signature_issuer, 66 signature_issuer,
@@ -843,9 +845,17 @@ parse_packet 19 =
843-- Represent unsupported packets as their tag and literal bytes 845-- Represent unsupported packets as their tag and literal bytes
844parse_packet tag = fmap (UnsupportedPacket tag) getRemainingByteString 846parse_packet tag = fmap (UnsupportedPacket tag) getRemainingByteString
845 847
848auto_fp_version :: Packet -> Word8
849auto_fp_version p | version p == 2 = 3
850 | otherwise = version p
851
852-- | Helper method for fingerprints and such
853fingerprint_material :: HasCallStack => Packet -> [B.ByteString]
854fingerprint_material p = fingerprint_materialv (auto_fp_version p) p
855
846-- | Helper method for fingerprints and such 856-- | Helper method for fingerprints and such
847fingerprint_material :: Packet -> [B.ByteString] 857fingerprint_materialv :: HasCallStack => Word8 -> Packet -> [B.ByteString]
848fingerprint_material p | version p == 5 = 858fingerprint_materialv 5 p =
849 [ 859 [
850 B.singleton 0x9A, 860 B.singleton 0x9A,
851 encode (10 + fromIntegral (B.length material) :: Word32), 861 encode (10 + fromIntegral (B.length material) :: Word32),
@@ -855,7 +865,7 @@ fingerprint_material p | version p == 5 =
855 ] 865 ]
856 where 866 where
857 material = B.concat $ encode_public_key_material p 867 material = B.concat $ encode_public_key_material p
858fingerprint_material p | version p == 4 = 868fingerprint_materialv 4 p =
859 [ 869 [
860 B.singleton 0x99, 870 B.singleton 0x99,
861 encode (6 + fromIntegral (B.length material) :: Word16), 871 encode (6 + fromIntegral (B.length material) :: Word16),
@@ -864,11 +874,11 @@ fingerprint_material p | version p == 4 =
864 ] 874 ]
865 where 875 where
866 material = B.concat $ encode_public_key_material p 876 material = B.concat $ encode_public_key_material p
867fingerprint_material p | version p `elem` [2, 3] = [n, e] 877fingerprint_materialv 3 p | key_algorithm p == RSA = [n, e]
868 where 878 where
869 n = B.drop 2 (encode (key p ! 'n')) 879 n = B.drop 2 (encode (key p ! 'n'))
870 e = B.drop 2 (encode (key p ! 'e')) 880 e = B.drop 2 (encode (key p ! 'e'))
871fingerprint_material _ = 881fingerprint_materialv _ _ =
872 error "Unsupported Packet version or type in fingerprint_material." 882 error "Unsupported Packet version or type in fingerprint_material."
873 883
874enum_to_word8 :: (Enum a) => a -> Word8 884enum_to_word8 :: (Enum a) => a -> Word8
diff --git a/Data/OpenPGP/Util.hs b/Data/OpenPGP/Util.hs
index 1781d6d..889ff10 100644
--- a/Data/OpenPGP/Util.hs
+++ b/Data/OpenPGP/Util.hs
@@ -1,5 +1,6 @@
1module Data.OpenPGP.Util 1module Data.OpenPGP.Util
2 ( fingerprint 2 ( fingerprint
3 , fingerprintv
3 , Fingerprint(..) 4 , Fingerprint(..)
4 , hex 5 , hex
5 , decryptSecretKey 6 , decryptSecretKey
diff --git a/Data/OpenPGP/Util/Fingerprint.hs b/Data/OpenPGP/Util/Fingerprint.hs
index d88661b..cda25f6 100644
--- a/Data/OpenPGP/Util/Fingerprint.hs
+++ b/Data/OpenPGP/Util/Fingerprint.hs
@@ -1,11 +1,12 @@
1{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
2module Data.OpenPGP.Util.Fingerprint (fingerprint,Fingerprint(..),hex) where 2module Data.OpenPGP.Util.Fingerprint (fingerprint,fingerprintv,Fingerprint(..),hex) where
3 3
4import qualified Data.OpenPGP as OpenPGP 4import qualified Data.OpenPGP as OpenPGP
5import qualified Data.ByteString as BS 5import qualified Data.ByteString as BS
6import qualified Data.ByteString.Lazy as LZ 6import qualified Data.ByteString.Lazy as LZ
7import Data.Char (toUpper) 7import Data.Char (toUpper)
8import Data.Word (Word8) 8import Data.Word (Word8)
9import GHC.Stack
9import Numeric (showHex) 10import Numeric (showHex)
10 11
11#if defined(VERSION_cryptonite) 12#if defined(VERSION_cryptonite)
@@ -39,12 +40,15 @@ hex (Fingerprint bs) = hexify bs
39 40
40-- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket 41-- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket
41-- <http://tools.ietf.org/html/rfc4880#section-12.2> 42-- <http://tools.ietf.org/html/rfc4880#section-12.2>
42fingerprint :: OpenPGP.Packet -> Fingerprint 43fingerprint :: HasCallStack => OpenPGP.Packet -> Fingerprint
43fingerprint p 44fingerprint p = fingerprintv (OpenPGP.auto_fp_version p) p
44 | OpenPGP.version p == 5 = Fingerprint $ sha256 material 45
45 | OpenPGP.version p == 4 = Fingerprint $ sha1 material 46fingerprintv :: HasCallStack => Word8 -> OpenPGP.Packet -> Fingerprint
46 | OpenPGP.version p `elem` [2, 3] = Fingerprint $ md5 material 47fingerprintv v p = case v of
47 | otherwise = error "Unsupported Packet version or type in fingerprint" 48 5 -> Fingerprint $ sha256 material
49 4 -> Fingerprint $ sha1 material
50 3 -> Fingerprint $ md5 material
51 _ -> error "Unsupported Packet version or type in fingerprint"
48 where 52 where
49 53
50#if defined(VERSION_cryptonite) 54#if defined(VERSION_cryptonite)
@@ -57,4 +61,4 @@ fingerprint p
57 md5 = MD5.hashlazy 61 md5 = MD5.hashlazy
58#endif 62#endif
59 63
60 material = LZ.concat $ OpenPGP.fingerprint_material p 64 material = LZ.concat $ OpenPGP.fingerprint_materialv v p