-- Serialize.hs: OpenPGP (RFC4880) serialization (using cereal)
-- Copyright © 2012-2020  Clint Adams
-- This software is released under the terms of the Expat license.
-- (See the LICENSE file).
module Codec.Encryption.OpenPGP.Serialize
  (
  -- * Serialization functions
    putSKAddendum
  , getSecretKey
  -- * Utilities
  , parsePkts
  ) where

import Control.Applicative (many, some)
import Control.Lens ((^.), _1)
import Control.Monad (guard, replicateM, replicateM_)
import Crypto.Number.Basic (numBits)
import Crypto.Number.Serialize (i2osp, os2ip)
import qualified Crypto.PubKey.DSA as D
import qualified Crypto.PubKey.ECC.ECDSA as ECDSA
import qualified Crypto.PubKey.ECC.Types as ECCT
import qualified Crypto.PubKey.RSA as R
import Data.Bifunctor (bimap)
import Data.Binary (Binary, get, put)
import Data.Binary.Get
  ( ByteOffset
  , Get
  , getByteString
  , getLazyByteString
  , getRemainingLazyByteString
  , getWord16be
  , getWord16le
  , getWord32be
  , getWord8
  , runGetOrFail
  )
import Data.Binary.Put
  ( Put
  , putByteString
  , putLazyByteString
  , putWord16be
  , putWord16le
  , putWord32be
  , putWord8
  , runPut
  )
import Data.Bits ((.&.), (.|.), shiftL, shiftR, testBit)
import qualified Data.ByteString as B
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BL
import qualified Data.Foldable as F
import Data.List (mapAccumL)
import qualified Data.List.NonEmpty as NE
import Data.Maybe (fromMaybe)
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8With, encodeUtf8)
import Data.Text.Encoding.Error (lenientDecode)
import Data.Word (Word16, Word32, Word8)
import Network.URI (nullURI, parseURI, uriToString)

import Codec.Encryption.OpenPGP.Internal
  ( curve2Curve
  , curveFromCurve
  , curveToCurveoidBS
  , curveoidBSToCurve
  , curveoidBSToEdSigningCurve
  , edSigningCurveToCurveoidBS
  , multiplicativeInverse
  , pubkeyToMPIs
  )
import Codec.Encryption.OpenPGP.Types

instance Binary SigSubPacket where
  get :: Get SigSubPacket
get = Get SigSubPacket
getSigSubPacket
  put :: SigSubPacket -> Put
put = SigSubPacket -> Put
putSigSubPacket

-- instance Binary (Set NotationFlag) where
--     put = putNotationFlagSet
instance Binary CompressionAlgorithm where
  get :: Get CompressionAlgorithm
get = Word8 -> CompressionAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> CompressionAlgorithm)
-> Get Word8 -> Get CompressionAlgorithm
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
  put :: CompressionAlgorithm -> Put
put = Word8 -> Put
putWord8 (Word8 -> Put)
-> (CompressionAlgorithm -> Word8) -> CompressionAlgorithm -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CompressionAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal

instance Binary PubKeyAlgorithm where
  get :: Get PubKeyAlgorithm
get = Word8 -> PubKeyAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> PubKeyAlgorithm) -> Get Word8 -> Get PubKeyAlgorithm
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
  put :: PubKeyAlgorithm -> Put
put = Word8 -> Put
putWord8 (Word8 -> Put)
-> (PubKeyAlgorithm -> Word8) -> PubKeyAlgorithm -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubKeyAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal

instance Binary HashAlgorithm where
  get :: Get HashAlgorithm
get = Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> HashAlgorithm) -> Get Word8 -> Get HashAlgorithm
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
  put :: HashAlgorithm -> Put
put = Word8 -> Put
putWord8 (Word8 -> Put) -> (HashAlgorithm -> Word8) -> HashAlgorithm -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal

instance Binary SymmetricAlgorithm where
  get :: Get SymmetricAlgorithm
get = Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> SymmetricAlgorithm)
-> Get Word8 -> Get SymmetricAlgorithm
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
  put :: SymmetricAlgorithm -> Put
put = Word8 -> Put
putWord8 (Word8 -> Put)
-> (SymmetricAlgorithm -> Word8) -> SymmetricAlgorithm -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymmetricAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal

instance Binary MPI where
  get :: Get MPI
get = Get MPI
getMPI
  put :: MPI -> Put
put = MPI -> Put
putMPI

instance Binary SigType where
  get :: Get SigType
get = Word8 -> SigType
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> SigType) -> Get Word8 -> Get SigType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
  put :: SigType -> Put
put = Word8 -> Put
putWord8 (Word8 -> Put) -> (SigType -> Word8) -> SigType -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SigType -> Word8
forall a. FutureVal a => a -> Word8
fromFVal

instance Binary UserAttrSubPacket where
  get :: Get UserAttrSubPacket
get = Get UserAttrSubPacket
getUserAttrSubPacket
  put :: UserAttrSubPacket -> Put
put = UserAttrSubPacket -> Put
putUserAttrSubPacket

instance Binary S2K where
  get :: Get S2K
get = Get S2K
getS2K
  put :: S2K -> Put
put = S2K -> Put
putS2K

instance Binary PKESK where
  get :: Get PKESK
get = (Pkt -> PKESK) -> Get Pkt -> Get PKESK
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> PKESK
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: PKESK -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (PKESK -> Pkt) -> PKESK -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PKESK -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary Signature where
  get :: Get Signature
get = (Pkt -> Signature) -> Get Pkt -> Get Signature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> Signature
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: Signature -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (Signature -> Pkt) -> Signature -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary SKESK where
  get :: Get SKESK
get = (Pkt -> SKESK) -> Get Pkt -> Get SKESK
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> SKESK
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: SKESK -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (SKESK -> Pkt) -> SKESK -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SKESK -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary OnePassSignature where
  get :: Get OnePassSignature
get = (Pkt -> OnePassSignature) -> Get Pkt -> Get OnePassSignature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> OnePassSignature
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: OnePassSignature -> Put
put = Pkt -> Put
putPkt (Pkt -> Put)
-> (OnePassSignature -> Pkt) -> OnePassSignature -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnePassSignature -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary SecretKey where
  get :: Get SecretKey
get = (Pkt -> SecretKey) -> Get Pkt -> Get SecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> SecretKey
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: SecretKey -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (SecretKey -> Pkt) -> SecretKey -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretKey -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary PublicKey where
  get :: Get PublicKey
get = (Pkt -> PublicKey) -> Get Pkt -> Get PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> PublicKey
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: PublicKey -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (PublicKey -> Pkt) -> PublicKey -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary SecretSubkey where
  get :: Get SecretSubkey
get = (Pkt -> SecretSubkey) -> Get Pkt -> Get SecretSubkey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> SecretSubkey
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: SecretSubkey -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (SecretSubkey -> Pkt) -> SecretSubkey -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretSubkey -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary CompressedData where
  get :: Get CompressedData
get = (Pkt -> CompressedData) -> Get Pkt -> Get CompressedData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> CompressedData
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: CompressedData -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (CompressedData -> Pkt) -> CompressedData -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CompressedData -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary SymEncData where
  get :: Get SymEncData
get = (Pkt -> SymEncData) -> Get Pkt -> Get SymEncData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> SymEncData
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: SymEncData -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (SymEncData -> Pkt) -> SymEncData -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymEncData -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary Marker where
  get :: Get Marker
get = (Pkt -> Marker) -> Get Pkt -> Get Marker
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> Marker
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: Marker -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (Marker -> Pkt) -> Marker -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Marker -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary LiteralData where
  get :: Get LiteralData
get = (Pkt -> LiteralData) -> Get Pkt -> Get LiteralData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> LiteralData
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: LiteralData -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (LiteralData -> Pkt) -> LiteralData -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LiteralData -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary Trust where
  get :: Get Trust
get = (Pkt -> Trust) -> Get Pkt -> Get Trust
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> Trust
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: Trust -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (Trust -> Pkt) -> Trust -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trust -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary UserId where
  get :: Get UserId
get = (Pkt -> UserId) -> Get Pkt -> Get UserId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> UserId
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: UserId -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (UserId -> Pkt) -> UserId -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserId -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary PublicSubkey where
  get :: Get PublicSubkey
get = (Pkt -> PublicSubkey) -> Get Pkt -> Get PublicSubkey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> PublicSubkey
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: PublicSubkey -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (PublicSubkey -> Pkt) -> PublicSubkey -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicSubkey -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary UserAttribute where
  get :: Get UserAttribute
get = (Pkt -> UserAttribute) -> Get Pkt -> Get UserAttribute
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> UserAttribute
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: UserAttribute -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (UserAttribute -> Pkt) -> UserAttribute -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserAttribute -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary SymEncIntegrityProtectedData where
  get :: Get SymEncIntegrityProtectedData
get = (Pkt -> SymEncIntegrityProtectedData)
-> Get Pkt -> Get SymEncIntegrityProtectedData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> SymEncIntegrityProtectedData
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: SymEncIntegrityProtectedData -> Put
put = Pkt -> Put
putPkt (Pkt -> Put)
-> (SymEncIntegrityProtectedData -> Pkt)
-> SymEncIntegrityProtectedData
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymEncIntegrityProtectedData -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary ModificationDetectionCode where
  get :: Get ModificationDetectionCode
get = (Pkt -> ModificationDetectionCode)
-> Get Pkt -> Get ModificationDetectionCode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> ModificationDetectionCode
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: ModificationDetectionCode -> Put
put = Pkt -> Put
putPkt (Pkt -> Put)
-> (ModificationDetectionCode -> Pkt)
-> ModificationDetectionCode
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModificationDetectionCode -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary OtherPacket where
  get :: Get OtherPacket
get = (Pkt -> OtherPacket) -> Get Pkt -> Get OtherPacket
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pkt -> OtherPacket
forall a. Packet a => Pkt -> a
fromPkt Get Pkt
getPkt
  put :: OtherPacket -> Put
put = Pkt -> Put
putPkt (Pkt -> Put) -> (OtherPacket -> Pkt) -> OtherPacket -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OtherPacket -> Pkt
forall a. Packet a => a -> Pkt
toPkt

instance Binary Pkt where
  get :: Get Pkt
get = Get Pkt
getPkt
  put :: Pkt -> Put
put = Pkt -> Put
putPkt

instance Binary a => Binary (Block a) where
  get :: Get (Block a)
get = [a] -> Block a
forall a. [a] -> Block a
Block ([a] -> Block a) -> Get [a] -> Get (Block a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a -> Get [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Get a
forall t. Binary t => Get t
get
  put :: Block a -> Put
put = (a -> Put) -> [a] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ a -> Put
forall t. Binary t => t -> Put
put ([a] -> Put) -> (Block a -> [a]) -> Block a -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block a -> [a]
forall a. Block a -> [a]
unBlock

instance Binary PKPayload where
  get :: Get PKPayload
get = Get PKPayload
getPKPayload
  put :: PKPayload -> Put
put = PKPayload -> Put
putPKPayload

instance Binary SignaturePayload where
  get :: Get SignaturePayload
get = Get SignaturePayload
getSignaturePayload
  put :: SignaturePayload -> Put
put = SignaturePayload -> Put
putSignaturePayload

instance Binary TK where
  get :: Get TK
get = Get TK
forall a. HasCallStack => a
undefined
  put :: TK -> Put
put = TK -> Put
putTK

getSigSubPacket :: Get SigSubPacket
getSigSubPacket :: Get SigSubPacket
getSigSubPacket = do
  ByteOffset
l <- (Word32 -> ByteOffset) -> Get Word32 -> Get ByteOffset
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Get Word32
getSubPacketLength
  (crit :: Bool
crit, pt :: Word8
pt) <- Get (Bool, Word8)
getSigSubPacketType
  Word8 -> Bool -> ByteOffset -> Get SigSubPacket
getSigSubPacket' Word8
pt Bool
crit ByteOffset
l
  where
    getSigSubPacket' :: Word8 -> Bool -> ByteOffset -> Get SigSubPacket
    getSigSubPacket' :: Word8 -> Bool -> ByteOffset -> Get SigSubPacket
getSigSubPacket' pt :: Word8
pt crit :: Bool
crit l :: ByteOffset
l
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 2 = do
        ThirtyTwoBitTimeStamp
et <- (Word32 -> ThirtyTwoBitTimeStamp)
-> Get Word32 -> Get ThirtyTwoBitTimeStamp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ThirtyTwoBitTimeStamp
ThirtyTwoBitTimeStamp Get Word32
getWord32be
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (ThirtyTwoBitTimeStamp -> SigSubPacketPayload
SigCreationTime ThirtyTwoBitTimeStamp
et)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 3 = do
        ThirtyTwoBitDuration
et <- (Word32 -> ThirtyTwoBitDuration)
-> Get Word32 -> Get ThirtyTwoBitDuration
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ThirtyTwoBitDuration
ThirtyTwoBitDuration Get Word32
getWord32be
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (ThirtyTwoBitDuration -> SigSubPacketPayload
SigExpirationTime ThirtyTwoBitDuration
et)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 4 = do
        Bool
e <- Get Bool
forall t. Binary t => Get t
get
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Bool -> SigSubPacketPayload
ExportableCertification Bool
e)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 5 = do
        Word8
tl <- Get Word8
getWord8
        Word8
ta <- Get Word8
getWord8
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Word8 -> Word8 -> SigSubPacketPayload
TrustSignature Word8
tl Word8
ta)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 6 = do
        ByteString
apdre <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 2)
        Word8
nul <- Get Word8
getWord8
        Bool -> Get ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word8
nul Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (ByteString -> SigSubPacketPayload
RegularExpression (ByteString -> ByteString
BL.copy ByteString
apdre))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 7 = do
        Bool
r <- Get Bool
forall t. Binary t => Get t
get
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Bool -> SigSubPacketPayload
Revocable Bool
r)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 9 = do
        ThirtyTwoBitDuration
et <- (Word32 -> ThirtyTwoBitDuration)
-> Get Word32 -> Get ThirtyTwoBitDuration
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ThirtyTwoBitDuration
ThirtyTwoBitDuration Get Word32
getWord32be
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (ThirtyTwoBitDuration -> SigSubPacketPayload
KeyExpirationTime ThirtyTwoBitDuration
et)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 11 = do
        [SymmetricAlgorithm]
sa <- Int -> Get SymmetricAlgorithm -> Get [SymmetricAlgorithm]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)) Get SymmetricAlgorithm
forall t. Binary t => Get t
get
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit ([SymmetricAlgorithm] -> SigSubPacketPayload
PreferredSymmetricAlgorithms [SymmetricAlgorithm]
sa)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 12 = do
        Word8
rclass <- Get Word8
getWord8
        Bool -> Get ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
rclass 7)
        PubKeyAlgorithm
algid <- Get PubKeyAlgorithm
forall t. Binary t => Get t
get
        ByteString
fp <- ByteOffset -> Get ByteString
getLazyByteString 20
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$
          Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket
            Bool
crit
            (Set RevocationClass
-> PubKeyAlgorithm -> TwentyOctetFingerprint -> SigSubPacketPayload
RevocationKey
               (ByteString -> Set RevocationClass
forall a. FutureFlag a => ByteString -> Set a
bsToFFSet (ByteString -> Set RevocationClass)
-> (Word8 -> ByteString) -> Word8 -> Set RevocationClass
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ByteString
BL.singleton (Word8 -> Set RevocationClass) -> Word8 -> Set RevocationClass
forall a b. (a -> b) -> a -> b
$ Word8
rclass Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x7f)
               PubKeyAlgorithm
algid
               (ByteString -> TwentyOctetFingerprint
TwentyOctetFingerprint ByteString
fp))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 16 = do
        ByteString
keyid <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (EightOctetKeyId -> SigSubPacketPayload
Issuer (ByteString -> EightOctetKeyId
EightOctetKeyId ByteString
keyid))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 20 = do
        ByteString
flags <- ByteOffset -> Get ByteString
getLazyByteString 4
        Word16
nl <- Get Word16
getWord16be
        Word16
