module Crypto.Store.CMS.PEM
( readCMSFile
, readCMSFileFromMemory
, berToContentInfo
, pemToContentInfo
, pemToContentInfoAccum
, writeCMSFile
, writeCMSFileToMemory
, contentInfoToDER
, contentInfoToPEM
) where
import qualified Data.ByteString as B
import Data.Either (rights)
import Crypto.Store.CMS.Info
import Crypto.Store.CMS.Util
import Crypto.Store.Error
import Crypto.Store.PEM
readCMSFile :: FilePath -> IO [ContentInfo]
readCMSFile :: String -> IO [ContentInfo]
readCMSFile String
path = [PEM] -> [ContentInfo]
accumulate ([PEM] -> [ContentInfo]) -> IO [PEM] -> IO [ContentInfo]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO [PEM]
readPEMs String
path
readCMSFileFromMemory :: B.ByteString -> [ContentInfo]
readCMSFileFromMemory :: ByteString -> [ContentInfo]
readCMSFileFromMemory = (String -> [ContentInfo])
-> ([PEM] -> [ContentInfo]) -> Either String [PEM] -> [ContentInfo]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([ContentInfo] -> String -> [ContentInfo]
forall a b. a -> b -> a
const []) [PEM] -> [ContentInfo]
accumulate (Either String [PEM] -> [ContentInfo])
-> (ByteString -> Either String [PEM])
-> ByteString
-> [ContentInfo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String [PEM]
pemParseBS
accumulate :: [PEM] -> [ContentInfo]
accumulate :: [PEM] -> [ContentInfo]
accumulate = [Either StoreError ContentInfo] -> [ContentInfo]
forall a b. [Either a b] -> [b]
rights ([Either StoreError ContentInfo] -> [ContentInfo])
-> ([PEM] -> [Either StoreError ContentInfo])
-> [PEM]
-> [ContentInfo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PEM -> Either StoreError ContentInfo)
-> [PEM] -> [Either StoreError ContentInfo]
forall a b. (a -> b) -> [a] -> [b]
map PEM -> Either StoreError ContentInfo
pemToContentInfo
berToContentInfo :: B.ByteString -> Either StoreError ContentInfo
berToContentInfo :: ByteString -> Either StoreError ContentInfo
berToContentInfo = ByteString -> Either StoreError ContentInfo
forall obj.
ParseASN1Object [ASN1Event] obj =>
ByteString -> Either StoreError obj
decodeASN1Object
pemToContentInfoAccum :: [Maybe ContentInfo] -> PEM -> [Maybe ContentInfo]
pemToContentInfoAccum :: [Maybe ContentInfo] -> PEM -> [Maybe ContentInfo]
pemToContentInfoAccum [Maybe ContentInfo]
acc PEM
pem =
(StoreError -> Maybe ContentInfo)
-> (ContentInfo -> Maybe ContentInfo)
-> Either StoreError ContentInfo
-> Maybe ContentInfo
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe ContentInfo -> StoreError -> Maybe ContentInfo
forall a b. a -> b -> a
const Maybe ContentInfo
forall a. Maybe a
Nothing) ContentInfo -> Maybe ContentInfo
forall a. a -> Maybe a
Just (PEM -> Either StoreError ContentInfo
pemToContentInfo PEM
pem) Maybe ContentInfo -> [Maybe ContentInfo] -> [Maybe ContentInfo]
forall a. a -> [a] -> [a]
: [Maybe ContentInfo]
acc
pemToContentInfo :: PEM -> Either StoreError ContentInfo
pemToContentInfo :: PEM -> Either StoreError ContentInfo
pemToContentInfo PEM
pem
| PEM -> String
pemName PEM
pem String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
names = ByteString -> Either StoreError ContentInfo
berToContentInfo (PEM -> ByteString
pemContent PEM
pem)
| Bool
otherwise = StoreError -> Either StoreError ContentInfo
forall a b. a -> Either a b
Left StoreError
UnexpectedNameForPEM
where names :: [String]
names = [ String
"CMS", String
"PKCS7" ]
writeCMSFile :: FilePath -> [ContentInfo] -> IO ()
writeCMSFile :: String -> [ContentInfo] -> IO ()
writeCMSFile String
path = String -> ByteString -> IO ()
B.writeFile String
path (ByteString -> IO ())
-> ([ContentInfo] -> ByteString) -> [ContentInfo] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ContentInfo] -> ByteString
writeCMSFileToMemory
writeCMSFileToMemory :: [ContentInfo] -> B.ByteString
writeCMSFileToMemory :: [ContentInfo] -> ByteString
writeCMSFileToMemory = [PEM] -> ByteString
pemsWriteBS ([PEM] -> ByteString)
-> ([ContentInfo] -> [PEM]) -> [ContentInfo] -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ContentInfo -> PEM) -> [ContentInfo] -> [PEM]
forall a b. (a -> b) -> [a] -> [b]
map ContentInfo -> PEM
contentInfoToPEM
contentInfoToDER :: ContentInfo -> B.ByteString
contentInfoToDER :: ContentInfo -> ByteString
contentInfoToDER = ContentInfo -> ByteString
forall obj. ProduceASN1Object ASN1P obj => obj -> ByteString
encodeASN1Object
contentInfoToPEM :: ContentInfo -> PEM
contentInfoToPEM :: ContentInfo -> PEM
contentInfoToPEM ContentInfo
info = PEM { pemName :: String
pemName = String
"CMS", pemHeader :: [(String, ByteString)]
pemHeader = [], pemContent :: ByteString
pemContent = ByteString
bs}
where bs :: ByteString
bs = ContentInfo -> ByteString
contentInfoToDER ContentInfo
info