Alternative TLS implementation in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.6 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, laddr, raddr string, config *Config) (*Conn, os.Error) {
  80. c, err := net.Dial(network, laddr, raddr)
  81. if err != nil {
  82. return nil, err
  83. }
  84. colonPos := strings.LastIndex(raddr, ":")
  85. if colonPos == -1 {
  86. colonPos = len(raddr)
  87. }
  88. hostname := raddr[:colonPos]
  89. if config == nil {
  90. config = defaultConfig()
  91. }
  92. if config.ServerName != "" {
  93. // Make a copy to avoid polluting argument or default.
  94. c := *config
  95. c.ServerName = hostname
  96. config = &c
  97. }
  98. conn := Client(c, config)
  99. if err = conn.Handshake(); err != nil {
  100. c.Close()
  101. return nil, err
  102. }
  103. return conn, nil
  104. }
  105. // LoadX509KeyPair reads and parses a public/private key pair from a pair of
  106. // files. The files must contain PEM encoded data.
  107. func LoadX509KeyPair(certFile string, keyFile string) (cert Certificate, err os.Error) {
  108. certPEMBlock, err := ioutil.ReadFile(certFile)
  109. if err != nil {
  110. return
  111. }
  112. certDERBlock, _ := pem.Decode(certPEMBlock)
  113. if certDERBlock == nil {
  114. err = os.ErrorString("crypto/tls: failed to parse certificate PEM data")
  115. return
  116. }
  117. cert.Certificate = [][]byte{certDERBlock.Bytes}
  118. keyPEMBlock, err := ioutil.ReadFile(keyFile)
  119. if err != nil {
  120. return
  121. }
  122. keyDERBlock, _ := pem.Decode(keyPEMBlock)
  123. if keyDERBlock == nil {
  124. err = os.ErrorString("crypto/tls: failed to parse key PEM data")
  125. return
  126. }
  127. key, err := x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
  128. if err != nil {
  129. err = os.ErrorString("crypto/tls: failed to parse key")
  130. return
  131. }
  132. cert.PrivateKey = key
  133. // We don't need to parse the public key for TLS, but we so do anyway
  134. // to check that it looks sane and matches the private key.
  135. x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes)
  136. if err != nil {
  137. return
  138. }
  139. if x509Cert.PublicKeyAlgorithm != x509.RSA || x509Cert.PublicKey.(*rsa.PublicKey).N.Cmp(key.PublicKey.N) != 0 {
  140. err = os.ErrorString("crypto/tls: private key does not match public key")
  141. return
  142. }
  143. return
  144. }