2011-04-15 22:18:39 +00:00
|
|
|
{- git-annex crypto
|
2011-04-16 17:25:27 +00:00
|
|
|
-
|
|
|
|
- Currently using gpg; could later be modified to support different
|
|
|
|
- crypto backends if neccessary.
|
2011-04-15 22:18:39 +00:00
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Crypto (
|
2011-04-16 22:22:52 +00:00
|
|
|
Cipher,
|
|
|
|
EncryptedCipher,
|
2011-04-15 22:18:39 +00:00
|
|
|
genCipher,
|
|
|
|
updateCipher,
|
2011-04-17 22:18:27 +00:00
|
|
|
describeCipher,
|
2011-04-15 22:18:39 +00:00
|
|
|
storeCipher,
|
|
|
|
extractCipher,
|
|
|
|
decryptCipher,
|
|
|
|
encryptKey,
|
2011-04-17 17:11:38 +00:00
|
|
|
withEncryptedHandle,
|
|
|
|
withDecryptedHandle,
|
2011-04-16 20:26:47 +00:00
|
|
|
withEncryptedContent,
|
|
|
|
withDecryptedContent,
|
2011-04-21 20:56:24 +00:00
|
|
|
|
|
|
|
prop_hmacWithCipher_sane
|
2011-04-15 22:18:39 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
|
|
|
import qualified Data.Map as M
|
2011-04-16 17:25:27 +00:00
|
|
|
import qualified Codec.Binary.Base64 as B64
|
2011-04-26 15:24:23 +00:00
|
|
|
import Data.ByteString.Lazy.UTF8 (fromString)
|
|
|
|
import Data.Digest.Pure.SHA
|
|
|
|
import System.Cmd.Utils
|
2011-04-16 17:25:27 +00:00
|
|
|
import Data.String.Utils
|
|
|
|
import Data.List
|
|
|
|
import Data.Bits.Utils
|
2011-04-16 20:26:47 +00:00
|
|
|
import System.IO
|
|
|
|
import System.Posix.IO
|
|
|
|
import System.Posix.Types
|
2011-04-17 04:34:38 +00:00
|
|
|
import System.Posix.Process
|
2011-04-21 20:37:14 +00:00
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception (finally)
|
2011-04-26 15:24:23 +00:00
|
|
|
import System.Exit
|
|
|
|
import System.Environment
|
2011-04-15 22:18:39 +00:00
|
|
|
|
|
|
|
import Types
|
2011-04-16 20:26:47 +00:00
|
|
|
import Key
|
2011-04-15 22:18:39 +00:00
|
|
|
import RemoteClass
|
|
|
|
import Utility
|
2011-04-16 20:41:46 +00:00
|
|
|
import CryptoTypes
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-17 05:34:28 +00:00
|
|
|
{- The first half of a Cipher is used for HMAC; the remainder
|
|
|
|
- is used as the GPG symmetric encryption passphrase.
|
|
|
|
-
|
2011-04-17 15:13:54 +00:00
|
|
|
- HMAC SHA1 needs only 64 bytes. The remainder is for expansion,
|
|
|
|
- perhaps to HMAC SHA512, which needs 128 bytes (ideally).
|
|
|
|
-
|
2011-04-17 05:34:28 +00:00
|
|
|
- 256 is enough for gpg's symetric cipher; unlike weaker public key
|
|
|
|
- crypto, the key does not need to be too large.
|
|
|
|
-}
|
|
|
|
cipherHalf :: Int
|
|
|
|
cipherHalf = 256
|
|
|
|
|
|
|
|
cipherSize :: Int
|
|
|
|
cipherSize = cipherHalf * 2
|
|
|
|
|
|
|
|
cipherPassphrase :: Cipher -> String
|
|
|
|
cipherPassphrase (Cipher c) = drop cipherHalf c
|
|
|
|
|
|
|
|
cipherHmac :: Cipher -> String
|
|
|
|
cipherHmac (Cipher c) = take cipherHalf c
|
|
|
|
|
2011-04-15 22:18:39 +00:00
|
|
|
{- Creates a new Cipher, encrypted as specified in the remote's configuration -}
|
|
|
|
genCipher :: RemoteConfig -> IO EncryptedCipher
|
2011-04-16 17:25:27 +00:00
|
|
|
genCipher c = do
|
|
|
|
ks <- configKeyIds c
|
2011-04-15 22:18:39 +00:00
|
|
|
random <- genrandom
|
2011-04-16 17:25:27 +00:00
|
|
|
encryptCipher (Cipher random) ks
|
2011-04-15 22:18:39 +00:00
|
|
|
where
|
2011-04-16 20:26:47 +00:00
|
|
|
genrandom = gpgRead
|
|
|
|
-- Armor the random data, to avoid newlines,
|
|
|
|
-- since gpg only reads ciphers up to the first
|
|
|
|
-- newline.
|
|
|
|
[ Params "--gen-random --armor"
|
2011-04-15 22:18:39 +00:00
|
|
|
, Param $ show randomquality
|
2011-04-17 05:34:28 +00:00
|
|
|
, Param $ show cipherSize
|
2011-04-15 22:18:39 +00:00
|
|
|
]
|
2011-04-17 05:34:28 +00:00
|
|
|
-- 1 is /dev/urandom; 2 is /dev/random
|
|
|
|
randomquality = 1 :: Int
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-16 17:25:27 +00:00
|
|
|
{- Updates an existing Cipher, re-encrypting it to add KeyIds specified in
|
|
|
|
- the remote's configuration. -}
|
2011-04-15 22:18:39 +00:00
|
|
|
updateCipher :: RemoteConfig -> EncryptedCipher -> IO EncryptedCipher
|
2011-04-16 17:25:27 +00:00
|
|
|
updateCipher c encipher@(EncryptedCipher _ ks) = do
|
|
|
|
ks' <- configKeyIds c
|
|
|
|
cipher <- decryptCipher c encipher
|
|
|
|
encryptCipher cipher (combine ks ks')
|
|
|
|
where
|
|
|
|
combine (KeyIds a) (KeyIds b) = KeyIds $ a ++ b
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-17 22:18:27 +00:00
|
|
|
describeCipher :: EncryptedCipher -> String
|
|
|
|
describeCipher (EncryptedCipher _ (KeyIds ks)) =
|
|
|
|
"with gpg " ++ keys ks ++ " " ++ unwords ks
|
|
|
|
where
|
|
|
|
keys [_] = "key"
|
|
|
|
keys _ = "keys"
|
|
|
|
|
2011-04-15 22:18:39 +00:00
|
|
|
{- Stores an EncryptedCipher in a remote's configuration. -}
|
|
|
|
storeCipher :: RemoteConfig -> EncryptedCipher -> RemoteConfig
|
2011-04-16 17:25:27 +00:00
|
|
|
storeCipher c (EncryptedCipher t ks) =
|
|
|
|
M.insert "cipher" (toB64 t) $ M.insert "cipherkeys" (show ks) c
|
2011-04-15 22:18:39 +00:00
|
|
|
|
|
|
|
{- Extracts an EncryptedCipher from a remote's configuration. -}
|
2011-04-16 17:25:27 +00:00
|
|
|
extractCipher :: RemoteConfig -> Maybe EncryptedCipher
|
|
|
|
extractCipher c =
|
|
|
|
case (M.lookup "cipher" c, M.lookup "cipherkeys" c) of
|
|
|
|
(Just t, Just ks) -> Just $ EncryptedCipher (fromB64 t) (read ks)
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
{- Encrypts a Cipher to the specified KeyIds. -}
|
|
|
|
encryptCipher :: Cipher -> KeyIds -> IO EncryptedCipher
|
|
|
|
encryptCipher (Cipher c) (KeyIds ks) = do
|
|
|
|
let ks' = nub $ sort ks -- gpg complains about duplicate recipient keyids
|
2011-04-16 20:26:47 +00:00
|
|
|
encipher <- gpgPipeStrict (encrypt++recipients ks') c
|
2011-04-16 17:25:27 +00:00
|
|
|
return $ EncryptedCipher encipher (KeyIds ks')
|
|
|
|
where
|
|
|
|
encrypt = [ Params "--encrypt" ]
|
|
|
|
recipients l =
|
|
|
|
-- Force gpg to only encrypt to the specified
|
|
|
|
-- recipients, not configured defaults.
|
|
|
|
[ Params "--no-encrypt-to --no-default-recipient"] ++
|
|
|
|
(concat $ map (\k -> [Param "--recipient", Param k]) l)
|
2011-04-15 22:18:39 +00:00
|
|
|
|
|
|
|
{- Decrypting an EncryptedCipher is expensive; the Cipher should be cached. -}
|
|
|
|
decryptCipher :: RemoteConfig -> EncryptedCipher -> IO Cipher
|
2011-04-16 17:25:27 +00:00
|
|
|
decryptCipher _ (EncryptedCipher encipher _) =
|
2011-04-16 20:26:47 +00:00
|
|
|
return . Cipher =<< gpgPipeStrict decrypt encipher
|
2011-04-16 17:25:27 +00:00
|
|
|
where
|
2011-04-16 20:26:47 +00:00
|
|
|
decrypt = [ Param "--decrypt" ]
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-16 20:26:47 +00:00
|
|
|
{- Generates an encrypted form of a Key. The encryption does not need to be
|
2011-04-15 22:18:39 +00:00
|
|
|
- reversable, nor does it need to be the same type of encryption used
|
2011-04-16 20:26:47 +00:00
|
|
|
- on content. It does need to be repeatable. -}
|
2011-04-15 22:18:39 +00:00
|
|
|
encryptKey :: Cipher -> Key -> IO Key
|
2011-04-17 05:34:28 +00:00
|
|
|
encryptKey c k =
|
2011-04-16 20:26:47 +00:00
|
|
|
return Key {
|
2011-04-21 20:56:24 +00:00
|
|
|
keyName = hmacWithCipher c (show k),
|
2011-04-17 03:02:09 +00:00
|
|
|
keyBackendName = "GPGHMACSHA1",
|
2011-04-16 20:26:47 +00:00
|
|
|
keySize = Nothing, -- size and mtime omitted
|
|
|
|
keyMtime = Nothing -- to avoid leaking data
|
|
|
|
}
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-17 17:11:38 +00:00
|
|
|
{- Runs an action, passing it a handle from which it can
|
2011-04-17 04:34:38 +00:00
|
|
|
- stream encrypted content. -}
|
2011-04-19 19:26:50 +00:00
|
|
|
withEncryptedHandle :: Cipher -> (IO L.ByteString) -> (Handle -> IO a) -> IO a
|
2011-04-17 17:11:38 +00:00
|
|
|
withEncryptedHandle = gpgCipherHandle [Params "--symmetric --force-mdc"]
|
|
|
|
|
|
|
|
{- Runs an action, passing it a handle from which it can
|
|
|
|
- stream decrypted content. -}
|
2011-04-19 19:26:50 +00:00
|
|
|
withDecryptedHandle :: Cipher -> (IO L.ByteString) -> (Handle -> IO a) -> IO a
|
2011-04-17 17:11:38 +00:00
|
|
|
withDecryptedHandle = gpgCipherHandle [Param "--decrypt"]
|
2011-04-17 04:34:38 +00:00
|
|
|
|
2011-04-16 20:26:47 +00:00
|
|
|
{- Streams encrypted content to an action. -}
|
2011-04-19 19:26:50 +00:00
|
|
|
withEncryptedContent :: Cipher -> (IO L.ByteString) -> (L.ByteString -> IO a) -> IO a
|
2011-04-17 17:11:38 +00:00
|
|
|
withEncryptedContent = pass withEncryptedHandle
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-16 20:26:47 +00:00
|
|
|
{- Streams decrypted content to an action. -}
|
2011-04-19 19:26:50 +00:00
|
|
|
withDecryptedContent :: Cipher -> (IO L.ByteString) -> (L.ByteString -> IO a) -> IO a
|
2011-04-17 17:11:38 +00:00
|
|
|
withDecryptedContent = pass withDecryptedHandle
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-19 19:26:50 +00:00
|
|
|
pass :: (Cipher -> (IO L.ByteString) -> (Handle -> IO a) -> IO a)
|
|
|
|
-> Cipher -> (IO L.ByteString) -> (L.ByteString -> IO a) -> IO a
|
2011-04-17 17:11:38 +00:00
|
|
|
pass to c i a = to c i $ \h -> a =<< L.hGetContents h
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-19 17:40:02 +00:00
|
|
|
gpgParams :: [CommandParam] -> IO [String]
|
|
|
|
gpgParams params = do
|
|
|
|
-- Enable batch mode if GPG_AGENT_INFO is set, to avoid extraneous
|
|
|
|
-- gpg output about password prompts.
|
|
|
|
e <- catch (getEnv "GPG_AGENT_INFO") (const $ return "")
|
|
|
|
let batch = if null e then [] else ["--batch"]
|
|
|
|
return $ batch ++ defaults ++ toCommand params
|
|
|
|
where
|
|
|
|
-- be quiet, even about checking the trustdb
|
|
|
|
defaults = ["--quiet", "--trust-model", "always"]
|
2011-04-15 22:18:39 +00:00
|
|
|
|
2011-04-16 20:26:47 +00:00
|
|
|
gpgRead :: [CommandParam] -> IO String
|
2011-04-19 17:40:02 +00:00
|
|
|
gpgRead params = do
|
|
|
|
params' <- gpgParams params
|
|
|
|
pOpen ReadFromPipe "gpg" params' hGetContentsStrict
|
2011-04-16 20:26:47 +00:00
|
|
|
|
|
|
|
gpgPipeStrict :: [CommandParam] -> String -> IO String
|
|
|
|
gpgPipeStrict params input = do
|
2011-04-19 17:40:02 +00:00
|
|
|
params' <- gpgParams params
|
|
|
|
(pid, fromh, toh) <- hPipeBoth "gpg" params'
|
2011-04-16 22:22:52 +00:00
|
|
|
_ <- forkIO $ finally (hPutStr toh input) (hClose toh)
|
|
|
|
output <- hGetContentsStrict fromh
|
|
|
|
forceSuccess pid
|
2011-04-16 20:26:47 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
{- Runs gpg with a cipher and some parameters, feeding it an input,
|
2011-04-19 18:45:19 +00:00
|
|
|
- and passing a handle to its output to an action.
|
|
|
|
-
|
|
|
|
- Note that to avoid deadlock with the cleanup stage,
|
|
|
|
- the action must fully consume gpg's input before returning. -}
|
2011-04-19 19:26:50 +00:00
|
|
|
gpgCipherHandle :: [CommandParam] -> Cipher -> (IO L.ByteString) -> (Handle -> IO a) -> IO a
|
|
|
|
gpgCipherHandle params c a b = do
|
2011-04-16 20:26:47 +00:00
|
|
|
-- pipe the passphrase into gpg on a fd
|
|
|
|
(frompipe, topipe) <- createPipe
|
|
|
|
_ <- forkIO $ do
|
2011-04-17 03:02:09 +00:00
|
|
|
toh <- fdToHandle topipe
|
2011-04-17 05:34:28 +00:00
|
|
|
hPutStrLn toh $ cipherPassphrase c
|
2011-04-16 20:26:47 +00:00
|
|
|
hClose toh
|
2011-04-17 03:02:09 +00:00
|
|
|
let Fd passphrasefd = frompipe
|
|
|
|
let passphrase = [Param "--passphrase-fd", Param $ show passphrasefd]
|
2011-04-17 04:34:38 +00:00
|
|
|
|
2011-04-19 17:40:02 +00:00
|
|
|
params' <- gpgParams $ passphrase ++ params
|
|
|
|
(pid, fromh, toh) <- hPipeBoth "gpg" params'
|
2011-04-17 04:34:38 +00:00
|
|
|
_ <- forkProcess $ do
|
2011-04-19 19:26:50 +00:00
|
|
|
L.hPut toh =<< a
|
2011-04-17 04:34:38 +00:00
|
|
|
hClose toh
|
|
|
|
exitSuccess
|
|
|
|
hClose toh
|
2011-04-19 19:26:50 +00:00
|
|
|
ret <- b fromh
|
2011-04-16 20:26:47 +00:00
|
|
|
|
|
|
|
-- cleanup
|
|
|
|
forceSuccess pid
|
|
|
|
closeFd frompipe
|
|
|
|
return ret
|
2011-04-16 17:25:27 +00:00
|
|
|
|
|
|
|
configKeyIds :: RemoteConfig -> IO KeyIds
|
|
|
|
configKeyIds c = do
|
|
|
|
let k = configGet c "encryption"
|
2011-04-16 20:26:47 +00:00
|
|
|
s <- gpgRead [Params "--with-colons --list-public-keys", Param k]
|
2011-04-16 17:25:27 +00:00
|
|
|
return $ KeyIds $ parseWithColons s
|
|
|
|
where
|
|
|
|
parseWithColons s = map keyIdField $ filter pubKey $ lines s
|
|
|
|
pubKey = isPrefixOf "pub:"
|
|
|
|
keyIdField s = (split ":" s) !! 4
|
|
|
|
|
|
|
|
configGet :: RemoteConfig -> String -> String
|
|
|
|
configGet c key =
|
|
|
|
case M.lookup key c of
|
|
|
|
Just v -> v
|
|
|
|
Nothing -> error $ "missing " ++ key ++ " in remote config"
|
2011-04-16 20:26:47 +00:00
|
|
|
|
|
|
|
toB64 :: String -> String
|
|
|
|
toB64 = B64.encode . s2w8
|
|
|
|
|
|
|
|
fromB64 :: String -> String
|
|
|
|
fromB64 s =
|
|
|
|
case B64.decode s of
|
|
|
|
Nothing -> error "bad base64 encoded data"
|
|
|
|
Just ws -> w82s ws
|
2011-04-21 20:56:24 +00:00
|
|
|
hmacWithCipher :: Cipher -> String -> String
|
|
|
|
hmacWithCipher c = hmacWithCipher' (cipherHmac c)
|
|
|
|
hmacWithCipher' :: String -> String -> String
|
2011-04-26 15:24:23 +00:00
|
|
|
hmacWithCipher' c s = showDigest $ hmacSha1 (fromString c) (fromString s)
|
2011-04-21 20:56:24 +00:00
|
|
|
|
|
|
|
{- Ensure that hmacWithCipher' returns the same thing forevermore. -}
|
|
|
|
prop_hmacWithCipher_sane :: Bool
|
|
|
|
prop_hmacWithCipher_sane = known_good == hmacWithCipher' "foo" "bar"
|
|
|
|
where
|
|
|
|
known_good = "46b4ec586117154dacd49d664e5d63fdc88efb51"
|