vl <- Get Word16
getWord16be
        ByteString
nn <- ByteOffset -> Get ByteString
getLazyByteString (Word16 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
nl)
        ByteString
nv <- ByteOffset -> Get ByteString
getLazyByteString (Word16 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
vl)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$
          Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket
            Bool
crit
            (Set NotationFlag
-> NotationName -> NotationValue -> SigSubPacketPayload
NotationData (ByteString -> Set NotationFlag
forall a. FutureFlag a => ByteString -> Set a
bsToFFSet ByteString
flags) (ByteString -> NotationName
NotationName ByteString
nn) (ByteString -> NotationValue
NotationValue ByteString
nv))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 21 = do
        [HashAlgorithm]
ha <- Int -> Get HashAlgorithm -> Get [HashAlgorithm]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)) Get HashAlgorithm
forall t. Binary t => Get t
get
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit ([HashAlgorithm] -> SigSubPacketPayload
PreferredHashAlgorithms [HashAlgorithm]
ha)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 22 = do
        [CompressionAlgorithm]
ca <- Int -> Get CompressionAlgorithm -> Get [CompressionAlgorithm]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)) Get CompressionAlgorithm
forall t. Binary t => Get t
get
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit ([CompressionAlgorithm] -> SigSubPacketPayload
PreferredCompressionAlgorithms [CompressionAlgorithm]
ca)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 23 = do
        ByteString
ksps <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Set KSPFlag -> SigSubPacketPayload
KeyServerPreferences (ByteString -> Set KSPFlag
forall a. FutureFlag a => ByteString -> Set a
bsToFFSet ByteString
ksps))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 24 = do
        ByteString
pks <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (ByteString -> SigSubPacketPayload
PreferredKeyServer ByteString
pks)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 25 = do
        Bool
primacy <- Get Bool
forall t. Binary t => Get t
get
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Bool -> SigSubPacketPayload
PrimaryUserId Bool
primacy)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 26 = do
        URL
url <-
          (ByteString -> URL) -> Get ByteString -> Get URL
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
            (URI -> URL
URL (URI -> URL) -> (ByteString -> URI) -> ByteString -> URL
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> Maybe URI -> URI
forall a. a -> Maybe a -> a
fromMaybe URI
nullURI (Maybe URI -> URI)
-> (ByteString -> Maybe URI) -> ByteString -> URI
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe URI
parseURI (String -> Maybe URI)
-> (ByteString -> String) -> ByteString -> Maybe URI
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack (Text -> String) -> (ByteString -> Text) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
             OnDecodeError -> ByteString -> Text
decodeUtf8With OnDecodeError
lenientDecode)
            (Int -> Get ByteString
getByteString (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)))
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (URL -> SigSubPacketPayload
PolicyURL URL
url)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 27 = do
        ByteString
kfs <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Set KeyFlag -> SigSubPacketPayload
KeyFlags (ByteString -> Set KeyFlag
forall a. FutureFlag a => ByteString -> Set a
bsToFFSet ByteString
kfs))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 28 = do
        ByteString
uid <- Int -> Get ByteString
getByteString (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1))
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$
          Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Text -> SigSubPacketPayload
SignersUserId (OnDecodeError -> ByteString -> Text
decodeUtf8With OnDecodeError
lenientDecode ByteString
uid))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 29 = do
        Word8
rcode <- Get Word8
getWord8
        Text
rreason <-
          (ByteString -> Text) -> Get ByteString -> Get Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
            (OnDecodeError -> ByteString -> Text
decodeUtf8With OnDecodeError
lenientDecode)
            (Int -> Get ByteString
getByteString (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 2)))
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (RevocationCode -> Text -> SigSubPacketPayload
ReasonForRevocation (Word8 -> RevocationCode
forall a. FutureVal a => Word8 -> a
toFVal Word8
rcode) Text
rreason)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 30 = do
        ByteString
fbs <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Set FeatureFlag -> SigSubPacketPayload
Features (ByteString -> Set FeatureFlag
forall a. FutureFlag a => ByteString -> Set a
bsToFFSet ByteString
fbs))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 31 = do
        PubKeyAlgorithm
pka <- Get PubKeyAlgorithm
forall t. Binary t => Get t
get
        HashAlgorithm
ha <- Get HashAlgorithm
forall t. Binary t => Get t
get
        ByteString
hash <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 3)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (PubKeyAlgorithm
-> HashAlgorithm -> ByteString -> SigSubPacketPayload
SignatureTarget PubKeyAlgorithm
pka HashAlgorithm
ha ByteString
hash)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 32 = do
        SignaturePayload
sp <- Get SignaturePayload
forall t. Binary t => Get t
get :: Get SignaturePayload
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (SignaturePayload -> SigSubPacketPayload
EmbeddedSignature SignaturePayload
sp)
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 33 = do
        Word8
kv <- Get Word8
getWord8
        ByteString
fp <-
          ByteOffset -> Get ByteString
getLazyByteString
            (if Word8
kv Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 4
               then 20
               else 32)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$
          Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Word8 -> TwentyOctetFingerprint -> SigSubPacketPayload
IssuerFingerprint Word8
kv (ByteString -> TwentyOctetFingerprint
TwentyOctetFingerprint ByteString
fp))
      | Word8
pt Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
> 99 Bool -> Bool -> Bool
&& Word8
pt Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< 111 = do
        ByteString
payload <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Word8 -> ByteString -> SigSubPacketPayload
UserDefinedSigSub Word8
pt ByteString
payload)
      | Bool
otherwise = do
        ByteString
payload <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        SigSubPacket -> Get SigSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (SigSubPacket -> Get SigSubPacket)
-> SigSubPacket -> Get SigSubPacket
forall a b. (a -> b) -> a -> b
$ Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Word8 -> ByteString -> SigSubPacketPayload
OtherSigSub Word8
pt ByteString
payload)

putSigSubPacket :: SigSubPacket -> Put
putSigSubPacket :: SigSubPacket -> Put
putSigSubPacket (SigSubPacket crit :: Bool
crit (SigCreationTime et :: ThirtyTwoBitTimeStamp
et)) = do
  Word32 -> Put
putSubPacketLength 5
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 2
  Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitTimeStamp -> Word32)
-> ThirtyTwoBitTimeStamp
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitTimeStamp -> Word32
unThirtyTwoBitTimeStamp (ThirtyTwoBitTimeStamp -> Put) -> ThirtyTwoBitTimeStamp -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitTimeStamp
et
putSigSubPacket (SigSubPacket crit :: Bool
crit (SigExpirationTime et :: ThirtyTwoBitDuration
et)) = do
  Word32 -> Put
putSubPacketLength 5
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 3
  Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitDuration -> Word32) -> ThirtyTwoBitDuration -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitDuration -> Word32
unThirtyTwoBitDuration (ThirtyTwoBitDuration -> Put) -> ThirtyTwoBitDuration -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitDuration
et
putSigSubPacket (SigSubPacket crit :: Bool
crit (ExportableCertification e :: Bool
e)) = do
  Word32 -> Put
putSubPacketLength 2
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 4
  Bool -> Put
forall t. Binary t => t -> Put
put Bool
e
putSigSubPacket (SigSubPacket crit :: Bool
crit (TrustSignature tl :: Word8
tl ta :: Word8
ta)) = do
  Word32 -> Put
putSubPacketLength 3
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 5
  Word8 -> Put
forall t. Binary t => t -> Put
put Word8
tl
  Word8 -> Put
forall t. Binary t => t -> Put
put Word8
ta
putSigSubPacket (SigSubPacket crit :: Bool
crit (RegularExpression apdre :: ByteString
apdre)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (2 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
apdre)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 6
  ByteString -> Put
putLazyByteString ByteString
apdre
  Word8 -> Put
putWord8 0
putSigSubPacket (SigSubPacket crit :: Bool
crit (Revocable r :: Bool
r)) = do
  Word32 -> Put
putSubPacketLength 2
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 7
  Bool -> Put
forall t. Binary t => t -> Put
put Bool
r
putSigSubPacket (SigSubPacket crit :: Bool
crit (KeyExpirationTime et :: ThirtyTwoBitDuration
et)) = do
  Word32 -> Put
putSubPacketLength 5
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 9
  Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitDuration -> Word32) -> ThirtyTwoBitDuration -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitDuration -> Word32
unThirtyTwoBitDuration (ThirtyTwoBitDuration -> Put) -> ThirtyTwoBitDuration -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitDuration
et
putSigSubPacket (SigSubPacket crit :: Bool
crit (PreferredSymmetricAlgorithms ess :: [SymmetricAlgorithm]
ess)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (Int -> Word32) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ (1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [SymmetricAlgorithm] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SymmetricAlgorithm]
ess)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 11
  (SymmetricAlgorithm -> Put) -> [SymmetricAlgorithm] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ SymmetricAlgorithm -> Put
forall t. Binary t => t -> Put
put [SymmetricAlgorithm]
ess
putSigSubPacket (SigSubPacket crit :: Bool
crit (RevocationKey rclass :: Set RevocationClass
rclass algid :: PubKeyAlgorithm
algid fp :: TwentyOctetFingerprint
fp)) = do
  Word32 -> Put
putSubPacketLength 23
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 12
  ByteString -> Put
putLazyByteString (ByteString -> Put)
-> (Set RevocationClass -> ByteString)
-> Set RevocationClass
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Set RevocationClass -> ByteString
forall a b. (Integral a, FutureFlag b) => a -> Set b -> ByteString
ffSetToFixedLengthBS (1 :: Int) (Set RevocationClass -> Put) -> Set RevocationClass -> Put
forall a b. (a -> b) -> a -> b
$
    RevocationClass -> Set RevocationClass -> Set RevocationClass
forall a. Ord a => a -> Set a -> Set a
Set.insert (Word8 -> RevocationClass
RClOther 0) Set RevocationClass
rclass
  PubKeyAlgorithm -> Put
forall t. Binary t => t -> Put
put PubKeyAlgorithm
algid
  ByteString -> Put
putLazyByteString (TwentyOctetFingerprint -> ByteString
unTOF TwentyOctetFingerprint
fp) -- 20 octets
putSigSubPacket (SigSubPacket crit :: Bool
crit (Issuer keyid :: EightOctetKeyId
keyid)) = do
  Word32 -> Put
putSubPacketLength 9
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 16
  ByteString -> Put
putLazyByteString (EightOctetKeyId -> ByteString
unEOKI EightOctetKeyId
keyid) -- 8 octets
putSigSubPacket (SigSubPacket crit :: Bool
crit (NotationData nfs :: Set NotationFlag
nfs (NotationName nn :: ByteString
nn) (NotationValue nv :: ByteString
nv))) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (9 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
nn ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
nv)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 20
  ByteString -> Put
putLazyByteString (ByteString -> Put)
-> (Set NotationFlag -> ByteString) -> Set NotationFlag -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Set NotationFlag -> ByteString
forall a b. (Integral a, FutureFlag b) => a -> Set b -> ByteString
ffSetToFixedLengthBS (4 :: Int) (Set NotationFlag -> Put) -> Set NotationFlag -> Put
forall a b. (a -> b) -> a -> b
$ Set NotationFlag
nfs
  Word16 -> Put
putWord16be (Word16 -> Put) -> (ByteString -> Word16) -> ByteString -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Word16)
-> (ByteString -> ByteOffset) -> ByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteOffset
BL.length (ByteString -> Put) -> ByteString -> Put
forall a b. (a -> b) -> a -> b
$ ByteString
nn
  Word16 -> Put
putWord16be (Word16 -> Put) -> (ByteString -> Word16) -> ByteString -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Word16)
-> (ByteString -> ByteOffset) -> ByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteOffset
BL.length (ByteString -> Put) -> ByteString -> Put
forall a b. (a -> b) -> a -> b
$ ByteString
nv
  ByteString -> Put
putLazyByteString ByteString
nn
  ByteString -> Put
putLazyByteString ByteString
nv
putSigSubPacket (SigSubPacket crit :: Bool
crit (PreferredHashAlgorithms ehs :: [HashAlgorithm]
ehs)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (Int -> Word32) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ (1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [HashAlgorithm] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [HashAlgorithm]
ehs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 21
  (HashAlgorithm -> Put) -> [HashAlgorithm] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ HashAlgorithm -> Put
forall t. Binary t => t -> Put
put [HashAlgorithm]
ehs
putSigSubPacket (SigSubPacket crit :: Bool
crit (PreferredCompressionAlgorithms ecs :: [CompressionAlgorithm]
ecs)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (Int -> Word32) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ (1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [CompressionAlgorithm] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CompressionAlgorithm]
ecs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 22
  (CompressionAlgorithm -> Put) -> [CompressionAlgorithm] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CompressionAlgorithm -> Put
forall t. Binary t => t -> Put
put [CompressionAlgorithm]
ecs
putSigSubPacket (SigSubPacket crit :: Bool
crit (KeyServerPreferences ksps :: Set KSPFlag
ksps)) = do
  let kbs :: ByteString
kbs = Set KSPFlag -> ByteString
forall a. FutureFlag a => Set a -> ByteString
ffSetToBS Set KSPFlag
ksps
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (1 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
kbs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 23
  ByteString -> Put
putLazyByteString ByteString
kbs
putSigSubPacket (SigSubPacket crit :: Bool
crit (PreferredKeyServer ks :: ByteString
ks)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (1 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
ks)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 24
  ByteString -> Put
putLazyByteString ByteString
ks
putSigSubPacket (SigSubPacket crit :: Bool
crit (PrimaryUserId primacy :: Bool
primacy)) = do
  Word32 -> Put
putSubPacketLength 2
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 25
  Bool -> Put
forall t. Binary t => t -> Put
put Bool
primacy
putSigSubPacket (SigSubPacket crit :: Bool
crit (PolicyURL (URL uri :: URI
uri))) = do
  let bs :: ByteString
bs = Text -> ByteString
encodeUtf8 (String -> Text
T.pack ((String -> String) -> URI -> String -> String
uriToString String -> String
forall a. a -> a
id URI
uri ""))
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (Int -> Word32) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ (1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
B.length ByteString
bs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 26
  ByteString -> Put
putByteString ByteString
bs
putSigSubPacket (SigSubPacket crit :: Bool
crit (KeyFlags kfs :: Set KeyFlag
kfs)) = do
  let kbs :: ByteString
kbs = Set KeyFlag -> ByteString
forall a. FutureFlag a => Set a -> ByteString
ffSetToBS Set KeyFlag
kfs
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (1 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
kbs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 27
  ByteString -> Put
putLazyByteString ByteString
kbs
putSigSubPacket (SigSubPacket crit :: Bool
crit (SignersUserId userid :: Text
userid)) = do
  let bs :: ByteString
bs = Text -> ByteString
encodeUtf8 Text
userid
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (Int -> Word32) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ (1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
B.length ByteString
bs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 28
  ByteString -> Put
putByteString ByteString
bs
putSigSubPacket (SigSubPacket crit :: Bool
crit (ReasonForRevocation rcode :: RevocationCode
rcode rreason :: Text
rreason)) = do
  let reasonbs :: ByteString
reasonbs = Text -> ByteString
encodeUtf8 Text
rreason
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (Int -> Word32) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ (2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
B.length ByteString
reasonbs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 29
  Word8 -> Put
putWord8 (Word8 -> Put)
-> (RevocationCode -> Word8) -> RevocationCode -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RevocationCode -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (RevocationCode -> Put) -> RevocationCode -> Put
forall a b. (a -> b) -> a -> b
$ RevocationCode
rcode
  ByteString -> Put
putByteString ByteString
reasonbs
putSigSubPacket (SigSubPacket crit :: Bool
crit (Features fs :: Set FeatureFlag
fs)) = do
  let fbs :: ByteString
fbs = Set FeatureFlag -> ByteString
forall a. FutureFlag a => Set a -> ByteString
ffSetToBS Set FeatureFlag
fs
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (1 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
fbs)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 30
  ByteString -> Put
putLazyByteString ByteString
fbs
putSigSubPacket (SigSubPacket crit :: Bool
crit (SignatureTarget pka :: PubKeyAlgorithm
pka ha :: HashAlgorithm
ha hash :: ByteString
hash)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (3 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
hash)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 31
  PubKeyAlgorithm -> Put
forall t. Binary t => t -> Put
put PubKeyAlgorithm
pka
  HashAlgorithm -> Put
forall t. Binary t => t -> Put
put HashAlgorithm
ha
  ByteString -> Put
putLazyByteString ByteString
hash
putSigSubPacket (SigSubPacket crit :: Bool
crit (EmbeddedSignature sp :: SignaturePayload
sp)) = do
  let spb :: ByteString
spb = Put -> ByteString
runPut (SignaturePayload -> Put
forall t. Binary t => t -> Put
put SignaturePayload
sp)
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (1 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
spb)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 32
  ByteString -> Put
putLazyByteString ByteString
spb
putSigSubPacket (SigSubPacket crit :: Bool
crit (IssuerFingerprint kv :: Word8
kv fp :: TwentyOctetFingerprint
fp)) = do
  let fpb :: ByteString
fpb = TwentyOctetFingerprint -> ByteString
unTOF TwentyOctetFingerprint
fp
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (2 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
fpb)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit 33
  Word8 -> Put
putWord8 Word8
kv
  ByteString -> Put
putLazyByteString ByteString
fpb
putSigSubPacket (SigSubPacket crit :: Bool
crit (UserDefinedSigSub ptype :: Word8
ptype payload :: ByteString
payload)) =
  SigSubPacket -> Put
putSigSubPacket (Bool -> SigSubPacketPayload -> SigSubPacket
SigSubPacket Bool
crit (Word8 -> ByteString -> SigSubPacketPayload
OtherSigSub Word8
ptype ByteString
payload))
putSigSubPacket (SigSubPacket crit :: Bool
crit (OtherSigSub ptype :: Word8
ptype payload :: ByteString
payload)) = do
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteOffset -> Word32) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ (1 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
payload)
  Bool -> Word8 -> Put
putSigSubPacketType Bool
crit Word8
ptype
  ByteString -> Put
putLazyByteString ByteString
payload

getSubPacketLength :: Get Word32
getSubPacketLength :: Get Word32
getSubPacketLength = Word8 -> Get Word32
forall a. Integral a => Word8 -> Get a
getSubPacketLength' (Word8 -> Get Word32) -> Get Word8 -> Get Word32
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Get Word8
getWord8
  where
    getSubPacketLength' :: Integral a => Word8 -> Get a
    getSubPacketLength' :: Word8 -> Get a
getSubPacketLength' f :: Word8
f
      | Word8
f Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< 192 = a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Get a) -> (Word8 -> a) -> Word8 -> Get a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Get a) -> Word8 -> Get a
forall a b. (a -> b) -> a -> b
$ Word8
f
      | Word8
f Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< 224 = do
        Word8
secondOctet <- Get Word8
getWord8
        a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Get a) -> (Int -> a) -> Int -> Get a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Get a) -> Int -> Get a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
f Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- 192) :: Int) 8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+
          (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
secondOctet :: Int) Int -> Int -> Int
forall a. Num a => a -> a -> a
+
          192
      | Word8
f Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 255 = do
        Word32
len <- Get Word32
getWord32be
        a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Get a) -> (Word32 -> a) -> Word32 -> Get a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> Get a) -> Word32 -> Get a
forall a b. (a -> b) -> a -> b
$ Word32
len
      | Bool
otherwise = String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Partial body length invalid."

putSubPacketLength :: Word32 -> Put
putSubPacketLength :: Word32 -> Put
putSubPacketLength l :: Word32
l
  | Word32
l Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< 192 = Word8 -> Put
putWord8 (Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
l)
  | Word32
l Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< 8384 =
    Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
l Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- 192) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 8) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 192 :: Int)) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Word8 -> Put
putWord8 (Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
l Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- 192) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0xff)
  | Word32
l Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= 0xffffffff = Word8 -> Put
putWord8 255 Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Word32 -> Put
putWord32be (Word32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
l)
  | Bool
otherwise = String -> Put
forall a. HasCallStack => String -> a
error ("too big (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Word32 -> String
forall a. Show a => a -> String
show Word32
l String -> String -> String
forall a. [a] -> [a] -> [a]
++ ")")

getSigSubPacketType :: Get (Bool, Word8)
getSigSubPacketType :: Get (Bool, Word8)
getSigSubPacketType = do
  Word8
x <- Get Word8
getWord8
  (Bool, Word8) -> Get (Bool, Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return
    (if Word8
x Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 128 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 128
       then (Bool
True, Word8
x Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 127)
       else (Bool
False, Word8
x))

putSigSubPacketType :: Bool -> Word8 -> Put
putSigSubPacketType :: Bool -> Word8 -> Put
putSigSubPacketType False sst :: Word8
sst = Word8 -> Put
putWord8 Word8
sst
putSigSubPacketType True sst :: Word8
sst = Word8 -> Put
putWord8 (Word8
sst Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 0x80)

bsToFFSet :: FutureFlag a => ByteString -> Set a
bsToFFSet :: ByteString -> Set a
bsToFFSet bs :: ByteString
bs =
  [a] -> Set a
forall a. Eq a => [a] -> Set a
Set.fromAscList ([a] -> Set a) -> ((Int, [[a]]) -> [a]) -> (Int, [[a]]) -> Set a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[a]] -> [a]) -> ((Int, [[a]]) -> [[a]]) -> (Int, [[a]]) -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, [[a]]) -> [[a]]
forall a b. (a, b) -> b
snd ((Int, [[a]]) -> Set a) -> (Int, [[a]]) -> Set a
forall a b. (a -> b) -> a -> b
$
  (Int -> Word8 -> (Int, [a])) -> Int -> [Word8] -> (Int, [[a]])
forall (t :: * -> *) a b c.
Traversable t =>
(a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL
    (\acc :: Int
acc y :: Word8
y -> (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 8, (Int -> [a]) -> [Int] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int -> Word8 -> Int -> [a]
forall a a. (Bits a, Num a, FutureFlag a) => Int -> a -> Int -> [a]
shifty Int
acc Word8
y) [0 .. 7]))
    0
    (ByteString -> [Word8]
BL.unpack ByteString
bs)
  where
    shifty :: Int -> a -> Int -> [a]
shifty acc :: Int
acc y :: a
y x :: Int
x = [Int -> a
forall a. FutureFlag a => Int -> a
toFFlag (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
x) | a
y a -> a -> a
forall a. Bits a => a -> a -> a
.&. a -> Int -> a
forall a. Bits a => a -> Int -> a
shiftR 128 Int
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Int -> a
forall a. Bits a => a -> Int -> a
shiftR 128 Int
x]

ffSetToFixedLengthBS :: (Integral a, FutureFlag b) => a -> Set b -> ByteString
ffSetToFixedLengthBS :: a -> Set b -> ByteString
ffSetToFixedLengthBS len :: a
len ffs :: Set b
ffs =
  ByteOffset -> ByteString -> ByteString
BL.take
    (a -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
len)
    (ByteString -> ByteString -> ByteString
BL.append (Set b -> ByteString
forall a. FutureFlag a => Set a -> ByteString
ffSetToBS Set b
ffs) ([Word8] -> ByteString
BL.pack (Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate 5 0)))

ffSetToBS :: FutureFlag a => Set a -> ByteString
ffSetToBS :: Set a -> ByteString
ffSetToBS = [Word8] -> ByteString
BL.pack ([Word8] -> ByteString)
-> (Set a -> [Word8]) -> Set a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set a -> [Word8]
forall a. FutureFlag a => Set a -> [Word8]
ffSetToBS'
  where
    ffSetToBS' :: FutureFlag a => Set a -> [Word8]
    ffSetToBS' :: Set a -> [Word8]
ffSetToBS' ks :: Set a
ks
      | Set a -> Bool
forall a. Set a -> Bool
Set.null Set a
ks = [] -- FIXME: should this be [0]?
      | Bool
otherwise =
        (Int -> Word8) -> [Int] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map
          (((Word8 -> Word8 -> Word8) -> Word8 -> [Word8] -> Word8
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
(.|.) 0 ([Word8] -> Word8) -> (Set a -> [Word8]) -> Set a -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Word8) -> [a] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
shiftR 128 (Int -> Word8) -> (a -> Int) -> a -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int) -> Int -> Int -> Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Int -> Int
forall a. Integral a => a -> a -> a
mod 8 (Int -> Int) -> (a -> Int) -> a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. FutureFlag a => a -> Int
fromFFlag) ([a] -> [Word8]) -> (Set a -> [a]) -> Set a -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            Set a -> [a]
forall a. Set a -> [a]
Set.toAscList) (Set a -> Word8) -> (Int -> Set a) -> Int -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
           (\x :: Int
x -> (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (\y :: a
y -> a -> Int
forall a. FutureFlag a => a -> Int
fromFFlag a
y Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 8 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
x) Set a
ks))
          [0 .. a -> Int
forall a. FutureFlag a => a -> Int
fromFFlag (Set a -> a
forall a. Set a -> a
Set.findMax Set a
ks) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 8]

fromS2K :: S2K -> ByteString
fromS2K :: S2K -> ByteString
fromS2K (Simple hashalgo :: HashAlgorithm
hashalgo) = [Word8] -> ByteString
BL.pack [0, Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ HashAlgorithm
hashalgo]
fromS2K (Salted hashalgo :: HashAlgorithm
hashalgo salt :: Salt
salt)
  | ByteString -> Int
B.length (Salt -> ByteString
unSalt Salt
salt) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 8 =
    [Word8] -> ByteString
BL.pack [1, Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ HashAlgorithm
hashalgo] ByteString -> ByteString -> ByteString
`BL.append`
    (ByteString -> ByteString
BL.fromStrict (ByteString -> ByteString)
-> (Salt -> ByteString) -> Salt -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Salt -> ByteString
unSalt) Salt
salt
  | Bool
otherwise = String -> ByteString
forall a. HasCallStack => String -> a
error "Confusing salt size"
fromS2K (IteratedSalted hashalgo :: HashAlgorithm
hashalgo salt :: Salt
salt count :: IterationCount
count)
  | ByteString -> Int
B.length (Salt -> ByteString
unSalt Salt
salt) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 8 =
    [Word8] -> ByteString
BL.pack [3, Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ HashAlgorithm
hashalgo] ByteString -> ByteString -> ByteString
`BL.append`
    (ByteString -> ByteString
BL.fromStrict (ByteString -> ByteString)
-> (Salt -> ByteString) -> Salt -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Salt -> ByteString
unSalt) Salt
salt ByteString -> Word8 -> ByteString
`BL.snoc`
    IterationCount -> Word8
encodeIterationCount IterationCount
count
  | Bool
otherwise = String -> ByteString
forall a. HasCallStack => String -> a
error "Confusing salt size"
fromS2K (OtherS2K _ bs :: ByteString
bs) = ByteString
bs

getPacketLength :: Get Integer
getPacketLength :: Get Integer
getPacketLength = do
  Word8
firstOctet <- Get Word8
getWord8
  Word8 -> Get Integer
forall a. Integral a => Word8 -> Get a
getPacketLength' Word8
firstOctet
  where
    getPacketLength' :: Integral a => Word8 -> Get a
    getPacketLength' :: Word8 -> Get a
getPacketLength' f :: Word8
f
      | Word8
f Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< 192 = a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Get a) -> (Word8 -> a) -> Word8 -> Get a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Get a) -> Word8 -> Get a
forall a b. (a -> b) -> a -> b
$ Word8
f
      | Word8
f Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< 224 = do
        Word8
secondOctet <- Get Word8
getWord8
        a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Get a) -> (Int -> a) -> Int -> Get a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Get a) -> Int -> Get a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
f Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- 192) :: Int) 8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+
          (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
secondOctet :: Int) Int -> Int -> Int
forall a. Num a => a -> a -> a
+
          192
      | Word8
f Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 255 = do
        Word32
len <- Get Word32
getWord32be
        a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Get a) -> (Word32 -> a) -> Word32 -> Get a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> Get a) -> Word32 -> Get a
forall a b. (a -> b) -> a -> b
$ Word32
len
      | Bool
otherwise = String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Partial body length support missing." --FIXME

putPacketLength :: Integer -> Put
putPacketLength :: Integer -> Put
putPacketLength l :: Integer
l
  | Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 192 = Word8 -> Put
putWord8 (Integer -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
l)
  | Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 8384 =
    Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer
l Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 192) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 8) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 192 :: Int)) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Word8 -> Put
putWord8 (Integer -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer
l Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 192) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0xff)
  | Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0x100000000 = Word8 -> Put
putWord8 255 Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Word32 -> Put
putWord32be (Integer -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
l)
  | Bool
otherwise = String -> Put
forall a. HasCallStack => String -> a
error "partial body length support needed" -- FIXME

getS2K :: Get S2K
getS2K :: Get S2K
getS2K = Word8 -> Get S2K
getS2K' (Word8 -> Get S2K) -> Get Word8 -> Get S2K
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Get Word8
getWord8
  where
    getS2K' :: Word8 -> Get S2K
    getS2K' :: Word8 -> Get S2K
getS2K' t :: Word8
t
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = do
        Word8
ha <- Get Word8
getWord8
        S2K -> Get S2K
forall (m :: * -> *) a. Monad m => a -> m a
return (S2K -> Get S2K) -> S2K -> Get S2K
forall a b. (a -> b) -> a -> b
$ HashAlgorithm -> S2K
Simple (Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ha)
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = do
        Word8
ha <- Get Word8
getWord8
        ByteString
salt <- Int -> Get ByteString
getByteString 8
        S2K -> Get S2K
forall (m :: * -> *) a. Monad m => a -> m a
return (S2K -> Get S2K) -> S2K -> Get S2K
forall a b. (a -> b) -> a -> b
$ HashAlgorithm -> Salt -> S2K
Salted (Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ha) (ByteString -> Salt
Salt ByteString
salt)
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 3 = do
        Word8
ha <- Get Word8
getWord8
        ByteString
salt <- Int -> Get ByteString
getByteString 8
        Word8
count <- Get Word8
getWord8
        S2K -> Get S2K
forall (m :: * -> *) a. Monad m => a -> m a
return (S2K -> Get S2K) -> S2K -> Get S2K
forall a b. (a -> b) -> a -> b
$
          HashAlgorithm -> Salt -> IterationCount -> S2K
IteratedSalted (Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ha) (ByteString -> Salt
Salt ByteString
salt) (Word8 -> IterationCount
decodeIterationCount Word8
count)
      | Bool
otherwise = do
        ByteString
bs <- Get ByteString
getRemainingLazyByteString
        S2K -> Get S2K
forall (m :: * -> *) a. Monad m => a -> m a
return (S2K -> Get S2K) -> S2K -> Get S2K
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString -> S2K
OtherS2K Word8
t ByteString
bs

putS2K :: S2K -> Put
putS2K :: S2K -> Put
putS2K (Simple hashalgo :: HashAlgorithm
hashalgo) = String -> Put
forall a. HasCallStack => String -> a
error ("confused by simple" String -> String -> String
forall a. [a] -> [a] -> [a]
++ HashAlgorithm -> String
forall a. Show a => a -> String
show HashAlgorithm
hashalgo)
putS2K (Salted hashalgo :: HashAlgorithm
hashalgo salt :: Salt
salt) =
  String -> Put
forall a. HasCallStack => String -> a
error ("confused by salted" String -> String -> String
forall a. [a] -> [a] -> [a]
++ HashAlgorithm -> String
forall a. Show a => a -> String
show HashAlgorithm
hashalgo String -> String -> String
forall a. [a] -> [a] -> [a]
++ " by " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Salt -> String
forall a. Show a => a -> String
show Salt
salt)
putS2K (IteratedSalted ha :: HashAlgorithm
ha salt :: Salt
salt count :: IterationCount
count) = do
  Word8 -> Put
putWord8 3
  HashAlgorithm -> Put
forall t. Binary t => t -> Put
put HashAlgorithm
ha
  ByteString -> Put
putByteString (Salt -> ByteString
unSalt Salt
salt)
  Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ IterationCount -> Word8
