diff options
Diffstat (limited to 'kiki.hs')
-rw-r--r-- | kiki.hs | 64 |
1 files changed, 0 insertions, 64 deletions
@@ -125,35 +125,6 @@ decode_sshrsa bs = do | |||
125 | return rsakey | 125 | return rsakey |
126 | 126 | ||
127 | 127 | ||
128 | rsaPrivateKeyFromPacket :: Packet -> Maybe RSAPrivateKey | ||
129 | rsaPrivateKeyFromPacket 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 } | ||
155 | rsaPrivateKeyFromPacket _ = Nothing | ||
156 | |||
157 | 128 | ||
158 | {- | 129 | {- |
159 | getPackets :: IO [Packet] | 130 | getPackets :: IO [Packet] |
@@ -165,17 +136,6 @@ getPackets = do | |||
165 | -} | 136 | -} |
166 | 137 | ||
167 | 138 | ||
168 | writePEM 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 | |||
179 | isEmbeddedSignature (EmbeddedSignaturePacket {}) = True | 139 | isEmbeddedSignature (EmbeddedSignaturePacket {}) = True |
180 | isEmbeddedSignature _ = False | 140 | isEmbeddedSignature _ = False |
181 | 141 | ||
@@ -603,30 +563,6 @@ guessKeyFormat 'S' "ssh-client" = "PEM" | |||
603 | guessKeyFormat 'S' "ssh-host" = "PEM" | 563 | guessKeyFormat 'S' "ssh-host" = "PEM" |
604 | guessKeyFormat _ _ = "PEM" -- "PGP" | 564 | guessKeyFormat _ _ = "PEM" -- "PGP" |
605 | 565 | ||
606 | writeKeyToFile 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 | |||
630 | readPublicKey :: Char8.ByteString -> RSAPublicKey | 566 | readPublicKey :: Char8.ByteString -> RSAPublicKey |
631 | readPublicKey bs = maybe er id $ do | 567 | readPublicKey bs = maybe er id $ do |
632 | let (pre,bs1) = Char8.splitAt 7 bs | 568 | let (pre,bs1) = Char8.splitAt 7 bs |