Skip to content

Commit 98656d0

Browse files
committed
ssh: refactor select_all function
1 parent 7cd7abb commit 98656d0

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

lib/ssh/src/ssh_transport.erl

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,14 +1145,9 @@ select_algorithm(Role, Client, Server,
11451145
Server#ssh_msg_kexinit.languages_client_to_server),
11461146
S_Lng = select(Client#ssh_msg_kexinit.languages_server_to_client,
11471147
Server#ssh_msg_kexinit.languages_server_to_client),
1148-
HKey = select_all(Client#ssh_msg_kexinit.server_host_key_algorithms,
1149-
Server#ssh_msg_kexinit.server_host_key_algorithms),
1150-
HK = case HKey of
1151-
[] -> undefined;
1152-
[HK0|_] -> HK0
1153-
end,
1154-
%% Fixme verify Kex against HKey list and algorithms
1155-
1148+
HKey = select(Client#ssh_msg_kexinit.server_host_key_algorithms,
1149+
Server#ssh_msg_kexinit.server_host_key_algorithms),
1150+
%% FIXME verify Kex against HKey list and algorithms (see RFC4253 sec 7.1)
11561151
Kex = select(Client#ssh_msg_kexinit.kex_algorithms,
11571152
Server#ssh_msg_kexinit.kex_algorithms),
11581153

@@ -1172,7 +1167,7 @@ select_algorithm(Role, Client, Server,
11721167
?GET_OPT(recv_ext_info,Opts),
11731168

11741169
{ok, #alg{kex = Kex,
1175-
hkey = HK,
1170+
hkey = HKey,
11761171
encrypt = Encrypt,
11771172
decrypt = Decrypt,
11781173
send_mac = SendMac,
@@ -1324,38 +1319,27 @@ alg_final(rcv, SSH0) ->
13241319
{ok,SSH3} = decompress_final(SSH2),
13251320
SSH3.
13261321

1327-
1328-
select_all(CL, SL) when length(CL) + length(SL) < ?MAX_NUM_ALGORITHMS ->
1329-
%% algorithms only used by client
1330-
%% NOTE: an algorithm occurring more than once in CL will still be present
1331-
%% in CLonly. This is not a problem for nice clients.
1332-
CLonly = CL -- SL,
1333-
1334-
%% algorithms used by client and server (client pref)
1335-
lists:foldr(fun(ALG, Acc) ->
1336-
try [list_to_existing_atom(ALG) | Acc]
1337-
catch
1338-
%% If an malicious client uses the same non-existing algorithm twice,
1339-
%% we will end up here
1340-
_:_ -> Acc
1341-
end
1342-
end, [], (CL -- CLonly));
1343-
1344-
select_all(CL, SL) ->
1345-
Error = lists:concat(["Received too many algorithms (",length(CL),"+",length(SL)," >= ",?MAX_NUM_ALGORITHMS,")."]),
1346-
?DISCONNECT(?SSH_DISCONNECT_PROTOCOL_ERROR,
1347-
Error).
1348-
1349-
13501322
select([], []) ->
13511323
none;
13521324
select(CL, SL) ->
1353-
C = case select_all(CL,SL) of
1354-
[] -> undefined;
1355-
[ALG|_] -> ALG
1356-
end,
1357-
C.
1358-
1325+
select_first(CL, SL).
1326+
1327+
select_first([ClientAlg | ClientRest], SL) ->
1328+
case lists:member(ClientAlg, SL) of
1329+
true ->
1330+
try list_to_existing_atom(ClientAlg) of
1331+
Alg when is_atom(Alg) ->
1332+
Alg
1333+
catch
1334+
error:badarg ->
1335+
select_first(ClientRest, SL)
1336+
end;
1337+
false ->
1338+
select_first(ClientRest, SL)
1339+
end;
1340+
select_first([], _) ->
1341+
undefined.
1342+
13591343
ssh_packet(#ssh_msg_kexinit{} = Msg, Ssh0) ->
13601344
BinMsg = ssh_message:encode(Msg),
13611345
Ssh = key_init(Ssh0#ssh.role, Ssh0, BinMsg),

0 commit comments

Comments
 (0)