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.
 
 
 
 
 
 

416 line
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.nonce = randomBytes(1+rand.Intn(255), rand)
  284. m.ticket = randomBytes(1+rand.Intn(40), rand)
  285. if rand.Intn(10) > 5 {
  286. m.withEarlyDataInfo = true
  287. m.maxEarlyDataLength = uint32(rand.Intn(0xffffffff))
  288. }
  289. return reflect.ValueOf(m)
  290. }
  291. func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value {
  292. s := &sessionState{}
  293. s.vers = uint16(rand.Intn(10000))
  294. s.cipherSuite = uint16(rand.Intn(10000))
  295. s.masterSecret = randomBytes(rand.Intn(100), rand)
  296. numCerts := rand.Intn(20)
  297. s.certificates = make([][]byte, numCerts)
  298. for i := 0; i < numCerts; i++ {
  299. s.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  300. }
  301. return reflect.ValueOf(s)
  302. }
  303. func (*sessionState13) Generate(rand *rand.Rand, size int) reflect.Value {
  304. s := &sessionState13{}
  305. s.vers = uint16(rand.Intn(10000))
  306. s.suite = uint16(rand.Intn(10000))
  307. s.ageAdd = uint32(rand.Intn(0xffffffff))
  308. s.maxEarlyDataLen = uint32(rand.Intn(0xffffffff))
  309. s.createdAt = uint64(rand.Int63n(0xfffffffffffffff))
  310. s.pskSecret = randomBytes(rand.Intn(100), rand)
  311. s.alpnProtocol = randomString(rand.Intn(100), rand)
  312. s.SNI = randomString(rand.Intn(100), rand)
  313. return reflect.ValueOf(s)
  314. }
  315. func TestRejectEmptySCTList(t *testing.T) {
  316. // https://tools.ietf.org/html/rfc6962#section-3.3.1 specifies that
  317. // empty SCT lists are invalid.
  318. var random [32]byte
  319. sct := []byte{0x42, 0x42, 0x42, 0x42}
  320. serverHello := serverHelloMsg{
  321. vers: VersionTLS12,
  322. random: random[:],
  323. scts: [][]byte{sct},
  324. }
  325. serverHelloBytes := serverHello.marshal()
  326. var serverHelloCopy serverHelloMsg
  327. if serverHelloCopy.unmarshal(serverHelloBytes) != alertSuccess {
  328. t.Fatal("Failed to unmarshal initial message")
  329. }
  330. // Change serverHelloBytes so that the SCT list is empty
  331. i := bytes.Index(serverHelloBytes, sct)
  332. if i < 0 {
  333. t.Fatal("Cannot find SCT in ServerHello")
  334. }
  335. var serverHelloEmptySCT []byte
  336. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[:i-6]...)
  337. // Append the extension length and SCT list length for an empty list.
  338. serverHelloEmptySCT = append(serverHelloEmptySCT, []byte{0, 2, 0, 0}...)
  339. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[i+4:]...)
  340. // Update the handshake message length.
  341. serverHelloEmptySCT[1] = byte((len(serverHelloEmptySCT) - 4) >> 16)
  342. serverHelloEmptySCT[2] = byte((len(serverHelloEmptySCT) - 4) >> 8)
  343. serverHelloEmptySCT[3] = byte(len(serverHelloEmptySCT) - 4)
  344. // Update the extensions length
  345. serverHelloEmptySCT[42] = byte((len(serverHelloEmptySCT) - 44) >> 8)
  346. serverHelloEmptySCT[43] = byte((len(serverHelloEmptySCT) - 44))
  347. if serverHelloCopy.unmarshal(serverHelloEmptySCT) == alertSuccess {
  348. t.Fatal("Unmarshaled ServerHello with empty SCT list")
  349. }
  350. }
  351. func TestRejectEmptySCT(t *testing.T) {
  352. // Not only must the SCT list be non-empty, but the SCT elements must
  353. // not be zero length.
  354. var random [32]byte
  355. serverHello := serverHelloMsg{
  356. vers: VersionTLS12,
  357. random: random[:],
  358. scts: [][]byte{nil},
  359. }
  360. serverHelloBytes := serverHello.marshal()
  361. var serverHelloCopy serverHelloMsg
  362. if serverHelloCopy.unmarshal(serverHelloBytes) == alertSuccess {
  363. t.Fatal("Unmarshaled ServerHello with zero-length SCT")
  364. }
  365. }