summaryrefslogtreecommitdiff
path: root/kiki.hs
diff options
context:
space:
mode:
Diffstat (limited to 'kiki.hs')
-rw-r--r--kiki.hs64
1 files changed, 0 insertions, 64 deletions
diff --git a/kiki.hs b/kiki.hs
index 43d170c..1586517 100644
--- a/kiki.hs
+++ b/kiki.hs
@@ -125,35 +125,6 @@ decode_sshrsa bs = do
125 return rsakey 125 return rsakey
126 126
127 127
128rsaPrivateKeyFromPacket :: Packet -> Maybe RSAPrivateKey
129rsaPrivateKeyFromPacket pkt@(SecretKeyPacket {}) = do
130 -- public fields...
131 n <- lookup 'n' $ key pkt
132 e <- lookup 'e' $ key pkt
133 -- secret fields
134 MPI d <- lookup 'd' $ key pkt
135 MPI q <- lookup 'p' $ key pkt -- Note: p & q swapped
136 MPI p <- lookup 'q' $ key pkt -- Note: p & q swapped
137
138 -- Note: Here we fail if 'u' key is missing.
139 -- Ideally, it would be better to compute (inverse q) mod p
140 -- see Algebra.Structures.EuclideanDomain.extendedEuclidAlg
141 -- (package constructive-algebra)
142 coefficient <- lookup 'u' $ key pkt
143
144 let dmodp1 = MPI $ d `mod` (p - 1)
145 dmodqminus1 = MPI $ d `mod` (q - 1)
146 return $ RSAPrivateKey
147 { rsaN = n
148 , rsaE = e
149 , rsaD = MPI d
150 , rsaP = MPI p
151 , rsaQ = MPI q
152 , rsaDmodP1 = dmodp1
153 , rsaDmodQminus1 = dmodqminus1
154 , rsaCoefficient = coefficient }
155rsaPrivateKeyFromPacket _ = Nothing
156
157 128
158{- 129{-
159getPackets :: IO [Packet] 130getPackets :: IO [Packet]
@@ -165,17 +136,6 @@ getPackets = do
165-} 136-}
166 137
167 138
168writePEM typ dta = pem
169 where
170 pem = unlines . concat $
171 [ ["-----BEGIN " <> typ <> "-----"]
172 , split64s dta
173 , ["-----END " <> typ <> "-----"] ]
174 split64s "" = []
175 split64s dta = line : split64s rest where (line,rest) = splitAt 64 dta
176
177 -- 64 byte lines
178
179isEmbeddedSignature (EmbeddedSignaturePacket {}) = True 139isEmbeddedSignature (EmbeddedSignaturePacket {}) = True
180isEmbeddedSignature _ = False 140isEmbeddedSignature _ = False
181 141
@@ -603,30 +563,6 @@ guessKeyFormat 'S' "ssh-client" = "PEM"
603guessKeyFormat 'S' "ssh-host" = "PEM" 563guessKeyFormat 'S' "ssh-host" = "PEM"
604guessKeyFormat _ _ = "PEM" -- "PGP" 564guessKeyFormat _ _ = "PEM" -- "PGP"
605 565
606writeKeyToFile False "PEM" fname packet =
607 case key_algorithm packet of
608 RSA -> do
609 flip (maybe (return ()))
610 (rsaPrivateKeyFromPacket packet) -- RSAPrivateKey
611 $ \rsa -> do
612 let asn1 = toASN1 rsa []
613 bs = encodeASN1 DER asn1
614 dta = Base64.encode (L.unpack bs)
615 output = writePEM "RSA PRIVATE KEY" dta
616 stamp = toEnum . fromEnum $ timestamp packet
617 createDirectoryIfMissing True (takeDirectory fname)
618 handleIO_ (warn $ fname ++ ": write failure") $ do
619 saved_mask <- setFileCreationMask 0o077
620 writeFile fname output
621 -- Note: The key's timestamp is included in it's fingerprint.
622 -- Therefore, we should attempt to preserve it.
623 setFileTimes fname stamp stamp
624 setFileCreationMask saved_mask
625 return ()
626 warn $ fname ++ ": exported"
627 return ()
628 algo -> warn $ fname ++ ": unable to export "++show algo++" key "++fingerprint packet
629
630readPublicKey :: Char8.ByteString -> RSAPublicKey 566readPublicKey :: Char8.ByteString -> RSAPublicKey
631readPublicKey bs = maybe er id $ do 567readPublicKey bs = maybe er id $ do
632 let (pre,bs1) = Char8.splitAt 7 bs 568 let (pre,bs1) = Char8.splitAt 7 bs