Skip to content

Commit

Permalink
Add new field ctxClientCerts to Context
Browse files Browse the repository at this point in the history
It's stores certificate chain in an IORef. This can be filled in
the onClientCertificate hook.
  • Loading branch information
eyeinsky committed Aug 20, 2019
1 parent 148097e commit e9e3fcb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/Network/TLS/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ contextNew backend params = liftIO $ do
lockWrite <- newMVar ()
lockRead <- newMVar ()
lockState <- newMVar ()
certChain <- newIORef Nothing

return Context
{ ctxConnection = getBackend backend
Expand All @@ -182,6 +183,7 @@ contextNew backend params = liftIO $ do
, ctxPendingActions = as
, ctxCertRequests = crs
, ctxKeyLogger = debugKeyLogger debug
, ctxClientCerts = certChain
}

-- | create a new context on an handle.
Expand Down
2 changes: 2 additions & 0 deletions core/Network/TLS/Context/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import Control.Exception (throwIO, Exception())
import Data.IORef
import Data.Tuple

import Data.X509

-- | Information related to a running context, e.g. current cipher
data Information = Information
Expand Down Expand Up @@ -128,6 +129,7 @@ data Context = Context
, ctxPendingActions :: IORef [PendingAction]
, ctxCertRequests :: IORef [Handshake13] -- ^ pending PHA requests
, ctxKeyLogger :: String -> IO ()
, ctxClientCerts :: IORef (Maybe CertificateChain)
}

data Established = NotEstablished
Expand Down
3 changes: 2 additions & 1 deletion core/Network/TLS/Handshake/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,8 @@ clientCertificate sparams ctx certs = do
-- Call application callback to see whether the
-- certificate chain is acceptable.
--
usage <- liftIO $ catchException (onClientCertificate (serverHooks sparams) certs) rejectOnException
let ioref = ctxClientCerts ctx
usage <- liftIO $ catchException (onClientCertificate (serverHooks sparams) certs ioref) rejectOnException
case usage of
CertificateUsageAccept -> verifyLeafKeyUsage [KeyUsage_digitalSignature] certs
CertificateUsageReject reason -> certificateRejected reason
Expand Down
6 changes: 4 additions & 2 deletions core/Network/TLS/Parameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module Network.TLS.Parameters
, CertificateRejectReason(..)
) where

import Data.IORef

import Network.TLS.Extension
import Network.TLS.Struct
import qualified Network.TLS.Struct as Struct
Expand Down Expand Up @@ -442,7 +444,7 @@ data ServerHooks = ServerHooks
-- The function is not expected to verify the key-usage
-- extension of the certificate. This verification is
-- performed by the library internally.
onClientCertificate :: CertificateChain -> IO CertificateUsage
onClientCertificate :: CertificateChain -> IORef (Maybe CertificateChain) -> IO CertificateUsage

-- | This action is called when the client certificate
-- cannot be verified. Return 'True' to accept the certificate
Expand Down Expand Up @@ -482,7 +484,7 @@ data ServerHooks = ServerHooks
defaultServerHooks :: ServerHooks
defaultServerHooks = ServerHooks
{ onCipherChoosing = \_ -> head
, onClientCertificate = \_ -> return $ CertificateUsageReject $ CertificateRejectOther "no client certificates expected"
, onClientCertificate = \_ _ -> return $ CertificateUsageReject $ CertificateRejectOther "no client certificates expected"
, onUnverifiedClientCert = return False
, onServerNameIndication = \_ -> return mempty
, onNewHandshake = \_ -> return True
Expand Down

0 comments on commit e9e3fcb

Please sign in to comment.