blob: 99ef64fc22d8923c30f264b81b199933fb95f143 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001// 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
Adam Langleydc7e9c42015-09-29 15:21:04 -07005package runner
Adam Langley95c29f32014-06-20 12:00:00 -07006
7import (
Nick Harperb41d2e42016-07-01 17:50:32 -04008 "crypto"
Adam Langley95c29f32014-06-20 12:00:00 -07009 "crypto/hmac"
10 "crypto/md5"
11 "crypto/sha1"
12 "crypto/sha256"
Adam Langley95c29f32014-06-20 12:00:00 -070013 "hash"
14)
15
16// Split a premaster secret in two as specified in RFC 4346, section 5.
17func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {
18 s1 = secret[0 : (len(secret)+1)/2]
19 s2 = secret[len(secret)/2:]
20 return
21}
22
23// pHash implements the P_hash function, as defined in RFC 4346, section 5.
24func pHash(result, secret, seed []byte, hash func() hash.Hash) {
25 h := hmac.New(hash, secret)
26 h.Write(seed)
27 a := h.Sum(nil)
28
29 j := 0
30 for j < len(result) {
31 h.Reset()
32 h.Write(a)
33 h.Write(seed)
34 b := h.Sum(nil)
35 todo := len(b)
36 if j+todo > len(result) {
37 todo = len(result) - j
38 }
39 copy(result[j:j+todo], b)
40 j += todo
41
42 h.Reset()
43 h.Write(a)
44 a = h.Sum(nil)
45 }
46}
47
48// prf10 implements the TLS 1.0 pseudo-random function, as defined in RFC 2246, section 5.
49func prf10(result, secret, label, seed []byte) {
50 hashSHA1 := sha1.New
51 hashMD5 := md5.New
52
53 labelAndSeed := make([]byte, len(label)+len(seed))
54 copy(labelAndSeed, label)
55 copy(labelAndSeed[len(label):], seed)
56
57 s1, s2 := splitPreMasterSecret(secret)
58 pHash(result, s1, labelAndSeed, hashMD5)
59 result2 := make([]byte, len(result))
60 pHash(result2, s2, labelAndSeed, hashSHA1)
61
62 for i, b := range result2 {
63 result[i] ^= b
64 }
65}
66
67// prf12 implements the TLS 1.2 pseudo-random function, as defined in RFC 5246, section 5.
68func prf12(hashFunc func() hash.Hash) func(result, secret, label, seed []byte) {
69 return func(result, secret, label, seed []byte) {
70 labelAndSeed := make([]byte, len(label)+len(seed))
71 copy(labelAndSeed, label)
72 copy(labelAndSeed[len(label):], seed)
73
74 pHash(result, secret, labelAndSeed, hashFunc)
75 }
76}
77
78// prf30 implements the SSL 3.0 pseudo-random function, as defined in
79// www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 6.
80func prf30(result, secret, label, seed []byte) {
81 hashSHA1 := sha1.New()
82 hashMD5 := md5.New()
83
84 done := 0
85 i := 0
86 // RFC5246 section 6.3 says that the largest PRF output needed is 128
87 // bytes. Since no more ciphersuites will be added to SSLv3, this will
88 // remain true. Each iteration gives us 16 bytes so 10 iterations will
89 // be sufficient.
90 var b [11]byte
91 for done < len(result) {
92 for j := 0; j <= i; j++ {
93 b[j] = 'A' + byte(i)
94 }
95
96 hashSHA1.Reset()
97 hashSHA1.Write(b[:i+1])
98 hashSHA1.Write(secret)
99 hashSHA1.Write(seed)
100 digest := hashSHA1.Sum(nil)
101
102 hashMD5.Reset()
103 hashMD5.Write(secret)
104 hashMD5.Write(digest)
105
106 done += copy(result[done:], hashMD5.Sum(nil))
107 i++
108 }
109}
110
111const (
112 tlsRandomLength = 32 // Length of a random nonce in TLS 1.1.
113 masterSecretLength = 48 // Length of a master secret in TLS 1.1.
114 finishedVerifyLength = 12 // Length of verify_data in a Finished message.
115)
116
117var masterSecretLabel = []byte("master secret")
Adam Langley75712922014-10-10 16:23:43 -0700118var extendedMasterSecretLabel = []byte("extended master secret")
Adam Langley95c29f32014-06-20 12:00:00 -0700119var keyExpansionLabel = []byte("key expansion")
120var clientFinishedLabel = []byte("client finished")
121var serverFinishedLabel = []byte("server finished")
Steven Valdezc4aa7272016-10-03 12:25:56 -0400122var finishedLabel = []byte("finished")
David Benjamind30a9902014-08-24 01:44:23 -0400123var channelIDLabel = []byte("TLS Channel ID signature\x00")
124var channelIDResumeLabel = []byte("Resumption\x00")
Adam Langley95c29f32014-06-20 12:00:00 -0700125
126func prfForVersion(version uint16, suite *cipherSuite) func(result, secret, label, seed []byte) {
127 switch version {
128 case VersionSSL30:
129 return prf30
130 case VersionTLS10, VersionTLS11:
131 return prf10
Nick Harper1fd39d82016-06-14 18:14:35 -0700132 // TODO(nharper): VersionTLS13 is in the case statement below only to
133 // support Fake TLS 1.3. Real TLS 1.3 should never call this function.
134 // Once we no longer support Fake TLS 1.3, the VersionTLS13 should be
135 // removed from this case statement.
136 case VersionTLS12, VersionTLS13:
David Benjamin8d315d72016-07-18 01:03:18 +0200137 if version == VersionTLS12 {
Nick Harperb41d2e42016-07-01 17:50:32 -0400138 return prf12(suite.hash().New)
Adam Langley95c29f32014-06-20 12:00:00 -0700139 }
Adam Langley95c29f32014-06-20 12:00:00 -0700140 }
Nick Harperb41d2e42016-07-01 17:50:32 -0400141 panic("unknown version")
Adam Langley95c29f32014-06-20 12:00:00 -0700142}
143
144// masterFromPreMasterSecret generates the master secret from the pre-master
145// secret. See http://tools.ietf.org/html/rfc5246#section-8.1
146func masterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret, clientRandom, serverRandom []byte) []byte {
147 var seed [tlsRandomLength * 2]byte
148 copy(seed[0:len(clientRandom)], clientRandom)
149 copy(seed[len(clientRandom):], serverRandom)
150 masterSecret := make([]byte, masterSecretLength)
151 prfForVersion(version, suite)(masterSecret, preMasterSecret, masterSecretLabel, seed[0:])
152 return masterSecret
153}
154
Adam Langley75712922014-10-10 16:23:43 -0700155// extendedMasterFromPreMasterSecret generates the master secret from the
156// pre-master secret when the Triple Handshake fix is in effect. See
David Benjamin43946d42016-02-01 08:42:19 -0500157// https://tools.ietf.org/html/rfc7627
Adam Langley75712922014-10-10 16:23:43 -0700158func extendedMasterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret []byte, h finishedHash) []byte {
159 masterSecret := make([]byte, masterSecretLength)
160 prfForVersion(version, suite)(masterSecret, preMasterSecret, extendedMasterSecretLabel, h.Sum())
161 return masterSecret
162}
163
Adam Langley95c29f32014-06-20 12:00:00 -0700164// keysFromMasterSecret generates the connection keys from the master
165// secret, given the lengths of the MAC key, cipher key and IV, as defined in
166// RFC 2246, section 6.3.
167func keysFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) {
168 var seed [tlsRandomLength * 2]byte
169 copy(seed[0:len(clientRandom)], serverRandom)
170 copy(seed[len(serverRandom):], clientRandom)
171
172 n := 2*macLen + 2*keyLen + 2*ivLen
173 keyMaterial := make([]byte, n)
174 prfForVersion(version, suite)(keyMaterial, masterSecret, keyExpansionLabel, seed[0:])
175 clientMAC = keyMaterial[:macLen]
176 keyMaterial = keyMaterial[macLen:]
177 serverMAC = keyMaterial[:macLen]
178 keyMaterial = keyMaterial[macLen:]
179 clientKey = keyMaterial[:keyLen]
180 keyMaterial = keyMaterial[keyLen:]
181 serverKey = keyMaterial[:keyLen]
182 keyMaterial = keyMaterial[keyLen:]
183 clientIV = keyMaterial[:ivLen]
184 keyMaterial = keyMaterial[ivLen:]
185 serverIV = keyMaterial[:ivLen]
186 return
187}
188
189func newFinishedHash(version uint16, cipherSuite *cipherSuite) finishedHash {
Nick Harperb41d2e42016-07-01 17:50:32 -0400190 var ret finishedHash
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Nick Harperb41d2e42016-07-01 17:50:32 -0400192 if version >= VersionTLS12 {
193 ret.hash = cipherSuite.hash()
194
195 ret.client = ret.hash.New()
196 ret.server = ret.hash.New()
197
David Benjamin8d315d72016-07-18 01:03:18 +0200198 if version == VersionTLS12 {
Nick Harperb41d2e42016-07-01 17:50:32 -0400199 ret.prf = prf12(ret.hash.New)
200 }
201 } else {
202 ret.hash = crypto.MD5SHA1
203
204 ret.client = sha1.New()
205 ret.server = sha1.New()
206 ret.clientMD5 = md5.New()
207 ret.serverMD5 = md5.New()
208
209 ret.prf = prf10
Adam Langley95c29f32014-06-20 12:00:00 -0700210 }
Nick Harperb41d2e42016-07-01 17:50:32 -0400211
212 ret.buffer = []byte{}
213 ret.version = version
214 return ret
Adam Langley95c29f32014-06-20 12:00:00 -0700215}
216
217// A finishedHash calculates the hash of a set of handshake messages suitable
218// for including in a Finished message.
219type finishedHash struct {
Nick Harperb41d2e42016-07-01 17:50:32 -0400220 hash crypto.Hash
221
Adam Langley95c29f32014-06-20 12:00:00 -0700222 client hash.Hash
223 server hash.Hash
224
225 // Prior to TLS 1.2, an additional MD5 hash is required.
226 clientMD5 hash.Hash
227 serverMD5 hash.Hash
228
David Benjamine098ec22014-08-27 23:13:20 -0400229 // In TLS 1.2 (and SSL 3 for implementation convenience), a
230 // full buffer is required.
231 buffer []byte
232
Nick Harperb41d2e42016-07-01 17:50:32 -0400233 // TLS 1.3 has a resumption context which is carried over on PSK
234 // resumption.
235 resumptionContextHash []byte
236
Adam Langley95c29f32014-06-20 12:00:00 -0700237 version uint16
238 prf func(result, secret, label, seed []byte)
239}
240
David Benjamine098ec22014-08-27 23:13:20 -0400241func (h *finishedHash) Write(msg []byte) (n int, err error) {
Adam Langley95c29f32014-06-20 12:00:00 -0700242 h.client.Write(msg)
243 h.server.Write(msg)
244
245 if h.version < VersionTLS12 {
246 h.clientMD5.Write(msg)
247 h.serverMD5.Write(msg)
248 }
David Benjamine098ec22014-08-27 23:13:20 -0400249
250 if h.buffer != nil {
251 h.buffer = append(h.buffer, msg...)
252 }
253
Adam Langley95c29f32014-06-20 12:00:00 -0700254 return len(msg), nil
255}
256
Adam Langley75712922014-10-10 16:23:43 -0700257func (h finishedHash) Sum() []byte {
258 if h.version >= VersionTLS12 {
259 return h.client.Sum(nil)
260 }
261
262 out := make([]byte, 0, md5.Size+sha1.Size)
263 out = h.clientMD5.Sum(out)
264 return h.client.Sum(out)
265}
266
Adam Langley95c29f32014-06-20 12:00:00 -0700267// finishedSum30 calculates the contents of the verify_data member of a SSLv3
268// Finished message given the MD5 and SHA1 hashes of a set of handshake
269// messages.
David Benjamine098ec22014-08-27 23:13:20 -0400270func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic []byte) []byte {
271 md5.Write(magic)
Adam Langley95c29f32014-06-20 12:00:00 -0700272 md5.Write(masterSecret)
273 md5.Write(ssl30Pad1[:])
274 md5Digest := md5.Sum(nil)
275
276 md5.Reset()
277 md5.Write(masterSecret)
278 md5.Write(ssl30Pad2[:])
279 md5.Write(md5Digest)
280 md5Digest = md5.Sum(nil)
281
David Benjamine098ec22014-08-27 23:13:20 -0400282 sha1.Write(magic)
Adam Langley95c29f32014-06-20 12:00:00 -0700283 sha1.Write(masterSecret)
284 sha1.Write(ssl30Pad1[:40])
285 sha1Digest := sha1.Sum(nil)
286
287 sha1.Reset()
288 sha1.Write(masterSecret)
289 sha1.Write(ssl30Pad2[:40])
290 sha1.Write(sha1Digest)
291 sha1Digest = sha1.Sum(nil)
292
293 ret := make([]byte, len(md5Digest)+len(sha1Digest))
294 copy(ret, md5Digest)
295 copy(ret[len(md5Digest):], sha1Digest)
296 return ret
297}
298
299var ssl3ClientFinishedMagic = [4]byte{0x43, 0x4c, 0x4e, 0x54}
300var ssl3ServerFinishedMagic = [4]byte{0x53, 0x52, 0x56, 0x52}
301
302// clientSum returns the contents of the verify_data member of a client's
303// Finished message.
Nick Harperb41d2e42016-07-01 17:50:32 -0400304func (h finishedHash) clientSum(baseKey []byte) []byte {
Adam Langley95c29f32014-06-20 12:00:00 -0700305 if h.version == VersionSSL30 {
Nick Harperb41d2e42016-07-01 17:50:32 -0400306 return finishedSum30(h.clientMD5, h.client, baseKey, ssl3ClientFinishedMagic[:])
Adam Langley95c29f32014-06-20 12:00:00 -0700307 }
308
David Benjamin8d315d72016-07-18 01:03:18 +0200309 if h.version < VersionTLS13 {
Nick Harperb41d2e42016-07-01 17:50:32 -0400310 out := make([]byte, finishedVerifyLength)
311 h.prf(out, baseKey, clientFinishedLabel, h.Sum())
312 return out
313 }
314
Steven Valdezc4aa7272016-10-03 12:25:56 -0400315 clientFinishedKey := hkdfExpandLabel(h.hash, baseKey, finishedLabel, nil, h.hash.Size())
Nick Harperb41d2e42016-07-01 17:50:32 -0400316 finishedHMAC := hmac.New(h.hash.New, clientFinishedKey)
317 finishedHMAC.Write(h.appendContextHashes(nil))
318 return finishedHMAC.Sum(nil)
Adam Langley95c29f32014-06-20 12:00:00 -0700319}
320
321// serverSum returns the contents of the verify_data member of a server's
322// Finished message.
Nick Harperb41d2e42016-07-01 17:50:32 -0400323func (h finishedHash) serverSum(baseKey []byte) []byte {
Adam Langley95c29f32014-06-20 12:00:00 -0700324 if h.version == VersionSSL30 {
Nick Harperb41d2e42016-07-01 17:50:32 -0400325 return finishedSum30(h.serverMD5, h.server, baseKey, ssl3ServerFinishedMagic[:])
Adam Langley95c29f32014-06-20 12:00:00 -0700326 }
327
David Benjamin8d315d72016-07-18 01:03:18 +0200328 if h.version < VersionTLS13 {
Nick Harperb41d2e42016-07-01 17:50:32 -0400329 out := make([]byte, finishedVerifyLength)
330 h.prf(out, baseKey, serverFinishedLabel, h.Sum())
331 return out
332 }
333
Steven Valdezc4aa7272016-10-03 12:25:56 -0400334 serverFinishedKey := hkdfExpandLabel(h.hash, baseKey, finishedLabel, nil, h.hash.Size())
Nick Harperb41d2e42016-07-01 17:50:32 -0400335 finishedHMAC := hmac.New(h.hash.New, serverFinishedKey)
336 finishedHMAC.Write(h.appendContextHashes(nil))
337 return finishedHMAC.Sum(nil)
Adam Langley95c29f32014-06-20 12:00:00 -0700338}
339
Nick Harper60edffd2016-06-21 15:19:24 -0700340// hashForClientCertificateSSL3 returns the hash to be signed for client
341// certificates in SSL 3.0.
342func (h finishedHash) hashForClientCertificateSSL3(masterSecret []byte) []byte {
343 md5Hash := md5.New()
344 md5Hash.Write(h.buffer)
345 sha1Hash := sha1.New()
346 sha1Hash.Write(h.buffer)
347 return finishedSum30(md5Hash, sha1Hash, masterSecret, nil)
Adam Langley95c29f32014-06-20 12:00:00 -0700348}
David Benjamind30a9902014-08-24 01:44:23 -0400349
350// hashForChannelID returns the hash to be signed for TLS Channel
351// ID. If a resumption, resumeHash has the previous handshake
352// hash. Otherwise, it is nil.
353func (h finishedHash) hashForChannelID(resumeHash []byte) []byte {
354 hash := sha256.New()
355 hash.Write(channelIDLabel)
356 if resumeHash != nil {
357 hash.Write(channelIDResumeLabel)
358 hash.Write(resumeHash)
359 }
360 hash.Write(h.server.Sum(nil))
361 return hash.Sum(nil)
362}
David Benjamine098ec22014-08-27 23:13:20 -0400363
364// discardHandshakeBuffer is called when there is no more need to
365// buffer the entirety of the handshake messages.
366func (h *finishedHash) discardHandshakeBuffer() {
367 h.buffer = nil
368}
Nick Harperb41d2e42016-07-01 17:50:32 -0400369
370// zeroSecretTLS13 returns the default all zeros secret for TLS 1.3, used when a
371// given secret is not available in the handshake. See draft-ietf-tls-tls13-13,
372// section 7.1.
373func (h *finishedHash) zeroSecret() []byte {
374 return make([]byte, h.hash.Size())
375}
376
377// setResumptionContext sets the TLS 1.3 resumption context.
378func (h *finishedHash) setResumptionContext(resumptionContext []byte) {
379 hash := h.hash.New()
380 hash.Write(resumptionContext)
381 h.resumptionContextHash = hash.Sum(nil)
382}
383
384// extractKey combines two secrets together with HKDF-Expand in the TLS 1.3 key
385// derivation schedule.
386func (h *finishedHash) extractKey(salt, ikm []byte) []byte {
387 return hkdfExtract(h.hash.New, salt, ikm)
388}
389
390// hkdfExpandLabel implements TLS 1.3's HKDF-Expand-Label function, as defined
391// in section 7.1 of draft-ietf-tls-tls13-13.
392func hkdfExpandLabel(hash crypto.Hash, secret, label, hashValue []byte, length int) []byte {
393 if len(label) > 255 || len(hashValue) > 255 {
394 panic("hkdfExpandLabel: label or hashValue too long")
395 }
396 hkdfLabel := make([]byte, 3+9+len(label)+1+len(hashValue))
397 x := hkdfLabel
398 x[0] = byte(length >> 8)
399 x[1] = byte(length)
400 x[2] = byte(9 + len(label))
401 x = x[3:]
402 copy(x, []byte("TLS 1.3, "))
403 x = x[9:]
404 copy(x, label)
405 x = x[len(label):]
406 x[0] = byte(len(hashValue))
407 copy(x[1:], hashValue)
408 return hkdfExpand(hash.New, secret, hkdfLabel, length)
409}
410
411// appendContextHashes returns the concatenation of the handshake hash and the
412// resumption context hash, as used in TLS 1.3.
413func (h *finishedHash) appendContextHashes(b []byte) []byte {
414 b = h.client.Sum(b)
415 b = append(b, h.resumptionContextHash...)
416 return b
417}
418
419// The following are labels for traffic secret derivation in TLS 1.3.
420var (
Steven Valdezc4aa7272016-10-03 12:25:56 -0400421 earlyTrafficLabel = []byte("client early traffic secret")
422 clientHandshakeTrafficLabel = []byte("client handshake traffic secret")
423 serverHandshakeTrafficLabel = []byte("server handshake traffic secret")
424 clientApplicationTrafficLabel = []byte("client application traffic secret")
425 serverApplicationTrafficLabel = []byte("server application traffic secret")
426 applicationTrafficLabel = []byte("application traffic secret")
427 exporterLabel = []byte("exporter master secret")
428 resumptionLabel = []byte("resumption master secret")
Nick Harperb41d2e42016-07-01 17:50:32 -0400429)
430
431// deriveSecret implements TLS 1.3's Derive-Secret function, as defined in
432// section 7.1 of draft ietf-tls-tls13-13.
433func (h *finishedHash) deriveSecret(secret, label []byte) []byte {
434 if h.resumptionContextHash == nil {
435 panic("Resumption context not set.")
436 }
437
438 return hkdfExpandLabel(h.hash, secret, label, h.appendContextHashes(nil), h.hash.Size())
439}
440
441// The following are context strings for CertificateVerify in TLS 1.3.
442var (
443 clientCertificateVerifyContextTLS13 = []byte("TLS 1.3, client CertificateVerify")
444 serverCertificateVerifyContextTLS13 = []byte("TLS 1.3, server CertificateVerify")
445)
446
447// certificateVerifyMessage returns the input to be signed for CertificateVerify
448// in TLS 1.3.
449func (h *finishedHash) certificateVerifyInput(context []byte) []byte {
450 const paddingLen = 64
451 b := make([]byte, paddingLen, paddingLen+len(context)+1+2*h.hash.Size())
452 for i := 0; i < paddingLen; i++ {
453 b[i] = 32
454 }
455 b = append(b, context...)
456 b = append(b, 0)
457 b = h.appendContextHashes(b)
458 return b
459}
460
461// The following are phase values for traffic key derivation in TLS 1.3.
462var (
463 earlyHandshakePhase = []byte("early handshake key expansion")
464 earlyApplicationPhase = []byte("early application data key expansion")
465 handshakePhase = []byte("handshake key expansion")
466 applicationPhase = []byte("application data key expansion")
467)
468
469type trafficDirection int
470
471const (
472 clientWrite trafficDirection = iota
473 serverWrite
474)
475
476// deriveTrafficAEAD derives traffic keys and constructs an AEAD given a traffic
477// secret.
David Benjaminc78aa4a2016-07-13 16:55:34 -0400478func deriveTrafficAEAD(version uint16, suite *cipherSuite, secret, phase []byte, side trafficDirection) interface{} {
Nick Harperb41d2e42016-07-01 17:50:32 -0400479 label := make([]byte, 0, len(phase)+2+16)
480 label = append(label, phase...)
Steven Valdezc4aa7272016-10-03 12:25:56 -0400481 label = append(label, []byte(", key")...)
Nick Harperb41d2e42016-07-01 17:50:32 -0400482 key := hkdfExpandLabel(suite.hash(), secret, label, nil, suite.keyLen)
483
484 label = label[:len(label)-3] // Remove "key" from the end.
485 label = append(label, []byte("iv")...)
486 iv := hkdfExpandLabel(suite.hash(), secret, label, nil, suite.ivLen(version))
487
488 return suite.aead(version, key, iv)
489}
David Benjamin21c00282016-07-18 21:56:23 +0200490
491func updateTrafficSecret(hash crypto.Hash, secret []byte) []byte {
492 return hkdfExpandLabel(hash, secret, applicationTrafficLabel, nil, hash.Size())
493}
Nick Harper0b3625b2016-07-25 16:16:28 -0700494
495func deriveResumptionPSK(suite *cipherSuite, resumptionSecret []byte) []byte {
496 return hkdfExpandLabel(suite.hash(), resumptionSecret, []byte("resumption psk"), nil, suite.hash().Size())
497}
498
499func deriveResumptionContext(suite *cipherSuite, resumptionSecret []byte) []byte {
500 return hkdfExpandLabel(suite.hash(), resumptionSecret, []byte("resumption context"), nil, suite.hash().Size())
501}