Skip to content

Commit

Permalink
Converted FFI imports from ccall to capi.
Browse files Browse the repository at this point in the history
Fixes #73. Part of #63.
  • Loading branch information
vshabanov committed Dec 17, 2022
1 parent 2b57dfe commit c41e27d
Show file tree
Hide file tree
Showing 32 changed files with 379 additions and 339 deletions.
5 changes: 3 additions & 2 deletions OpenSSL.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
-- |HsOpenSSL is an OpenSSL binding for Haskell. It can generate RSA
-- and DSA keys, read and write PEM files, generate message digests,
-- sign and verify messages, encrypt and decrypt messages.
Expand Down Expand Up @@ -52,10 +53,10 @@ import System.IO.Unsafe
import Control.Exception (onException, mask_)
#endif

foreign import ccall "HsOpenSSL_init"
foreign import capi "HsOpenSSL.h HsOpenSSL_init"
initSSL :: IO ()

foreign import ccall "HsOpenSSL_setupMutex"
foreign import capi "HsOpenSSL.h HsOpenSSL_setupMutex"
setupMutex :: IO ()


Expand Down
33 changes: 17 additions & 16 deletions OpenSSL/ASN1.hsc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
module OpenSSL.ASN1
( ASN1_OBJECT
, obj2nid
Expand Down Expand Up @@ -36,15 +37,15 @@ import System.Locale

{- ASN1_OBJECT --------------------------------------------------------------- -}

data ASN1_OBJECT
data {-# CTYPE "openssl/asn1.h" "ASN1_OBJECT" #-} ASN1_OBJECT

foreign import ccall unsafe "OBJ_obj2nid"
foreign import capi unsafe "openssl/objects.h OBJ_obj2nid"
obj2nid :: Ptr ASN1_OBJECT -> IO CInt

foreign import ccall unsafe "OBJ_nid2sn"
foreign import capi unsafe "openssl/objects.h OBJ_nid2sn"
_nid2sn :: CInt -> IO CString

foreign import ccall unsafe "OBJ_nid2ln"
foreign import capi unsafe "openssl/objects.h OBJ_nid2ln"
_nid2ln :: CInt -> IO CString


Expand All @@ -58,7 +59,7 @@ nid2ln nid = _nid2ln nid >>= peekCString

{- ASN1_STRING --------------------------------------------------------------- -}

data ASN1_STRING
data {-# CTYPE "openssl/asn1.h" "ASN1_STRING" #-} ASN1_STRING

peekASN1String :: Ptr ASN1_STRING -> IO String
peekASN1String strPtr
Expand All @@ -69,18 +70,18 @@ peekASN1String strPtr

{- ASN1_INTEGER -------------------------------------------------------------- -}

data ASN1_INTEGER
data {-# CTYPE "openssl/asn1.h" "ASN1_INTEGER" #-} ASN1_INTEGER

foreign import ccall unsafe "HsOpenSSL_M_ASN1_INTEGER_new"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_M_ASN1_INTEGER_new"
_ASN1_INTEGER_new :: IO (Ptr ASN1_INTEGER)

foreign import ccall unsafe "HsOpenSSL_M_ASN1_INTEGER_free"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_M_ASN1_INTEGER_free"
_ASN1_INTEGER_free :: Ptr ASN1_INTEGER -> IO ()

foreign import ccall unsafe "ASN1_INTEGER_to_BN"
foreign import capi unsafe "openssl/asn1.h ASN1_INTEGER_to_BN"
_ASN1_INTEGER_to_BN :: Ptr ASN1_INTEGER -> Ptr BIGNUM -> IO (Ptr BIGNUM)

foreign import ccall unsafe "BN_to_ASN1_INTEGER"
foreign import capi unsafe "openssl/asn1.h BN_to_ASN1_INTEGER"
_BN_to_ASN1_INTEGER :: Ptr BIGNUM -> Ptr ASN1_INTEGER -> IO (Ptr ASN1_INTEGER)


Expand Down Expand Up @@ -108,18 +109,18 @@ withASN1Integer int m

{- ASN1_TIME ---------------------------------------------------------------- -}

data ASN1_TIME
data {-# CTYPE "openssl/asn1.h" "ASN1_TIME" #-} ASN1_TIME

foreign import ccall unsafe "HsOpenSSL_M_ASN1_TIME_new"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_M_ASN1_TIME_new"
_ASN1_TIME_new :: IO (Ptr ASN1_TIME)

foreign import ccall unsafe "HsOpenSSL_M_ASN1_TIME_free"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_M_ASN1_TIME_free"
_ASN1_TIME_free :: Ptr ASN1_TIME -> IO ()

foreign import ccall unsafe "ASN1_TIME_set"
foreign import capi unsafe "openssl/asn1.h ASN1_TIME_set"
_ASN1_TIME_set :: Ptr ASN1_TIME -> CTime -> IO (Ptr ASN1_TIME)

foreign import ccall unsafe "ASN1_TIME_print"
foreign import capi unsafe "openssl/asn1.h ASN1_TIME_print"
_ASN1_TIME_print :: Ptr BIO_ -> Ptr ASN1_TIME -> IO CInt


Expand All @@ -130,7 +131,7 @@ peekASN1Time time
_ASN1_TIME_print bioPtr time
>>= failIf_ (/= 1)
timeStr <- bioRead bio
#if MIN_VERSION_time(1,5,0)
#if MIN_VERSION_time(1,5,0)
case parseTimeM True defaultTimeLocale "%b %e %H:%M:%S %Y %Z" timeStr of
#else
case parseTime defaultTimeLocale "%b %e %H:%M:%S %Y %Z" timeStr of
Expand Down
47 changes: 24 additions & 23 deletions OpenSSL/BIO.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
{- --------------------------------------------------------------------------- -}
{- -}
{- FOR INTERNAL USE ONLY -}
Expand Down Expand Up @@ -87,25 +88,25 @@ import System.IO.Unsafe

{- bio ---------------------------------------------------------------------- -}

data BIO_METHOD
data {-# CTYPE "openssl/bio.h" "BIO_METHOD" #-} BIO_METHOD

-- |@BIO@ is a @ForeignPtr@ to an opaque BIO object. They are created by newXXX actions.
newtype BIO = BIO (ForeignPtr BIO_)
data BIO_
data {-# CTYPE "openssl/bio.h" "BIO" #-} BIO_

foreign import ccall unsafe "BIO_new"
foreign import capi unsafe "openssl/bio.h BIO_new"
_new :: Ptr BIO_METHOD -> IO (Ptr BIO_)

foreign import ccall unsafe "BIO_free"
foreign import capi unsafe "openssl/bio.h BIO_free"
_free :: Ptr BIO_ -> IO ()

foreign import ccall unsafe "BIO_push"
foreign import capi unsafe "openssl/bio.h BIO_push"
_push :: Ptr BIO_ -> Ptr BIO_ -> IO (Ptr BIO_)

foreign import ccall unsafe "HsOpenSSL_BIO_set_flags"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_set_flags"
_set_flags :: Ptr BIO_ -> CInt -> IO ()

foreign import ccall unsafe "HsOpenSSL_BIO_should_retry"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_should_retry"
_should_retry :: Ptr BIO_ -> IO CInt


Expand Down Expand Up @@ -178,7 +179,7 @@ bioJoin (a:b:xs) = bioPush a b >> bioJoin (b:xs)
setFlags :: BIO -> CInt -> IO ()
setFlags bio flags
= withBioPtr bio $ flip _set_flags flags


bioShouldRetry :: BIO -> IO Bool
bioShouldRetry bio
Expand All @@ -188,13 +189,13 @@ bioShouldRetry bio

{- ctrl --------------------------------------------------------------------- -}

foreign import ccall unsafe "HsOpenSSL_BIO_flush"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_flush"
_flush :: Ptr BIO_ -> IO CInt

foreign import ccall unsafe "HsOpenSSL_BIO_reset"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_reset"
_reset :: Ptr BIO_ -> IO CInt

foreign import ccall unsafe "HsOpenSSL_BIO_eof"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_eof"
_eof :: Ptr BIO_ -> IO CInt

-- |@'bioFlush' bio@ normally writes out any internally buffered data,
Expand Down Expand Up @@ -223,13 +224,13 @@ bioEOF bio

{- I/O ---------------------------------------------------------------------- -}

foreign import ccall unsafe "BIO_read"
foreign import capi unsafe "openssl/bio.h BIO_read"
_read :: Ptr BIO_ -> Ptr CChar -> CInt -> IO CInt

foreign import ccall unsafe "BIO_gets"
foreign import capi unsafe "openssl/bio.h BIO_gets"
_gets :: Ptr BIO_ -> Ptr CChar -> CInt -> IO CInt

foreign import ccall unsafe "BIO_write"
foreign import capi unsafe "openssl/bio.h BIO_write"
_write :: Ptr BIO_ -> Ptr CChar -> CInt -> IO CInt

-- |@'bioRead' bio@ lazily reads all data in @bio@.
Expand Down Expand Up @@ -259,7 +260,7 @@ bioReadLBS :: BIO -> IO L.ByteString
bioReadLBS bio = fmap L.fromChunks lazyRead
where
chunkSize = L.defaultChunkSize

lazyRead = unsafeInterleaveIO loop

loop = do bs <- bioReadBS bio chunkSize
Expand Down Expand Up @@ -335,10 +336,10 @@ bioWriteLBS bio lbs

{- base64 ------------------------------------------------------------------- -}

foreign import ccall unsafe "BIO_f_base64"
foreign import capi unsafe "openssl/bio.h BIO_f_base64"
f_base64 :: IO (Ptr BIO_METHOD)

foreign import ccall unsafe "HsOpenSSL_BIO_FLAGS_BASE64_NO_NL"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_FLAGS_BASE64_NO_NL"
_FLAGS_BASE64_NO_NL :: CInt

-- |@'newBase64' noNL@ creates a Base64 BIO filter. This is a filter
Expand All @@ -362,10 +363,10 @@ newBase64 noNL

{- buffer ------------------------------------------------------------------- -}

foreign import ccall unsafe "BIO_f_buffer"
foreign import capi unsafe "openssl/bio.h BIO_f_buffer"
f_buffer :: IO (Ptr BIO_METHOD)

foreign import ccall unsafe "HsOpenSSL_BIO_set_buffer_size"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_BIO_set_buffer_size"
_set_buffer_size :: Ptr BIO_ -> CInt -> IO CInt


Expand Down Expand Up @@ -410,10 +411,10 @@ newBuffer bufSize

{- mem ---------------------------------------------------------------------- -}

foreign import ccall unsafe "BIO_s_mem"
foreign import capi unsafe "openssl/bio.h BIO_s_mem"
s_mem :: IO (Ptr BIO_METHOD)

foreign import ccall unsafe "BIO_new_mem_buf"
foreign import capi unsafe "openssl/bio.h BIO_new_mem_buf"
_new_mem_buf :: Ptr CChar -> CInt -> IO (Ptr BIO_)


Expand Down Expand Up @@ -454,7 +455,7 @@ newConstMemBS bs

bio <- newForeignPtr_ bioPtr
Conc.addForeignPtrFinalizer bio (_free bioPtr >> touchForeignPtr foreignBuf)

return $ BIO bio

-- |@'newConstMemLBS' lbs@ is like 'newConstMem' but takes a
Expand All @@ -465,7 +466,7 @@ newConstMemLBS lbs

{- null --------------------------------------------------------------------- -}

foreign import ccall unsafe "BIO_s_null"
foreign import capi unsafe "openssl/bio.h BIO_s_null"
s_null :: IO (Ptr BIO_METHOD)

-- |@'newNullBIO'@ creates a null BIO sink\/source. Data written to
Expand Down
35 changes: 18 additions & 17 deletions OpenSSL/BN.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endif
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
#if defined(FAST_BIGNUM)
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
Expand Down Expand Up @@ -69,13 +70,13 @@ import Foreign.C

-- |'BigNum' is an opaque object representing a big number.
newtype BigNum = BigNum (Ptr BIGNUM)
data BIGNUM
data {-# CTYPE "openssl/bn.h" "BIGNUM" #-} BIGNUM


foreign import ccall unsafe "BN_new"
foreign import capi unsafe "openssl/bn.h BN_new"
_new :: IO (Ptr BIGNUM)

foreign import ccall unsafe "BN_free"
foreign import capi unsafe "openssl/bn.h BN_free"
_free :: Ptr BIGNUM -> IO ()

-- |@'allocaBN' f@ allocates a 'BigNum' and computes @f@. Then it
Expand All @@ -97,13 +98,13 @@ wrapBN = BigNum

{- slow, safe functions ----------------------------------------------------- -}

foreign import ccall unsafe "BN_bn2dec"
foreign import capi unsafe "openssl/bn.h BN_bn2dec"
_bn2dec :: Ptr BIGNUM -> IO CString

foreign import ccall unsafe "BN_dec2bn"
foreign import capi unsafe "openssl/bn.h BN_dec2bn"
_dec2bn :: Ptr (Ptr BIGNUM) -> CString -> IO CInt

foreign import ccall unsafe "HsOpenSSL_OPENSSL_free"
foreign import capi unsafe "HsOpenSSL.h HsOpenSSL_OPENSSL_free"
_openssl_free :: Ptr a -> IO ()

-- |Convert a BIGNUM to an 'Integer'.
Expand Down Expand Up @@ -137,10 +138,10 @@ integerToBN i = do
-- GC could do nasty things to the data which we thought that we had a pointer
-- to

foreign import ccall unsafe "memcpy"
foreign import capi unsafe "string.h memcpy"
_copy_in :: ByteArray## -> Ptr () -> CSize -> IO (Ptr ())

foreign import ccall unsafe "memcpy"
foreign import capi unsafe "string.h memcpy"
_copy_out :: Ptr () -> ByteArray## -> CSize -> IO (Ptr ())

-- These are taken from Data.Binary's disabled fast Integer support
Expand Down Expand Up @@ -242,10 +243,10 @@ integerToBN v =
withBN :: Integer -> (BigNum -> IO a) -> IO a
withBN dec m = bracket (integerToBN dec) (_free . unwrapBN) m

foreign import ccall unsafe "BN_bn2mpi"
foreign import capi unsafe "openssl/bn.h BN_bn2mpi"
_bn2mpi :: Ptr BIGNUM -> Ptr CChar -> IO CInt

foreign import ccall unsafe "BN_mpi2bn"
foreign import capi unsafe "openssl/bn.h BN_mpi2bn"
_mpi2bn :: Ptr CChar -> CInt -> Ptr BIGNUM -> IO (Ptr BIGNUM)

-- |This is an alias to 'bnToInteger'.
Expand Down Expand Up @@ -284,16 +285,16 @@ mpiToInteger mpi = do
_free (unwrapBN bn)
return v

foreign import ccall unsafe "BN_mod_exp"
_mod_exp :: Ptr BIGNUM -> Ptr BIGNUM -> Ptr BIGNUM -> Ptr BIGNUM -> BNCtx -> IO (Ptr BIGNUM)
foreign import capi unsafe "openssl/bn.h BN_mod_exp"
_mod_exp :: Ptr BIGNUM -> Ptr BIGNUM -> Ptr BIGNUM -> Ptr BIGNUM -> BNCtx -> IO CInt

type BNCtx = Ptr BNCTX
data BNCTX
data {-# CTYPE "openssl/bn.h" "BN_CTX" #-} BNCTX

foreign import ccall unsafe "BN_CTX_new"
foreign import capi unsafe "openssl/bn.h BN_CTX_new"
_BN_ctx_new :: IO BNCtx

foreign import ccall unsafe "BN_CTX_free"
foreign import capi unsafe "openssl/bn.h BN_CTX_free"
_BN_ctx_free :: BNCtx -> IO ()

withBNCtx :: (BNCtx -> IO a) -> IO a
Expand All @@ -312,10 +313,10 @@ modexp a p m = unsafePerformIO (do

{- Random Integer generation ------------------------------------------------ -}

foreign import ccall unsafe "BN_rand_range"
foreign import capi unsafe "openssl/bn.h BN_rand_range"
_BN_rand_range :: Ptr BIGNUM -> Ptr BIGNUM -> IO CInt

foreign import ccall unsafe "BN_pseudo_rand_range"
foreign import capi unsafe "openssl/bn.h BN_pseudo_rand_range"
_BN_pseudo_rand_range :: Ptr BIGNUM -> Ptr BIGNUM -> IO CInt

-- | Return a strongly random number in the range 0 <= x < n where the given
Expand Down
Loading

0 comments on commit c41e27d

Please sign in to comment.