Bug 208192 - [Curl] Add TLS debugging feature to log encryption keys
Summary: [Curl] Add TLS debugging feature to log encryption keys
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Platform (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Fujii Hironori
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-02-25 06:08 PST by Takashi Komori
Modified: 2020-03-03 16:30 PST (History)
18 users (show)

See Also:


Attachments
Add TLS debugging feature. (5.89 KB, patch)
2020-02-25 06:13 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (8.73 KB, patch)
2020-02-25 23:47 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (6.81 KB, patch)
2020-02-26 00:59 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (8.73 KB, patch)
2020-02-26 01:32 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (9.57 KB, patch)
2020-02-27 06:12 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (9.50 KB, patch)
2020-02-28 00:53 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
[Patch] Changed the default ENABLE_TLS_DEBUG to OFF (2.11 KB, patch)
2020-03-01 16:39 PST, Fujii Hironori
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Takashi Komori 2020-02-25 06:08:43 PST
Chrome has a debugging feature which enables people to record keys which is used for TLS encryption.
In this ticket we will append almost the same feature to Curl port.
Comment 1 Takashi Komori 2020-02-25 06:13:18 PST
Created attachment 391645 [details]
Add TLS debugging feature.
Comment 2 Takashi Komori 2020-02-25 06:17:27 PST
This patch adds TLS debugging feature which writes TLS key log to the file which is specified by environment variable "WEBKIT_CURL_TLS_KEY_LOG_FILE"
If we have the key log file and packet dump, we can decrypt the TLS packets which browser sent/received.
Outputted key log file follows the NSS key log format, and it supports less or equal to TLS version 1.2

 https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format

This feature is enabled when only we enable ENABLE_TLS_DEBUG flag on build time as below.

>perl .\Tools\Scripts\build-webkit --wincairo --cmakeargs="-DENABLE_TLS_DEBUG=1"

Chrome also has the almost the same feature, it outputs the key log file to the path which is specified by environment variable "SSLKEYLOGFILE"
Comment 3 Takashi Komori 2020-02-25 06:21:57 PST
Usage:
1) Build wincairo with ENABLE_TLS_DEBUG=1
2) Set environment variable WEBKIT_CURL_TLS_KEY_LOG_FILE to a path which you have write privilege.
3) Start WireShark and start capturing packets.
4) Start MiniBrowser and brows some https sites.
5) Stop capturing packets.
6) Open Setting -> preference -> Protocols -> tls on WireShark
7) Set the path you specified to WEBKIT_CURLL_TLS_KEY_LOG_FILE to (Pre)=Master-Secret log filename.
8) You can see decrypted packets on WireShark.
Comment 4 Fujii Hironori 2020-02-25 06:50:24 PST
Can we include this feature in release builds like Firefox and Chrome?
Firefox and Chrome are using same name env bar. Can we use the same name?
Comment 5 Fujii Hironori 2020-02-25 07:05:16 PST
Comment on attachment 391645 [details]
Add TLS debugging feature.

View in context: https://bugs.webkit.org/attachment.cgi?id=391645&action=review

> Source/WebCore/platform/network/curl/CurlContext.h:131
> +    bool shouldLogTlsKey() const { return !m_tlsKeyLogFilePath.isEmpty(); }

‘Tls’ seems to violate WebKit coding style. https://webkit.org/code-style-guidelines/#names-basic
TLS is better?

> Source/WebCore/platform/network/curl/CurlSSLVerifier.cpp:123
> +    String line("CLIENT_RANDOM ");

