packages feed

tls 2.6.1 → 2.6.2

raw patch · 10 files changed

+160/−24 lines, 10 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

CHANGELOG.md view
@@ -1,5 +1,12 @@ # Change log for "tls" +## Version 2.6.2++* Make bye robust after a failed handshake or a vanished peer.+  [#657](https://github.com/haskell-tls/hs-tls/pull/657)+* Close the connection when post-handshake auth fails.+  [#659](https://github.com/haskell-tls/hs-tls/pull/659)+ ## Version 2.6.1  * Use up a session for 0-RTT only after checking the ClientHello.
Network/TLS/Backend.hs view
@@ -88,16 +88,20 @@ #if MIN_VERSION_network(3,2,12)     getBackendRecvWith sock = Just $ RecvWith recvAllWith       where-        recvAllWith n giveUp = fmap B.concat . sequence <$> loop n+        recvAllWith n giveUp = loop n []           where-            loop 0 = return []-            loop left = do+            done = return . Right . B.concat . reverse+            loop 0 acc = done acc+            loop left acc = do                 r <- Network.recvWith sock left giveUp                 case r of-                    Left g -> return [Left g]+                    -- Bytes already received are answered, not 'Left'.+                    Left g+                        | null acc -> return $ Left g+                        | otherwise -> done acc                     Right bs-                        | B.null bs -> return []-                        | otherwise -> (Right bs :) <$> loop (left - B.length bs)+                        | B.null bs -> done acc+                        | otherwise -> loop (left - B.length bs) (bs : acc) #endif  instance HasBackend Handle where
Network/TLS/Core.hs view
@@ -39,6 +39,7 @@ import qualified Data.ByteString.Char8 as C8 import qualified Data.ByteString.Lazy as L import Data.IORef+import GHC.IO.Exception (IOErrorType (..), IOException (..))  import Network.TLS.Backend (Backend (..), RecvWith (..)) import Network.TLS.Context@@ -133,7 +134,9 @@  -- | Notify the peer that this side wants to close the connection, by -- sending close_notify.  It should be called before closing the backend.--- This doesn't actually close the backend.+-- This doesn't actually close the backend.  Nothing is sent after a failed+-- handshake or a received alert, and a peer which has gone already is not+-- an error. -- -- Proper usage is as follows: --@@ -200,7 +203,13 @@             let giveUp = readTVar late >>= check                 recv n = recvWith n giveUp >>= either (\() -> E.throwIO GaveUpRecv) return                 ctx' = ctx{ctxBackend = (ctxBackend ctx){backendRecv = recv}}-            void $ tryWithReadLock ctx $ E.try @GaveUpRecv $ recvHS13 ctx' chk+            void $+                tryWithReadLock ctx $+                    recvHS13 ctx' chk `catchException` \e ->+                        -- If the receive failed rather than gave up, the+                        -- connection is gone and close_notify is not sent.+                        when (isNothing (E.fromException @GaveUpRecv e)) $+                            setEOF ctx  -- | Thrown by a receive in 'bye' which has given up, and caught there. data GaveUpRecv = GaveUpRecv deriving (Show)@@ -215,10 +224,17 @@     eof <- ctxEOF ctx     tls13 <- tls13orLater ctx     unless eof $-        withWriteLock ctx $-            if tls13-                then sendPacket13 ctx $ Alert13 [(AlertLevel_Warning, CloseNotify)]-                else sendPacket12 ctx $ Alert [(AlertLevel_Warning, CloseNotify)]+        E.handleJust vanished return $+            withWriteLock ctx $+                if tls13+                    then sendPacket13 ctx $ Alert13 [(AlertLevel_Warning, CloseNotify)]+                    else sendPacket12 ctx $ Alert [(AlertLevel_Warning, CloseNotify)]+  where+    -- The peer may have closed the connection without close_notify:+    -- EPIPE or ECONNRESET is then just the end of it.+    vanished e+        | ioe_type e == ResourceVanished = Just ()+        | otherwise = Nothing  -- | If the ALPN extension has been used, this will -- return the protocol agreed upon.@@ -297,8 +313,9 @@             | otherwise = 2 ^ (24 :: Int)  -- | Get data out of Data packet, and automatically renegotiate if a Handshake--- ClientHello is received.  An empty result means EOF.  Use--- 'closeNotifyReceived' to tell an orderly closure from a truncation.+-- ClientHello is received.  An empty result means EOF, after which it+-- throws 'PostHandshake' 'Error_EOF'.  Use 'closeNotifyReceived' to tell+-- an orderly closure from a truncation. recvData :: MonadIO m => Context -> m ByteString recvData ctx = liftIO $ do     tls13 <- tls13orLater ctx
Network/TLS/Error.hs view
@@ -13,7 +13,9 @@ -- -- Prior to version 1.8.0, this type had an @Exception@ instance. -- In version 1.8.0, this instance was removed, and functions in--- this library now only throw 'TLSException'.+-- this library now throw 'TLSException' for TLS errors.  Exceptions+-- of the backend, such as an 'E.IOException' for a reset connection,+-- pass through 'Network.TLS.recvData' and 'Network.TLS.sendData' as they are. data TLSError     = -- | mainly for instance of Error       Error_Misc String
Network/TLS/Handshake/Common.hs view
@@ -96,6 +96,9 @@                     sendPacket13 ctx $                         Alert13 [errorToAlert tlserror]             else sendPacket12 ctx $ Alert [errorToAlert tlserror]+    -- The connection is over: 'bye' neither waits nor sends close_notify.+    -- Both callers hold the read lock.+    setEOF ctx     handshakeFailed tlserror   where     ignoreIOErr :: E.IOException -> IO ()
Network/TLS/Handshake/Server/TLS13.hs view
@@ -410,21 +410,27 @@     let (level, desc) = errorToAlert err         reason = errorToAlertMessage err         send = sendPacket13 ctx . Alert13-    catchException (send [(level, desc)]) (\_ -> return ())+    withWriteLock ctx $ catchException (send [(level, desc)]) (\_ -> return ())     setEOF ctx     E.throwIO $ Terminated False reason err  handleEx :: Context -> IO Bool -> IO Bool handleEx ctx f = catchException f $ \exception -> do+    -- The connection is closed already, e.g. by 'terminate' with its alert.+    eof <- ctxEOF ctx+    when eof $ E.throwIO exception     -- If the error was an Uncontextualized TLSException, we replace the-    -- context with HandshakeFailed. If it's anything else, we convert-    -- it to a string and wrap it with Error_Misc and HandshakeFailed.+    -- context with PostHandshake. If it's anything else, we convert+    -- it to a string and wrap it with Error_Misc and PostHandshake.     let tlserror = case E.fromException exception of             Just e | Uncontextualized e' <- e -> e'             _ -> Error_Misc (show exception)-    sendPacket13 ctx $ Alert13 [errorToAlert tlserror]-    void $ E.throwIO $ PostHandshake tlserror-    return False+    withWriteLock ctx $+        catchException+            (sendPacket13 ctx $ Alert13 [errorToAlert tlserror])+            (\_ -> return ())+    setEOF ctx+    E.throwIO $ PostHandshake tlserror  ---------------------------------------------------------------- 
Network/TLS/Record/Recv.hs view
@@ -4,6 +4,7 @@     recvRecord13, ) where +import qualified Control.Exception as E import qualified Data.ByteString as B  import Network.TLS.Context.Internal@@ -131,7 +132,9 @@  readExactBytes :: Context -> Int -> IO (Either TLSError ByteString) readExactBytes ctx sz = do-    hdrbs <- contextRecv ctx sz+    -- A broken transport ends the connection, which also wakes up those+    -- waiting for a post-handshake authentication.+    hdrbs <- contextRecv ctx sz `E.catch` broken     if B.length hdrbs == sz         then return $ Right hdrbs         else do@@ -146,3 +149,6 @@                                 ++ " bytes, got: "                                 ++ show (B.length hdrbs)                             )+  where+    broken :: E.IOException -> IO a+    broken e = setEOF ctx >> E.throwIO e
test/ByeSpec.hs view
@@ -1,13 +1,16 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE CPP #-}+{-# LANGUAGE TypeApplications #-}  module ByeSpec where  import Control.Concurrent import Control.Concurrent.Async import Control.Concurrent.STM+import qualified Control.Exception as E import Control.Monad (when) import Data.ByteString (ByteString)+import Data.Either (isLeft) import Data.IORef import qualified Network.Socket as N import qualified Network.Socket.ByteString as NB@@ -48,6 +51,10 @@                 recvWith 4 (readTVar late >>= check) `shouldReturn` Left ()                 NB.sendAll ss "abcd"                 recvWith 4 (retry :: STM ()) `shouldReturn` Right "abcd"+                -- The bytes received before giving up are not lost.+                NB.sendAll ss "ab"+                late' <- registerDelay 50000+                recvWith 4 (readTVar late' >>= check) `shouldReturn` Right "ab"         N.close cs         N.close ss #else@@ -73,6 +80,41 @@         writeIORef armed True         timeout 5000000 (bye cCtx) `shouldReturn` Just ()         readIORef readAfter `shouldReturn` False++    it "neither waits nor sends after a failed handshake" $ do+        (cParams, sParams) <- generate arbitraryPairParams13+        pipe <- newPipe+        _ <- runPipe pipe+        used <- newIORef False+        let noop = return ()+            cBackend = Backend noop noop (writePipeC pipe) (readPipeC pipe)+            sBackend = Backend noop noop (writePipeS pipe) (readPipeS pipe)+            reject _ = return $ CertificateUsageReject $ CertificateRejectOther "test"+            sParams' =+                sParams+                    { serverWantClientCert = True+                    , serverHooks = (serverHooks sParams){onClientCertificate = reject}+                    }+        cCtx <- contextNew cBackend cParams+        sCtx <-+            contextNew (GivingUp sBackend $ recvWithVia (readPipeS pipe) used) sParams'+        (_, r) <-+            concurrently+                (E.try @E.SomeException $ handshake cCtx)+                (E.try @TLSException $ handshake sCtx)+        r `shouldSatisfy` isLeft+        timeout 5000000 (bye sCtx) `shouldReturn` Just ()+        readIORef used `shouldReturn` False++    it "does not throw when the peer has gone" $ do+        (cParams, sParams) <- generate arbitraryPairParams13+        (cs, ss) <- N.socketPair N.AF_UNIX N.Stream N.defaultProtocol+        cCtx <- contextNew cs cParams+        sCtx <- contextNew ss sParams+        concurrently_ (handshake cCtx) (handshake sCtx)+        N.close ss+        timeout 5000000 (bye cCtx) `shouldReturn` Just ()+        N.close cs  -- | A backend which can give up on a receive. data GivingUp = GivingUp Backend RecvWith
test/HandshakeSpec.hs view
@@ -304,6 +304,10 @@             post_handshake_auth_async_wait True         it "fails an asynchronous post-handshake auth with a rejected certificate" $             post_handshake_auth_async_wait False+        it "fails the waiter when the transport breaks" $+            post_handshake_auth_async_reset+        it "closes the connection when a synchronous post-handshake auth fails" $+            post_handshake_auth_sync_fail         it "commits the client certificate of a post-handshake auth at Finished" $             post_handshake_auth_async_finished         it@@ -3384,6 +3388,24 @@         _ <- requestCertificate ctx -- two simultaneously         sendData ctx "response 2" +-- | After a rejected certificate, the alert has closed the connection.+post_handshake_auth_sync_fail :: IO ()+post_handshake_auth_sync_fail = do+    params <- generate arbitraryPairParams13+    cred <- generate arbitraryPHACredential+    withPairContext (phaParams False cred params) $ \(cCtx, sCtx) ->+        concurrently_ (hsServer sCtx) (hsClient cCtx)+  where+    hsClient ctx = do+        handshake ctx+        sendData ctx "request 1"+        void (recvData ctx) `shouldThrow` anyTLSException+    hsServer ctx = do+        handshake ctx+        recvDataAssert ctx "request 1"+        void (requestCertificate ctx) `shouldThrow` anyTLSException+        sendData ctx "response 1" `shouldThrow` anyTLSException+ type PairParams = (ClientParams, ServerParams)  -- | Client and server parameters for post-handshake authentication with@@ -3479,6 +3501,33 @@                 void (recvData ctx) `shouldThrow` anyTLSException                 takeMVar result >>= (`shouldSatisfy` isFailed)     isFailed (PHAFailed _) = True+    isFailed _ = False++-- | The transport breaks while a response is pending: the waiter fails+-- instead of blocking forever.+post_handshake_auth_async_reset :: IO ()+post_handshake_auth_async_reset = do+    params <- generate arbitraryPairParams13+    cred <- generate arbitraryPHACredential+    let (cParams, sParams) = phaParams True cred params+    pipe <- newPipe+    _ <- runPipe pipe+    broken <- newIORef False+    let noop = return ()+        sRecv n = do+            b <- readIORef broken+            if b then ioError (userError "reset") else readPipeS pipe n+        cBackend = Backend noop noop (writePipeC pipe) (readPipeC pipe)+        sBackend = Backend noop noop (writePipeS pipe) sRecv+    cCtx <- contextNew cBackend cParams+    sCtx <- contextNew sBackend sParams+    concurrently_ (handshake cCtx) (handshake sCtx)+    check <- requestCertificateAsync sCtx+    writeIORef broken True+    void (recvData sCtx) `shouldThrow` anyIOException+    timeout 1000000 (check True) >>= (`shouldSatisfy` isFailed)+  where+    isFailed (Just (PHAFailed _)) = True     isFailed _ = False  -- | The client's Finished is held back after its CertificateVerify has been
tls.cabal view
@@ -1,6 +1,6 @@ cabal-version:      2.0 name:               tls-version:            2.6.1+version:            2.6.2 license:            BSD3 license-file:       LICENSE copyright:          Vincent Hanquez <vincent@snarc.org>@@ -124,7 +124,7 @@         base16-bytestring,         bytestring >=0.10 && <0.13,         cereal >=0.5.3 && <0.6,-        crypton >=2.1.9 && <2.3,+        crypton >=2.1.9 && <2.4,         crypton-asn1-encoding >= 0.10.0 && < 0.12,         crypton-asn1-types >= 0.4.1 && < 0.5,         crypton-x509 >=1.10 && <1.12,