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.
 
 
 
 
 
 

263 lines
6.3 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. package tls
  5. import (
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "io"
  10. "io/ioutil"
  11. "sync"
  12. "time"
  13. )
  14. const (
  15. maxPlaintext = 16384 // maximum plaintext payload length
  16. maxCiphertext = 16384 + 2048 // maximum ciphertext payload length
  17. recordHeaderLen = 5 // record header length
  18. maxHandshake = 65536 // maximum handshake we support (protocol max is 16 MB)
  19. minVersion = 0x0301 // minimum supported version - TLS 1.0
  20. maxVersion = 0x0301 // maximum supported version - TLS 1.0
  21. )
  22. // TLS record types.
  23. type recordType uint8
  24. const (
  25. recordTypeChangeCipherSpec recordType = 20
  26. recordTypeAlert recordType = 21
  27. recordTypeHandshake recordType = 22
  28. recordTypeApplicationData recordType = 23
  29. )
  30. // TLS handshake message types.
  31. const (
  32. typeClientHello uint8 = 1
  33. typeServerHello uint8 = 2
  34. typeCertificate uint8 = 11
  35. typeServerKeyExchange uint8 = 12
  36. typeCertificateRequest uint8 = 13
  37. typeServerHelloDone uint8 = 14
  38. typeCertificateVerify uint8 = 15
  39. typeClientKeyExchange uint8 = 16
  40. typeFinished uint8 = 20
  41. typeCertificateStatus uint8 = 22
  42. typeNextProtocol uint8 = 67 // Not IANA assigned
  43. )
  44. // TLS compression types.
  45. const (
  46. compressionNone uint8 = 0
  47. )
  48. // TLS extension numbers
  49. var (
  50. extensionServerName uint16 = 0
  51. extensionStatusRequest uint16 = 5
  52. extensionSupportedCurves uint16 = 10
  53. extensionSupportedPoints uint16 = 11
  54. extensionNextProtoNeg uint16 = 13172 // not IANA assigned
  55. )
  56. // TLS Elliptic Curves
  57. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
  58. var (
  59. curveP256 uint16 = 23
  60. curveP384 uint16 = 24
  61. curveP521 uint16 = 25
  62. )
  63. // TLS Elliptic Curve Point Formats
  64. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-9
  65. var (
  66. pointFormatUncompressed uint8 = 0
  67. )
  68. // TLS CertificateStatusType (RFC 3546)
  69. const (
  70. statusTypeOCSP uint8 = 1
  71. )
  72. // Certificate types (for certificateRequestMsg)
  73. const (
  74. certTypeRSASign = 1 // A certificate containing an RSA key
  75. certTypeDSSSign = 2 // A certificate containing a DSA key
  76. certTypeRSAFixedDH = 3 // A certificate containing a static DH key
  77. certTypeDSSFixedDH = 4 // A certficiate containing a static DH key
  78. // Rest of these are reserved by the TLS spec
  79. )
  80. // ConnectionState records basic TLS details about the connection.
  81. type ConnectionState struct {
  82. HandshakeComplete bool
  83. CipherSuite uint16
  84. NegotiatedProtocol string
  85. NegotiatedProtocolIsMutual bool
  86. // the certificate chain that was presented by the other side
  87. PeerCertificates []*x509.Certificate
  88. }
  89. // A Config structure is used to configure a TLS client or server. After one
  90. // has been passed to a TLS function it must not be modified.
  91. type Config struct {
  92. // Rand provides the source of entropy for nonces and RSA blinding.
  93. // If Rand is nil, TLS uses the cryptographic random reader in package
  94. // crypto/rand.
  95. Rand io.Reader
  96. // Time returns the current time as the number of seconds since the epoch.
  97. // If Time is nil, TLS uses the system time.Seconds.
  98. Time func() int64
  99. // Certificates contains one or more certificate chains
  100. // to present to the other side of the connection.
  101. // Server configurations must include at least one certificate.
  102. Certificates []Certificate
  103. // RootCAs defines the set of root certificate authorities
  104. // that clients use when verifying server certificates.
  105. // If RootCAs is nil, TLS uses the host's root CA set.
  106. RootCAs *CASet
  107. // NextProtos is a list of supported, application level protocols.
  108. NextProtos []string
  109. // ServerName is included in the client's handshake to support virtual
  110. // hosting.
  111. ServerName string
  112. // AuthenticateClient controls whether a server will request a certificate
  113. // from the client. It does not require that the client send a
  114. // certificate nor does it require that the certificate sent be
  115. // anything more than self-signed.
  116. AuthenticateClient bool
  117. // CipherSuites is a list of supported cipher suites. If CipherSuites
  118. // is nil, TLS uses a list of suites supported by the implementation.
  119. CipherSuites []uint16
  120. }
  121. func (c *Config) rand() io.Reader {
  122. r := c.Rand
  123. if r == nil {
  124. return rand.Reader
  125. }
  126. return r
  127. }
  128. func (c *Config) time() int64 {
  129. t := c.Time
  130. if t == nil {
  131. t = time.Seconds
  132. }
  133. return t()
  134. }
  135. func (c *Config) rootCAs() *CASet {
  136. s := c.RootCAs
  137. if s == nil {
  138. s = defaultRoots()
  139. }
  140. return s
  141. }
  142. func (c *Config) cipherSuites() []uint16 {
  143. s := c.CipherSuites
  144. if s == nil {
  145. s = defaultCipherSuites()
  146. }
  147. return s
  148. }
  149. // A Certificate is a chain of one or more certificates, leaf first.
  150. type Certificate struct {
  151. Certificate [][]byte
  152. PrivateKey *rsa.PrivateKey
  153. }
  154. // A TLS record.
  155. type record struct {
  156. contentType recordType
  157. major, minor uint8
  158. payload []byte
  159. }
  160. type handshakeMessage interface {
  161. marshal() []byte
  162. unmarshal([]byte) bool
  163. }
  164. // mutualVersion returns the protocol version to use given the advertised
  165. // version of the peer.
  166. func mutualVersion(vers uint16) (uint16, bool) {
  167. if vers < minVersion {
  168. return 0, false
  169. }
  170. if vers > maxVersion {
  171. vers = maxVersion
  172. }
  173. return vers, true
  174. }
  175. var emptyConfig Config
  176. func defaultConfig() *Config {
  177. return &emptyConfig
  178. }
  179. // Possible certificate files; stop after finding one.
  180. // On OS X we should really be using the Directory Services keychain
  181. // but that requires a lot of Mach goo to get at. Instead we use
  182. // the same root set that curl uses.
  183. var certFiles = []string{
  184. "/etc/ssl/certs/ca-certificates.crt", // Linux etc
  185. "/usr/share/curl/curl-ca-bundle.crt", // OS X
  186. }
  187. var once sync.Once
  188. func defaultRoots() *CASet {
  189. once.Do(initDefaults)
  190. return varDefaultRoots
  191. }
  192. func defaultCipherSuites() []uint16 {
  193. once.Do(initDefaults)
  194. return varDefaultCipherSuites
  195. }
  196. func initDefaults() {
  197. initDefaultRoots()
  198. initDefaultCipherSuites()
  199. }
  200. var varDefaultRoots *CASet
  201. func initDefaultRoots() {
  202. roots := NewCASet()
  203. for _, file := range certFiles {
  204. data, err := ioutil.ReadFile(file)
  205. if err == nil {
  206. roots.SetFromPEM(data)
  207. break
  208. }
  209. }
  210. varDefaultRoots = roots
  211. }
  212. var varDefaultCipherSuites []uint16
  213. func initDefaultCipherSuites() {
  214. varDefaultCipherSuites = make([]uint16, len(cipherSuites))
  215. i := 0
  216. for id, _ := range cipherSuites {
  217. varDefaultCipherSuites[i] = id
  218. i++
  219. }
  220. }