Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
Adam Langley | dc7e9c4 | 2015-09-29 15:21:04 -0700 | [diff] [blame] | 5 | package runner |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 8 | "crypto" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 9 | "crypto/hmac" |
| 10 | "crypto/md5" |
| 11 | "crypto/sha1" |
| 12 | "crypto/sha256" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 13 | "hash" |
| 14 | ) |
| 15 | |
| 16 | // Split a premaster secret in two as specified in RFC 4346, section 5. |
| 17 | func 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. |
| 24 | func 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. |
| 49 | func 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. |
| 68 | func 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. |
| 80 | func 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 | |
| 111 | const ( |
| 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 | |
| 117 | var masterSecretLabel = []byte("master secret") |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 118 | var extendedMasterSecretLabel = []byte("extended master secret") |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | var keyExpansionLabel = []byte("key expansion") |
| 120 | var clientFinishedLabel = []byte("client finished") |
| 121 | var serverFinishedLabel = []byte("server finished") |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 122 | var finishedLabel = []byte("finished") |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 123 | var channelIDLabel = []byte("TLS Channel ID signature\x00") |
| 124 | var channelIDResumeLabel = []byte("Resumption\x00") |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | |
| 126 | func 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 Harper | dfec182 | 2016-12-06 13:45:45 -0800 | [diff] [blame] | 132 | case VersionTLS12: |
| 133 | return prf12(suite.hash().New) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | } |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 135 | panic("unknown version") |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // masterFromPreMasterSecret generates the master secret from the pre-master |
| 139 | // secret. See http://tools.ietf.org/html/rfc5246#section-8.1 |
| 140 | func masterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret, clientRandom, serverRandom []byte) []byte { |
| 141 | var seed [tlsRandomLength * 2]byte |
| 142 | copy(seed[0:len(clientRandom)], clientRandom) |
| 143 | copy(seed[len(clientRandom):], serverRandom) |
| 144 | masterSecret := make([]byte, masterSecretLength) |
| 145 | prfForVersion(version, suite)(masterSecret, preMasterSecret, masterSecretLabel, seed[0:]) |
| 146 | return masterSecret |
| 147 | } |
| 148 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 149 | // extendedMasterFromPreMasterSecret generates the master secret from the |
| 150 | // pre-master secret when the Triple Handshake fix is in effect. See |
David Benjamin | 43946d4 | 2016-02-01 08:42:19 -0500 | [diff] [blame] | 151 | // https://tools.ietf.org/html/rfc7627 |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 152 | func extendedMasterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret []byte, h finishedHash) []byte { |
| 153 | masterSecret := make([]byte, masterSecretLength) |
| 154 | prfForVersion(version, suite)(masterSecret, preMasterSecret, extendedMasterSecretLabel, h.Sum()) |
| 155 | return masterSecret |
| 156 | } |
| 157 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | // keysFromMasterSecret generates the connection keys from the master |
| 159 | // secret, given the lengths of the MAC key, cipher key and IV, as defined in |
| 160 | // RFC 2246, section 6.3. |
| 161 | func keysFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) { |
| 162 | var seed [tlsRandomLength * 2]byte |
| 163 | copy(seed[0:len(clientRandom)], serverRandom) |
| 164 | copy(seed[len(serverRandom):], clientRandom) |
| 165 | |
| 166 | n := 2*macLen + 2*keyLen + 2*ivLen |
| 167 | keyMaterial := make([]byte, n) |
| 168 | prfForVersion(version, suite)(keyMaterial, masterSecret, keyExpansionLabel, seed[0:]) |
| 169 | clientMAC = keyMaterial[:macLen] |
| 170 | keyMaterial = keyMaterial[macLen:] |
| 171 | serverMAC = keyMaterial[:macLen] |
| 172 | keyMaterial = keyMaterial[macLen:] |
| 173 | clientKey = keyMaterial[:keyLen] |
| 174 | keyMaterial = keyMaterial[keyLen:] |
| 175 | serverKey = keyMaterial[:keyLen] |
| 176 | keyMaterial = keyMaterial[keyLen:] |
| 177 | clientIV = keyMaterial[:ivLen] |
| 178 | keyMaterial = keyMaterial[ivLen:] |
| 179 | serverIV = keyMaterial[:ivLen] |
| 180 | return |
| 181 | } |
| 182 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 183 | func newFinishedHash(wireVersion uint16, isDTLS bool, cipherSuite *cipherSuite) finishedHash { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 184 | var ret finishedHash |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 186 | version, ok := wireToVersion(wireVersion, isDTLS) |
| 187 | if !ok { |
| 188 | panic("unknown version") |
| 189 | } |
| 190 | |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 191 | if version >= VersionTLS12 { |
| 192 | ret.hash = cipherSuite.hash() |
| 193 | |
| 194 | ret.client = ret.hash.New() |
| 195 | ret.server = ret.hash.New() |
| 196 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 197 | if version == VersionTLS12 { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 198 | ret.prf = prf12(ret.hash.New) |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 199 | } else { |
| 200 | ret.secret = make([]byte, ret.hash.Size()) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 201 | } |
| 202 | } else { |
| 203 | ret.hash = crypto.MD5SHA1 |
| 204 | |
| 205 | ret.client = sha1.New() |
| 206 | ret.server = sha1.New() |
| 207 | ret.clientMD5 = md5.New() |
| 208 | ret.serverMD5 = md5.New() |
| 209 | |
| 210 | ret.prf = prf10 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | } |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 212 | |
| 213 | ret.buffer = []byte{} |
| 214 | ret.version = version |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 215 | ret.wireVersion = wireVersion |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 216 | return ret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // A finishedHash calculates the hash of a set of handshake messages suitable |
| 220 | // for including in a Finished message. |
| 221 | type finishedHash struct { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 222 | hash crypto.Hash |
| 223 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | client hash.Hash |
| 225 | server hash.Hash |
| 226 | |
| 227 | // Prior to TLS 1.2, an additional MD5 hash is required. |
| 228 | clientMD5 hash.Hash |
| 229 | serverMD5 hash.Hash |
| 230 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 231 | // In TLS 1.2 (and SSL 3 for implementation convenience), a |
| 232 | // full buffer is required. |
| 233 | buffer []byte |
| 234 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 235 | version uint16 |
| 236 | wireVersion uint16 |
| 237 | prf func(result, secret, label, seed []byte) |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 238 | |
| 239 | // secret, in TLS 1.3, is the running input secret. |
| 240 | secret []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 243 | func (h *finishedHash) UpdateForHelloRetryRequest() (err error) { |
| 244 | data := newByteBuilder() |
| 245 | data.addU8(typeMessageHash) |
| 246 | data.addU24(h.hash.Size()) |
| 247 | data.addBytes(h.Sum()) |
| 248 | h.client = h.hash.New() |
| 249 | h.server = h.hash.New() |
| 250 | if h.buffer != nil { |
| 251 | h.buffer = []byte{} |
| 252 | } |
| 253 | h.Write(data.finish()) |
| 254 | return nil |
| 255 | } |
| 256 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 257 | func (h *finishedHash) Write(msg []byte) (n int, err error) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 258 | h.client.Write(msg) |
| 259 | h.server.Write(msg) |
| 260 | |
| 261 | if h.version < VersionTLS12 { |
| 262 | h.clientMD5.Write(msg) |
| 263 | h.serverMD5.Write(msg) |
| 264 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 265 | |
| 266 | if h.buffer != nil { |
| 267 | h.buffer = append(h.buffer, msg...) |
| 268 | } |
| 269 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | return len(msg), nil |
| 271 | } |
| 272 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 273 | func (h finishedHash) Sum() []byte { |
| 274 | if h.version >= VersionTLS12 { |
| 275 | return h.client.Sum(nil) |
| 276 | } |
| 277 | |
| 278 | out := make([]byte, 0, md5.Size+sha1.Size) |
| 279 | out = h.clientMD5.Sum(out) |
| 280 | return h.client.Sum(out) |
| 281 | } |
| 282 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | // finishedSum30 calculates the contents of the verify_data member of a SSLv3 |
| 284 | // Finished message given the MD5 and SHA1 hashes of a set of handshake |
| 285 | // messages. |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 286 | func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic []byte) []byte { |
| 287 | md5.Write(magic) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | md5.Write(masterSecret) |
| 289 | md5.Write(ssl30Pad1[:]) |
| 290 | md5Digest := md5.Sum(nil) |
| 291 | |
| 292 | md5.Reset() |
| 293 | md5.Write(masterSecret) |
| 294 | md5.Write(ssl30Pad2[:]) |
| 295 | md5.Write(md5Digest) |
| 296 | md5Digest = md5.Sum(nil) |
| 297 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 298 | sha1.Write(magic) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | sha1.Write(masterSecret) |
| 300 | sha1.Write(ssl30Pad1[:40]) |
| 301 | sha1Digest := sha1.Sum(nil) |
| 302 | |
| 303 | sha1.Reset() |
| 304 | sha1.Write(masterSecret) |
| 305 | sha1.Write(ssl30Pad2[:40]) |
| 306 | sha1.Write(sha1Digest) |
| 307 | sha1Digest = sha1.Sum(nil) |
| 308 | |
| 309 | ret := make([]byte, len(md5Digest)+len(sha1Digest)) |
| 310 | copy(ret, md5Digest) |
| 311 | copy(ret[len(md5Digest):], sha1Digest) |
| 312 | return ret |
| 313 | } |
| 314 | |
| 315 | var ssl3ClientFinishedMagic = [4]byte{0x43, 0x4c, 0x4e, 0x54} |
| 316 | var ssl3ServerFinishedMagic = [4]byte{0x53, 0x52, 0x56, 0x52} |
| 317 | |
| 318 | // clientSum returns the contents of the verify_data member of a client's |
| 319 | // Finished message. |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 320 | func (h finishedHash) clientSum(baseKey []byte) []byte { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | if h.version == VersionSSL30 { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 322 | return finishedSum30(h.clientMD5, h.client, baseKey, ssl3ClientFinishedMagic[:]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | } |
| 324 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 325 | if h.version < VersionTLS13 { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 326 | out := make([]byte, finishedVerifyLength) |
| 327 | h.prf(out, baseKey, clientFinishedLabel, h.Sum()) |
| 328 | return out |
| 329 | } |
| 330 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 331 | clientFinishedKey := hkdfExpandLabel(h.hash, baseKey, finishedLabel, nil, h.hash.Size()) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 332 | finishedHMAC := hmac.New(h.hash.New, clientFinishedKey) |
| 333 | finishedHMAC.Write(h.appendContextHashes(nil)) |
| 334 | return finishedHMAC.Sum(nil) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // serverSum returns the contents of the verify_data member of a server's |
| 338 | // Finished message. |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 339 | func (h finishedHash) serverSum(baseKey []byte) []byte { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 340 | if h.version == VersionSSL30 { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 341 | return finishedSum30(h.serverMD5, h.server, baseKey, ssl3ServerFinishedMagic[:]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 342 | } |
| 343 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 344 | if h.version < VersionTLS13 { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 345 | out := make([]byte, finishedVerifyLength) |
| 346 | h.prf(out, baseKey, serverFinishedLabel, h.Sum()) |
| 347 | return out |
| 348 | } |
| 349 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 350 | serverFinishedKey := hkdfExpandLabel(h.hash, baseKey, finishedLabel, nil, h.hash.Size()) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 351 | finishedHMAC := hmac.New(h.hash.New, serverFinishedKey) |
| 352 | finishedHMAC.Write(h.appendContextHashes(nil)) |
| 353 | return finishedHMAC.Sum(nil) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 356 | // hashForClientCertificateSSL3 returns the hash to be signed for client |
| 357 | // certificates in SSL 3.0. |
| 358 | func (h finishedHash) hashForClientCertificateSSL3(masterSecret []byte) []byte { |
| 359 | md5Hash := md5.New() |
| 360 | md5Hash.Write(h.buffer) |
| 361 | sha1Hash := sha1.New() |
| 362 | sha1Hash.Write(h.buffer) |
| 363 | return finishedSum30(md5Hash, sha1Hash, masterSecret, nil) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 365 | |
| 366 | // hashForChannelID returns the hash to be signed for TLS Channel |
| 367 | // ID. If a resumption, resumeHash has the previous handshake |
| 368 | // hash. Otherwise, it is nil. |
| 369 | func (h finishedHash) hashForChannelID(resumeHash []byte) []byte { |
| 370 | hash := sha256.New() |
| 371 | hash.Write(channelIDLabel) |
| 372 | if resumeHash != nil { |
| 373 | hash.Write(channelIDResumeLabel) |
| 374 | hash.Write(resumeHash) |
| 375 | } |
Nick Harper | c984611 | 2016-10-17 15:05:35 -0700 | [diff] [blame] | 376 | hash.Write(h.Sum()) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 377 | return hash.Sum(nil) |
| 378 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 379 | |
| 380 | // discardHandshakeBuffer is called when there is no more need to |
| 381 | // buffer the entirety of the handshake messages. |
| 382 | func (h *finishedHash) discardHandshakeBuffer() { |
| 383 | h.buffer = nil |
| 384 | } |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 385 | |
| 386 | // zeroSecretTLS13 returns the default all zeros secret for TLS 1.3, used when a |
David Benjamin | a128a55 | 2016-10-13 14:26:33 -0400 | [diff] [blame] | 387 | // given secret is not available in the handshake. See draft-ietf-tls-tls13-16, |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 388 | // section 7.1. |
| 389 | func (h *finishedHash) zeroSecret() []byte { |
| 390 | return make([]byte, h.hash.Size()) |
| 391 | } |
| 392 | |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 393 | // addEntropy incorporates ikm into the running TLS 1.3 secret with HKDF-Expand. |
| 394 | func (h *finishedHash) addEntropy(ikm []byte) { |
| 395 | h.secret = hkdfExtract(h.hash.New, h.secret, ikm) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 398 | func (h *finishedHash) nextSecret() { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 399 | h.secret = hkdfExpandLabel(h.hash, h.secret, []byte("derived"), h.hash.New().Sum(nil), h.hash.Size()) |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 402 | // hkdfExpandLabel implements TLS 1.3's HKDF-Expand-Label function, as defined |
David Benjamin | a128a55 | 2016-10-13 14:26:33 -0400 | [diff] [blame] | 403 | // in section 7.1 of draft-ietf-tls-tls13-16. |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 404 | func hkdfExpandLabel(hash crypto.Hash, secret, label, hashValue []byte, length int) []byte { |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 405 | if len(label) > 255 || len(hashValue) > 255 { |
| 406 | panic("hkdfExpandLabel: label or hashValue too long") |
| 407 | } |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 408 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 409 | versionLabel := []byte("tls13 ") |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 410 | hkdfLabel := make([]byte, 3+len(versionLabel)+len(label)+1+len(hashValue)) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 411 | x := hkdfLabel |
| 412 | x[0] = byte(length >> 8) |
| 413 | x[1] = byte(length) |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 414 | x[2] = byte(len(versionLabel) + len(label)) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 415 | x = x[3:] |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 416 | copy(x, versionLabel) |
| 417 | x = x[len(versionLabel):] |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 418 | copy(x, label) |
| 419 | x = x[len(label):] |
| 420 | x[0] = byte(len(hashValue)) |
| 421 | copy(x[1:], hashValue) |
| 422 | return hkdfExpand(hash.New, secret, hkdfLabel, length) |
| 423 | } |
| 424 | |
| 425 | // appendContextHashes returns the concatenation of the handshake hash and the |
| 426 | // resumption context hash, as used in TLS 1.3. |
| 427 | func (h *finishedHash) appendContextHashes(b []byte) []byte { |
| 428 | b = h.client.Sum(b) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 429 | return b |
| 430 | } |
| 431 | |
| 432 | // The following are labels for traffic secret derivation in TLS 1.3. |
| 433 | var ( |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 434 | externalPSKBinderLabel = []byte("ext binder") |
| 435 | resumptionPSKBinderLabel = []byte("res binder") |
| 436 | earlyTrafficLabel = []byte("c e traffic") |
| 437 | clientHandshakeTrafficLabel = []byte("c hs traffic") |
| 438 | serverHandshakeTrafficLabel = []byte("s hs traffic") |
| 439 | clientApplicationTrafficLabel = []byte("c ap traffic") |
| 440 | serverApplicationTrafficLabel = []byte("s ap traffic") |
| 441 | applicationTrafficLabel = []byte("traffic upd") |
| 442 | earlyExporterLabel = []byte("e exp master") |
| 443 | exporterLabel = []byte("exp master") |
| 444 | resumptionLabel = []byte("res master") |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 445 | |
| 446 | resumptionPSKLabel = []byte("resumption") |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 447 | ) |
| 448 | |
| 449 | // deriveSecret implements TLS 1.3's Derive-Secret function, as defined in |
David Benjamin | a128a55 | 2016-10-13 14:26:33 -0400 | [diff] [blame] | 450 | // section 7.1 of draft ietf-tls-tls13-16. |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 451 | func (h *finishedHash) deriveSecret(label []byte) []byte { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 452 | return hkdfExpandLabel(h.hash, h.secret, label, h.appendContextHashes(nil), h.hash.Size()) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // The following are context strings for CertificateVerify in TLS 1.3. |
| 456 | var ( |
| 457 | clientCertificateVerifyContextTLS13 = []byte("TLS 1.3, client CertificateVerify") |
| 458 | serverCertificateVerifyContextTLS13 = []byte("TLS 1.3, server CertificateVerify") |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 459 | channelIDContextTLS13 = []byte("TLS 1.3, Channel ID") |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 460 | ) |
| 461 | |
| 462 | // certificateVerifyMessage returns the input to be signed for CertificateVerify |
| 463 | // in TLS 1.3. |
| 464 | func (h *finishedHash) certificateVerifyInput(context []byte) []byte { |
| 465 | const paddingLen = 64 |
| 466 | b := make([]byte, paddingLen, paddingLen+len(context)+1+2*h.hash.Size()) |
| 467 | for i := 0; i < paddingLen; i++ { |
| 468 | b[i] = 32 |
| 469 | } |
| 470 | b = append(b, context...) |
| 471 | b = append(b, 0) |
| 472 | b = h.appendContextHashes(b) |
| 473 | return b |
| 474 | } |
| 475 | |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 476 | type trafficDirection int |
| 477 | |
| 478 | const ( |
| 479 | clientWrite trafficDirection = iota |
| 480 | serverWrite |
| 481 | ) |
| 482 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 483 | var ( |
| 484 | keyTLS13 = []byte("key") |
| 485 | ivTLS13 = []byte("iv") |
| 486 | ) |
| 487 | |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 488 | // deriveTrafficAEAD derives traffic keys and constructs an AEAD given a traffic |
| 489 | // secret. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 490 | func deriveTrafficAEAD(version uint16, suite *cipherSuite, secret []byte, side trafficDirection) interface{} { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 491 | key := hkdfExpandLabel(suite.hash(), secret, keyTLS13, nil, suite.keyLen) |
| 492 | iv := hkdfExpandLabel(suite.hash(), secret, ivTLS13, nil, suite.ivLen(version)) |
Nick Harper | b41d2e4 | 2016-07-01 17:50:32 -0400 | [diff] [blame] | 493 | |
| 494 | return suite.aead(version, key, iv) |
| 495 | } |
David Benjamin | 21c0028 | 2016-07-18 21:56:23 +0200 | [diff] [blame] | 496 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 497 | func updateTrafficSecret(hash crypto.Hash, version uint16, secret []byte) []byte { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 498 | return hkdfExpandLabel(hash, secret, applicationTrafficLabel, nil, hash.Size()) |
David Benjamin | 21c0028 | 2016-07-18 21:56:23 +0200 | [diff] [blame] | 499 | } |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame] | 500 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 501 | func computePSKBinder(psk []byte, version uint16, label []byte, cipherSuite *cipherSuite, clientHello, helloRetryRequest, truncatedHello []byte) []byte { |
| 502 | finishedHash := newFinishedHash(version, false, cipherSuite) |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 503 | finishedHash.addEntropy(psk) |
| 504 | binderKey := finishedHash.deriveSecret(label) |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 505 | finishedHash.Write(clientHello) |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 506 | if len(helloRetryRequest) != 0 { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 507 | finishedHash.UpdateForHelloRetryRequest() |
| 508 | } |
| 509 | finishedHash.Write(helloRetryRequest) |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 510 | finishedHash.Write(truncatedHello) |
| 511 | return finishedHash.clientSum(binderKey) |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame] | 512 | } |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 513 | |
| 514 | func deriveSessionPSK(suite *cipherSuite, version uint16, masterSecret []byte, nonce []byte) []byte { |
| 515 | hash := suite.hash() |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame^] | 516 | return hkdfExpandLabel(hash, masterSecret, resumptionPSKLabel, nonce, hash.Size()) |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 517 | } |