packages feed

tls-session-manager 0.1.2 → 0.1.3

raw patch · 4 files changed

+63/−12 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

ChangeLog.md view
@@ -1,5 +1,10 @@ # ChangeLog for tls-session-manager +## 0.1.3++* Refuse a replayed 0-RTT ticket in the session ticket manager.+  [#634](https://github.com/haskell-tls/hs-tls/pull/634)+ ## 0.1.2  * Require tls >= 2.4.13 and build the tests with `defaultSessionData`.
Network/TLS/SessionTicket.hs view
@@ -2,11 +2,8 @@  -- | A manager for TLS 1.2/1.3 session ticket. -----   Tracking client hello is not implemented yet.---   So, if this is used for TLS 1.3 0-RTT,---   replay attack is possible.---   If your application data in 0-RTT changes the status of server side,---   use 'Network.TLS.SessionManager' instead.+--   For TLS 1.3 0-RTT, a ticket is accepted at most once while a replay+--   of its ClientHello could still be fresh. -- --   A dedicated thread is running repeatedly to replece --   secret keys. So, energy saving is not achieved.@@ -22,8 +19,12 @@ import Codec.Serialise import qualified Crypto.Token as CT import qualified Data.ByteString.Lazy as L+import Data.IORef+import Data.Int (Int64)+import qualified Data.OrdPSQ as Q import Network.TLS import Network.TLS.Internal+import System.Clock  -- | Configuration for session tickets. data Config = Config@@ -47,7 +48,7 @@ -- | Creating a session ticket manager. newSessionTicketManager :: Config -> IO SessionManager newSessionTicketManager Config{..} =-    sessionTicketManager <$> CT.spawnTokenManager conf+    sessionTicketManager <$> CT.spawnTokenManager conf <*> newIORef Q.empty   where     conf =         CT.defaultConfig@@ -59,7 +60,8 @@ newSessionTicketManager' :: Config -> IO (SessionManager, IO ()) newSessionTicketManager' Config{..} = do     tokenmgr <- CT.spawnTokenManager conf-    return (sessionTicketManager tokenmgr, CT.killTokenManager tokenmgr)+    used <- newIORef Q.empty+    return (sessionTicketManager tokenmgr used, CT.killTokenManager tokenmgr)   where     conf =         CT.defaultConfig@@ -68,11 +70,15 @@             , CT.threadName = "TLS ticket manager"             } -sessionTicketManager :: CT.TokenManager -> SessionManager-sessionTicketManager ctmgr =+-- | Tickets used for 0-RTT, with when they may be forgotten in+-- milliseconds of the wall clock.+type Used = IORef (Q.OrdPSQ Ticket Int64 ())++sessionTicketManager :: CT.TokenManager -> Used -> SessionManager+sessionTicketManager ctmgr used =     noSessionManager         { sessionResume = resume ctmgr-        , sessionResumeOnlyOnce = resume ctmgr+        , sessionResumeOnlyOnce = resumeOnlyOnce ctmgr used         , sessionEstablish = establish ctmgr         , sessionInvalidate = \_ -> return ()         , sessionUseTicket = True@@ -93,3 +99,30 @@                 Left _ -> return Nothing                 Right sd -> return $ Just sd     | otherwise = return Nothing++-- A replay has the same age, so the same expected arrival time E, which is+-- within the tolerance of the first use.  The server accepts it only within+-- the tolerance of E, so the ticket is remembered for twice the tolerance.+resumeOnlyOnce :: CT.TokenManager -> Used -> Ticket -> IO (Maybe SessionData)+resumeOnlyOnce ctmgr used ticket = do+    msd <- resume ctmgr ticket+    case msd of+        Nothing -> return Nothing+        Just sd -> do+            now <- milliseconds+            let rtt = maybe 0 fromIntegral $ sessionTicketInfo sd >>= estimatedRTT+                -- 'max 2000 rtt' as in the server, and a second of margin+                forget = now + 2 * max 2000 rtt + 1000+            first <- atomicModifyIORef' used $ \q ->+                let q' = snd $ Q.atMostView now q+                 in case Q.lookup ticket q' of+                        Just _ -> (q', False)+                        Nothing -> (Q.insert ticket forget () q', True)+            return $ if first then Just sd else Nothing++-- The wall clock, as the server checks freshness with it.  If it goes+-- back, the ticket is remembered longer, just as a replay stays fresh.+milliseconds :: IO Int64+milliseconds = do+    TimeSpec s ns <- getTime Realtime+    return $ s * 1000 + ns `div` 1000000
test/SessionManagerSpec.hs view
@@ -17,6 +17,8 @@     cons,     toValue,  )+import Network.TLS.SessionTicket (newSessionTicketManager')+import qualified Network.TLS.SessionTicket as SessionTicket import Test.Hspec  spec :: Spec@@ -52,6 +54,17 @@             _ <- sessionEstablish mgr "a" $ sessionData Nothing             sessionResumeOnlyOnce mgr "a" >>= (`shouldSatisfy` isJust)             sessionResumeOnlyOnce mgr "a" >>= (`shouldSatisfy` isNothing)+    describe "newSessionTicketManager" $ do+        it "resumes a ticket only once for 0-RTT while a replay is fresh" $ do+            (mgr, kill) <- newSessionTicketManager' SessionTicket.defaultConfig+            Just ticket <- sessionEstablish mgr "" $ sessionData (Just 3600)+            sessionResumeOnlyOnce mgr ticket >>= (`shouldSatisfy` isJust)+            sessionResumeOnlyOnce mgr ticket >>= (`shouldSatisfy` isNothing)+            sessionResume mgr ticket >>= (`shouldSatisfy` isJust)+            -- twice the tolerance of 2 seconds and a second of margin+            threadDelay 5100000+            sessionResumeOnlyOnce mgr ticket >>= (`shouldSatisfy` isJust)+            kill     -- While the reaper prunes, the sessions are taken away and an empty     -- workload is left in their place; the pruned ones are merged back.     describe "pruning" $ do
tls-session-manager.cabal view
@@ -1,6 +1,6 @@ cabal-version:      2.0 name:               tls-session-manager-version:            0.1.2+version:            0.1.3 license:            BSD3 license-file:       LICENSE maintainer:         kazu@iij.ad.jp@@ -29,7 +29,7 @@         psqueues >= 0.2 && < 0.3,         ram,         serialise >= 0.2 && < 0.3,-        tls >= 2.4.13 && < 2.6+        tls >= 2.4.13 && < 2.7      if impl(ghc >=8)         default-extensions: Strict StrictData