bd5affa362
This deals with the possible security problem that someone could make an unusually low UUID and generate keys that are all constructed to hash to a number that, mod the number of repositories in the group, == 0. So balanced preferred content would always put those keys in the repository with the low UUID as long as the group contains the number of repositories that the attacker anticipated. Presumably the attacker than holds the data for ransom? Dunno. Anyway, the partial solution is to use HMAC (sha256) with all the UUIDs combined together as the "secret", and the key as the "message". Now any change in the set of UUIDs in a group will invalidate the attacker's constructed keys from hashing to anything in particular. Given that there are plenty of other things someone can do if they can write to the repository -- including modifying preferred content so only their repository wants files, and numcopies so other repositories drom them -- this seems like safeguard enough. Note that, in balancedPicker, combineduuids is memoized.
46 lines
1.1 KiB
Haskell
46 lines
1.1 KiB
Haskell
{- values verified using a shared secret
|
|
-
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
|
-
|
|
- License: BSD-2-clause
|
|
-}
|
|
|
|
module Utility.Verifiable (
|
|
Secret,
|
|
HMACDigest,
|
|
Verifiable(..),
|
|
mkVerifiable,
|
|
verify,
|
|
prop_verifiable_sane,
|
|
) where
|
|
|
|
import Data.ByteString.UTF8 (fromString)
|
|
import qualified Data.ByteString as S
|
|
|
|
import Utility.Hash
|
|
import Utility.QuickCheck
|
|
|
|
type Secret = S.ByteString
|
|
type HMACDigest = String
|
|
|
|
{- A value, verifiable using a HMAC digest and a secret. -}
|
|
data Verifiable a = Verifiable
|
|
{ verifiableVal :: a
|
|
, verifiableDigest :: HMACDigest
|
|
}
|
|
deriving (Eq, Read, Show)
|
|
|
|
mkVerifiable :: Show a => a -> Secret -> Verifiable a
|
|
mkVerifiable a secret = Verifiable a (calcDigest (show a) secret)
|
|
|
|
verify :: (Eq a, Show a) => Verifiable a -> Secret -> Bool
|
|
verify v secret = v == mkVerifiable (verifiableVal v) secret
|
|
|
|
calcDigest :: String -> Secret -> HMACDigest
|
|
calcDigest v secret = calcMac show HmacSha1 secret (fromString v)
|
|
|
|
prop_verifiable_sane :: TestableString -> TestableString -> Bool
|
|
prop_verifiable_sane v ts =
|
|
verify (mkVerifiable (fromTestableString v) secret) secret
|
|
where
|
|
secret = fromString (fromTestableString ts)
|