ticket.go 5.3 KB

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