Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add match_fun clause to deal with ip addresses in TLS handshake #418

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/mint/core/transport/ssl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,20 @@ defmodule Mint.Core.Transport.SSL do
end
end

# Workaround for a bug that was fixed in OTP 27:
# Before OTP 27 when connecting to an IP address and the server offers a
# certificate with its IP address in the "subject alternate names" extension,
# the TLS handshake fails with a `{:bad_cert, :hostname_check_failed}`.
# This clause can be removed once we depend on OTP 27+.
defp match_fun({:dns_id, hostname}, {:iPAddress, ip}) do
with {:ok, ip_tuple} <- :inet.parse_address(hostname),
^ip <- Tuple.to_list(ip_tuple) do
true
else
_ -> :default
end
end

defp match_fun(_reference, _presented), do: :default

defp domain_without_host([]), do: []
Expand Down
4 changes: 4 additions & 0 deletions test/mint/core/transport/ssl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ defmodule Mint.Core.Transport.SSLTest do
refute :mint_shims.pkix_verify_hostname(cert, ip: {1, 2, 3, 4})
refute :mint_shims.pkix_verify_hostname(cert, ip: {10, 11, 12, 13})
end

test "custom match fun for IP addresses as hostname", %{cert: cert} do
assert {:valid, _} = SSL.verify_fun(cert, :valid_peer, dns_id: ~c"10.67.16.75")
end
end

# Certificate chain rooted in an expired root CA, and CA store containing
Expand Down
Loading