Skip to content

Commit 8414010

Browse files
committed
Move from depending on xmpp to just stringprep
1 parent b30857a commit 8414010

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
{erl_opts, [debug_info, {src_dirs, ["src"]}, {i, "include"}]}.
2424

25-
{deps, [{xmpp, "~> 1.12.0", {git, "https://github.com/processone/xmpp", {tag, "1.12.0"}}}]}.
25+
{deps, [{stringprep, "~> 1.0.33", {git, "https://github.com/processone/stringprep", {tag, "1.0.33"}}}]}.
2626

2727
{cover_enabled, true}.
2828
{cover_export_enabled, true}.

src/pgsql_sasl.erl

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ client_step(State, ServerResponse) ->
5555
R = proplists:get_value(<<"r">>, SResp),
5656
S = base64:decode(proplists:get_value(<<"s">>, SResp)),
5757
Nonce = State#sasl_state.nonce,
58-
NonceSize = size(Nonce),
58+
NonceSize = byte_size(Nonce),
5959
case R of
6060
<<Nonce:NonceSize/binary, _/binary>> ->
6161
ClientMsg1 = client_first_message_bare(
@@ -67,20 +67,20 @@ client_step(State, ServerResponse) ->
6767
ServerResponse/binary, ",",
6868
ClientMsg2/binary>>,
6969
Password = State#sasl_state.password,
70-
SaltedPassword = scram:salted_password(
70+
SaltedPassword = salted_password(
7171
sha256, Password, S, I),
7272
ClientKey =
73-
scram:client_key(sha256, SaltedPassword),
74-
StoredKey = scram:stored_key(sha256, ClientKey),
73+
client_key(sha256, SaltedPassword),
74+
StoredKey = stored_key(sha256, ClientKey),
7575
ClientSignature =
76-
scram:client_signature(sha256, StoredKey, AuthMessage),
76+
client_signature(sha256, StoredKey, AuthMessage),
7777
ClientProof =
7878
crypto:exor(ClientKey, ClientSignature),
7979
P = base64:encode(ClientProof),
8080
Msg = <<ClientMsg2/binary, ",p=", P/binary>>,
8181
ServerKey =
82-
scram:server_key(sha256, SaltedPassword),
83-
V = scram:server_signature(sha256, ServerKey, AuthMessage),
82+
server_key(sha256, SaltedPassword),
83+
V = server_signature(sha256, ServerKey, AuthMessage),
8484
{ok, Msg, State#sasl_state{nonce = R, verify = V}};
8585
_ ->
8686
{error, "Bad SASL server nonce"}
@@ -140,3 +140,30 @@ parse4(<<C, Cs/binary>>, Key, Val, Ts) ->
140140
parse4(<<>>, Key, Val, Ts) ->
141141
parse1(<<>>, <<>>, [{Key, Val} | Ts]).
142142

143+
salted_password(Algo, Password, Salt, IterationCount) ->
144+
hi(Algo, stringprep:resourceprep(Password), Salt, IterationCount).
145+
146+
client_key(Algo, SaltedPassword) ->
147+
crypto:mac(hmac, Algo, SaltedPassword, <<"Client Key">>).
148+
149+
stored_key(Algo, ClientKey) -> crypto:hash(Algo, ClientKey).
150+
151+
server_key(Algo, SaltedPassword) ->
152+
crypto:mac(hmac, Algo, SaltedPassword, <<"Server Key">>).
153+
154+
client_signature(Algo, StoredKey, AuthMessage) ->
155+
crypto:mac(hmac, Algo, StoredKey, AuthMessage).
156+
157+
server_signature(Algo, ServerKey, AuthMessage) ->
158+
crypto:mac(hmac, Algo, ServerKey, AuthMessage).
159+
160+
hi(Algo, Password, Salt, IterationCount) ->
161+
U1 = crypto:mac(hmac, Algo, Password, <<Salt/binary, 0, 0, 0, 1>>),
162+
crypto:exor(U1, hi_round(Algo, Password, U1, IterationCount - 1)).
163+
164+
hi_round(Algo, Password, UPrev, 1) ->
165+
crypto:mac(hmac, Algo, Password, UPrev);
166+
hi_round(Algo, Password, UPrev, IterationCount) ->
167+
U = crypto:mac(hmac, Algo, Password, UPrev),
168+
crypto:exor(U, hi_round(Algo, Password, U, IterationCount - 1)).
169+

0 commit comments

Comments
 (0)