25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

415 satır
11 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. "bytes"
  7. "math/rand"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "testing/quick"
  12. )
  13. var tests = []interface{}{
  14. &clientHelloMsg{},
  15. &serverHelloMsg{},
  16. &finishedMsg{},
  17. &certificateMsg{},
  18. &certificateRequestMsg{},
  19. &certificateVerifyMsg{},
  20. &certificateStatusMsg{},
  21. &clientKeyExchangeMsg{},
  22. &nextProtoMsg{},
  23. &newSessionTicketMsg{},
  24. &sessionState{},
  25. &encryptedExtensionsMsg{},
  26. &certificateMsg13{},
  27. &newSessionTicketMsg13{},
  28. &sessionState13{},
  29. }
  30. type testMessage interface {
  31. marshal() []byte
  32. unmarshal([]byte) alert
  33. equal(interface{}) bool
  34. }
  35. func TestMarshalUnmarshal(t *testing.T) {
  36. rand := rand.New(rand.NewSource(0))
  37. for i, iface := range tests {
  38. ty := reflect.ValueOf(iface).Type()
  39. n := 100
  40. if testing.Short() {
  41. n = 5
  42. }
  43. for j := 0; j < n; j++ {
  44. v, ok := quick.Value(ty, rand)
  45. if !ok {
  46. t.Errorf("#%d: failed to create value", i)
  47. break
  48. }
  49. m1 := v.Interface().(testMessage)
  50. marshaled := m1.marshal()
  51. m2 := iface.(testMessage)
  52. if m2.unmarshal(marshaled) != alertSuccess {
  53. t.Errorf("#%d.%d failed to unmarshal %#v %x", i, j, m1, marshaled)
  54. break
  55. }
  56. m2.marshal() // to fill any marshal cache in the message
  57. if !m1.equal(m2) {
  58. t.Errorf("#%d.%d got:%#v want:%#v %x", i, j, m2, m1, marshaled)
  59. break
  60. }
  61. if i >= 3 {
  62. // The first three message types (ClientHello,
  63. // ServerHello and Finished) are allowed to
  64. // have parsable prefixes because the extension
  65. // data is optional and the length of the
  66. // Finished varies across versions.
  67. for j := 0; j < len(marshaled); j++ {
  68. if m2.unmarshal(marshaled[0:j]) == alertSuccess {
  69. t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1)
  70. break
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. func TestFuzz(t *testing.T) {
  78. rand := rand.New(rand.NewSource(0))
  79. for _, iface := range tests {
  80. m := iface.(testMessage)
  81. for j := 0; j < 1000; j++ {
  82. len := rand.Intn(100)
  83. bytes := randomBytes(len, rand)
  84. // This just looks for crashes due to bounds errors etc.
  85. m.unmarshal(bytes)
  86. }
  87. }
  88. }
  89. func randomBytes(n int, rand *rand.Rand) []byte {
  90. r := make([]byte, n)
  91. if _, err := rand.Read(r); err != nil {
  92. panic("rand.Read failed: " + err.Error())
  93. }
  94. return r
  95. }
  96. func randomString(n int, rand *rand.Rand) string {
  97. b := randomBytes(n, rand)
  98. return string(b)
  99. }
  100. func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  101. m := &clientHelloMsg{}
  102. m.vers = uint16(rand.Intn(65536))
  103. m.random = randomBytes(32, rand)
  104. m.sessionId = randomBytes(rand.Intn(32), rand)
  105. m.cipherSuites = make([]uint16, rand.Intn(63)+1)
  106. for i := 0; i < len(m.cipherSuites); i++ {
  107. cs := uint16(rand.Int31())
  108. if cs == scsvRenegotiation {
  109. cs += 1
  110. }
  111. m.cipherSuites[i] = cs
  112. }
  113. m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
  114. if rand.Intn(10) > 5 {
  115. m.nextProtoNeg = true
  116. }
  117. if rand.Intn(10) > 5 {
  118. m.serverName = randomString(rand.Intn(255), rand)
  119. for strings.HasSuffix(m.serverName, ".") {
  120. m.serverName = m.serverName[:len(m.serverName)-1]
  121. }
  122. }
  123. m.ocspStapling = rand.Intn(10) > 5
  124. m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)
  125. m.supportedCurves = make([]CurveID, rand.Intn(5)+1)
  126. for i := range m.supportedCurves {
  127. m.supportedCurves[i] = CurveID(rand.Intn(30000))
  128. }
  129. if rand.Intn(10) > 5 {
  130. m.ticketSupported = true
  131. if rand.Intn(10) > 5 {
  132. m.sessionTicket = randomBytes(rand.Intn(300), rand)
  133. }
  134. }
  135. if rand.Intn(10) > 5 {
  136. m.supportedSignatureAlgorithms = supportedSignatureAlgorithms
  137. }
  138. m.alpnProtocols = make([]string, rand.Intn(5))
  139. for i := range m.alpnProtocols {
  140. m.alpnProtocols[i] = randomString(rand.Intn(20)+1, rand)
  141. }
  142. if rand.Intn(10) > 5 {
  143. m.scts = true
  144. }
  145. m.keyShares = make([]keyShare, rand.Intn(4))
  146. for i := range m.keyShares {
  147. m.keyShares[i].group = CurveID(rand.Intn(30000))
  148. m.keyShares[i].data = randomBytes(rand.Intn(300), rand)
  149. }
  150. m.supportedVersions = make([]uint16, rand.Intn(5))
  151. for i := range m.supportedVersions {
  152. m.supportedVersions[i] = uint16(rand.Intn(30000))
  153. }
  154. if rand.Intn(10) > 5 {
  155. m.earlyData = true
  156. }
  157. return reflect.ValueOf(m)
  158. }
  159. func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  160. m := &serverHelloMsg{}
  161. m.vers = uint16(rand.Intn(65536))
  162. m.random = randomBytes(32, rand)
  163. m.sessionId = randomBytes(rand.Intn(32), rand)
  164. m.cipherSuite = uint16(rand.Int31())
  165. m.compressionMethod = uint8(rand.Intn(256))
  166. if rand.Intn(10) > 5 {
  167. m.nextProtoNeg = true
  168. n := rand.Intn(10)
  169. m.nextProtos = make([]string, n)
  170. for i := 0; i < n; i++ {
  171. m.nextProtos[i] = randomString(20, rand)
  172. }
  173. }
  174. if rand.Intn(10) > 5 {
  175. m.ocspStapling = true
  176. }
  177. if rand.Intn(10) > 5 {
  178. m.ticketSupported = true
  179. }
  180. m.alpnProtocol = randomString(rand.Intn(32)+1, rand)
  181. if rand.Intn(10) > 5 {
  182. numSCTs := rand.Intn(4)
  183. m.scts = make([][]byte, numSCTs)
  184. for i := range m.scts {
  185. m.scts[i] = randomBytes(rand.Intn(500), rand)
  186. }
  187. }
  188. if rand.Intn(10) > 5 {
  189. m.keyShare.group = CurveID(rand.Intn(30000))
  190. m.keyShare.data = randomBytes(rand.Intn(300), rand)
  191. }
  192. if rand.Intn(10) > 5 {
  193. m.psk = true
  194. m.pskIdentity = uint16(rand.Int31())
  195. }
  196. return reflect.ValueOf(m)
  197. }
  198. func (*encryptedExtensionsMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  199. m := &encryptedExtensionsMsg{}
  200. if rand.Intn(10) > 5 {
  201. m.alpnProtocol = randomString(rand.Intn(32)+1, rand)
  202. }
  203. if rand.Intn(10) > 5 {
  204. m.earlyData = true
  205. }
  206. return reflect.ValueOf(m)
  207. }
  208. func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  209. m := &certificateMsg{}
  210. numCerts := rand.Intn(20)
  211. m.certificates = make([][]byte, numCerts)
  212. for i := 0; i < numCerts; i++ {
  213. m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  214. }
  215. return reflect.ValueOf(m)
  216. }
  217. func (*certificateMsg13) Generate(rand *rand.Rand, size int) reflect.Value {
  218. m := &certificateMsg13{}
  219. numCerts := rand.Intn(20)
  220. m.certificates = make([]certificateEntry, numCerts)
  221. for i := 0; i < numCerts; i++ {
  222. m.certificates[i].data = randomBytes(rand.Intn(10)+1, rand)
  223. if rand.Intn(2) == 1 {
  224. m.certificates[i].ocspStaple = randomBytes(rand.Intn(10)+1, rand)
  225. }
  226. numScts := rand.Intn(3)
  227. for j := 0; j < numScts; j++ {
  228. m.certificates[i].sctList = append(m.certificates[i].sctList, randomBytes(rand.Intn(10)+1, rand))
  229. }
  230. }
  231. m.requestContext = randomBytes(rand.Intn(5), rand)
  232. return reflect.ValueOf(m)
  233. }
  234. func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  235. m := &certificateRequestMsg{}
  236. m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
  237. numCAs := rand.Intn(100)
  238. m.certificateAuthorities = make([][]byte, numCAs)
  239. for i := 0; i < numCAs; i++ {
  240. m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
  241. }
  242. return reflect.ValueOf(m)
  243. }
  244. func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  245. m := &certificateVerifyMsg{}
  246. m.signature = randomBytes(rand.Intn(15)+1, rand)
  247. return reflect.ValueOf(m)
  248. }
  249. func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  250. m := &certificateStatusMsg{}
  251. if rand.Intn(10) > 5 {
  252. m.statusType = statusTypeOCSP
  253. m.response = randomBytes(rand.Intn(10)+1, rand)
  254. } else {
  255. m.statusType = 42
  256. }
  257. return reflect.ValueOf(m)
  258. }
  259. func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  260. m := &clientKeyExchangeMsg{}
  261. m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
  262. return reflect.ValueOf(m)
  263. }
  264. func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  265. m := &finishedMsg{}
  266. m.verifyData = randomBytes(12, rand)
  267. return reflect.ValueOf(m)
  268. }
  269. func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  270. m := &nextProtoMsg{}
  271. m.proto = randomString(rand.Intn(255), rand)
  272. return reflect.ValueOf(m)
  273. }
  274. func (*newSessionTicketMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  275. m := &newSessionTicketMsg{}
  276. m.ticket = randomBytes(rand.Intn(4), rand)
  277. return reflect.ValueOf(m)
  278. }
  279. func (*newSessionTicketMsg13) Generate(rand *rand.Rand, size int) reflect.Value {
  280. m := &newSessionTicketMsg13{}
  281. m.ageAdd = uint32(rand.Intn(0xffffffff))
  282. m.lifetime = uint32(rand.Intn(0xffffffff))
  283. m.ticket = randomBytes(rand.Intn(40), rand)
  284. if rand.Intn(10) > 5 {
  285. m.withEarlyDataInfo = true
  286. m.maxEarlyDataLength = uint32(rand.Intn(0xffffffff))
  287. }
  288. return reflect.ValueOf(m)
  289. }
  290. func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value {
  291. s := &sessionState{}
  292. s.vers = uint16(rand.Intn(10000))
  293. s.cipherSuite = uint16(rand.Intn(10000))
  294. s.masterSecret = randomBytes(rand.Intn(100), rand)
  295. numCerts := rand.Intn(20)
  296. s.certificates = make([][]byte, numCerts)
  297. for i := 0; i < numCerts; i++ {
  298. s.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  299. }
  300. return reflect.ValueOf(s)
  301. }
  302. func (*sessionState13) Generate(rand *rand.Rand, size int) reflect.Value {
  303. s := &sessionState13{}
  304. s.vers = uint16(rand.Intn(10000))
  305. s.suite = uint16(rand.Intn(10000))
  306. s.ageAdd = uint32(rand.Intn(0xffffffff))
  307. s.maxEarlyDataLen = uint32(rand.Intn(0xffffffff))
  308. s.createdAt = uint64(rand.Int63n(0xfffffffffffffff))
  309. s.resumptionSecret = randomBytes(rand.Intn(100), rand)
  310. s.alpnProtocol = randomString(rand.Intn(100), rand)
  311. s.SNI = randomString(rand.Intn(100), rand)
  312. return reflect.ValueOf(s)
  313. }
  314. func TestRejectEmptySCTList(t *testing.T) {
  315. // https://tools.ietf.org/html/rfc6962#section-3.3.1 specifies that
  316. // empty SCT lists are invalid.
  317. var random [32]byte
  318. sct := []byte{0x42, 0x42, 0x42, 0x42}
  319. serverHello := serverHelloMsg{
  320. vers: VersionTLS12,
  321. random: random[:],
  322. scts: [][]byte{sct},
  323. }
  324. serverHelloBytes := serverHello.marshal()
  325. var serverHelloCopy serverHelloMsg
  326. if serverHelloCopy.unmarshal(serverHelloBytes) != alertSuccess {
  327. t.Fatal("Failed to unmarshal initial message")
  328. }
  329. // Change serverHelloBytes so that the SCT list is empty
  330. i := bytes.Index(serverHelloBytes, sct)
  331. if i < 0 {
  332. t.Fatal("Cannot find SCT in ServerHello")
  333. }
  334. var serverHelloEmptySCT []byte
  335. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[:i-6]...)
  336. // Append the extension length and SCT list length for an empty list.
  337. serverHelloEmptySCT = append(serverHelloEmptySCT, []byte{0, 2, 0, 0}...)
  338. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[i+4:]...)
  339. // Update the handshake message length.
  340. serverHelloEmptySCT[1] = byte((len(serverHelloEmptySCT) - 4) >> 16)
  341. serverHelloEmptySCT[2] = byte((len(serverHelloEmptySCT) - 4) >> 8)
  342. serverHelloEmptySCT[3] = byte(len(serverHelloEmptySCT) - 4)
  343. // Update the extensions length
  344. serverHelloEmptySCT[42] = byte((len(serverHelloEmptySCT) - 44) >> 8)
  345. serverHelloEmptySCT[43] = byte((len(serverHelloEmptySCT) - 44))
  346. if serverHelloCopy.unmarshal(serverHelloEmptySCT) == alertSuccess {
  347. t.Fatal("Unmarshaled ServerHello with empty SCT list")
  348. }
  349. }
  350. func TestRejectEmptySCT(t *testing.T) {
  351. // Not only must the SCT list be non-empty, but the SCT elements must
  352. // not be zero length.
  353. var random [32]byte
  354. serverHello := serverHelloMsg{
  355. vers: VersionTLS12,
  356. random: random[:],
  357. scts: [][]byte{nil},
  358. }
  359. serverHelloBytes := serverHello.marshal()
  360. var serverHelloCopy serverHelloMsg
  361. if serverHelloCopy.unmarshal(serverHelloBytes) == alertSuccess {
  362. t.Fatal("Unmarshaled ServerHello with zero-length SCT")
  363. }
  364. }