|
|
|
@ -27,6 +27,7 @@ import qualified Data.Text.Encoding as Text |
|
|
|
import Data.Word(Word8) |
|
|
|
import Data.Word(Word8) |
|
|
|
|
|
|
|
|
|
|
|
import Network.Xmpp.Sasl.Common |
|
|
|
import Network.Xmpp.Sasl.Common |
|
|
|
|
|
|
|
import Network.Xmpp.Sasl.StringPrep |
|
|
|
import Network.Xmpp.Sasl.Types |
|
|
|
import Network.Xmpp.Sasl.Types |
|
|
|
|
|
|
|
|
|
|
|
-- | Bit-wise xor of byte strings |
|
|
|
-- | Bit-wise xor of byte strings |
|
|
|
@ -57,83 +58,91 @@ scram :: (Crypto.Hash ctx hash) |
|
|
|
-> Maybe Text.Text -- ^ authorization ID |
|
|
|
-> Maybe Text.Text -- ^ authorization ID |
|
|
|
-> Text.Text -- ^ password |
|
|
|
-> Text.Text -- ^ password |
|
|
|
-> SaslM () |
|
|
|
-> SaslM () |
|
|
|
scram hashToken authcid authzid' password = do |
|
|
|
scram hashToken authcid authzid password = case credentials of |
|
|
|
cnonce <- liftIO $ makeNonce |
|
|
|
Nothing -> throwError $ AuthStringPrepError |
|
|
|
saslInit "SCRAM-SHA-1" (Just $ cFirstMessage cnonce) |
|
|
|
Just (ac, az, pw) -> scramhelper hashToken ac az pw |
|
|
|
liftIO $ putStrLn "pulling challenge" |
|
|
|
|
|
|
|
sFirstMessage <- saslFromJust =<< pullChallenge |
|
|
|
|
|
|
|
liftIO $ putStrLn "pulled challenge" |
|
|
|
|
|
|
|
pairs <- toPairs sFirstMessage |
|
|
|
|
|
|
|
(nonce, salt, ic) <- fromPairs pairs cnonce |
|
|
|
|
|
|
|
let (cfm, v) = cFinalMessageAndVerifier nonce salt ic sFirstMessage cnonce |
|
|
|
|
|
|
|
respond $ Just cfm |
|
|
|
|
|
|
|
finalPairs <- toPairs =<< saslFromJust =<< pullFinalMessage |
|
|
|
|
|
|
|
unless (lookup "v" finalPairs == Just v) $ throwError AuthServerAuthError |
|
|
|
|
|
|
|
return () |
|
|
|
|
|
|
|
where |
|
|
|
where |
|
|
|
-- We need to jump through some hoops to get a polymorphic solution |
|
|
|
credentials = do |
|
|
|
encode :: Crypto.Hash ctx hash => hash -> hash -> BS.ByteString |
|
|
|
ac <- normalizeUsername authcid |
|
|
|
encode _hashtoken = Crypto.encode |
|
|
|
az <- case authzid of |
|
|
|
hash str = encode hashToken $ Crypto.hash' str |
|
|
|
Nothing -> Just Nothing |
|
|
|
hmac key str = encode hashToken $ Crypto.hmac' (Crypto.MacKey key) str |
|
|
|
Just az' -> Just <$> normalizeUsername az' |
|
|
|
|
|
|
|
pw <- normalizePassword password |
|
|
|
authzid = (\z -> "a=" +++ normalize z) <$> authzid' |
|
|
|
return (ac, az, pw) |
|
|
|
gs2CbindFlag = "n" -- we don't support channel binding yet |
|
|
|
scramhelper hashToken authcid authzid' password = do |
|
|
|
gs2Header = merge $ [ gs2CbindFlag |
|
|
|
cnonce <- liftIO $ makeNonce |
|
|
|
, maybe "" id authzid |
|
|
|
saslInit "SCRAM-SHA-1" (Just $ cFirstMessage cnonce) |
|
|
|
, "" |
|
|
|
sFirstMessage <- saslFromJust =<< pullChallenge |
|
|
|
] |
|
|
|
pairs <- toPairs sFirstMessage |
|
|
|
cbindData = "" -- we don't support channel binding yet |
|
|
|
(nonce, salt, ic) <- fromPairs pairs cnonce |
|
|
|
cFirstMessageBare cnonce = merge [ "n=" +++ normalize authcid |
|
|
|
let (cfm, v) = cFinalMessageAndVerifier nonce salt ic sFirstMessage cnonce |
|
|
|
, "r=" +++ cnonce] |
|
|
|
respond $ Just cfm |
|
|
|
cFirstMessage cnonce = gs2Header +++ cFirstMessageBare cnonce |
|
|
|
finalPairs <- toPairs =<< saslFromJust =<< pullFinalMessage |
|
|
|
|
|
|
|
unless (lookup "v" finalPairs == Just v) $ throwError AuthServerAuthError |
|
|
|
fromPairs :: Pairs |
|
|
|
return () |
|
|
|
-> BS.ByteString |
|
|
|
|
|
|
|
-> SaslM (BS.ByteString, BS.ByteString, Int) |
|
|
|
|
|
|
|
fromPairs pairs cnonce | Just nonce <- lookup "r" pairs |
|
|
|
|
|
|
|
, cnonce `BS.isPrefixOf` nonce |
|
|
|
|
|
|
|
, Just salt' <- lookup "s" pairs |
|
|
|
|
|
|
|
, Right salt <- B64.decode salt' |
|
|
|
|
|
|
|
, Just ic <- lookup "i" pairs |
|
|
|
|
|
|
|
, [(i,"")] <- reads $ BS8.unpack ic |
|
|
|
|
|
|
|
= return (nonce, salt, i :: Int) |
|
|
|
|
|
|
|
fromPairs _ _ = throwError $ AuthChallengeError |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cFinalMessageAndVerifier nonce salt ic sfm cnonce |
|
|
|
|
|
|
|
= (merge [ cFinalMessageWOProof |
|
|
|
|
|
|
|
, "p=" +++ B64.encode clientProof |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
, B64.encode serverSignature |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
where |
|
|
|
where |
|
|
|
cFinalMessageWOProof = merge [ "c=" +++ B64.encode gs2Header |
|
|
|
-- We need to jump through some hoops to get a polymorphic solution |
|
|
|
, "r=" +++ nonce] |
|
|
|
encode :: Crypto.Hash ctx hash => hash -> hash -> BS.ByteString |
|
|
|
saltedPassword = hi (normalize password) salt ic |
|
|
|
encode _hashtoken = Crypto.encode |
|
|
|
clientKey = hmac saltedPassword "Client Key" |
|
|
|
hash str = encode hashToken $ Crypto.hash' str |
|
|
|
storedKey = hash clientKey |
|
|
|
hmac key str = encode hashToken $ Crypto.hmac' (Crypto.MacKey key) str |
|
|
|
authMessage = merge [ cFirstMessageBare cnonce |
|
|
|
|
|
|
|
, sfm |
|
|
|
authzid = (\z -> "a=" +++ Text.encodeUtf8 z) <$> authzid' |
|
|
|
, cFinalMessageWOProof |
|
|
|
gs2CbindFlag = "n" -- we don't support channel binding yet |
|
|
|
] |
|
|
|
gs2Header = merge $ [ gs2CbindFlag |
|
|
|
clientSignature = hmac storedKey authMessage |
|
|
|
, maybe "" id authzid |
|
|
|
clientProof = clientKey `xorBS` clientSignature |
|
|
|
, "" |
|
|
|
serverKey = hmac saltedPassword "Server Key" |
|
|
|
] |
|
|
|
serverSignature = hmac serverKey authMessage |
|
|
|
cbindData = "" -- we don't support channel binding yet |
|
|
|
|
|
|
|
cFirstMessageBare cnonce = merge [ "n=" +++ Text.encodeUtf8 authcid |
|
|
|
-- helper |
|
|
|
, "r=" +++ cnonce] |
|
|
|
hi str salt ic = foldl1' xorBS (take ic us) |
|
|
|
cFirstMessage cnonce = gs2Header +++ cFirstMessageBare cnonce |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fromPairs :: Pairs |
|
|
|
|
|
|
|
-> BS.ByteString |
|
|
|
|
|
|
|
-> SaslM (BS.ByteString, BS.ByteString, Int) |
|
|
|
|
|
|
|
fromPairs pairs cnonce | Just nonce <- lookup "r" pairs |
|
|
|
|
|
|
|
, cnonce `BS.isPrefixOf` nonce |
|
|
|
|
|
|
|
, Just salt' <- lookup "s" pairs |
|
|
|
|
|
|
|
, Right salt <- B64.decode salt' |
|
|
|
|
|
|
|
, Just ic <- lookup "i" pairs |
|
|
|
|
|
|
|
, [(i,"")] <- reads $ BS8.unpack ic |
|
|
|
|
|
|
|
= return (nonce, salt, i :: Int) |
|
|
|
|
|
|
|
fromPairs _ _ = throwError $ AuthChallengeError |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cFinalMessageAndVerifier nonce salt ic sfm cnonce |
|
|
|
|
|
|
|
= (merge [ cFinalMessageWOProof |
|
|
|
|
|
|
|
, "p=" +++ B64.encode clientProof |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
, B64.encode serverSignature |
|
|
|
|
|
|
|
) |
|
|
|
where |
|
|
|
where |
|
|
|
u1 = hmac str (salt +++ (BS.pack [0,0,0,1])) |
|
|
|
cFinalMessageWOProof = merge [ "c=" +++ B64.encode gs2Header |
|
|
|
us = iterate (hmac str) u1 |
|
|
|
, "r=" +++ nonce] |
|
|
|
|
|
|
|
saltedPassword = hi (Text.encodeUtf8 password) salt ic |
|
|
|
normalize = Text.encodeUtf8 . id -- TODO: stringprep |
|
|
|
clientKey = hmac saltedPassword "Client Key" |
|
|
|
base64 = B64.encode |
|
|
|
storedKey = hash clientKey |
|
|
|
|
|
|
|
authMessage = merge [ cFirstMessageBare cnonce |
|
|
|
|
|
|
|
, sfm |
|
|
|
|
|
|
|
, cFinalMessageWOProof |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
clientSignature = hmac storedKey authMessage |
|
|
|
|
|
|
|
clientProof = clientKey `xorBS` clientSignature |
|
|
|
|
|
|
|
serverKey = hmac saltedPassword "Server Key" |
|
|
|
|
|
|
|
serverSignature = hmac serverKey authMessage |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- helper |
|
|
|
|
|
|
|
hi str salt ic = foldl1' xorBS (take ic us) |
|
|
|
|
|
|
|
where |
|
|
|
|
|
|
|
u1 = hmac str (salt +++ (BS.pack [0,0,0,1])) |
|
|
|
|
|
|
|
us = iterate (hmac str) u1 |
|
|
|
|
|
|
|
|
|
|
|
-- | 'scram' spezialised to the SHA-1 hash function, packaged as a SaslHandler |
|
|
|
-- | 'scram' spezialised to the SHA-1 hash function, packaged as a SaslHandler |
|
|
|
scramSha1 :: SaslM Text.Text -> SaslHandler |
|
|
|
scramSha1 :: Text.Text -- ^ username |
|
|
|
scramSha1 passwd = ("SCRAM-SHA-1" |
|
|
|
-> Maybe Text.Text -- ^ authorization ID |
|
|
|
, \_hostname authcid authzid -> do |
|
|
|
-> Text.Text -- ^ password |
|
|
|
pw <- passwd |
|
|
|
-> SaslHandler |
|
|
|
scram (hashToken :: Crypto.SHA1) authcid authzid pw |
|
|
|
scramSha1 authcid authzid passwd = |
|
|
|
) |
|
|
|
("SCRAM-SHA-1" |
|
|
|
|
|
|
|
, scram (hashToken :: Crypto.SHA1) authcid authzid passwd |
|
|
|
|
|
|
|
) |
|
|
|
|