Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

177 linhas
4.7 KiB

  1. // Copyright 2009 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. // This package partially implements the TLS 1.1 protocol, as specified in RFC 4346.
  5. package tls
  6. import (
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "encoding/pem"
  10. "io/ioutil"
  11. "net"
  12. "os"
  13. "strings"
  14. )
  15. // Server returns a new TLS server side connection
  16. // using conn as the underlying transport.
  17. // The configuration config must be non-nil and must have
  18. // at least one certificate.
  19. func Server(conn net.Conn, config *Config) *Conn {
  20. return &Conn{conn: conn, config: config}
  21. }
  22. // Client returns a new TLS client side connection
  23. // using conn as the underlying transport.
  24. // Client interprets a nil configuration as equivalent to
  25. // the zero configuration; see the documentation of Config
  26. // for the defaults.
  27. func Client(conn net.Conn, config *Config) *Conn {
  28. return &Conn{conn: conn, config: config, isClient: true}
  29. }
  30. // A Listener implements a network listener (net.Listener) for TLS connections.
  31. type Listener struct {
  32. listener net.Listener
  33. config *Config
  34. }
  35. // Accept waits for and returns the next incoming TLS connection.
  36. // The returned connection c is a *tls.Conn.
  37. func (l *Listener) Accept() (c net.Conn, err os.Error) {
  38. c, err = l.listener.Accept()
  39. if err != nil {
  40. return
  41. }
  42. c = Server(c, l.config)
  43. return
  44. }
  45. // Close closes the listener.
  46. func (l *Listener) Close() os.Error { return l.listener.Close() }
  47. // Addr returns the listener's network address.
  48. func (l *Listener) Addr() net.Addr { return l.listener.Addr() }
  49. // NewListener creates a Listener which accepts connections from an inner
  50. // Listener and wraps each connection with Server.
  51. // The configuration config must be non-nil and must have
  52. // at least one certificate.
  53. func NewListener(listener net.Listener, config *Config) (l *Listener) {
  54. l = new(Listener)
  55. l.listener = listener
  56. l.config = config
  57. return
  58. }
  59. // Listen creates a TLS listener accepting connections on the
  60. // given network address using net.Listen.
  61. // The configuration config must be non-nil and must have
  62. // at least one certificate.
  63. func Listen(network, laddr string, config *Config) (*Listener, os.Error) {
  64. if config == nil || len(config.Certificates) == 0 {
  65. return nil, os.NewError("tls.Listen: no certificates in configuration")
  66. }
  67. l, err := net.Listen(network, laddr)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return NewListener(l, config), nil
  72. }
  73. // Dial connects to the given network address using net.Dial
  74. // and then initiates a TLS handshake, returning the resulting
  75. // TLS connection.
  76. // Dial interprets a nil configuration as equivalent to
  77. // the zero configuration; see the documentation of Config
  78. // for the defaults.
  79. func Dial(network, addr string, config *Config) (*Conn, os.Error) {
  80. raddr := addr
  81. c, err := net.Dial(network, raddr)
  82. if err != nil {
  83. return nil, err
  84. }
  85. colonPos := strings.LastIndex(raddr, ":")
  86. if colonPos == -1 {
  87. colonPos = len(raddr)
  88. }
  89. hostname := raddr[:colonPos]
  90. if config == nil {
  91. config = defaultConfig()
  92. }
  93. if config.ServerName != "" {
  94. // Make a copy to avoid polluting argument or default.
  95. c := *config
  96. c.ServerName = hostname
  97. config = &c
  98. }
  99. conn := Client(c, config)
  100. if err = conn.Handshake(); err != nil {
  101. c.Close()
  102. return nil, err
  103. }
  104. return conn, nil
  105. }
  106. // LoadX509KeyPair reads and parses a public/private key pair from a pair of
  107. // files. The files must contain PEM encoded data.
  108. func LoadX509KeyPair(certFile string, keyFile string) (cert Certificate, err os.Error) {
  109. certPEMBlock, err := ioutil.ReadFile(certFile)
  110. if err != nil {
  111. return
  112. }
  113. var certDERBlock *pem.Block
  114. for {
  115. certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
  116. if certDERBlock == nil {
  117. break
  118. }
  119. if certDERBlock.Type == "CERTIFICATE" {
  120. cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
  121. }
  122. }
  123. if len(cert.Certificate) == 0 {
  124. err = os.ErrorString("crypto/tls: failed to parse certificate PEM data")
  125. return
  126. }
  127. keyPEMBlock, err := ioutil.ReadFile(keyFile)
  128. if err != nil {
  129. return
  130. }
  131. keyDERBlock, _ := pem.Decode(keyPEMBlock)
  132. if keyDERBlock == nil {
  133. err = os.ErrorString("crypto/tls: failed to parse key PEM data")
  134. return
  135. }
  136. key, err := x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
  137. if err != nil {
  138. err = os.ErrorString("crypto/tls: failed to parse key")
  139. return
  140. }
  141. cert.PrivateKey = key
  142. // We don't need to parse the public key for TLS, but we so do anyway
  143. // to check that it looks sane and matches the private key.
  144. x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
  145. if err != nil {
  146. return
  147. }
  148. if x509Cert.PublicKeyAlgorithm != x509.RSA || x509Cert.PublicKey.(*rsa.PublicKey).N.Cmp(key.PublicKey.N) != 0 {
  149. err = os.ErrorString("crypto/tls: private key does not match public key")
  150. return
  151. }
  152. return
  153. }