Skip to content

Commit 321c830

Browse files
authored
Add match_fun clause to deal with IP addresses in TLS handshake (#418)
1 parent 7bb9ee7 commit 321c830

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/mint/core/transport/ssl.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,20 @@ defmodule Mint.Core.Transport.SSL do
509509
end
510510
end
511511

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

514528
defp domain_without_host([]), do: []

test/mint/core/transport/ssl_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ defmodule Mint.Core.Transport.SSLTest do
148148
refute :mint_shims.pkix_verify_hostname(cert, ip: {1, 2, 3, 4})
149149
refute :mint_shims.pkix_verify_hostname(cert, ip: {10, 11, 12, 13})
150150
end
151+
152+
test "custom match fun for IP addresses as hostname", %{cert: cert} do
153+
assert {:valid, _} = SSL.verify_fun(cert, :valid_peer, dns_id: ~c"10.67.16.75")
154+
end
151155
end
152156

153157
# Certificate chain rooted in an expired root CA, and CA store containing

0 commit comments

Comments
 (0)