Skip to content

Commit

Permalink
Merge pull request #94 from glguy/glguy/pointer-fixes
Browse files Browse the repository at this point in the history
Fix up incompatible pointer issues
  • Loading branch information
Kleidukos authored Aug 19, 2024
2 parents b7a4999 + 7c90fc3 commit 57b22b1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
6 changes: 3 additions & 3 deletions OpenSSL/BIO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ newBuffer bufSize

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

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

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


Expand Down Expand Up @@ -466,7 +466,7 @@ newConstMemLBS lbs

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

foreign import capi unsafe "openssl/bio.h BIO_s_null"
foreign import ccall 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
8 changes: 4 additions & 4 deletions OpenSSL/BN.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ withBN :: Integer -> (BigNum -> IO a) -> IO a
withBN dec m = bracket (integerToBN dec) (_free . unwrapBN) m

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

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

-- |This is an alias to 'bnToInteger'.
peekBN :: BigNum -> IO Integer
Expand All @@ -265,13 +265,13 @@ bnToMPI bn = do
bytes <- _bn2mpi (unwrapBN bn) nullPtr
allocaBytes (fromIntegral bytes) (\buffer -> do
_ <- _bn2mpi (unwrapBN bn) buffer
BS.packCStringLen (buffer, fromIntegral bytes))
BS.packCStringLen (castPtr buffer, fromIntegral bytes))

-- | Convert an MPI into a BigNum. See bnToMPI for details of the format
mpiToBN :: BS.ByteString -> IO BigNum
mpiToBN mpi = do
BS.useAsCStringLen mpi (\(ptr, len) -> do
_mpi2bn ptr (fromIntegral len) nullPtr) >>= return . wrapBN
_mpi2bn (castPtr ptr) (fromIntegral len) nullPtr) >>= return . wrapBN

-- | Convert an Integer to an MPI. See bnToMPI for the format
integerToMPI :: Integer -> IO BS.ByteString
Expand Down
12 changes: 6 additions & 6 deletions OpenSSL/EVP/Base64.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8
import Data.List
#if MIN_VERSION_base(4,5,0)
import Foreign.C.Types (CChar(..), CInt(..))
import Foreign.C.Types (CUChar(..), CInt(..))
#else
import Foreign.C.Types (CChar, CInt)
import Foreign.C.Types (CUChar, CInt)
#endif
import Foreign.Ptr (Ptr, castPtr)
import System.IO.Unsafe (unsafePerformIO)
Expand All @@ -50,7 +50,7 @@ nextBlock minLen (xs, src)
{- encode -------------------------------------------------------------------- -}

foreign import capi unsafe "openssl/evp.h EVP_EncodeBlock"
_EncodeBlock :: Ptr CChar -> Ptr CChar -> CInt -> IO CInt
_EncodeBlock :: Ptr CUChar -> Ptr CUChar -> CInt -> IO CInt


encodeBlock :: B8.ByteString -> B8.ByteString
Expand All @@ -59,7 +59,7 @@ encodeBlock inBS
unsafeUseAsCStringLen inBS $ \ (inBuf, inLen) ->
createAndTrim maxOutLen $ \ outBuf ->
fmap fromIntegral
(_EncodeBlock (castPtr outBuf) inBuf (fromIntegral inLen))
(_EncodeBlock (castPtr outBuf) (castPtr inBuf) (fromIntegral inLen))
where
maxOutLen = (inputLen `div` 3 + 1) * 4 + 1 -- +1: '\0'
inputLen = B8.length inBS
Expand Down Expand Up @@ -104,7 +104,7 @@ encodeBase64LBS inLBS
{- decode -------------------------------------------------------------------- -}

foreign import capi unsafe "openssl/evp.h EVP_DecodeBlock"
_DecodeBlock :: Ptr CChar -> Ptr CChar -> CInt -> IO CInt
_DecodeBlock :: Ptr CUChar -> Ptr CUChar -> CInt -> IO CInt


decodeBlock :: B8.ByteString -> B8.ByteString
Expand All @@ -113,7 +113,7 @@ decodeBlock inBS
unsafePerformIO $
unsafeUseAsCStringLen inBS $ \ (inBuf, inLen) ->
createAndTrim (B8.length inBS) $ \ outBuf ->
_DecodeBlock (castPtr outBuf) inBuf (fromIntegral inLen)
_DecodeBlock (castPtr outBuf) (castPtr inBuf) (fromIntegral inLen)
>>= \ outLen -> return (fromIntegral outLen - paddingLen)
where
paddingLen :: Int
Expand Down
6 changes: 3 additions & 3 deletions OpenSSL/EVP/Internal.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import qualified Data.ByteString.Lazy.Internal as L8
import Control.Applicative ((<$>))
#endif
import Control.Exception (mask, mask_, bracket, onException)
import Foreign.C.Types (CChar)
import Foreign.C.Types (CChar, CUChar)
#if MIN_VERSION_base(4,5,0)
import Foreign.C.Types (CInt(..), CUInt(..), CSize(..))
#else
Expand Down Expand Up @@ -335,10 +335,10 @@ foreign import capi unsafe "openssl/hmac.h HMAC_Init"
_hmac_init :: Ptr HMAC_CTX -> Ptr () -> CInt -> Ptr EVP_MD -> IO CInt

foreign import capi unsafe "openssl/hmac.h HMAC_Update"
_hmac_update :: Ptr HMAC_CTX -> Ptr CChar -> CInt -> IO CInt
_hmac_update :: Ptr HMAC_CTX -> Ptr CUChar -> CSize -> IO CInt

foreign import capi unsafe "openssl/hmac.h HMAC_Final"
_hmac_final :: Ptr HMAC_CTX -> Ptr CChar -> Ptr CInt -> IO CUInt
_hmac_final :: Ptr HMAC_CTX -> Ptr CUChar -> Ptr CUInt -> IO CUInt

foreign import capi unsafe "HsOpenSSL &HsOpenSSL_HMAC_CTX_free"
_hmac_ctx_free :: FunPtr (Ptr HMAC_CTX -> IO ())
Expand Down
12 changes: 0 additions & 12 deletions cbits/HsOpenSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@
#include <openssl/x509v3.h>
#include <openssl/dsa.h>

/* A dirty hack to work around for broken versions of Cabal:
* https://github.com/phonohawk/HsOpenSSL/issues/8
*
* The trick is to abuse the fact that -Icbits is (almost) always
* passed to hsc2hs so we can reach the cabal_macros.h from cbits, but
* see #23, #24 and #25...
*/
#if !defined(MIN_VERSION_base) && \
!defined(HSOPENSSL_NEED_NOT_INCLUDE_CABAL_MACROS_H)
# include "../dist/build/autogen/cabal_macros.h"
#endif

/* LibreSSL *******************************************************************/
#if (defined LIBRESSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER == 0x20000000L)
#undef OPENSSL_VERSION_NUMBER
Expand Down

0 comments on commit 57b22b1

Please sign in to comment.