packages feed

crypton-x509-1.11.1 / Data / X509 / PrivateKey.hs

{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE PatternSynonyms #-}{-# LANGUAGE StandaloneDeriving #-}-- |-- Module      : Data.X509.PrivateKey-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : experimental-- Portability : unknown---- Private key handling in X.509 infrastructuremodule Data.X509.PrivateKey (    PrivKey (..),    PrivKeyEC (..),    PrivKeyMLDSA,    MLKeyForm (..),    MLDSAKeyForm,    pattern MLDSAKeySeed,    pattern MLDSAKeyExpanded,    pattern MLDSAKeyBoth,    privkeyMLDSAFromSeed,    privkeyMLDSAFromKey,    privkeyMLDSA_key,    privkeyMLDSA_seed,    privkeyMLDSA_form,    PrivKeyMLKEM,    privkeyMLKEMFromSeed,    privkeyMLKEMFromKey,    privkeyMLKEM_key,    privkeyMLKEM_seed,    privkeyMLKEM_form,    privkeyToAlg,) whereimport Data.Maybe (fromMaybe)import Data.Proxy (Proxy (..))import Data.ByteArray (ByteArrayAccess, ScrubbedBytes, convert)import qualified Data.ByteString as Bimport Data.ASN1.BinaryEncodingimport Data.ASN1.BitArrayimport Data.ASN1.Encodingimport Data.ASN1.Stream (getConstructedEnd)import Data.ASN1.Typesimport Data.X509.AlgorithmIdentifierimport Data.X509.Internal (safeDecode)import Data.X509.OID (curvesOIDTable, lookupByOID, lookupOID)import Data.X509.PublicKey (SerializedPoint (..))import Crypto.Debug (DebugShow (..), debugShowBytes)import Crypto.Error (CryptoFailable (..))import Crypto.Number.Basic (numBytes)import Crypto.Number.Serialize (i2osp, i2ospOf_, os2ip)import qualified Crypto.PubKey.Curve25519 as X25519import qualified Crypto.PubKey.Curve448 as X448import qualified Crypto.PubKey.DSA as DSAimport qualified Crypto.PubKey.ECC.Types as ECCimport qualified Crypto.PubKey.Ed25519 as Ed25519import qualified Crypto.PubKey.Ed448 as Ed448import qualified Crypto.PubKey.MLDSA as MLDSAimport qualified Crypto.PubKey.MLKEM as MLKEMimport qualified Crypto.PubKey.RSA as RSA-- | Elliptic Curve Private Key---- TODO: missing support for binary curve.data PrivKeyEC    = PrivKeyEC_Prime        { privkeyEC_priv :: Integer        , privkeyEC_a :: Integer        , privkeyEC_b :: Integer        , privkeyEC_prime :: Integer        , privkeyEC_generator :: SerializedPoint        , privkeyEC_order :: Integer        , privkeyEC_cofactor :: Integer        , privkeyEC_seed :: Integer        }    | PrivKeyEC_Named        { privkeyEC_name :: ECC.CurveName        , privkeyEC_priv :: Integer        }    deriving (Eq)-- | The curve is shown; @privkeyEC_priv@ is not.  Use-- 'Crypto.Debug.debugShow' to see it.instance Show PrivKeyEC where    showsPrec = showsPrivKeyEC (showString "<secret>")instance DebugShow PrivKeyEC where    debugShow k = showsPrivKeyEC (shows $ privkeyEC_priv k) 0 k ""-- | What the two instances above share, so that a field added to-- 'PrivKeyEC' cannot reach one of them and not the other.  The first-- argument renders @privkeyEC_priv@; everything else is what @deriving-- Show@ used to write.showsPrivKeyEC :: ShowS -> Int -> PrivKeyEC -> ShowSshowsPrivKeyEC priv d (PrivKeyEC_Prime _ a b p g o c s) =    showParen (d > 10) $        showString "PrivKeyEC_Prime {privkeyEC_priv = "            . priv            . showString ", privkeyEC_a = "            . shows a            . showString ", privkeyEC_b = "            . shows b            . showString ", privkeyEC_prime = "            . shows p            . showString ", privkeyEC_generator = "            . shows g            . showString ", privkeyEC_order = "            . shows o            . showString ", privkeyEC_cofactor = "            . shows c            . showString ", privkeyEC_seed = "            . shows s            . showChar '}'showsPrivKeyEC priv d (PrivKeyEC_Named n _) =    showParen (d > 10) $        showString "PrivKeyEC_Named {privkeyEC_name = "            . shows n            . showString ", privkeyEC_priv = "            . priv            . showChar '}'-- | Which of the three forms of an ML-DSA (RFC 9881 Section 6) or ML-KEM-- (RFC 9935 Section 6) private key a key was read as, and is written back-- out as.data MLKeyForm    = -- | The seed alone, @seed@, which Section 6 recommends.      MLKeySeed    | -- | The expanded key alone, @expandedKey@.      MLKeyExpanded    | -- | Both together, @both@.      MLKeyBoth    deriving (Show, Eq)-- | The former name of 'MLKeyForm'.type MLDSAKeyForm = MLKeyForm{-# DEPRECATED MLDSAKeyForm "Use MLKeyForm" #-}-- | The former name of 'MLKeySeed'.pattern MLDSAKeySeed :: MLKeyFormpattern MLDSAKeySeed = MLKeySeed-- | The former name of 'MLKeyExpanded'.pattern MLDSAKeyExpanded :: MLKeyFormpattern MLDSAKeyExpanded = MLKeyExpanded-- | The former name of 'MLKeyBoth'.pattern MLDSAKeyBoth :: MLKeyFormpattern MLDSAKeyBoth = MLKeyBoth{-# COMPLETE MLDSAKeySeed, MLDSAKeyExpanded, MLDSAKeyBoth #-}{-# DEPRECATED MLDSAKeySeed "Use MLKeySeed" #-}{-# DEPRECATED MLDSAKeyExpanded "Use MLKeyExpanded" #-}{-# DEPRECATED MLDSAKeyBoth "Use MLKeyBoth" #-}-- | An ML-DSA private key.---- RFC 9881 Section 6 writes one as the seed it was derived from, as the-- expanded key, or as both, and which of the three it was is part of the-- key here.  A key read from a file is therefore written back out as the-- same thing it came in as, rather than as whichever form this module-- happens to prefer.---- The expanded key is there whatever the form: a seed is expanded as it is-- read, so 'privkeyMLDSA_key' always answers.  The seed is not, since a-- key written as @expandedKey@ does not carry one; 'privkeyMLDSA_seed' is-- a 'Maybe' for that reason, and a key whose seed was never on the wire-- cannot be given one back -- @crypton@'s-- @Crypto.PubKey.MLDSA.generateKeyPairAndSeed@ is where a seed that is to-- be kept has to be caught.---- The constructors are not exported: a seed and an expanded key that do-- not belong together would be a key that signs with one and claims the-- other, so the only ways in are 'privkeyMLDSAFromSeed', which expands the-- seed itself, and 'privkeyMLDSAFromKey', which has no seed to disagree-- with.---- Two keys are equal when they hold the same thing /in the same form/: the-- same key as a seed and as an expanded key are two different files, and-- the encoding round trip says so.data PrivKeyMLDSA p    = PrivKeyMLDSA_Seed ScrubbedBytes (MLDSA.SigningKey p)    | PrivKeyMLDSA_Expanded (MLDSA.SigningKey p)    | PrivKeyMLDSA_Both ScrubbedBytes (MLDSA.SigningKey p)    deriving (Eq)-- | What the two instances below share, so that a form added to-- 'PrivKeyMLDSA' cannot reach one of them and not the other.  The-- arguments render the seed and the key.showsPrivKeyMLDSA    :: (ScrubbedBytes -> String)    -> (MLDSA.SigningKey p -> String)    -> PrivKeyMLDSA p    -> StringshowsPrivKeyMLDSA seed key k = case k of    PrivKeyMLDSA_Seed s sk -> con "PrivKeyMLDSA_Seed" [seed s, key sk]    PrivKeyMLDSA_Expanded sk -> con "PrivKeyMLDSA_Expanded" [key sk]    PrivKeyMLDSA_Both s sk -> con "PrivKeyMLDSA_Both" [seed s, key sk]  where    con name as = unwords (name : map (\a -> "(" ++ a ++ ")") as)-- | The form is shown; neither the seed nor the key is.  Use-- 'Crypto.Debug.debugShow' to see them.instance Show (PrivKeyMLDSA p) where    show = showsPrivKeyMLDSA (const "<secret>") showinstance DebugShow (PrivKeyMLDSA p) where    debugShow = showsPrivKeyMLDSA (debugShowBytes "seed") debugShow-- | Build a key from its seed, to be written back out in the given form.---- The expanded key is derived here, so it cannot fail to be the one the-- seed belongs to.  Passing 'MLKeyExpanded' throws the seed away, which-- is the way to turn a seed into a key that will be written without one.privkeyMLDSAFromSeed    :: (MLDSA.MLDSA p, ByteArrayAccess ba)    => proxy p -> MLKeyForm -> ba -> CryptoFailable (PrivKeyMLDSA p)privkeyMLDSAFromSeed p form bs = case MLDSA.keyPairFromSeed p bs of    CryptoFailed e -> CryptoFailed e    CryptoPassed (_, sk) ->        CryptoPassed $ case form of            MLKeySeed -> PrivKeyMLDSA_Seed seed sk            MLKeyExpanded -> PrivKeyMLDSA_Expanded sk            MLKeyBoth -> PrivKeyMLDSA_Both seed sk  where    seed = convert bs :: ScrubbedBytes-- | Build a key from the expanded key alone, which is what a key that was-- generated without keeping its seed has to be.privkeyMLDSAFromKey :: MLDSA.SigningKey p -> PrivKeyMLDSA pprivkeyMLDSAFromKey = PrivKeyMLDSA_Expanded-- | The expanded key, whatever form the key is in.privkeyMLDSA_key :: PrivKeyMLDSA p -> MLDSA.SigningKey pprivkeyMLDSA_key (PrivKeyMLDSA_Seed _ sk) = skprivkeyMLDSA_key (PrivKeyMLDSA_Expanded sk) = skprivkeyMLDSA_key (PrivKeyMLDSA_Both _ sk) = sk-- | The seed, for a key that has one.privkeyMLDSA_seed :: PrivKeyMLDSA p -> Maybe ScrubbedBytesprivkeyMLDSA_seed (PrivKeyMLDSA_Seed s _) = Just sprivkeyMLDSA_seed (PrivKeyMLDSA_Expanded _) = NothingprivkeyMLDSA_seed (PrivKeyMLDSA_Both s _) = Just s-- | The form the key will be written out in.privkeyMLDSA_form :: PrivKeyMLDSA p -> MLKeyFormprivkeyMLDSA_form (PrivKeyMLDSA_Seed _ _) = MLKeySeedprivkeyMLDSA_form (PrivKeyMLDSA_Expanded _) = MLKeyExpandedprivkeyMLDSA_form (PrivKeyMLDSA_Both _ _) = MLKeyBoth-- | An ML-KEM private key, kept in the form it was read as, like-- 'PrivKeyMLDSA'.  The constructors are not exported.data PrivKeyMLKEM p    = PrivKeyMLKEM_Seed ScrubbedBytes (MLKEM.DecapsulationKey p)    | PrivKeyMLKEM_Expanded (MLKEM.DecapsulationKey p)    | PrivKeyMLKEM_Both ScrubbedBytes (MLKEM.DecapsulationKey p)deriving instance MLKEM.MLKEM p => Eq (PrivKeyMLKEM p)showsPrivKeyMLKEM    :: (ScrubbedBytes -> String)    -> (MLKEM.DecapsulationKey p -> String)    -> PrivKeyMLKEM p    -> StringshowsPrivKeyMLKEM seed key k = case k of    PrivKeyMLKEM_Seed s sk -> con "PrivKeyMLKEM_Seed" [seed s, key sk]    PrivKeyMLKEM_Expanded sk -> con "PrivKeyMLKEM_Expanded" [key sk]    PrivKeyMLKEM_Both s sk -> con "PrivKeyMLKEM_Both" [seed s, key sk]  where    con name as = unwords (name : map (\a -> "(" ++ a ++ ")") as)-- | The form is shown; neither the seed nor the key is.instance MLKEM.MLKEM p => Show (PrivKeyMLKEM p) where    show = showsPrivKeyMLKEM (const "<secret>") showinstance MLKEM.MLKEM p => DebugShow (PrivKeyMLKEM p) where    debugShow = showsPrivKeyMLKEM (debugShowBytes "seed") debugShow-- | Build a key from its seed, to be written back out in the given form.privkeyMLKEMFromSeed    :: (MLKEM.MLKEM p, ByteArrayAccess ba)    => proxy p -> MLKeyForm -> ba -> CryptoFailable (PrivKeyMLKEM p)privkeyMLKEMFromSeed p form bs = case MLKEM.keyPairFromSeed p bs of    CryptoFailed e -> CryptoFailed e    CryptoPassed (_, sk) ->        CryptoPassed $ case form of            MLKeySeed -> PrivKeyMLKEM_Seed seed sk            MLKeyExpanded -> PrivKeyMLKEM_Expanded sk            MLKeyBoth -> PrivKeyMLKEM_Both seed sk  where    seed = convert bs :: ScrubbedBytes-- | Build a key from the expanded key alone.privkeyMLKEMFromKey :: MLKEM.DecapsulationKey p -> PrivKeyMLKEM pprivkeyMLKEMFromKey = PrivKeyMLKEM_Expanded-- | The expanded key, whatever form the key is in.privkeyMLKEM_key :: PrivKeyMLKEM p -> MLKEM.DecapsulationKey pprivkeyMLKEM_key (PrivKeyMLKEM_Seed _ sk) = skprivkeyMLKEM_key (PrivKeyMLKEM_Expanded sk) = skprivkeyMLKEM_key (PrivKeyMLKEM_Both _ sk) = sk-- | The seed, for a key that has one.privkeyMLKEM_seed :: PrivKeyMLKEM p -> Maybe ScrubbedBytesprivkeyMLKEM_seed (PrivKeyMLKEM_Seed s _) = Just sprivkeyMLKEM_seed (PrivKeyMLKEM_Expanded _) = NothingprivkeyMLKEM_seed (PrivKeyMLKEM_Both s _) = Just s-- | The form the key will be written out in.privkeyMLKEM_form :: PrivKeyMLKEM p -> MLKeyFormprivkeyMLKEM_form (PrivKeyMLKEM_Seed _ _) = MLKeySeedprivkeyMLKEM_form (PrivKeyMLKEM_Expanded _) = MLKeyExpandedprivkeyMLKEM_form (PrivKeyMLKEM_Both _ _) = MLKeyBoth-- | Private key types known and used in X.509data PrivKey    = -- | RSA private key      PrivKeyRSA RSA.PrivateKey    | -- | RSASSA-PSS private key, restricted to the parameters if any      PrivKeyRSAPSS RSA.PrivateKey (Maybe PSSParams)    | -- | DSA private key      PrivKeyDSA DSA.PrivateKey    | -- | EC private key      PrivKeyEC PrivKeyEC    | -- | X25519 private key      PrivKeyX25519 X25519.SecretKey    | -- | X448 private key      PrivKeyX448 X448.SecretKey    | -- | Ed25519 private key      PrivKeyEd25519 Ed25519.SecretKey    | -- | Ed448 private key      PrivKeyEd448 Ed448.SecretKey    | -- | ML-DSA-44 private key      PrivKeyMLDSA44 (PrivKeyMLDSA MLDSA.MLDSA44)    | -- | ML-DSA-65 private key      PrivKeyMLDSA65 (PrivKeyMLDSA MLDSA.MLDSA65)    | -- | ML-DSA-87 private key      PrivKeyMLDSA87 (PrivKeyMLDSA MLDSA.MLDSA87)    | -- | ML-KEM-512 private key      PrivKeyMLKEM512 (PrivKeyMLKEM MLKEM.MLKEM512)    | -- | ML-KEM-768 private key      PrivKeyMLKEM768 (PrivKeyMLKEM MLKEM.MLKEM768)    | -- | ML-KEM-1024 private key      PrivKeyMLKEM1024 (PrivKeyMLKEM MLKEM.MLKEM1024)    deriving (Show, Eq)-- | Rendering a private key with the key material in it, for the times when-- that is what is meant.  Each arm is @crypton@'s own 'debugShow' for that-- key type, except the EC one, which is this module's.instance DebugShow PrivKey where    debugShow k = case k of        PrivKeyRSA p -> con "PrivKeyRSA" $ debugShow p        PrivKeyRSAPSS p mp -> con "PrivKeyRSAPSS" (debugShow p ++ ") (" ++ show mp)        PrivKeyDSA p -> con "PrivKeyDSA" $ debugShow p        PrivKeyEC p -> con "PrivKeyEC" $ debugShow p        PrivKeyX25519 p -> con "PrivKeyX25519" $ debugShow p        PrivKeyX448 p -> con "PrivKeyX448" $ debugShow p        PrivKeyEd25519 p -> con "PrivKeyEd25519" $ debugShow p        PrivKeyEd448 p -> con "PrivKeyEd448" $ debugShow p        PrivKeyMLDSA44 p -> con "PrivKeyMLDSA44" $ debugShow p        PrivKeyMLDSA65 p -> con "PrivKeyMLDSA65" $ debugShow p        PrivKeyMLDSA87 p -> con "PrivKeyMLDSA87" $ debugShow p        PrivKeyMLKEM512 p -> con "PrivKeyMLKEM512" $ debugShow p        PrivKeyMLKEM768 p -> con "PrivKeyMLKEM768" $ debugShow p        PrivKeyMLKEM1024 p -> con "PrivKeyMLKEM1024" $ debugShow p      where        con name body = name ++ " (" ++ body ++ ")"instance ASN1Object PrivKey where    fromASN1 = privkeyFromASN1    toASN1 = privkeyToASN1privkeyFromASN1 :: [ASN1] -> Either String (PrivKey, [ASN1])privkeyFromASN1 asn1 =    (mapFst PrivKeyRSA <$> rsaFromASN1 asn1)        <!> rsaPSSFromASN1 asn1        <!> (mapFst PrivKeyDSA <$> dsaFromASN1 asn1)        <!> (mapFst PrivKeyEC <$> ecdsaFromASN1 asn1)        <!> newcurveFromASN1 asn1  where    mapFst f (a, b) = (f a, b)    Left _ <!> b = b    a <!> _ = arsaFromASN1 :: [ASN1] -> Either String (RSA.PrivateKey, [ASN1])rsaFromASN1    ( Start Sequence : IntVal 0 : IntVal n : IntVal e : IntVal d            : IntVal p            : IntVal q            : IntVal dP            : IntVal dQ            : IntVal qinv            : End Sequence            : as        ) = pure (key, as)      where        key = RSA.PrivateKey (RSA.PublicKey (go n 1) n e) d p q dP dQ qinv        go m i            | 2 ^ (i * 8) > m = i            | otherwise = go m (i + 1)rsaFromASN1    ( Start Sequence : IntVal 0 : Start Sequence            : OID [1, 2, 840, 113549, 1, 1, 1]            : Null            : End Sequence            : OctetString bytes            : End Sequence            : as        ) = do        asn1 <- mapLeft failure (safeDecode $ decodeASN1' BER bytes)        fmap (const as) <$> rsaFromASN1 asn1      where        failure = ("rsaFromASN1: " ++)rsaFromASN1 _ = Left "rsaFromASN1: unexpected format"-- | PKCS #8 RSASSA-PSS key (RFC 4055).rsaPSSFromASN1 :: [ASN1] -> Either String (PrivKey, [ASN1])rsaPSSFromASN1 (Start Sequence : IntVal 0 : Start Sequence : OID oid : xs)    | oid == getObjectID PubKeyALG_RSAPSS = do        (mp, ys) <- pssKeyParamsFromASN1 xs        case ys of            OctetString bytes : End Sequence : as -> do                asn1 <- mapLeft ("rsaPSSFromASN1: " ++) (safeDecode $ decodeASN1' BER bytes)                case rsaFromASN1 asn1 of                    Right (k, []) -> Right (PrivKeyRSAPSS k mp, as)                    Right _ -> Left "rsaPSSFromASN1: trailing data"                    Left err -> Left err            _ -> Left "rsaPSSFromASN1: unexpected format"rsaPSSFromASN1 _ = Left "rsaPSSFromASN1: unexpected format"dsaFromASN1 :: [ASN1] -> Either String (DSA.PrivateKey, [ASN1])dsaFromASN1    ( Start Sequence : IntVal 0 : IntVal p : IntVal q : IntVal g            : IntVal _            : IntVal x            : End Sequence            : as        ) =        pure (DSA.PrivateKey (DSA.Params p g q) x, as)dsaFromASN1    ( Start Sequence : IntVal 0 : Start Sequence            : OID [1, 2, 840, 10040, 4, 1]            : Start Sequence            : IntVal p            : IntVal q            : IntVal g            : End Sequence            : End Sequence            : OctetString bytes            : End Sequence            : as        ) = case safeDecode $ decodeASN1' BER bytes of        Right [IntVal x] -> pure (DSA.PrivateKey (DSA.Params p g q) x, as)        Right _ -> Left "DSA.PrivateKey.fromASN1: unexpected format"        Left e -> Left $ "DSA.PrivateKey.fromASN1: " ++ edsaFromASN1 _ = Left "DSA.PrivateKey.fromASN1: unexpected format"ecdsaFromASN1 :: [ASN1] -> Either String (PrivKeyEC, [ASN1])ecdsaFromASN1 = go []  where    failing = ("ECDSA.PrivateKey.fromASN1: " ++)    go acc (Start Sequence : IntVal 1 : OctetString bytes : rest) = do        key <- subgo (oid ++ acc)        case rest'' of            End Sequence : rest''' -> pure (key, rest''')            _ -> Left $ failing "unexpected EC format"      where        d = os2ip bytes        (oid, rest') = spanTag 0 rest        (_, rest'') = spanTag 1 rest'        subgo (OID oid_ : _) = maybe failure success mcurve          where            failure = Left $ failing $ "unknown curve " ++ show oid_            success = Right . flip PrivKeyEC_Named d            mcurve = lookupByOID curvesOIDTable oid_        subgo            ( Start Sequence : IntVal 1 : Start Sequence                    : OID [1, 2, 840, 10045, 1, 1]                    : IntVal p                    : End Sequence                    : Start Sequence                    : OctetString a                    : OctetString b                    : BitString s                    : End Sequence                    : OctetString g                    : IntVal o                    : IntVal c                    : End Sequence                    : _                ) =                pure $ PrivKeyEC_Prime d a' b' p g' o c s'              where                a' = os2ip a                b' = os2ip b                g' = SerializedPoint g                s' = os2ip $ bitArrayGetData s        subgo (Null : rest_) = subgo rest_        subgo [] = Left $ failing "curve is missing"        subgo _ = Left $ failing "unexpected curve format"    go        acc        ( Start Sequence : IntVal 0 : Start Sequence                : OID [1, 2, 840, 10045, 2, 1]                : rest            ) = case rest' of            (OctetString bytes : rest'') -> do                asn1 <- mapLeft failing (safeDecode $ decodeASN1' BER bytes)                fmap (const rest'') <$> go (oid ++ acc) asn1            _ -> Left $ failing "unexpected EC format"          where            (oid, rest') = spanEnd 0 rest    go _ _ = Left $ failing "unexpected EC format"    spanEnd :: Word -> [ASN1] -> ([ASN1], [ASN1])    spanEnd = loop id      where        loop dlist n (a@(Start _) : as) = loop (dlist . (a :)) (n + 1) as        loop dlist 0 (End _ : as) = (dlist [], as)        loop dlist n (a@(End _) : as) = loop (dlist . (a :)) (n - 1) as        loop dlist n (a : as) = loop (dlist . (a :)) n as        loop dlist _ [] = (dlist [], [])    spanTag :: Int -> [ASN1] -> ([ASN1], [ASN1])    spanTag a (Start (Container _ b) : as) | a == b = spanEnd 0 as    spanTag _ as = ([], as)newcurveFromASN1 :: [ASN1] -> Either String (PrivKey, [ASN1])newcurveFromASN1    ( Start Sequence            : IntVal v            : Start Sequence            : OID oid            : End Sequence            : OctetString bs            : xs        )        | isValidVersion v = do            let (_, ys) = containerWithTag 0 xs            case primitiveWithTag 1 ys of                (_, End Sequence : zs) ->                    case getP oid of                        Just (name, parse) -> do                            let err s = Left (name ++ ".SecretKey.fromASN1: " ++ s)                            case safeDecode $ decodeASN1' BER bs of                                Right [OctetString key] ->                                    case parse key of                                        CryptoPassed s -> Right (s, zs)                                        CryptoFailed e -> err ("invalid secret key: " ++ show e)                                Right _ -> err "unexpected inner format"                                Left e -> err e                        Nothing -> case getMLDSA oid of                            Just (name, parse) ->                                case safeDecode $ decodeASN1' BER bs of                                    Right inner -> (\k -> (k, zs)) <$> parse inner                                    Left e -> Left (name ++ ".SigningKey.fromASN1: " ++ e)                            Nothing -> Left ("newcurveFromASN1: unexpected OID " ++ show oid)                _ -> Left "newcurveFromASN1: unexpected end format"        | otherwise = Left ("newcurveFromASN1: unexpected version: " ++ show v)      where        getP [1, 3, 101, 110] = Just ("X25519", fmap PrivKeyX25519 . X25519.secretKey)        getP [1, 3, 101, 111] = Just ("X448", fmap PrivKeyX448 . X448.secretKey)        getP [1, 3, 101, 112] = Just ("Ed25519", fmap PrivKeyEd25519 . Ed25519.secretKey)        getP [1, 3, 101, 113] = Just ("Ed448", fmap PrivKeyEd448 . Ed448.secretKey)        getP _ = Nothing        getMLDSA [2, 16, 840, 1, 101, 3, 4, 3, 17] =            Just                ( "ML-DSA-44"                , mldsaFromASN1 (Proxy :: Proxy MLDSA.MLDSA44) PrivKeyMLDSA44 "ML-DSA-44"                )        getMLDSA [2, 16, 840, 1, 101, 3, 4, 3, 18] =            Just                ( "ML-DSA-65"                , mldsaFromASN1 (Proxy :: Proxy MLDSA.MLDSA65) PrivKeyMLDSA65 "ML-DSA-65"                )        getMLDSA [2, 16, 840, 1, 101, 3, 4, 3, 19] =            Just                ( "ML-DSA-87"                , mldsaFromASN1 (Proxy :: Proxy MLDSA.MLDSA87) PrivKeyMLDSA87 "ML-DSA-87"                )        getMLDSA [2, 16, 840, 1, 101, 3, 4, 4, 1] =            Just                ( "ML-KEM-512"                , mlkemFromASN1 (Proxy :: Proxy MLKEM.MLKEM512) PrivKeyMLKEM512 "ML-KEM-512"                )        getMLDSA [2, 16, 840, 1, 101, 3, 4, 4, 2] =            Just                ( "ML-KEM-768"                , mlkemFromASN1 (Proxy :: Proxy MLKEM.MLKEM768) PrivKeyMLKEM768 "ML-KEM-768"                )        getMLDSA [2, 16, 840, 1, 101, 3, 4, 4, 3] =            Just                ( "ML-KEM-1024"                , mlkemFromASN1 (Proxy :: Proxy MLKEM.MLKEM1024) PrivKeyMLKEM1024 "ML-KEM-1024"                )        getMLDSA _ = Nothing        isValidVersion version = version >= 0 && version <= 1newcurveFromASN1 _ =    Left "newcurveFromASN1: unexpected format"-- | The private key of RFC 9881 Section 6: a CHOICE of the seed, tagged-- [0], the expanded key, or both.  A seed is expanded into the key; with-- both, the expanded key must be what the seed expands to.  Which of the-- three it was is kept, so that 'mldsaToASN1' writes back what was read.mldsaFromASN1    :: MLDSA.MLDSA p    => proxy p    -> (PrivKeyMLDSA p -> PrivKey)    -> String    -> [ASN1]    -> Either String PrivKeymldsaFromASN1 p con name inner = case inner of    [Other Context 0 seed] -> con <$> fromSeed MLKeySeed seed    [OctetString expanded] -> con . privkeyMLDSAFromKey <$> fromExpanded expanded    [Start Sequence, OctetString seed, OctetString expanded, End Sequence] -> do        k <- fromSeed MLKeyBoth seed        sk <- fromExpanded expanded        if privkeyMLDSA_key k == sk            then Right (con k)            else err "seed and expandedKey do not match"    _ -> err "unexpected inner format"  where    err s = Left (name ++ ".SigningKey.fromASN1: " ++ s)    fromSeed form seed = case privkeyMLDSAFromSeed p form seed of        CryptoPassed k -> Right k        CryptoFailed e -> err ("invalid seed: " ++ show e)    fromExpanded expanded = case MLDSA.signingKey expanded of        CryptoPassed sk -> Right sk        CryptoFailed e -> err ("invalid expandedKey: " ++ show e)-- | The private key of RFC 9935 Section 6, in the same three forms as-- 'mldsaFromASN1'.mlkemFromASN1    :: MLKEM.MLKEM p    => proxy p    -> (PrivKeyMLKEM p -> PrivKey)    -> String    -> [ASN1]    -> Either String PrivKeymlkemFromASN1 p con name inner = case inner of    [Other Context 0 seed] -> con <$> fromSeed MLKeySeed seed    [OctetString expanded] -> con . privkeyMLKEMFromKey <$> fromExpanded expanded    [Start Sequence, OctetString seed, OctetString expanded, End Sequence] -> do        k <- fromSeed MLKeyBoth seed        sk <- fromExpanded expanded        if privkeyMLKEM_key k == sk            then Right (con k)            else err "seed and expandedKey do not match"    _ -> err "unexpected inner format"  where    err s = Left (name ++ ".DecapsulationKey.fromASN1: " ++ s)    fromSeed form seed = case privkeyMLKEMFromSeed p form seed of        CryptoPassed k -> Right k        CryptoFailed e -> err ("invalid seed: " ++ show e)    fromExpanded expanded = case MLKEM.decapsulationKey expanded of        CryptoPassed sk -> Right sk        CryptoFailed e -> err ("invalid expandedKey: " ++ show e)containerWithTag :: ASN1Tag -> [ASN1] -> ([ASN1], [ASN1])containerWithTag etag (Start (Container _ atag) : xs)    | etag == atag = getConstructedEnd 0 xscontainerWithTag _ xs = ([], xs)primitiveWithTag :: ASN1Tag -> [ASN1] -> (Maybe B.ByteString, [ASN1])primitiveWithTag etag (Other _ atag bs : xs)    | etag == atag = (Just bs, xs)primitiveWithTag _ xs = (Nothing, xs)privkeyToASN1 :: PrivKey -> ASN1SprivkeyToASN1 (PrivKeyRSA rsa) = rsaToASN1 rsaprivkeyToASN1 (PrivKeyRSAPSS rsa mp) =    (++) $        Start Sequence            : IntVal 0            : pssKeyAlgToASN1 mp [OctetString (encodeASN1' DER (rsaToASN1 rsa [])), End Sequence]privkeyToASN1 (PrivKeyDSA dsa) = dsaToASN1 dsaprivkeyToASN1 (PrivKeyEC ecdsa) = ecdsaToASN1 ecdsaprivkeyToASN1 (PrivKeyX25519 k) = newcurveToASN1 [1, 3, 101, 110] kprivkeyToASN1 (PrivKeyX448 k) = newcurveToASN1 [1, 3, 101, 111] kprivkeyToASN1 (PrivKeyEd25519 k) = newcurveToASN1 [1, 3, 101, 112] kprivkeyToASN1 (PrivKeyEd448 k) = newcurveToASN1 [1, 3, 101, 113] kprivkeyToASN1 (PrivKeyMLDSA44 k) = mldsaToASN1 (getObjectID PubKeyALG_MLDSA44) kprivkeyToASN1 (PrivKeyMLDSA65 k) = mldsaToASN1 (getObjectID PubKeyALG_MLDSA65) kprivkeyToASN1 (PrivKeyMLDSA87 k) = mldsaToASN1 (getObjectID PubKeyALG_MLDSA87) kprivkeyToASN1 (PrivKeyMLKEM512 k) = mlkemToASN1 (getObjectID PubKeyALG_MLKEM512) kprivkeyToASN1 (PrivKeyMLKEM768 k) = mlkemToASN1 (getObjectID PubKeyALG_MLKEM768) kprivkeyToASN1 (PrivKeyMLKEM1024 k) = mlkemToASN1 (getObjectID PubKeyALG_MLKEM1024) krsaToASN1 :: RSA.PrivateKey -> ASN1SrsaToASN1 key =    (++)        [ Start Sequence        , IntVal 0        , IntVal n        , IntVal e        , IntVal d        , IntVal p        , IntVal q        , IntVal dP        , IntVal dQ        , IntVal qinv        , End Sequence        ]  where    RSA.PrivateKey (RSA.PublicKey _ n e) d p q dP dQ qinv = keydsaToASN1 :: DSA.PrivateKey -> ASN1SdsaToASN1 (DSA.PrivateKey params@(DSA.Params p g q) y) =    (++)        [ Start Sequence        , IntVal 0        , IntVal p        , IntVal q        , IntVal g        , IntVal x        , IntVal y        , End Sequence        ]  where    x = DSA.calculatePublic params yecdsaToASN1 :: PrivKeyEC -> ASN1S-- RFC 5915: the private key is as long as the curve order.ecdsaToASN1 (PrivKeyEC_Named curveName d) =    (++)        [ Start Sequence        , IntVal 1        , OctetString (i2ospOf_ (numBytes order) d)        , Start (Container Context 0)        , OID oid        , End (Container Context 0)        , End Sequence        ]  where    order = ECC.ecc_n $ ECC.common_curve $ ECC.getCurveByName curveName    err = error . ("ECDSA.PrivateKey.toASN1: " ++)    oid =        fromMaybe            (err $ "missing named curve " ++ show curveName)            (lookupOID curvesOIDTable curveName)ecdsaToASN1 (PrivKeyEC_Prime d a b p g o c s) =    (++)        [ Start Sequence        , IntVal 1        , OctetString (i2ospOf_ (numBytes o) d)        , Start (Container Context 0)        , Start Sequence        , IntVal 1        , Start Sequence        , OID [1, 2, 840, 10045, 1, 1]        , IntVal p        , End Sequence        , Start Sequence        , OctetString a'        , OctetString b'        , BitString s'        , End Sequence        , OctetString g'        , IntVal o        , IntVal c        , End Sequence        , End (Container Context 0)        , End Sequence        ]  where    a' = i2osp a    b' = i2osp b    SerializedPoint g' = g    s' = BitArray (8 * fromIntegral (B.length bytes)) bytes      where        bytes = i2osp snewcurveToASN1 :: ByteArrayAccess key => OID -> key -> ASN1SnewcurveToASN1 oid key =    (++)        [ Start Sequence        , IntVal 0        , Start Sequence        , OID oid        , End Sequence        , OctetString (encodeASN1' DER [OctetString $ convert key])        , End Sequence        ]-- | The inverse of 'mldsaFromASN1': the form the key is in is the form it-- is written in, so reading a key and writing it again gives back the-- bytes it came from.mldsaToASN1 :: OID -> PrivKeyMLDSA p -> ASN1SmldsaToASN1 oid k =    (++)        [ Start Sequence        , IntVal 0        , Start Sequence        , OID oid        , End Sequence        , OctetString (encodeASN1' DER inner)        , End Sequence        ]  where    inner = case k of        PrivKeyMLDSA_Seed s _ -> [Other Context 0 (convert s)]        PrivKeyMLDSA_Expanded sk -> [OctetString (convert sk)]        PrivKeyMLDSA_Both s sk ->            [ Start Sequence            , OctetString (convert s)            , OctetString (convert sk)            , End Sequence            ]-- | The inverse of 'mlkemFromASN1'.mlkemToASN1 :: MLKEM.MLKEM p => OID -> PrivKeyMLKEM p -> ASN1SmlkemToASN1 oid k =    (++)        [ Start Sequence        , IntVal 0        , Start Sequence        , OID oid        , End Sequence        , OctetString (encodeASN1' DER inner)        , End Sequence        ]  where    inner = case k of        PrivKeyMLKEM_Seed s _ -> [Other Context 0 (convert s)]        PrivKeyMLKEM_Expanded sk -> [OctetString (convert sk)]        PrivKeyMLKEM_Both s sk ->            [ Start Sequence            , OctetString (convert s)            , OctetString (convert sk)            , End Sequence            ]mapLeft :: (a0 -> a1) -> Either a0 b -> Either a1 bmapLeft f (Left x) = Left (f x)mapLeft _ (Right x) = Right x-- | Convert a Private key to the Public Key Algorithm typeprivkeyToAlg :: PrivKey -> PubKeyALGprivkeyToAlg (PrivKeyRSA _) = PubKeyALG_RSAprivkeyToAlg (PrivKeyRSAPSS _ _) = PubKeyALG_RSAPSSprivkeyToAlg (PrivKeyDSA _) = PubKeyALG_DSAprivkeyToAlg (PrivKeyEC _) = PubKeyALG_ECprivkeyToAlg (PrivKeyX25519 _) = PubKeyALG_X25519privkeyToAlg (PrivKeyX448 _) = PubKeyALG_X448privkeyToAlg (PrivKeyEd25519 _) = PubKeyALG_Ed25519privkeyToAlg (PrivKeyEd448 _) = PubKeyALG_Ed448privkeyToAlg (PrivKeyMLDSA44 _) = PubKeyALG_MLDSA44privkeyToAlg (PrivKeyMLDSA65 _) = PubKeyALG_MLDSA65privkeyToAlg (PrivKeyMLDSA87 _) = PubKeyALG_MLDSA87privkeyToAlg (PrivKeyMLKEM512 _) = PubKeyALG_MLKEM512privkeyToAlg (PrivKeyMLKEM768 _) = PubKeyALG_MLKEM768privkeyToAlg (PrivKeyMLKEM1024 _) = PubKeyALG_MLKEM1024