encodeIterationCount IterationCount
count
putS2K (OtherS2K t :: Word8
t bs :: ByteString
bs) = Word8 -> Put
putWord8 Word8
t Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putLazyByteString ByteString
bs

getPacketTypeAndPayload :: Get (Word8, ByteString)
getPacketTypeAndPayload :: Get (Word8, ByteString)
getPacketTypeAndPayload = do
  Word8
tag <- Get Word8
getWord8
  Bool -> Get ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
tag 7)
  case Word8
tag Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x40 of
    0x00 -> do
      let t :: Word8
t = Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
shiftR (Word8
tag Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x3c) 2
      case Word8
tag Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x03 of
        0 -> do
          Word8
len <- Get Word8
getWord8
          ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString (Word8 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
len)
          (Word8, ByteString) -> Get (Word8, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8
t, ByteString
bs)
        1 -> do
          Word16
len <- Get Word16
getWord16be
          ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString (Word16 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
len)
          (Word8, ByteString) -> Get (Word8, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8
t, ByteString
bs)
        2 -> do
          Word32
len <- Get Word32
getWord32be
          ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString (Word32 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
len)
          (Word8, ByteString) -> Get (Word8, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8
t, ByteString
bs)
        3 -> do
          ByteString
bs <- Get ByteString
getRemainingLazyByteString
          (Word8, ByteString) -> Get (Word8, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8
t, ByteString
bs)
        _ -> String -> Get (Word8, ByteString)
forall a. HasCallStack => String -> a
error "This should never happen (getPacketTypeAndPayload/0x00)."
    0x40 -> do
      ByteOffset
len <- (Integer -> ByteOffset) -> Get Integer -> Get ByteOffset
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Get Integer
getPacketLength
      ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
      (Word8, ByteString) -> Get (Word8, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8
tag Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x3f, ByteString
bs)
    _ -> String -> Get (Word8, ByteString)
forall a. HasCallStack => String -> a
error "This should never happen (getPacketTypeAndPayload/???)."

getPkt :: Get Pkt
getPkt :: Get Pkt
getPkt = do
  (t :: Word8
t, pl :: ByteString
pl) <- Get (Word8, ByteString)
getPacketTypeAndPayload
  case Get Pkt
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Word8 -> ByteOffset -> Get Pkt
getPkt' Word8
t (ByteString -> ByteOffset
BL.length ByteString
pl)) ByteString
pl of
    Left (_, _, e :: String
e) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$! String -> Word8 -> ByteString -> Pkt
BrokenPacketPkt String
e Word8
t ByteString
pl
    Right (_, _, p :: Pkt
p) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return Pkt
p
  where
    getPkt' :: Word8 -> ByteOffset -> Get Pkt
    getPkt' :: Word8 -> ByteOffset -> Get Pkt
getPkt' t :: Word8
t len :: ByteOffset
len
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = do
        Word8
pv <- Get Word8
getWord8
        ByteString
eokeyid <- ByteOffset -> Get ByteString
getLazyByteString 8
        Word8
pka <- Get Word8
getWord8
        ByteString
mpib <- Get ByteString
getRemainingLazyByteString
        case Get [MPI]
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, [MPI])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get MPI -> Get [MPI]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some Get MPI
getMPI) ByteString
mpib of
          Left (_, _, e :: String
e) -> String -> Get Pkt
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("PKESK MPIs " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
          Right (_, _, sk :: [MPI]
sk) ->
            Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$
            Word8 -> EightOctetKeyId -> PubKeyAlgorithm -> NonEmpty MPI -> Pkt
PKESKPkt Word8
pv (ByteString -> EightOctetKeyId
EightOctetKeyId ByteString
eokeyid) (Word8 -> PubKeyAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
pka) ([MPI] -> NonEmpty MPI
forall a. [a] -> NonEmpty a
NE.fromList [MPI]
sk)
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 2 = do
        ByteString
bs <- Get ByteString
getRemainingLazyByteString
        case Get SignaturePayload
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, SignaturePayload)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail Get SignaturePayload
forall t. Binary t => Get t
get ByteString
bs of
          Left (_, _, e :: String
e) -> String -> Get Pkt
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("signature packet " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
          Right (_, _, sp :: SignaturePayload
sp) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ SignaturePayload -> Pkt
SignaturePkt SignaturePayload
sp
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 3 = do
        Word8
pv <- Get Word8
getWord8
        Word8
symalgo <- Get Word8
getWord8
        S2K
s2k <- Get S2K
getS2K
        ByteString
esk <- Get ByteString
getRemainingLazyByteString
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$
          Word8 -> SymmetricAlgorithm -> S2K -> Maybe ByteString -> Pkt
SKESKPkt
            Word8
pv
            (Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
symalgo)
            S2K
s2k
            (if ByteString -> Bool
BL.null ByteString
esk
               then Maybe ByteString
forall a. Maybe a
Nothing
               else ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
esk)
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 4 = do
        Word8
pv <- Get Word8
getWord8
        Word8
sigtype <- Get Word8
getWord8
        Word8
ha <- Get Word8
getWord8
        Word8
pka <- Get Word8
getWord8
        ByteString
skeyid <- ByteOffset -> Get ByteString
getLazyByteString 8
        Word8
nested <- Get Word8
getWord8
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$
          Word8
-> SigType
-> HashAlgorithm
-> PubKeyAlgorithm
-> EightOctetKeyId
-> Bool
-> Pkt
OnePassSignaturePkt
            Word8
pv
            (Word8 -> SigType
forall a. FutureVal a => Word8 -> a
toFVal Word8
sigtype)
            (Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ha)
            (Word8 -> PubKeyAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
pka)
            (ByteString -> EightOctetKeyId
EightOctetKeyId ByteString
skeyid)
            (Word8
nested Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0)
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 5 = do
        ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        let ps :: Either
  (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
ps =
              (Get Pkt
 -> ByteString
 -> Either
      (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt))
-> ByteString
-> Get Pkt
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Get Pkt
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail ByteString
bs (Get Pkt
 -> Either
      (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt))
-> Get Pkt
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a b. (a -> b) -> a -> b
$ do
                PKPayload
pkp <- Get PKPayload
getPKPayload
                SKAddendum
ska <- PKPayload -> Get SKAddendum
getSKAddendum PKPayload
pkp
                Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ PKPayload -> SKAddendum -> Pkt
SecretKeyPkt PKPayload
pkp SKAddendum
ska
        case Either
  (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
ps of
          Left (_, _, err :: String
err) -> String -> Get Pkt
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("secret key " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)
          Right (_, _, key :: Pkt
key) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return Pkt
key
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 6 = do
        PKPayload
pkp <- Get PKPayload
getPKPayload
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ PKPayload -> Pkt
PublicKeyPkt PKPayload
pkp
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 7 = do
        ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        let ps :: Either
  (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
ps =
              (Get Pkt
 -> ByteString
 -> Either
      (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt))
-> ByteString
-> Get Pkt
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Get Pkt
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail ByteString
bs (Get Pkt
 -> Either
      (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt))
-> Get Pkt
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a b. (a -> b) -> a -> b
$ do
                PKPayload
pkp <- Get PKPayload
getPKPayload
                SKAddendum
ska <- PKPayload -> Get SKAddendum
getSKAddendum PKPayload
pkp
                Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ PKPayload -> SKAddendum -> Pkt
SecretSubkeyPkt PKPayload
pkp SKAddendum
ska
        case Either
  (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
ps of
          Left (_, _, err :: String
err) -> String -> Get Pkt
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("secret subkey " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)
          Right (_, _, key :: Pkt
key) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return Pkt
key
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 8 = do
        Word8
ca <- Get Word8
getWord8
        ByteString
cdata <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
len ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ CompressionAlgorithm -> ByteString -> Pkt
CompressedDataPkt (Word8 -> CompressionAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ca) ByteString
cdata
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 9 = do
        ByteString
sdata <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ ByteString -> Pkt
SymEncDataPkt ByteString
sdata
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 10 = do
        ByteString
marker <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ ByteString -> Pkt
MarkerPkt ByteString
marker
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 11 = do
        Word8
dt <- Get Word8
getWord8
        Word8
flen <- Get Word8
getWord8
        ByteString
fn <- ByteOffset -> Get ByteString
getLazyByteString (Word8 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
flen)
        ThirtyTwoBitTimeStamp
ts <- (Word32 -> ThirtyTwoBitTimeStamp)
-> Get Word32 -> Get ThirtyTwoBitTimeStamp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ThirtyTwoBitTimeStamp
ThirtyTwoBitTimeStamp Get Word32
getWord32be
        ByteString
ldata <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
len ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- (6 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Word8 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
flen))
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ DataType
-> ByteString -> ThirtyTwoBitTimeStamp -> ByteString -> Pkt
LiteralDataPkt (Word8 -> DataType
forall a. FutureVal a => Word8 -> a
toFVal Word8
dt) ByteString
fn ThirtyTwoBitTimeStamp
ts ByteString
ldata
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 12 = do
        ByteString
tdata <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ ByteString -> Pkt
TrustPkt ByteString
tdata
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 13 = do
        ByteString
udata <- Int -> Get ByteString
getByteString (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteOffset
len)
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> (ByteString -> Pkt) -> ByteString -> Get Pkt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Pkt
UserIdPkt (Text -> Pkt) -> (ByteString -> Text) -> ByteString -> Pkt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnDecodeError -> ByteString -> Text
decodeUtf8With OnDecodeError
lenientDecode (ByteString -> Get Pkt) -> ByteString -> Get Pkt
forall a b. (a -> b) -> a -> b
$ ByteString
udata
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 14 = do
        ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        let ps :: Either
  (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
ps =
              (Get Pkt
 -> ByteString
 -> Either
      (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt))
-> ByteString
-> Get Pkt
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Get Pkt
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail ByteString
bs (Get Pkt
 -> Either
      (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt))
-> Get Pkt
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
forall a b. (a -> b) -> a -> b
$ do
                PKPayload
pkp <- Get PKPayload
getPKPayload
                Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ PKPayload -> Pkt
PublicSubkeyPkt PKPayload
pkp
        case Either
  (ByteString, ByteOffset, String) (ByteString, ByteOffset, Pkt)
ps of
          Left (_, _, err :: String
err) -> String -> Get Pkt
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("public subkey " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)
          Right (_, _, key :: Pkt
key) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return Pkt
key
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 17 = do
        ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        case Get [UserAttrSubPacket]
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, [UserAttrSubPacket])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get UserAttrSubPacket -> Get [UserAttrSubPacket]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Get UserAttrSubPacket
getUserAttrSubPacket) ByteString
bs of
          Left (_, _, err :: String
err) -> String -> Get Pkt
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("user attribute " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)
          Right (_, _, uas :: [UserAttrSubPacket]
uas) -> Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ [UserAttrSubPacket] -> Pkt
UserAttributePkt [UserAttrSubPacket]
uas
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 18 = do
        Word8
pv <- Get Word8
getWord8 -- should be 1
        ByteString
b <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
len ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString -> Pkt
SymEncIntegrityProtectedDataPkt Word8
pv ByteString
b
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 19 = do
        ByteString
hash <- ByteOffset -> Get ByteString
getLazyByteString 20
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ ByteString -> Pkt
ModificationDetectionCodePkt ByteString
hash
      | Bool
otherwise = do
        ByteString
payload <- ByteOffset -> Get ByteString
getLazyByteString ByteOffset
len
        Pkt -> Get Pkt
forall (m :: * -> *) a. Monad m => a -> m a
return (Pkt -> Get Pkt) -> Pkt -> Get Pkt
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString -> Pkt
OtherPacketPkt Word8
t ByteString
payload

getUserAttrSubPacket :: Get UserAttrSubPacket
getUserAttrSubPacket :: Get UserAttrSubPacket
getUserAttrSubPacket = do
  ByteOffset
l <- (Word32 -> ByteOffset) -> Get Word32 -> Get ByteOffset
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Get Word32
getSubPacketLength
  Word8
t <- Get Word8
getWord8
  Word8 -> ByteOffset -> Get UserAttrSubPacket
getUserAttrSubPacket' Word8
t ByteOffset
l
  where
    getUserAttrSubPacket' :: Word8 -> ByteOffset -> Get UserAttrSubPacket
    getUserAttrSubPacket' :: Word8 -> ByteOffset -> Get UserAttrSubPacket
getUserAttrSubPacket' t :: Word8
t l :: ByteOffset
l
      | Word8
t Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = do
        Word16
_ <- Get Word16
getWord16le -- ihlen
        Word8
hver <- Get Word8
getWord8 -- should be 1
        Word8
iformat <- Get Word8
getWord8
        ByteString
nuls <- ByteOffset -> Get ByteString
getLazyByteString 12 -- should be NULs
        ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 17)
        if Word8
hver Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= 1 Bool -> Bool -> Bool
|| ByteString
nuls ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= [Word8] -> ByteString
BL.pack (Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate 12 0)
          then String -> Get UserAttrSubPacket
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Corrupt UAt subpacket"
          else UserAttrSubPacket -> Get UserAttrSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (UserAttrSubPacket -> Get UserAttrSubPacket)
-> UserAttrSubPacket -> Get UserAttrSubPacket
forall a b. (a -> b) -> a -> b
$ ImageHeader -> ByteString -> UserAttrSubPacket
ImageAttribute (ImageFormat -> ImageHeader
ImageHV1 (Word8 -> ImageFormat
forall a. FutureVal a => Word8 -> a
toFVal Word8
iformat)) ByteString
bs
      | Bool
otherwise = do
        ByteString
bs <- ByteOffset -> Get ByteString
getLazyByteString (ByteOffset
l ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- 1)
        UserAttrSubPacket -> Get UserAttrSubPacket
forall (m :: * -> *) a. Monad m => a -> m a
return (UserAttrSubPacket -> Get UserAttrSubPacket)
-> UserAttrSubPacket -> Get UserAttrSubPacket
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString -> UserAttrSubPacket
OtherUASub Word8
t ByteString
bs

putUserAttrSubPacket :: UserAttrSubPacket -> Put
putUserAttrSubPacket :: UserAttrSubPacket -> Put
putUserAttrSubPacket ua :: UserAttrSubPacket
ua = do
  let sp :: ByteString
sp = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ UserAttrSubPacket -> Put
putUserAttrSubPacket' UserAttrSubPacket
ua
  Word32 -> Put
putSubPacketLength (Word32 -> Put) -> (ByteString -> Word32) -> ByteString -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Word32)
-> (ByteString -> ByteOffset) -> ByteString -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteOffset
BL.length (ByteString -> Put) -> ByteString -> Put
forall a b. (a -> b) -> a -> b
$ ByteString
sp
  ByteString -> Put
putLazyByteString ByteString
sp
  where
    putUserAttrSubPacket' :: UserAttrSubPacket -> Put
putUserAttrSubPacket' (ImageAttribute (ImageHV1 iformat :: ImageFormat
iformat) idata :: ByteString
idata) = do
      Word8 -> Put
putWord8 1
      Word16 -> Put
putWord16le 16
      Word8 -> Put
putWord8 1
      Word8 -> Put
putWord8 (ImageFormat -> Word8
forall a. FutureVal a => a -> Word8
fromFVal ImageFormat
iformat)
      Int -> Put -> Put
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ 12 (Put -> Put) -> Put -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Put
putWord8 0
      ByteString -> Put
putLazyByteString ByteString
idata
    putUserAttrSubPacket' (OtherUASub t :: Word8
t bs :: ByteString
bs) = do
      Word8 -> Put
putWord8 Word8
t
      ByteString -> Put
putLazyByteString ByteString
bs

putPkt :: Pkt -> Put
putPkt :: Pkt -> Put
putPkt (PKESKPkt pv :: Word8
pv eokeyid :: EightOctetKeyId
eokeyid pka :: PubKeyAlgorithm
pka mpis :: NonEmpty MPI
mpis) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 1)
  let bsk :: ByteString
bsk = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ (MPI -> Put) -> NonEmpty MPI -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
F.mapM_ MPI -> Put
forall t. Binary t => t -> Put
put NonEmpty MPI
mpis
  Integer -> Put
