Skip to content

Commit

Permalink
Remove dynamic wrapper on rand_bytes/1 function
Browse files Browse the repository at this point in the history
Because crypto:rand_bytes/1 is deprecated since releases 19, we had added a
wrapper function in yaws_dynopts module to use crypto:strong_rand_bytes/1 when
possible. This was mandatory for R14 and R15 releases. But, since we have
removed the support for these releases, the wrapper function is useless.

Now, use use crypto:strong_rand_bytes/1 directly.

Ref #292
  • Loading branch information
capflam committed Nov 18, 2016
1 parent 410d81a commit 6327273
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 49 deletions.
2 changes: 1 addition & 1 deletion applications/chat/src/chat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ chat_server(Users0) ->
end,
chat_server(Users);
{new_session, User, From} ->
Cookie = integer_to_list(bin2int(yaws_dynopts:rand_bytes(16))),
Cookie = integer_to_list(bin2int(crypto:strong_rand_bytes(16))),
Session = #user{cookie=Cookie, user=User, color=pick_color()},
From ! {session_manager, Cookie, Session},
chat_server([Session|Users]);
Expand Down
4 changes: 2 additions & 2 deletions applications/mail/src/mail.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ session_manager(C0, LastGC0, Cfg) ->
end,
session_manager(C, LastGC, Cfg);
{new_session, Session, From} ->
Cookie = integer_to_list(bin2int(yaws_dynopts:rand_bytes(16))),
Cookie = integer_to_list(bin2int(crypto:strong_rand_bytes(16))),
From ! {session_manager, Cookie},
session_manager([{Cookie, Session#session{cookie=Cookie},
yaws:get_time_tuple()}|C], LastGC, Cfg);
Expand Down Expand Up @@ -1948,7 +1948,7 @@ dat2str_boundary([Y1,Y2, Mo, D, H, M, S | _Diff]) ->
lists:flatten(
io_lib:format("~s_~2.2.0w_~s_~w_~2.2.0w:~2.2.0w:~2.2.0w_~w",
[weekday(Y1,Y2,Mo,D), D, int_to_mt(Mo),
y(Y1,Y2),H,M,S,bin2int(yaws_dynopts:rand_bytes(4))])).
y(Y1,Y2),H,M,S,bin2int(crypto:strong_rand_bytes(4))])).

bin2int(Bin) ->
lists:foldl(fun(N, Acc) -> Acc * 256 + N end, 0, binary_to_list(Bin)).
Expand Down
2 changes: 1 addition & 1 deletion applications/mail/src/smtp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ dat2str_boundary({{Y, Mo, D}, {H, M, S}}) ->
lists:flatten(
io_lib:format("~s_~2.2.0w_~s_~w_~2.2.0w:~2.2.0w:~2.2.0w_~w",
[weekday(Y,Mo,D), D, int_to_mt(Mo),
Y,H,M,S,bin2int(yaws_dynopts:rand_bytes(4))])).
Y,H,M,S,bin2int(crypto:strong_rand_bytes(4))])).

bin2int(Bin) ->
lists:foldl(fun(N, Acc) -> Acc * 256 + N end, 0, binary_to_list(Bin)).
Expand Down
4 changes: 2 additions & 2 deletions src/yaws_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ parse_yaws_auth_file([{file, File}|T], Auth0) ->

