Skip to content

Add SChannel TLS 1.3 support - #7784

Closed
wyattoday wants to merge 25 commits into
curl:masterfrom
wyattoday:schannel-ng
Closed

wyattoday wants to merge 25 commits into
curl:masterfrom
wyattoday:schannel-ng

Conversation

@wyattoday

@wyattoday wyattoday commented Sep 27, 2021 •

Copy link
Copy Markdown
Contributor

Add SChannel TLS 1.3 support to curl / libcurl.

Has detection at runtime to enable / disable support depending on version of Windows.

Also, added a new define to disable optional code: DISABLE_SCHANNEL_CLIENT_CERT (reduces compiled code size if that feature is not needed).

A bit more work needs to be done before this is "merge-ready", but TLS 1.3 support is working.


This depends on this PR being merged: #7727

Remaining work: Implement "set_ssl_ciphers" for SCH_CREDENTIALS. This is in the inverse of the SCHANNEL_CRED behavior.

Namely, we're disabling ciphers rather than enabling them. This will require a bit of thinking. I'll work on this this week, but if anyone wants to jump in and write that function, I'll be happy to accept the help.

Once that is implemented, everything else should be good to go. But I'll happily accept input on any of it.

…ible version checking). This fixes ALPN with executables without the proper manifest. Also begins support for TFO and TLS 1.3 on Windows.
Has detection at runtime to enable / disable support depending on version of Windows.

Also, add 2 new defines to disable redundant code: DISABLE_SCHANNEL_MANUAL_VERIFY (SChannel already handles verification internally) and DISABLE_SCHANNEL_CLIENT_CERT (reduces compiled code size if that feature is not needed).

A bit more work needs to be done before this is "merge-ready", but TLS 1.3 support is working.
Comment thread lib/vtls/schannel.c Outdated
if(curlx_verify_windows_version(10, 0, 17763, PLATFORM_WINNT,
VERSION_GREATER_THAN_EQUAL)) {

SCH_CREDENTIALS Credentials = { 0 };

@mback2k mback2k Sep 27, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally use lower-case names for local variables.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix those tomorrow, thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, had a free minute. Fixed now.

Comment thread lib/vtls/schannel.c Outdated
goto cleanup;
}
if(BACKEND->encdata_offset) {
if(BACKEND->encdata_offset && inbuf[1].cbBuffer) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated to TLS 1.3.

@wyattoday wyattoday Sep 27, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required. I spent a few days mulling over and debugging what exactly was happening. With the new SChannel support of SCH_CREDENTIALS, inbuf[1] can be empty while encdata_offset is not empty when receiving a SEC_I_RENEGOTIATE return code.

Unfortunately, MS's documentation on this behavior is slim, so I can't tell you why this is the case, just that it is the case.

Long story short: without this change SCH_CREDENTIALS support (and thus TLS 1.3 support) will fail.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, but what happens for applications not using TLS 1.3 following the old code path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same working behavior when using TLS 1.2 (or older) on either the SCH_CREDENTIALS or SCHANNEL_CRED code-paths.

@jay jay Sep 30, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this change effectively renders the check moot. I added the check in 3e7ec1e, but going through it just now I can't remember why. Maybe I thought it was not possible that encrypted data would be sent before the renegotiate completed? However my note in the commit message contradicts that guess:

- Do not recurse on renegotiation.

Instead we'll continue on to process any trailing encrypted data
received during the renegotiation only.

So I think my intention was to process that data.

edit: Update... on further review I actually did intend to error there because apparently I saw that's what chromium or microsoft did. Maybe at the time there was no scenario where it could happen. See this comment (Note I manually tested renegotiation later on, but we don't have an automated test for it).

@wyattoday wyattoday Sep 30, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I couldn't reproduce a case where SEC_I_RENEGOTIATE was received, BACKEND->encdata_offset was != 0 and it wasn't just faulty error handling. The code still handles the case where inbuf[1].cbBuffer == 0 and inbuf[3].cbBuffer != 0. But is that necessary (or is it a code-path that will never be executed)? I have no idea -- I couldn't reproduce a case that got that result.