do you need this temporary string? Why don’t you output by using fprintf?
Comment 6 Fujii Hironori 2020-02-25 16:21:45 PST
(In reply to Fujii Hironori from comment #4)
> Can we include this feature in release builds like Firefox and Chrome?

I meant production builds.
Comment 7 Takashi Komori 2020-02-25 23:47:48 PST
Created attachment 391724 [details]
Patch
Comment 8 Takashi Komori 2020-02-25 23:50:23 PST
(In reply to Fujii Hironori from comment #4)
> Can we include this feature in release builds like Firefox and Chrome?
> Firefox and Chrome are using same name env bar. Can we use the same name?

Included in production build and changed the environment variable name to SSLKEYLOGFILE.
Comment 9 Takashi Komori 2020-02-25 23:50:54 PST
(In reply to Fujii Hironori from comment #5)
> Comment on attachment 391645 [details]
> Add TLS debugging feature.
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=391645&action=review
> 
> > Source/WebCore/platform/network/curl/CurlContext.h:131
> > +    bool shouldLogTlsKey() const { return !m_tlsKeyLogFilePath.isEmpty(); }
> 
> ‘Tls’ seems to violate WebKit coding style.
> https://webkit.org/code-style-guidelines/#names-basic
> TLS is better?

Fixed.

> 
> > Source/WebCore/platform/network/curl/CurlSSLVerifier.cpp:123
> > +    String line("CLIENT_RANDOM ");
> 
> do you need this temporary string? Why don’t you output by using fprintf?

Fixed.
Comment 10 Fujii Hironori 2020-02-26 00:00:31 PST
Comment on attachment 391724 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=391724&action=review

> Source/cmake/OptionsWinCairo.cmake:56
> +WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)

Do you want to disable this feature? If not, remove all #if ENABLE(TLS_DEBUG).
Comment 11 Takashi Komori 2020-02-26 00:59:00 PST
Created attachment 391727 [details]
Patch
Comment 12 Takashi Komori 2020-02-26 00:59:50 PST
(In reply to Fujii Hironori from comment #10)
> Comment on attachment 391724 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=391724&action=review
> 
> > Source/cmake/OptionsWinCairo.cmake:56
> > +WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)
> 
> Do you want to disable this feature? If not, remove all #if
> ENABLE(TLS_DEBUG).

Removed.
Comment 13 Takashi Komori 2020-02-26 01:11:31 PST
(In reply to Takashi Komori from comment #12)
> (In reply to Fujii Hironori from comment #10)
> > Comment on attachment 391724 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=391724&action=review
> > 
> > > Source/cmake/OptionsWinCairo.cmake:56
> > > +WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)
> > 
> > Do you want to disable this feature? If not, remove all #if
> > ENABLE(TLS_DEBUG).
> 
> Removed.

Removed but some platform might want disabling option for a security perspective.
Comment 14 Takashi Komori 2020-02-26 01:32:55 PST
Created attachment 391729 [details]
Patch
Comment 15 Takashi Komori 2020-02-26 01:33:53 PST
Logging encryption keys feature could be a great security hole.
Some developers who use WebKit for example for some embedded system might concern that there is no way to disable the feature.
Considering that, we would better offer the disabling option.
Comment 16 Fujii Hironori 2020-02-26 02:25:49 PST
How do Chrome and Firefox deal with the great security hole?
Comment 17 Takashi Komori 2020-02-26 03:21:34 PST
(In reply to Fujii Hironori from comment #16)
> How do Chrome and Firefox deal with the great security hole?

Other browsers don't seem to have a strong safety guard for this feature.
In other words just setting the environment variable makes browsers start recording encryption keys into local PC.

If the recorded keys is secure and not stolen, the feature itself is secure too.
But we shouldn't assume all systems which use WebKit are implemented right and secure.
So I think offering developers the disabling option is reasonable.

Also I'm concerning browsers don't have any explicit way to reset or remove recorded keys.
Comment 18 Fujii Hironori 2020-02-26 12:25:34 PST
(In reply to Takashi Komori from comment #17)
> (In reply to Fujii Hironori from comment #16)
> > How do Chrome and Firefox deal with the great security hole?
> 
> Other browsers don't seem to have a strong safety guard for this feature.
> In other words just setting the environment variable makes browsers start
> recording encryption keys into local PC.
> 
> If the recorded keys is secure and not stolen, the feature itself is secure
> too.
> But we shouldn't assume all systems which use WebKit are implemented right
> and secure.
> So I think offering developers the disabling option is reasonable.

If it's possible for someone to steal file from PC, it's impossible to make the browser safe.
 
> Also I'm concerning browsers don't have any explicit way to reset or remove
> recorded keys.

I think it's enough to invoke command `rm $SSLKEYLOGFILE`.
Comment 19 Alex Christensen 2020-02-26 12:41:37 PST
Comment on attachment 391729 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=391729&action=review

> Source/WebCore/platform/network/curl/CurlSSLVerifier.cpp:122
> +    auto fp = fopen(CurlContext::singleton().tlsKeyLogFilePath().utf8().data(), "a");

Are you sure you want all this file I/O on the main thread?
Is there an advantage to using wtf/FileSystem.h?

> Source/WebCore/platform/network/curl/CurlSSLVerifier.cpp:126
> +    fprintf(fp, "CLIENT_RANDOM ");

Do you care about the return code?

> Source/cmake/OptionsWinCairo.cmake:56
> +WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)

You probably want to have this off by default.
Comment 20 Fujii Hironori 2020-02-26 15:53:21 PST
Comment on attachment 391729 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=391729&action=review

> ChangeLog:16
> +        * Source/cmake/OptionsWinCairo.cmake:

PlayStation port is also using curl. Don't you change OptionsPlayStation.cmake, too?
Comment 21 Takashi Komori 2020-02-27 06:12:12 PST
Created attachment 391853 [details]
Patch
Comment 22 Takashi Komori 2020-02-27 06:25:24 PST
(In reply to Alex Christensen from comment #19)
> Comment on attachment 391729 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=391729&action=review
> 
> > Source/WebCore/platform/network/curl/CurlSSLVerifier.cpp:122
> > +    auto fp = fopen(CurlContext::singleton().tlsKeyLogFilePath().utf8().data(), "a");
> 
> Are you sure you want all this file I/O on the main thread?
> Is there an advantage to using wtf/FileSystem.h?

No, CurlSSLVerifier::infoCallback is invoked as a callback function in sub thread.

> 
> > Source/WebCore/platform/network/curl/CurlSSLVerifier.cpp:126
> > +    fprintf(fp, "CLIENT_RANDOM ");
> 
> Do you care about the return code?
> 

I think it doesn't seem to be necessary caring the return code.
Other parts using fprintf in WebKit also doesn't care.

> > Source/cmake/OptionsWinCairo.cmake:56
> > +WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)
> 
> You probably want to have this off by default.

Yes.
Even if the ON is preferable by default, disabling option could be useful for some developers.
Comment 23 Takashi Komori 2020-02-27 06:25:58 PST
(In reply to Fujii Hironori from comment #20)
> Comment on attachment 391729 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=391729&action=review
> 
> > ChangeLog:16
> > +        * Source/cmake/OptionsWinCairo.cmake:
> 
> PlayStation port is also using curl. Don't you change
> OptionsPlayStation.cmake, too?

Added.
Comment 24 Takashi Komori 2020-02-27 06:44:34 PST
(In reply to Alex Christensen from comment #19)
> Comment on attachment 391729 [details]
> Patch
> 
> Are you sure you want all this file I/O on the main thread?
> Is there an advantage to using wtf/FileSystem.h?
> 

wtf/FileSystem.h doesn't have a mode which appends at tail of a file.
If we use FileSystem.h for logging, we have to keep open PlatformFileHandle as we don't have append mode.
On Windows platform, it means the key log file remains open until the browser exits, and we can't access the log file until  the browser exits.

We might have to consider adding appending mode in another ticket.
Comment 25 Fujii Hironori 2020-02-27 12:15:10 PST
Comment on attachment 391853 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=391853&action=review

LGTM

> Source/cmake/OptionsWinCairo.cmake:58
> +WEBKIT_OPTION_END()

WEBKIT_OPTION_END is already called in OptionsWin.cmake for WinCairo.
It seems that WEBKIT_OPTION_END shouldn't be called twice because FEATURE_DEFINES will get duplicated entries.
I think it should be moved into OptionsWin.cmake.

if (WTF_PLATFORM_WIN_CAIRO)
    WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)
endif ()
Comment 26 Takashi Komori 2020-02-28 00:53:27 PST
Created attachment 391968 [details]
Patch
Comment 27 Takashi Komori 2020-02-28 00:54:13 PST
(In reply to Fujii Hironori from comment #25)
> WEBKIT_OPTION_END is already called in OptionsWin.cmake for WinCairo.
> It seems that WEBKIT_OPTION_END shouldn't be called twice because
> FEATURE_DEFINES will get duplicated entries.
> I think it should be moved into OptionsWin.cmake.
> 
> if (WTF_PLATFORM_WIN_CAIRO)
>     WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support"
> PRIVATE ON)
> endif ()

Fixed.
Comment 28 WebKit Commit Bot 2020-02-28 13:35:14 PST
Comment on attachment 391968 [details]
Patch

Clearing flags on attachment: 391968

Committed r257656: <https://trac.webkit.org/changeset/257656>
Comment 29 WebKit Commit Bot 2020-02-28 13:35:17 PST
All reviewed patches have been landed.  Closing bug.
Comment 30 Radar WebKit Bug Importer 2020-02-28 13:36:15 PST
<rdar://problem/59900616>
Comment 31 Alex Christensen 2020-02-28 14:45:10 PST
Comment on attachment 391968 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=391968&action=review

> Source/cmake/OptionsWin.cmake:76
> +    WEBKIT_OPTION_DEFINE(ENABLE_TLS_DEBUG "Enable TLS key log support" PRIVATE ON)

Again, this should really be off by default.  Please change it.  If someone who isn't an expert in TLS or WebKit builds WebKit, we want them to be safe.
Comment 32 Fujii Hironori 2020-03-01 16:39:01 PST
Reopening to attach new patch.
Comment 33 Fujii Hironori 2020-03-01 16:39:03 PST
Created attachment 392109 [details]
[Patch] Changed the default ENABLE_TLS_DEBUG to OFF
Comment 34 WebKit Commit Bot 2020-03-03 16:30:18 PST
Comment on attachment 392109 [details]
[Patch] Changed the default ENABLE_TLS_DEBUG to OFF

Clearing flags on attachment: 392109

Committed r257813: <https://trac.webkit.org/changeset/257813>
Comment 35 WebKit Commit Bot 2020-03-03 16:30:20 PST
All reviewed patches have been landed.  Closing bug.