parse_yaws_auth_file([{User, Password}|T], Auth0)
when is_list(User), is_list(Password) ->
Salt = yaws_dynopts:rand_bytes(32),
Salt = crypto:strong_rand_bytes(32),
Hash = crypto:hash(sha256, [Salt, Password]),
Users = case lists:member({User, sha256, Salt, Hash}, Auth0#auth.users) of
true -> Auth0#auth.users;
Expand Down Expand Up @@ -3356,7 +3356,7 @@ parse_auth_user(User, Lno) ->
{error, ?F("Invalid user at line ~w", [Lno])}
end;
_ ->
Salt = yaws_dynopts:rand_bytes(32),
Salt = crypto:strong_rand_bytes(32),
{Name, sha256, Salt, crypto:hash(sha256, [Salt, Passwd])}
end
catch
Expand Down
2 changes: 1 addition & 1 deletion src/yaws_ctl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ auth([User, Algo, Passwd]) ->
Algo == sha224 orelse Algo == sha256 orelse
Algo == sha384 orelse Algo == sha512 orelse
Algo == ripemd160 ->
Salt = yaws_dynopts:rand_bytes(32),
Salt = crypto:strong_rand_bytes(32),
B64Salt = base64:encode(Salt),
Hash = crypto:hash(Algo, [Salt, atom_to_list(Passwd)]),
B64Hash = base64:encode(Hash),
Expand Down
27 changes: 0 additions & 27 deletions src/yaws_dynopts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
have_ssl_sni/0,
have_ssl_log_alert/0,
have_erlang_sendfile/0,
have_crypto_strong_rand_bytes/0,
have_erlang_now/0,
have_rand/0,

rand_bytes/1,
unique_triple/0,
get_time_tuple/0,
now_secs/0,
Expand Down Expand Up @@ -47,10 +45,6 @@ have_ssl_log_alert() ->
have_erlang_sendfile() ->
is_greater_or_equal(erlang:system_info(version), "6.0").

%% crypto:rand_bytes/1 is deprecated since releases 19 (ERTS >= 8.0)
have_crypto_strong_rand_bytes() ->
lists:member({strong_rand_bytes, 1}, crypto:module_info(exports)).

%% erlang:now/0 is deprecated since releases 18 (ERTS >= 7.0)
have_erlang_now() ->
is_less(erlang:system_info(version), "7.0").
Expand All @@ -59,12 +53,6 @@ have_erlang_now() ->
have_rand() ->
(code:which(rand) /= non_existing).

rand_bytes(N) ->
case have_crypto_strong_rand_bytes() of
true -> crypto:strong_rand_bytes(N);
false -> (fun crypto:rand_bytes/1)(N)
end.

unique_triple() ->
case have_erlang_now() of
true ->
Expand Down Expand Up @@ -200,11 +188,6 @@ compile_options() ->
{d, 'HAVE_SSL_LOG_ALERT', have_ssl_log_alert()},
{d, 'HAVE_ERLANG_SENDFILE', have_erlang_sendfile()}
]
++
case have_crypto_strong_rand_bytes() of
true -> [{d, 'HAVE_CRYPTO_STRONG_RAND_BYTES'}];
false -> []
end
++
case have_erlang_now() of
true -> [{d, 'HAVE_ERLANG_NOW'}];
Expand All @@ -230,11 +213,9 @@ source() ->
" have_ssl_sni/0,",
" have_ssl_log_alert/0,",
" have_erlang_sendfile/0,",
" have_crypto_strong_rand_bytes/0,",
" have_erlang_now/0,",
" have_rand/0,"
"",
" rand_bytes/1,",
" unique_triple/0,",
" get_time_tuple/0,",
" now_secs/0,",
Expand All @@ -255,14 +236,6 @@ source() ->
"have_ssl_log_alert() -> ?HAVE_SSL_LOG_ALERT.",
"have_erlang_sendfile() -> ?HAVE_ERLANG_SENDFILE.",
"",
"-ifdef(HAVE_CRYPTO_STRONG_RAND_BYTES).",
"have_crypto_strong_rand_bytes() -> true.",
"rand_bytes(N) -> crypto:strong_rand_bytes(N).",
"-else.",
"have_crypto_strong_rand_bytes() -> false.",
"rand_bytes(N) -> crypto:rand_bytes(N).",
"-endif.",
""
"-ifdef(HAVE_ERLANG_NOW).",
"have_erlang_now() -> true.",
"unique_triple() ->",
Expand Down
2 changes: 1 addition & 1 deletion src/yaws_ls.erl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ out(A) ->


generate_random_fn() ->
Bytes = try yaws_dynopts:rand_bytes(64) of
Bytes = try crypto:strong_rand_bytes(64) of
B when is_bitstring(B) ->
B
catch _:_ ->
Expand Down
2 changes: 1 addition & 1 deletion src/yaws_session_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ handle_call({new_session, Opaque, undefined, Cleanup, Cookie}, From, State) ->
handle_call({new_session, Opaque, ?TTL, Cleanup, Cookie}, From, State);