putPacketLength (Integer -> Put) -> (ByteOffset -> Integer) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ 10 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
bsk
  Word8 -> Put
putWord8 Word8
pv -- must be 3
  ByteString -> Put
putLazyByteString (EightOctetKeyId -> ByteString
unEOKI EightOctetKeyId
eokeyid) -- must be 8 octets
  Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (PubKeyAlgorithm -> Word8) -> PubKeyAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubKeyAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (PubKeyAlgorithm -> Word8) -> PubKeyAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ PubKeyAlgorithm
pka
  ByteString -> Put
putLazyByteString ByteString
bsk
putPkt (SignaturePkt sp :: SignaturePayload
sp) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 2)
  let bs :: ByteString
bs = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ SignaturePayload -> Put
forall t. Binary t => t -> Put
put SignaturePayload
sp
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (SKESKPkt pv :: Word8
pv symalgo :: SymmetricAlgorithm
symalgo s2k :: S2K
s2k mesk :: Maybe ByteString
mesk) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 3)
  let bs2k :: ByteString
bs2k = S2K -> ByteString
fromS2K S2K
s2k
  let bsk :: ByteString
bsk = ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
BL.empty Maybe ByteString
mesk
  Integer -> Put
putPacketLength (Integer -> Put) -> (ByteOffset -> Integer) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ 2 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
bs2k ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ ByteString -> ByteOffset
BL.length ByteString
bsk
  Word8 -> Put
putWord8 Word8
pv -- should be 4
  Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (SymmetricAlgorithm -> Word8) -> SymmetricAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymmetricAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (SymmetricAlgorithm -> Word8) -> SymmetricAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ SymmetricAlgorithm
symalgo
  ByteString -> Put
putLazyByteString ByteString
bs2k
  ByteString -> Put
putLazyByteString ByteString
bsk
putPkt (OnePassSignaturePkt pv :: Word8
pv sigtype :: SigType
sigtype ha :: HashAlgorithm
ha pka :: PubKeyAlgorithm
pka skeyid :: EightOctetKeyId
skeyid nested :: Bool
nested) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 4)
  let bs :: ByteString
bs =
        Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ do
          Word8 -> Put
putWord8 Word8
pv -- should be 3
          Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8) -> (SigType -> Word8) -> SigType -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SigType -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (SigType -> Word8) -> SigType -> Word8
forall a b. (a -> b) -> a -> b
$ SigType
sigtype
          Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (HashAlgorithm -> Word8) -> HashAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ HashAlgorithm
ha
          Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (PubKeyAlgorithm -> Word8) -> PubKeyAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubKeyAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (PubKeyAlgorithm -> Word8) -> PubKeyAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ PubKeyAlgorithm
pka
          ByteString -> Put
putLazyByteString (EightOctetKeyId -> ByteString
unEOKI EightOctetKeyId
skeyid)
          Word8 -> Put
putWord8 (Word8 -> Put) -> (Bool -> Word8) -> Bool -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> (Bool -> Int) -> Bool -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Int
forall a. Enum a => a -> Int
fromEnum (Bool -> Put) -> Bool -> Put
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not Bool
nested -- FIXME: what do other values mean?
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (SecretKeyPkt pkp :: PKPayload
pkp ska :: SKAddendum
ska) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 5)
  let bs :: ByteString
bs = Put -> ByteString
runPut (PKPayload -> Put
putPKPayload PKPayload
pkp Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SKAddendum -> Put
putSKAddendum SKAddendum
ska)
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (PublicKeyPkt pkp :: PKPayload
pkp) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 6)
  let bs :: ByteString
bs = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ PKPayload -> Put
putPKPayload PKPayload
pkp
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (SecretSubkeyPkt pkp :: PKPayload
pkp ska :: SKAddendum
ska) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 7)
  let bs :: ByteString
bs = Put -> ByteString
runPut (PKPayload -> Put
putPKPayload PKPayload
pkp Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SKAddendum -> Put
putSKAddendum SKAddendum
ska)
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (CompressedDataPkt ca :: CompressionAlgorithm
ca cdata :: ByteString
cdata) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 8)
  let bs :: ByteString
bs =
        Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ do
          Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8)
-> (CompressionAlgorithm -> Word8) -> CompressionAlgorithm -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CompressionAlgorithm -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (CompressionAlgorithm -> Word8) -> CompressionAlgorithm -> Word8
forall a b. (a -> b) -> a -> b
$ CompressionAlgorithm
ca
          ByteString -> Put
putLazyByteString ByteString
cdata
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (SymEncDataPkt b :: ByteString
b) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 9)
  ByteString -> Put
putLengthThenPayload ByteString
b
putPkt (MarkerPkt b :: ByteString
b) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 10)
  ByteString -> Put
putLengthThenPayload ByteString
b
putPkt (LiteralDataPkt dt :: DataType
dt fn :: ByteString
fn ts :: ThirtyTwoBitTimeStamp
ts b :: ByteString
b) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 11)
  let bs :: ByteString
bs =
        Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ do
          Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ Word8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8) -> (DataType -> Word8) -> DataType -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataType -> Word8
forall a. FutureVal a => a -> Word8
fromFVal (DataType -> Word8) -> DataType -> Word8
forall a b. (a -> b) -> a -> b
$ DataType
dt
          Word8 -> Put
putWord8 (Word8 -> Put) -> Word8 -> Put
forall a b. (a -> b) -> a -> b
$ ByteOffset -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Word8)
-> (ByteString -> ByteOffset) -> ByteString -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteOffset
BL.length (ByteString -> Word8) -> ByteString -> Word8
forall a b. (a -> b) -> a -> b
$ ByteString
fn
          ByteString -> Put
putLazyByteString ByteString
fn
          Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitTimeStamp -> Word32)
-> ThirtyTwoBitTimeStamp
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitTimeStamp -> Word32
unThirtyTwoBitTimeStamp (ThirtyTwoBitTimeStamp -> Put) -> ThirtyTwoBitTimeStamp -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitTimeStamp
ts
          ByteString -> Put
putLazyByteString ByteString
b
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (TrustPkt b :: ByteString
b) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 12)
  ByteString -> Put
putLengthThenPayload ByteString
b
putPkt (UserIdPkt u :: Text
u) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 13)
  let bs :: ByteString
bs = Text -> ByteString
encodeUtf8 Text
u
  Integer -> Put
putPacketLength (Integer -> Put) -> (Int -> Integer) -> Int -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Put) -> Int -> Put
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
B.length ByteString
bs
  ByteString -> Put
putByteString ByteString
bs
putPkt (PublicSubkeyPkt pkp :: PKPayload
pkp) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 14)
  let bs :: ByteString
bs = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ PKPayload -> Put
putPKPayload PKPayload
pkp
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (UserAttributePkt us :: [UserAttrSubPacket]
us) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 17)
  let bs :: ByteString
bs = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ (UserAttrSubPacket -> Put) -> [UserAttrSubPacket] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ UserAttrSubPacket -> Put
forall t. Binary t => t -> Put
put [UserAttrSubPacket]
us
  ByteString -> Put
putLengthThenPayload ByteString
bs
putPkt (SymEncIntegrityProtectedDataPkt pv :: Word8
pv b :: ByteString
b) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 18)
  Integer -> Put
putPacketLength (Integer -> Put) -> (ByteOffset -> Integer) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteOffset
BL.length ByteString
b ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ 1
  Word8 -> Put
putWord8 Word8
pv -- should be 1
  ByteString -> Put
putLazyByteString ByteString
b
putPkt (ModificationDetectionCodePkt hash :: ByteString
hash) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 19)
  ByteString -> Put
putLengthThenPayload ByteString
hash
putPkt (OtherPacketPkt t :: Word8
t payload :: ByteString
payload) = do
  Word8 -> Put
putWord8 (0xc0 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
t) -- FIXME: restrict t
  ByteString -> Put
putLengthThenPayload ByteString
payload
putPkt (BrokenPacketPkt _ t :: Word8
t payload :: ByteString
payload) = Pkt -> Put
putPkt (Word8 -> ByteString -> Pkt
OtherPacketPkt Word8
t ByteString
payload)

putLengthThenPayload :: ByteString -> Put
putLengthThenPayload :: ByteString -> Put
putLengthThenPayload bs :: ByteString
bs = do
  Integer -> Put
putPacketLength (Integer -> Put) -> (ByteOffset -> Integer) -> ByteOffset -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Put) -> ByteOffset -> Put
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteOffset
BL.length ByteString
bs
  ByteString -> Put
putLazyByteString ByteString
bs

getMPI :: Get MPI
getMPI :: Get MPI
getMPI = do
  Word16
mpilen <- Get Word16
getWord16be
  ByteString
bs <- Int -> Get ByteString
getByteString (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16
mpilen Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
+ 7) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 8)
  MPI -> Get MPI
forall (m :: * -> *) a. Monad m => a -> m a
return (MPI -> Get MPI) -> MPI -> Get MPI
forall a b. (a -> b) -> a -> b
$ Integer -> MPI
MPI (ByteString -> Integer
forall ba. ByteArrayAccess ba => ba -> Integer
os2ip ByteString
bs)

getPubkey :: PubKeyAlgorithm -> Get PKey
getPubkey :: PubKeyAlgorithm -> Get PKey
getPubkey RSA = do
  MPI n :: Integer
n <- Get MPI
forall t. Binary t => Get t
get
  MPI e :: Integer
e <- Get MPI
forall t. Binary t => Get t
get
  PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey) -> PKey -> Get PKey
forall a b. (a -> b) -> a -> b
$
    RSA_PublicKey -> PKey
RSAPubKey
      (PublicKey -> RSA_PublicKey
RSA_PublicKey (Int -> Integer -> Integer -> PublicKey
R.PublicKey (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int) -> (Integer -> Int) -> Integer -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
B.length (ByteString -> Int) -> (Integer -> ByteString) -> Integer -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> ByteString
forall ba. ByteArray ba => Integer -> ba
i2osp (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer
n) Integer
n Integer
e))
getPubkey DeprecatedRSAEncryptOnly = PubKeyAlgorithm -> Get PKey
getPubkey PubKeyAlgorithm
RSA
getPubkey DeprecatedRSASignOnly = PubKeyAlgorithm -> Get PKey
getPubkey PubKeyAlgorithm
RSA
getPubkey DSA = do
  MPI p :: Integer
p <- Get MPI
forall t. Binary t => Get t
get
  MPI q :: Integer
q <- Get MPI
forall t. Binary t => Get t
get
  MPI g :: Integer
g <- Get MPI
forall t. Binary t => Get t
get
  MPI y :: Integer
y <- Get MPI
forall t. Binary t => Get t
get
  PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey) -> PKey -> Get PKey
forall a b. (a -> b) -> a -> b
$ DSA_PublicKey -> PKey
DSAPubKey (PublicKey -> DSA_PublicKey
DSA_PublicKey (Params -> Integer -> PublicKey
D.PublicKey (Integer -> Integer -> Integer -> Params
D.Params Integer
p Integer
g Integer
q) Integer
y))
getPubkey ElgamalEncryptOnly = PubKeyAlgorithm -> Get PKey
getPubkey PubKeyAlgorithm
ForbiddenElgamal
getPubkey ForbiddenElgamal = do
  MPI p :: Integer
p <- Get MPI
forall t. Binary t => Get t
get
  MPI g :: Integer
g <- Get MPI
forall t. Binary t => Get t
get
  MPI y :: Integer
y <- Get MPI
forall t. Binary t => Get t
get
  PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey) -> PKey -> Get PKey
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Integer -> PKey
ElGamalPubKey Integer
p Integer
g Integer
y
getPubkey ECDSA = do
  Word8
curvelength <- Get Word8
getWord8 -- FIXME: test for 0 or 0xFF as they are reserved
  ByteString
curveoid <- Int -> Get ByteString
getByteString (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
curvelength)
  MPI mpi :: Integer
mpi <- Get MPI
getMPI -- FIXME: check length against curve type?
  case ByteString -> Either String ECCCurve
curveoidBSToCurve ByteString
curveoid of
    Left e :: String
e -> String -> Get PKey
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
e
    Right Curve25519 -> PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey) -> PKey -> Get PKey
forall a b. (a -> b) -> a -> b
$ EdSigningCurve -> EPoint -> PKey
EdDSAPubKey EdSigningCurve
Ed25519 (Integer -> EPoint
EPoint Integer
mpi)
    Right curve :: ECCCurve
curve ->
      case ByteString -> Either String PublicPoint
bs2Point (Integer -> ByteString
forall ba. ByteArray ba => Integer -> ba
i2osp Integer
mpi) of
        Left e :: String
e -> String -> Get PKey
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
e
        Right point :: PublicPoint
point ->
          PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey)
-> (PublicPoint -> PKey) -> PublicPoint -> Get PKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ECDSA_PublicKey -> PKey
ECDSAPubKey (ECDSA_PublicKey -> PKey)
-> (PublicPoint -> ECDSA_PublicKey) -> PublicPoint -> PKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> ECDSA_PublicKey
ECDSA_PublicKey (PublicKey -> ECDSA_PublicKey)
-> (PublicPoint -> PublicKey) -> PublicPoint -> ECDSA_PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          Curve -> PublicPoint -> PublicKey
ECDSA.PublicKey (ECCCurve -> Curve
curve2Curve ECCCurve
curve) (PublicPoint -> Get PKey) -> PublicPoint -> Get PKey
forall a b. (a -> b) -> a -> b
$
          PublicPoint
point
getPubkey ECDH = do
  PKey
ed <- PubKeyAlgorithm -> Get PKey
getPubkey PubKeyAlgorithm
ECDSA -- could be an ECDSA or an EdDSA
  Word8
kdflen <- Get Word8
getWord8 -- FIXME: should be 3, test for 0 or 0xFF as they are reserved
  Word8
one <- Get Word8
getWord8 -- FIXME: should be 1
  HashAlgorithm
kdfHA <- Get HashAlgorithm
forall t. Binary t => Get t
get
  SymmetricAlgorithm
kdfSA <- Get SymmetricAlgorithm
forall t. Binary t => Get t
get
  PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey) -> PKey -> Get PKey
forall a b. (a -> b) -> a -> b
$ PKey -> HashAlgorithm -> SymmetricAlgorithm -> PKey
ECDHPubKey PKey
ed HashAlgorithm
kdfHA SymmetricAlgorithm
kdfSA
getPubkey EdDSA = do
  Word8
curvelength <- Get Word8
getWord8 -- FIXME: test for 0 or 0xFF as they are reserved
  ByteString
curveoid <- Int -> Get ByteString
getByteString (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
curvelength)
  MPI mpi :: Integer
mpi <- Get MPI
getMPI -- FIXME: check length against curve type?
  case ByteString -> Either String EdSigningCurve
curveoidBSToEdSigningCurve ByteString
curveoid of
    Left e :: String
e -> String -> Get PKey
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
e
    Right Ed25519 -> PKey -> Get PKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PKey -> Get PKey) -> (EPoint -> PKey) -> EPoint -> Get PKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EdSigningCurve -> EPoint -> PKey
EdDSAPubKey EdSigningCurve
Ed25519 (EPoint -> Get PKey) -> EPoint -> Get PKey
forall a b. (a -> b) -> a -> b
$ Integer -> EPoint
EPoint Integer
mpi
getPubkey _ = ByteString -> PKey
UnknownPKey (ByteString -> PKey) -> Get ByteString -> Get PKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
getRemainingLazyByteString

bs2Point :: B.ByteString -> Either String ECDSA.PublicPoint
bs2Point :: ByteString -> Either String PublicPoint
bs2Point bs :: ByteString
bs =
  let xy :: ByteString
xy = Int -> ByteString -> ByteString
B.drop 1 ByteString
bs
   in let l :: Int
l = ByteString -> Int
B.length ByteString
xy
       in if ByteString -> Word8
B.head ByteString
bs Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0x04
            then PublicPoint -> Either String PublicPoint
