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.
 
 
 
 
 
 

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