summaryrefslogtreecommitdiff
path: root/lib/Transforms.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Transforms.hs')
-rw-r--r--lib/Transforms.hs135
1 files changed, 8 insertions, 127 deletions
diff --git a/lib/Transforms.hs b/lib/Transforms.hs
index 0a3a9a6..edc18bb 100644
--- a/lib/Transforms.hs
+++ b/lib/Transforms.hs
@@ -22,7 +22,6 @@ import FunctorToMaybe
22import GnuPGAgent ( key_nbits ) 22import GnuPGAgent ( key_nbits )
23import PacketTranscoder 23import PacketTranscoder
24import TimeUtil 24import TimeUtil
25import qualified Data.Traversable as Traversable
26import qualified Data.ByteString as S 25import qualified Data.ByteString as S
27import qualified Data.ByteString.Lazy as L 26import qualified Data.ByteString.Lazy as L
28import qualified Data.ByteString.Lazy.Char8 as Char8 27import qualified Data.ByteString.Lazy.Char8 as Char8
@@ -120,25 +119,6 @@ data UserIDRecord = UserIDRecord {
120} 119}
121 deriving Show 120 deriving Show
122 121
123data PGPKeyFlags =
124 Special
125 | Vouch -- 0001 C -- Signkey
126 | Sign -- 0010 S
127 | VouchSign -- 0011
128 | Communication -- 0100 E
129 | VouchCommunication -- 0101
130 | SignCommunication -- 0110
131 | VouchSignCommunication -- 0111
132 | Storage -- 1000 E
133 | VouchStorage -- 1001
134 | SignStorage -- 1010
135 | VouchSignStorage -- 1011
136 | Encrypt -- 1100 E
137 | VouchEncrypt -- 1101
138 | SignEncrypt -- 1110
139 | VouchSignEncrypt -- 1111
140 deriving (Eq,Show,Read,Enum)
141
142 122
143 123
144-- Functions 124-- Functions
@@ -235,18 +215,6 @@ mkUsage tag = NotationDataPacket
235 } 215 }
236 216
237 217
238unk :: Bool -> MappedPacket -> MappedPacket
239unk isPublic = if isPublic then toPacket secretToPublic else id
240 where toPacket f mp@(MappedPacket {packet=p}) = mp {packet=(f p)}
241
242
243unsig :: FilePath -> Bool -> SigAndTrust -> [MappedPacket]
244unsig fname isPublic (sig,trustmap) =
245 sig : map (asMapped (-1)) ( take 1 . Map.elems $ Map.filterWithKey f trustmap)
246 where
247 f n _ = n==fname -- && trace ("fname=n="++show n) True
248 asMapped n p = let m = mappedPacket fname p
249 in m { locations = fmap (\x->x {originalNum=n}) (locations m) }
250 218
251smallpr :: Packet -> [Char] 219smallpr :: Packet -> [Char]
252smallpr k = drop 24 $ fingerprint k 220smallpr k = drop 24 $ fingerprint k
@@ -360,34 +328,6 @@ accBindings bs = as
360 (bc,_,bkind,bhashed,bclaimaints) 328 (bc,_,bkind,bhashed,bclaimaints)
361 = (ac .|. bc,p,akind++bkind,ahashed++bhashed,aclaimaints++bclaimaints) 329 = (ac .|. bc,p,akind++bkind,ahashed++bhashed,aclaimaints++bclaimaints)
362 330
363sortByHint :: FilePath -> (a -> MappedPacket) -> [a] -> [a]
364sortByHint fname f = sortBy (comparing gethint)
365 where
366 gethint = maybe defnum originalNum . Map.lookup fname . locations . f
367 defnum = -1
368
369concatSort ::
370 FilePath -> ([a] -> MappedPacket) -> (b -> [a]) -> [b] -> [a]
371concatSort fname getp f = concat . sortByHint fname getp . map f
372
373flattenUid :: FilePath -> Bool -> (String,([SigAndTrust],OriginMap)) -> [MappedPacket]
374flattenUid fname ispub (str,(sigs,om)) =
375 (mappedPacket "" $ UserIDPacket str) {locations=om} : concatSort fname head (unsig fname ispub) sigs
376
377flattenSub :: FilePath -> Bool -> SubKey -> [MappedPacket]
378flattenSub fname ispub (SubKey key sigs) = unk ispub key: concatSort fname head (unsig fname ispub) sigs
379
380flattenAllUids :: FilePath -> Bool -> Map.Map String ([SigAndTrust],OriginMap) -> [MappedPacket]
381flattenAllUids fname ispub uids =
382 concatSort fname head (flattenUid fname ispub) (Map.assocs uids)
383
384flattenTop :: FilePath -> Bool -> KeyData -> [MappedPacket]
385flattenTop fname ispub (KeyData key sigs uids subkeys) =
386 unk ispub key :
387 ( flattenAllUids fname ispub uids
388 ++ concatSort fname head (flattenSub fname ispub) (Map.elems subkeys))
389
390
391sigpackets :: 331sigpackets ::
392 Monad m => 332 Monad m =>
393 Word8 -> [SignatureSubpacket] -> [SignatureSubpacket] -> m Packet 333 Word8 -> [SignatureSubpacket] -> [SignatureSubpacket] -> m Packet
@@ -407,72 +347,11 @@ sigpackets typ hashed unhashed = return $
407keyFlags :: t -> [Packet] -> [SignatureSubpacket] 347keyFlags :: t -> [Packet] -> [SignatureSubpacket]
408keyFlags wkun uids = keyFlags0 wkun (filter isSignaturePacket uids) 348keyFlags wkun uids = keyFlags0 wkun (filter isSignaturePacket uids)
409 349
410-- XXX keyFlags and keyflags are different functions. 350
411keyflags :: SignatureSubpacket -> Maybe PGPKeyFlags 351
412keyflags flgs@(KeyFlagsPacket {}) = 352
413 Just . toEnum $ 353
414 ( bit 0x1 certify_keys 354
415 .|. bit 0x2 sign_data
416 .|. bit 0x4 encrypt_communication
417 .|. bit 0x8 encrypt_storage ) :: Maybe PGPKeyFlags
418 -- other flags:
419 -- split_key
420 -- authentication (ssh-client)
421 -- group_key
422 where
423 bit v f = if f flgs then v else 0
424keyflags _ = Nothing
425
426
427
428secretToPublic :: Packet -> Packet
429secretToPublic pkt@(SecretKeyPacket {}) =
430 PublicKeyPacket { version = version pkt
431 , timestamp = timestamp pkt
432 , key_algorithm = key_algorithm pkt
433 -- , ecc_curve = ecc_curve pkt
434 , key = let seckey = key pkt
435 pubs = public_key_fields (key_algorithm pkt)
436 in filter (\(k,v) -> k `elem` pubs) seckey
437 , is_subkey = is_subkey pkt
438 , v3_days_of_validity = Nothing
439 }
440secretToPublic pkt = pkt
441
442
443
444uidkey :: Packet -> String
445uidkey (UserIDPacket str) = str
446
447usageString :: PGPKeyFlags -> String
448usageString flgs =
449 case flgs of
450 Special -> "special"
451 Vouch -> "vouch" -- signkey
452 Sign -> "sign"
453 VouchSign -> "vouch-sign"
454 Communication -> "communication"
455 VouchCommunication -> "vouch-communication"
456 SignCommunication -> "sign-communication"
457 VouchSignCommunication -> "vouch-sign-communication"
458 Storage -> "storage"
459 VouchStorage -> "vouch-storage"
460 SignStorage -> "sign-storage"
461 VouchSignStorage -> "vouch-sign-storage"
462 Encrypt -> "encrypt"
463 VouchEncrypt -> "vouch-encrypt"
464 SignEncrypt -> "sign-encrypt"
465 VouchSignEncrypt -> "vouch-sign-encrypt"
466
467
468
469usage :: SignatureSubpacket -> Maybe String
470usage (NotationDataPacket
471 { human_readable = True
472 , notation_name = "usage@"
473 , notation_value = u
474 }) = Just u
475usage _ = Nothing
476 355
477 356
478ifSecret :: Packet -> t -> t -> t 357ifSecret :: Packet -> t -> t -> t
@@ -487,7 +366,7 @@ showPacket p | isKey p = (if is_subkey p
487 ++ " "++fingerprint p 366 ++ " "++fingerprint p
488 ++ " "++show (key_algorithm p) 367 ++ " "++show (key_algorithm p)
489 ++ case key_nbits p of { 0 -> ""; n -> "("++show n++")" } 368 ++ case key_nbits p of { 0 -> ""; n -> "("++show n++")" }
490 | isUserID p = showPacket0 p ++ " " ++ show (uidkey p) 369 | Just uid <- isUserID p = showPacket0 p ++ " " ++ show uid
491 -- isSignaturePacket p = showPacket0 p ++ maybe "" ((++) (" ^ signed"++sigusage p++": ")) (signature_issuer p) 370 -- isSignaturePacket p = showPacket0 p ++ maybe "" ((++) (" ^ signed"++sigusage p++": ")) (signature_issuer p)
492 | isSignaturePacket p = showPacket0 p ++ maybe "" (" ^ signed: "++) (signature_issuer p) ++ sigusage p 371 | isSignaturePacket p = showPacket0 p ++ maybe "" (" ^ signed: "++) (signature_issuer p) ++ sigusage p
493 | otherwise = showPacket0 p 372 | otherwise = showPacket0 p
@@ -721,8 +600,10 @@ makeSig doDecrypt top fname subkey_p tags mbsig = do
721 (es,qs) = partition isExpiration ps 600 (es,qs) = partition isExpiration ps
722 stamp = listToMaybe . sortBy (comparing Down) $ 601 stamp = listToMaybe . sortBy (comparing Down) $
723 map unwrap cs where unwrap (SignatureCreationTimePacket x) = x 602 map unwrap cs where unwrap (SignatureCreationTimePacket x) = x
603 unwrap _ = error "isCreation fail"
724 exp = listToMaybe $ sort $ 604 exp = listToMaybe $ sort $
725 map unwrap es where unwrap (SignatureExpirationTimePacket x) = x 605 map unwrap es where unwrap (SignatureExpirationTimePacket x) = x
606 unwrap _ = error "isExpiration fail"
726 expires = liftA2 (+) stamp exp 607 expires = liftA2 (+) stamp exp
727 timestamp <- now 608 timestamp <- now
728 if fmap ( (< timestamp) . fromIntegral) expires == Just True then 609 if fmap ( (< timestamp) . fromIntegral) expires == Just True then