Kris Kwiatkowski
d3e18f99e2
* includes AD in authentication check of TLS records As per 5.2 of TLS 1.3 draft-28, the additional data is record header. * tests: Update tests in order to support draft-28 * Interoperability: Updates NSS and BoringSSL versions to the one supporting draft-28 * Bogo: Updates revision number to use tests for draft-28 * FIX: makefile was using test-compat target instead of test-interop * DC test: constify * Use binary interface to encode in big-endian
163 lines
5.8 KiB
Go
163 lines
5.8 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package tls_test
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
)
|
|
|
|
// zeroSource is an io.Reader that returns an unlimited number of zero bytes.
|
|
type zeroSource struct{}
|
|
|
|
func (zeroSource) Read(b []byte) (n int, err error) {
|
|
for i := range b {
|
|
b[i] = 0
|
|
}
|
|
|
|
return len(b), nil
|
|
}
|
|
|
|
func ExampleDial() {
|
|
// Connecting with a custom root-certificate set.
|
|
|
|
const rootPEM = `
|
|
-----BEGIN CERTIFICATE-----
|
|
MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
|
|
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
|
|
YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
|
|
EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy
|
|
bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
|
|
AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0C1NP
|
|
VaTOgmKV7utZX8bhBYASxF6UP7xbSDj0U/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For this example we only
|
|
// have one. It's also possible to omit this in order to use the
|
|
// default root set of the current operating system.
|
|
roots := x509.NewCertPool()
|
|
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
|
|
if !ok {
|
|
panic("failed to parse root certificate")
|
|
}
|
|
|
|
conn, err := tls.Dial("tcp", "mail.google.com:443", &tls.Config{
|
|
RootCAs: roots,
|
|
})
|
|
if err != nil {
|
|
panic("failed to connect: " + err.Error())
|
|
}
|
|
conn.Close()
|
|
}
|
|
|
|
func ExampleConfig_keyLogWriter_TLS12() {
|
|
// Debugging TLS applications by decrypting a network traffic capture.
|
|
|
|
// WARNING: Use of KeyLogWriter compromises security and should only be
|
|
// used for debugging.
|
|
|
|
// Dummy test HTTP server for the example with insecure random so output is
|
|
// reproducible.
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
server.TLS = &tls.Config{
|
|
Rand: zeroSource{}, // for example only; don't do this.
|
|
MaxVersion: tls.VersionTLS12,
|
|
}
|
|
server.StartTLS()
|
|
defer server.Close()
|
|
|
|
// Typically the log would go to an open file:
|
|
// w, err := os.OpenFile("tls-secrets.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
w := os.Stdout
|
|
|
|
client := &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
KeyLogWriter: w,
|
|
|
|
Rand: zeroSource{}, // for reproducible output; don't do this.
|
|
InsecureSkipVerify: true, // test server certificate is not trusted.
|
|
},
|
|
},
|
|
}
|
|
resp, err := client.Get(server.URL)
|
|
if err != nil {
|
|
log.Fatalf("Failed to get URL: %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
|
|
// The resulting file can be used with Wireshark to decrypt the TLS
|
|
// connection by setting (Pre)-Master-Secret log filename in SSL Protocol
|
|
// preferences.
|
|
|
|
// Output:
|
|
// CLIENT_RANDOM 0000000000000000000000000000000000000000000000000000000000000000 baca0df460a688e44ce018b025183cc2353ae01f89755ef766eedd3ecc302888ee3b3a22962e45f48c20df15a98c0e80
|
|
}
|
|
|
|
func ExampleConfig_keyLogWriter_TLS13() {
|
|
// Debugging TLS applications by decrypting a network traffic capture.
|
|
|
|
// WARNING: Use of KeyLogWriter compromises security and should only be
|
|
// used for debugging.
|
|
|
|
// Dummy test HTTP server for the example with insecure random so output is
|
|
// reproducible.
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
server.TLS = &tls.Config{
|
|
Rand: zeroSource{}, // for example only; don't do this.
|
|
}
|
|
server.StartTLS()
|
|
defer server.Close()
|
|
|
|
// Typically the log would go to an open file:
|
|
// w, err := os.OpenFile("tls-secrets.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
w := os.Stdout
|
|
|
|
client := &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
KeyLogWriter: w,
|
|
|
|
Rand: zeroSource{}, // for reproducible output; don't do this.
|
|
InsecureSkipVerify: true, // test server certificate is not trusted.
|
|
},
|
|
},
|
|
}
|
|
resp, err := client.Get(server.URL)
|
|
if err != nil {
|
|
log.Fatalf("Failed to get URL: %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
|
|
// The resulting file can be used with Wireshark to decrypt the TLS
|
|
// connection by setting (Pre)-Master-Secret log filename in SSL Protocol
|
|
// preferences.
|
|
|
|
// Output:
|
|
// CLIENT_HANDSHAKE_TRAFFIC_SECRET 0000000000000000000000000000000000000000000000000000000000000000 16ca97d21087a14d406b2601b4713dd82b156cc01d54665baaa4bdb62b72b9a4
|
|
// SERVER_HANDSHAKE_TRAFFIC_SECRET 0000000000000000000000000000000000000000000000000000000000000000 102c68d960da4f5e2b76a99636ac07bb5774e43b8ce8c14aa4dfd9bf54d11754
|
|
// SERVER_TRAFFIC_SECRET_0 0000000000000000000000000000000000000000000000000000000000000000 f3208d533bb885f32f52142acb484eed104739970c2f426e72a1ee31f6d28650
|
|
// CLIENT_TRAFFIC_SECRET_0 0000000000000000000000000000000000000000000000000000000000000000 70de6b1936df7db171c02f9cfdb04dfa9405a891c959beb15b86f26b2057ba23
|
|
}
|