-- | Internal module, not subject to PVP.
module Rustls.Internal.FFI
  ( ConstPtr (..),
    ConstCString,

    -- * Client

    -- ** Config
    ClientConfig,
    ClientConfigBuilder,
    clientConfigBuilderNewCustom,
    clientConfigBuilderFree,
    clientConfigBuilderBuild,
    clientConfigFree,
    clientConfigBuilderSetALPNProtocols,
    clientConfigBuilderSetEnableSNI,
    clientConfigBuilderSetCertifiedKey,
    WebPkiServerCertVerifierBuilder,
    ServerCertVerifier,
    webPkiServerCertVerifierBuilderNewWithProvider,
    webPkiServerCertVerifierBuilderAddCrl,
    webPkiServerCertVerifierBuilderFree,
    webPkiServerCertVerifierBuilderBuild,
    platformServerCertVerifierWithProvider,
    serverCertVerifierFree,
    clientConfigBuilderSetServerVerifier,

    -- ** Connection
    clientConnectionNew,
    serverConnectionNew,

    -- * Server

    -- ** Config
    ServerConfig,
    ServerConfigBuilder,
    serverConfigBuilderNewCustom,
    serverConfigBuilderFree,
    serverConfigBuilderBuild,
    serverConfigFree,
    serverConfigBuilderSetALPNProtocols,
    serverConfigBuilderSetIgnoreClientOrder,
    serverConfigBuilderSetCertifiedKeys,
    WebPkiClientCertVerifierBuilder,
    ClientCertVerifier,
    webPkiClientCertVerifierBuilderNewWithProvider,
    webPkiClientCertVerifierBuilderAddCrl,
    webPkiClientCertVerifierBuilderAllowUnauthenticated,
    webPkiClientCertVerifierBuilderFree,
    webPkiClientCertVerifierBuilderBuild,
    clientCertVerifierFree,
    serverConfigBuilderSetClientVerifier,

    -- * Certificate stuff
    CertifiedKey,
    certifiedKeyBuild,
    certifiedKeyFree,
    Certificate,
    certificateGetDER,

    -- * Connection
    Connection,
    connectionFree,

    -- ** Read/write

    -- *** Read
    ReadCallback,
    mkReadCallback,
    connectionWantsRead,
    connectionRead,
    connectionReadTls,

    -- *** Write
    WriteCallback,
    mkWriteCallback,
    connectionWantsWrite,
    connectionWrite,
    connectionWriteTls,

    -- ** Misc
    connectionProcessNewPackets,
    connectionIsHandshaking,
    connectionSendCloseNotify,
    connectionSetBufferLimit,
    connectionGetALPNProtocol,
    connectionGetProtocolVersion,
    connectionGetNegotiatedCipherSuite,
    connectionGetNegotiatedCipherSuiteName,
    serverConnectionGetSNIHostname,
    connectionGetPeerCertificate,

    -- ** Logging
    connectionSetLogCallback,
    LogCallback,
    mkLogCallback,
    LogParams (..),
    LogLevel (..),

    -- * Misc
    Str (..),
    SliceBytes (..),
    hsVersion,
    Userdata,

    -- ** 'Result'
    Result (..),
    resultIsCertError,
    errorMsg,

    -- *** Some values
    resultOk,
    resultInsufficientSize,

    -- ** 'IOResult'
    IOResult (..),
    ioResultOk,
    ioResultErr,

    -- ** TLS params
    SupportedCipherSuite,
    supportedCipherSuiteGetSuite,
    hsSupportedCipherSuiteGetName,
    hsSupportedCiphersuiteProtocolVersion,
    TLSVersion (..),
    pattern TLS12,
    pattern TLS13,

    -- ** Crypto provider
    CryptoProvider,
    CryptoProviderBuilder,
    cryptoProviderBuilderNewFromDefault,
    cryptoProviderBuilderNewWithBase,
    cryptoProviderBuilderSetCipherSuites,
    cryptoProviderBuilderBuild,
    cryptoProviderBuilderFree,
    cryptoProviderFree,
    cryptoProviderCiphersuitesLen,
    cryptoProviderCiphersuitesGet,

    -- ** Root cert store
    RootCertStoreBuilder,
    RootCertStore,
    rootCertStoreBuilderNew,
    rootCertStoreBuilderAddPem,
    rootCertStoreBuilderLoadRootsFromFile,
    rootCertStoreBuilderFree,
    rootCertStoreBuilderBuild,
    rootCertStoreFree,
  )
where

import Data.Word
import Foreign
import Foreign.C
import Foreign.Storable.Generic
import GHC.Generics (Generic)

#if MIN_VERSION_base(4,18,0)
import Foreign.C.ConstPtr
#else
newtype ConstPtr a = ConstPtr {unConstPtr :: Ptr a}
  deriving newtype (Show, Eq, Storable)
#endif

type ConstCString = ConstPtr CChar

-- Misc

data {-# CTYPE "rustls.h" "rustls_str" #-} Str = Str CString CSize
  deriving stock ((forall x. Str -> Rep Str x)
-> (forall x. Rep Str x -> Str) -> Generic Str
forall x. Rep Str x -> Str
forall x. Str -> Rep Str x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Str -> Rep Str x
from :: forall x. Str -> Rep Str x
$cto :: forall x. Rep Str x -> Str
to :: forall x. Rep Str x -> Str
Generic)
  deriving anyclass (Str -> Int
(Str -> Int)
-> (Str -> Int)
-> (forall b. Ptr b -> Int -> IO Str)
-> (forall b. Ptr b -> Int -> Str -> IO ())
-> GStorable Str
forall b. Ptr b -> Int -> IO Str
forall b. Ptr b -> Int -> Str -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> GStorable a
$cgsizeOf :: Str -> Int
gsizeOf :: Str -> Int
$cgalignment :: Str -> Int
galignment :: Str -> Int
$cgpeekByteOff :: forall b. Ptr b -> Int -> IO Str
gpeekByteOff :: forall b. Ptr b -> Int -> IO Str
$cgpokeByteOff :: forall b. Ptr b -> Int -> Str -> IO ()
gpokeByteOff :: forall b. Ptr b -> Int -> Str -> IO ()
GStorable)

data {-# CTYPE "rustls.h" "rustls_slice_bytes" #-} SliceBytes = SliceBytes (Ptr Word8) CSize
  deriving stock ((forall x. SliceBytes -> Rep SliceBytes x)
-> (forall x. Rep SliceBytes x -> SliceBytes) -> Generic SliceBytes
forall x. Rep SliceBytes x -> SliceBytes
forall x. SliceBytes -> Rep SliceBytes x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SliceBytes -> Rep SliceBytes x
from :: forall x. SliceBytes -> Rep SliceBytes x
$cto :: forall x. Rep SliceBytes x -> SliceBytes
to :: forall x. Rep SliceBytes x -> SliceBytes
Generic)
  deriving anyclass (SliceBytes -> Int
(SliceBytes -> Int)
-> (SliceBytes -> Int)
-> (forall b. Ptr b -> Int -> IO SliceBytes)
-> (forall b. Ptr b -> Int -> SliceBytes -> IO ())
-> GStorable SliceBytes
forall b. Ptr b -> Int -> IO SliceBytes
forall b. Ptr b -> Int -> SliceBytes -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> GStorable a
$cgsizeOf :: SliceBytes -> Int
gsizeOf :: SliceBytes -> Int
$cgalignment :: SliceBytes -> Int
galignment :: SliceBytes -> Int
$cgpeekByteOff :: forall b. Ptr b -> Int -> IO SliceBytes
gpeekByteOff :: forall b. Ptr b -> Int -> IO SliceBytes
$cgpokeByteOff :: forall b. Ptr b -> Int -> SliceBytes -> IO ()
gpokeByteOff :: forall b. Ptr b -> Int -> SliceBytes -> IO ()
GStorable)

foreign import capi unsafe "hs_rustls.h hs_rustls_version"
  hsVersion :: Ptr Str -> IO ()

newtype {-# CTYPE "rustls.h" "rustls_result" #-} Result = Result Word32
  deriving stock (Int -> Result -> ShowS
[Result] -> ShowS
Result -> String
(Int -> Result -> ShowS)
-> (Result -> String) -> ([Result] -> ShowS) -> Show Result
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Result -> ShowS
showsPrec :: Int -> Result -> ShowS
$cshow :: Result -> String
show :: Result -> String
$cshowList :: [Result] -> ShowS
showList :: [Result] -> ShowS
Show, Result -> Result -> Bool
(Result -> Result -> Bool)
-> (Result -> Result -> Bool) -> Eq Result
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Result -> Result -> Bool
== :: Result -> Result -> Bool
$c/= :: Result -> Result -> Bool
/= :: Result -> Result -> Bool
Eq, Eq Result
Eq Result =>
(Result -> Result -> Ordering)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Result)
-> (Result -> Result -> Result)
-> Ord Result
Result -> Result -> Bool
Result -> Result -> Ordering
Result -> Result -> Result
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Result -> Result -> Ordering
compare :: Result -> Result -> Ordering
$c< :: Result -> Result -> Bool
< :: Result -> Result -> Bool
$c<= :: Result -> Result -> Bool
<= :: Result -> Result -> Bool
$c> :: Result -> Result -> Bool
> :: Result -> Result -> Bool
$c>= :: Result -> Result -> Bool
>= :: Result -> Result -> Bool
$cmax :: Result -> Result -> Result
max :: Result -> Result -> Result
$cmin :: Result -> Result -> Result
min :: Result -> Result -> Result
Ord)

foreign import capi "rustls.h value RUSTLS_RESULT_OK"
  resultOk :: Result

foreign import capi "rustls.h value RUSTLS_RESULT_INSUFFICIENT_SIZE"
  resultInsufficientSize :: Result

foreign import capi unsafe "rustls.h rustls_result_is_cert_error"
  resultIsCertError :: Result -> CBool

foreign import capi unsafe "rustls.h rustls_error"
  errorMsg :: Result -> CString -> CSize -> Ptr CSize -> IO ()

newtype {-# CTYPE "rustls.h" "rustls_io_result" #-} IOResult = IOResult CInt
  deriving stock (IOResult -> IOResult -> Bool
(IOResult -> IOResult -> Bool)
-> (IOResult -> IOResult -> Bool) -> Eq IOResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IOResult -> IOResult -> Bool
== :: IOResult -> IOResult -> Bool
$c/= :: IOResult -> IOResult -> Bool
/= :: IOResult -> IOResult -> Bool
Eq)

ioResultOk :: IOResult
ioResultOk :: IOResult
ioResultOk = CInt -> IOResult
IOResult CInt
0

ioResultErr :: IOResult
ioResultErr :: IOResult
ioResultErr = CInt -> IOResult
IOResult CInt
1

-- | (Unused) userdata.
data Userdata

-- Client

data {-# CTYPE "rustls.h" "rustls_client_config" #-} ClientConfig

data {-# CTYPE "rustls.h" "rustls_client_config_builder" #-} ClientConfigBuilder

foreign import capi unsafe "rustls.h rustls_client_config_builder_new_custom"
  clientConfigBuilderNewCustom ::
    ConstPtr CryptoProvider ->
    ConstPtr TLSVersion ->
    CSize ->
    Ptr (Ptr ClientConfigBuilder) ->
    IO Result

foreign import capi unsafe "rustls.h rustls_client_config_builder_free"
  clientConfigBuilderFree :: Ptr ClientConfigBuilder -> IO ()

foreign import capi unsafe "rustls.h rustls_client_config_builder_build"
  clientConfigBuilderBuild ::
    Ptr ClientConfigBuilder -> Ptr (ConstPtr ClientConfig) -> IO Result

foreign import capi unsafe "rustls.h &rustls_client_config_free"
  clientConfigFree :: FinalizerPtr ClientConfig

foreign import capi unsafe "rustls.h rustls_client_connection_new"
  clientConnectionNew ::
    ConstPtr ClientConfig ->
    -- | Hostname.
    ConstCString ->
    Ptr (Ptr Connection) ->
    IO Result

foreign import capi unsafe "rustls.h rustls_client_config_builder_set_alpn_protocols"
  clientConfigBuilderSetALPNProtocols ::
    Ptr ClientConfigBuilder -> ConstPtr SliceBytes -> CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_client_config_builder_set_enable_sni"
  clientConfigBuilderSetEnableSNI :: Ptr ClientConfigBuilder -> CBool -> IO ()

foreign import capi unsafe "rustls.h rustls_client_config_builder_set_certified_key"
  clientConfigBuilderSetCertifiedKey ::
    Ptr ClientConfigBuilder -> ConstPtr (ConstPtr CertifiedKey) -> CSize -> IO Result

data
  {-# CTYPE "rustls.h" "rustls_web_pki_server_cert_verifier_builder" #-}
  WebPkiServerCertVerifierBuilder

data
  {-# CTYPE "rustls.h" "rustls_server_cert_verifier" #-}
  ServerCertVerifier

foreign import capi unsafe "rustls.h rustls_web_pki_server_cert_verifier_builder_new_with_provider"
  webPkiServerCertVerifierBuilderNewWithProvider ::
    ConstPtr CryptoProvider ->
    ConstPtr RootCertStore ->
    IO (Ptr WebPkiServerCertVerifierBuilder)

foreign import capi unsafe "rustls.h rustls_web_pki_server_cert_verifier_builder_add_crl"
  webPkiServerCertVerifierBuilderAddCrl ::
    Ptr WebPkiServerCertVerifierBuilder -> ConstPtr Word8 -> CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_web_pki_server_cert_verifier_builder_free"
  webPkiServerCertVerifierBuilderFree ::
    Ptr WebPkiServerCertVerifierBuilder -> IO ()

foreign import capi unsafe "rustls.h rustls_web_pki_server_cert_verifier_builder_build"
  webPkiServerCertVerifierBuilderBuild ::
    Ptr WebPkiServerCertVerifierBuilder -> Ptr (Ptr ServerCertVerifier) -> IO Result

foreign import capi unsafe "rustls.h rustls_platform_server_cert_verifier_with_provider"
  platformServerCertVerifierWithProvider ::
    ConstPtr CryptoProvider -> IO (Ptr ServerCertVerifier)

foreign import capi unsafe "rustls.h rustls_server_cert_verifier_free"
  serverCertVerifierFree :: Ptr ServerCertVerifier -> IO ()

foreign import capi unsafe "rustls.h rustls_client_config_builder_set_server_verifier"
  clientConfigBuilderSetServerVerifier ::
    Ptr ClientConfigBuilder -> ConstPtr ServerCertVerifier -> IO ()

-- Server

data {-# CTYPE "rustls.h" "rustls_server_config" #-} ServerConfig

data {-# CTYPE "rustls.h" "rustls_server_config_builder" #-} ServerConfigBuilder

foreign import capi unsafe "rustls.h rustls_server_config_builder_new_custom"
  serverConfigBuilderNewCustom ::
    ConstPtr CryptoProvider ->
    ConstPtr TLSVersion ->
    CSize ->
    Ptr (Ptr ServerConfigBuilder) ->
    IO Result

foreign import capi unsafe "rustls.h rustls_server_config_builder_free"
  serverConfigBuilderFree :: Ptr ServerConfigBuilder -> IO ()

foreign import capi unsafe "rustls.h rustls_server_config_builder_build"
  serverConfigBuilderBuild ::
    Ptr ServerConfigBuilder -> Ptr (ConstPtr ServerConfig) -> IO Result

foreign import capi unsafe "rustls.h &rustls_server_config_free"
  serverConfigFree :: FinalizerPtr ServerConfig

foreign import capi unsafe "rustls.h rustls_server_connection_new"
  serverConnectionNew :: ConstPtr ServerConfig -> Ptr (Ptr Connection) -> IO Result

foreign import capi unsafe "rustls.h rustls_server_config_builder_set_alpn_protocols"
  serverConfigBuilderSetALPNProtocols ::
    Ptr ServerConfigBuilder -> ConstPtr SliceBytes -> CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_server_config_builder_set_ignore_client_order"
  serverConfigBuilderSetIgnoreClientOrder :: Ptr ServerConfigBuilder -> CBool -> IO Result

foreign import capi unsafe "rustls.h rustls_server_config_builder_set_certified_keys"
  serverConfigBuilderSetCertifiedKeys ::
    Ptr ServerConfigBuilder -> ConstPtr (ConstPtr CertifiedKey) -> CSize -> IO Result

data
  {-# CTYPE "rustls.h" "rustls_web_pki_client_cert_verifier_builder" #-}
  WebPkiClientCertVerifierBuilder

data
  {-# CTYPE "rustls.h" "rustls_client_cert_verifier" #-}
  ClientCertVerifier

-- TODO all features?

foreign import capi unsafe "rustls.h rustls_web_pki_client_cert_verifier_builder_new_with_provider"
  webPkiClientCertVerifierBuilderNewWithProvider ::
    ConstPtr CryptoProvider ->
    ConstPtr RootCertStore ->
    IO (Ptr WebPkiClientCertVerifierBuilder)

foreign import capi unsafe "rustls.h rustls_web_pki_client_cert_verifier_builder_add_crl"
  webPkiClientCertVerifierBuilderAddCrl ::
    Ptr WebPkiClientCertVerifierBuilder -> ConstPtr Word8 -> CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_web_pki_client_cert_verifier_builder_allow_unauthenticated"
  webPkiClientCertVerifierBuilderAllowUnauthenticated ::
    Ptr WebPkiClientCertVerifierBuilder -> IO Result

foreign import capi unsafe "rustls.h rustls_web_pki_client_cert_verifier_builder_free"
  webPkiClientCertVerifierBuilderFree :: Ptr WebPkiClientCertVerifierBuilder -> IO ()

foreign import capi unsafe "rustls.h rustls_web_pki_client_cert_verifier_builder_build"
  webPkiClientCertVerifierBuilderBuild ::
    Ptr WebPkiClientCertVerifierBuilder -> Ptr (Ptr ClientCertVerifier) -> IO Result

foreign import capi unsafe "rustls.h rustls_client_cert_verifier_free"
  clientCertVerifierFree :: Ptr ClientCertVerifier -> IO ()

foreign import capi unsafe "rustls.h rustls_server_config_builder_set_client_verifier"
  serverConfigBuilderSetClientVerifier ::
    Ptr ServerConfigBuilder -> ConstPtr ClientCertVerifier -> IO ()

-- add custom session persistence functions?

-- connection

data {-# CTYPE "rustls.h" "rustls_connection" #-} Connection

foreign import capi unsafe "rustls.h rustls_connection_free"
  connectionFree :: Ptr Connection -> IO ()

type LogCallback = Ptr Userdata -> ConstPtr LogParams -> IO ()

foreign import ccall "wrapper"
  mkLogCallback :: LogCallback -> IO (FunPtr LogCallback)

newtype LogLevel = LogLevel CSize
  deriving stock (Int -> LogLevel -> ShowS
[LogLevel] -> ShowS
LogLevel -> String
(Int -> LogLevel -> ShowS)
-> (LogLevel -> String) -> ([LogLevel] -> ShowS) -> Show LogLevel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LogLevel -> ShowS
showsPrec :: Int -> LogLevel -> ShowS
$cshow :: LogLevel -> String
show :: LogLevel -> String
$cshowList :: [LogLevel] -> ShowS
showList :: [LogLevel] -> ShowS
Show, LogLevel -> LogLevel -> Bool
(LogLevel -> LogLevel -> Bool)
-> (LogLevel -> LogLevel -> Bool) -> Eq LogLevel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LogLevel -> LogLevel -> Bool
== :: LogLevel -> LogLevel -> Bool
$c/= :: LogLevel -> LogLevel -> Bool
/= :: LogLevel -> LogLevel -> Bool
Eq)
  deriving newtype (Ptr LogLevel -> IO LogLevel
Ptr LogLevel -> Int -> IO LogLevel
Ptr LogLevel -> Int -> LogLevel -> IO ()
Ptr LogLevel -> LogLevel -> IO ()
LogLevel -> Int
(LogLevel -> Int)
-> (LogLevel -> Int)
-> (Ptr LogLevel -> Int -> IO LogLevel)
-> (Ptr LogLevel -> Int -> LogLevel -> IO ())
-> (forall b. Ptr b -> Int -> IO LogLevel)
-> (forall b. Ptr b -> Int -> LogLevel -> IO ())
-> (Ptr LogLevel -> IO LogLevel)
-> (Ptr LogLevel -> LogLevel -> IO ())
-> Storable LogLevel
forall b. Ptr b -> Int -> IO LogLevel
forall b. Ptr b -> Int -> LogLevel -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
$csizeOf :: LogLevel -> Int
sizeOf :: LogLevel -> Int
$calignment :: LogLevel -> Int
alignment :: LogLevel -> Int
$cpeekElemOff :: Ptr LogLevel -> Int -> IO LogLevel
peekElemOff :: Ptr LogLevel -> Int -> IO LogLevel
$cpokeElemOff :: Ptr LogLevel -> Int -> LogLevel -> IO ()
pokeElemOff :: Ptr LogLevel -> Int -> LogLevel -> IO ()
$cpeekByteOff :: forall b. Ptr b -> Int -> IO LogLevel
peekByteOff :: forall b. Ptr b -> Int -> IO LogLevel
$cpokeByteOff :: forall b. Ptr b -> Int -> LogLevel -> IO ()
pokeByteOff :: forall b. Ptr b -> Int -> LogLevel -> IO ()
$cpeek :: Ptr LogLevel -> IO LogLevel
peek :: Ptr LogLevel -> IO LogLevel
$cpoke :: Ptr LogLevel -> LogLevel -> IO ()
poke :: Ptr LogLevel -> LogLevel -> IO ()
Storable)

data LogParams = LogParams
  { LogParams -> LogLevel
rustlsLogParamsLevel :: LogLevel,
    LogParams -> Str
rustlsLogParamsMessage :: Str
  }
  deriving stock ((forall x. LogParams -> Rep LogParams x)
-> (forall x. Rep LogParams x -> LogParams) -> Generic LogParams
forall x. Rep LogParams x -> LogParams
forall x. LogParams -> Rep LogParams x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. LogParams -> Rep LogParams x
from :: forall x. LogParams -> Rep LogParams x
$cto :: forall x. Rep LogParams x -> LogParams
to :: forall x. Rep LogParams x -> LogParams
Generic)
  deriving anyclass (LogParams -> Int
(LogParams -> Int)
-> (LogParams -> Int)
-> (forall b. Ptr b -> Int -> IO LogParams)
-> (forall b. Ptr b -> Int -> LogParams -> IO ())
-> GStorable LogParams
forall b. Ptr b -> Int -> IO LogParams
forall b. Ptr b -> Int -> LogParams -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> GStorable a
$cgsizeOf :: LogParams -> Int
gsizeOf :: LogParams -> Int
$cgalignment :: LogParams -> Int
galignment :: LogParams -> Int
$cgpeekByteOff :: forall b. Ptr b -> Int -> IO LogParams
gpeekByteOff :: forall b. Ptr b -> Int -> IO LogParams
$cgpokeByteOff :: forall b. Ptr b -> Int -> LogParams -> IO ()
gpokeByteOff :: forall b. Ptr b -> Int -> LogParams -> IO ()
GStorable)

foreign import capi unsafe "rustls.h rustls_connection_set_log_callback"
  connectionSetLogCallback :: Ptr Connection -> FunPtr LogCallback -> IO ()

foreign import capi unsafe "rustls.h rustls_connection_is_handshaking"
  connectionIsHandshaking :: ConstPtr Connection -> IO CBool

foreign import capi unsafe "rustls.h rustls_connection_get_alpn_protocol"
  connectionGetALPNProtocol :: ConstPtr Connection -> Ptr (ConstPtr Word8) -> Ptr CSize -> IO ()

foreign import capi unsafe "rustls.h rustls_connection_get_protocol_version"
  connectionGetProtocolVersion :: ConstPtr Connection -> IO TLSVersion

foreign import capi unsafe "rustls.h rustls_connection_get_negotiated_ciphersuite"
  connectionGetNegotiatedCipherSuite :: ConstPtr Connection -> IO Word16

foreign import capi unsafe "rustls.h hs_rustls_connection_get_negotiated_ciphersuite_name"
  connectionGetNegotiatedCipherSuiteName :: ConstPtr Connection -> Ptr Str -> IO ()

foreign import capi unsafe "rustls.h rustls_server_connection_get_server_name"
  serverConnectionGetSNIHostname :: ConstPtr Connection -> Ptr Word8 -> CSize -> Ptr CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_connection_get_peer_certificate"
  connectionGetPeerCertificate :: ConstPtr Connection -> CSize -> IO (ConstPtr Certificate)

-- connection read

type ReadCallback = Ptr Userdata -> Ptr Word8 -> CSize -> Ptr CSize -> IO IOResult

foreign import ccall "wrapper"
  mkReadCallback :: ReadCallback -> IO (FunPtr ReadCallback)

foreign import capi "rustls.h rustls_connection_read_tls"
  connectionReadTls ::
    Ptr Connection -> FunPtr ReadCallback -> Ptr Userdata -> Ptr CSize -> IO IOResult

foreign import capi "rustls.h rustls_connection_read"
  connectionRead :: Ptr Connection -> Ptr Word8 -> CSize -> Ptr CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_connection_wants_read"
  connectionWantsRead :: ConstPtr Connection -> IO CBool

-- connection write

type WriteCallback = Ptr Userdata -> ConstPtr Word8 -> CSize -> Ptr CSize -> IO IOResult

foreign import ccall "wrapper"
  mkWriteCallback :: WriteCallback -> IO (FunPtr WriteCallback)

foreign import capi "rustls.h rustls_connection_write_tls"
  connectionWriteTls ::
    Ptr Connection -> FunPtr WriteCallback -> Ptr Userdata -> Ptr CSize -> IO IOResult

foreign import capi "rustls.h rustls_connection_write"
  connectionWrite :: Ptr Connection -> Ptr Word8 -> CSize -> Ptr CSize -> IO Result

foreign import capi unsafe "rustls.h rustls_connection_wants_write"
  connectionWantsWrite :: ConstPtr Connection -> IO CBool

-- misc

foreign import capi "rustls.h rustls_connection_process_new_packets"
  connectionProcessNewPackets :: Ptr Connection -> IO Result

foreign import capi "rustls.h rustls_connection_send_close_notify"
  connectionSendCloseNotify :: Ptr Connection -> IO ()

-- TODO high level bindings?
foreign import capi unsafe "rustls.h rustls_connection_set_buffer_limit"
  connectionSetBufferLimit :: Ptr Connection -> CSize -> IO ()

data {-# CTYPE "rustls.h" "rustls_certified_key" #-} CertifiedKey

foreign import capi unsafe "rustls.h rustls_certified_key_build"
  certifiedKeyBuild ::
    ConstPtr Word8 -> CSize -> ConstPtr Word8 -> CSize -> Ptr (ConstPtr CertifiedKey) -> IO Result

foreign import capi unsafe "rustls.h rustls_certified_key_free"
  certifiedKeyFree :: ConstPtr CertifiedKey -> IO ()

data {-# CTYPE "rustls.h" "rustls_certificate" #-} Certificate

foreign import capi unsafe "rustls.h rustls_certificate_get_der"
  certificateGetDER :: ConstPtr Certificate -> Ptr (ConstPtr Word8) -> Ptr CSize -> IO Result

-- TLS params

data {-# CTYPE "rustls.h" "rustls_supported_ciphersuite" #-} SupportedCipherSuite

foreign import capi unsafe "rustls.h rustls_supported_ciphersuite_get_suite"
  supportedCipherSuiteGetSuite :: ConstPtr SupportedCipherSuite -> Word16

foreign import capi unsafe "hs_rustls.h hs_rustls_supported_ciphersuite_get_name"
  hsSupportedCipherSuiteGetName :: ConstPtr SupportedCipherSuite -> Ptr Str -> IO ()

foreign import capi unsafe "hs_rustls.h hs_rustls_supported_ciphersuite_protocol_version"
  hsSupportedCiphersuiteProtocolVersion :: ConstPtr SupportedCipherSuite -> IO TLSVersion

-- | A TLS protocol version supported by Rustls.
newtype {-# CTYPE "stdint.h" "uint16_t" #-} TLSVersion = TLSVersion
  { TLSVersion -> Word16
unTLSVersion :: Word16
  }
  deriving stock (Int -> TLSVersion -> ShowS
[TLSVersion] -> ShowS
TLSVersion -> String
(Int -> TLSVersion -> ShowS)
-> (TLSVersion -> String)
-> ([TLSVersion] -> ShowS)
-> Show TLSVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TLSVersion -> ShowS
showsPrec :: Int -> TLSVersion -> ShowS
$cshow :: TLSVersion -> String
show :: TLSVersion -> String
$cshowList :: [TLSVersion] -> ShowS
showList :: [TLSVersion] -> ShowS
Show, TLSVersion -> TLSVersion -> Bool
(TLSVersion -> TLSVersion -> Bool)
-> (TLSVersion -> TLSVersion -> Bool) -> Eq TLSVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TLSVersion -> TLSVersion -> Bool
== :: TLSVersion -> TLSVersion -> Bool
$c/= :: TLSVersion -> TLSVersion -> Bool
/= :: TLSVersion -> TLSVersion -> Bool
Eq, Eq TLSVersion
Eq TLSVersion =>
(TLSVersion -> TLSVersion -> Ordering)
-> (TLSVersion -> TLSVersion -> Bool)
-> (TLSVersion -> TLSVersion -> Bool)
-> (TLSVersion -> TLSVersion -> Bool)
-> (TLSVersion -> TLSVersion -> Bool)
-> (TLSVersion -> TLSVersion -> TLSVersion)
-> (TLSVersion -> TLSVersion -> TLSVersion)
-> Ord TLSVersion
TLSVersion -> TLSVersion -> Bool
TLSVersion -> TLSVersion -> Ordering
TLSVersion -> TLSVersion -> TLSVersion
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TLSVersion -> TLSVersion -> Ordering
compare :: TLSVersion -> TLSVersion -> Ordering
$c< :: TLSVersion -> TLSVersion -> Bool
< :: TLSVersion -> TLSVersion -> Bool
$c<= :: TLSVersion -> TLSVersion -> Bool
<= :: TLSVersion -> TLSVersion -> Bool
$c> :: TLSVersion -> TLSVersion -> Bool
> :: TLSVersion -> TLSVersion -> Bool
$c>= :: TLSVersion -> TLSVersion -> Bool
>= :: TLSVersion -> TLSVersion -> Bool
$cmax :: TLSVersion -> TLSVersion -> TLSVersion
max :: TLSVersion -> TLSVersion -> TLSVersion
$cmin :: TLSVersion -> TLSVersion -> TLSVersion
min :: TLSVersion -> TLSVersion -> TLSVersion
Ord)
  deriving newtype (Ptr TLSVersion -> IO TLSVersion
Ptr TLSVersion -> Int -> IO TLSVersion
Ptr TLSVersion -> Int -> TLSVersion -> IO ()
Ptr TLSVersion -> TLSVersion -> IO ()
TLSVersion -> Int
(TLSVersion -> Int)
-> (TLSVersion -> Int)
-> (Ptr TLSVersion -> Int -> IO TLSVersion)
-> (Ptr TLSVersion -> Int -> TLSVersion -> IO ())
-> (forall b. Ptr b -> Int -> IO TLSVersion)
-> (forall b. Ptr b -> Int -> TLSVersion -> IO ())
-> (Ptr TLSVersion -> IO TLSVersion)
-> (Ptr TLSVersion -> TLSVersion -> IO ())
-> Storable TLSVersion
forall b. Ptr b -> Int -> IO TLSVersion
forall b. Ptr b -> Int -> TLSVersion -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
$csizeOf :: TLSVersion -> Int
sizeOf :: TLSVersion -> Int
$calignment :: TLSVersion -> Int
alignment :: TLSVersion -> Int
$cpeekElemOff :: Ptr TLSVersion -> Int -> IO TLSVersion
peekElemOff :: Ptr TLSVersion -> Int -> IO TLSVersion
$cpokeElemOff :: Ptr TLSVersion -> Int -> TLSVersion -> IO ()
pokeElemOff :: Ptr TLSVersion -> Int -> TLSVersion -> IO ()
$cpeekByteOff :: forall b. Ptr b -> Int -> IO TLSVersion
peekByteOff :: forall b. Ptr b -> Int -> IO TLSVersion
$cpokeByteOff :: forall b. Ptr b -> Int -> TLSVersion -> IO ()
pokeByteOff :: forall b. Ptr b -> Int -> TLSVersion -> IO ()
$cpeek :: Ptr TLSVersion -> IO TLSVersion
peek :: Ptr TLSVersion -> IO TLSVersion
$cpoke :: Ptr TLSVersion -> TLSVersion -> IO ()
poke :: Ptr TLSVersion -> TLSVersion -> IO ()
Storable)

pattern TLS12, TLS13 :: TLSVersion
pattern $mTLS12 :: forall {r}. TLSVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bTLS12 :: TLSVersion
TLS12 = TLSVersion 0x0303
pattern $mTLS13 :: forall {r}. TLSVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bTLS13 :: TLSVersion
TLS13 = TLSVersion 0x0304

-- Crypto provider

data {-# CTYPE "rustls.h" "rustls_crypto_provider" #-} CryptoProvider

data {-# CTYPE "rustls.h" "rustls_crypto_provider_builder" #-} CryptoProviderBuilder

foreign import capi unsafe "rustls.h rustls_crypto_provider_builder_new_from_default"
  cryptoProviderBuilderNewFromDefault :: Ptr (Ptr CryptoProviderBuilder) -> IO Result

foreign import capi unsafe "rustls.h rustls_crypto_provider_builder_new_with_base"
  cryptoProviderBuilderNewWithBase :: ConstPtr CryptoProvider -> IO (Ptr CryptoProviderBuilder)

foreign import capi unsafe "rustls.h rustls_crypto_provider_builder_set_cipher_suites"
  cryptoProviderBuilderSetCipherSuites ::
    Ptr CryptoProviderBuilder ->
    ConstPtr (ConstPtr SupportedCipherSuite) ->
    CSize ->
    IO Result

foreign import capi unsafe "rustls.h rustls_crypto_provider_builder_build"
  cryptoProviderBuilderBuild ::
    Ptr CryptoProviderBuilder ->
    Ptr (ConstPtr CryptoProvider) ->
    IO Result

foreign import capi unsafe "rustls.h rustls_crypto_provider_builder_free"
  cryptoProviderBuilderFree :: Ptr CryptoProviderBuilder -> IO ()

foreign import capi unsafe "rustls.h &rustls_crypto_provider_free"
  cryptoProviderFree :: FinalizerPtr CryptoProvider

foreign import capi unsafe "rustls.h rustls_crypto_provider_ciphersuites_len"
  cryptoProviderCiphersuitesLen :: ConstPtr CryptoProvider -> CSize

foreign import capi unsafe "rustls.h rustls_crypto_provider_ciphersuites_get"
  cryptoProviderCiphersuitesGet ::
    ConstPtr CryptoProvider -> CSize -> ConstPtr SupportedCipherSuite

-- Root cert store

data {-# CTYPE "rustls.h" "rustls_root_cert_store_builder" #-} RootCertStoreBuilder

data {-# CTYPE "rustls.h" "rustls_root_cert_store" #-} RootCertStore

foreign import capi unsafe "rustls.h rustls_root_cert_store_builder_new"
  rootCertStoreBuilderNew :: IO (Ptr RootCertStoreBuilder)

foreign import capi unsafe "rustls.h rustls_root_cert_store_builder_add_pem"
  rootCertStoreBuilderAddPem ::
    Ptr RootCertStoreBuilder -> ConstPtr Word8 -> CSize -> CBool -> IO Result

foreign import capi unsafe "rustls.h rustls_root_cert_store_builder_load_roots_from_file"
  rootCertStoreBuilderLoadRootsFromFile ::
    Ptr RootCertStoreBuilder -> ConstCString -> CBool -> IO Result

foreign import capi unsafe "rustls.h rustls_root_cert_store_builder_free"
  rootCertStoreBuilderFree :: Ptr RootCertStoreBuilder -> IO ()

foreign import capi unsafe "rustls.h rustls_root_cert_store_builder_build"
  rootCertStoreBuilderBuild ::
    Ptr RootCertStoreBuilder -> Ptr (ConstPtr RootCertStore) -> IO Result

foreign import capi unsafe "rustls.h rustls_root_cert_store_free"
  rootCertStoreFree :: ConstPtr RootCertStore -> IO ()