Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

414 Zeilen
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([][]byte, numCerts)
  227. for i := 0; i < numCerts; i++ {
  228. m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  229. }
  230. m.requestContext = randomBytes(rand.Intn(5), rand)
  231. return reflect.ValueOf(m)
  232. }
  233. func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  234. m := &certificateRequestMsg{}
  235. m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
  236. numCAs := rand.Intn(100)
  237. m.certificateAuthorities = make([][]byte, numCAs)
  238. for i := 0; i < numCAs; i++ {
  239. m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
  240. }
  241. return reflect.ValueOf(m)
  242. }
  243. func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  244. m := &certificateVerifyMsg{}
  245. m.signature = randomBytes(rand.Intn(15)+1, rand)
  246. return reflect.ValueOf(m)
  247. }
  248. func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  249. m := &certificateStatusMsg{}
  250. if rand.Intn(10) > 5 {
  251. m.statusType = statusTypeOCSP
  252. m.response = randomBytes(rand.Intn(10)+1, rand)
  253. } else {
  254. m.statusType = 42
  255. }
  256. return reflect.ValueOf(m)
  257. }
  258. func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  259. m := &clientKeyExchangeMsg{}
  260. m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
  261. return reflect.ValueOf(m)
  262. }
  263. func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  264. m := &finishedMsg{}
  265. m.verifyData = randomBytes(12, rand)
  266. return reflect.ValueOf(m)
  267. }
  268. func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  269. m := &nextProtoMsg{}
  270. m.proto = randomString(rand.Intn(255), rand)
  271. return reflect.ValueOf(m)
  272. }
  273. func (*newSessionTicketMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  274. m := &newSessionTicketMsg{}
  275. m.ticket = randomBytes(rand.Intn(4), rand)
  276. return reflect.ValueOf(m)
  277. }
  278. func (*newSessionTicketMsg13) Generate(rand *rand.Rand, size int) reflect.Value {
  279. m := &newSessionTicketMsg13{}
  280. m.ageAdd = uint32(rand.Intn(0xffffffff))
  281. m.lifetime = uint32(rand.Intn(0xffffffff))
  282. m.ticket = randomBytes(rand.Intn(40), rand)
  283. if rand.Intn(10) > 5 {
  284. m.withEarlyDataInfo = true
  285. m.maxEarlyDataLength = uint32(rand.Intn(0xffffffff))
  286. }
  287. return reflect.ValueOf(m)
  288. }
  289. func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value {
  290. s := &sessionState{}
  291. s.vers = uint16(rand.Intn(10000))
  292. s.cipherSuite = uint16(rand.Intn(10000))
  293. s.masterSecret = randomBytes(rand.Intn(100), rand)
  294. numCerts := rand.Intn(20)
  295. s.certificates = make([][]byte, numCerts)
  296. for i := 0; i < numCerts; i++ {
  297. s.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  298. }
  299. return reflect.ValueOf(s)
  300. }
  301. func (*sessionState13) Generate(rand *rand.Rand, size int) reflect.Value {
  302. s := &sessionState13{}
  303. s.vers = uint16(rand.Intn(10000))
  304. s.suite = uint16(rand.Intn(10000))
  305. s.ageAdd = uint32(rand.Intn(0xffffffff))
  306. s.maxEarlyDataLen = uint32(rand.Intn(0xffffffff))
  307. s.createdAt = uint64(rand.Int63n(0xfffffffffffffff))
  308. s.resumptionSecret = randomBytes(rand.Intn(100), rand)
  309. s.alpnProtocol = randomString(rand.Intn(100), rand)
  310. s.SNI = randomString(rand.Intn(100), rand)
  311. return reflect.ValueOf(s)
  312. }
  313. func TestRejectEmptySCTList(t *testing.T) {
  314. // https://tools.ietf.org/html/rfc6962#section-3.3.1 specifies that
  315. // empty SCT lists are invalid.
  316. var random [32]byte
  317. sct := []byte{0x42, 0x42, 0x42, 0x42}
  318. serverHello := serverHelloMsg{
  319. vers: VersionTLS12,
  320. random: random[:],
  321. scts: [][]byte{sct},
  322. }
  323. serverHelloBytes := serverHello.marshal()
  324. var serverHelloCopy serverHelloMsg
  325. if serverHelloCopy.unmarshal(serverHelloBytes) != alertSuccess {
  326. t.Fatal("Failed to unmarshal initial message")
  327. }
  328. // Change serverHelloBytes so that the SCT list is empty
  329. i := bytes.Index(serverHelloBytes, sct)
  330. if i < 0 {
  331. t.Fatal("Cannot find SCT in ServerHello")
  332. }
  333. var serverHelloEmptySCT []byte
  334. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[:i-6]...)
  335. // Append the extension length and SCT list length for an empty list.
  336. serverHelloEmptySCT = append(serverHelloEmptySCT, []byte{0, 2, 0, 0}...)
  337. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[i+4:]...)
  338. // Update the handshake message length.
  339. serverHelloEmptySCT[1] = byte((len(serverHelloEmptySCT) - 4) >> 16)
  340. serverHelloEmptySCT[2] = byte((len(serverHelloEmptySCT) - 4) >> 8)
  341. serverHelloEmptySCT[3] = byte(len(serverHelloEmptySCT) - 4)
  342. // Update the extensions length
  343. serverHelloEmptySCT[42] = byte((len(serverHelloEmptySCT) - 44) >> 8)
  344. serverHelloEmptySCT[43] = byte((len(serverHelloEmptySCT) - 44))
  345. if serverHelloCopy.unmarshal(serverHelloEmptySCT) == alertSuccess {
  346. t.Fatal("Unmarshaled ServerHello with empty SCT list")
  347. }
  348. }
  349. func TestRejectEmptySCT(t *testing.T) {
  350. // Not only must the SCT list be non-empty, but the SCT elements must
  351. // not be zero length.
  352. var random [32]byte
  353. serverHello := serverHelloMsg{
  354. vers: VersionTLS12,
  355. random: random[:],
  356. scts: [][]byte{nil},
  357. }
  358. serverHelloBytes := serverHello.marshal()
  359. var serverHelloCopy serverHelloMsg
  360. if serverHelloCopy.unmarshal(serverHelloBytes) == alertSuccess {
  361. t.Fatal("Unmarshaled ServerHello with zero-length SCT")
  362. }
  363. }