2003-03-12 20:48:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : cyrsasl_digest.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : DIGEST-MD5 SASL mechanism
|
|
|
|
%%% Created : 11 Mar 2003 by Alexey Shchepin <alexey@sevcom.net>
|
2009-06-09 12:56:49 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 ProcessOne
|
2009-06-09 12:56:49 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-03-12 20:48:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(cyrsasl_digest).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
|
|
|
|
-export([start/1,
|
|
|
|
stop/0,
|
2010-04-15 17:20:16 +02:00
|
|
|
mech_new/1,
|
2003-03-12 20:48:05 +01:00
|
|
|
mech_step/2]).
|
|
|
|
|
2008-03-21 15:44:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2010-04-15 17:20:16 +02:00
|
|
|
-include("cyrsasl.hrl").
|
2008-03-21 15:44:16 +01:00
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
-behaviour(cyrsasl).
|
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
%% @type mechstate() = {state, Step, Nonce, Username, AuthzId, GetPassword, CheckPassword, AuthModule, Host}
|
2009-01-23 11:10:33 +01:00
|
|
|
%% Step = 1 | 3 | 5
|
|
|
|
%% Nonce = string()
|
|
|
|
%% Username = string()
|
|
|
|
%% AuthzId = string()
|
|
|
|
%% GetPassword = function()
|
|
|
|
%% AuthModule = atom()
|
|
|
|
%% Host = string().
|
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
-record(state, {step, nonce, username, authzid, get_password, check_password, auth_module,
|
2009-01-19 11:14:04 +01:00
|
|
|
host}).
|
2003-03-12 20:48:05 +01:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (Opts) -> true
|
|
|
|
%% Opts = term()
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
start(_Opts) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
cyrsasl:register_mechanism("DIGEST-MD5", ?MODULE, true).
|
2003-03-12 20:48:05 +01:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec () -> ok
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
stop() ->
|
|
|
|
ok.
|
|
|
|
|
2010-04-15 17:20:16 +02:00
|
|
|
mech_new(#sasl_params{host=Host, get_password=GetPassword,
|
|
|
|
check_password_digest=CheckPasswordDigest}) ->
|
2003-03-12 20:48:05 +01:00
|
|
|
{ok, #state{step = 1,
|
2005-04-17 20:08:34 +02:00
|
|
|
nonce = randoms:get_string(),
|
2009-01-19 11:14:04 +01:00
|
|
|
host = Host,
|
2009-04-22 13:44:03 +02:00
|
|
|
get_password = GetPassword,
|
|
|
|
check_password = CheckPasswordDigest}}.
|
2003-03-12 20:48:05 +01:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (State, ClientIn) -> Ok | Continue | Error
|
|
|
|
%% State = mechstate()
|
|
|
|
%% ClientIn = string()
|
|
|
|
%% Ok = {ok, Props}
|
|
|
|
%% Props = [Prop]
|
|
|
|
%% Prop = {username, Username} | {authzid, AuthzId} | {auth_module, AuthModule}
|
|
|
|
%% Username = string()
|
|
|
|
%% AuthzId = string()
|
|
|
|
%% AuthModule = atom()
|
|
|
|
%% Continue = {continue, ServerOut, New_State}
|
|
|
|
%% ServerOut = string()
|
|
|
|
%% New_State = mechstate()
|
|
|
|
%% Error = {error, Reason} | {error, Reason, Username}
|
|
|
|
%% Reason = term()
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
mech_step(#state{step = 1, nonce = Nonce} = State, _) ->
|
2003-03-12 20:48:05 +01:00
|
|
|
{continue,
|
2003-10-16 20:17:44 +02:00
|
|
|
"nonce=\"" ++ Nonce ++
|
2003-10-07 22:31:44 +02:00
|
|
|
"\",qop=\"auth\",charset=utf-8,algorithm=md5-sess",
|
2003-03-12 20:48:05 +01:00
|
|
|
State#state{step = 3}};
|
|
|
|
mech_step(#state{step = 3, nonce = Nonce} = State, ClientIn) ->
|
|
|
|
case parse(ClientIn) of
|
|
|
|
bad ->
|
2008-07-08 17:43:52 +02:00
|
|
|
{error, 'bad-protocol'};
|
2003-03-12 20:48:05 +01:00
|
|
|
KeyVals ->
|
2009-01-19 18:45:11 +01:00
|
|
|
DigestURI = proplists:get_value("digest-uri", KeyVals, ""),
|
2009-01-19 16:58:16 +01:00
|
|
|
UserName = proplists:get_value("username", KeyVals, ""),
|
2009-01-19 11:14:04 +01:00
|
|
|
case is_digesturi_valid(DigestURI, State#state.host) of
|
|
|
|
false ->
|
|
|
|
?DEBUG("User login not authorized because digest-uri "
|
|
|
|
"seems invalid: ~p", [DigestURI]),
|
2008-07-08 17:43:52 +02:00
|
|
|
{error, 'not-authorized', UserName};
|
2009-01-19 11:14:04 +01:00
|
|
|
true ->
|
2009-01-19 16:58:16 +01:00
|
|
|
AuthzId = proplists:get_value("authzid", KeyVals, ""),
|
2009-01-19 11:14:04 +01:00
|
|
|
case (State#state.get_password)(UserName) of
|
|
|
|
{false, _} ->
|
|
|
|
{error, 'not-authorized', UserName};
|
|
|
|
{Passwd, AuthModule} ->
|
2009-04-27 22:24:21 +02:00
|
|
|
case (State#state.check_password)(UserName, "",
|
2009-04-22 13:44:03 +02:00
|
|
|
proplists:get_value("response", KeyVals, ""),
|
|
|
|
fun(PW) -> response(KeyVals, UserName, PW, Nonce, AuthzId,
|
|
|
|
"AUTHENTICATE") end) of
|
|
|
|
{true, _} ->
|
2009-01-19 11:14:04 +01:00
|
|
|
RspAuth = response(KeyVals,
|
|
|
|
UserName, Passwd,
|
|
|
|
Nonce, AuthzId, ""),
|
|
|
|
{continue,
|
|
|
|
"rspauth=" ++ RspAuth,
|
|
|
|
State#state{step = 5,
|
|
|
|
auth_module = AuthModule,
|
|
|
|
username = UserName,
|
|
|
|
authzid = AuthzId}};
|
2009-04-27 22:24:21 +02:00
|
|
|
false ->
|
|
|
|
{error, 'not-authorized', UserName};
|
2009-04-22 13:44:03 +02:00
|
|
|
{false, _} ->
|
2009-01-19 11:14:04 +01:00
|
|
|
{error, 'not-authorized', UserName}
|
|
|
|
end
|
2003-03-12 20:48:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
2003-06-07 19:30:25 +02:00
|
|
|
mech_step(#state{step = 5,
|
2008-04-22 19:41:30 +02:00
|
|
|
auth_module = AuthModule,
|
2003-06-07 19:30:25 +02:00
|
|
|
username = UserName,
|
2005-04-17 20:08:34 +02:00
|
|
|
authzid = AuthzId}, "") ->
|
2008-04-22 19:41:30 +02:00
|
|
|
{ok, [{username, UserName}, {authzid, AuthzId},
|
|
|
|
{auth_module, AuthModule}]};
|
2003-03-12 20:48:05 +01:00
|
|
|
mech_step(A, B) ->
|
2008-03-21 15:44:16 +01:00
|
|
|
?DEBUG("SASL DIGEST: A ~p B ~p", [A,B]),
|
2008-07-08 17:43:52 +02:00
|
|
|
{error, 'bad-protocol'}.
|
2003-03-12 20:48:05 +01:00
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (S) -> [{Key, Value}] | bad
|
|
|
|
%% S = string()
|
|
|
|
%% Key = string()
|
|
|
|
%% Value = string()
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
parse(S) ->
|
|
|
|
parse1(S, "", []).
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
parse1([$= | Cs], S, Ts) ->
|
|
|
|
parse2(Cs, lists:reverse(S), "", Ts);
|
2005-08-11 01:29:16 +02:00
|
|
|
parse1([$, | Cs], [], Ts) ->
|
|
|
|
parse1(Cs, [], Ts);
|
|
|
|
parse1([$\s | Cs], [], Ts) ->
|
|
|
|
parse1(Cs, [], Ts);
|
2003-03-12 20:48:05 +01:00
|
|
|
parse1([C | Cs], S, Ts) ->
|
|
|
|
parse1(Cs, [C | S], Ts);
|
|
|
|
parse1([], [], T) ->
|
|
|
|
lists:reverse(T);
|
2005-04-17 20:08:34 +02:00
|
|
|
parse1([], _S, _T) ->
|
2003-03-12 20:48:05 +01:00
|
|
|
bad.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2008-02-14 06:23:16 +01:00
|
|
|
parse2([$\" | Cs], Key, Val, Ts) ->
|
2003-03-12 20:48:05 +01:00
|
|
|
parse3(Cs, Key, Val, Ts);
|
|
|
|
parse2([C | Cs], Key, Val, Ts) ->
|
|
|
|
parse4(Cs, Key, [C | Val], Ts);
|
|
|
|
parse2([], _, _, _) ->
|
|
|
|
bad.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2008-02-14 06:23:16 +01:00
|
|
|
parse3([$\" | Cs], Key, Val, Ts) ->
|
2003-03-12 20:48:05 +01:00
|
|
|
parse4(Cs, Key, Val, Ts);
|
2008-02-14 06:23:16 +01:00
|
|
|
parse3([$\\, C | Cs], Key, Val, Ts) ->
|
|
|
|
parse3(Cs, Key, [C | Val], Ts);
|
2003-03-12 20:48:05 +01:00
|
|
|
parse3([C | Cs], Key, Val, Ts) ->
|
|
|
|
parse3(Cs, Key, [C | Val], Ts);
|
|
|
|
parse3([], _, _, _) ->
|
|
|
|
bad.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
parse4([$, | Cs], Key, Val, Ts) ->
|
|
|
|
parse1(Cs, "", [{Key, lists:reverse(Val)} | Ts]);
|
2005-08-11 01:29:16 +02:00
|
|
|
parse4([$\s | Cs], Key, Val, Ts) ->
|
|
|
|
parse4(Cs, Key, Val, Ts);
|
2003-03-12 20:48:05 +01:00
|
|
|
parse4([C | Cs], Key, Val, Ts) ->
|
|
|
|
parse4(Cs, Key, [C | Val], Ts);
|
|
|
|
parse4([], Key, Val, Ts) ->
|
|
|
|
parse1([], "", [{Key, lists:reverse(Val)} | Ts]).
|
|
|
|
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (DigestURICase, JabberHost) -> bool()
|
|
|
|
%% DigestURICase = string()
|
|
|
|
%% JabberHost = string()
|
|
|
|
%%
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @doc Check if the digest-uri is valid.
|
|
|
|
%% RFC-2831 allows to provide the IP address in Host,
|
|
|
|
%% however ejabberd doesn't allow that.
|
|
|
|
%% If the service (for example jabber.example.org)
|
|
|
|
%% is provided by several hosts (being one of them server3.example.org),
|
|
|
|
%% then digest-uri can be like xmpp/server3.example.org/jabber.example.org
|
|
|
|
%% In that case, ejabberd only checks the service name, not the host.
|
2009-01-23 11:10:33 +01:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
is_digesturi_valid(DigestURICase, JabberHost) ->
|
2009-01-19 16:58:16 +01:00
|
|
|
DigestURI = exmpp_stringprep:to_lower(DigestURICase),
|
2009-01-19 11:14:04 +01:00
|
|
|
case catch string:tokens(DigestURI, "/") of
|
|
|
|
["xmpp", Host] when Host == JabberHost ->
|
|
|
|
true;
|
|
|
|
["xmpp", _Host, ServName] when ServName == JabberHost ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2003-03-12 20:48:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
digit_to_xchar(D) when (D >= 0) and (D < 10) ->
|
|
|
|
D + 48;
|
|
|
|
digit_to_xchar(D) ->
|
|
|
|
D + 87.
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
hex(S) ->
|
|
|
|
hex(S, []).
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2003-03-12 20:48:05 +01:00
|
|
|
hex([], Res) ->
|
|
|
|
lists:reverse(Res);
|
|
|
|
hex([N | Ns], Res) ->
|
|
|
|
hex(Ns, [digit_to_xchar(N rem 16),
|
|
|
|
digit_to_xchar(N div 16) | Res]).
|
|
|
|
|
|
|
|
|
2009-01-23 11:10:33 +01:00
|
|
|
%% @spec (KeyVals, User, Passwd, Nonce, AuthzId, A2Prefix) -> string()
|
|
|
|
%% KeyVals = [{Key, Value}]
|
|
|
|
%% Key = string()
|
|
|
|
%% Value = string()
|
|
|
|
%% User = string()
|
|
|
|
%% Passwd = string()
|
|
|
|
%% Nonce = string()
|
|
|
|
%% AuthzId = nil() | string()
|
|
|
|
%% A2Prefix = string()
|
|
|
|
|
2003-10-16 20:17:44 +02:00
|
|
|
response(KeyVals, User, Passwd, Nonce, AuthzId, A2Prefix) ->
|
2009-01-19 16:58:16 +01:00
|
|
|
Realm = proplists:get_value("realm", KeyVals, ""),
|
|
|
|
CNonce = proplists:get_value("cnonce", KeyVals, ""),
|
|
|
|
DigestURI = proplists:get_value("digest-uri", KeyVals, ""),
|
|
|
|
NC = proplists:get_value("nc", KeyVals, ""),
|
|
|
|
QOP = proplists:get_value("qop", KeyVals, ""),
|
2010-12-01 20:24:55 +01:00
|
|
|
%% handle non-fully latin-1 strings as specified
|
|
|
|
%% on RFC 2831 Section 2.1.2.1 (EJAB-476)
|
|
|
|
SUser = sanitize(User),
|
|
|
|
SPasswd = sanitize(Passwd),
|
|
|
|
SRealm = sanitize(Realm),
|
2003-06-07 19:30:25 +02:00
|
|
|
A1 = case AuthzId of
|
|
|
|
"" ->
|
|
|
|
binary_to_list(
|
2010-12-01 20:24:55 +01:00
|
|
|
crypto:md5(SUser ++ ":" ++ SRealm ++ ":" ++ SPasswd)) ++
|
2003-06-07 19:30:25 +02:00
|
|
|
":" ++ Nonce ++ ":" ++ CNonce;
|
|
|
|
_ ->
|
|
|
|
binary_to_list(
|
2010-12-01 20:24:55 +01:00
|
|
|
crypto:md5(SUser ++ ":" ++ SRealm ++ ":" ++ SPasswd)) ++
|
2003-06-07 19:30:25 +02:00
|
|
|
":" ++ Nonce ++ ":" ++ CNonce ++ ":" ++ AuthzId
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
A2 = case QOP of
|
|
|
|
"auth" ->
|
|
|
|
A2Prefix ++ ":" ++ DigestURI;
|
|
|
|
_ ->
|
|
|
|
A2Prefix ++ ":" ++ DigestURI ++
|
|
|
|
":00000000000000000000000000000000"
|
|
|
|
end,
|
2003-03-12 20:48:05 +01:00
|
|
|
T = hex(binary_to_list(crypto:md5(A1))) ++ ":" ++ Nonce ++ ":" ++
|
|
|
|
NC ++ ":" ++ CNonce ++ ":" ++ QOP ++ ":" ++
|
|
|
|
hex(binary_to_list(crypto:md5(A2))),
|
|
|
|
hex(binary_to_list(crypto:md5(T))).
|
|
|
|
|
|
|
|
|
2010-12-01 20:24:55 +01:00
|
|
|
sanitize(V) ->
|
|
|
|
L = from_utf8(V),
|
|
|
|
case lists:all(fun is_latin1/1, L) of
|
|
|
|
true -> L;
|
|
|
|
false -> V
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%% copied from xmerl_ucs:from_utf8/1 and xmerl_ucs:is_latin1/1 , to not
|
|
|
|
%%%% require xmerl as a dependency only for this.
|
|
|
|
|
|
|
|
from_utf8(Bin) when is_binary(Bin) -> from_utf8(binary_to_list(Bin));
|
|
|
|
from_utf8(List) ->
|
|
|
|
case expand_utf8(List) of
|
|
|
|
{Result,0} -> Result;
|
|
|
|
{_Res,_NumBadChar} ->
|
|
|
|
exit({ucs,{bad_utf8_character_code}})
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%% expand_utf8([Byte]) -> {[UnicodeChar],NumberOfBadBytes}
|
|
|
|
%% Expand UTF8 byte sequences to ISO 10646/Unicode
|
|
|
|
%% charactes. Any illegal bytes are removed and the number of
|
|
|
|
%% bad bytes are returned.
|
|
|
|
%%
|
|
|
|
%% Reference:
|
|
|
|
%% RFC 3629: "UTF-8, a transformation format of ISO 10646".
|
|
|
|
|
|
|
|
expand_utf8(Str) ->
|
|
|
|
expand_utf8_1(Str, [], 0).
|
|
|
|
|
|
|
|
expand_utf8_1([C|Cs], Acc, Bad) when C < 16#80 ->
|
|
|
|
%% Plain Ascii character.
|
|
|
|
expand_utf8_1(Cs, [C|Acc], Bad);
|
|
|
|
expand_utf8_1([C1,C2|Cs], Acc, Bad) when C1 band 16#E0 =:= 16#C0,
|
|
|
|
C2 band 16#C0 =:= 16#80 ->
|
|
|
|
case ((C1 band 16#1F) bsl 6) bor (C2 band 16#3F) of
|
|
|
|
C when 16#80 =< C ->
|
|
|
|
expand_utf8_1(Cs, [C|Acc], Bad);
|
|
|
|
_ ->
|
|
|
|
%% Bad range.
|
|
|
|
expand_utf8_1(Cs, Acc, Bad+1)
|
|
|
|
end;
|
|
|
|
expand_utf8_1([C1,C2,C3|Cs], Acc, Bad) when C1 band 16#F0 =:= 16#E0,
|
|
|
|
C2 band 16#C0 =:= 16#80,
|
|
|
|
C3 band 16#C0 =:= 16#80 ->
|
|
|
|
case ((((C1 band 16#0F) bsl 6) bor (C2 band 16#3F)) bsl 6) bor
|
|
|
|
(C3 band 16#3F) of
|
|
|
|
C when 16#800 =< C ->
|
|
|
|
expand_utf8_1(Cs, [C|Acc], Bad);
|
|
|
|
_ ->
|
|
|
|
%% Bad range.
|
|
|
|
expand_utf8_1(Cs, Acc, Bad+1)
|
|
|
|
end;
|
|
|
|
expand_utf8_1([C1,C2,C3,C4|Cs], Acc, Bad) when C1 band 16#F8 =:= 16#F0,
|
|
|
|
C2 band 16#C0 =:= 16#80,
|
|
|
|
C3 band 16#C0 =:= 16#80,
|
|
|
|
C4 band 16#C0 =:= 16#80 ->
|
|
|
|
case ((((((C1 band 16#0F) bsl 6) bor (C2 band 16#3F)) bsl 6) bor
|
|
|
|
(C3 band 16#3F)) bsl 6) bor (C4 band 16#3F) of
|
|
|
|
C when 16#10000 =< C ->
|
|
|
|
expand_utf8_1(Cs, [C|Acc], Bad);
|
|
|
|
_ ->
|
|
|
|
%% Bad range.
|
|
|
|
expand_utf8_1(Cs, Acc, Bad+1)
|
|
|
|
end;
|
|
|
|
expand_utf8_1([_|Cs], Acc, Bad) ->
|
|
|
|
%% Ignore bad character.
|
|
|
|
expand_utf8_1(Cs, Acc, Bad+1);
|
|
|
|
expand_utf8_1([], Acc, Bad) -> {lists:reverse(Acc),Bad}.
|
|
|
|
|
|
|
|
|
|
|
|
%%% Test for legitimate Latin-1 code
|
|
|
|
is_latin1(Ch) when is_integer(Ch), Ch >= 0, Ch =< 255 -> true;
|
|
|
|
is_latin1(_) -> false.
|
2003-03-12 20:48:05 +01:00
|
|
|
|