example_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tls_test
  5. import (
  6. //"crypto/tls"
  7. "crypto/x509"
  8. //"log"
  9. //"net/http"
  10. //"net/http/httptest"
  11. //"os"
  12. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tls"
  13. )
  14. // zeroSource is an io.Reader that returns an unlimited number of zero bytes.
  15. type zeroSource struct{}
  16. func (zeroSource) Read(b []byte) (n int, err error) {
  17. for i := range b {
  18. b[i] = 0
  19. }
  20. return len(b), nil
  21. }
  22. func ExampleDial() {
  23. // Connecting with a custom root-certificate set.
  24. const rootPEM = `
  25. -----BEGIN CERTIFICATE-----
  26. MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
  27. MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
  28. YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
  29. EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy
  30. bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
  31. AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0C1NP
  32. VaTOgmKV7utZX8bhBYASxF6UP7xbSDj0U/ck5vuR6RXEz/RTDfRK/J9U3n2+oGtv
  33. h8DQUB8oMANA2ghzUWx//zo8pzcGjr1LEQTrfSTe5vn8MXH7lNVg8y5Kr0LSy+rE
  34. ahqyzFPdFUuLH8gZYR/Nnag+YyuENWllhMgZxUYi+FOVvuOAShDGKuy6lyARxzmZ
  35. EASg8GF6lSWMTlJ14rbtCMoU/M4iarNOz0YDl5cDfsCx3nuvRTPPuj5xt970JSXC
  36. DTWJnZ37DhF5iR43xa+OcmkCAwEAAaOB+zCB+DAfBgNVHSMEGDAWgBTAephojYn7
  37. qwVkDBF9qn1luMrMTjAdBgNVHQ4EFgQUSt0GFhu89mi1dvWBtrtiGrpagS8wEgYD
  38. VR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMwMTAvoC2g
  39. K4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9ndGdsb2JhbC5jcmwwPQYI
  40. KwYBBQUHAQEEMTAvMC0GCCsGAQUFBzABhiFodHRwOi8vZ3RnbG9iYWwtb2NzcC5n
  41. ZW90cnVzdC5jb20wFwYDVR0gBBAwDjAMBgorBgEEAdZ5AgUBMA0GCSqGSIb3DQEB
  42. BQUAA4IBAQA21waAESetKhSbOHezI6B1WLuxfoNCunLaHtiONgaX4PCVOzf9G0JY
  43. /iLIa704XtE7JW4S615ndkZAkNoUyHgN7ZVm2o6Gb4ChulYylYbc3GrKBIxbf/a/
  44. zG+FA1jDaFETzf3I93k9mTXwVqO94FntT0QJo544evZG0R0SnU++0ED8Vf4GXjza
  45. HFa9llF7b1cq26KqltyMdMKVvvBulRP/F/A8rLIQjcxz++iPAsbw+zOzlTvjwsto
  46. WHPbqCRiOwY1nQ2pM714A5AuTHhdUDqB1O6gyHA43LL5Z/qHQF1hwFGPa4NrzQU6
  47. yuGnBXj8ytqU0CwIPX4WecigUCAkVDNx
  48. -----END CERTIFICATE-----`
  49. // First, create the set of root certificates. For this example we only
  50. // have one. It's also possible to omit this in order to use the
  51. // default root set of the current operating system.
  52. roots := x509.NewCertPool()
  53. ok := roots.AppendCertsFromPEM([]byte(rootPEM))
  54. if !ok {
  55. panic("failed to parse root certificate")
  56. }
  57. conn, err := tls.Dial("tcp", "mail.google.com:443", &tls.Config{
  58. RootCAs: roots,
  59. })
  60. if err != nil {
  61. panic("failed to connect: " + err.Error())
  62. }
  63. conn.Close()
  64. }
  65. // [Psiphon]
  66. // Disable test due to TLSClientConfig type mismatch
  67. /*
  68. func ExampleConfig_keyLogWriter() {
  69. // Debugging TLS applications by decrypting a network traffic capture.
  70. // WARNING: Use of KeyLogWriter compromises security and should only be
  71. // used for debugging.
  72. // Dummy test HTTP server for the example with insecure random so output is
  73. // reproducible.
  74. server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
  75. server.TLS = &tls.Config{
  76. Rand: zeroSource{}, // for example only; don't do this.
  77. }
  78. server.StartTLS()
  79. defer server.Close()
  80. // Typically the log would go to an open file:
  81. // w, err := os.OpenFile("tls-secrets.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  82. w := os.Stdout
  83. client := &http.Client{
  84. Transport: &http.Transport{
  85. TLSClientConfig: &tls.Config{
  86. KeyLogWriter: w,
  87. Rand: zeroSource{}, // for reproducible output; don't do this.
  88. InsecureSkipVerify: true, // test server certificate is not trusted.
  89. },
  90. },
  91. }
  92. resp, err := client.Get(server.URL)
  93. if err != nil {
  94. log.Fatalf("Failed to get URL: %v", err)
  95. }
  96. resp.Body.Close()
  97. // The resulting file can be used with Wireshark to decrypt the TLS
  98. // connection by setting (Pre)-Master-Secret log filename in SSL Protocol
  99. // preferences.
  100. // Output:
  101. // CLIENT_RANDOM 0000000000000000000000000000000000000000000000000000000000000000 baca0df460a688e44ce018b025183cc2353ae01f89755ef766eedd3ecc302888ee3b3a22962e45f48c20df15a98c0e80
  102. }
  103. */