module OpenSSL.PEM
(
PemPasswordCallback
, PemPasswordRWState(..)
, PemPasswordSupply(..)
, writePKCS8PrivateKey
, readPrivateKey
, writePublicKey
, readPublicKey
, writeX509
, readX509
, PemX509ReqFormat(..)
, writeX509Req
, readX509Req
, writeCRL
, readCRL
, writePkcs7
, readPkcs7
)
where
import Control.Exception hiding (try)
import Control.Monad
import Foreign
import Foreign.C
import OpenSSL.BIO
import OpenSSL.EVP.Cipher hiding (cipher)
import OpenSSL.EVP.PKey
import OpenSSL.PKCS7
import OpenSSL.Utils
import OpenSSL.X509
import OpenSSL.X509.Request
import OpenSSL.X509.Revocation
import Prelude hiding (catch)
import System.IO
type PemPasswordCallback = Int -> PemPasswordRWState -> IO String
type PemPasswordCallback' = Ptr CChar -> Int -> Int -> Ptr () -> IO Int
data PemPasswordRWState = PwRead
| PwWrite
data PemPasswordSupply = PwNone
| PwStr String
| PwCallback PemPasswordCallback
| PwTTY
foreign import ccall "wrapper"
mkPemPasswordCallback :: PemPasswordCallback' -> IO (FunPtr PemPasswordCallback')
rwflagToState :: Int -> PemPasswordRWState
rwflagToState 0 = PwRead
rwflagToState 1 = PwWrite
rwflagToState _ = undefined
callPasswordCB :: PemPasswordCallback -> PemPasswordCallback'
callPasswordCB cb buf bufLen rwflag _
= let mode = rwflagToState rwflag
try = do passStr <- cb bufLen mode
let passLen = length passStr
when (passLen > bufLen)
$ failForTooLongPassword bufLen
pokeArray buf $ map (toEnum . fromEnum) passStr
return passLen
in
try `catch` \ exc ->
do hPutStrLn stderr (show (exc :: SomeException))
return 0
where
failForTooLongPassword :: Int -> IO a
failForTooLongPassword len
= fail ("callPasswordCB: the password which the callback returned is too long: "
++ "it must be at most " ++ show len ++ " bytes.")
foreign import ccall safe "PEM_write_bio_PKCS8PrivateKey"
_write_bio_PKCS8PrivateKey :: Ptr BIO_
-> Ptr EVP_PKEY
-> Ptr EVP_CIPHER
-> Ptr CChar
-> CInt
-> FunPtr PemPasswordCallback'
-> Ptr a
-> IO CInt
writePKCS8PrivateKey' :: BIO
-> PKey
-> Maybe (Cipher, PemPasswordSupply)
-> IO ()
writePKCS8PrivateKey' bio pkey encryption
= withBioPtr bio $ \ bioPtr ->
withPKeyPtr pkey $ \ pkeyPtr ->
do ret <- case encryption of
Nothing
-> _write_bio_PKCS8PrivateKey bioPtr pkeyPtr nullPtr nullPtr 0 nullFunPtr nullPtr
Just (_, PwNone)
-> _write_bio_PKCS8PrivateKey bioPtr pkeyPtr nullPtr nullPtr 0 nullFunPtr nullPtr
Just (cipher, PwStr passStr)
-> withCStringLen passStr $ \ (passPtr, passLen) ->
withCipherPtr cipher $ \ cipherPtr ->
_write_bio_PKCS8PrivateKey bioPtr pkeyPtr cipherPtr passPtr (fromIntegral passLen) nullFunPtr nullPtr
Just (cipher, PwCallback cb)
-> withCipherPtr cipher $ \ cipherPtr ->
do cbPtr <- mkPemPasswordCallback $ callPasswordCB cb
ret <- _write_bio_PKCS8PrivateKey bioPtr pkeyPtr cipherPtr nullPtr 0 cbPtr nullPtr
freeHaskellFunPtr cbPtr
return ret
Just (cipher, PwTTY)
-> withCipherPtr cipher $ \ cipherPtr ->
_write_bio_PKCS8PrivateKey bioPtr pkeyPtr cipherPtr nullPtr 0 nullFunPtr nullPtr
failIf (/= 1) ret
return ()
writePKCS8PrivateKey
:: PKey
-> Maybe (Cipher, PemPasswordSupply)
-> IO String
writePKCS8PrivateKey pkey encryption
= do mem <- newMem
writePKCS8PrivateKey' mem pkey encryption
bioRead mem
foreign import ccall safe "PEM_read_bio_PrivateKey"
_read_bio_PrivateKey :: Ptr BIO_
-> Ptr (Ptr EVP_PKEY)
-> FunPtr PemPasswordCallback'
-> Ptr ()
-> IO (Ptr EVP_PKEY)
readPrivateKey' :: BIO -> PemPasswordSupply -> IO PKey
readPrivateKey' bio supply
= withBioPtr bio $ \ bioPtr ->
do pkeyPtr <- case supply of
PwNone
-> withCString "" $ \ strPtr ->
_read_bio_PrivateKey bioPtr nullPtr nullFunPtr (castPtr strPtr)
PwStr passStr
-> do cbPtr <- mkPemPasswordCallback $
callPasswordCB $ \ _ _ ->
return passStr
pkeyPtr <- _read_bio_PrivateKey bioPtr nullPtr cbPtr nullPtr
freeHaskellFunPtr cbPtr
return pkeyPtr
PwCallback cb
-> do cbPtr <- mkPemPasswordCallback $ callPasswordCB cb
pkeyPtr <- _read_bio_PrivateKey bioPtr nullPtr cbPtr nullPtr
freeHaskellFunPtr cbPtr
return pkeyPtr
PwTTY
-> _read_bio_PrivateKey bioPtr nullPtr nullFunPtr nullPtr
failIfNull pkeyPtr
wrapPKeyPtr pkeyPtr
readPrivateKey :: String -> PemPasswordSupply -> IO PKey
readPrivateKey pemStr supply
= do mem <- newConstMem pemStr
readPrivateKey' mem supply
foreign import ccall unsafe "PEM_write_bio_PUBKEY"
_write_bio_PUBKEY :: Ptr BIO_ -> Ptr EVP_PKEY -> IO CInt
foreign import ccall unsafe "PEM_read_bio_PUBKEY"
_read_bio_PUBKEY :: Ptr BIO_
-> Ptr (Ptr EVP_PKEY)
-> FunPtr PemPasswordCallback'
-> Ptr ()
-> IO (Ptr EVP_PKEY)
writePublicKey' :: BIO -> PKey -> IO ()
writePublicKey' bio pkey
= withBioPtr bio $ \ bioPtr ->
withPKeyPtr pkey $ \ pkeyPtr ->
_write_bio_PUBKEY bioPtr pkeyPtr >>= failIf (/= 1) >> return ()
writePublicKey :: PKey -> IO String
writePublicKey pkey
= do mem <- newMem
writePublicKey' mem pkey
bioRead mem
readPublicKey' :: BIO -> IO PKey
readPublicKey' bio
= withBioPtr bio $ \ bioPtr ->
withCString "" $ \ passPtr ->
_read_bio_PUBKEY bioPtr nullPtr nullFunPtr (castPtr passPtr)
>>= failIfNull
>>= wrapPKeyPtr
readPublicKey :: String -> IO PKey
readPublicKey pemStr
= newConstMem pemStr >>= readPublicKey'
foreign import ccall unsafe "PEM_write_bio_X509_AUX"
_write_bio_X509_AUX :: Ptr BIO_
-> Ptr X509_
-> IO CInt
foreign import ccall safe "PEM_read_bio_X509_AUX"
_read_bio_X509_AUX :: Ptr BIO_
-> Ptr (Ptr X509_)
-> FunPtr PemPasswordCallback'
-> Ptr ()
-> IO (Ptr X509_)
writeX509' :: BIO -> X509 -> IO ()
writeX509' bio x509
= withBioPtr bio $ \ bioPtr ->
withX509Ptr x509 $ \ x509Ptr ->
_write_bio_X509_AUX bioPtr x509Ptr
>>= failIf (/= 1)
>> return ()
writeX509 :: X509 -> IO String
writeX509 x509
= do mem <- newMem
writeX509' mem x509
bioRead mem
readX509' :: BIO -> IO X509
readX509' bio
= withBioPtr bio $ \ bioPtr ->
withCString "" $ \ passPtr ->
_read_bio_X509_AUX bioPtr nullPtr nullFunPtr (castPtr passPtr)
>>= failIfNull
>>= wrapX509
readX509 :: String -> IO X509
readX509 pemStr
= newConstMem pemStr >>= readX509'
foreign import ccall unsafe "PEM_write_bio_X509_REQ"
_write_bio_X509_REQ :: Ptr BIO_
-> Ptr X509_REQ
-> IO CInt
foreign import ccall unsafe "PEM_write_bio_X509_REQ_NEW"
_write_bio_X509_REQ_NEW :: Ptr BIO_
-> Ptr X509_REQ
-> IO CInt
foreign import ccall safe "PEM_read_bio_X509_REQ"
_read_bio_X509_REQ :: Ptr BIO_
-> Ptr (Ptr X509_REQ)
-> FunPtr PemPasswordCallback'
-> Ptr ()
-> IO (Ptr X509_REQ)
data PemX509ReqFormat
= ReqNewFormat
| ReqOldFormat
writeX509Req' :: BIO -> X509Req -> PemX509ReqFormat -> IO ()
writeX509Req' bio req format
= withBioPtr bio $ \ bioPtr ->
withX509ReqPtr req $ \ reqPtr ->
writer bioPtr reqPtr
>>= failIf (/= 1)
>> return ()
where
writer = case format of
ReqNewFormat -> _write_bio_X509_REQ_NEW
ReqOldFormat -> _write_bio_X509_REQ
writeX509Req :: X509Req
-> PemX509ReqFormat
-> IO String
writeX509Req req format
= do mem <- newMem
writeX509Req' mem req format
bioRead mem
readX509Req' :: BIO -> IO X509Req
readX509Req' bio
= withBioPtr bio $ \ bioPtr ->
withCString "" $ \ passPtr ->
_read_bio_X509_REQ bioPtr nullPtr nullFunPtr (castPtr passPtr)
>>= failIfNull
>>= wrapX509Req
readX509Req :: String -> IO X509Req
readX509Req pemStr
= newConstMem pemStr >>= readX509Req'
foreign import ccall unsafe "PEM_write_bio_X509_CRL"
_write_bio_X509_CRL :: Ptr BIO_
-> Ptr X509_CRL
-> IO CInt
foreign import ccall safe "PEM_read_bio_X509_CRL"
_read_bio_X509_CRL :: Ptr BIO_
-> Ptr (Ptr X509_CRL)
-> FunPtr PemPasswordCallback'
-> Ptr ()
-> IO (Ptr X509_CRL)
writeCRL' :: BIO -> CRL -> IO ()
writeCRL' bio crl
= withBioPtr bio $ \ bioPtr ->
withCRLPtr crl $ \ crlPtr ->
_write_bio_X509_CRL bioPtr crlPtr
>>= failIf (/= 1)
>> return ()
writeCRL :: CRL -> IO String
writeCRL crl
= do mem <- newMem
writeCRL' mem crl
bioRead mem
readCRL' :: BIO -> IO CRL
readCRL' bio
= withBioPtr bio $ \ bioPtr ->
withCString "" $ \ passPtr ->
_read_bio_X509_CRL bioPtr nullPtr nullFunPtr (castPtr passPtr)
>>= failIfNull
>>= wrapCRL
readCRL :: String -> IO CRL
readCRL pemStr
= newConstMem pemStr >>= readCRL'
foreign import ccall unsafe "PEM_write_bio_PKCS7"
_write_bio_PKCS7 :: Ptr BIO_
-> Ptr PKCS7
-> IO CInt
foreign import ccall safe "PEM_read_bio_PKCS7"
_read_bio_PKCS7 :: Ptr BIO_
-> Ptr (Ptr PKCS7)
-> FunPtr PemPasswordCallback'
-> Ptr ()
-> IO (Ptr PKCS7)
writePkcs7' :: BIO -> Pkcs7 -> IO ()
writePkcs7' bio pkcs7
= withBioPtr bio $ \ bioPtr ->
withPkcs7Ptr pkcs7 $ \ pkcs7Ptr ->
_write_bio_PKCS7 bioPtr pkcs7Ptr
>>= failIf (/= 1)
>> return ()
writePkcs7 :: Pkcs7 -> IO String
writePkcs7 pkcs7
= do mem <- newMem
writePkcs7' mem pkcs7
bioRead mem
readPkcs7' :: BIO -> IO Pkcs7
readPkcs7' bio
= withBioPtr bio $ \ bioPtr ->
withCString "" $ \ passPtr ->
_read_bio_PKCS7 bioPtr nullPtr nullFunPtr (castPtr passPtr)
>>= failIfNull
>>= wrapPkcs7Ptr
readPkcs7 :: String -> IO Pkcs7
readPkcs7 pemStr
= newConstMem pemStr >>= readPkcs7'