forall (m :: * -> *) a. Monad m => a -> m a
return
                   ((Integer -> Integer -> PublicPoint)
-> (Integer, Integer) -> PublicPoint
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry
                      Integer -> Integer -> PublicPoint
ECCT.Point
                      ((ByteString -> Integer)
-> (ByteString -> Integer)
-> (ByteString, ByteString)
-> (Integer, Integer)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ByteString -> Integer
forall ba. ByteArrayAccess ba => ba -> Integer
os2ip ByteString -> Integer
forall ba. ByteArrayAccess ba => ba -> Integer
os2ip (Int -> ByteString -> (ByteString, ByteString)
B.splitAt (Int -> Int -> Int
forall a. Integral a => a -> a -> a
div Int
l 2) ByteString
xy)))
            else String -> Either String PublicPoint
forall a b. a -> Either a b
Left (String -> Either String PublicPoint)
-> String -> Either String PublicPoint
forall a b. (a -> b) -> a -> b
$ "unknown type of point: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Word8] -> String
forall a. Show a => a -> String
show (ByteString -> [Word8]
B.unpack ByteString
bs)

putPubkey :: PKey -> Put
putPubkey :: PKey -> Put
putPubkey (UnknownPKey bs :: ByteString
bs) = ByteString -> Put
putLazyByteString ByteString
bs
putPubkey p :: PKey
p@(ECDSAPubKey (ECDSA_PublicKey (ECDSA.PublicKey curve :: Curve
curve _))) =
  let Right curveoidbs :: ByteString
curveoidbs = ECCCurve -> Either String ByteString
curveToCurveoidBS (Curve -> ECCCurve
curveFromCurve Curve
curve)
   in Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
curveoidbs)) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putByteString ByteString
curveoidbs Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      (MPI -> Put) -> [MPI] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MPI -> Put
forall t. Binary t => t -> Put
put (PKey -> [MPI]
pubkeyToMPIs PKey
p) -- FIXME: do not output length 0 or 0xff
putPubkey p :: PKey
p@(ECDHPubKey (ECDSAPubKey (ECDSA_PublicKey (ECDSA.PublicKey curve :: Curve
curve _))) kha :: HashAlgorithm
kha ksa :: SymmetricAlgorithm
ksa) =
  let Right curveoidbs :: ByteString
curveoidbs = ECCCurve -> Either String ByteString
curveToCurveoidBS (Curve -> ECCCurve
curveFromCurve Curve
curve)
   in Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
curveoidbs)) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putByteString ByteString
curveoidbs Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      (MPI -> Put) -> [MPI] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MPI -> Put
forall t. Binary t => t -> Put
put (PKey -> [MPI]
pubkeyToMPIs PKey
p) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      Word8 -> Put
putWord8 0x03 Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      Word8 -> Put
putWord8 0x01 Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      HashAlgorithm -> Put
forall t. Binary t => t -> Put
put HashAlgorithm
kha Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      SymmetricAlgorithm -> Put
forall t. Binary t => t -> Put
put SymmetricAlgorithm
ksa -- FIXME: do not output length 0 or 0xff
putPubkey p :: PKey
p@(ECDHPubKey (EdDSAPubKey curve :: EdSigningCurve
curve _) kha :: HashAlgorithm
kha ksa :: SymmetricAlgorithm
ksa) =
  let Right curveoidbs :: ByteString
curveoidbs = ECCCurve -> Either String ByteString
curveToCurveoidBS (EdSigningCurve -> ECCCurve
ed2ec EdSigningCurve
curve)
   in Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
curveoidbs)) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putByteString ByteString
curveoidbs Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      (MPI -> Put) -> [MPI] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MPI -> Put
forall t. Binary t => t -> Put
put (PKey -> [MPI]
pubkeyToMPIs PKey
p) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      Word8 -> Put
putWord8 0x03 Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      Word8 -> Put
putWord8 0x01 Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      HashAlgorithm -> Put
forall t. Binary t => t -> Put
put HashAlgorithm
kha Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      SymmetricAlgorithm -> Put
forall t. Binary t => t -> Put
put SymmetricAlgorithm
ksa -- FIXME: do not output length 0 or 0xff
  where
    ed2ec :: EdSigningCurve -> ECCCurve
ed2ec Ed25519 = ECCCurve
Curve25519
putPubkey p :: PKey
p@(EdDSAPubKey curve :: EdSigningCurve
curve _) =
  let Right curveoidbs :: ByteString
curveoidbs = EdSigningCurve -> Either String ByteString
edSigningCurveToCurveoidBS EdSigningCurve
curve
   in Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
curveoidbs)) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putByteString ByteString
curveoidbs Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
      (MPI -> Put) -> [MPI] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MPI -> Put
forall t. Binary t => t -> Put
put (PKey -> [MPI]
pubkeyToMPIs PKey
p) -- FIXME: do not output length 0 or 0xff
putPubkey p :: PKey
p = (MPI -> Put) -> [MPI] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MPI -> Put
forall t. Binary t => t -> Put
put (PKey -> [MPI]
pubkeyToMPIs PKey
p)

getSecretKey :: PKPayload -> Get SKey
getSecretKey :: PKPayload -> Get SKey
getSecretKey pkp :: PKPayload
pkp
  | PKPayload -> PubKeyAlgorithm
_pkalgo PKPayload
pkp PubKeyAlgorithm -> [PubKeyAlgorithm] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [PubKeyAlgorithm
RSA, PubKeyAlgorithm
DeprecatedRSAEncryptOnly, PubKeyAlgorithm
DeprecatedRSASignOnly] = do
    MPI d :: Integer
d <- Get MPI
forall t. Binary t => Get t
get
    MPI p :: Integer
p <- Get MPI
forall t. Binary t => Get t
get
    MPI q :: Integer
q <- Get MPI
forall t. Binary t => Get t
get
    MPI _ <- Get MPI
forall t. Binary t => Get t
get -- u
    let dP :: Integer
dP = 0
        dQ :: Integer
dQ = 0
        qinv :: Integer
qinv = 0
        pub :: PublicKey
pub = (\(RSAPubKey (RSA_PublicKey x :: PublicKey
x)) -> PublicKey
x) (PKPayload
pkp PKPayload -> Getting PKey PKPayload PKey -> PKey
forall s a. s -> Getting a s a -> a
^. Getting PKey PKPayload PKey
Lens' PKPayload PKey
pubkey)
    SKey -> Get SKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SKey -> Get SKey) -> SKey -> Get SKey
forall a b. (a -> b) -> a -> b
$ RSA_PrivateKey -> SKey
RSAPrivateKey (PrivateKey -> RSA_PrivateKey
RSA_PrivateKey (PublicKey
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> PrivateKey
R.PrivateKey PublicKey
pub Integer
d Integer
p Integer
q Integer
dP Integer
dQ Integer
qinv))
  | PKPayload -> PubKeyAlgorithm
_pkalgo PKPayload
pkp PubKeyAlgorithm -> PubKeyAlgorithm -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyAlgorithm
DSA = do
    MPI x :: Integer
x <- Get MPI
forall t. Binary t => Get t
get
    SKey -> Get SKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SKey -> Get SKey) -> SKey -> Get SKey
forall a b. (a -> b) -> a -> b
$ DSA_PrivateKey -> SKey
DSAPrivateKey (PrivateKey -> DSA_PrivateKey
DSA_PrivateKey (Params -> Integer -> PrivateKey
D.PrivateKey (Integer -> Integer -> Integer -> Params
D.Params 0 0 0) Integer
x))
  | PKPayload -> PubKeyAlgorithm
_pkalgo PKPayload
pkp PubKeyAlgorithm -> [PubKeyAlgorithm] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [PubKeyAlgorithm
ElgamalEncryptOnly, PubKeyAlgorithm
ForbiddenElgamal] = do
    MPI x :: Integer
x <- Get MPI
forall t. Binary t => Get t
get
    SKey -> Get SKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SKey -> Get SKey) -> SKey -> Get SKey
forall a b. (a -> b) -> a -> b
$ Integer -> SKey
ElGamalPrivateKey Integer
x
  | PKPayload -> PubKeyAlgorithm
_pkalgo PKPayload
pkp PubKeyAlgorithm -> PubKeyAlgorithm -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyAlgorithm
ECDSA = do
    MPI pn :: Integer
pn <- Get MPI
forall t. Binary t => Get t
get
    let pubcurve :: Curve
pubcurve =
          (\(ECDSAPubKey (ECDSA_PublicKey p :: PublicKey
p)) -> PublicKey -> Curve
ECDSA.public_curve PublicKey
p)
            (PKPayload
pkp PKPayload -> Getting PKey PKPayload PKey -> PKey
forall s a. s -> Getting a s a -> a
^. Getting PKey PKPayload PKey
Lens' PKPayload PKey
pubkey)
    SKey -> Get SKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SKey -> Get SKey) -> SKey -> Get SKey
forall a b. (a -> b) -> a -> b
$ ECDSA_PrivateKey -> SKey
ECDSAPrivateKey (PrivateKey -> ECDSA_PrivateKey
ECDSA_PrivateKey (Curve -> Integer -> PrivateKey
ECDSA.PrivateKey Curve
pubcurve Integer
pn))
  | PKPayload -> PubKeyAlgorithm
_pkalgo PKPayload
pkp PubKeyAlgorithm -> PubKeyAlgorithm -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyAlgorithm
ECDH -- FIXME: deduplicate this and above
   = do
    MPI pn :: Integer
pn <- Get MPI
forall t. Binary t => Get t
get
    let pubcurve :: Curve
pubcurve =
          (\(ECDSAPubKey (ECDSA_PublicKey p :: PublicKey
p)) -> PublicKey -> Curve
ECDSA.public_curve PublicKey
p)
            (PKPayload
pkp PKPayload -> Getting PKey PKPayload PKey -> PKey
forall s a. s -> Getting a s a -> a
^. Getting PKey PKPayload PKey
Lens' PKPayload PKey
pubkey)
    SKey -> Get SKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SKey -> Get SKey) -> SKey -> Get SKey
forall a b. (a -> b) -> a -> b
$ ECDSA_PrivateKey -> SKey
ECDHPrivateKey (PrivateKey -> ECDSA_PrivateKey
ECDSA_PrivateKey (Curve -> Integer -> PrivateKey
ECDSA.PrivateKey Curve
pubcurve Integer
pn))

putSKey :: SKey -> Put
putSKey :: SKey -> Put
putSKey (RSAPrivateKey (RSA_PrivateKey (R.PrivateKey _ d :: Integer
d p :: Integer
p q :: Integer
q _ _ _))) =
  MPI -> Put
forall t. Binary t => t -> Put
put (Integer -> MPI
MPI Integer
d) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MPI -> Put
forall t. Binary t => t -> Put
put (Integer -> MPI
MPI Integer
p) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MPI -> Put
forall t. Binary t => t -> Put
put (Integer -> MPI
MPI Integer
q) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MPI -> Put
forall t. Binary t => t -> Put
put (Integer -> MPI
MPI Integer
u)
  where
    u :: Integer
u = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
multiplicativeInverse Integer
q Integer
p

putMPI :: MPI -> Put
putMPI :: MPI -> Put
putMPI (MPI i :: Integer
i) = do
  let bs :: ByteString
bs = Integer -> ByteString
forall ba. ByteArray ba => Integer -> ba
i2osp Integer
i
  Word16 -> Put
putWord16be (Word16 -> Put) -> (Integer -> Word16) -> Integer -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word16) -> (Integer -> Int) -> Integer -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Int
numBits (Integer -> Put) -> Integer -> Put
forall a b. (a -> b) -> a -> b
$ Integer
i
  ByteString -> Put
putByteString ByteString
bs

getPKPayload :: Get PKPayload
getPKPayload :: Get PKPayload
getPKPayload = do
  Word8
version <- Get Word8
getWord8
  ThirtyTwoBitTimeStamp
ctime <- (Word32 -> ThirtyTwoBitTimeStamp)
-> Get Word32 -> Get ThirtyTwoBitTimeStamp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ThirtyTwoBitTimeStamp
ThirtyTwoBitTimeStamp Get Word32
getWord32be
  if Word8
version Word8 -> [Word8] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [2, 3]
    then do
      Word16
v3e <- Get Word16
getWord16be
      PubKeyAlgorithm
pka <- Get PubKeyAlgorithm
forall t. Binary t => Get t
get
      PKey
pk <- PubKeyAlgorithm -> Get PKey
getPubkey PubKeyAlgorithm
pka
      PKPayload -> Get PKPayload
forall (m :: * -> *) a. Monad m => a -> m a
return (PKPayload -> Get PKPayload) -> PKPayload -> Get PKPayload
forall a b. (a -> b) -> a -> b
$! KeyVersion
-> ThirtyTwoBitTimeStamp
-> Word16
-> PubKeyAlgorithm
-> PKey
-> PKPayload
PKPayload KeyVersion
DeprecatedV3 ThirtyTwoBitTimeStamp
ctime Word16
v3e PubKeyAlgorithm
pka PKey
pk
    else do
      PubKeyAlgorithm
pka <- Get PubKeyAlgorithm
forall t. Binary t => Get t
get
      PKey
pk <- PubKeyAlgorithm -> Get PKey
getPubkey PubKeyAlgorithm
pka
      PKPayload -> Get PKPayload
forall (m :: * -> *) a. Monad m => a -> m a
return (PKPayload -> Get PKPayload) -> PKPayload -> Get PKPayload
forall a b. (a -> b) -> a -> b
$! KeyVersion
-> ThirtyTwoBitTimeStamp
-> Word16
-> PubKeyAlgorithm
-> PKey
-> PKPayload
PKPayload KeyVersion
V4 ThirtyTwoBitTimeStamp
ctime 0 PubKeyAlgorithm
pka PKey
pk

putPKPayload :: PKPayload -> Put
putPKPayload :: PKPayload -> Put
putPKPayload (PKPayload DeprecatedV3 ctime :: ThirtyTwoBitTimeStamp
ctime v3e :: Word16
v3e pka :: PubKeyAlgorithm
pka pk :: PKey
pk) = do
  Word8 -> Put
putWord8 3
  Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitTimeStamp -> Word32)
-> ThirtyTwoBitTimeStamp
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitTimeStamp -> Word32
unThirtyTwoBitTimeStamp (ThirtyTwoBitTimeStamp -> Put) -> ThirtyTwoBitTimeStamp -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitTimeStamp
ctime
  Word16 -> Put
putWord16be Word16
v3e
  PubKeyAlgorithm -> Put
forall t. Binary t => t -> Put
put PubKeyAlgorithm
pka
  PKey -> Put
putPubkey PKey
pk
putPKPayload (PKPayload V4 ctime :: ThirtyTwoBitTimeStamp
ctime _ pka :: PubKeyAlgorithm
pka pk :: PKey
pk) = do
  Word8 -> Put
putWord8 4
  Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitTimeStamp -> Word32)
-> ThirtyTwoBitTimeStamp
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitTimeStamp -> Word32
unThirtyTwoBitTimeStamp (ThirtyTwoBitTimeStamp -> Put) -> ThirtyTwoBitTimeStamp -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitTimeStamp
ctime
  PubKeyAlgorithm -> Put
forall t. Binary t => t -> Put
put PubKeyAlgorithm
pka
  PKey -> Put
putPubkey PKey
pk

getSKAddendum :: PKPayload -> Get SKAddendum
getSKAddendum :: PKPayload -> Get SKAddendum
getSKAddendum pkp :: PKPayload
pkp = do
  Word8
s2kusage <- Get Word8
getWord8
  case Word8
s2kusage of
    0 -> do
      SKey
sk <- PKPayload -> Get SKey
getSecretKey PKPayload
pkp
      Word16
checksum <- Get Word16
getWord16be -- FIXME: validate checksum?
      SKAddendum -> Get SKAddendum
forall (m :: * -> *) a. Monad m => a -> m a
return (SKAddendum -> Get SKAddendum) -> SKAddendum -> Get SKAddendum
forall a b. (a -> b) -> a -> b
$ SKey -> Word16 -> SKAddendum
SUUnencrypted SKey
sk Word16
checksum
    255 -> do
      Word8
symenc <- Get Word8
getWord8
      S2K
s2k <- Get S2K
getS2K
      case S2K