handle_call({new_session, Opaque, TTL, Cleanup, undefined}, From, State) ->
N = bin2int(yaws_dynopts:rand_bytes(16)),
N = bin2int(crypto:strong_rand_bytes(16)),
Cookie = atom_to_list(node()) ++ [$-|integer_to_list(N)],
handle_call({new_session, Opaque, TTL, Cleanup, Cookie}, From, State);

Expand Down
11 changes: 0 additions & 11 deletions testsuite/dynopts_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ default_dynopts(Config) ->
?assertEqual(ok, check_ssl_sni(Config)),
?assertEqual(ok, check_ssl_log_alert(Config)),
?assertEqual(ok, check_erlang_sendfile(Config)),
?assertEqual(ok, check_crypto_strong_rand_bytes(Config)),
?assertEqual(ok, check_erlang_now(Config)),
?assertEqual(ok, check_rand(Config)),
ok.
Expand All @@ -56,7 +55,6 @@ generated_dynopts(_Config) ->
SSLSni = yaws_dynopts:have_ssl_sni(),
SSLLogAlert = yaws_dynopts:have_ssl_log_alert(),
ErlSendfile = yaws_dynopts:have_erlang_sendfile(),
CryptoRnd = yaws_dynopts:have_crypto_strong_rand_bytes(),
ErlNow = yaws_dynopts:have_erlang_now(),
Rand = yaws_dynopts:have_rand(),

Expand All @@ -69,7 +67,6 @@ generated_dynopts(_Config) ->
?assertEqual(SSLSni, yaws_dynopts:have_ssl_sni()),
?assertEqual(SSLLogAlert, yaws_dynopts:have_ssl_log_alert()),
?assertEqual(ErlSendfile, yaws_dynopts:have_erlang_sendfile()),
?assertEqual(CryptoRnd, yaws_dynopts:have_crypto_strong_rand_bytes()),
?assertEqual(ErlNow, yaws_dynopts:have_erlang_now()),
?assertEqual(Rand, yaws_dynopts:have_rand()),
ok.
Expand Down Expand Up @@ -137,14 +134,6 @@ check_erlang_sendfile(_Config) ->
end,
ok.

check_crypto_strong_rand_bytes(_Config) ->
Funs = crypto:module_info(exports),
case yaws_dynopts:have_crypto_strong_rand_bytes() of
true -> true = lists:member({strong_rand_bytes, 1}, Funs);
false -> false = lists:member({strong_rand_bytes, 1}, Funs)
end,
ok.

check_erlang_now(_Config) ->
Funs = erlang:module_info(exports),
case yaws_dynopts:have_erlang_now() of
Expand Down
4 changes: 2 additions & 2 deletions testsuite/websockets_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ too_big_frame(Config) ->
{ok, Sock} = open("localhost", testsuite:get_yaws_port(1, Config)),
?assertMatch({ok, {101, _}}, wsopen(Sock, Key, WSPath, "http://localhost", 13)),

Payload1 = yaws_dynopts:rand_bytes(16*1024*1024),
Payload1 = crypto:strong_rand_bytes(16*1024*1024),
SndFrame1 = #frame{opcode=?WS_OPCODE_BINARY, payload=Payload1},
?assertEqual(ok, send_frame(Sock, SndFrame1, all)),
{ok, RcvFrame} = read_frame(Sock),
Expand Down Expand Up @@ -1507,7 +1507,7 @@ too_big_message(Config) ->
{ok, Sock} = open("localhost", testsuite:get_yaws_port(1, Config)),
?assertMatch({ok, {101, _}}, wsopen(Sock, Key, WSPath, "http://localhost", 13)),

Payload1 = yaws_dynopts:rand_bytes(16*1024*1024),
Payload1 = crypto:strong_rand_bytes(16*1024*1024),
<<Frag1:(4*1024)/binary, Frag2:(4*1024)/binary,
Frag3:(4*1024)/binary, Frag4/binary>> = Payload1,
SndFrame1 = #frame{fin=false, opcode=?WS_OPCODE_BINARY, payload=Frag1},
Expand Down

0 comments on commit 6327273

Please sign in to comment.