summaryrefslogtreecommitdiff
path: root/lib/Kiki.hs
blob: 0684830850b0cf9c7332f13a0cf462003c39f877 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{-# LANGUAGE CPP #-}
module Kiki where

import Control.Applicative
import Control.Arrow
import Control.Monad
import Data.ASN1.BinaryEncoding
import Data.ASN1.Encoding
import Data.ASN1.Types
import Data.Binary
import Data.List
import Data.Maybe
import Data.OpenPGP
import Data.OpenPGP.Util
import Data.Ord
import System.Directory
import System.FilePath.Posix
import System.IO
import System.Posix.User
import qualified Codec.Binary.Base64 as Base64
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as Char8
import qualified Data.Map.Strict as Map
import qualified SSHKey as SSH

import CommandLine
import KeyRing

-- |
-- Regenerate /var/cache/kiki
refresh :: (FilePath -> FilePath) -> CommonArgsParsed -> IO ()
refresh root homepass = do
    let homepass' = homepass { cap_homespec = fmap root (cap_homespec homepass) }
    KikiResult r report <- runKeyRing $ minimalOp homepass'
    let mroot = case root "" of
                    "/" -> Nothing
                    ""  -> Nothing
                    pth -> Just pth
    case r of
        KikiSuccess rt -> refreshCache rt mroot
        _ -> return () -- XXX: silent fail?

data CommonArgsParsed = CommonArgsParsed { cap_homespec :: Maybe String, cap_passfd :: Maybe InputFile }

streaminfo :: StreamInfo
streaminfo = StreamInfo
    { fill = KF_None
    , spill = KF_None
    , typ = KeyRingFile
    , initializer = NoCreate
    , access = AutoAccess
    , transforms = []
    }

minimalOp :: CommonArgsParsed -> KeyRingOperation
minimalOp cap = op
 where
    streaminfo = StreamInfo { fill = KF_None
                            , typ = KeyRingFile
                            , spill = KF_All
                            , initializer = NoCreate
                            , access = AutoAccess
                            , transforms = []
                            }
    op = KeyRingOperation
        { opFiles = Map.fromList $
            [ ( HomeSec, streaminfo { access = Sec })
            , ( HomePub, streaminfo { access = Pub })
            ]
        , opPassphrases = do pfile <- maybeToList (cap_passfd cap)
                             return $ PassphraseSpec Nothing Nothing pfile
        , opTransforms = []
        , opHome = cap_homespec cap
        }

run :: [String] -> Args (IO ()) -> IO ()
run args x =
    case runArgs (parseInvocation (uncurry fancy kikiOptions "") args) x of
        Left e   -> hPutStrLn stderr $ usageErrorMessage e
        Right io -> io

importAndRefresh :: (FilePath -> FilePath) -> CommonArgsParsed -> IO ()
importAndRefresh root cmn = do
    let rootdir = do guard (root "x" /= "x")
                     Just $ root ""

    me <- getEffectiveUserID

    let noChrootArg = rootdir == Nothing
        bUnprivileged = (me/=0) && noChrootArg
    if rootdir==Just "" then error "--chroot requires an argument" else do

    let homespec = mplus (slash <$> rootdir <*> cap_homespec cmn)
                         (fmap (++"/root/.gnupg") rootdir)
        sshkeygen size = Just $ concat [ "mkdir -p \"$(dirname $file)\" && "
                                       , "ssh-keygen -P \"\" -q -f $file -b "
                                       , show size ]
        mkdirFor path = do
            let dir = takeDirectory path
            -- putStrLn $ "mkdirFor " ++ show dir
            createDirectoryIfMissing True dir
        -- ssl = Just "mkdir -p \"$(dirname $file)\" && openssl genrsa -out $file 1024"
    (home,secring,pubring,mbwk) <- unconditionally $ getHomeDir homespec
    osHomeDir <- if bUnprivileged then getHomeDirectory else return "/root"

    -- Generate secring.gpg if it does not exist...
    gotsec <- doesFileExist secring
    when (not gotsec) $ do
        {-  ssh-keygen to create master key...
        let mkpath = home ++ "/master-key"
        mkdirFor mkpath
        e <- systemEnv [ ("file",mkpath) ] (fromJust $ sshkeygen 4096)
        case e of
            ExitFailure num -> error "ssh-keygen failed to create master key"
            ExitSuccess     -> return ()
        [PEMPacket mk] <- readSecretPEMFile (ArgFile mkpath)
        writeInputFileL (InputFileContext secring pubring)
                        HomeSec
                        ( encode $ Message [mk { is_subkey = False }] )
        -}
        master <- (\k -> k { is_subkey = False }) <$> generateKey (GenRSA $ 4096 `div` 8 )
        mkdirFor secring
        writeInputFileL (InputFileContext secring pubring)
                        HomeSec
                        $ encode $ Message [master { is_subkey = False}]

    gotpub <- doesFileExist pubring
    when (not gotpub) $ do
        mkdirFor pubring
        writeInputFileL (InputFileContext secring pubring)
                        HomePub
                        ( encode $ Message [] )

    -- Old paths..
    --
    -- Private
    -- pem tor        /var/lib/tor/samizdat/private_key
    -- pem ssh-client %(home)/.ssh/id_rsa
    -- pem ssh-server /etc/ssh/ssh_host_rsa_key
    -- pem ipsec      /etc/ipsec.d/private/%(onion).pem

    -- Public
    -- ssh-client %(home)/.ssh/id_rsa.pub
    -- ssh-server /etc/ssh/ssh_host_rsa_key.pub
    -- ipsec      /etc/ipsec.d/certs/%(onion).pem

    -- First, we ensure that the tor key exists and is imported
    -- so that we know where to put the strongswan key.
    let passfd = cap_passfd cmn
        strm = StreamInfo { typ = KeyRingFile
                          , fill = KF_None
                          , spill = KF_All
                          , access = AutoAccess
                          , initializer = NoCreate
                          , transforms = [] }
        buildStreamInfo rtyp ftyp = StreamInfo { typ = ftyp
                                               , fill = rtyp
                                               , spill = KF_All
                                               , access = AutoAccess
                                               , initializer = NoCreate
                                               , transforms = [] }
        peminfo bits usage =
                  StreamInfo { typ = PEMFile
                             , fill = KF_None -- KF_Match usage
                             , spill = KF_Match usage
                             , access = Sec
                             , initializer = Internal (GenRSA $ bits `div` 8)
                             , transforms = []
                             }
        sshcpath = fromMaybe "" rootdir ++ osHomeDir ++ ".ssh/id_rsa"
        sshspath = fromMaybe "" rootdir ++ "/etc/ssh/ssh_host_rsa_key"
        op = KeyRingOperation
             { opFiles = Map.fromList $
                [ ( HomeSec, buildStreamInfo KF_All KeyRingFile )
                , ( HomePub, (buildStreamInfo KF_All KeyRingFile) { access = Pub } )
                , ( Generate 0 (GenRSA (1024 `div` 8)), strm { spill = KF_Match "tor" })
                , ( Generate 1 (GenRSA (1024 `div` 8)), strm { spill = KF_Match "ipsec" })
                , ( ArgFile sshcpath, (peminfo 2048 "ssh-client") )
                , ( ArgFile sshspath, (peminfo 2048 "ssh-server") )
                ]
             , opPassphrases = do pfd <- maybeToList passfd
                                  return $ PassphraseSpec Nothing Nothing pfd
             , opHome = homespec
             , opTransforms = []
             }
        -- doNothing = return ()
        nop = KeyRingOperation
            { opFiles = Map.empty
            , opPassphrases = do pfd <- maybeToList passfd
                                 return $ PassphraseSpec Nothing Nothing pfd
            , opHome=homespec, opTransforms = []
            }
    -- if bUnprivileged then doNothing else mkdirFor torpath
    KikiResult rt report <- runKeyRing (if bUnprivileged then nop else op)
    forM_ report $ \(fname,act) -> do
        putStrLn $ fname ++ ": " ++ reportString act
    rt <- case rt of
        BadPassphrase ->
            error "Operation requires correct passphrase. (Hint: Use --passphrase-fd=0 to input it on stdin.)"
        _ -> unconditionally $ return rt

    when (not bUnprivileged) $ refreshCache rt rootdir


refreshCache :: KeyRingRuntime -> Maybe FilePath -> IO ()
refreshCache rt rootdir = do

    let mkpath pth = fromMaybe "" rootdir ++ "/var/cache/kiki/"++pth

        write f bs = do
            createDirectoryIfMissing True $ takeDirectory f
            writeFile f bs

    let oname' = do wk <- rtWorkingKey rt
                    -- XXX unnecessary signature check
                    onionNameForContact (keykey wk) (rtKeyDB rt)
        bUnprivileged = False -- TODO
    if (oname' == Nothing) && (not bUnprivileged) then error "Missing tor key" else do
    let oname = fromMaybe "" oname'
    -- sshcpathpub0 = fromMaybe "" rootdir ++ osHomeDir </> ".ssh" </> "id_rsa.pub"
    -- sshspathpub0 = fromMaybe "" rootdir ++ "/etc/ssh/ssh_host_rsa_key.pub"
    -- contactipsec0 = fromMaybe "" rootdir ++ "/etc/ipsec.d/certs/%(onion).pem"

    -- Finally, export public keys if they do not exist.
    flip (maybe $ warn "missing working key?") (rtGrip rt) $ \grip -> do
    either warn (write $ mkpath "root/.ssh/id_rsa.pub")
            $ show_ssh' "ssh-client" grip (rtKeyDB rt)
    either warn (write $ mkpath "ssh_host_rsa_key.pub")
        $ show_ssh' "ssh-server" grip (rtKeyDB rt)
    either warn (write $ mkpath "ipsec.d/certs/" ++ oname++".pem")
        $ show_pem' "ipsec" grip (rtKeyDB rt) pemFromPacket

    let cs = filter notme (Map.elems $ rtKeyDB rt)
        kk = keykey (fromJust $ rtWorkingKey rt)
        notme kd = keykey (keyPacket kd) /= kk

        installConctact kd = do
            -- The getHostnames command requires a valid cross-signed tor key
            -- for each onion name returned in (_,(ns,_)).
            let (_,(ns,_)) = getHostnames kd
                contactname = fmap Char8.unpack $ listToMaybe ns -- only first onion name.
            flip (maybe $ return ()) contactname $ \contactname -> do

            let cpath = interp (Map.singleton "onion" contactname) "ipsec.d/certs/%(onion).pem"
                their_master = packet $ keyMappedPacket kd
                -- We find all cross-certified ipsec keys for the given cross-certified onion name.
                ipsecs = sortOn (Down . timestamp)
                         $ getCrossSignedSubkeys their_master (keySubKeys kd) "ipsec"
            forM_ (take 1 ipsecs) $ \k -> do
            either warn (write $ mkpath cpath) $ pemFromPacket k

    mapM_ installConctact cs


#if !MIN_VERSION_base(4,8,0)
sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn f =
    map snd . sortBy (comparing fst) . map (\x -> let y = f x in y `seq` (y, x))
#endif

pemFromPacket k = do
    let rsa = pkcs8 . fromJust $ rsaKeyFromPacket k
        der = encodeASN1 DER (toASN1 rsa [])
        qq  = Base64.encode (L.unpack der)
    return $
        writePEM "PUBLIC KEY" qq -- ("TODO "++show keyspec)

show_pem keyspec wkgrip db = either warn putStrLn $ show_pem' keyspec wkgrip db pemFromPacket

show_pem' keyspec wkgrip db keyfmt = do
    let s = parseSpec wkgrip keyspec
    flip (maybe . Left $ keyspec ++ ": not found")
         (selectPublicKey s db)
         keyfmt

warn str = hPutStrLn stderr str

show_ssh keyspec wkgrip db = either warn putStrLn $ show_ssh' keyspec wkgrip db

show_ssh' keyspec wkgrip db = do
    let s = parseSpec wkgrip keyspec
    flip (maybe . Left $ keyspec ++ ": not found")
         (selectPublicKey s db)
         $ return . sshblobFromPacket

-- |
-- interpolate %var patterns in a string.
interp vars raw = es >>= interp1
 where
    gs = groupBy (\_ c -> c/='%') raw
    es = dropWhile null $ gobbleEscapes ("":gs)
            where gobbleEscapes :: [String] -> [String]
                  gobbleEscapes (a:"%":b:bs) = (a++b) : gobbleEscapes bs
                  gobbleEscapes (g:gs)       = g : gobbleEscapes gs
                  gobbleEscapes []           = []
    interp1 ('%':'(':str) = fromMaybe "" (Map.lookup key vars) ++ drop 1 rest
                            where (key,rest) = break (==')') str
    interp1 plain = plain

sshblobFromPacket k = blob
 where
    Just (RSAKey (MPI n) (MPI e)) = rsaKeyFromPacket k
    bs = SSH.keyblob (n,e)
    blob = Char8.unpack bs

replaceSshServerKeys root cmn = do
    let homepass' = cmn { cap_homespec = fmap root (cap_homespec cmn) }
        replaceSSH op = op { opFiles = files }
            where
                files = Map.adjust delssh HomeSec
                        $ Map.adjust delssh HomePub
                        $ Map.insert (ArgFile $ root "/etc/ssh/ssh_host_rsa_key") strm $ opFiles op
                strm = streaminfo { typ = PEMFile, spill = KF_Match "ssh-server", access = Sec }
                delssh strm = strm { transforms = DeleteSubkeyByUsage "ssh-server" : transforms strm
                                   , fill = KF_All }
    KikiResult r report <- runKeyRing $ minimalOp homepass'
    case r of
        KikiSuccess rt -> Kiki.refreshCache rt $ case root "" of
                                                    "/" -> Nothing
                                                    ""  -> Nothing
                                                    pth -> Just pth
        err            -> hPutStrLn stderr $ errorString err

slash :: String -> String -> String
slash "/"    ('/':xs) = '/':xs
slash ""     ('/':xs) = '/':xs
slash ""     xs       = '/':xs
slash (y:ys) xs       = y:slash ys xs

ㄧchroot :: Args (FilePath -> FilePath)
ㄧchroot = pure (\r a -> slash r a) <*> arg "--chroot" <|> pure id

ㄧhomedir :: Args CommonArgsParsed
ㄧhomedir = CommonArgsParsed
                <$> optional (arg "--homedir")
                <*> optional (FileDesc <$> read <$> arg "--passphrase-fd")

kikiOptions :: ( [(String,Int)], [String] )
kikiOptions = ( ss, ps )
 where
    ss = [("--chroot",1),("--passphrase-fd",1),("--homedir",1)]
    ps = []