Packages
locus
1.11.0-beta
2.3.15
2.3.14
2.3.13
2.3.12
2.3.11
2.3.10
2.3.9
2.3.8
2.3.7
2.3.6
2.3.5
2.3.4
2.3.3
retired
2.3.2
2.3.1
2.3.0
2.2.2
2.2.1
2.2.0
2.1.0
2.0.0
1.16.1
1.16.0
1.15.0
1.14.1
1.14.0
1.13.2
1.13.1
1.13.0
1.12.2
1.12.1
1.12.0
1.11.0
1.11.0-beta
1.10.2
1.10.1
1.10.0
1.9.0
1.9.0-beta
1.8.0
1.7.0
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.2
1.0.1
1.0.0
MMDB reader for geolocation and ASN lookup of IP addresses, supporting MaxMind GeoLite2/GeoIP2 and other providers
Current section
Files
Jump to
Current section
Files
src/locus_https_requests.erl
%% Copyright (c) 2017-2020 Guilherme Andrade
%%
%% Permission is hereby granted, free of charge, to any person obtaining a
%% copy of this software and associated documentation files (the "Software"),
%% to deal in the Software without restriction, including without limitation
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%% DEALINGS IN THE SOFTWARE.
%%
%% locus is an independent project and has not been authorized, sponsored,
%% or otherwise approved by MaxMind.
%% @private
-module(locus_https_requests).
-include_lib("public_key/include/OTP-PUB-KEY.hrl").
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export(
[ssl_opts_for_ca_authentication/1
]).
%% ------------------------------------------------------------------
%% Internal Function Exports
%% ------------------------------------------------------------------
-export(
[partial_chain/1
]).
%% ------------------------------------------------------------------
%% Record and Type Definitions
%% ------------------------------------------------------------------
-ifdef(SSL_OLD_CLIENT_OPTIONS).
-type ssl_option() :: ssl:connect_option().
-else.
-type ssl_option() :: ssl:tls_client_option().
-endif.
-export_type([ssl_option/0]).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
-spec ssl_opts_for_ca_authentication(string()) -> [ssl_option(), ...].
ssl_opts_for_ca_authentication(URL) ->
case http_uri:parse(URL) of
{ok, {_Scheme, _UserInfo, Host, _Port, _Path, _Query}} ->
ssl_opts_for_ca_authentication_(Host);
{ok, {_Scheme, _UserInfo, Host, _Port, _Path, _Query, _Fragment}} ->
ssl_opts_for_ca_authentication_(Host)
end.
%% ------------------------------------------------------------------
%% Exported Internal Function Definitions
%% ------------------------------------------------------------------
-spec partial_chain([public_key:der_encoded()]) -> {trusted_ca, binary()} | unknown_ca.
partial_chain(Certs) ->
% Taken from frisky, which took it from hackney,
% licensed under Apache 2 license,
% which in turn took it from rebar3, licensed under BSD.
Certs1 = lists:reverse([{Cert, public_key:pkix_decode_cert(Cert, otp)} ||
Cert <- Certs]),
CACerts = certifi:cacerts(),
CACerts1 = [public_key:pkix_decode_cert(Cert, otp) || Cert <- CACerts],
case lists_anymap(
fun ({_, Cert}) ->
check_cert(CACerts1, Cert)
end,
Certs1)
of
{true, Trusted} ->
{trusted_ca, element(1, Trusted)};
false ->
unknown_ca
end.
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
ssl_opts_for_ca_authentication_(Host) when is_list(Host) ->
% Taken from frisky, which took it from hackney,
% licensed under Apache 2 license.
CACerts = certifi:cacerts(),
VerifyFun = {fun ssl_verify_hostname:verify_fun/3, [{check_hostname, Host}]},
[{verify, verify_peer},
{depth, 99},
{cacerts, CACerts},
{partial_chain, fun ?MODULE:partial_chain/1},
{verify_fun, VerifyFun}].
check_cert(CACerts, Cert) ->
% Taken from frisky, which took it from hackney,
% licensed under Apache 2 license.
CertPKI = extract_public_key_info(Cert),
lists:any(
fun(CACert) ->
extract_public_key_info(CACert) =:= CertPKI
end, CACerts).
extract_public_key_info(Cert) ->
((Cert#'OTPCertificate'.tbsCertificate)#'OTPTBSCertificate'.subjectPublicKeyInfo).
lists_anymap(Fun, [H|T]) ->
case Fun(H) of
true -> {true, H};
false -> lists_anymap(Fun, T)
end;
lists_anymap(_Fun, []) ->
false.