Alternative TLS implementation in Go
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

handshake_messages_test.go 11 KiB

crypto/tls: decouple handshake signatures from the handshake hash. Prior to TLS 1.2, the handshake had a pleasing property that one could incrementally hash it and, from that, get the needed hashes for both the CertificateVerify and Finished messages. TLS 1.2 introduced negotiation for the signature and hash and it became possible for the handshake hash to be, say, SHA-384, but for the CertificateVerify to sign the handshake with SHA-1. The problem is that one doesn't know in advance which hashes will be needed and thus the handshake needs to be buffered. Go ignored this, always kept a single handshake hash, and any signatures over the handshake had to use that hash. However, there are a set of servers that inspect the client's offered signature hash functions and will abort the handshake if one of the server's certificates is signed with a hash function outside of that set. https://robertsspaceindustries.com/ is an example of such a server. Clearly not a lot of thought happened when that server code was written, but its out there and we have to deal with it. This change decouples the handshake hash from the CertificateVerify hash. This lays the groundwork for advertising support for SHA-384 but doesn't actually make that change in the interests of reviewability. Updating the advertised hash functions will cause changes in many of the testdata/ files and some errors might get lost in the noise. This change only needs to update four testdata/ files: one because a SHA-384-based handshake is now being signed with SHA-256 and the others because the TLS 1.2 CertificateRequest message now includes SHA-1. This change also has the effect of adding support for client-certificates in SSLv3 servers. However, SSLv3 is now disabled by default so this should be moot. It would be possible to avoid much of this change and just support SHA-384 for the ServerKeyExchange as the SKX only signs over the nonces and SKX params (a design mistake in TLS). However, that would leave Go in the odd situation where it advertised support for SHA-384, but would only use the handshake hash when signing client certificates. I fear that'll just cause problems in the future. Much of this code was written by davidben@ for the purposes of testing BoringSSL. Partly addresses #9757 Change-Id: I5137a472b6076812af387a5a69fc62c7373cd485 Reviewed-on: https://go-review.googlesource.com/9415 Run-TryBot: Adam Langley <agl@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
9 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. }