summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--KnownHosts.hs30
-rw-r--r--SSHKey.hs8
-rw-r--r--kiki.hs3
3 files changed, 31 insertions, 10 deletions
diff --git a/KnownHosts.hs b/KnownHosts.hs
index a8f4fe3..e04ad90 100644
--- a/KnownHosts.hs
+++ b/KnownHosts.hs
@@ -11,6 +11,7 @@ data KnownHostsEntry = KnownHostsEntry
11 , eSp :: [L.ByteString] 11 , eSp :: [L.ByteString]
12 , eComment :: L.ByteString 12 , eComment :: L.ByteString
13 , eKeyBlob :: L.ByteString 13 , eKeyBlob :: L.ByteString
14 , eKey :: Maybe SSH.Key
14 , eKeyHeader :: L.ByteString 15 , eKeyHeader :: L.ByteString
15 , eHosts :: [L.ByteString] 16 , eHosts :: [L.ByteString]
16 } 17 }
@@ -20,12 +21,15 @@ serializeLine :: KnownHostsEntry -> L.ByteString
20serializeLine (KnownHostsEntry { eSp = sp 21serializeLine (KnownHostsEntry { eSp = sp
21 , eComment = comment 22 , eComment = comment
22 , eKeyHeader = hdr 23 , eKeyHeader = hdr
23 , eKeyBlob = key 24 , eKeyBlob = blob
25 , eKey = _
24 , eHosts = hosts 26 , eHosts = hosts
25 }) 27 })
26 = L.concat $ zipWith L.append [host,hdr,key] (take 3 sp) ++ [comment] 28 = L.concat $ zipWith L.append [host,hdr,blob] (take 3 sp) ++ [comment]
29 -- = L.concat $ zipWith L.append [host,hdr,keyblob] (take 3 sp) ++ [comment]
27 where 30 where
28 host = L.intercalate "," hosts 31 host = L.intercalate "," hosts
32 -- keyblob = L.dropWhile isSpace $ L.dropWhile (not . isSpace) $ SSH.keyblob key
29 33
30 34
31parseLine :: Int -> L.ByteString -> KnownHostsEntry 35parseLine :: Int -> L.ByteString -> KnownHostsEntry
@@ -34,7 +38,8 @@ parseLine num str = KnownHostsEntry
34 , eSp = sp 38 , eSp = sp
35 , eComment = comment 39 , eComment = comment
36 , eKeyHeader = hdr 40 , eKeyHeader = hdr
37 , eKeyBlob = key 41 , eKeyBlob = blob
42 , eKey = key
38 , eHosts = hosts 43 , eHosts = hosts
39 } 44 }
40 where 45 where
@@ -47,6 +52,23 @@ parseLine num str = KnownHostsEntry
47 h0 <- take 1 hs 52 h0 <- take 1 hs
48 h0 : hs' 53 h0 : hs'
49 hdr = L.concat $ take 1 (drop 1 ns) 54 hdr = L.concat $ take 1 (drop 1 ns)
50 key = L.concat $ take 1 (drop 2 ns) 55 blob = L.concat $ take 1 (drop 2 ns)
56 key = SSH.blobkey $ L.concat $ hdr:" ":blob:[]
51 comment = L.concat $ zipWith L.append (drop 3 ns) (drop 3 sp ++ repeat "") 57 comment = L.concat $ zipWith L.append (drop 3 ns) (drop 3 sp ++ repeat "")
52 58
59data KnownHosts = KnownHosts
60 { khcount :: Int
61 , khentries :: [KnownHostsEntry]
62 }
63
64decode :: L.ByteString -> KnownHosts
65decode input = renum $ foldr grok (KnownHosts 0 []) ls
66 where
67 ls = L.lines input
68 grok x (KnownHosts cnt es) =
69 KnownHosts (cnt+1) (parseLine 0 x:es)
70 renum kh = kh { khentries = zipWith setnum [1..] (khentries kh) }
71 where setnum n e = e { eLineno=n }
72
73encode :: KnownHosts -> L.ByteString
74encode kh = L.unlines $ map serializeLine (khentries kh)
diff --git a/SSHKey.hs b/SSHKey.hs
index 6e92e43..488f55f 100644
--- a/SSHKey.hs
+++ b/SSHKey.hs
@@ -8,7 +8,7 @@ import Data.Binary.Get ( runGet )
8import Data.Binary.Put ( putWord32be, runPut, putByteString ) 8import Data.Binary.Put ( putWord32be, runPut, putByteString )
9import Data.Binary ( get, put ) 9import Data.Binary ( get, put )
10import Data.Monoid ( (<>) ) 10import Data.Monoid ( (<>) )
11import Data.Maybe ( fromMaybe, listToMaybe ) 11import Data.Maybe ( listToMaybe )
12import Data.Char ( isSpace ) 12import Data.Char ( isSpace )
13import Control.Monad ( guard ) 13import Control.Monad ( guard )
14import LengthPrefixedBE 14import LengthPrefixedBE
@@ -28,8 +28,8 @@ keyblob (n,e) = "ssh-rsa " <> blob
28 put (LengthPrefixedBE e) 28 put (LengthPrefixedBE e)
29 put (LengthPrefixedBE n) 29 put (LengthPrefixedBE n)
30 30
31blobkey :: L8.ByteString -> Key 31blobkey :: L8.ByteString -> Maybe Key
32blobkey bs = fromMaybe er $ do 32blobkey bs = do
33 let (pre,bs1) = L8.splitAt 7 bs 33 let (pre,bs1) = L8.splitAt 7 bs
34 guard $ pre == "ssh-rsa" 34 guard $ pre == "ssh-rsa"
35 let (sp,bs2) = L8.span isSpace bs1 35 let (sp,bs2) = L8.span isSpace bs1
@@ -38,8 +38,6 @@ blobkey bs = fromMaybe er $ do
38 qq <- L.pack `fmap` Base64.decode (L8.unpack bs3) 38 qq <- L.pack `fmap` Base64.decode (L8.unpack bs3)
39 decode_sshrsa qq 39 decode_sshrsa qq
40 where 40 where
41 er = error "Unsupported key format"
42
43 decode_sshrsa :: L8.ByteString -> Maybe Key 41 decode_sshrsa :: L8.ByteString -> Maybe Key
44 decode_sshrsa bs = do 42 decode_sshrsa bs = do
45 let (pre,bs1) = L8.splitAt 11 bs 43 let (pre,bs1) = L8.splitAt 11 bs
diff --git a/kiki.hs b/kiki.hs
index 98ac4c7..c00ab84 100644
--- a/kiki.hs
+++ b/kiki.hs
@@ -224,7 +224,8 @@ modifyUID other = other
224readPublicKey :: Char8.ByteString -> RSAPublicKey 224readPublicKey :: Char8.ByteString -> RSAPublicKey
225readPublicKey bs = RSAKey (MPI n) (MPI e) 225readPublicKey bs = RSAKey (MPI n) (MPI e)
226 where 226 where
227 (n,e) = SSH.blobkey bs 227 (n,e) = fromMaybe (error "Unsupported key format")
228 $ SSH.blobkey bs
228 229
229-- | Returns the given list with its last element modified. 230-- | Returns the given list with its last element modified.
230toLast :: (x -> x) -> [x] -> [x] 231toLast :: (x -> x) -> [x] -> [x]