4

I am trying to export/write the SSL master secret and keys to a file from the chromium browser. I would appreciate if someone could advice me how to do this.

To write the premaster secret we can simply export the SSLKEYLOGFILE variable in the environment. The premaster secrets can be used by wireshark to decrypt an HTTPS session.

The premaster secret is used to compute the master secret which is further used to create 6 keys - CLIENT_WRITE_MAC CLIENT_IV CLIENT_WRITE and 3 more for the server.

I want to output these keys to a file instead of the premaster secret.

I figured if I could use wireshark code to simply output it but this is more complex I believe the code for wireshark handling SSL packets and using the premaster secret is in here. github.com/boundary/wireshark/blob/master/epan/dissectors/packet-ssl-utils.c

Another way to proceed is to make changes to the chromium browser and compile it. I think changes need to be made here. https://code.google.com/p/chromium/codesearch#chromium/src/net/third_party/nss/ssl/derive.c&q=client_write_mac_secret&sq=package:chromium&type=cs&l=214

I was going through more source code and I found this file to be related.

https://code.google.com/p/chromium/codesearch#chromium/src/net/third_party/nss/ssl/sslsock.c&q=SSLKEYLOG&sq=package:chromium&dr=C&l=3569

Looking at the code above I notice that there are more environment variables that can be set. Does anybody know if the SSLDEBUG environment can be set in the same way as the SSLKEYLOG variable. Any other way or technique to do this would help also

I have not been able to successfully do export the keys so far.

2 Answers 2

2

The six keys are derived from the master secret, the client random and the server random. You can get the master secret and the client random from the SSLKEYLOGFILE. I suppose you can sniff the server random with Wireshark from the server key exchange message.

If you have those, you can call some library function or script on them to get the six keys.

The way this works is that a HMAC is computed over the random numbers and the master secret and the result is cut in parts to get the six secrets:

To generate the key material, compute

  key_block = PRF(SecurityParameters.master_secret,
                  "key expansion",
                  SecurityParameters.server_random +
                  SecurityParameters.client_random);

until enough output has been generated. Then, the key_block is
partitioned as follows:

  client_write_MAC_key[SecurityParameters.mac_key_length]
  server_write_MAC_key[SecurityParameters.mac_key_length]
  client_write_key[SecurityParameters.enc_key_length]
  server_write_key[SecurityParameters.enc_key_length]
  client_write_IV[SecurityParameters.fixed_iv_length]
  server_write_IV[SecurityParameters.fixed_iv_length]

More info:

1
  • Thanks. I read these documents and saw these functions as well. I figured if it would be possible to reuse some of ssldump's source code as well for creating the keys. But I would like this to be deployable and automated (I cant collect server random every time from wireshark) which is why I need to go a level lower. That is by writing a function in chrome that would output the keys or wireshark dumping the keys after taking as input the premaster secret file. I do not know how to do that.
    – Choi
    Commented Apr 7, 2016 at 14:33
1

I figured it out.
To do this, you need to download the latest version of Wireshark source code. I ran my test on Wireshark 2.0.1

You need to make changes to the file - /epan/dissectors/packet-ssl-utils.c in the Wireshark source folder.

Print the variables to a file from line 3179 - 3194.
You can find the Client write key, Server write key, Client MAC key, Server MAC key, Client IV and Server IV)

To write to a file in C use this

File: *fptr; fptr = fopen("directory you want to open a file in", "a+");

fprintf("data"); // this will write data to the file

Note - To do it a more objective way, change and create the following functions

void custom_ssl_print_data(const gchar* name, const guchar* data, size_t len){

//Write the following lines 

File *ssl_debug_file;

ssl_debug_file=fopen("directory you want to open the file in","a+");

//Copy original functionality from line 4927

}


void custom_ssl_print_string(const gchar* name, const StringInfo* data){

//Copy original functionality from line 4953
}

Now use these functions to export your keys to a file.

Go to the main wireshark source folder.
Run:

./autogen.sh
./configure
sudo make
sudo make install

and run Wireshark in the terminal. (You still need to feed wireshark the premaster secret file by exporting the SSLKEYLOGFILE environment variable)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .