diff options
author | Benjamin Barenblat <bbaren@google.com> | 2018-09-01 15:12:30 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2018-09-04 11:47:56 -0500 |
commit | a31daa3545c0a8cb5f95e88d66cfcee55a7ee925 (patch) | |
tree | 9c10af070163f12fe369c185e1b7681db492e5a1 /src/BTLS/Result.hs | |
parent | 92a90ad43381f6897a93503027d67ac0b1032f3e (diff) |
Enable error checking for HMAC computations
Diffstat (limited to 'src/BTLS/Result.hs')
-rw-r--r-- | src/BTLS/Result.hs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/BTLS/Result.hs b/src/BTLS/Result.hs index 7cd5839..63626bd 100644 --- a/src/BTLS/Result.hs +++ b/src/BTLS/Result.hs @@ -14,11 +14,15 @@ module BTLS.Result ( alwaysSucceeds, requireSuccess - , Error, file, line, errorData, errorDataIsHumanReadable + , Result, Error, file, line, errorData, errorDataIsHumanReadable + , check ) where +import Control.Concurrent (rtsSupportsBoundThreads, runInBoundThread) import Control.Exception (assert) -import Control.Monad (when) +import Control.Monad (guard, unless, when) +import Control.Monad.Loops (unfoldM) +import Control.Monad.Trans.Except (ExceptT(ExceptT)) import Data.Bits ((.&.)) import Data.ByteString (ByteString) import Foreign (allocaArray) @@ -34,6 +38,8 @@ alwaysSucceeds r = assert (r == 1) (return ()) requireSuccess :: CInt -> IO () requireSuccess r = when (r /= 1) $ ioError (userError "BoringSSL failure") +type Result = Either [Error] + data Error = Error { err :: Err , file :: FilePath @@ -52,3 +58,26 @@ instance Show Error where allocaArray len $ \pOut -> do errErrorStringN (err e) pOut len peekCString pOut + +errorFromTuple :: (Err, FilePath, Int, Maybe ByteString, CInt) -> Error +errorFromTuple = uncurry5 Error + +dequeueError :: IO (Maybe Error) +dequeueError = do + e@((Err code), _file, _line, _extra, _flags) <- errGetErrorLineData + guard (code /= 0) + return (Just (errorFromTuple e)) + +check :: IO Int -> ExceptT [Error] IO () +check f = do + unless rtsSupportsBoundThreads $ + error "btls requires the threaded runtime. Please recompile with -threaded." + ExceptT $ runInBoundThread $ do + -- TODO(bbaren): Assert that the error queue is clear + r <- f + if r == 1 + then Right <$> return () + else Left <$> unfoldM dequeueError + +uncurry5 :: (a -> b -> c -> d -> e -> z) -> (a, b, c, d, e) -> z +uncurry5 f (a, b, c, d, e) = f a b c d e |