module Network.TLS.Parameters
(
ClientParams(..)
, ServerParams(..)
, CommonParams
, DebugParams(..)
, ClientHooks(..)
, ServerHooks(..)
, Supported(..)
, Shared(..)
, defaultParamsClient
, MaxFragmentEnum(..)
, CertificateUsage(..)
, CertificateRejectReason(..)
) where
import Network.TLS.Extension
import Network.TLS.Struct
import qualified Network.TLS.Struct as Struct
import Network.TLS.Session
import Network.TLS.Cipher
import Network.TLS.Measurement
import Network.TLS.Compression
import Network.TLS.Crypto
import Network.TLS.Credentials
import Network.TLS.X509
import Network.TLS.RNG (Seed)
import Data.Default.Class
import qualified Data.ByteString as B
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid (mempty)
#endif
type HostName = String
type CommonParams = (Supported, Shared, DebugParams)
data DebugParams = DebugParams
{
debugSeed :: Maybe Seed
, debugPrintSeed :: Seed -> IO ()
}
defaultDebugParams :: DebugParams
defaultDebugParams = DebugParams
{ debugSeed = Nothing
, debugPrintSeed = const (return ())
}
instance Show DebugParams where
show _ = "DebugParams"
instance Default DebugParams where
def = defaultDebugParams
data ClientParams = ClientParams
{ clientUseMaxFragmentLength :: Maybe MaxFragmentEnum
, clientServerIdentification :: (HostName, Bytes)
, clientUseServerNameIndication :: Bool
, clientWantSessionResume :: Maybe (SessionID, SessionData)
, clientShared :: Shared
, clientHooks :: ClientHooks
, clientSupported :: Supported
, clientDebug :: DebugParams
} deriving (Show)
defaultParamsClient :: HostName -> Bytes -> ClientParams
defaultParamsClient serverName serverId = ClientParams
{ clientWantSessionResume = Nothing
, clientUseMaxFragmentLength = Nothing
, clientServerIdentification = (serverName, serverId)
, clientUseServerNameIndication = True
, clientShared = def
, clientHooks = def
, clientSupported = def
, clientDebug = defaultDebugParams
}
data ServerParams = ServerParams
{
serverWantClientCert :: Bool
, serverCACertificates :: [SignedCertificate]
, serverDHEParams :: Maybe DHParams
, serverShared :: Shared
, serverHooks :: ServerHooks
, serverSupported :: Supported
, serverDebug :: DebugParams
} deriving (Show)
defaultParamsServer :: ServerParams
defaultParamsServer = ServerParams
{ serverWantClientCert = False
, serverCACertificates = []
, serverDHEParams = Nothing
, serverHooks = def
, serverShared = def
, serverSupported = def
, serverDebug = defaultDebugParams
}
instance Default ServerParams where
def = defaultParamsServer
data Supported = Supported
{
supportedVersions :: [Version]
, supportedCiphers :: [Cipher]
, supportedCompressions :: [Compression]
, supportedHashSignatures :: [HashAndSignatureAlgorithm]
, supportedSecureRenegotiation :: Bool
, supportedClientInitiatedRenegotiation :: Bool
, supportedSession :: Bool
, supportedFallbackScsv :: Bool
, supportedEmptyPacket :: Bool
} deriving (Show,Eq)
defaultSupported :: Supported
defaultSupported = Supported
{ supportedVersions = [TLS12,TLS11,TLS10]
, supportedCiphers = []
, supportedCompressions = [nullCompression]
, supportedHashSignatures = [ (Struct.HashSHA512, SignatureRSA)
, (Struct.HashSHA384, SignatureRSA)
, (Struct.HashSHA256, SignatureRSA)
, (Struct.HashSHA224, SignatureRSA)
, (Struct.HashSHA1, SignatureRSA)
, (Struct.HashSHA1, SignatureDSS)
]
, supportedSecureRenegotiation = True
, supportedClientInitiatedRenegotiation = False
, supportedSession = True
, supportedFallbackScsv = True
, supportedEmptyPacket = True
}
instance Default Supported where
def = defaultSupported
data Shared = Shared
{ sharedCredentials :: Credentials
, sharedSessionManager :: SessionManager
, sharedCAStore :: CertificateStore
, sharedValidationCache :: ValidationCache
}
instance Show Shared where
show _ = "Shared"
instance Default Shared where
def = Shared
{ sharedCAStore = mempty
, sharedCredentials = mempty
, sharedSessionManager = noSessionManager
, sharedValidationCache = def
}
data ClientHooks = ClientHooks
{
onCertificateRequest :: ([CertificateType],
Maybe [HashAndSignatureAlgorithm],
[DistinguishedName]) -> IO (Maybe (CertificateChain, PrivKey))
, onNPNServerSuggest :: Maybe ([B.ByteString] -> IO B.ByteString)
, onServerCertificate :: CertificateStore -> ValidationCache -> ServiceID -> CertificateChain -> IO [FailedReason]
, onSuggestALPN :: IO (Maybe [B.ByteString])
}
defaultClientHooks :: ClientHooks
defaultClientHooks = ClientHooks
{ onCertificateRequest = \ _ -> return Nothing
, onNPNServerSuggest = Nothing
, onServerCertificate = validateDefault
, onSuggestALPN = return Nothing
}
instance Show ClientHooks where
show _ = "ClientHooks"
instance Default ClientHooks where
def = defaultClientHooks
data ServerHooks = ServerHooks
{
onClientCertificate :: CertificateChain -> IO CertificateUsage
, onUnverifiedClientCert :: IO Bool
, onCipherChoosing :: Version -> [Cipher] -> Cipher
, onServerNameIndication :: Maybe HostName -> IO Credentials
, onSuggestNextProtocols :: IO (Maybe [B.ByteString])
, onNewHandshake :: Measurement -> IO Bool
, onALPNClientSuggest :: Maybe ([B.ByteString] -> IO B.ByteString)
}
defaultServerHooks :: ServerHooks
defaultServerHooks = ServerHooks
{ onCipherChoosing = \_ -> head
, onClientCertificate = \_ -> return $ CertificateUsageReject $ CertificateRejectOther "no client certificates expected"
, onUnverifiedClientCert = return False
, onServerNameIndication = \_ -> return mempty
, onSuggestNextProtocols = return Nothing
, onNewHandshake = \_ -> return True
, onALPNClientSuggest = Nothing
}
instance Show ServerHooks where
show _ = "ServerHooks"
instance Default ServerHooks where
def = defaultServerHooks