{"thread":{"id":"66505","subject":"[PATCH] imap-send: use ASN1_STRING_length","startedAt":"2026-10-11T21:09:28Z","lastAt":"2026-10-19T08:48:48Z","messageCount":6,"participants":["Xiangfei Ding","Junio C Hamano","Aniket Gupta","Beat Bolli"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"554723","messageId":"20261011210928.3900020-1-xfding@google.com","threadId":"66505","inReplyTo":null,"subject":"[PATCH] imap-send: use ASN1_STRING_length","fromName":"Xiangfei Ding","fromEmail":"xfding@google.com","sentAt":"2026-10-11T21:09:28Z","receivedAt":"2026-10-11T21:09:28Z","isPatch":true,"sender":{"key":"xfding@google.com","avatar":null},"body":"From: xfding <xfding@google.com>\n\nX.509 has never stipulated that ASN1_STRING shall be null-terminated in general.\nIn OpenSSL, `ASN1_STRING_get0_data()` returns an internal pointer to\nthe raw string payload, which is lucky for us null-terminated. This is probably\nabout to change as OpenSSL derivatives like BoringSSL would adopt changes so\nthat the data buffer would not be null-terminated anymore.\n\nStrictly speaking, printing or accessing `ASN1_STRING` data should always query\n`ASN1_STRING_length`. This means that we should adapt `host_matches` and\n`verify_hostname` to use `strncasecmp` and print strings with designated length.\n\nSigned-off-by: Xiangfei Ding <dingxiangfei2009@protonmail.ch>\nSigned-off-by: Xiangfei Ding <xfding@google.com>\n---\n imap-send.c | 15 +++++++++------\n 1 file changed, 9 insertions(+), 6 deletions(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex bf1d2cf74d..bd306b8d0a 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -225,19 +225,21 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\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 pat_len = ASN1_STRING_length(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+\tif (memchr(pattern, '\\0', pat_len))\n+\t\treturn 0;\n \n-\tif (pattern[0] == '*' && pattern[1] == '.') {\n+\tif (pat_len >= 2 && pattern[0] == '*' && pattern[1] == '.') {\n \t\tpattern += 2;\n+\t\tpat_len -= 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+\treturn *host && strlen(host) == (size_t)pat_len && !strncasecmp(host, pattern, pat_len);\n }\n \n static int verify_hostname(X509 *cert, const char *hostname)\n@@ -278,8 +280,9 @@ static int verify_hostname(X509 *cert, const char *hostname)\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+\treturn error(\"certificate owner '%.*s' does not match hostname '%s'\",\n+\t\t     (int)ASN1_STRING_length(cname),\n+\t\t     (const char *)ASN1_STRING_get0_data(cname), hostname);\n }\n \n static int ssl_socket_connect(struct imap_socket *sock,\n-- \n2.56.0.385.gd3acb90ef8-goog\n\n\n"},{"id":"554728","messageId":"xmqqcxtfet2k.fsf@gitster.g","threadId":"66505","inReplyTo":"20261011210928.3900020-1-xfding@google.com","subject":"Re: [PATCH] imap-send: use ASN1_STRING_length","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-11T23:05:55Z","receivedAt":"2026-10-11T23:05:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Xiangfei Ding <xfding@google.com> writes:\n\n> From: xfding <xfding@google.com>\n\nThis line is called in-body From: that is used to override the \"From:\"\nheader your MUA will add.  But in this case, I think you do not want\nto override \"Xiangfei Ding <xfding@google.com>\", which is what your\nMUA sent the message as.  In any case, either MUA From or in-body From\nis used as the author identity when this patch e-mail is committed, so\nit should match your sign-off we see later.\n\n> X.509 has never stipulated that ASN1_STRING shall be null-terminated in general.\n> In OpenSSL, `ASN1_STRING_get0_data()` returns an internal pointer to\n> the raw string payload, which is lucky for us null-terminated.\n\nOK.\n\n> This is probably\n> about to change as OpenSSL derivatives like BoringSSL would adopt changes so\n> that the data buffer would not be null-terminated anymore.\n\nIt is unclear from reading the above if you mean only derivatives\nwill adopt changes not to NUL-terminate, or OpenSSL will change\nASN1_STRING_get0_data() to return a string that is not\nNUL-terminated, and derivatives will follow suit.\n\n> Strictly speaking, printing or accessing `ASN1_STRING` data should always query\n> `ASN1_STRING_length`. This means that we should adapt `host_matches` and\n> `verify_hostname` to use `strncasecmp` and print strings with designated length.\n\nMakes sense.  I take that the second paragraph to mean \"not using\nASN1_STRING_length happened to be OK, but that is not true anymore\nwith various implementations of OpenSSL like libraries\",\nillustrating the need and urgency to make this change?\n\nIn our log message, usually all of the above (i.e., observation of\nthe current situation and pointing out problems in the status quo)\nare followed by what to do to solve the problem by giving an order\nto hypothetical somebody to update the code to \"make it so\", e.g.,\n\n    Update code that assumes ASN1_STRING data are NUL-terminated to\n    use ASN1_STRING_length and treat the data as <pointer, length>\n    pair.\n\nbut the above description you gave is sufficiently detailed and left\nno need to make a separate description of the solution.\n\n> Signed-off-by: Xiangfei Ding <dingxiangfei2009@protonmail.ch>\n> Signed-off-by: Xiangfei Ding <xfding@google.com>\n\nWhat are these two separate sign-off lines doing?\n\nUnless rules have changed recently, I think Google OSPO tells you as\na Google engineer [*] to use your @google.com address when making\ncontributions to open source projects, \"unless you have a history of\ncontributing to the repo under a different email before your\nemployment at Google\".  So only the second one would be what you\nwant to use, I presume?  Again, as I mentioned earlier, it matches\nthe address your MUA sent the message as, so you do not need/want\nthe in-body From: line.\n\n[Reference]\n\n * https://opensource.google/documentation/reference/patching\n\n> diff --git a/imap-send.c b/imap-send.c\n> index bf1d2cf74d..bd306b8d0a 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -225,19 +225,21 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED,\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 pat_len = ASN1_STRING_length(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> +\tif (memchr(pattern, '\\0', pat_len))\n> +\t\treturn 0;\n\nOK, the above is essentially a no-op.  But we have the length in a\nvariable so we can reuse it in the rest of the function, which is\ngood.\n\n> -\tif (pattern[0] == '*' && pattern[1] == '.') {\n> +\tif (pat_len >= 2 && pattern[0] == '*' && pattern[1] == '.') {\n>  \t\tpattern += 2;\n> +\t\tpat_len -= 2;\n>  \t\tif (!(host = strchr(host, '.')))\n>  \t\t\treturn 0;\n>  \t\thost++;\n>  \t}\n\nOK, we check for \"*.\" only when we have at least two bytes, and\nconsume these two bytes if they match.\n\n> -\treturn *host && *pattern && !strcasecmp(host, pattern);\n> +\treturn *host && strlen(host) == (size_t)pat_len && !strncasecmp(host, pattern, pat_len);\n\nLIne-wrap this overly long line.\n\n\treturn *host && strlen(host) == (size_t)pat_len &&\n               !strncasecmp(host, pattern, pat_len);\n\nCurious.  If we declare pat_len to be of type size_t, we would no\nlonger need to cast it here.  Even though we passed pat_len to\nmemchr() earlier, that call implicitly promoted int to size_t, which\nwould also become unnecessary.\n\n>  }\n>  \n>  static int verify_hostname(X509 *cert, const char *hostname)\n> @@ -278,8 +280,9 @@ static int verify_hostname(X509 *cert, const char *hostname)\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> +\treturn error(\"certificate owner '%.*s' does not match hostname '%s'\",\n> +\t\t     (int)ASN1_STRING_length(cname),\n\nBecause ASN1_STRING_length() is declared to return \"int\", this cast\nis not strictly needed, but it is good to be explicit in a case like\nthis, I think.\n\n> +\t\t     (const char *)ASN1_STRING_get0_data(cname), hostname);\n\nGreat.  \n\n"},{"id":"554771","messageId":"20261012151748.62723-1-aniketgupta3001@gmail.com","threadId":"66505","inReplyTo":"20261011210928.3900020-1-xfding@google.com","subject":"Re: [PATCH] imap-send: use ASN1_STRING_length","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-10-12T15:17:48Z","receivedAt":"2026-10-12T15:17:48Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Hello Xiangfei,\n\n> +\tint pat_len = ASN1_STRING_length(asn1_str);\n\nIt seems that with OpenSSL 4.1.0-beta2 this is deprecated (ASN1_STRING_get_length() is the replacement), so it warns, and fails with DEVELOPER=1. The patch adds a second use at the error message. Beat Bolli has a series from 7 Sept (\"imap-send: future proofing and two correctness fixes\") that also touches host_matches(), maybe we can check how the two can fit together?\n\nThanks,\nAniket\n\n"},{"id":"554778","messageId":"xmqqv77696sf.fsf@gitster.g","threadId":"66505","inReplyTo":"20261012151748.62723-1-aniketgupta3001@gmail.com","subject":"Re: [PATCH] imap-send: use ASN1_STRING_length","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-12T17:18:24Z","receivedAt":"2026-10-12T17:18:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Aniket Gupta <aniketgupta3001@gmail.com> writes:\n\n> Hello Xiangfei,\n>\n>> +\tint pat_len = ASN1_STRING_length(asn1_str);\n>\n> It seems that with OpenSSL 4.1.0-beta2 this is deprecated\n> (ASN1_STRING_get_length() is the replacement), so it warns, and\n> fails with DEVELOPER=1. The patch adds a second use at the error\n> message. Beat Bolli has a series from 7 Sept (\"imap-send: future\n> proofing and two correctness fixes\") that also touches\n> host_matches(), maybe we can check how the two can fit together?\n>\n> Thanks,\n> Aniket\n\n[jch: rewrapped long unreadable lines]\n\nThat topic also needs rework, unfortunately.\n\n  https://lore.kernel.org/git/20260907211210.2621693-1-dev+git@drbeat.li/\n\nI guess we can salvage the patches 1 and 2 with fairly trivial\nchanges, namely with the style fix in [1/3], and pointer arithmetic\nthat breaks later free() fix in [2/3].  I am not sure how involved\nit would be to fix [3/3] in such a way suggested by brian in the\nthread.\n\nThanks for reminding us of the old thread.\n\n\n\n"},{"id":"554806","messageId":"5d5dfa51-abc3-4a8f-badd-48ee398ad73f@drbeat.li","threadId":"66505","inReplyTo":"xmqqv77696sf.fsf@gitster.g","subject":"Re: [PATCH] imap-send: use ASN1_STRING_length","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2026-10-12T20:09:16Z","receivedAt":"2026-10-12T20:09:16Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"On 12.10.2026 19:18, Junio C Hamano wrote:\n> Aniket Gupta <aniketgupta3001@gmail.com> writes:\n> >> Hello Xiangfei,\n>>\n>>> +\tint pat_len = ASN1_STRING_length(asn1_str);\n>>\n>> It seems that with OpenSSL 4.1.0-beta2 this is deprecated\n>> (ASN1_STRING_get_length() is the replacement), so it warns, and\n>> fails with DEVELOPER=1. The patch adds a second use at the error\n>> message. Beat Bolli has a series from 7 Sept (\"imap-send: future\n>> proofing and two correctness fixes\") that also touches\n>> host_matches(), maybe we can check how the two can fit together?\n>>\n>> Thanks,\n>> Aniket\n> > [jch: rewrapped long unreadable lines]\n> > That topic also needs rework, unfortunately.\n> >    https://lore.kernel.org/git/20260907211210.2621693-1-dev+git@drbeat.li/\n> > I guess we can salvage the patches 1 and 2 with fairly trivial\n> changes, namely with the style fix in [1/3], and pointer arithmetic\n> that breaks later free() fix in [2/3].  I am not sure how involved\n> it would be to fix [3/3] in such a way suggested by brian in the\n> thread.\n> > Thanks for reminding us of the old thread.\n> I'm working on a new branch that uses OpenSSL's built-in hostname\nverification. All this low level ASN.1 code will disappear as a result.\n\nBest regards, Beat\n\n"},{"id":"555397","messageId":"CABtdbsmUU-J+Ytdno36hBH2acjT_1Ee6dQ2DeCAXhwaZ910Paw@mail.gmail.com","threadId":"66505","inReplyTo":"5d5dfa51-abc3-4a8f-badd-48ee398ad73f@drbeat.li","subject":"Re: [PATCH] imap-send: use ASN1_STRING_length","fromName":"Xiangfei Ding","fromEmail":"xfding@google.com","sentAt":"2026-10-19T08:48:48Z","receivedAt":"2026-10-19T08:48:48Z","isPatch":true,"sender":{"key":"xfding@google.com","avatar":null},"body":"Dear all,\n\nAh sorry, this is buried in my mailbox. Many apologies for the late reply.\n\n> On 12.10.2026 19:18, Junio C Hamano wrote:\n\n>> What are these two separate sign-off lines doing?\n\nI contributed to the kernel under the other email address. I shall use\nthis email address for future correspondence and sign-offs to avoid\nconfusion.\n\nI have finished reviewing the patch from Beat. Since I really prefer\nthat approach, I will retract this patch.\n\nHave a nice day, git.\n\nBest Regards,\nXiangfei Ding\n\nSigned-off-by: Xiangfei Ding <xfding@google.com>\n\nOn Mon, Oct 12, 2026 at 10:09 PM Beat Bolli <dev+git@drbeat.li> wrote:\n>\n> On 12.10.2026 19:18, Junio C Hamano wrote:\n> > Aniket Gupta <aniketgupta3001@gmail.com> writes:\n> >\n> >> Hello Xiangfei,\n> >>\n> >>> +   int pat_len = ASN1_STRING_length(asn1_str);\n> >>\n> >> It seems that with OpenSSL 4.1.0-beta2 this is deprecated\n> >> (ASN1_STRING_get_length() is the replacement), so it warns, and\n> >> fails with DEVELOPER=1. The patch adds a second use at the error\n> >> message. Beat Bolli has a series from 7 Sept (\"imap-send: future\n> >> proofing and two correctness fixes\") that also touches\n> >> host_matches(), maybe we can check how the two can fit together?\n> >>\n> >> Thanks,\n> >> Aniket\n> >\n> > [jch: rewrapped long unreadable lines]\n> >\n> > That topic also needs rework, unfortunately.\n> >\n> >    https://lore.kernel.org/git/20260907211210.2621693-1-dev+git@drbeat.li/\n> >\n> > I guess we can salvage the patches 1 and 2 with fairly trivial\n> > changes, namely with the style fix in [1/3], and pointer arithmetic\n> > that breaks later free() fix in [2/3].  I am not sure how involved\n> > it would be to fix [3/3] in such a way suggested by brian in the\n> > thread.\n> >\n> > Thanks for reminding us of the old thread.\n> >\n>\n> I'm working on a new branch that uses OpenSSL's built-in hostname\n> verification. All this low level ASN.1 code will disappear as a result.\n>\n> Best regards, Beat\n\n"}]}