Skip to content

Commit

Permalink
removing duplicated code on group.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Apr 11, 2017
1 parent 30e59eb commit ff7a254
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 7 additions & 7 deletions core/Network/TLS/Handshake/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ handshakeServerWith sparams ctx clientHello@(ClientHello clientVersion _ clientS
-- (i.e. elliptic curves and D-H groups)
let serverGroups = supportedGroups (ctxSupported ctx) `intersect` availableGroups
possibleGroups = serverGroups `intersect` clientGroups
hasCommonGroupForECDHE = not (null possibleGroups)
hasCommonGroup cipher =
hasCommonGroupForECDHE <- case possibleGroups of
[] -> return False
g:_ -> do
usingState_ ctx $ setGroup g
return True
let hasCommonGroup cipher =
case cipherKeyExchange cipher of
CipherKeyExchange_ECDHE_RSA -> hasCommonGroupForECDHE
CipherKeyExchange_ECDHE_ECDSA -> hasCommonGroupForECDHE
Expand Down Expand Up @@ -404,11 +408,7 @@ doHandshake sparams mcred ctx chosenVersion usedCipher usedCompression clientSes
return serverParams

generateSKX_ECDHE sigAlg = do
clientGroups <- fromJust "ClientGroupSuggest" <$> usingState_ ctx getClientGroupSuggest
let serverGroups = supportedGroups (ctxSupported ctx) `intersect` availableGroups
grp <- case serverGroups `intersect` clientGroups of
[] -> throwCore $ Error_Protocol ("no common group", True, HandshakeFailure)
g:_ -> return g
Just grp <- usingState_ ctx getGroup -- cannot be Nothing
serverParams <- setup_ECDHE grp
mhash <- decideHash sigAlg
signed <- digitallySignECDHParams ctx serverParams sigAlg mhash
Expand Down
10 changes: 10 additions & 0 deletions core/Network/TLS/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module Network.TLS.State
, getVerifiedData
, setSession
, getSession
, setGroup
, getGroup
, isSessionResuming
, isClientContext
-- * random
Expand Down Expand Up @@ -90,6 +92,7 @@ data TLSState = TLSState
, stRandomGen :: StateRNG
, stVersion :: Maybe Version
, stClientContext :: Role
, stGroup :: Maybe Group
}

newtype TLSSt a = TLSSt { runTLSSt :: ErrT TLSError (State TLSState) a }
Expand Down Expand Up @@ -125,6 +128,7 @@ newTLSState rng clientContext = TLSState
, stRandomGen = rng
, stVersion = Nothing
, stClientContext = clientContext
, stGroup = Nothing
}

updateVerifiedData :: Role -> Bytes -> TLSSt ()
Expand Down Expand Up @@ -250,6 +254,12 @@ setClientSNI hn = modify (\st -> st { stClientSNI = Just hn })
getClientSNI :: TLSSt (Maybe HostName)
getClientSNI = gets stClientSNI

setGroup :: Group -> TLSSt ()
setGroup grp = modify (\st -> st { stGroup = Just grp })

getGroup :: TLSSt (Maybe Group)
getGroup = gets stGroup

getVerifiedData :: Role -> TLSSt Bytes
getVerifiedData client = gets (if client == ClientRole then stClientVerifiedData else stServerVerifiedData)

Expand Down

0 comments on commit ff7a254

Please sign in to comment.