Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:jpmorganchase/constellation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmn committed Feb 8, 2018
2 parents dc9a1ac + f5939b6 commit 967964a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.stack-work/
.stack-work
h
*.key
*.pub
Expand Down
2 changes: 1 addition & 1 deletion Constellation/Enclave.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ getCombinedKeys Enclave{..} sender rcpts = do
Nothing -> error "getCombinedKeys: Matching private key not found"
Just pk -> (k:ks, ncc, True)
where
k = safeBeforeNM (unPublicKey sender) pk (unPublicKey rcpt)
!k = safeBeforeNM (unPublicKey sender) pk (unPublicKey rcpt)
ncc = HM.insert (sender, rcpt) k cc
Just k -> (k:ks, cc, chd)
when ccChanged $ writeTVar enclaveComboCache finalCc
Expand Down
5 changes: 4 additions & 1 deletion Constellation/Enclave/Payload.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ encrypt pl sender pk rcpts = encrypt' pl sender cks
where
cks = map (safeBeforeNM sender pk) rcpts

safeBeforeNM :: Box.PublicKey -> Box.SecretKey -> Box.PublicKey -> Box.CombinedKey
safeBeforeNM :: Box.PublicKey
-> Box.SecretKey
-> Box.PublicKey
-> Box.CombinedKey
safeBeforeNM sender pk rcpt
| sender == rcpt = error "safeBeforeNM: Sender cannot be a recipient"
| otherwise = Box.beforeNM pk rcpt
Expand Down
64 changes: 40 additions & 24 deletions Constellation/Node/Api.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
Expand Down Expand Up @@ -62,13 +63,13 @@ instance ToJSON SendResponse where

data Receive = Receive
{ rreqKey :: Text
, rreqTo :: PublicKey
, rreqTo :: Maybe PublicKey
} deriving (Show)

instance FromJSON Receive where
parseJSON (Object v) = Receive
<$> v .: "key"
<*> v .: "to"
<$> v .: "key"
<*> v .:? "to"
parseJSON _ = mzero

data ReceiveResponse = ReceiveResponse
Expand Down Expand Up @@ -148,29 +149,44 @@ hFrom = "c11n-from"
hTo :: HeaderName
hTo = "c11n-to"

hKey :: HeaderName
hKey = "c11n-key"

decodeSendRaw :: BL.ByteString -> RequestHeaders -> Either String Send
decodeSendRaw b h = case getHeaderCommaValues hTo h of
[] -> Left "decodeSendRaw: To header not found"
to -> case efrom of
Left err -> Left err
Right from -> Right Send
to -> case onePublicKeyFromHeaderValues $ getHeaderValues hFrom h of
Left err -> Left err
Right mfrom -> Right Send
{ sreqPayload = toStrict b
, sreqFrom = from
, sreqFrom = mfrom
, sreqTo = map mustDecodeB64PublicKey to
}
where
efrom = case getHeaderValues hFrom h of
[] -> Right Nothing
[fromB64] -> case convertFromBase Base64 fromB64 of
Left err -> Left err
Right from -> case mkPublicKey from of
Nothing -> Left "decodeSendRaw: Invalid From public key"
Just pub -> Right $ Just pub
_ -> Left "decodeSendRaw: More than one From value given"

onePublicKeyFromHeaderValues :: [ByteString] -> Either String (Maybe PublicKey)
onePublicKeyFromHeaderValues [] = Right Nothing
onePublicKeyFromHeaderValues [fromB64] = case convertFromBase Base64 fromB64 of
Left err -> Left err
Right from -> case mkPublicKey from of
Nothing -> Left "onePublicKeyFromHeaderValues: Invalid public key"
Just pub -> Right $ Just pub
onePublicKeyFromHeaderValues _ =
Left "onePublicKeyFromHeaderValues: More than one value given"

mustDecodeB64PublicKey :: ByteString -> PublicKey
mustDecodeB64PublicKey = fromJust . mkPublicKey . mustB64DecodeBs

decodeReceiveRaw :: RequestHeaders -> Either String Receive
decodeReceiveRaw h = case getHeaderCommaValues hKey h of
[] -> Left "decodeReceiveRaw: Key header not found"
[k] -> case onePublicKeyFromHeaderValues $ getHeaderValues hTo h of
Left err -> Left err
Right mto -> Right Receive
{ rreqKey = TE.decodeUtf8 k
, rreqTo = mto
}
_ -> Left "decodeReceiveRaw: More than one Key value given"

whitelist :: [String] -> Whitelist
whitelist strs = Whitelist
{ wlIPv4 = Set.fromList v4Addrs
Expand Down Expand Up @@ -232,11 +248,11 @@ parseRequest :: [Text] -> BL.ByteString -> RequestHeaders -> Either String ApiRe
-----
-- Node client
-----
parseRequest ["send"] b _ = ApiSend <$> AE.eitherDecode' b
parseRequest ["receive"] b _ = ApiReceive <$> AE.eitherDecode' b
parseRequest ["sendraw"] b h = ApiSend <$> decodeSendRaw b h
parseRequest ["receiveraw"] b _ = ApiReceiveRaw <$> AE.eitherDecode' b
parseRequest ["delete"] b _ = ApiDelete <$> AE.eitherDecode' b
parseRequest ["send"] b _ = ApiSend <$> AE.eitherDecode' b
parseRequest ["receive"] b _ = ApiReceive <$> AE.eitherDecode' b
parseRequest ["sendraw"] b h = ApiSend <$> decodeSendRaw b h
parseRequest ["receiveraw"] _ h = ApiReceiveRaw <$> decodeReceiveRaw h
parseRequest ["delete"] b _ = ApiDelete <$> AE.eitherDecode' b
-----
-- Node to node
-----
Expand Down Expand Up @@ -299,9 +315,9 @@ send node Send{..} = do
else return $ Left $ "sendRequest: Errors while running sendPayload: " ++ show eks

receive :: Node -> Receive -> IO (Either String ReceiveResponse)
receive node Receive{..} = do
epl <- receivePayload node rreqKey rreqTo
case epl of
receive node Receive{..} = case rreqTo <|> nodeDefaultPub node of
Nothing -> return $ Left "receive: No To public key given and no default is set"
Just to -> receivePayload node rreqKey to >>= \case
Left err -> return $ Left err
Right pl -> return $ Right ReceiveResponse
{ rresPayload = pl
Expand Down

0 comments on commit 967964a

Please sign in to comment.