summaryrefslogtreecommitdiffhomepage
path: root/Gpg.hs
blob: 7002e169cf3fe8aff86361162e2b47498ed48c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{- Copyright 2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Gpg where

import Types
import System.Process
import Data.List.Split
import Data.Maybe
import System.Exit
import qualified Data.ByteString as B
import qualified Data.ByteString.UTF8 as BU8

listSecretKeys :: IO [(Name, KeyId)]
listSecretKeys = mapMaybe parse . lines <$> readProcess "gpg"
	["--batch", "--with-colons", "--list-secret-keys"] ""
  where
	parse l = case splitOn ":" l of
		("sec":_:_:_:kid:_:_:_:_:n:_) -> Just 
			(Name (BU8.fromString n), KeyId (BU8.fromString kid))
		_ -> Nothing

getSecretKey :: KeyId -> IO SecretKey
getSecretKey (KeyId kid) = do
	(_, Just hout, _, ph) <- createProcess (proc "gpg" ps)
		{ std_out = CreatePipe }
	secretkey <- SecretKey <$> B.hGetContents hout
	exitcode <- waitForProcess ph
	case exitcode of
		ExitSuccess -> return secretkey
		_ -> error "gpg --export-secret-key failed"
  where
	ps = ["--batch", "--export-secret-key", BU8.toString kid]

-- | Check if a given gpg key is present on the keyserver.
-- (Without downloading the key.)
knownByKeyServer :: KeyId -> IO Bool
knownByKeyServer kid = undefined
-- gpg --batch --with-colons --search-keys 2>/dev/null
-- check if output includes pub: line