-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98bc2d6
commit bab9e06
Showing
6 changed files
with
30 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,8 @@ | |
-author('[email protected]'). | ||
|
||
-record(?MODULE, { | ||
transport :: fast_tls | gen_tcp, | ||
socket :: fast_tls:tls_socket() | gen_tcp:socket() | ||
transport :: just_tls | gen_tcp, | ||
socket :: just_tls:tls_socket() | gen_tcp:socket() | ||
}). | ||
|
||
-type t() :: #?MODULE{}. | ||
|
@@ -38,9 +38,9 @@ wrap(Socket, ConnOpts) -> | |
-spec wrap(gen_tcp:socket(), #{tls := mongoose_tls:options()}, ExtraOpts :: map()) -> | ||
{ok, t()} | {error, any()}. | ||
wrap(Socket, #{tls := Opts}, ExtraOpts) -> | ||
PreparedOpts = mongoose_tls:prepare_options(fast_tls, maps:merge(Opts, ExtraOpts)), | ||
case fast_tls:tcp_to_tls(Socket, PreparedOpts) of | ||
{ok, TLSSocket} -> {ok, #?MODULE{transport = fast_tls, socket = TLSSocket}}; | ||
PreparedOpts = maps:merge(Opts, ExtraOpts), | ||
case just_tls:tcp_to_tls(Socket, PreparedOpts) of | ||
{ok, TLSSocket} -> {ok, #?MODULE{transport = just_tls, socket = TLSSocket}}; | ||
Error -> Error | ||
end; | ||
wrap(Socket, #{}, _ExtraOpts) -> | ||
|
@@ -49,32 +49,32 @@ wrap(Socket, #{}, _ExtraOpts) -> | |
-spec setopts(t(), Opts :: proplists:proplist()) -> ok | {error, term()}. | ||
setopts(#?MODULE{transport = gen_tcp, socket = Socket}, Opts) -> | ||
inet:setopts(Socket, Opts); | ||
setopts(#?MODULE{transport = fast_tls, socket = Socket}, Opts) -> | ||
fast_tls:setopts(Socket, Opts). | ||
setopts(#?MODULE{transport = just_tls, socket = Socket}, Opts) -> | ||
just_tls:setopts(Socket, Opts). | ||
|
||
-spec recv_data(t(), Data :: binary()) -> {ok, binary()} | {error, any()}. | ||
recv_data(#?MODULE{transport = gen_tcp}, Data) -> | ||
{ok, Data}; | ||
recv_data(#?MODULE{transport = fast_tls, socket = Socket}, Data) -> | ||
fast_tls:recv_data(Socket, Data). | ||
recv_data(#?MODULE{transport = just_tls, socket = Socket}, Data) -> | ||
just_tls:recv_data(Socket, Data). | ||
|
||
-spec close(t()) -> ok | {error, any()}. | ||
close(#?MODULE{transport = gen_tcp, socket = Socket}) -> | ||
gen_tcp:close(Socket); | ||
close(#?MODULE{transport = fast_tls, socket = Socket}) -> | ||
fast_tls:close(Socket). | ||
close(#?MODULE{transport = just_tls, socket = Socket}) -> | ||
just_tls:close(Socket). | ||
|
||
-spec send(t(), Data :: binary()) -> ok | {error, any()}. | ||
send(#?MODULE{transport = gen_tcp, socket = Socket}, Data) -> | ||
gen_tcp:send(Socket, Data); | ||
send(#?MODULE{transport = fast_tls, socket = Socket}, Data) -> | ||
fast_tls:send(Socket, Data). | ||
send(#?MODULE{transport = just_tls, socket = Socket}, Data) -> | ||
just_tls:send(Socket, Data). | ||
|
||
-spec peername(t()) -> {inet:ip_address(), inet:port_number()} | unknown. | ||
peername(#?MODULE{transport = gen_tcp, socket = Socket}) -> | ||
normalize_peername(inet:peername(Socket)); | ||
peername(#?MODULE{transport = fast_tls, socket = Socket}) -> | ||
normalize_peername(fast_tls:peername(Socket)). | ||
peername(#?MODULE{transport = just_tls, socket = Socket}) -> | ||
normalize_peername(just_tls:peername(Socket)). | ||
|
||
-spec normalize_peername({ok, {inet:ip_address(), inet:port_number()}} | any()) -> | ||
{inet:ip_address(), inet:port_number()} | unknown. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,6 @@ | |
%%% @copyright (C) 1999-2018, Erlang Solutions Ltd | ||
%%% @author Denys Gonchar <[email protected]> | ||
%%% @doc this module provides general TLS interface for MongooseIM. | ||
%%% | ||
%%% by default tls_module is set to fast_tls, alternatively it can be any | ||
%%% module that implements mongoose_tls behaviour | ||
%%% @end | ||
%%%============================================================================= | ||
-module(mongoose_tls). | ||
-copyright("2018, Erlang Solutions Ltd."). | ||
|
@@ -30,13 +26,13 @@ | |
|
||
-ignore_xref([get_sockmod/1]). | ||
|
||
-type tls_socket() :: fast_tls:tls_socket() | just_tls:tls_socket(). | ||
-type tls_socket() :: just_tls:tls_socket(). | ||
-type cert() :: {ok, Cert::any()} | {bad_cert, bitstring()} | no_peer_cert. | ||
|
||
%% Options used for client-side and server-side TLS connections. | ||
%% All modules implementing this behaviour have to support the mandatory 'verify_mode' option. | ||
%% Other options should be supported if the implementing module supports it. | ||
-type options() :: #{module => module(), % fast_tls by default | ||
-type options() :: #{module => module(), | ||
connect => boolean(), % set to 'true' for a client-side call to tcp_to_tls/2 | ||
verify_mode := peer | selfsigned_peer | none, | ||
mode => tls | starttls | starttls_required, % only ejabberd_s2s_out doesn't use it (yet) | ||
|
@@ -50,10 +46,8 @@ | |
keyfile => string(), | ||
password => string(), | ||
versions => [atom()], | ||
server_name_indication => sni_options(), % client-only | ||
|
||
% only for fast_tls | ||
protocol_options => [string()]}. | ||
server_name_indication => sni_options() % client-only | ||
}. | ||
|
||
-type sni_options() :: #{enabled := boolean, | ||
protocol := default | https, | ||
|
@@ -105,7 +99,7 @@ | |
|
||
-spec tcp_to_tls(inet:socket(), options()) -> {ok, socket()} | {error, any()}. | ||
tcp_to_tls(TCPSocket, Opts) -> | ||
Module = maps:get(module, Opts, fast_tls), | ||
Module = maps:get(module, Opts, just_tls), | ||
PreparedOpts = prepare_options(Module, maps:remove(module, Opts)), | ||
case Module:tcp_to_tls(TCPSocket, PreparedOpts) of | ||
{ok, TLSSocket} -> | ||
|
@@ -119,23 +113,9 @@ tcp_to_tls(TCPSocket, Opts) -> | |
end. | ||
|
||
-spec prepare_options(module(), options()) -> any(). | ||
prepare_options(fast_tls, Opts) -> | ||
%% fast_tls is an external library and its API cannot use Opts directly | ||
lists:flatmap(fun({K, V}) -> fast_tls_opt(K, V) end, maps:to_list(Opts)); | ||
prepare_options(_Module, Opts) -> | ||
Opts. | ||
|
||
fast_tls_opt(connect, true) -> [connect]; | ||
fast_tls_opt(connect, false) -> []; | ||
fast_tls_opt(mode, _) -> []; | ||
fast_tls_opt(verify_mode, peer) -> []; | ||
fast_tls_opt(verify_mode, none) -> [verify_none]; | ||
fast_tls_opt(cacertfile, File) -> [{cafile, File}]; | ||
fast_tls_opt(dhfile, File) -> [{dhfile, File}]; | ||
fast_tls_opt(certfile, File) -> [{certfile, File}]; | ||
fast_tls_opt(ciphers, Ciphers) -> [{ciphers, Ciphers}]; | ||
fast_tls_opt(protocol_options, ProtoOpts) -> [{protocol_options, string:join(ProtoOpts, "|")}]. | ||
|
||
default_ciphers() -> | ||
"TLSv1.2:TLSv1.3". | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters