Repository: mitmproxy/mitmproxy

Issue:
An error occurred when trying to open a punycode domain
#### Problem Description

When trying to open a punycode domain, for example https://xn--80afnfom.xn--80ahmohdapg.xn--80asehdb/login, an error occurs in mitmproxy

mitmproxy log

```
[13:35:19.966][192.168.20.31:53287] client connect
[13:35:20.032][192.168.20.31:53287] server connect мойгаз.смородина.онлайн:443 (194.226.55.22:443)
[13:35:20.074] Addon error: DNSName values should be passed as an A-label string. This means unicode characters should be encoded via a library like idna.
Traceback (most recent call last):
  File "mitmproxy\certs.py", line 271, in dummy_cert
  File "ipaddress.py", line 54, in ip_address
ValueError: 'мойгаз.смородина.онлайн' does not appear to be an IPv4 or IPv6 address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "cryptography\x509\general_name.py", line 84, in __init__
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mitmproxy\addons\tlsconfig.py", line 177, in tls_start_client
  File "mitmproxy\addons\tlsconfig.py", line 516, in get_cert
  File "mitmproxy\certs.py", line 526, in get_cert
  File "mitmproxy\certs.py", line 273, in dummy_cert
  File "cryptography\x509\general_name.py", line 86, in __init__
ValueError: DNSName values should be passed as an A-label string. This means unicode characters should be encoded via a library like idna.
[13:35:20.078][192.168.20.31:53287] No TLS context was provided, failing connection.
[13:35:20.079][192.168.20.31:53287] client disconnect
[13:35:20.079][192.168.20.31:53287] server disconnect мойгаз.смородина.онлайн:443 (194.226.55.22:443)
```

#### Steps to reproduce the behavior:

1.  Open in browser https://xn--80afnfom.xn--80ahmohdapg.xn--80asehdb/login
2. Result 
![image](https://github.com/mitmproxy/mitmproxy/assets/4923679/9bf298e3-bcb2-44d9-8b22-aefe123a9be8)
 

#### System 

```
Mitmproxy: 10.0.0 binary
Python:    3.11.4
OpenSSL:   OpenSSL 3.0.7 1 Nov 2022
Platform:  Windows-10-10.0.19045-SP0
```


Discussion:

Resolving patch:
diff --git a/mitmproxy/certs.py b/mitmproxy/certs.py
--- a/mitmproxy/certs.py
+++ b/mitmproxy/certs.py
@@ -270,6 +270,7 @@ def dummy_cert(
         try:
             ip = ipaddress.ip_address(x)
         except ValueError:
+            x = x.encode("idna").decode()
             ss.append(x509.DNSName(x))
         else:
             ss.append(x509.IPAddress(ip))


Test patch:
diff --git a/test/mitmproxy/test_certs.py b/test/mitmproxy/test_certs.py
--- a/test/mitmproxy/test_certs.py
+++ b/test/mitmproxy/test_certs.py
@@ -141,11 +141,17 @@ def test_with_ca(self, tstore):
             tstore.default_privatekey,
             tstore.default_ca._cert,
             "foo.com",
-            ["one.com", "two.com", "*.three.com", "127.0.0.1"],
+            ["one.com", "two.com", "*.three.com", "127.0.0.1", "bücher.example"],
             "Foo Ltd.",
         )
         assert r.cn == "foo.com"
-        assert r.altnames == ["one.com", "two.com", "*.three.com", "127.0.0.1"]
+        assert r.altnames == [
+            "one.com",
+            "two.com",
+            "*.three.com",
+            "xn--bcher-kva.example",
+            "127.0.0.1",
+        ]
         assert r.organization == "Foo Ltd."
 
         r = certs.dummy_cert(