Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

410 lignes
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) bool
  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) {
  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]) {
  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. m.cipherSuites[i] = uint16(rand.Int31())
  109. }
  110. m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
  111. if rand.Intn(10) > 5 {
  112. m.nextProtoNeg = true
  113. }
  114. if rand.Intn(10) > 5 {
  115. m.serverName = randomString(rand.Intn(255), rand)
  116. for strings.HasSuffix(m.serverName, ".") {
  117. m.serverName = m.serverName[:len(m.serverName)-1]
  118. }
  119. }
  120. m.ocspStapling = rand.Intn(10) > 5
  121. m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)
  122. m.supportedCurves = make([]CurveID, rand.Intn(5)+1)
  123. for i := range m.supportedCurves {
  124. m.supportedCurves[i] = CurveID(rand.Intn(30000))
  125. }
  126. if rand.Intn(10) > 5 {
  127. m.ticketSupported = true
  128. if rand.Intn(10) > 5 {
  129. m.sessionTicket = randomBytes(rand.Intn(300), rand)
  130. }
  131. }
  132. if rand.Intn(10) > 5 {
  133. m.signatureAndHashes = supportedSignatureAlgorithms
  134. }
  135. m.alpnProtocols = make([]string, rand.Intn(5))
  136. for i := range m.alpnProtocols {
  137. m.alpnProtocols[i] = randomString(rand.Intn(20)+1, rand)
  138. }
  139. if rand.Intn(10) > 5 {
  140. m.scts = true
  141. }
  142. m.keyShares = make([]keyShare, rand.Intn(4))
  143. for i := range m.keyShares {
  144. m.keyShares[i].group = CurveID(rand.Intn(30000))
  145. m.keyShares[i].data = randomBytes(rand.Intn(300), rand)
  146. }
  147. m.supportedVersions = make([]uint16, rand.Intn(5))
  148. for i := range m.supportedVersions {
  149. m.supportedVersions[i] = uint16(rand.Intn(30000))
  150. }
  151. if rand.Intn(10) > 5 {
  152. m.earlyData = true
  153. }
  154. return reflect.ValueOf(m)
  155. }
  156. func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  157. m := &serverHelloMsg{}
  158. m.vers = uint16(rand.Intn(65536))
  159. m.random = randomBytes(32, rand)
  160. m.sessionId = randomBytes(rand.Intn(32), rand)
  161. m.cipherSuite = uint16(rand.Int31())
  162. m.compressionMethod = uint8(rand.Intn(256))
  163. if rand.Intn(10) > 5 {
  164. m.nextProtoNeg = true
  165. n := rand.Intn(10)
  166. m.nextProtos = make([]string, n)
  167. for i := 0; i < n; i++ {
  168. m.nextProtos[i] = randomString(20, rand)
  169. }
  170. }
  171. if rand.Intn(10) > 5 {
  172. m.ocspStapling = true
  173. }
  174. if rand.Intn(10) > 5 {
  175. m.ticketSupported = true
  176. }
  177. m.alpnProtocol = randomString(rand.Intn(32)+1, rand)
  178. if rand.Intn(10) > 5 {
  179. numSCTs := rand.Intn(4)
  180. m.scts = make([][]byte, numSCTs)
  181. for i := range m.scts {
  182. m.scts[i] = randomBytes(rand.Intn(500), rand)
  183. }
  184. }
  185. return reflect.ValueOf(m)
  186. }
  187. func (*serverHelloMsg13) Generate(rand *rand.Rand, size int) reflect.Value {
  188. m := &serverHelloMsg13{}
  189. m.vers = uint16(rand.Intn(65536))
  190. m.random = randomBytes(32, rand)
  191. m.cipherSuite = uint16(rand.Int31())
  192. m.keyShare.group = CurveID(rand.Intn(30000))
  193. m.keyShare.data = randomBytes(rand.Intn(300), rand)
  194. if rand.Intn(10) > 5 {
  195. m.psk = true
  196. m.pskIdentity = uint16(rand.Int31())
  197. }
  198. return reflect.ValueOf(m)
  199. }
  200. func (*encryptedExtensionsMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  201. m := &encryptedExtensionsMsg{}
  202. if rand.Intn(10) > 5 {
  203. m.alpnProtocol = randomString(rand.Intn(32)+1, rand)
  204. }
  205. if rand.Intn(10) > 5 {
  206. m.earlyData = true
  207. }
  208. return reflect.ValueOf(m)
  209. }
  210. func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  211. m := &certificateMsg{}
  212. numCerts := rand.Intn(20)
  213. m.certificates = make([][]byte, numCerts)
  214. for i := 0; i < numCerts; i++ {
  215. m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  216. }
  217. return reflect.ValueOf(m)
  218. }
  219. func (*certificateMsg13) Generate(rand *rand.Rand, size int) reflect.Value {
  220. m := &certificateMsg13{}
  221. numCerts := rand.Intn(20)
  222. m.certificates = make([][]byte, numCerts)
  223. for i := 0; i < numCerts; i++ {
  224. m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  225. }
  226. m.requestContext = randomBytes(rand.Intn(5), rand)
  227. return reflect.ValueOf(m)
  228. }
  229. func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  230. m := &certificateRequestMsg{}
  231. m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
  232. numCAs := rand.Intn(100)
  233. m.certificateAuthorities = make([][]byte, numCAs)
  234. for i := 0; i < numCAs; i++ {
  235. m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
  236. }
  237. return reflect.ValueOf(m)
  238. }
  239. func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  240. m := &certificateVerifyMsg{}
  241. m.signature = randomBytes(rand.Intn(15)+1, rand)
  242. return reflect.ValueOf(m)
  243. }
  244. func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  245. m := &certificateStatusMsg{}
  246. if rand.Intn(10) > 5 {
  247. m.statusType = statusTypeOCSP
  248. m.response = randomBytes(rand.Intn(10)+1, rand)
  249. } else {
  250. m.statusType = 42
  251. }
  252. return reflect.ValueOf(m)
  253. }
  254. func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  255. m := &clientKeyExchangeMsg{}
  256. m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
  257. return reflect.ValueOf(m)
  258. }
  259. func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  260. m := &finishedMsg{}
  261. m.verifyData = randomBytes(12, rand)
  262. return reflect.ValueOf(m)
  263. }
  264. func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  265. m := &nextProtoMsg{}
  266. m.proto = randomString(rand.Intn(255), rand)
  267. return reflect.ValueOf(m)
  268. }
  269. func (*newSessionTicketMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  270. m := &newSessionTicketMsg{}
  271. m.ticket = randomBytes(rand.Intn(4), rand)
  272. return reflect.ValueOf(m)
  273. }
  274. func (*newSessionTicketMsg13) Generate(rand *rand.Rand, size int) reflect.Value {
  275. m := &newSessionTicketMsg13{}
  276. m.ageAdd = uint32(rand.Intn(0xffffffff))
  277. m.lifetime = uint32(rand.Intn(0xffffffff))
  278. m.ticket = randomBytes(rand.Intn(40), rand)
  279. if rand.Intn(10) > 5 {
  280. m.withEarlyDataInfo = true
  281. m.maxEarlyDataLength = uint32(rand.Intn(0xffffffff))
  282. }
  283. return reflect.ValueOf(m)
  284. }
  285. func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value {
  286. s := &sessionState{}
  287. s.vers = uint16(rand.Intn(10000))
  288. s.cipherSuite = uint16(rand.Intn(10000))
  289. s.masterSecret = randomBytes(rand.Intn(100), rand)
  290. numCerts := rand.Intn(20)
  291. s.certificates = make([][]byte, numCerts)
  292. for i := 0; i < numCerts; i++ {
  293. s.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  294. }
  295. return reflect.ValueOf(s)
  296. }
  297. func (*sessionState13) Generate(rand *rand.Rand, size int) reflect.Value {
  298. s := &sessionState13{}
  299. s.vers = uint16(rand.Intn(10000))
  300. s.suite = uint16(rand.Intn(10000))
  301. s.ageAdd = uint32(rand.Intn(0xffffffff))
  302. s.maxEarlyDataLen = uint32(rand.Intn(0xffffffff))
  303. s.createdAt = uint64(rand.Int63n(0xfffffffffffffff))
  304. s.resumptionSecret = randomBytes(rand.Intn(100), rand)
  305. s.alpnProtocol = randomString(rand.Intn(100), rand)
  306. s.SNI = randomString(rand.Intn(100), rand)
  307. return reflect.ValueOf(s)
  308. }
  309. func TestRejectEmptySCTList(t *testing.T) {
  310. // https://tools.ietf.org/html/rfc6962#section-3.3.1 specifies that
  311. // empty SCT lists are invalid.
  312. var random [32]byte
  313. sct := []byte{0x42, 0x42, 0x42, 0x42}
  314. serverHello := serverHelloMsg{
  315. vers: VersionTLS12,
  316. random: random[:],
  317. scts: [][]byte{sct},
  318. }
  319. serverHelloBytes := serverHello.marshal()
  320. var serverHelloCopy serverHelloMsg
  321. if !serverHelloCopy.unmarshal(serverHelloBytes) {
  322. t.Fatal("Failed to unmarshal initial message")
  323. }
  324. // Change serverHelloBytes so that the SCT list is empty
  325. i := bytes.Index(serverHelloBytes, sct)
  326. if i < 0 {
  327. t.Fatal("Cannot find SCT in ServerHello")
  328. }
  329. var serverHelloEmptySCT []byte
  330. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[:i-6]...)
  331. // Append the extension length and SCT list length for an empty list.
  332. serverHelloEmptySCT = append(serverHelloEmptySCT, []byte{0, 2, 0, 0}...)
  333. serverHelloEmptySCT = append(serverHelloEmptySCT, serverHelloBytes[i+4:]...)
  334. // Update the handshake message length.
  335. serverHelloEmptySCT[1] = byte((len(serverHelloEmptySCT) - 4) >> 16)
  336. serverHelloEmptySCT[2] = byte((len(serverHelloEmptySCT) - 4) >> 8)
  337. serverHelloEmptySCT[3] = byte(len(serverHelloEmptySCT) - 4)
  338. // Update the extensions length
  339. serverHelloEmptySCT[42] = byte((len(serverHelloEmptySCT) - 44) >> 8)
  340. serverHelloEmptySCT[43] = byte((len(serverHelloEmptySCT) - 44))
  341. if serverHelloCopy.unmarshal(serverHelloEmptySCT) {
  342. t.Fatal("Unmarshaled ServerHello with empty SCT list")
  343. }
  344. }
  345. func TestRejectEmptySCT(t *testing.T) {
  346. // Not only must the SCT list be non-empty, but the SCT elements must
  347. // not be zero length.
  348. var random [32]byte
  349. serverHello := serverHelloMsg{
  350. vers: VersionTLS12,
  351. random: random[:],
  352. scts: [][]byte{nil},
  353. }
  354. serverHelloBytes := serverHello.marshal()
  355. var serverHelloCopy serverHelloMsg
  356. if serverHelloCopy.unmarshal(serverHelloBytes) {
  357. t.Fatal("Unmarshaled ServerHello with zero-length SCT")
  358. }
  359. }