PageRenderTime 79ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/crypto/tls/handshake_messages_test.go

https://gitlab.com/OBSERVER-DLL/go
Go | 262 lines | 221 code | 32 blank | 9 comment | 32 complexity | 8b70dae723cb7034c4c3dabbb917ac28 MD5 | raw file
  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. "math/rand"
  7. "reflect"
  8. "testing"
  9. "testing/quick"
  10. )
  11. var tests = []interface{}{
  12. &clientHelloMsg{},
  13. &serverHelloMsg{},
  14. &finishedMsg{},
  15. &certificateMsg{},
  16. &certificateRequestMsg{},
  17. &certificateVerifyMsg{},
  18. &certificateStatusMsg{},
  19. &clientKeyExchangeMsg{},
  20. &nextProtoMsg{},
  21. &newSessionTicketMsg{},
  22. &sessionState{},
  23. }
  24. type testMessage interface {
  25. marshal() []byte
  26. unmarshal([]byte) bool
  27. equal(interface{}) bool
  28. }
  29. func TestMarshalUnmarshal(t *testing.T) {
  30. rand := rand.New(rand.NewSource(0))
  31. for i, iface := range tests {
  32. ty := reflect.ValueOf(iface).Type()
  33. n := 100
  34. if testing.Short() {
  35. n = 5
  36. }
  37. for j := 0; j < n; j++ {
  38. v, ok := quick.Value(ty, rand)
  39. if !ok {
  40. t.Errorf("#%d: failed to create value", i)
  41. break
  42. }
  43. m1 := v.Interface().(testMessage)
  44. marshaled := m1.marshal()
  45. m2 := iface.(testMessage)
  46. if !m2.unmarshal(marshaled) {
  47. t.Errorf("#%d failed to unmarshal %#v %x", i, m1, marshaled)
  48. break
  49. }
  50. m2.marshal() // to fill any marshal cache in the message
  51. if !m1.equal(m2) {
  52. t.Errorf("#%d got:%#v want:%#v %x", i, m2, m1, marshaled)
  53. break
  54. }
  55. if i >= 3 {
  56. // The first three message types (ClientHello,
  57. // ServerHello and Finished) are allowed to
  58. // have parsable prefixes because the extension
  59. // data is optional and the length of the
  60. // Finished varies across versions.
  61. for j := 0; j < len(marshaled); j++ {
  62. if m2.unmarshal(marshaled[0:j]) {
  63. t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1)
  64. break
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. func TestFuzz(t *testing.T) {
  72. rand := rand.New(rand.NewSource(0))
  73. for _, iface := range tests {
  74. m := iface.(testMessage)
  75. for j := 0; j < 1000; j++ {
  76. len := rand.Intn(100)
  77. bytes := randomBytes(len, rand)
  78. // This just looks for crashes due to bounds errors etc.
  79. m.unmarshal(bytes)
  80. }
  81. }
  82. }
  83. func randomBytes(n int, rand *rand.Rand) []byte {
  84. r := make([]byte, n)
  85. for i := 0; i < n; i++ {
  86. r[i] = byte(rand.Int31())
  87. }
  88. return r
  89. }
  90. func randomString(n int, rand *rand.Rand) string {
  91. b := randomBytes(n, rand)
  92. return string(b)
  93. }
  94. func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  95. m := &clientHelloMsg{}
  96. m.vers = uint16(rand.Intn(65536))
  97. m.random = randomBytes(32, rand)
  98. m.sessionId = randomBytes(rand.Intn(32), rand)
  99. m.cipherSuites = make([]uint16, rand.Intn(63)+1)
  100. for i := 0; i < len(m.cipherSuites); i++ {
  101. m.cipherSuites[i] = uint16(rand.Int31())
  102. }
  103. m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
  104. if rand.Intn(10) > 5 {
  105. m.nextProtoNeg = true
  106. }
  107. if rand.Intn(10) > 5 {
  108. m.serverName = randomString(rand.Intn(255), rand)
  109. }
  110. m.ocspStapling = rand.Intn(10) > 5
  111. m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)
  112. m.supportedCurves = make([]CurveID, rand.Intn(5)+1)
  113. for i := range m.supportedCurves {
  114. m.supportedCurves[i] = CurveID(rand.Intn(30000))
  115. }
  116. if rand.Intn(10) > 5 {
  117. m.ticketSupported = true
  118. if rand.Intn(10) > 5 {
  119. m.sessionTicket = randomBytes(rand.Intn(300), rand)
  120. }
  121. }
  122. if rand.Intn(10) > 5 {
  123. m.signatureAndHashes = supportedSignatureAlgorithms
  124. }
  125. m.alpnProtocols = make([]string, rand.Intn(5))
  126. for i := range m.alpnProtocols {
  127. m.alpnProtocols[i] = randomString(rand.Intn(20)+1, rand)
  128. }
  129. if rand.Intn(10) > 5 {
  130. m.scts = true
  131. }
  132. return reflect.ValueOf(m)
  133. }
  134. func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  135. m := &serverHelloMsg{}
  136. m.vers = uint16(rand.Intn(65536))
  137. m.random = randomBytes(32, rand)
  138. m.sessionId = randomBytes(rand.Intn(32), rand)
  139. m.cipherSuite = uint16(rand.Int31())
  140. m.compressionMethod = uint8(rand.Intn(256))
  141. if rand.Intn(10) > 5 {
  142. m.nextProtoNeg = true
  143. n := rand.Intn(10)
  144. m.nextProtos = make([]string, n)
  145. for i := 0; i < n; i++ {
  146. m.nextProtos[i] = randomString(20, rand)
  147. }
  148. }
  149. if rand.Intn(10) > 5 {
  150. m.ocspStapling = true
  151. }
  152. if rand.Intn(10) > 5 {
  153. m.ticketSupported = true
  154. }
  155. m.alpnProtocol = randomString(rand.Intn(32)+1, rand)
  156. if rand.Intn(10) > 5 {
  157. numSCTs := rand.Intn(4)
  158. m.scts = make([][]byte, numSCTs)
  159. for i := range m.scts {
  160. m.scts[i] = randomBytes(rand.Intn(500), rand)
  161. }
  162. }
  163. return reflect.ValueOf(m)
  164. }
  165. func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  166. m := &certificateMsg{}
  167. numCerts := rand.Intn(20)
  168. m.certificates = make([][]byte, numCerts)
  169. for i := 0; i < numCerts; i++ {
  170. m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  171. }
  172. return reflect.ValueOf(m)
  173. }
  174. func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  175. m := &certificateRequestMsg{}
  176. m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
  177. numCAs := rand.Intn(100)
  178. m.certificateAuthorities = make([][]byte, numCAs)
  179. for i := 0; i < numCAs; i++ {
  180. m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
  181. }
  182. return reflect.ValueOf(m)
  183. }
  184. func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  185. m := &certificateVerifyMsg{}
  186. m.signature = randomBytes(rand.Intn(15)+1, rand)
  187. return reflect.ValueOf(m)
  188. }
  189. func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  190. m := &certificateStatusMsg{}
  191. if rand.Intn(10) > 5 {
  192. m.statusType = statusTypeOCSP
  193. m.response = randomBytes(rand.Intn(10)+1, rand)
  194. } else {
  195. m.statusType = 42
  196. }
  197. return reflect.ValueOf(m)
  198. }
  199. func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  200. m := &clientKeyExchangeMsg{}
  201. m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
  202. return reflect.ValueOf(m)
  203. }
  204. func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  205. m := &finishedMsg{}
  206. m.verifyData = randomBytes(12, rand)
  207. return reflect.ValueOf(m)
  208. }
  209. func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  210. m := &nextProtoMsg{}
  211. m.proto = randomString(rand.Intn(255), rand)
  212. return reflect.ValueOf(m)
  213. }
  214. func (*newSessionTicketMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  215. m := &newSessionTicketMsg{}
  216. m.ticket = randomBytes(rand.Intn(4), rand)
  217. return reflect.ValueOf(m)
  218. }
  219. func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value {
  220. s := &sessionState{}
  221. s.vers = uint16(rand.Intn(10000))
  222. s.cipherSuite = uint16(rand.Intn(10000))
  223. s.masterSecret = randomBytes(rand.Intn(100), rand)
  224. numCerts := rand.Intn(20)
  225. s.certificates = make([][]byte, numCerts)
  226. for i := 0; i < numCerts; i++ {
  227. s.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  228. }
  229. return reflect.ValueOf(s)
  230. }