2011-12-21 01:47:56 +00:00
|
|
|
|
{- gpg interface
|
|
|
|
|
-
|
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Utility.Gpg where
|
|
|
|
|
|
|
|
|
|
import System.Posix.Types
|
|
|
|
|
import Control.Applicative
|
|
|
|
|
import Control.Concurrent
|
2012-07-18 19:30:26 +00:00
|
|
|
|
import Control.Exception (bracket)
|
2011-12-21 03:20:36 +00:00
|
|
|
|
import System.Posix.Env (setEnv, unsetEnv, getEnv)
|
2011-12-21 01:47:56 +00:00
|
|
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
|
|
newtype KeyIds = KeyIds [String]
|
2012-12-13 04:45:27 +00:00
|
|
|
|
deriving (Ord, Eq)
|
2011-12-21 01:47:56 +00:00
|
|
|
|
|
|
|
|
|
stdParams :: [CommandParam] -> IO [String]
|
|
|
|
|
stdParams params = do
|
|
|
|
|
-- Enable batch mode if GPG_AGENT_INFO is set, to avoid extraneous
|
2012-01-09 22:19:29 +00:00
|
|
|
|
-- gpg output about password prompts. GPG_BATCH is set by the test
|
|
|
|
|
-- suite for a similar reason.
|
2011-12-21 03:20:36 +00:00
|
|
|
|
e <- getEnv "GPG_AGENT_INFO"
|
2012-01-09 22:19:29 +00:00
|
|
|
|
b <- getEnv "GPG_BATCH"
|
|
|
|
|
let batch = if isNothing e && isNothing b
|
|
|
|
|
then []
|
2012-08-17 15:22:11 +00:00
|
|
|
|
else ["--batch", "--no-tty", "--use-agent"]
|
2011-12-21 01:47:56 +00:00
|
|
|
|
return $ batch ++ defaults ++ toCommand params
|
2012-12-13 04:24:19 +00:00
|
|
|
|
where
|
|
|
|
|
-- be quiet, even about checking the trustdb
|
|
|
|
|
defaults = ["--quiet", "--trust-model", "always"]
|
2011-12-21 01:47:56 +00:00
|
|
|
|
|
|
|
|
|
{- Runs gpg with some params and returns its stdout, strictly. -}
|
|
|
|
|
readStrict :: [CommandParam] -> IO String
|
|
|
|
|
readStrict params = do
|
|
|
|
|
params' <- stdParams params
|
2012-07-19 04:43:36 +00:00
|
|
|
|
withHandle StdoutHandle createProcessSuccess (proc "gpg" params') $ \h -> do
|
|
|
|
|
hSetBinaryMode h True
|
|
|
|
|
hGetContentsStrict h
|
2011-12-21 01:47:56 +00:00
|
|
|
|
|
2011-12-21 03:20:36 +00:00
|
|
|
|
{- Runs gpg, piping an input value to it, and returning its stdout,
|
2011-12-21 01:47:56 +00:00
|
|
|
|
- strictly. -}
|
|
|
|
|
pipeStrict :: [CommandParam] -> String -> IO String
|
|
|
|
|
pipeStrict params input = do
|
|
|
|
|
params' <- stdParams params
|
2012-07-19 04:43:36 +00:00
|
|
|
|
withBothHandles createProcessSuccess (proc "gpg" params') $ \(to, from) -> do
|
|
|
|
|
hSetBinaryMode to True
|
|
|
|
|
hSetBinaryMode from True
|
|
|
|
|
hPutStr to input
|
|
|
|
|
hClose to
|
|
|
|
|
hGetContentsStrict from
|
2011-12-21 01:47:56 +00:00
|
|
|
|
|
2012-11-18 19:27:44 +00:00
|
|
|
|
{- Runs gpg with some parameters. First sends it a passphrase via
|
|
|
|
|
- --passphrase-fd. Then runs a feeder action that is passed a handle and
|
|
|
|
|
- should write to it all the data to input to gpg. Finally, runs
|
|
|
|
|
- a reader action that is passed a handle to gpg's output.
|
2011-12-21 01:47:56 +00:00
|
|
|
|
-
|
2013-01-16 19:27:46 +00:00
|
|
|
|
- Runs gpg in batch mode; this is necessary to avoid gpg 2.x prompting for
|
|
|
|
|
- the passphrase.
|
|
|
|
|
-
|
2011-12-21 01:47:56 +00:00
|
|
|
|
- Note that to avoid deadlock with the cleanup stage,
|
2012-11-18 19:27:44 +00:00
|
|
|
|
- the reader must fully consume gpg's input before returning. -}
|
|
|
|
|
feedRead :: [CommandParam] -> String -> (Handle -> IO ()) -> (Handle -> IO a) -> IO a
|
|
|
|
|
feedRead params passphrase feeder reader = do
|
2011-12-21 01:47:56 +00:00
|
|
|
|
-- pipe the passphrase into gpg on a fd
|
|
|
|
|
(frompipe, topipe) <- createPipe
|
2012-09-09 17:11:54 +00:00
|
|
|
|
void $ forkIO $ do
|
2011-12-21 01:47:56 +00:00
|
|
|
|
toh <- fdToHandle topipe
|
|
|
|
|
hPutStrLn toh passphrase
|
|
|
|
|
hClose toh
|
|
|
|
|
let Fd pfd = frompipe
|
|
|
|
|
let passphrasefd = [Param "--passphrase-fd", Param $ show pfd]
|
|
|
|
|
|
2013-01-16 19:27:46 +00:00
|
|
|
|
params' <- stdParams $ [Param "--batch"] ++ passphrasefd ++ params
|
2012-07-19 04:43:36 +00:00
|
|
|
|
closeFd frompipe `after`
|
|
|
|
|
withBothHandles createProcessSuccess (proc "gpg" params') go
|
2012-12-13 04:24:19 +00:00
|
|
|
|
where
|
|
|
|
|
go (to, from) = do
|
|
|
|
|
void $ forkIO $ do
|
|
|
|
|
feeder to
|
|
|
|
|
hClose to
|
|
|
|
|
reader from
|
2011-12-21 01:47:56 +00:00
|
|
|
|
|
|
|
|
|
{- Finds gpg public keys matching some string. (Could be an email address,
|
|
|
|
|
- a key id, or a name. -}
|
|
|
|
|
findPubKeys :: String -> IO KeyIds
|
|
|
|
|
findPubKeys for = KeyIds . parse <$> readStrict params
|
2012-12-13 04:24:19 +00:00
|
|
|
|
where
|
|
|
|
|
params = [Params "--with-colons --list-public-keys", Param for]
|
|
|
|
|
parse = catMaybes . map (keyIdField . split ":") . lines
|
|
|
|
|
keyIdField ("pub":_:_:_:f:_) = Just f
|
|
|
|
|
keyIdField _ = Nothing
|
2011-12-21 03:20:36 +00:00
|
|
|
|
|
2012-04-29 18:02:18 +00:00
|
|
|
|
{- Creates a block of high-quality random data suitable to use as a cipher.
|
|
|
|
|
- It is armored, to avoid newlines, since gpg only reads ciphers up to the
|
2013-03-04 00:39:01 +00:00
|
|
|
|
- first newline. -}
|
2012-04-29 18:02:18 +00:00
|
|
|
|
genRandom :: Int -> IO String
|
2013-03-04 00:39:01 +00:00
|
|
|
|
genRandom size = checksize <$> readStrict
|
|
|
|
|
[ Params params
|
2012-04-29 18:02:18 +00:00
|
|
|
|
, Param $ show randomquality
|
|
|
|
|
, Param $ show size
|
|
|
|
|
]
|
2012-12-13 04:24:19 +00:00
|
|
|
|
where
|
2013-03-04 00:39:01 +00:00
|
|
|
|
params = "--gen-random --armor"
|
|
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
|
-- 1 is /dev/urandom; 2 is /dev/random
|
|
|
|
|
randomquality = 1 :: Int
|
2011-12-21 03:20:36 +00:00
|
|
|
|
|
2013-03-04 00:39:01 +00:00
|
|
|
|
{- The size is the number of bytes of entropy desired; the data is
|
|
|
|
|
- base64 encoded, so needs 8 bits to represent every 6 bytes of
|
|
|
|
|
- entropy. -}
|
|
|
|
|
expectedlength = size * 8 `div` 6
|
|
|
|
|
|
|
|
|
|
checksize s = let len = length s in
|
|
|
|
|
if len >= expectedlength
|
|
|
|
|
then s
|
|
|
|
|
else shortread len
|
|
|
|
|
|
|
|
|
|
shortread got = error $ unwords
|
|
|
|
|
[ "Not enough bytes returned from gpg", params
|
|
|
|
|
, "(got", show got, "; expected", show expectedlength, ")"
|
|
|
|
|
]
|
|
|
|
|
|
2011-12-21 03:20:36 +00:00
|
|
|
|
{- A test key. This is provided pre-generated since generating a new gpg
|
|
|
|
|
- key is too much work (requires too much entropy) for a test suite to
|
|
|
|
|
- do.
|
|
|
|
|
-
|
|
|
|
|
- This key was generated with no exipiration date, and a small keysize.
|
|
|
|
|
- It has an empty passphrase. -}
|
|
|
|
|
testKeyId :: String
|
|
|
|
|
testKeyId = "129D6E0AC537B9C7"
|
|
|
|
|
testKey :: String
|
|
|
|
|
testKey = keyBlock True
|
|
|
|
|
[ "mI0ETvFAZgEEAKnqwWgZqznMhi1RQExem2H8t3OyKDxaNN3rBN8T6LWGGqAYV4wT"
|
|
|
|
|
, "r8In5tfsnz64bKpE1Qi68JURFwY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x3JQBACgUobj/7aB9N2rQ1EE2xhN+AgOMhAvQVYIYu5L4jmZTs9r"
|
|
|
|
|
, "PMQH6f/AGLKxAsdZIG13AOyQ/dNnjcqezc17HSV9MycB6ormVtF5Q0/GSbuKq2EW"
|
|
|
|
|
, "IJf+/dFjxEmflWpbxw/36pEd/EReLX8b8qDIYadK6BpiWN9xgEiBv/oEJAxffw=="
|
|
|
|
|
, "=LDsg"
|
|
|
|
|
]
|
|
|
|
|
keyBlock :: Bool -> [String] -> String
|
|
|
|
|
keyBlock public ls = unlines
|
|
|
|
|
[ "-----BEGIN PGP "++t++" KEY BLOCK-----"
|
|
|
|
|
, "Version: GnuPG v1.4.11 (GNU/Linux)"
|
|
|
|
|
, ""
|
|
|
|
|
, unlines ls
|
|
|
|
|
, "-----END PGP "++t++" KEY BLOCK-----"
|
|
|
|
|
]
|
2012-12-13 04:24:19 +00:00
|
|
|
|
where
|
|
|
|
|
t
|
|
|
|
|
| public = "PUBLIC"
|
|
|
|
|
| otherwise = "PRIVATE"
|
2011-12-21 03:20:36 +00:00
|
|
|
|
|
|
|
|
|
{- Runs an action using gpg in a test harness, in which gpg does
|
|
|
|
|
- not use ~/.gpg/, but a directory with the test key set up to be used. -}
|
|
|
|
|
testHarness :: IO a -> IO a
|
|
|
|
|
testHarness a = do
|
|
|
|
|
orig <- getEnv var
|
|
|
|
|
bracket setup (cleanup orig) (const a)
|
2012-12-13 04:24:19 +00:00
|
|
|
|
where
|
|
|
|
|
var = "GNUPGHOME"
|
2011-12-21 03:20:36 +00:00
|
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
|
setup = do
|
|
|
|
|
base <- getTemporaryDirectory
|
|
|
|
|
dir <- mktmpdir $ base </> "gpgtmpXXXXXX"
|
|
|
|
|
setEnv var dir True
|
|
|
|
|
_ <- pipeStrict [Params "--import -q"] $ unlines
|
|
|
|
|
[testSecretKey, testKey]
|
|
|
|
|
return dir
|
2011-12-21 03:20:36 +00:00
|
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
|
cleanup orig tmpdir = removeDirectoryRecursive tmpdir >> reset orig
|
|
|
|
|
reset (Just v) = setEnv var v True
|
|
|
|
|
reset _ = unsetEnv var
|
2011-12-21 03:20:36 +00:00
|
|
|
|
|
|
|
|
|
{- Tests the test harness. -}
|
|
|
|
|
testTestHarness :: IO Bool
|
|
|
|
|
testTestHarness = do
|
|
|
|
|
keys <- testHarness $ findPubKeys testKeyId
|
|
|
|
|
return $ KeyIds [testKeyId] == keys
|