{"thread":{"id":"66288","subject":"[PATCH 0/3] imap-send: future proofing and two correctness fixes","startedAt":"2026-09-07T21:15:57Z","lastAt":"2026-10-19T08:44:05Z","messageCount":23,"participants":["Beat Bolli","Junio C Hamano","brian m. carlson","Patrick Steinhardt","Aniket Gupta","Xiangfei Ding"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"552168","messageId":"20260907211210.2621693-1-dev+git@drbeat.li","threadId":"66288","inReplyTo":null,"subject":"[PATCH 0/3] imap-send: future proofing and two correctness fixes","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-09-07T21:12:07Z","receivedAt":"2026-09-07T21:15:57Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"Hi!\n\nPatch 1 future-proofs against a renamed ASN1_STRING function.\n\nPatch 2 fixes an incorrect assumption about NUL-termination of\nASN1_STRINGs.\n\nPatch 3 only checks the certificate subject common name if no DNS\nsubject alternative names are available, as defined by RFC 6125.\n\n\nBeat Bolli (3):\n  imap-send: prepare for OpenSSL 4.1\n  imap-send: don't expect an ASN1_STRING to be NUL-terminated\n  imap-send: only check the CN if no SAN DNS names are present\n\n imap-send.c | 33 ++++++++++++++++++++++++---------\n 1 file changed, 24 insertions(+), 9 deletions(-)\n\n-- \n2.53.0\n\n"},{"id":"552169","messageId":"20260907211210.2621693-4-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20260907211210.2621693-1-dev+git@drbeat.li","subject":"[PATCH 3/3] imap-send: only check the CN if no SAN DNS names are present","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-09-07T21:12:10Z","receivedAt":"2026-09-07T21:16:17Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"Checking the certificate subject's common name may only be done if the\nsubjectAltNames extension contains no DNS entries. If no SAN DNS name\nmatches, there's no match.\n\nPer RFC 6125 section 6.4.4[1]:\n\n    As noted, a client MUST NOT seek a match for a reference identifier\n    of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,\n    URI-ID, or any application-specific identifier types supported by the\n    client.\n\nThis change was inspired by a similar commit in the HAProxy project[2].\n\n[1]: https://datatracker.ietf.org/doc/html/rfc6125#section-6.4.4\n[2]: https://github.com/haproxy/haproxy/commit/75129aaacb7a7b172f4e5334db71d6c1c50a3dbf\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 13 +++++++++----\n 1 file changed, 9 insertions(+), 4 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex 9a807cdde8..66d3dbfaa5 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -256,11 +256,11 @@ static int verify_hostname(X509 *cert, const char *hostname)\n #endif\n \tconst X509_NAME_ENTRY *cname_entry;\n \tconst ASN1_STRING *cname;\n-\tint i, found;\n+\tint i, found, has_san_dns;\n \tSTACK_OF(GENERAL_NAME) *subj_alt_names;\n \n \t/* try the DNS subjectAltNames */\n-\tfound = 0;\n+\tfound = has_san_dns = 0;\n \tif ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {\n \t\tint num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);\n \t\tfor (i = 0; !found && i < num_subj_alt_names; i++) {\n@@ -268,13 +268,18 @@ static int verify_hostname(X509 *cert, const char *hostname)\n \t\t\tGENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);\n \t\t\tASN1_STRING *subj_alt_str = GENERAL_NAME_get0_value(subj_alt_name, &ntype);\n \n-\t\t\tif (ntype == GEN_DNS && host_matches(hostname, subj_alt_str))\n-\t\t\t\tfound = 1;\n+\t\t\tif (ntype == GEN_DNS) {\n+\t\t\t\thas_san_dns = 1;\n+\t\t\t\tif (host_matches(hostname, subj_alt_str))\n+\t\t\t\t\tfound = 1;\n+\t\t\t}\n \t\t}\n \t\tsk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);\n \t}\n \tif (found)\n \t\treturn 0;\n+\tif (has_san_dns)\n+\t\treturn error(\"none of the subjectAltNames matches hostname '%s'\", hostname);\n \n \t/* try the common name */\n \tif (!(subj = X509_get_subject_name(cert)))\n-- \n2.53.0\n\n"},{"id":"552170","messageId":"20260907211210.2621693-3-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20260907211210.2621693-1-dev+git@drbeat.li","subject":"[PATCH 2/3] imap-send: don't expect an ASN1_STRING to be NUL-terminated","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-09-07T21:12:09Z","receivedAt":"2026-09-07T21:23:04Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"As highlighted by a recent OpenSSL commit[1], ASN1_STRINGs were never\ndocumented to be terminated by a NUL byte, but our code treats the\npattern as such in the strcasecmp() call.\n\nMake a NUL-terminated copy to avoid Undefined Behavior.\n\n[1]: https://github.com/openssl/openssl/commit/4b581a4666c3e470a01a7323801b2ba8ccfa478c\n     (Add a migration entry for ASN1_STRINGs, 2026-08-06)\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 15 ++++++++++-----\n 1 file changed, 10 insertions(+), 5 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex 977d78005c..9a807cdde8 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -226,20 +226,25 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n \n static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n {\n-\tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n+\tint ret = 0;\n+\tsize_t len = ASN1_STRING_get_length(asn1_str);\n+\tchar *pattern = xmemdupz(ASN1_STRING_get0_data(asn1_str), len);\n \n \t/* embedded NUL characters may open a security hole */\n-\tif (memchr(pattern, '\\0', ASN1_STRING_get_length(asn1_str)))\n-\t    return 0;\n+\tif (memchr(pattern, '\\0', len))\n+\t    goto out;\n \n \tif (pattern[0] == '*' && pattern[1] == '.') {\n \t\tpattern += 2;\n \t\tif (!(host = strchr(host, '.')))\n-\t\t\treturn 0;\n+\t\t\tgoto out;\n \t\thost++;\n \t}\n \n-\treturn *host && *pattern && !strcasecmp(host, pattern);\n+\tret = *host && *pattern && !strcasecmp(host, pattern);\n+out:\n+\tfree(pattern);\n+\treturn ret;\n }\n \n static int verify_hostname(X509 *cert, const char *hostname)\n-- \n2.53.0\n\n"},{"id":"552171","messageId":"20260907211210.2621693-2-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20260907211210.2621693-1-dev+git@drbeat.li","subject":"[PATCH 1/3] imap-send: prepare for OpenSSL 4.1","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-09-07T21:12:08Z","receivedAt":"2026-09-07T21:23:10Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"OpenSSL master (to be v4.1 after the release) renamed the function\nASN1_STRING_length() to ASN1_STRING_get_length(). Map the new name to\nthe old one if we're compiling with a pre-4.1 version.\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 7 ++++++-\n 1 file changed, 6 insertions(+), 1 deletion(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex 0d16d02029..977d78005c 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -219,12 +219,17 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n \n #else\n \n+#if (OPENSSL_VERSION_NUMBER < 0x40100000L)\n+// map to the pre-4.1 name\n+#define ASN1_STRING_get_length(s) ASN1_STRING_length(s)\n+#endif\n+\n static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n {\n \tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n \n \t/* embedded NUL characters may open a security hole */\n-\tif (memchr(pattern, '\\0', ASN1_STRING_length(asn1_str)))\n+\tif (memchr(pattern, '\\0', ASN1_STRING_get_length(asn1_str)))\n \t    return 0;\n \n \tif (pattern[0] == '*' && pattern[1] == '.') {\n-- \n2.53.0\n\n"},{"id":"552174","messageId":"xmqqwlswzgmq.fsf@gitster.g","threadId":"66288","inReplyTo":"20260907211210.2621693-3-dev+git@drbeat.li","subject":"Re: [PATCH 2/3] imap-send: don't expect an ASN1_STRING to be NUL-terminated","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-08T01:17:01Z","receivedAt":"2026-09-08T01:17:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Beat Bolli <dev+git@drbeat.li> writes:\n\n> -\tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n> +\tint ret = 0;\n> +\tsize_t len = ASN1_STRING_get_length(asn1_str);\n> +\tchar *pattern = xmemdupz(ASN1_STRING_get0_data(asn1_str), len);\n>  \n>  \t/* embedded NUL characters may open a security hole */\n> -\tif (memchr(pattern, '\\0', ASN1_STRING_get_length(asn1_str)))\n> -\t    return 0;\n> +\tif (memchr(pattern, '\\0', len))\n> +\t    goto out;\n>  \n>  \tif (pattern[0] == '*' && pattern[1] == '.') {\n>  \t\tpattern += 2;\n>  \t\tif (!(host = strchr(host, '.')))\n> -\t\t\treturn 0;\n> +\t\t\tgoto out;\n>  \t\thost++;\n>  \t}\n>  \n> -\treturn *host && *pattern && !strcasecmp(host, pattern);\n> +\tret = *host && *pattern && !strcasecmp(host, pattern);\n> +out:\n> +\tfree(pattern);\n\nThere is a code path that increments the \"pattern\" variable by 2.\nRunning free() on it would not have a pleasant outcome.\n\nThe pattern we often employ in our codebase is to have a separate\nvariable \"char *pattern_to_free\" and have it used only for a call\nto free().\n\n\n> +\treturn ret;\n>  }\n>  \n>  static int verify_hostname(X509 *cert, const char *hostname)\n"},{"id":"552175","messageId":"ap9kv-ORyzzeUiqb@fruit.crustytoothpaste.net","threadId":"66288","inReplyTo":"20260907211210.2621693-4-dev+git@drbeat.li","subject":"Re: [PATCH 3/3] imap-send: only check the CN if no SAN DNS names are present","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-09-08T01:28:32Z","receivedAt":"2026-09-08T01:28:34Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-09-07 at 21:12:10, Beat Bolli wrote:\n> Checking the certificate subject's common name may only be done if the\n> subjectAltNames extension contains no DNS entries. If no SAN DNS name\n> matches, there's no match.\n> \n> Per RFC 6125 section 6.4.4[1]:\n> \n>     As noted, a client MUST NOT seek a match for a reference identifier\n>     of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,\n>     URI-ID, or any application-specific identifier types supported by the\n>     client.\n> \n> This change was inspired by a similar commit in the HAProxy project[2].\n\nTLS is not supposed to use the CN at all these days and Go's\nimplementation completely ignores it.  subjectAltName is supposed to be\nused in all cases.\n\n> diff --git a/imap-send.c b/imap-send.c\n> index 9a807cdde8..66d3dbfaa5 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -256,11 +256,11 @@ static int verify_hostname(X509 *cert, const char *hostname)\n>  #endif\n>  \tconst X509_NAME_ENTRY *cname_entry;\n>  \tconst ASN1_STRING *cname;\n> -\tint i, found;\n> +\tint i, found, has_san_dns;\n>  \tSTACK_OF(GENERAL_NAME) *subj_alt_names;\n>  \n>  \t/* try the DNS subjectAltNames */\n> -\tfound = 0;\n> +\tfound = has_san_dns = 0;\n>  \tif ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {\n>  \t\tint num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);\n>  \t\tfor (i = 0; !found && i < num_subj_alt_names; i++) {\n> @@ -268,13 +268,18 @@ static int verify_hostname(X509 *cert, const char *hostname)\n>  \t\t\tGENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);\n>  \t\t\tASN1_STRING *subj_alt_str = GENERAL_NAME_get0_value(subj_alt_name, &ntype);\n>  \n> -\t\t\tif (ntype == GEN_DNS && host_matches(hostname, subj_alt_str))\n> -\t\t\t\tfound = 1;\n> +\t\t\tif (ntype == GEN_DNS) {\n> +\t\t\t\thas_san_dns = 1;\n> +\t\t\t\tif (host_matches(hostname, subj_alt_str))\n> +\t\t\t\t\tfound = 1;\n> +\t\t\t}\n\nThis handles certificates with DNS names but not IP addresses.  So, for\ninstance, this match wouldn't work for the certificates for 1.1.1.1\n(assuming they had public IMAP service).\n\n>  \t\t}\n>  \t\tsk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);\n>  \t}\n>  \tif (found)\n>  \t\treturn 0;\n> +\tif (has_san_dns)\n> +\t\treturn error(\"none of the subjectAltNames matches hostname '%s'\", hostname);\n\nI know OpenSSL has built-in hostname verification that can be used as of\nOpenSSL 1.0.2[0].  Is there a reason we're still doing this by hand?\n\nRelying on OpenSSL's verification would mean that (a) we would not have\nto worry about getting verification wrong in a security-sensitive way\nand (b) OpenSSL would handle the policy and standards compliance\nfunctionality.\n\n[0] https://wiki.openssl.org/index.php/Hostname_validation\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\n"},{"id":"552182","messageId":"xmqqfqzkz8aq.fsf@gitster.g","threadId":"66288","inReplyTo":"20260907211210.2621693-2-dev+git@drbeat.li","subject":"Re: [PATCH 1/3] imap-send: prepare for OpenSSL 4.1","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-08T04:17:01Z","receivedAt":"2026-09-08T04:17:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Beat Bolli <dev+git@drbeat.li> writes:\n\n> OpenSSL master (to be v4.1 after the release) renamed the function\n> ASN1_STRING_length() to ASN1_STRING_get_length(). Map the new name to\n> the old one if we're compiling with a pre-4.1 version.\n>\n> Signed-off-by: Beat Bolli <dev+git@drbeat.li>\n> ---\n>  imap-send.c | 7 ++++++-\n>  1 file changed, 6 insertions(+), 1 deletion(-)\n>\n> diff --git a/imap-send.c b/imap-send.c\n> index 0d16d02029..977d78005c 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -219,12 +219,17 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n>  \n>  #else\n>  \n> +#if (OPENSSL_VERSION_NUMBER < 0x40100000L)\n> +// map to the pre-4.1 name\n\nStyle?\n\n> +#define ASN1_STRING_get_length(s) ASN1_STRING_length(s)\n> +#endif\n> +\n>  static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n>  {\n>  \tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n>  \n>  \t/* embedded NUL characters may open a security hole */\n> -\tif (memchr(pattern, '\\0', ASN1_STRING_length(asn1_str)))\n> +\tif (memchr(pattern, '\\0', ASN1_STRING_get_length(asn1_str)))\n>  \t    return 0;\n>  \n>  \tif (pattern[0] == '*' && pattern[1] == '.') {\n"},{"id":"552189","messageId":"ap_GvB8Lonkn0nEy@pks.im","threadId":"66288","inReplyTo":"20260907211210.2621693-2-dev+git@drbeat.li","subject":"Re: [PATCH 1/3] imap-send: prepare for OpenSSL 4.1","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-09-08T08:26:36Z","receivedAt":"2026-09-08T08:26:42Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Sep 07, 2026 at 11:12:08PM +0200, Beat Bolli wrote:\n> OpenSSL master (to be v4.1 after the release) renamed the function\n> ASN1_STRING_length() to ASN1_STRING_get_length(). Map the new name to\n> the old one if we're compiling with a pre-4.1 version.\n\nI can see [1] that the new functions indeed exist now. But it doesn't\nsay anything about the old functions, they still exist and don't seem to\nbe deprecated. So why do we even have to switch to the new function?\n\nPatrick\n\n[1]: https://docs.openssl.org/master/man3/ASN1_STRING_length/\n"},{"id":"552190","messageId":"ap_Gwx4g7t0vjsj2@pks.im","threadId":"66288","inReplyTo":"20260907211210.2621693-3-dev+git@drbeat.li","subject":"Re: [PATCH 2/3] imap-send: don't expect an ASN1_STRING to be NUL-terminated","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-09-08T08:26:43Z","receivedAt":"2026-09-08T08:26:47Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Sep 07, 2026 at 11:12:09PM +0200, Beat Bolli wrote:\n> diff --git a/imap-send.c b/imap-send.c\n> index 977d78005c..9a807cdde8 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -226,20 +226,25 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n>  \n>  static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n>  {\n> -\tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n> +\tint ret = 0;\n> +\tsize_t len = ASN1_STRING_get_length(asn1_str);\n> +\tchar *pattern = xmemdupz(ASN1_STRING_get0_data(asn1_str), len);\n>  \n>  \t/* embedded NUL characters may open a security hole */\n> -\tif (memchr(pattern, '\\0', ASN1_STRING_get_length(asn1_str)))\n> -\t    return 0;\n> +\tif (memchr(pattern, '\\0', len))\n> +\t    goto out;\n>  \n>  \tif (pattern[0] == '*' && pattern[1] == '.') {\n>  \t\tpattern += 2;\n>  \t\tif (!(host = strchr(host, '.')))\n> -\t\t\treturn 0;\n> +\t\t\tgoto out;\n>  \t\thost++;\n>  \t}\n>  \n> -\treturn *host && *pattern && !strcasecmp(host, pattern);\n> +\tret = *host && *pattern && !strcasecmp(host, pattern);\n> +out:\n> +\tfree(pattern);\n> +\treturn ret;\n>  }\n\nI don't quite see a reason why we even have to memdup the string. We\nalready use memchr, which is bounded by the length of the string. We do\nhave two other sites though:\n\n  - We use strchr, but that can be adapted to use memchr.\n\n  - Likewise, we use strcasecmp, but that can be adapted to use\n    strncasecmp.\n\nSo with that, all calls that inspect the string would be bounded by the\nlength of the encoded string, and that means we don't have to copy the\nstring first, do we?\n\nPatrick\n"},{"id":"552685","messageId":"872286d0-7786-46cc-b26f-16f6d487e608@drbeat.li","threadId":"66288","inReplyTo":"ap_GvB8Lonkn0nEy@pks.im","subject":"Re: [PATCH 1/3] imap-send: prepare for OpenSSL 4.1","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-09-14T09:10:36Z","receivedAt":"2026-09-14T09:23:40Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"Hi Patrick\n\nOn 08.09.2026 10:26, Patrick Steinhardt wrote:\n> On Mon, Sep 07, 2026 at 11:12:08PM +0200, Beat Bolli wrote:\n>> OpenSSL master (to be v4.1 after the release) renamed the function\n>> ASN1_STRING_length() to ASN1_STRING_get_length(). Map the new name to\n>> the old one if we're compiling with a pre-4.1 version.\n> \n> I can see [1] that the new functions indeed exist now. But it doesn't\n> say anything about the old functions, they still exist and don't seem to\n> be deprecated. So why do we even have to switch to the new function?\n> \n\nThe very page you give below contains this text:\n\n---- 8< ----\nThe following functions have been deprecated since OpenSSL 4.1, and can \nbe hidden entirely by defining OPENSSL_API_COMPAT with a suitable \nversion value, see openssl_user_macros(7):\n\nint ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);\nint ASN1_STRING_length(ASN1_STRING *x);\n---- 8< ----\n\nWe also don't define any compatibility macros for OpenSSL, and the build \nfailed because of the deprecation warning that was turned into an error \nbecause of DEVELOPER=1.\n\nSo I still think this patch is needed.\n\nBeat\n\n> [1]: https://docs.openssl.org/master/man3/ASN1_STRING_length/\n\n"},{"id":"553043","messageId":"arPG2yDxeVTPwpTg@pks.im","threadId":"66288","inReplyTo":"872286d0-7786-46cc-b26f-16f6d487e608@drbeat.li","subject":"Re: [PATCH 1/3] imap-send: prepare for OpenSSL 4.1","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-09-23T12:32:27Z","receivedAt":"2026-09-23T12:32:38Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Sep 14, 2026 at 11:10:36AM +0200, Beat Bolli wrote:\n> Hi Patrick\n> \n> On 08.09.2026 10:26, Patrick Steinhardt wrote:\n> > On Mon, Sep 07, 2026 at 11:12:08PM +0200, Beat Bolli wrote:\n> > > OpenSSL master (to be v4.1 after the release) renamed the function\n> > > ASN1_STRING_length() to ASN1_STRING_get_length(). Map the new name to\n> > > the old one if we're compiling with a pre-4.1 version.\n> > \n> > I can see [1] that the new functions indeed exist now. But it doesn't\n> > say anything about the old functions, they still exist and don't seem to\n> > be deprecated. So why do we even have to switch to the new function?\n> > \n> \n> The very page you give below contains this text:\n> \n> ---- 8< ----\n> The following functions have been deprecated since OpenSSL 4.1, and can be\n> hidden entirely by defining OPENSSL_API_COMPAT with a suitable version\n> value, see openssl_user_macros(7):\n> \n> int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);\n> int ASN1_STRING_length(ASN1_STRING *x);\n> ---- 8< ----\n> \n> We also don't define any compatibility macros for OpenSSL, and the build\n> failed because of the deprecation warning that was turned into an error\n> because of DEVELOPER=1.\n\nAh, I missed the part about OPENSSL_API_COMPAT. I think it would make\nsense to explicitly point that out in the commit message. Thanks!\n\nPatrick\n"},{"id":"554808","messageId":"20261012203057.969878-1-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20260907211210.2621693-1-dev+git@drbeat.li","subject":"[PATCH v2 1/2] imap-send: use the recommended TLS_method()","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-10-12T20:30:56Z","receivedAt":"2026-10-12T20:30:56Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"SSLv23_method() has been deprecated since OpenSSL 1.1.0, and\nTLS_method() is the recommended function.\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex bf1d2cf74d..9f21705d77 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -298,9 +298,9 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \tSSL_library_init();\n \tSSL_load_error_strings();\n \n-\tmeth = SSLv23_method();\n+\tmeth = TLS_method();\n \tif (!meth) {\n-\t\tssl_socket_perror(\"SSLv23_method\");\n+\t\tssl_socket_perror(\"TLS_method\");\n \t\treturn -1;\n \t}\n \n-- \n2.53.0\n\n\n"},{"id":"554809","messageId":"20261012203057.969878-2-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20260907211210.2621693-1-dev+git@drbeat.li","subject":"[PATCH v2 2/2] imap-send: let OpenSSL do the hostname verification","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-10-12T20:30:57Z","receivedAt":"2026-10-12T20:30:57Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"According to the OpenSSL Wiki[1], OpenSSL knows how to verify the peer's\nhostname since version 1.0.2. A simpler interface is available since\n1.1.0, which is what is implemented here.\n\nAs a side-effect, we get all present and future improvements done by\nOpenSSL for free. E.g., the old code did not handle IP addresses in the\nsubject alternative names, while the new code does.\n\nIf the verification fails, print the error message as returned by\nOpenSSL.\n\n[1]: https://wiki.openssl.org/index.php/Hostname_validation\n\nSuggested-by: brian m. carlson <sandals@crustytoothpaste.net>\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 86 +++++++++--------------------------------------------\n 1 file changed, 14 insertions(+), 72 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex 9f21705d77..cd9eae15d0 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -222,66 +222,6 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n \n #else\n \n-static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n-{\n-\tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n-\n-\t/* embedded NUL characters may open a security hole */\n-\tif (memchr(pattern, '\\0', ASN1_STRING_length(asn1_str)))\n-\t    return 0;\n-\n-\tif (pattern[0] == '*' && pattern[1] == '.') {\n-\t\tpattern += 2;\n-\t\tif (!(host = strchr(host, '.')))\n-\t\t\treturn 0;\n-\t\thost++;\n-\t}\n-\n-\treturn *host && *pattern && !strcasecmp(host, pattern);\n-}\n-\n-static int verify_hostname(X509 *cert, const char *hostname)\n-{\n-#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)\n-\tconst X509_NAME *subj;\n-#else\n-\tX509_NAME *subj;\n-#endif\n-\tconst X509_NAME_ENTRY *cname_entry;\n-\tconst ASN1_STRING *cname;\n-\tint i, found;\n-\tSTACK_OF(GENERAL_NAME) *subj_alt_names;\n-\n-\t/* try the DNS subjectAltNames */\n-\tfound = 0;\n-\tif ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {\n-\t\tint num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);\n-\t\tfor (i = 0; !found && i < num_subj_alt_names; i++) {\n-\t\t\tint ntype;\n-\t\t\tGENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);\n-\t\t\tASN1_STRING *subj_alt_str = GENERAL_NAME_get0_value(subj_alt_name, &ntype);\n-\n-\t\t\tif (ntype == GEN_DNS && host_matches(hostname, subj_alt_str))\n-\t\t\t\tfound = 1;\n-\t\t}\n-\t\tsk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);\n-\t}\n-\tif (found)\n-\t\treturn 0;\n-\n-\t/* try the common name */\n-\tif (!(subj = X509_get_subject_name(cert)))\n-\t\treturn error(\"cannot get certificate subject\");\n-\tif ((i = X509_NAME_get_index_by_NID(subj, NID_commonName, -1)) < 0 ||\n-\t    (cname_entry = X509_NAME_get_entry(subj, i)) == NULL ||\n-\t    (cname = X509_NAME_ENTRY_get_data(cname_entry)) == NULL)\n-\t\treturn error(\"cannot get certificate common name\");\n-\tif (host_matches(hostname, cname))\n-\t\treturn 0;\n-\treturn error(\"certificate owner '%s' does not match hostname '%s'\",\n-\t\t     ASN1_STRING_get0_data(cname), hostname);\n-}\n-\n static int ssl_socket_connect(struct imap_socket *sock,\n \t\t\t      const struct imap_server_conf *cfg,\n \t\t\t      int use_tls_only)\n@@ -293,7 +233,6 @@ static int ssl_socket_connect(struct imap_socket *sock,\n #endif\n \tSSL_CTX *ctx;\n \tint ret;\n-\tX509 *cert;\n \n \tSSL_library_init();\n \tSSL_load_error_strings();\n@@ -313,9 +252,6 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \tif (use_tls_only)\n \t\tSSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);\n \n-\tif (cfg->ssl_verify)\n-\t\tSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);\n-\n \tif (!SSL_CTX_set_default_verify_paths(ctx)) {\n \t\tssl_socket_perror(\"SSL_CTX_set_default_verify_paths\");\n \t\treturn -1;\n@@ -325,6 +261,15 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \t\tssl_socket_perror(\"SSL_new\");\n \t\treturn -1;\n \t}\n+\tif (cfg->ssl_verify) {\n+\t\tSSL_set_hostflags(sock->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);\n+\t\tif (!SSL_set1_host(sock->ssl, cfg->host)) {\n+\t\t\tssl_socket_perror(\"SSL_set1_host\");\n+\t\t\treturn -1;\n+\t\t}\n+\t\tSSL_set_verify(sock->ssl, SSL_VERIFY_PEER, NULL);\n+\t}\n+\n \tif (!SSL_set_rfd(sock->ssl, sock->fd[0])) {\n \t\tssl_socket_perror(\"SSL_set_rfd\");\n \t\treturn -1;\n@@ -352,14 +297,11 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \t}\n \n \tif (cfg->ssl_verify) {\n-\t\t/* make sure the hostname matches that of the certificate */\n-\t\tcert = SSL_get_peer_certificate(sock->ssl);\n-\t\tif (!cert)\n-\t\t\treturn error(\"unable to get peer certificate.\");\n-\t\tif (SSL_get_verify_result(sock->ssl) != X509_V_OK)\n-\t\t\treturn error(\"unable to verify peer certificate\");\n-\t\tif (verify_hostname(cert, cfg->host) < 0)\n-\t\t\treturn -1;\n+\t\t/* make sure the certificate was verified */\n+\t\tlong res = SSL_get_verify_result(sock->ssl);\n+\t\tif (res != X509_V_OK)\n+\t\t\treturn error(\"unable to verify peer certificate: %s\",\n+\t\t\t\t     X509_verify_cert_error_string(res));\n \t}\n \n \treturn 0;\n-- \n2.53.0\n\n\n"},{"id":"554817","messageId":"20261012220039.136932-1-aniketgupta3001@gmail.com","threadId":"66288","inReplyTo":"20261012203057.969878-2-dev+git@drbeat.li","subject":"Re: [PATCH v2 2/2] imap-send: let OpenSSL do the hostname verification","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-10-12T22:00:39Z","receivedAt":"2026-10-12T22:00:39Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Hello Beat,\n\n> +\t\tlong res = SSL_get_verify_result(sock->ssl);\n> +\t\tif (res != X509_V_OK)\n> +\t\t\treturn error(\"unable to verify peer certificate: %s\",\n\nI don't think this can be reached. With SSL_VERIFY_PEER, SSL_connect()\nalready fails, so a hostname mismatch would only print \"certificate\nverify failed\". It does not name the host like the old code did. Could\nwe print the verify result when SSL_connect() fails?\n\nThanks,\nAniket\n\n"},{"id":"554856","messageId":"as4uwMflv6qzcoTh@pks.im","threadId":"66288","inReplyTo":"20261012203057.969878-2-dev+git@drbeat.li","subject":"Re: [PATCH v2 2/2] imap-send: let OpenSSL do the hostname verification","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-13T13:14:40Z","receivedAt":"2026-10-13T13:14:40Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Oct 12, 2026 at 10:30:57PM +0200, Beat Bolli wrote:\n> According to the OpenSSL Wiki[1], OpenSSL knows how to verify the peer's\n> hostname since version 1.0.2. A simpler interface is available since\n> 1.1.0, which is what is implemented here.\n\nWhat is implicit here is that Git performs its own hostname\nverification. A short introduction explaining the status quo and why\nthe status quo is insufficient would help the reviewer.\n\nWhat's also missing is a quick note on the minimum supported version of\nOpenSSL.\n\n> As a side-effect, we get all present and future improvements done by\n> OpenSSL for free. E.g., the old code did not handle IP addresses in the\n> subject alternative names, while the new code does.\n\nThat sounds like it's win-win: less code and more supported scenarios!\n\n> If the verification fails, print the error message as returned by\n> OpenSSL.\n> \n> [1]: https://wiki.openssl.org/index.php/Hostname_validation\n> \n> Suggested-by: brian m. carlson <sandals@crustytoothpaste.net>\n> Signed-off-by: Beat Bolli <dev+git@drbeat.li>\n> ---\n>  imap-send.c | 86 +++++++++--------------------------------------------\n>  1 file changed, 14 insertions(+), 72 deletions(-)\n\nDon't we have any tests for this? If not, can we maybe add some to make\nsure that we indeed still verify the hostname?\n\n> diff --git a/imap-send.c b/imap-send.c\n> index 9f21705d77..cd9eae15d0 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -325,6 +261,15 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>  \t\tssl_socket_perror(\"SSL_new\");\n>  \t\treturn -1;\n>  \t}\n> +\tif (cfg->ssl_verify) {\n> +\t\tSSL_set_hostflags(sock->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);\n\nThis matches the documentation, but it also points out the\nX509_CHECK_FLAG_NO_WILDCARDS as a second commonly used flag. Why do we\npick one over the other?\n\n> @@ -352,14 +297,11 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>  \t}\n>  \n>  \tif (cfg->ssl_verify) {\n> -\t\t/* make sure the hostname matches that of the certificate */\n> -\t\tcert = SSL_get_peer_certificate(sock->ssl);\n> -\t\tif (!cert)\n> -\t\t\treturn error(\"unable to get peer certificate.\");\n> -\t\tif (SSL_get_verify_result(sock->ssl) != X509_V_OK)\n> -\t\t\treturn error(\"unable to verify peer certificate\");\n> -\t\tif (verify_hostname(cert, cfg->host) < 0)\n> -\t\t\treturn -1;\n> +\t\t/* make sure the certificate was verified */\n> +\t\tlong res = SSL_get_verify_result(sock->ssl);\n> +\t\tif (res != X509_V_OK)\n> +\t\t\treturn error(\"unable to verify peer certificate: %s\",\n> +\t\t\t\t     X509_verify_cert_error_string(res));\n\nWait, this feels fishy to me. Take the following from [1]:\n\n  In other cases where no peer certificate or raw public key was\n  presented, the returned result can still be X509_V_OK, for example\n  when a server did not request a client certificate during the initial\n  handshake. X509_V_OK alone does not establish how or whether the peer\n  was authenticated. A suitably provisioned external PSK can\n  authenticate a peer without a certificate or raw public key. When\n  certificate or raw public key authentication is expected, applications\n  must also check that the expected credential is present using\n  SSL_get_peer_certificate(3) or SSL_get0_peer_rpk(3).\n\nSo this here basically allows for a MITM attack in case the intercepting\nadversary simply strips the certificate altogether if I understand\ncorrectly. And to fix that we must retain the call to\n`SSL_get_peer_certificate()`.\n\nThat makes me want to have tests even more now.\n\n[1]: https://docs.openssl.org/4.1/man3/SSL_get_verify_result/#bugs\n\nPatrick\n\n"},{"id":"555232","messageId":"CABtdbskPaYgVnV+5P7-0G8KVYK1FBekwe2ygoYkZcvCR3RXj8Q@mail.gmail.com","threadId":"66288","inReplyTo":"20261012220039.136932-1-aniketgupta3001@gmail.com","subject":"Re: [PATCH v2 2/2] imap-send: let OpenSSL do the hostname verification","fromName":"Xiangfei Ding","fromEmail":"xfding@google.com","sentAt":"2026-10-16T10:56:54Z","receivedAt":"2026-10-16T10:56:54Z","isPatch":true,"sender":{"key":"xfding@google.com","avatar":null},"body":"Hello Beat and Aniket,\n\n> I don't think this can be reached. With SSL_VERIFY_PEER, SSL_connect()\n> already fails, so a hostname mismatch would only print \"certificate\n> verify failed\". It does not name the host like the old code did. Could\n> we print the verify result when SSL_connect() fails?\n\nI suppose that we will have to install a callback at SSL_set_verify,\nfrom which we could leave the bad hostname in a \"mailbox\" so that\nin case SSL_connect() fails we could pick it up and print it out.\n\nWould you mind me posting a patch on top of yours?\n\nBest,\nXiang\n\nOn Tue, Oct 13, 2026 at 12:00 AM Aniket Gupta <aniketgupta3001@gmail.com> wrote:\n>\n> Hello Beat,\n>\n> > +             long res = SSL_get_verify_result(sock->ssl);\n> > +             if (res != X509_V_OK)\n> > +                     return error(\"unable to verify peer certificate: %s\",\n>\n> I don't think this can be reached. With SSL_VERIFY_PEER, SSL_connect()\n> already fails, so a hostname mismatch would only print \"certificate\n> verify failed\". It does not name the host like the old code did. Could\n> we print the verify result when SSL_connect() fails?\n>\n> Thanks,\n> Aniket\n\n"},{"id":"555312","messageId":"20261017100956.529225-1-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20261012203057.969878-1-dev+git@drbeat.li","subject":"[PATCH v3 1/3] imap-send: use the recommended TLS_method()","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-10-17T10:09:54Z","receivedAt":"2026-10-17T10:09:54Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"SSLv23_method() has been deprecated since OpenSSL 1.1.0, and\nTLS_method() is the recommended function[1].\n\nLet's use the current function, lest a reader of the code thinks we\ndon't support current TLS versions.\n\n[1]: https://docs.openssl.org/master/man3/SSL_CTX_new/#history\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex bf1d2cf74d..9f21705d77 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -298,9 +298,9 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \tSSL_library_init();\n \tSSL_load_error_strings();\n \n-\tmeth = SSLv23_method();\n+\tmeth = TLS_method();\n \tif (!meth) {\n-\t\tssl_socket_perror(\"SSLv23_method\");\n+\t\tssl_socket_perror(\"TLS_method\");\n \t\treturn -1;\n \t}\n \n-- \n2.53.0\n\n\n"},{"id":"555313","messageId":"20261017100956.529225-2-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20261017100956.529225-1-dev+git@drbeat.li","subject":"[PATCH v3 2/3] imap-send: let OpenSSL do the hostname verification","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-10-17T10:09:55Z","receivedAt":"2026-10-17T10:09:55Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"imap-send performs its own TLS hostname verification. This is not ideal\nbecause it does not support all certificate features.\n\nLet's use OpenSSL built-in verification, which is supported in its\ncurrent form since OpenSSL 1.1.0. This gets us all present and future\nimprovements done by OpenSSL for free. E.g., the old code did not handle\nIP addresses in the subject alternative names, while the new code does.\n\nIf the verification fails, print the error message as returned by\nOpenSSL.\n\n[1]: https://wiki.openssl.org/index.php/Hostname_validation\n\nSuggested-by: brian m. carlson <sandals@crustytoothpaste.net>\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\nWe cannot easily print the certificate's hostname in case of a mismatch,\nbut the cURL branch does not, either.\n\nUnfortuately, the diff looks bad because two functions are replaced by\nthe new verify_peer() function that is completely unrelated.\n\n imap-send.c | 101 +++++++++++++++++-----------------------------------\n 1 file changed, 33 insertions(+), 68 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex 9f21705d77..ec4e28e423 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -222,64 +222,30 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n \n #else\n \n-static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n+static int verify_peer(const SSL *ssl, int did_connect)\n {\n-\tconst char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n+\tlong verify;\n \n-\t/* embedded NUL characters may open a security hole */\n-\tif (memchr(pattern, '\\0', ASN1_STRING_length(asn1_str)))\n-\t    return 0;\n-\n-\tif (pattern[0] == '*' && pattern[1] == '.') {\n-\t\tpattern += 2;\n-\t\tif (!(host = strchr(host, '.')))\n-\t\t\treturn 0;\n-\t\thost++;\n-\t}\n-\n-\treturn *host && *pattern && !strcasecmp(host, pattern);\n-}\n+\t/*\n+\t * If we connected, we require a certificate.\n+\t * If we did not connect because of a certificate error, the server\n+\t * certificate is not installed in the SSL*, but the verification\n+\t * result will still be returned below.\n+\t */\n+\tif (did_connect) {\n+\t\tX509 *cert = SSL_get_peer_certificate(ssl);\n \n-static int verify_hostname(X509 *cert, const char *hostname)\n-{\n-#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)\n-\tconst X509_NAME *subj;\n-#else\n-\tX509_NAME *subj;\n-#endif\n-\tconst X509_NAME_ENTRY *cname_entry;\n-\tconst ASN1_STRING *cname;\n-\tint i, found;\n-\tSTACK_OF(GENERAL_NAME) *subj_alt_names;\n-\n-\t/* try the DNS subjectAltNames */\n-\tfound = 0;\n-\tif ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {\n-\t\tint num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);\n-\t\tfor (i = 0; !found && i < num_subj_alt_names; i++) {\n-\t\t\tint ntype;\n-\t\t\tGENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);\n-\t\t\tASN1_STRING *subj_alt_str = GENERAL_NAME_get0_value(subj_alt_name, &ntype);\n-\n-\t\t\tif (ntype == GEN_DNS && host_matches(hostname, subj_alt_str))\n-\t\t\t\tfound = 1;\n-\t\t}\n-\t\tsk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);\n+\t\tif (!cert)\n+\t\t\treturn error(\"unable to get peer certificate.\");\n+\t\tX509_free(cert);\n \t}\n-\tif (found)\n-\t\treturn 0;\n \n-\t/* try the common name */\n-\tif (!(subj = X509_get_subject_name(cert)))\n-\t\treturn error(\"cannot get certificate subject\");\n-\tif ((i = X509_NAME_get_index_by_NID(subj, NID_commonName, -1)) < 0 ||\n-\t    (cname_entry = X509_NAME_get_entry(subj, i)) == NULL ||\n-\t    (cname = X509_NAME_ENTRY_get_data(cname_entry)) == NULL)\n-\t\treturn error(\"cannot get certificate common name\");\n-\tif (host_matches(hostname, cname))\n-\t\treturn 0;\n-\treturn error(\"certificate owner '%s' does not match hostname '%s'\",\n-\t\t     ASN1_STRING_get0_data(cname), hostname);\n+\t/* check the verification result, ignoring the connection success */\n+\tverify = SSL_get_verify_result(ssl);\n+\tif (verify != X509_V_OK)\n+\t\treturn error(\"unable to verify peer certificate: %s\",\n+\t\t\t     X509_verify_cert_error_string(verify));\n+\treturn 0;\n }\n \n static int ssl_socket_connect(struct imap_socket *sock,\n@@ -293,7 +259,6 @@ static int ssl_socket_connect(struct imap_socket *sock,\n #endif\n \tSSL_CTX *ctx;\n \tint ret;\n-\tX509 *cert;\n \n \tSSL_library_init();\n \tSSL_load_error_strings();\n@@ -313,9 +278,6 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \tif (use_tls_only)\n \t\tSSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);\n \n-\tif (cfg->ssl_verify)\n-\t\tSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);\n-\n \tif (!SSL_CTX_set_default_verify_paths(ctx)) {\n \t\tssl_socket_perror(\"SSL_CTX_set_default_verify_paths\");\n \t\treturn -1;\n@@ -325,6 +287,16 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \t\tssl_socket_perror(\"SSL_new\");\n \t\treturn -1;\n \t}\n+\tif (cfg->ssl_verify) {\n+\t\t/* follow RFC 9525 section 6.3: no partial wildcards */\n+\t\tSSL_set_hostflags(sock->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);\n+\t\tif (!SSL_set1_host(sock->ssl, cfg->host)) {\n+\t\t\tssl_socket_perror(\"SSL_set1_host\");\n+\t\t\treturn -1;\n+\t\t}\n+\t\tSSL_set_verify(sock->ssl, SSL_VERIFY_PEER, NULL);\n+\t}\n+\n \tif (!SSL_set_rfd(sock->ssl, sock->fd[0])) {\n \t\tssl_socket_perror(\"SSL_set_rfd\");\n \t\treturn -1;\n@@ -348,20 +320,13 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \tret = SSL_connect(sock->ssl);\n \tif (ret <= 0) {\n \t\tsocket_perror(\"SSL_connect\", sock, ret);\n+\t\tif (cfg->ssl_verify)\n+\t\t\tverify_peer(sock->ssl, 0);\n \t\treturn -1;\n \t}\n \n-\tif (cfg->ssl_verify) {\n-\t\t/* make sure the hostname matches that of the certificate */\n-\t\tcert = SSL_get_peer_certificate(sock->ssl);\n-\t\tif (!cert)\n-\t\t\treturn error(\"unable to get peer certificate.\");\n-\t\tif (SSL_get_verify_result(sock->ssl) != X509_V_OK)\n-\t\t\treturn error(\"unable to verify peer certificate\");\n-\t\tif (verify_hostname(cert, cfg->host) < 0)\n-\t\t\treturn -1;\n-\t}\n-\n+\tif (cfg->ssl_verify)\n+\t\treturn verify_peer(sock->ssl, 1);\n \treturn 0;\n }\n #endif\n-- \n2.53.0\n\n\n"},{"id":"555314","messageId":"20261017100956.529225-3-dev+git@drbeat.li","threadId":"66288","inReplyTo":"20261017100956.529225-1-dev+git@drbeat.li","subject":"[PATCH v3 3/3] imap-send: prepare for OpenSSL 4.0","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-10-17T10:09:56Z","receivedAt":"2026-10-17T10:09:56Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"OpenSSL 4.0 deprecated the SSL_set1_host function if favor of the new\nfunction SSL_set1_dnsname, which fortunately takes the same parameters.\n\nAdd compatibility #defines for the function and its string variant for\nthe error message.\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\n imap-send.c | 11 +++++++++--\n 1 file changed, 9 insertions(+), 2 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex ec4e28e423..526bde2f55 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -248,6 +248,13 @@ static int verify_peer(const SSL *ssl, int did_connect)\n \treturn 0;\n }\n \n+#if (OPENSSL_VERSION_NUMBER < 0x40000000L)\n+#define SSL_set1_dnsname(ssl, host) SSL_set1_host((ssl), (host))\n+#define SSL_SET_DNSNAME_FN \"SSL_set1_host\"\n+#else\n+#define SSL_SET_DNSNAME_FN \"SSL_set1_dnsname\"\n+#endif\n+\n static int ssl_socket_connect(struct imap_socket *sock,\n \t\t\t      const struct imap_server_conf *cfg,\n \t\t\t      int use_tls_only)\n@@ -290,8 +297,8 @@ static int ssl_socket_connect(struct imap_socket *sock,\n \tif (cfg->ssl_verify) {\n \t\t/* follow RFC 9525 section 6.3: no partial wildcards */\n \t\tSSL_set_hostflags(sock->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);\n-\t\tif (!SSL_set1_host(sock->ssl, cfg->host)) {\n-\t\t\tssl_socket_perror(\"SSL_set1_host\");\n+\t\tif (!SSL_set1_dnsname(sock->ssl, cfg->host)) {\n+\t\t\tssl_socket_perror(SSL_SET_DNSNAME_FN);\n \t\t\treturn -1;\n \t\t}\n \t\tSSL_set_verify(sock->ssl, SSL_VERIFY_PEER, NULL);\n-- \n2.53.0\n\n\n"},{"id":"555384","messageId":"atW7CLcWmKRk4sMv@pks.im","threadId":"66288","inReplyTo":"20261017100956.529225-1-dev+git@drbeat.li","subject":"Re: [PATCH v3 1/3] imap-send: use the recommended TLS_method()","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-19T06:39:04Z","receivedAt":"2026-10-19T06:39:04Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sat, Oct 17, 2026 at 12:09:54PM +0200, Beat Bolli wrote:\n> SSLv23_method() has been deprecated since OpenSSL 1.1.0, and\n> TLS_method() is the recommended function[1].\n> \n> Let's use the current function, lest a reader of the code thinks we\n> don't support current TLS versions.\n> \n> [1]: https://docs.openssl.org/master/man3/SSL_CTX_new/#history\n> \n> Signed-off-by: Beat Bolli <dev+git@drbeat.li>\n\nI would have appreciated a cover letter that points out:\n\n  - What this series is doing.\n\n  - What has changed between v2 and v3, ideally including a range-diff.\n\nLike, why is there a third patch now, for example? I have no idea, so\nfor reviews to go smooth and efficient we require help from the authors\nof a given patch series.\n\nI would recommend using b4 going forward, which automatically manages\nall of those details for you.\n\nAlso, we strongly encourage folks to please reply to review feedback\ndirectly instead of just sending the next version of a patch series.\n\nPatrick\n\n"},{"id":"555385","messageId":"atW7DJZwHYK_hABy@pks.im","threadId":"66288","inReplyTo":"20261017100956.529225-2-dev+git@drbeat.li","subject":"Re: [PATCH v3 2/3] imap-send: let OpenSSL do the hostname verification","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-19T06:39:08Z","receivedAt":"2026-10-19T06:39:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sat, Oct 17, 2026 at 12:09:55PM +0200, Beat Bolli wrote:\n> imap-send performs its own TLS hostname verification. This is not ideal\n> because it does not support all certificate features.\n> \n> Let's use OpenSSL built-in verification, which is supported in its\n> current form since OpenSSL 1.1.0. This gets us all present and future\n> improvements done by OpenSSL for free. E.g., the old code did not handle\n> IP addresses in the subject alternative names, while the new code does.\n> \n> If the verification fails, print the error message as returned by\n> OpenSSL.\n> \n> [1]: https://wiki.openssl.org/index.php/Hostname_validation\n> \n> Suggested-by: brian m. carlson <sandals@crustytoothpaste.net>\n> Signed-off-by: Beat Bolli <dev+git@drbeat.li>\n> ---\n> We cannot easily print the certificate's hostname in case of a mismatch,\n> but the cURL branch does not, either.\n> \n> Unfortuately, the diff looks bad because two functions are replaced by\n> the new verify_peer() function that is completely unrelated.\n\nWould it have been possible to make the diff a bit easier to read, for\nexample by having a preparatory patch that simply moves code around into\nthis new `verify_peer()` function without yet changing semantics, and\nthen changing semantics in a second patch?\n\n> diff --git a/imap-send.c b/imap-send.c\n> index 9f21705d77..ec4e28e423 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -348,20 +320,13 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>  \tret = SSL_connect(sock->ssl);\n>  \tif (ret <= 0) {\n>  \t\tsocket_perror(\"SSL_connect\", sock, ret);\n> +\t\tif (cfg->ssl_verify)\n> +\t\t\tverify_peer(sock->ssl, 0);\n\nWhy do we need to verify the certificate when we failed connecting to\nthe peer? We didn't do that before, either, did we?\n\nPatrick\n\n"},{"id":"555386","messageId":"atW7EcdjIEodXMD3@pks.im","threadId":"66288","inReplyTo":"20261017100956.529225-3-dev+git@drbeat.li","subject":"Re: [PATCH v3 3/3] imap-send: prepare for OpenSSL 4.0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-19T06:39:13Z","receivedAt":"2026-10-19T06:39:13Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sat, Oct 17, 2026 at 12:09:56PM +0200, Beat Bolli wrote:\n> OpenSSL 4.0 deprecated the SSL_set1_host function if favor of the new\n> function SSL_set1_dnsname, which fortunately takes the same parameters.\n> \n> Add compatibility #defines for the function and its string variant for\n> the error message.\n\nThis patch does not make any sense -- the only site where we call the\ndeprecated function is the one you have introduced yourself in the\npreceding commit. Consequently, this should already be part of that\ncommit.\n\n> diff --git a/imap-send.c b/imap-send.c\n> index ec4e28e423..526bde2f55 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -248,6 +248,13 @@ static int verify_peer(const SSL *ssl, int did_connect)\n>  \treturn 0;\n>  }\n>  \n> +#if (OPENSSL_VERSION_NUMBER < 0x40000000L)\n> +#define SSL_set1_dnsname(ssl, host) SSL_set1_host((ssl), (host))\n> +#define SSL_SET_DNSNAME_FN \"SSL_set1_host\"\n> +#else\n> +#define SSL_SET_DNSNAME_FN \"SSL_set1_dnsname\"\n> +#endif\n> +\n>  static int ssl_socket_connect(struct imap_socket *sock,\n>  \t\t\t      const struct imap_server_conf *cfg,\n>  \t\t\t      int use_tls_only)\n> @@ -290,8 +297,8 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>  \tif (cfg->ssl_verify) {\n>  \t\t/* follow RFC 9525 section 6.3: no partial wildcards */\n>  \t\tSSL_set_hostflags(sock->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);\n> -\t\tif (!SSL_set1_host(sock->ssl, cfg->host)) {\n> -\t\t\tssl_socket_perror(\"SSL_set1_host\");\n> +\t\tif (!SSL_set1_dnsname(sock->ssl, cfg->host)) {\n\nSo according to [1], `SSL_set1_host` was replaced by `SSL_set1_dnsname`\nand `SSL_set1_ipaddr`. So do we know that `cfg->host` really always is a\nDNS name and never an IP address?\n\n> +\t\t\tssl_socket_perror(SSL_SET_DNSNAME_FN);\n>  \t\t\treturn -1;\n>  \t\t}\n\nI wonder whether it's really all that helpful to also change the error\nmessage accordingly. Shouldn't it be sufficient to unconditionally print\nthe new error message here?\n\nPatrick\n\n[1]: https://docs.openssl.org/master/man3/SSL_set1_host/#description\n\n"},{"id":"555395","messageId":"CABtdbsnrQ61eJv4GhawiFSHkN01tJhV5tYj8MNQT8F3zf3PgYw@mail.gmail.com","threadId":"66288","inReplyTo":"20261017100956.529225-2-dev+git@drbeat.li","subject":"Re: [PATCH v3 2/3] imap-send: let OpenSSL do the hostname verification","fromName":"Xiangfei Ding","fromEmail":"xfding@google.com","sentAt":"2026-10-19T08:44:05Z","receivedAt":"2026-10-19T08:44:05Z","isPatch":true,"sender":{"key":"xfding@google.com","avatar":null},"body":"> let OpenSSL do the hostname verification\n\nI think this is a much cleaner solution, thank you so much!\n\nReviewed-by: Xiangfei Ding <xfding@google.com>\n\nOn Sat, Oct 17, 2026 at 12:10 PM Beat Bolli <dev+git@drbeat.li> wrote:\n>\n> imap-send performs its own TLS hostname verification. This is not ideal\n> because it does not support all certificate features.\n>\n> Let's use OpenSSL built-in verification, which is supported in its\n> current form since OpenSSL 1.1.0. This gets us all present and future\n> improvements done by OpenSSL for free. E.g., the old code did not handle\n> IP addresses in the subject alternative names, while the new code does.\n>\n> If the verification fails, print the error message as returned by\n> OpenSSL.\n>\n> [1]: https://wiki.openssl.org/index.php/Hostname_validation\n>\n> Suggested-by: brian m. carlson <sandals@crustytoothpaste.net>\n> Signed-off-by: Beat Bolli <dev+git@drbeat.li>\n> ---\n> We cannot easily print the certificate's hostname in case of a mismatch,\n> but the cURL branch does not, either.\n>\n> Unfortuately, the diff looks bad because two functions are replaced by\n> the new verify_peer() function that is completely unrelated.\n>\n>  imap-send.c | 101 +++++++++++++++++-----------------------------------\n>  1 file changed, 33 insertions(+), 68 deletions(-)\n>\n> diff --git a/imap-send.c b/imap-send.c\n> index 9f21705d77..ec4e28e423 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -222,64 +222,30 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\n>\n>  #else\n>\n> -static int host_matches(const char *host, const ASN1_STRING *asn1_str)\n> +static int verify_peer(const SSL *ssl, int did_connect)\n>  {\n> -       const char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str);\n> +       long verify;\n>\n> -       /* embedded NUL characters may open a security hole */\n> -       if (memchr(pattern, '\\0', ASN1_STRING_length(asn1_str)))\n> -           return 0;\n> -\n> -       if (pattern[0] == '*' && pattern[1] == '.') {\n> -               pattern += 2;\n> -               if (!(host = strchr(host, '.')))\n> -                       return 0;\n> -               host++;\n> -       }\n> -\n> -       return *host && *pattern && !strcasecmp(host, pattern);\n> -}\n> +       /*\n> +        * If we connected, we require a certificate.\n> +        * If we did not connect because of a certificate error, the server\n> +        * certificate is not installed in the SSL*, but the verification\n> +        * result will still be returned below.\n> +        */\n> +       if (did_connect) {\n> +               X509 *cert = SSL_get_peer_certificate(ssl);\n>\n> -static int verify_hostname(X509 *cert, const char *hostname)\n> -{\n> -#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)\n> -       const X509_NAME *subj;\n> -#else\n> -       X509_NAME *subj;\n> -#endif\n> -       const X509_NAME_ENTRY *cname_entry;\n> -       const ASN1_STRING *cname;\n> -       int i, found;\n> -       STACK_OF(GENERAL_NAME) *subj_alt_names;\n> -\n> -       /* try the DNS subjectAltNames */\n> -       found = 0;\n> -       if ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {\n> -               int num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);\n> -               for (i = 0; !found && i < num_subj_alt_names; i++) {\n> -                       int ntype;\n> -                       GENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);\n> -                       ASN1_STRING *subj_alt_str = GENERAL_NAME_get0_value(subj_alt_name, &ntype);\n> -\n> -                       if (ntype == GEN_DNS && host_matches(hostname, subj_alt_str))\n> -                               found = 1;\n> -               }\n> -               sk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);\n> +               if (!cert)\n> +                       return error(\"unable to get peer certificate.\");\n> +               X509_free(cert);\n>         }\n> -       if (found)\n> -               return 0;\n>\n> -       /* try the common name */\n> -       if (!(subj = X509_get_subject_name(cert)))\n> -               return error(\"cannot get certificate subject\");\n> -       if ((i = X509_NAME_get_index_by_NID(subj, NID_commonName, -1)) < 0 ||\n> -           (cname_entry = X509_NAME_get_entry(subj, i)) == NULL ||\n> -           (cname = X509_NAME_ENTRY_get_data(cname_entry)) == NULL)\n> -               return error(\"cannot get certificate common name\");\n> -       if (host_matches(hostname, cname))\n> -               return 0;\n> -       return error(\"certificate owner '%s' does not match hostname '%s'\",\n> -                    ASN1_STRING_get0_data(cname), hostname);\n> +       /* check the verification result, ignoring the connection success */\n> +       verify = SSL_get_verify_result(ssl);\n> +       if (verify != X509_V_OK)\n> +               return error(\"unable to verify peer certificate: %s\",\n> +                            X509_verify_cert_error_string(verify));\n> +       return 0;\n>  }\n>\n>  static int ssl_socket_connect(struct imap_socket *sock,\n> @@ -293,7 +259,6 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>  #endif\n>         SSL_CTX *ctx;\n>         int ret;\n> -       X509 *cert;\n>\n>         SSL_library_init();\n>         SSL_load_error_strings();\n> @@ -313,9 +278,6 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>         if (use_tls_only)\n>                 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);\n>\n> -       if (cfg->ssl_verify)\n> -               SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);\n> -\n>         if (!SSL_CTX_set_default_verify_paths(ctx)) {\n>                 ssl_socket_perror(\"SSL_CTX_set_default_verify_paths\");\n>                 return -1;\n> @@ -325,6 +287,16 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>                 ssl_socket_perror(\"SSL_new\");\n>                 return -1;\n>         }\n> +       if (cfg->ssl_verify) {\n> +               /* follow RFC 9525 section 6.3: no partial wildcards */\n> +               SSL_set_hostflags(sock->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);\n> +               if (!SSL_set1_host(sock->ssl, cfg->host)) {\n> +                       ssl_socket_perror(\"SSL_set1_host\");\n> +                       return -1;\n> +               }\n> +               SSL_set_verify(sock->ssl, SSL_VERIFY_PEER, NULL);\n> +       }\n> +\n>         if (!SSL_set_rfd(sock->ssl, sock->fd[0])) {\n>                 ssl_socket_perror(\"SSL_set_rfd\");\n>                 return -1;\n> @@ -348,20 +320,13 @@ static int ssl_socket_connect(struct imap_socket *sock,\n>         ret = SSL_connect(sock->ssl);\n>         if (ret <= 0) {\n>                 socket_perror(\"SSL_connect\", sock, ret);\n> +               if (cfg->ssl_verify)\n> +                       verify_peer(sock->ssl, 0);\n>                 return -1;\n>         }\n>\n> -       if (cfg->ssl_verify) {\n> -               /* make sure the hostname matches that of the certificate */\n> -               cert = SSL_get_peer_certificate(sock->ssl);\n> -               if (!cert)\n> -                       return error(\"unable to get peer certificate.\");\n> -               if (SSL_get_verify_result(sock->ssl) != X509_V_OK)\n> -                       return error(\"unable to verify peer certificate\");\n> -               if (verify_hostname(cert, cfg->host) < 0)\n> -                       return -1;\n> -       }\n> -\n> +       if (cfg->ssl_verify)\n> +               return verify_peer(sock->ssl, 1);\n>         return 0;\n>  }\n>  #endif\n> --\n> 2.53.0\n>\n\n"}]}