s2k -- FIXME: this is a mess
            of
        OtherS2K _ _ -> SKAddendum -> Get SKAddendum
forall (m :: * -> *) a. Monad m => a -> m a
return (SKAddendum -> Get SKAddendum) -> SKAddendum -> Get SKAddendum
forall a b. (a -> b) -> a -> b
$ SymmetricAlgorithm -> S2K -> IV -> ByteString -> SKAddendum
SUS16bit (Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
symenc) S2K
s2k IV
forall a. Monoid a => a
mempty ByteString
BL.empty
        _ -> do
          ByteString
iv <- Int -> Get ByteString
getByteString (SymmetricAlgorithm -> Int
symEncBlockSize (SymmetricAlgorithm -> Int)
-> (Word8 -> SymmetricAlgorithm) -> Word8 -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> Int) -> Word8 -> Int
forall a b. (a -> b) -> a -> b
$ Word8
symenc)
          ByteString
encryptedblock <- Get ByteString
getRemainingLazyByteString
          SKAddendum -> Get SKAddendum
forall (m :: * -> *) a. Monad m => a -> m a
return (SKAddendum -> Get SKAddendum) -> SKAddendum -> Get SKAddendum
forall a b. (a -> b) -> a -> b
$ SymmetricAlgorithm -> S2K -> IV -> ByteString -> SKAddendum
SUS16bit (Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
symenc) S2K
s2k (ByteString -> IV
IV ByteString
iv) ByteString
encryptedblock
    254 -> do
      Word8
symenc <- Get Word8
getWord8
      S2K
s2k <- Get S2K
getS2K
      case S2K
s2k -- FIXME: this is a mess
            of
        OtherS2K _ _ -> SKAddendum -> Get SKAddendum
forall (m :: * -> *) a. Monad m => a -> m a
return (SKAddendum -> Get SKAddendum) -> SKAddendum -> Get SKAddendum
forall a b. (a -> b) -> a -> b
$ SymmetricAlgorithm -> S2K -> IV -> ByteString -> SKAddendum
SUSSHA1 (Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
symenc) S2K
s2k IV
forall a. Monoid a => a
mempty ByteString
BL.empty
        _ -> do
          ByteString
iv <- Int -> Get ByteString
getByteString (SymmetricAlgorithm -> Int
symEncBlockSize (SymmetricAlgorithm -> Int)
-> (Word8 -> SymmetricAlgorithm) -> Word8 -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> Int) -> Word8 -> Int
forall a b. (a -> b) -> a -> b
$ Word8
symenc)
          ByteString
encryptedblock <- Get ByteString
getRemainingLazyByteString
          SKAddendum -> Get SKAddendum
forall (m :: * -> *) a. Monad m => a -> m a
return (SKAddendum -> Get SKAddendum) -> SKAddendum -> Get SKAddendum
forall a b. (a -> b) -> a -> b
$ SymmetricAlgorithm -> S2K -> IV -> ByteString -> SKAddendum
SUSSHA1 (Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
symenc) S2K
s2k (ByteString -> IV
IV ByteString
iv) ByteString
encryptedblock
    symenc :: Word8
symenc -> do
      ByteString
iv <- Int -> Get ByteString
getByteString (SymmetricAlgorithm -> Int
symEncBlockSize (SymmetricAlgorithm -> Int)
-> (Word8 -> SymmetricAlgorithm) -> Word8 -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal (Word8 -> Int) -> Word8 -> Int
forall a b. (a -> b) -> a -> b
$ Word8
symenc)
      ByteString
encryptedblock <- Get ByteString
getRemainingLazyByteString
      SKAddendum -> Get SKAddendum
forall (m :: * -> *) a. Monad m => a -> m a
return (SKAddendum -> Get SKAddendum) -> SKAddendum -> Get SKAddendum
forall a b. (a -> b) -> a -> b
$ SymmetricAlgorithm -> IV -> ByteString -> SKAddendum
SUSym (Word8 -> SymmetricAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
symenc) (ByteString -> IV
IV ByteString
iv) ByteString
encryptedblock

putSKAddendum :: SKAddendum -> Put
putSKAddendum :: SKAddendum -> Put
putSKAddendum (SUSSHA1 symenc :: SymmetricAlgorithm
symenc s2k :: S2K
s2k iv :: IV
iv encryptedblock :: ByteString
encryptedblock) = do
  Word8 -> Put
putWord8 254
  SymmetricAlgorithm -> Put
forall t. Binary t => t -> Put
put SymmetricAlgorithm
symenc
  S2K -> Put
forall t. Binary t => t -> Put
put S2K
s2k
  ByteString -> Put
putByteString (IV -> ByteString
unIV IV
iv)
  ByteString -> Put
putLazyByteString ByteString
encryptedblock
putSKAddendum (SUUnencrypted sk :: SKey
sk checksum :: Word16
checksum) = do
  Word8 -> Put
putWord8 0
  let skb :: ByteString
skb = Put -> ByteString
runPut (SKey -> Put
putSKey SKey
sk)
  ByteString -> Put
putLazyByteString ByteString
skb
  Word16 -> Put
putWord16be
    (if Word16
checksum Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
== 0
       then (Word16 -> Word8 -> Word16) -> Word16 -> ByteString -> Word16
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BL.foldl (\a :: Word16
a b :: Word8
b -> Word16 -> Word16 -> Word16
forall a. Integral a => a -> a -> a
mod (Word16
a Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
+ Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b) 0xffff) (0 :: Word16) ByteString
skb
       else Word16
checksum) -- FIXME: be saner
putSKAddendum _ = String -> Put
forall a. HasCallStack => String -> a
error "Type not supported"

symEncBlockSize :: SymmetricAlgorithm -> Int
symEncBlockSize :: SymmetricAlgorithm -> Int
symEncBlockSize Plaintext = 0
symEncBlockSize IDEA = 8
symEncBlockSize TripleDES = 8
symEncBlockSize CAST5 = 8
symEncBlockSize Blowfish = 8
symEncBlockSize AES128 = 16
symEncBlockSize AES192 = 16
symEncBlockSize AES256 = 16
symEncBlockSize Twofish = 16
symEncBlockSize Camellia128 = 16
symEncBlockSize _ = 8 -- FIXME

decodeIterationCount :: Word8 -> IterationCount
decodeIterationCount :: Word8 -> IterationCount
decodeIterationCount c :: Word8
c =
  Int -> IterationCount
