|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE FlexibleInstances #-} |
| 3 | +{-# LANGUAGE StandaloneDeriving #-} |
| 4 | +{-# LANGUAGE TypeFamilies #-} |
| 5 | +{-# LANGUAGE TypeOperators #-} |
| 6 | + |
| 7 | +-- | Encapsulates signature validation utilities leveraged by the mempool writer |
| 8 | +-- |
| 9 | +module DMQ.Protocol.SigSubmission.Validate where |
| 10 | + |
| 11 | +import Control.Exception |
| 12 | +import Control.Monad.Class.MonadTime.SI |
| 13 | +import Control.Monad.Trans.Except |
| 14 | +import Control.Monad.Trans.Except.Extra |
| 15 | +import Data.ByteString (ByteString) |
| 16 | +import Data.ByteString.Lazy qualified as LBS |
| 17 | +import Data.Map.Strict qualified as Map |
| 18 | +import Data.Maybe (isNothing) |
| 19 | +import Data.Text (Text) |
| 20 | +import Data.Text qualified as Text |
| 21 | +import Data.Typeable |
| 22 | +import Data.Word |
| 23 | +import Text.Printf |
| 24 | + |
| 25 | +import Cardano.Crypto.DSIGN.Class (ContextDSIGN) |
| 26 | +import Cardano.Crypto.DSIGN.Class qualified as DSIGN |
| 27 | +import Cardano.Crypto.KES.Class (KESAlgorithm (..)) |
| 28 | +import Cardano.KESAgent.KES.Crypto as KES |
| 29 | +import Cardano.KESAgent.KES.OCert (OCert (..), OCertSignable, validateOCert) |
| 30 | +import Cardano.Ledger.BaseTypes.NonZero |
| 31 | +import Cardano.Ledger.Hashes |
| 32 | + |
| 33 | +import DMQ.Diffusion.NodeKernel (PoolValidationCtx (..)) |
| 34 | +import DMQ.Protocol.SigSubmission.Type |
| 35 | +import Ouroboros.Consensus.Shelley.Ledger.Query |
| 36 | +import Ouroboros.Network.TxSubmission.Mempool.Simple |
| 37 | +import Ouroboros.Network.Util.ShowProxy |
| 38 | + |
| 39 | + |
| 40 | +-- | The type of non-fatal failures reported by the mempool writer |
| 41 | +-- for invalid messages |
| 42 | +-- |
| 43 | +data instance MempoolAddFail (Sig crypto) = |
| 44 | + SigInvalid Text |
| 45 | + | SigDuplicate |
| 46 | + | SigExpired |
| 47 | + | SigResultOther Text |
| 48 | + deriving (Eq, Show) |
| 49 | + |
| 50 | +instance (Typeable crypto) => ShowProxy (MempoolAddFail (Sig crypto)) |
| 51 | + |
| 52 | +-- | The type of exception raised by the mempool writer for invalid messages |
| 53 | +-- as determined by the validation procedure and severity policy |
| 54 | +-- |
| 55 | +newtype instance InvalidTxsError SigValidationError = InvalidTxsError SigValidationError |
| 56 | + |
| 57 | +deriving instance Show (InvalidTxsError SigValidationError) |
| 58 | +instance Exception (InvalidTxsError SigValidationError) |
| 59 | + |
| 60 | +-- | The policy which is realized by the mempool writer when encountering |
| 61 | +-- an invalid message. |
| 62 | +-- |
| 63 | +data ValidationSeverity = |
| 64 | + FailDefault | FailSoft |
| 65 | + |
| 66 | +data SigValidationError = |
| 67 | + InvalidKESSignature KESPeriod KESPeriod String |
| 68 | + | InvalidSignatureOCERT |
| 69 | + !Word64 -- OCert counter |
| 70 | + !KESPeriod -- OCert KES period |
| 71 | + !String -- DSIGN error message |
| 72 | + | KESBeforeStartOCERT KESPeriod KESPeriod |
| 73 | + | KESAfterEndOCERT KESPeriod KESPeriod |
| 74 | + | UnrecognizedPool |
| 75 | + | NotInitialized |
| 76 | + | ClockSkew |
| 77 | + deriving Show |
| 78 | + |
| 79 | +-- TODO fine tune policy |
| 80 | +sigValidationPolicy |
| 81 | + :: SigValidationError |
| 82 | + -> Either (MempoolAddFail (Sig crypto)) (MempoolAddFail (Sig crypto)) |
| 83 | +sigValidationPolicy sve = case sve of |
| 84 | + InvalidKESSignature {} -> Left . SigInvalid . Text.pack . show $ sve |
| 85 | + InvalidSignatureOCERT {} -> Left . SigInvalid . Text.pack . show $ sve |
| 86 | + KESAfterEndOCERT {} -> Left SigExpired |
| 87 | + KESBeforeStartOCERT start sig -> |
| 88 | + Left . SigResultOther . Text.pack |
| 89 | + $ printf "KESBeforeStartOCERT %s %s" (show start) (show sig) |
| 90 | + UnrecognizedPool -> Left . SigInvalid $ Text.pack "unrecognized pool id" |
| 91 | + ClockSkew -> Left . SigInvalid $ Text.pack "clock skew out of range" |
| 92 | + NotInitialized -> Right . SigResultOther $ Text.pack "not initialized yet" |
| 93 | + |
| 94 | +-- TODO: |
| 95 | +-- We don't validate ocert numbers, since we might not have necessary |
| 96 | +-- information to do so, but we can validate that they are growing. |
| 97 | +validateSig :: forall crypto. |
| 98 | + ( Crypto crypto |
| 99 | + , ContextDSIGN (KES.DSIGN crypto) ~ () |
| 100 | + , DSIGN.Signable (DSIGN crypto) (OCertSignable crypto) |
| 101 | + , ContextKES (KES crypto) ~ () |
| 102 | + , Signable (KES crypto) ByteString |
| 103 | + ) |
| 104 | + => ValidationSeverity |
| 105 | + -> (DSIGN.VerKeyDSIGN (DSIGN crypto) -> KeyHash StakePool) |
| 106 | + -> [Sig crypto] |
| 107 | + -> PoolValidationCtx |
| 108 | + -- ^ cardano pool id verification |
| 109 | + -> Except (InvalidTxsError SigValidationError) [Either (MempoolAddFail (Sig crypto)) ()] |
| 110 | +validateSig severity verKeyHashingFn sigs ctx = firstExceptT InvalidTxsError $ traverse process sigs |
| 111 | + where |
| 112 | + DMQPoolValidationCtx now mNextEpoch pools = ctx |
| 113 | + |
| 114 | + process Sig { sigSignedBytes = signedBytes, |
| 115 | + sigKESPeriod, |
| 116 | + sigOpCertificate = SigOpCertificate ocert@OCert { |
| 117 | + ocertKESPeriod, |
| 118 | + ocertVkHot, |
| 119 | + ocertN |
| 120 | + }, |
| 121 | + sigColdKey = SigColdKey coldKey, |
| 122 | + sigKESSignature = SigKESSignature kesSig |
| 123 | + } = do |
| 124 | + e1 <- sigKESPeriod < endKESPeriod |
| 125 | + ?! KESAfterEndOCERT endKESPeriod sigKESPeriod |
| 126 | + e2 <- sigKESPeriod >= startKESPeriod |
| 127 | + ?! KESBeforeStartOCERT startKESPeriod sigKESPeriod |
| 128 | + e3 <- case Map.lookup (verKeyHashingFn coldKey) pools of |
| 129 | + Nothing | isNothing mNextEpoch -> classifyError NotInitialized |
| 130 | + | otherwise -> classifyError UnrecognizedPool |
| 131 | + Just ss | not (isZero (ssSetPool ss)) -> right $ Right () |
| 132 | + | not (isZero (ssMarkPool ss)) |
| 133 | + , Just nextEpoch <- mNextEpoch |
| 134 | + -- TODO make this a constant |
| 135 | + , diffUTCTime nextEpoch now <= 5 -> right $ Right () |
| 136 | + | otherwise -> classifyError ClockSkew |
| 137 | + -- validate OCert, which includes verifying its signature |
| 138 | + e4 <- validateOCert coldKey ocertVkHot ocert |
| 139 | + ?!: InvalidSignatureOCERT ocertN sigKESPeriod |
| 140 | + -- validate KES signature of the payload |
| 141 | + e5 <- verifyKES () ocertVkHot |
| 142 | + (unKESPeriod sigKESPeriod - unKESPeriod startKESPeriod) |
| 143 | + (LBS.toStrict signedBytes) |
| 144 | + kesSig |
| 145 | + ?!: InvalidKESSignature ocertKESPeriod sigKESPeriod |
| 146 | + -- for eg. remember to run all results with possibly non-fatal errors |
| 147 | + right $ e1 >> e2 >> e3 >> e4 >> e5 |
| 148 | + where |
| 149 | + startKESPeriod, endKESPeriod :: KESPeriod |
| 150 | + |
| 151 | + startKESPeriod = ocertKESPeriod |
| 152 | + -- TODO: is `totalPeriodsKES` the same as `praosMaxKESEvo` |
| 153 | + -- or `sgMaxKESEvolution` in the genesis file? |
| 154 | + endKESPeriod = KESPeriod $ unKESPeriod startKESPeriod |
| 155 | + + totalPeriodsKES (Proxy :: Proxy (KES crypto)) |
| 156 | + |
| 157 | + classifyError sigValidationError = case severity of |
| 158 | + FailSoft -> |
| 159 | + let mempoolAddFail = either id id (sigValidationPolicy sigValidationError) |
| 160 | + in right . Left $ mempoolAddFail |
| 161 | + FailDefault -> |
| 162 | + either (const $ throwE sigValidationError) (right . Left) |
| 163 | + (sigValidationPolicy sigValidationError) |
| 164 | + |
| 165 | + (?!:) :: Either e1 () |
| 166 | + -> (e1 -> SigValidationError) |
| 167 | + -> Except SigValidationError (Either (MempoolAddFail (Sig crypto)) ()) |
| 168 | + (?!:) = (handleE classifyError .) . flip firstExceptT . hoistEither . fmap Right |
| 169 | + |
| 170 | + (?!) :: Bool |
| 171 | + -> SigValidationError |
| 172 | + -> Except SigValidationError (Either (MempoolAddFail (Sig crypto)) ()) |
| 173 | + (?!) flag sve = if flag then right $ Right () else classifyError sve |
| 174 | + |
| 175 | + infix 1 ?! |
| 176 | + infix 1 ?!: |
0 commit comments