Unfortunately that commit message and those PR discussions didn't get me any closer to reproducing a test-case.

Edit:

If there doesn't exist a test-case for this behavior, I'm fine with removing that chunk of code entirely:

        if(BACKEND->encdata_offset && inbuf[1].cbBuffer) {
          *err = CURLE_RECV_ERROR;
          infof(data, "schannel: can't renegotiate, "
                "encrypted data available");
          goto cleanup;
        }

All of the tests I've run on supported versions of Windows have worked fine without that check present.

Comment thread lib/vtls/schannel.c
!BACKEND->recv_sspi_close_notify) {
bool isWin2k = curlx_verify_windows_version(5, 0, PLATFORM_WINNT,

#if defined(_WIN32_WINNT) && _WIN32_WINNT <= 0x0500

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand the whole thing of combining runtime and compile time checks like this. What is the purpose here?

@wyattoday wyattoday Sep 27, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing dead code-paths (smaller compiled binary). This is inherited from the other PR that this PR depends on: #7727

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is worth the effort as it over-complicates the code and makes reading it a lot harder. Generally speaking we try to perform Windows specific checks during runtime, because it is common for a Windows application to be build on a one platform version and then shipped to a couple of others. Therefore compile-time checks do not make much sense with the Windows ecosystem.

@gvollant

Copy link
Copy Markdown
Contributor

Hello @wyattoday , I suggest you rebase and do a git fixup / squash to merge all (or somes of) your commit

Comment thread lib/vtls/schannel.h Outdated
#ifdef __MINGW32__
#include <_mingw.h>
#ifdef __MINGW64_VERSION_MAJOR
#if defined(__MINGW64_VERSION_MAJOR) && !defined(DISABLE_SCHANNEL_MANUAL_VERIFY)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the two new flags/options DISABLE_SCHANNEL_MANUAL_VERIFY and DISABLE_SCHANNEL_CLIENT_CERT should be done in a separate PR as this is unrelated to TLS 1.3.

(SChannel already handles verification internally)

Also this is not true for all versions of Windows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this is not true for all versions of Windows.

Really? From my research (again, documentation is spotty on MSDN), auto-verification has been supported since at least Windows 2000.

Either way, I can make a separate PR that adds those 2 options. Or better yet, adds the inverse of DISABLE_SCHANNEL_MANUAL_VERIFY (ENABLE_SCHANNEL_MANUAL_VERIFY) so that by-default end-users will be smaller binaries.

Would that ease the merging of this PR? If so, I'll do that.

@mback2k mback2k Sep 28, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the commit originally introducing manual verification: 1e4c57f

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so it was added for Windows CE. So, it makes sense to just enable manual verification for Windows CE only. So, do away with DISABLE_SCHANNEL_MANUAL_VERIFY and just use _WIN32_WCE (which looks like the original behavior implemented).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've removed DISABLE_SCHANNEL_MANUAL_VERIFY and used _WIN32_WCE as was originally written.

Comment thread lib/version_win32.c
#if !OLDER_THAN_WIN2K
#include <winnt.h>
#ifdef _MSC_VER
#pragma comment(lib, "ntdll.lib")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this PR, but I guess reviewing it will be a lot easier once #7727 is settled. Not necessarily merged as it is right now as I don't think the discussion about ntdll is finished.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. That's why I made the other PR a couple of weeks ago, so there would be ample time to get that merged and then this reviewed.

Unfortunately we needed TLS 1.3 support on Windows and we couldn't wait any longer. We figured it would be better to share what we had immediately since Windows 11 will be released next week. And we didn't want other people wasting their time re-implementing the work we already did.

@mback2k

mback2k commented Sep 28, 2021

Copy link
Copy Markdown
Member

It seems like this is currently causing a lot of tests to fail on the various Windows CIs. This is definitely a blocker and needs to be fixed before we can consider this PR.

@vszakats

vszakats commented Oct 5, 2021

Copy link
Copy Markdown
Member

To have a clearer picture of what this PR does, I'd suggest rebasing it on @jay's #7810, and thus dropping all version-detection-related extra bits from this patch.

It'd be good to see why SCH_CREDENTIALS is necessary to enable TLS 1.3. "Normally" a client isn't required to do anything extra to use a newer TLS version, if the underlying TLS-layer starts supporting it. Microsoft says that TLS 1.3 should be supported by SChannel without extra settings. Anything in the contrary is possible of course, but the point is to only add code (esp. platform/version specific code), if absolutely necessary.

Winternl.h should lose the capitalization, otherwise it will not be found in cross-build situations where the build host uses a case-sensitive file system (e.g. Linux), where this header is distributed in full lowercase (i.e. mingw/WINE).

@wyattoday

wyattoday commented Oct 5, 2021 •

Copy link
Copy Markdown
Contributor Author

@vszakats

To have a clearer picture of what this PR does, I'd suggest rebasing it on @jay's #7810, and thus dropping all version-detection-related extra bits from this patch.

I will once that PR has landed and when I (or someone else) lands a PR adding build version checking.

Microsoft says that TLS 1.3 should be supported by SChannel without extra settings.

They don't really say that in that table, though. Unfortunately, as you've likely seen, the documentation is sparse. I remember reading it either in a Microsoft blog by a network-stack programmer or a comment on that blog by said programmer.

Either way, you can confirm it yourself by passing the TLS_1_3 flag to the old SCHANNEL_CRED struct. You'll see a failure at the negotiation stage.

Edit: @jay already tried this a year or so ago and got an error similar to what I described.

Winternl.h should lose the capitalization, otherwise it will not be found in cross-build situations where the build host uses a case-sensitive file system (e.g. Linux), where this header is distributed in full lowercase (i.e. mingw/WINE).

I'll fix that once the rebasing on the fixed version checking is done. Thanks!

jay pushed a commit to jay/curl that referenced this pull request Dec 1, 2021
Prior to this change the build number was not checked during version
comparison, and the platform id was supposed to be checked but wasn't.

Checking the build number is required for enabling "evergreen"
Windows 10/11 features (like TLS 1.3).

Ref: curl#7784

Closes curl#7824
jay pushed a commit that referenced this pull request Dec 2, 2021
Prior to this change the build number was not checked during version
comparison, and the platform id was supposed to be checked but wasn't.

Checking the build number is required for enabling "evergreen"
Windows 10/11 features (like TLS 1.3).

Ref: #7784

Closes #7824
Closes #7867
@bagder

bagder commented Jan 19, 2022

Copy link
Copy Markdown
Member

What's the current status of this work? It needs a rebase and force-push at the very least...

@wyattoday

Copy link
Copy Markdown
Contributor Author

I was planning on revisiting this Thursday or Friday this week. I'll start with a rebase on main ("master"), and then make the handful of changes requested throughout the thread.

@gvollant

Copy link
Copy Markdown
Contributor

I was planning on revisiting this Thursday or Friday this week. I'll start with a rebase on main ("master"), and then make the handful of changes requested throughout the thread.

Yes, please do a fast rebase and merge commit. This will allow review

@wyattoday

Copy link
Copy Markdown
Contributor Author

Close in favor of the new PR with completed work, build on "main".

#8419

@gvollant

Copy link
Copy Markdown
Contributor

after merging #8419, the next step is pehaps support msquic on schannel , see https://github.com/microsoft/msquic/blob/main/docs/Platforms.md

@bagder

bagder commented Feb 21, 2022

Copy link
Copy Markdown
Member

@nibanks has started working on adding support for msh3 to curl (nibanks#1). msh3 uses mquic, which should be an excellent step torwards getting h3 support in curl on Windows powered by Schannel.

@Jrarmyjr

This comment was marked as off-topic.

1 similar comment
@Jrarmyjr

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

TLS Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

7 participants