Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

201 Zeilen
4.7 KiB

  1. // Copyright 2012 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. "bytes"
  7. "crypto/aes"
  8. "crypto/cipher"
  9. "crypto/hmac"
  10. "crypto/sha256"
  11. "crypto/subtle"
  12. "errors"
  13. "io"
  14. )
  15. // sessionState contains the information that is serialized into a session
  16. // ticket in order to later resume a connection.
  17. type sessionState struct {
  18. vers uint16
  19. cipherSuite uint16
  20. masterSecret []byte
  21. certificates [][]byte
  22. // usedOldKey is true if the ticket from which this session came from
  23. // was encrypted with an older key and thus should be refreshed.
  24. usedOldKey bool
  25. }
  26. func (s *sessionState) equal(i interface{}) bool {
  27. s1, ok := i.(*sessionState)
  28. if !ok {
  29. return false
  30. }
  31. if s.vers != s1.vers ||
  32. s.cipherSuite != s1.cipherSuite ||
  33. !bytes.Equal(s.masterSecret, s1.masterSecret) {
  34. return false
  35. }
  36. if len(s.certificates) != len(s1.certificates) {
  37. return false
  38. }
  39. for i := range s.certificates {
  40. if !bytes.Equal(s.certificates[i], s1.certificates[i]) {
  41. return false
  42. }
  43. }
  44. return true
  45. }
  46. func (s *sessionState) marshal() []byte {
  47. length := 2 + 2 + 2 + len(s.masterSecret) + 2
  48. for _, cert := range s.certificates {
  49. length += 4 + len(cert)
  50. }
  51. ret := make([]byte, length)
  52. x := ret
  53. x[0] = byte(s.vers >> 8)
  54. x[1] = byte(s.vers)
  55. x[2] = byte(s.cipherSuite >> 8)
  56. x[3] = byte(s.cipherSuite)
  57. x[4] = byte(len(s.masterSecret) >> 8)
  58. x[5] = byte(len(s.masterSecret))
  59. x = x[6:]
  60. copy(x, s.masterSecret)
  61. x = x[len(s.masterSecret):]
  62. x[0] = byte(len(s.certificates) >> 8)
  63. x[1] = byte(len(s.certificates))
  64. x = x[2:]
  65. for _, cert := range s.certificates {
  66. x[0] = byte(len(cert) >> 24)
  67. x[1] = byte(len(cert) >> 16)
  68. x[2] = byte(len(cert) >> 8)
  69. x[3] = byte(len(cert))
  70. copy(x[4:], cert)
  71. x = x[4+len(cert):]
  72. }
  73. return ret
  74. }
  75. func (s *sessionState) unmarshal(data []byte) bool {
  76. if len(data) < 8 {
  77. return false
  78. }
  79. s.vers = uint16(data[0])<<8 | uint16(data[1])
  80. s.cipherSuite = uint16(data[2])<<8 | uint16(data[3])
  81. masterSecretLen := int(data[4])<<8 | int(data[5])
  82. data = data[6:]
  83. if len(data) < masterSecretLen {
  84. return false
  85. }
  86. s.masterSecret = data[:masterSecretLen]
  87. data = data[masterSecretLen:]
  88. if len(data) < 2 {
  89. return false
  90. }
  91. numCerts := int(data[0])<<8 | int(data[1])
  92. data = data[2:]
  93. s.certificates = make([][]byte, numCerts)
  94. for i := range s.certificates {
  95. if len(data) < 4 {
  96. return false
  97. }
  98. certLen := int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  99. data = data[4:]
  100. if certLen < 0 {
  101. return false
  102. }
  103. if len(data) < certLen {
  104. return false
  105. }
  106. s.certificates[i] = data[:certLen]
  107. data = data[certLen:]
  108. }
  109. return len(data) == 0
  110. }
  111. func (c *Conn) encryptTicket(state *sessionState) ([]byte, error) {
  112. serialized := state.marshal()
  113. encrypted := make([]byte, ticketKeyNameLen+aes.BlockSize+len(serialized)+sha256.Size)
  114. keyName := encrypted[:ticketKeyNameLen]
  115. iv := encrypted[ticketKeyNameLen : ticketKeyNameLen+aes.BlockSize]
  116. macBytes := encrypted[len(encrypted)-sha256.Size:]
  117. if _, err := io.ReadFull(c.config.rand(), iv); err != nil {
  118. return nil, err
  119. }
  120. key := c.config.ticketKeys()[0]
  121. copy(keyName, key.keyName[:])
  122. block, err := aes.NewCipher(key.aesKey[:])
  123. if err != nil {
  124. return nil, errors.New("tls: failed to create cipher while encrypting ticket: " + err.Error())
  125. }
  126. cipher.NewCTR(block, iv).XORKeyStream(encrypted[ticketKeyNameLen+aes.BlockSize:], serialized)
  127. mac := hmac.New(sha256.New, key.hmacKey[:])
  128. mac.Write(encrypted[:len(encrypted)-sha256.Size])
  129. mac.Sum(macBytes[:0])
  130. return encrypted, nil
  131. }
  132. func (c *Conn) decryptTicket(encrypted []byte) (*sessionState, bool) {
  133. if c.config.SessionTicketsDisabled ||
  134. len(encrypted) < ticketKeyNameLen+aes.BlockSize+sha256.Size {
  135. return nil, false
  136. }
  137. keyName := encrypted[:ticketKeyNameLen]
  138. iv := encrypted[ticketKeyNameLen : ticketKeyNameLen+aes.BlockSize]
  139. macBytes := encrypted[len(encrypted)-sha256.Size:]
  140. keys := c.config.ticketKeys()
  141. keyIndex := -1
  142. for i, candidateKey := range keys {
  143. if bytes.Equal(keyName, candidateKey.keyName[:]) {
  144. keyIndex = i
  145. break
  146. }
  147. }
  148. if keyIndex == -1 {
  149. return nil, false
  150. }
  151. key := &keys[keyIndex]
  152. mac := hmac.New(sha256.New, key.hmacKey[:])
  153. mac.Write(encrypted[:len(encrypted)-sha256.Size])
  154. expected := mac.Sum(nil)
  155. if subtle.ConstantTimeCompare(macBytes, expected) != 1 {
  156. return nil, false
  157. }
  158. block, err := aes.NewCipher(key.aesKey[:])
  159. if err != nil {
  160. return nil, false
  161. }
  162. ciphertext := encrypted[ticketKeyNameLen+aes.BlockSize : len(encrypted)-sha256.Size]
  163. plaintext := ciphertext
  164. cipher.NewCTR(block, iv).XORKeyStream(plaintext, ciphertext)
  165. state := &sessionState{usedOldKey: keyIndex > 0}
  166. ok := state.unmarshal(plaintext)
  167. return state, ok
  168. }