From de1a24cf818af86841e5101c96183a83fd8b3cb5 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 9 May 2014 20:44:38 -0400 Subject: remove extractPEM/splitPEM in favor of pemParser --- KeyRing.hs | 40 ++++------------------------------------ 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/KeyRing.hs b/KeyRing.hs index b0dff04..fe0f3cd 100644 --- a/KeyRing.hs +++ b/KeyRing.hs @@ -79,7 +79,6 @@ module KeyRing , getBindings , accBindings , isSubkeySignature - , extractPEM , torhash ) where @@ -112,8 +111,8 @@ import Data.Time.Clock.POSIX ( POSIXTime ) import Data.Bits ( Bits ) import Data.Text.Encoding ( encodeUtf8 ) import qualified Data.Map as Map -import qualified Data.ByteString.Lazy as L ( unpack, pack, null, readFile, writeFile - , ByteString, toChunks, hGetContents, hPut, concat, fromChunks, isSuffixOf ) +import qualified Data.ByteString.Lazy as L ( unpack, null, readFile, writeFile + , ByteString, toChunks, hGetContents, hPut, concat, fromChunks ) import qualified Data.ByteString as S ( ByteString, unpack, splitAt, concat, cons, spanEnd, hGetContents, readFile) import qualified Crypto.Types.PubKey.ECC as ECC import qualified Codec.Binary.Base32 as Base32 @@ -152,6 +151,7 @@ import qualified Data.ByteString.Lazy.Char8 as Char8 import TimeUtil import PEM +import ScanningParser import qualified Hosts import qualified CryptoCoins import Base58 @@ -1386,7 +1386,7 @@ readKeyFromFile False "PEM" fname = do -- Therefore, we should attempt to preserve it. timestamp <- getInputFileTime ctx fname input <- readInputFileL ctx fname - let dta = extractPEM "RSA PRIVATE KEY" input + let dta = fmap pemBlob $ listToMaybe $ scanAndParse (pemParser $ Just "RSA PRIVATE KEY") $ Char8.lines input -- Char8.putStrLn $ "dta = " <> dta let rsa = do e <- decodeASN1 DER <$> dta @@ -1419,38 +1419,6 @@ readKeyFromFile False "PEM" fname = do } readKeyFromFile is_public fmt filename = error ("unimplemented key type: "++fmt) -extractPEM :: ByteString -> ByteString -> Maybe ByteString -extractPEM typ pem = if L.null blob then Nothing else Just blob - where - blob = pemBlob $ fst $ splitPEM (Just typ) $ Char8.lines pem - -splitPEM :: Maybe ByteString -> [ByteString] -> (PEMBlob,([ByteString],[ByteString])) -splitPEM mtyp [] = (PEMBlob "" "", ([],[])) -splitPEM mtyp pem = - case mblob of - Just blob -> (PEMBlob typ blob,(bs,drop 1 rs)) - Nothing -> let (ret,(ts,us)) = splitPEM mtyp $ drop 1 rs - in (ret, (bs++ys++ts,us)) - where - mblob = L.pack <$> Base64.decode (Char8.unpack dta) - dta = case ys of - _:dta_lines -> Char8.concat dta_lines - [] -> "" - (typ,(bs,xs)) = - case mtyp of - Just typ -> (typ,) $ span (/="-----BEGIN " <> typ <> "-----") pem - Nothing -> (L.concat $ take 1 typs, xs0) - where - xs0 = span (not . ("-----BEGIN " `Char8.isPrefixOf`)) - pem - typs = do - x0 <- fmap (Char8.drop 11) $ take 1 (fst xs0) - guard $ "-----" `L.isSuffixOf` x0 - let typ = Char8.take (Char8.length x0 - 5) x0 - return typ - (ys,rs) = span (/="-----END " <> typ <> "-----") xs - - doImport :: Ord k => (MappedPacket -> IO (KikiCondition Packet)) -- cgit v1.2.3