Alternative TLS implementation in Go
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.8 KiB

  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. )
  13. // zeroSource is an io.Reader that returns an unlimited number of zero bytes.
  14. type zeroSource struct{}
  15. func (zeroSource) Read(b []byte) (n int, err error) {
  16. for i := range b {
  17. b[i] = 0
  18. }
  19. return len(b), nil
  20. }
  21. func ExampleDial() {
  22. // Connecting with a custom root-certificate set.
  23. const rootPEM = `
  24. -----BEGIN CERTIFICATE-----
  25. MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
  26. MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
  27. YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
  28. EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy
  29. bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
  30. AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0C1NP
  31. VaTOgmKV7utZX8bhBYASxF6UP7xbSDj0U/ck5vuR6RXEz/RTDfRK/J9U3n2+oGtv
  32. h8DQUB8oMANA2ghzUWx//zo8pzcGjr1LEQTrfSTe5vn8MXH7lNVg8y5Kr0LSy+rE
  33. ahqyzFPdFUuLH8gZYR/Nnag+YyuENWllhMgZxUYi+FOVvuOAShDGKuy6lyARxzmZ
  34. EASg8GF6lSWMTlJ14rbtCMoU/M4iarNOz0YDl5cDfsCx3nuvRTPPuj5xt970JSXC
  35. DTWJnZ37DhF5iR43xa+OcmkCAwEAAaOB+zCB+DAfBgNVHSMEGDAWgBTAephojYn7
  36. qwVkDBF9qn1luMrMTjAdBgNVHQ4EFgQUSt0GFhu89mi1dvWBtrtiGrpagS8wEgYD
  37. VR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMwMTAvoC2g
  38. K4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9ndGdsb2JhbC5jcmwwPQYI
  39. KwYBBQUHAQEEMTAvMC0GCCsGAQUFBzABhiFodHRwOi8vZ3RnbG9iYWwtb2NzcC5n
  40. ZW90cnVzdC5jb20wFwYDVR0gBBAwDjAMBgorBgEEAdZ5AgUBMA0GCSqGSIb3DQEB
  41. BQUAA4IBAQA21waAESetKhSbOHezI6B1WLuxfoNCunLaHtiONgaX4PCVOzf9G0JY
  42. /iLIa704XtE7JW4S615ndkZAkNoUyHgN7ZVm2o6Gb4ChulYylYbc3GrKBIxbf/a/
  43. zG+FA1jDaFETzf3I93k9mTXwVqO94FntT0QJo544evZG0R0SnU++0ED8Vf4GXjza
  44. HFa9llF7b1cq26KqltyMdMKVvvBulRP/F/A8rLIQjcxz++iPAsbw+zOzlTvjwsto
  45. WHPbqCRiOwY1nQ2pM714A5AuTHhdUDqB1O6gyHA43LL5Z/qHQF1hwFGPa4NrzQU6
  46. yuGnBXj8ytqU0CwIPX4WecigUCAkVDNx
  47. -----END CERTIFICATE-----`
  48. // First, create the set of root certificates. For this example we only
  49. // have one. It's also possible to omit this in order to use the
  50. // default root set of the current operating system.
  51. roots := x509.NewCertPool()
  52. ok := roots.AppendCertsFromPEM([]byte(rootPEM))
  53. if !ok {
  54. panic("failed to parse root certificate")
  55. }
  56. conn, err := tls.Dial("tcp", "mail.google.com:443", &tls.Config{
  57. RootCAs: roots,
  58. })
  59. if err != nil {
  60. panic("failed to connect: " + err.Error())
  61. }
  62. conn.Close()
  63. }
  64. func ExampleConfig_keyLogWriter_TLS12() {
  65. // Debugging TLS applications by decrypting a network traffic capture.
  66. // WARNING: Use of KeyLogWriter compromises security and should only be
  67. // used for debugging.
  68. // Dummy test HTTP server for the example with insecure random so output is
  69. // reproducible.
  70. server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
  71. server.TLS = &tls.Config{
  72. Rand: zeroSource{}, // for example only; don't do this.
  73. MaxVersion: tls.VersionTLS12,
  74. }
  75. server.StartTLS()
  76. defer server.Close()
  77. // Typically the log would go to an open file:
  78. // w, err := os.OpenFile("tls-secrets.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  79. w := os.Stdout
  80. client := &http.Client{
  81. Transport: &http.Transport{
  82. TLSClientConfig: &tls.Config{
  83. KeyLogWriter: w,
  84. Rand: zeroSource{}, // for reproducible output; don't do this.
  85. InsecureSkipVerify: true, // test server certificate is not trusted.
  86. },
  87. },
  88. }
  89. resp, err := client.Get(server.URL)
  90. if err != nil {
  91. log.Fatalf("Failed to get URL: %v", err)
  92. }
  93. resp.Body.Close()
  94. // The resulting file can be used with Wireshark to decrypt the TLS
  95. // connection by setting (Pre)-Master-Secret log filename in SSL Protocol
  96. // preferences.
  97. // Output:
  98. // CLIENT_RANDOM 0000000000000000000000000000000000000000000000000000000000000000 baca0df460a688e44ce018b025183cc2353ae01f89755ef766eedd3ecc302888ee3b3a22962e45f48c20df15a98c0e80
  99. }
  100. func ExampleConfig_keyLogWriter_TLS13() {
  101. // Debugging TLS applications by decrypting a network traffic capture.
  102. // WARNING: Use of KeyLogWriter compromises security and should only be
  103. // used for debugging.
  104. // Dummy test HTTP server for the example with insecure random so output is
  105. // reproducible.
  106. server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
  107. server.TLS = &tls.Config{
  108. Rand: zeroSource{}, // for example only; don't do this.
  109. }
  110. server.StartTLS()
  111. defer server.Close()
  112. // Typically the log would go to an open file:
  113. // w, err := os.OpenFile("tls-secrets.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  114. w := os.Stdout
  115. client := &http.Client{
  116. Transport: &http.Transport{
  117. TLSClientConfig: &tls.Config{
  118. KeyLogWriter: w,
  119. Rand: zeroSource{}, // for reproducible output; don't do this.
  120. InsecureSkipVerify: true, // test server certificate is not trusted.
  121. },
  122. },
  123. }
  124. resp, err := client.Get(server.URL)
  125. if err != nil {
  126. log.Fatalf("Failed to get URL: %v", err)
  127. }
  128. resp.Body.Close()
  129. // The resulting file can be used with Wireshark to decrypt the TLS
  130. // connection by setting (Pre)-Master-Secret log filename in SSL Protocol
  131. // preferences.
  132. // Output:
  133. // CLIENT_HANDSHAKE_TRAFFIC_SECRET 0000000000000000000000000000000000000000000000000000000000000000 16ca97d21087a14d406b2601b4713dd82b156cc01d54665baaa4bdb62b72b9a4
  134. // SERVER_HANDSHAKE_TRAFFIC_SECRET 0000000000000000000000000000000000000000000000000000000000000000 102c68d960da4f5e2b76a99636ac07bb5774e43b8ce8c14aa4dfd9bf54d11754
  135. // SERVER_TRAFFIC_SECRET_0 0000000000000000000000000000000000000000000000000000000000000000 f3208d533bb885f32f52142acb484eed104739970c2f426e72a1ee31f6d28650
  136. // CLIENT_TRAFFIC_SECRET_0 0000000000000000000000000000000000000000000000000000000000000000 70de6b1936df7db171c02f9cfdb04dfa9405a891c959beb15b86f26b2057ba23
  137. }