IterationCount
    ((16 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 15)) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` ((Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 4) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 6))

encodeIterationCount :: IterationCount -> Word8 -- should this really be a lookup table?
encodeIterationCount :: IterationCount -> Word8
encodeIterationCount 1024 = 0
encodeIterationCount 1088 = 1
encodeIterationCount 1152 = 2
encodeIterationCount 1216 = 3
encodeIterationCount 1280 = 4
encodeIterationCount 1344 = 5
encodeIterationCount 1408 = 6
encodeIterationCount 1472 = 7
encodeIterationCount 1536 = 8
encodeIterationCount 1600 = 9
encodeIterationCount 1664 = 10
encodeIterationCount 1728 = 11
encodeIterationCount 1792 = 12
encodeIterationCount 1856 = 13
encodeIterationCount 1920 = 14
encodeIterationCount 1984 = 15
encodeIterationCount 2048 = 16
encodeIterationCount 2176 = 17
encodeIterationCount 2304 = 18
encodeIterationCount 2432 = 19
encodeIterationCount 2560 = 20
encodeIterationCount 2688 = 21
encodeIterationCount 2816 = 22
encodeIterationCount 2944 = 23
encodeIterationCount 3072 = 24
encodeIterationCount 3200 = 25
encodeIterationCount 3328 = 26
encodeIterationCount 3456 = 27
encodeIterationCount 3584 = 28
encodeIterationCount 3712 = 29
encodeIterationCount 3840 = 30
encodeIterationCount 3968 = 31
encodeIterationCount 4096 = 32
encodeIterationCount 4352 = 33
encodeIterationCount 4608 = 34
encodeIterationCount 4864 = 35
encodeIterationCount 5120 = 36
encodeIterationCount 5376 = 37
encodeIterationCount 5632 = 38
encodeIterationCount 5888 = 39
encodeIterationCount 6144 = 40
encodeIterationCount 6400 = 41
encodeIterationCount 6656 = 42
encodeIterationCount 6912 = 43
encodeIterationCount 7168 = 44
encodeIterationCount 7424 = 45
encodeIterationCount 7680 = 46
encodeIterationCount 7936 = 47
encodeIterationCount 8192 = 48
encodeIterationCount 8704 = 49
encodeIterationCount 9216 = 50
encodeIterationCount 9728 = 51
encodeIterationCount 10240 = 52
encodeIterationCount 10752 = 53
encodeIterationCount 11264 = 54
encodeIterationCount 11776 = 55
encodeIterationCount 12288 = 56
encodeIterationCount 12800 = 57
encodeIterationCount 13312 = 58
encodeIterationCount 13824 = 59
encodeIterationCount 14336 = 60
encodeIterationCount 14848 = 61
encodeIterationCount 15360 = 62
encodeIterationCount 15872 = 63
encodeIterationCount 16384 = 64
encodeIterationCount 17408 = 65
encodeIterationCount 18432 = 66
encodeIterationCount 19456 = 67
encodeIterationCount 20480 = 68
encodeIterationCount 21504 = 69
encodeIterationCount 22528 = 70
encodeIterationCount 23552 = 71
encodeIterationCount 24576 = 72
encodeIterationCount 25600 = 73
encodeIterationCount 26624 = 74
encodeIterationCount 27648 = 75
encodeIterationCount 28672 = 76
encodeIterationCount 29696 = 77
encodeIterationCount 30720 = 78
encodeIterationCount 31744 = 79
encodeIterationCount 32768 = 80
encodeIterationCount 34816 = 81
encodeIterationCount 36864 = 82
encodeIterationCount 38912 = 83
encodeIterationCount 40960 = 84
encodeIterationCount 43008 = 85
encodeIterationCount 45056 = 86
encodeIterationCount 47104 = 87
encodeIterationCount 49152 = 88
encodeIterationCount 51200 = 89
encodeIterationCount 53248 = 90
encodeIterationCount 55296 = 91
encodeIterationCount 57344 = 92
encodeIterationCount 59392 = 93
encodeIterationCount 61440 = 94
encodeIterationCount 63488 = 95
encodeIterationCount 65536 = 96
encodeIterationCount 69632 = 97
encodeIterationCount 73728 = 98
encodeIterationCount 77824 = 99
encodeIterationCount 81920 = 100
encodeIterationCount 86016 = 101
encodeIterationCount 90112 = 102
encodeIterationCount 94208 = 103
encodeIterationCount 98304 = 104
encodeIterationCount 102400 = 105
encodeIterationCount 106496 = 106
encodeIterationCount 110592 = 107
encodeIterationCount 114688 = 108
encodeIterationCount 118784 = 109
encodeIterationCount 122880 = 110
encodeIterationCount 126976 = 111
encodeIterationCount 131072 = 112
encodeIterationCount 139264 = 113
encodeIterationCount 147456 = 114
encodeIterationCount 155648 = 115
encodeIterationCount 163840 = 116
encodeIterationCount 172032 = 117
encodeIterationCount 180224 = 118
encodeIterationCount 188416 = 119
encodeIterationCount 196608 = 120
encodeIterationCount 204800 = 121
encodeIterationCount 212992 = 122
encodeIterationCount 221184 = 123
encodeIterationCount 229376 = 124
encodeIterationCount 237568 = 125
encodeIterationCount 245760 = 126
encodeIterationCount 253952 = 127
encodeIterationCount 262144 = 128
encodeIterationCount 278528 = 129
encodeIterationCount 294912 = 130
encodeIterationCount 311296 = 131
encodeIterationCount 327680 = 132
encodeIterationCount 344064 = 133
encodeIterationCount 360448 = 134
encodeIterationCount 376832 = 135
encodeIterationCount 393216 = 136
encodeIterationCount 409600 = 137
encodeIterationCount 425984 = 138
encodeIterationCount 442368 = 139
encodeIterationCount 458752 = 140
encodeIterationCount 475136 = 141
encodeIterationCount 491520 = 142
encodeIterationCount 507904 = 143
encodeIterationCount 524288 = 144
encodeIterationCount 557056 = 145
encodeIterationCount 589824 = 146
encodeIterationCount 622592 = 147
encodeIterationCount 655360 = 148
encodeIterationCount 688128 = 149
encodeIterationCount 720896 = 150
encodeIterationCount 753664 = 151
encodeIterationCount 786432 = 152
encodeIterationCount 819200 = 153
encodeIterationCount 851968 = 154
encodeIterationCount 884736 = 155
encodeIterationCount 917504 = 156
encodeIterationCount 950272 = 157
encodeIterationCount 983040 = 158
encodeIterationCount 1015808 = 159
encodeIterationCount 1048576 = 160
encodeIterationCount 1114112 = 161
encodeIterationCount 1179648 = 162
encodeIterationCount 1245184 = 163
encodeIterationCount 1310720 = 164
encodeIterationCount 1376256 = 165
encodeIterationCount 1441792 = 166
encodeIterationCount 1507328 = 167
encodeIterationCount 1572864 = 168
encodeIterationCount 1638400 = 169
encodeIterationCount 1703936 = 170
encodeIterationCount 1769472 = 171
encodeIterationCount 1835008 = 172
encodeIterationCount 1900544 = 173
encodeIterationCount 1966080 = 174
encodeIterationCount 2031616 = 175
encodeIterationCount 2097152 = 176
encodeIterationCount 2228224 = 177
encodeIterationCount 2359296 = 178
encodeIterationCount 2490368 = 179
encodeIterationCount 2621440 = 180
encodeIterationCount 2752512 = 181
encodeIterationCount 2883584 = 182
encodeIterationCount 3014656 = 183
encodeIterationCount 3145728 = 184
encodeIterationCount 3276800 = 185
encodeIterationCount 3407872 = 186
encodeIterationCount 3538944 = 187
encodeIterationCount 3670016 = 188
encodeIterationCount 3801088 = 189
encodeIterationCount 3932160 = 190
encodeIterationCount 4063232 = 191
encodeIterationCount 4194304 = 192
encodeIterationCount 4456448 = 193
encodeIterationCount 4718592 = 194
encodeIterationCount 4980736 = 195
encodeIterationCount 5242880 = 196
encodeIterationCount 5505024 = 197
encodeIterationCount 5767168 = 198
encodeIterationCount 6029312 = 199
encodeIterationCount 6291456 = 200
encodeIterationCount 6553600 = 201
encodeIterationCount 6815744 = 202
encodeIterationCount 7077888 = 203
encodeIterationCount 7340032 = 204
encodeIterationCount 7602176 = 205
encodeIterationCount 7864320 = 206
encodeIterationCount 8126464 = 207
encodeIterationCount 8388608 = 208
encodeIterationCount 8912896 = 209
encodeIterationCount 9437184 = 210
encodeIterationCount 9961472 = 211
encodeIterationCount 10485760 = 212
encodeIterationCount 11010048 = 213
encodeIterationCount 11534336 = 214
encodeIterationCount 12058624 = 215
encodeIterationCount 12582912 = 216
encodeIterationCount 13107200 = 217
encodeIterationCount 13631488 = 218
encodeIterationCount 14155776 = 219
encodeIterationCount 14680064 = 220
encodeIterationCount 15204352 = 221
encodeIterationCount 15728640 = 222
encodeIterationCount 16252928 = 223
encodeIterationCount 16777216 = 224
encodeIterationCount 17825792 = 225
encodeIterationCount 18874368 = 226
encodeIterationCount 19922944 = 227
encodeIterationCount 20971520 = 228
encodeIterationCount 22020096 = 229
encodeIterationCount 23068672 = 230
encodeIterationCount 24117248 = 231
encodeIterationCount 25165824 = 232
encodeIterationCount 26214400 = 233
encodeIterationCount 27262976 = 234
encodeIterationCount 28311552 = 235
encodeIterationCount 29360128 = 236
encodeIterationCount 30408704 = 237
encodeIterationCount 31457280 = 238
encodeIterationCount 32505856 = 239
encodeIterationCount 33554432 = 240
encodeIterationCount 35651584 = 241
encodeIterationCount 37748736 = 242
encodeIterationCount 39845888 = 243
encodeIterationCount 41943040 = 244
encodeIterationCount 44040192 = 245
encodeIterationCount 46137344 = 246
encodeIterationCount 48234496 = 247
encodeIterationCount 50331648 = 248
encodeIterationCount 52428800 = 249
encodeIterationCount 54525952 = 250
encodeIterationCount 56623104 = 251
encodeIterationCount 58720256 = 252
encodeIterationCount 60817408 = 253
encodeIterationCount 62914560 = 254
encodeIterationCount 65011712 = 255
encodeIterationCount n :: IterationCount
n = String -> Word8
forall a. HasCallStack => String -> a
error ("invalid iteration count" String -> String -> String
forall a. [a] -> [a] -> [a]
++ IterationCount -> String
forall a. Show a => a -> String
show IterationCount
n)

getSignaturePayload :: Get SignaturePayload
getSignaturePayload :: Get SignaturePayload
getSignaturePayload = do
  Word8
pv <- Get Word8
getWord8
  case Word8
pv of
    3 -> do
      Word8
hashlen <- Get Word8
getWord8
      Bool -> Get ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word8
hashlen Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 5)
      Word8
st <- Get Word8
getWord8
      ThirtyTwoBitTimeStamp
ctime <- (Word32 -> ThirtyTwoBitTimeStamp)
-> Get Word32 -> Get ThirtyTwoBitTimeStamp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ThirtyTwoBitTimeStamp
ThirtyTwoBitTimeStamp Get Word32
getWord32be
      ByteString
eok <- ByteOffset -> Get ByteString
getLazyByteString 8
      Word8
pka <- Get Word8
forall t. Binary t => Get t
get
      Word8
ha <- Get Word8
forall t. Binary t => Get t
get
      Word16
left16 <- Get Word16
getWord16be
      ByteString
mpib <- Get ByteString
getRemainingLazyByteString
      case Get [MPI]
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, [MPI])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get MPI -> Get [MPI]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some Get MPI
getMPI) ByteString
mpib of
        Left (_, _, e :: String
e) -> String -> Get SignaturePayload
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("v3 sig MPIs " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
        Right (_, _, mpis :: [MPI]
mpis) ->
          SignaturePayload -> Get SignaturePayload
forall (m :: * -> *) a. Monad m => a -> m a
return (SignaturePayload -> Get SignaturePayload)
-> SignaturePayload -> Get SignaturePayload
forall a b. (a -> b) -> a -> b
$
          SigType
-> ThirtyTwoBitTimeStamp
-> EightOctetKeyId
-> PubKeyAlgorithm
-> HashAlgorithm
-> Word16
-> NonEmpty MPI
-> SignaturePayload
SigV3
            (Word8 -> SigType
forall a. FutureVal a => Word8 -> a
toFVal Word8
st)
            ThirtyTwoBitTimeStamp
ctime
            (ByteString -> EightOctetKeyId
EightOctetKeyId ByteString
eok)
            (Word8 -> PubKeyAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
pka)
            (Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ha)
            Word16
left16
            ([MPI] -> NonEmpty MPI
forall a. [a] -> NonEmpty a
NE.fromList [MPI]
mpis)
    4 -> do
      Word8
st <- Get Word8
getWord8
      Word8
pka <- Get Word8
forall t. Binary t => Get t
get
      Word8
ha <- Get Word8
forall t. Binary t => Get t
get
      Word16
hlen <- Get Word16
getWord16be
      ByteString
hb <- ByteOffset -> Get ByteString
getLazyByteString (Word16 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
hlen)
      let hashed :: [SigSubPacket]
hashed =
            case Get [SigSubPacket]
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, [SigSubPacket])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get SigSubPacket -> Get [SigSubPacket]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Get SigSubPacket
getSigSubPacket) ByteString
hb of
              Left (_, _, err :: String
err) -> String -> [SigSubPacket]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("v4 sig hasheds " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)
              Right (_, _, h :: [SigSubPacket]
h) -> [SigSubPacket]
h
      Word16
ulen <- Get Word16
getWord16be
      ByteString
ub <- ByteOffset -> Get ByteString
getLazyByteString (Word16 -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
ulen)
      let unhashed :: [SigSubPacket]
unhashed =
            case Get [SigSubPacket]
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, [SigSubPacket])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get SigSubPacket -> Get [SigSubPacket]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Get SigSubPacket
getSigSubPacket) ByteString
ub of
              Left (_, _, err :: String
err) -> String -> [SigSubPacket]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("v4 sig unhasheds " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)
              Right (_, _, u :: [SigSubPacket]
u) -> [SigSubPacket]
u
      Word16
left16 <- Get Word16
getWord16be
      ByteString
mpib <- Get ByteString
getRemainingLazyByteString
      case Get [MPI]
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, [MPI])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get MPI -> Get [MPI]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some Get MPI
getMPI) ByteString
mpib of
        Left (_, _, e :: String
e) -> String -> Get SignaturePayload
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("v4 sig MPIs " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
        Right (_, _, mpis :: [MPI]
mpis) ->
          SignaturePayload -> Get SignaturePayload
forall (m :: * -> *) a. Monad m => a -> m a
return (SignaturePayload -> Get SignaturePayload)
-> SignaturePayload -> Get SignaturePayload
forall a b. (a -> b) -> a -> b
$
          SigType
-> PubKeyAlgorithm
-> HashAlgorithm
-> [SigSubPacket]
-> [SigSubPacket]
-> Word16
-> NonEmpty MPI
-> SignaturePayload
SigV4
            (Word8 -> SigType
forall a. FutureVal a => Word8 -> a
toFVal Word8
st)
            (Word8 -> PubKeyAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
pka)
            (Word8 -> HashAlgorithm
forall a. FutureVal a => Word8 -> a
toFVal Word8
ha)
            [SigSubPacket]
hashed
            [SigSubPacket]
unhashed
            Word16
left16
            ([MPI] -> NonEmpty MPI
forall a. [a] -> NonEmpty a
NE.fromList [MPI]
mpis)
    _ -> do
      ByteString
bs <- Get ByteString
getRemainingLazyByteString
      SignaturePayload -> Get SignaturePayload
forall (m :: * -> *) a. Monad m => a -> m a
return (SignaturePayload -> Get SignaturePayload)
-> SignaturePayload -> Get SignaturePayload
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString -> SignaturePayload
SigVOther Word8
pv ByteString
bs

putSignaturePayload :: SignaturePayload -> Put
putSignaturePayload :: SignaturePayload -> Put
putSignaturePayload (SigV3 st :: SigType
st ctime :: ThirtyTwoBitTimeStamp
ctime eok :: EightOctetKeyId
eok pka :: PubKeyAlgorithm
pka ha :: HashAlgorithm
ha left16 :: Word16
left16 mpis :: NonEmpty MPI
mpis) = do
  Word8 -> Put
putWord8 3
  Word8 -> Put
putWord8 5 -- hashlen
  SigType -> Put
forall t. Binary t => t -> Put
put SigType
st
  Word32 -> Put
putWord32be (Word32 -> Put)
-> (ThirtyTwoBitTimeStamp -> Word32)
-> ThirtyTwoBitTimeStamp
-> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThirtyTwoBitTimeStamp -> Word32
unThirtyTwoBitTimeStamp (ThirtyTwoBitTimeStamp -> Put) -> ThirtyTwoBitTimeStamp -> Put
forall a b. (a -> b) -> a -> b
$ ThirtyTwoBitTimeStamp
ctime
  ByteString -> Put
putLazyByteString (EightOctetKeyId -> ByteString
unEOKI EightOctetKeyId
eok)
  PubKeyAlgorithm -> Put
forall t. Binary t => t -> Put
put PubKeyAlgorithm
pka
  HashAlgorithm -> Put
forall t. Binary t => t -> Put
put HashAlgorithm
ha
  Word16 -> Put
putWord16be Word16
left16
  (MPI -> Put) -> NonEmpty MPI -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
F.mapM_ MPI -> Put
forall t. Binary t => t -> Put
put NonEmpty MPI
mpis
putSignaturePayload (SigV4 st :: SigType
st pka :: PubKeyAlgorithm
pka ha :: HashAlgorithm
ha hashed :: [SigSubPacket]
hashed unhashed :: [SigSubPacket]
unhashed left16 :: Word16
left16 mpis :: NonEmpty MPI
mpis) = do
  Word8 -> Put
putWord8 4
  SigType -> Put
forall t. Binary t => t -> Put
put SigType
st
  PubKeyAlgorithm -> Put
forall t. Binary t => t -> Put
put PubKeyAlgorithm
pka
  HashAlgorithm -> Put
forall t. Binary t => t -> Put
put HashAlgorithm
ha
  let hb :: ByteString
hb = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ (SigSubPacket -> Put) -> [SigSubPacket] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ SigSubPacket -> Put
forall t. Binary t => t -> Put
put [SigSubPacket]
hashed
  Word16 -> Put
putWord16be (Word16 -> Put) -> (ByteString -> Word16) -> ByteString -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Word16)
-> (ByteString -> ByteOffset) -> ByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteOffset
BL.length (ByteString -> Put) -> ByteString -> Put
forall a b. (a -> b) -> a -> b
$ ByteString
hb
  ByteString -> Put
putLazyByteString ByteString
hb
  let ub :: ByteString
ub = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ (SigSubPacket -> Put) -> [SigSubPacket] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ SigSubPacket -> Put
forall t. Binary t => t -> Put
put [SigSubPacket]
unhashed
  Word16 -> Put
putWord16be (Word16 -> Put) -> (ByteString -> Word16) -> ByteString -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Word16)
-> (ByteString -> ByteOffset) -> ByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteOffset
BL.length (ByteString -> Put) -> ByteString -> Put
forall a b. (a -> b) -> a -> b
$ ByteString
ub
  ByteString -> Put
putLazyByteString ByteString
ub
  Word16 -> Put
putWord16be Word16
left16
  (MPI -> Put) -> NonEmpty MPI -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
F.mapM_ MPI -> Put
forall t. Binary t => t -> Put
put NonEmpty MPI
mpis
putSignaturePayload (SigVOther pv :: Word8
pv bs :: ByteString
bs) = do
  Word8 -> Put
putWord8 Word8
pv
  ByteString -> Put
putLazyByteString ByteString
bs

putTK :: TK -> Put
putTK :: TK -> Put
putTK key :: TK
key = do
  let pkp :: PKPayload
pkp = TK
key TK -> Getting PKPayload TK PKPayload -> PKPayload
forall s a. s -> Getting a s a -> a
^. ((PKPayload, Maybe SKAddendum)
 -> Const PKPayload (PKPayload, Maybe SKAddendum))
-> TK -> Const PKPayload TK
Lens' TK (PKPayload, Maybe SKAddendum)
tkKey (((PKPayload, Maybe SKAddendum)
  -> Const PKPayload (PKPayload, Maybe SKAddendum))
 -> TK -> Const PKPayload TK)
-> ((PKPayload -> Const PKPayload PKPayload)
    -> (PKPayload, Maybe SKAddendum)
    -> Const PKPayload (PKPayload, Maybe SKAddendum))
-> Getting PKPayload TK PKPayload
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PKPayload -> Const PKPayload PKPayload)
-> (PKPayload, Maybe SKAddendum)
-> Const PKPayload (PKPayload, Maybe SKAddendum)
forall s t a b. Field1 s t a b => Lens s t a b
_1
  Put -> (SKAddendum -> Put) -> Maybe SKAddendum -> Put
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (PublicKey -> Put
forall t. Binary t => t -> Put
put (PKPayload -> PublicKey
PublicKey PKPayload
pkp))
    (\ska :: SKAddendum
ska -> SecretKey -> Put
forall t. Binary t => t -> Put
put (PKPayload -> SKAddendum -> SecretKey
SecretKey PKPayload
pkp SKAddendum
ska))
    ((PKPayload, Maybe SKAddendum) -> Maybe SKAddendum
forall a b. (a, b) -> b
snd (TK
key TK
-> Getting
     (PKPayload, Maybe SKAddendum) TK (PKPayload, Maybe SKAddendum)
-> (PKPayload, Maybe SKAddendum)
forall s a. s -> Getting a s a -> a
^. Getting
  (PKPayload, Maybe SKAddendum) TK (PKPayload, Maybe SKAddendum)
Lens' TK (PKPayload, Maybe SKAddendum)
tkKey))
  (SignaturePayload -> Put) -> [SignaturePayload] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Signature -> Put
forall t. Binary t => t -> Put
put (Signature -> Put)
-> (SignaturePayload -> Signature) -> SignaturePayload -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignaturePayload -> Signature
Signature) (TK -> [SignaturePayload]
_tkRevs TK
key)
  ((Text, [SignaturePayload]) -> Put)
-> [(Text, [SignaturePayload])] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text, [SignaturePayload]) -> Put
forall (t :: * -> *).
Foldable t =>
(Text, t SignaturePayload) -> Put
putUid' (TK -> [(Text, [SignaturePayload])]
_tkUIDs TK
key)
  (([UserAttrSubPacket], [SignaturePayload]) -> Put)
-> [([UserAttrSubPacket], [SignaturePayload])] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([UserAttrSubPacket], [SignaturePayload]) -> Put
forall (t :: * -> *).
Foldable t =>
([UserAttrSubPacket], t SignaturePayload) -> Put
putUat' (TK -> [([UserAttrSubPacket], [SignaturePayload])]
_tkUAts TK
key)
  ((Pkt, [SignaturePayload]) -> Put)
-> [(Pkt, [SignaturePayload])] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Pkt, [SignaturePayload]) -> Put
forall t (t :: * -> *).
(Binary t, Foldable t) =>
(t, t SignaturePayload) -> Put
putSub' (TK -> [(Pkt, [SignaturePayload])]
_tkSubs TK
key)
  where
    putUid' :: (Text, t SignaturePayload) -> Put
putUid' (u :: Text
u, sps :: t SignaturePayload
sps) = UserId -> Put
forall t. Binary t => t -> Put
put (Text -> UserId
UserId Text
u) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (SignaturePayload -> Put) -> t SignaturePayload -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Signature -> Put
forall t. Binary t => t -> Put
put (Signature -> Put)
-> (SignaturePayload -> Signature) -> SignaturePayload -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignaturePayload -> Signature
Signature) t SignaturePayload
sps
    putUat' :: ([UserAttrSubPacket], t SignaturePayload) -> Put
putUat' (us :: [UserAttrSubPacket]
us, sps :: t SignaturePayload
sps) = UserAttribute -> Put
forall t. Binary t => t -> Put
put ([UserAttrSubPacket] -> UserAttribute
UserAttribute [UserAttrSubPacket]
us) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (SignaturePayload -> Put) -> t SignaturePayload -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Signature -> Put
forall t. Binary t => t -> Put
put (Signature -> Put)
-> (SignaturePayload -> Signature) -> SignaturePayload -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignaturePayload -> Signature
Signature) t SignaturePayload
sps
    putSub' :: (t, t SignaturePayload) -> Put
putSub' (p :: t
p, sps :: t SignaturePayload
sps) = t -> Put
forall t. Binary t => t -> Put
put t
p Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (SignaturePayload -> Put) -> t SignaturePayload -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Signature -> Put
forall t. Binary t => t -> Put
put (Signature -> Put)
-> (SignaturePayload -> Signature) -> SignaturePayload -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignaturePayload -> Signature
Signature) t SignaturePayload
sps

-- | Parse the packets from a ByteString, with no error reporting
parsePkts :: ByteString -> [Pkt]
parsePkts :: ByteString -> [Pkt]
parsePkts lbs :: ByteString
lbs =
  case Get [Pkt]
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, [Pkt])
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail (Get Pkt -> Get [Pkt]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some Get Pkt
getPkt) ByteString
lbs of
    Left (_, _, e :: String
e) -> []
    Right (_, _, p :: [Pkt]
p) -> [Pkt]
p