2009-11-03 02:25:20 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2013-07-03 00:58:56 +01:00
|
|
|
"crypto"
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
|
|
|
"hash"
|
2009-11-03 02:25:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Split a premaster secret in two as specified in RFC 4346, section 5.
|
|
|
|
func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {
|
2009-12-15 23:33:31 +00:00
|
|
|
s1 = secret[0 : (len(secret)+1)/2]
|
|
|
|
s2 = secret[len(secret)/2:]
|
|
|
|
return
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pHash implements the P_hash function, as defined in RFC 4346, section 5.
|
2010-08-26 18:32:29 +01:00
|
|
|
func pHash(result, secret, seed []byte, hash func() hash.Hash) {
|
2009-12-15 23:33:31 +00:00
|
|
|
h := hmac.New(hash, secret)
|
|
|
|
h.Write(seed)
|
2011-12-01 17:35:37 +00:00
|
|
|
a := h.Sum(nil)
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
j := 0
|
2009-11-03 02:25:20 +00:00
|
|
|
for j < len(result) {
|
2009-12-15 23:33:31 +00:00
|
|
|
h.Reset()
|
|
|
|
h.Write(a)
|
|
|
|
h.Write(seed)
|
2011-12-01 17:35:37 +00:00
|
|
|
b := h.Sum(nil)
|
2009-12-15 23:33:31 +00:00
|
|
|
todo := len(b)
|
2009-11-03 02:25:20 +00:00
|
|
|
if j+todo > len(result) {
|
2009-11-10 05:13:17 +00:00
|
|
|
todo = len(result) - j
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
copy(result[j:j+todo], b)
|
|
|
|
j += todo
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
h.Reset()
|
|
|
|
h.Write(a)
|
2011-12-01 17:35:37 +00:00
|
|
|
a = h.Sum(nil)
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 00:58:56 +01:00
|
|
|
// prf10 implements the TLS 1.0 pseudo-random function, as defined in RFC 2246, section 5.
|
|
|
|
func prf10(result, secret, label, seed []byte) {
|
2010-08-26 18:32:29 +01:00
|
|
|
hashSHA1 := sha1.New
|
|
|
|
hashMD5 := md5.New
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
labelAndSeed := make([]byte, len(label)+len(seed))
|
|
|
|
copy(labelAndSeed, label)
|
|
|
|
copy(labelAndSeed[len(label):], seed)
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
s1, s2 := splitPreMasterSecret(secret)
|
|
|
|
pHash(result, s1, labelAndSeed, hashMD5)
|
|
|
|
result2 := make([]byte, len(result))
|
|
|
|
pHash(result2, s2, labelAndSeed, hashSHA1)
|
2009-11-03 02:25:20 +00:00
|
|
|
|
|
|
|
for i, b := range result2 {
|
2009-11-09 20:07:39 +00:00
|
|
|
result[i] ^= b
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 00:15:18 +00:00
|
|
|
// prf12New returns a function implementing the TLS 1.2 pseudo-random function,
|
|
|
|
// as defined in RFC 5246, section 5, using the given hash.
|
|
|
|
func prf12New(tls12Hash crypto.Hash) func(result, secret, label, seed []byte) {
|
|
|
|
return func(result, secret, label, seed []byte) {
|
|
|
|
labelAndSeed := make([]byte, len(label)+len(seed))
|
|
|
|
copy(labelAndSeed, label)
|
|
|
|
copy(labelAndSeed[len(label):], seed)
|
|
|
|
pHash(result, secret, labelAndSeed, tls12Hash.New)
|
|
|
|
}
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// prf30 implements the SSL 3.0 pseudo-random function, as defined in
|
2011-09-14 20:32:19 +01:00
|
|
|
// www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 6.
|
2013-07-03 00:58:56 +01:00
|
|
|
func prf30(result, secret, label, seed []byte) {
|
2011-09-14 20:32:19 +01:00
|
|
|
hashSHA1 := sha1.New()
|
|
|
|
hashMD5 := md5.New()
|
|
|
|
|
|
|
|
done := 0
|
|
|
|
i := 0
|
|
|
|
// RFC5246 section 6.3 says that the largest PRF output needed is 128
|
|
|
|
// bytes. Since no more ciphersuites will be added to SSLv3, this will
|
|
|
|
// remain true. Each iteration gives us 16 bytes so 10 iterations will
|
|
|
|
// be sufficient.
|
|
|
|
var b [11]byte
|
|
|
|
for done < len(result) {
|
|
|
|
for j := 0; j <= i; j++ {
|
|
|
|
b[j] = 'A' + byte(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashSHA1.Reset()
|
|
|
|
hashSHA1.Write(b[:i+1])
|
|
|
|
hashSHA1.Write(secret)
|
|
|
|
hashSHA1.Write(seed)
|
2011-12-01 17:35:37 +00:00
|
|
|
digest := hashSHA1.Sum(nil)
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
hashMD5.Reset()
|
|
|
|
hashMD5.Write(secret)
|
|
|
|
hashMD5.Write(digest)
|
|
|
|
|
2011-12-01 17:35:37 +00:00
|
|
|
done += copy(result[done:], hashMD5.Sum(nil))
|
2011-09-14 20:32:19 +01:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 02:25:20 +00:00
|
|
|
const (
|
2009-12-15 23:33:31 +00:00
|
|
|
tlsRandomLength = 32 // Length of a random nonce in TLS 1.1.
|
|
|
|
masterSecretLength = 48 // Length of a master secret in TLS 1.1.
|
|
|
|
finishedVerifyLength = 12 // Length of verify_data in a Finished message.
|
2009-11-03 02:25:20 +00:00
|
|
|
)
|
|
|
|
|
2010-02-26 00:01:29 +00:00
|
|
|
var masterSecretLabel = []byte("master secret")
|
|
|
|
var keyExpansionLabel = []byte("key expansion")
|
|
|
|
var clientFinishedLabel = []byte("client finished")
|
|
|
|
var serverFinishedLabel = []byte("server finished")
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2015-02-04 00:15:18 +00:00
|
|
|
func prfForVersion(version uint16, tls12Hash crypto.Hash) func(result, secret, label, seed []byte) {
|
2013-07-03 00:58:56 +01:00
|
|
|
switch version {
|
|
|
|
case VersionSSL30:
|
|
|
|
return prf30
|
|
|
|
case VersionTLS10, VersionTLS11:
|
|
|
|
return prf10
|
|
|
|
case VersionTLS12:
|
2015-02-04 00:15:18 +00:00
|
|
|
return prf12New(tls12Hash)
|
2013-07-03 00:58:56 +01:00
|
|
|
default:
|
|
|
|
panic("unknown version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:52:43 +01:00
|
|
|
// masterFromPreMasterSecret generates the master secret from the pre-master
|
|
|
|
// secret. See http://tools.ietf.org/html/rfc5246#section-8.1
|
2015-02-04 00:15:18 +00:00
|
|
|
func masterFromPreMasterSecret(version uint16, tls12Hash crypto.Hash, preMasterSecret, clientRandom, serverRandom []byte) []byte {
|
2009-12-15 23:33:31 +00:00
|
|
|
var seed [tlsRandomLength * 2]byte
|
|
|
|
copy(seed[0:len(clientRandom)], clientRandom)
|
|
|
|
copy(seed[len(clientRandom):], serverRandom)
|
2012-09-24 21:52:43 +01:00
|
|
|
masterSecret := make([]byte, masterSecretLength)
|
2015-02-04 00:15:18 +00:00
|
|
|
prfForVersion(version, tls12Hash)(masterSecret, preMasterSecret, masterSecretLabel, seed[0:])
|
2012-09-24 21:52:43 +01:00
|
|
|
return masterSecret
|
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
|
2012-09-24 21:52:43 +01:00
|
|
|
// keysFromMasterSecret generates the connection keys from the master
|
|
|
|
// secret, given the lengths of the MAC key, cipher key and IV, as defined in
|
|
|
|
// RFC 2246, section 6.3.
|
2015-02-04 00:15:18 +00:00
|
|
|
func keysFromMasterSecret(version uint16, tls12Hash crypto.Hash, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) {
|
2012-09-24 21:52:43 +01:00
|
|
|
var seed [tlsRandomLength * 2]byte
|
2009-12-15 23:33:31 +00:00
|
|
|
copy(seed[0:len(clientRandom)], serverRandom)
|
|
|
|
copy(seed[len(serverRandom):], clientRandom)
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
n := 2*macLen + 2*keyLen + 2*ivLen
|
2009-12-15 23:33:31 +00:00
|
|
|
keyMaterial := make([]byte, n)
|
2015-02-04 00:15:18 +00:00
|
|
|
prfForVersion(version, tls12Hash)(keyMaterial, masterSecret, keyExpansionLabel, seed[0:])
|
2010-12-15 16:49:55 +00:00
|
|
|
clientMAC = keyMaterial[:macLen]
|
|
|
|
keyMaterial = keyMaterial[macLen:]
|
|
|
|
serverMAC = keyMaterial[:macLen]
|
|
|
|
keyMaterial = keyMaterial[macLen:]
|
|
|
|
clientKey = keyMaterial[:keyLen]
|
|
|
|
keyMaterial = keyMaterial[keyLen:]
|
|
|
|
serverKey = keyMaterial[:keyLen]
|
|
|
|
keyMaterial = keyMaterial[keyLen:]
|
|
|
|
clientIV = keyMaterial[:ivLen]
|
|
|
|
keyMaterial = keyMaterial[ivLen:]
|
|
|
|
serverIV = keyMaterial[:ivLen]
|
2009-12-15 23:33:31 +00:00
|
|
|
return
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 00:15:18 +00:00
|
|
|
func newFinishedHash(version uint16, tls12Hash crypto.Hash) finishedHash {
|
2013-07-03 00:58:56 +01:00
|
|
|
if version >= VersionTLS12 {
|
2015-02-04 00:15:18 +00:00
|
|
|
return finishedHash{tls12Hash.New(), tls12Hash.New(), nil, nil, version, prfForVersion(version, tls12Hash)}
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
2015-02-04 00:15:18 +00:00
|
|
|
return finishedHash{sha1.New(), sha1.New(), md5.New(), md5.New(), version, prfForVersion(version, tls12Hash)}
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
|
2009-11-03 02:25:20 +00:00
|
|
|
// A finishedHash calculates the hash of a set of handshake messages suitable
|
|
|
|
// for including in a Finished message.
|
|
|
|
type finishedHash struct {
|
2013-07-03 00:58:56 +01:00
|
|
|
client hash.Hash
|
|
|
|
server hash.Hash
|
|
|
|
|
|
|
|
// Prior to TLS 1.2, an additional MD5 hash is required.
|
|
|
|
clientMD5 hash.Hash
|
|
|
|
serverMD5 hash.Hash
|
|
|
|
|
|
|
|
version uint16
|
2015-02-04 00:15:18 +00:00
|
|
|
prf func(result, secret, label, seed []byte)
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (h finishedHash) Write(msg []byte) (n int, err error) {
|
2013-07-03 00:58:56 +01:00
|
|
|
h.client.Write(msg)
|
|
|
|
h.server.Write(msg)
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2013-07-03 00:58:56 +01:00
|
|
|
if h.version < VersionTLS12 {
|
|
|
|
h.clientMD5.Write(msg)
|
|
|
|
h.serverMD5.Write(msg)
|
|
|
|
}
|
|
|
|
return len(msg), nil
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 20:32:19 +01:00
|
|
|
// finishedSum30 calculates the contents of the verify_data member of a SSLv3
|
|
|
|
// Finished message given the MD5 and SHA1 hashes of a set of handshake
|
|
|
|
// messages.
|
|
|
|
func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic [4]byte) []byte {
|
|
|
|
md5.Write(magic[:])
|
|
|
|
md5.Write(masterSecret)
|
|
|
|
md5.Write(ssl30Pad1[:])
|
2011-12-01 17:35:37 +00:00
|
|
|
md5Digest := md5.Sum(nil)
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
md5.Reset()
|
|
|
|
md5.Write(masterSecret)
|
|
|
|
md5.Write(ssl30Pad2[:])
|
|
|
|
md5.Write(md5Digest)
|
2011-12-01 17:35:37 +00:00
|
|
|
md5Digest = md5.Sum(nil)
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
sha1.Write(magic[:])
|
|
|
|
sha1.Write(masterSecret)
|
|
|
|
sha1.Write(ssl30Pad1[:40])
|
2011-12-01 17:35:37 +00:00
|
|
|
sha1Digest := sha1.Sum(nil)
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
sha1.Reset()
|
|
|
|
sha1.Write(masterSecret)
|
|
|
|
sha1.Write(ssl30Pad2[:40])
|
|
|
|
sha1.Write(sha1Digest)
|
2011-12-01 17:35:37 +00:00
|
|
|
sha1Digest = sha1.Sum(nil)
|
2011-09-14 20:32:19 +01:00
|
|
|
|
|
|
|
ret := make([]byte, len(md5Digest)+len(sha1Digest))
|
|
|
|
copy(ret, md5Digest)
|
|
|
|
copy(ret[len(md5Digest):], sha1Digest)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
var ssl3ClientFinishedMagic = [4]byte{0x43, 0x4c, 0x4e, 0x54}
|
|
|
|
var ssl3ServerFinishedMagic = [4]byte{0x53, 0x52, 0x56, 0x52}
|
|
|
|
|
2009-11-03 02:25:20 +00:00
|
|
|
// clientSum returns the contents of the verify_data member of a client's
|
|
|
|
// Finished message.
|
|
|
|
func (h finishedHash) clientSum(masterSecret []byte) []byte {
|
2013-06-05 01:02:22 +01:00
|
|
|
if h.version == VersionSSL30 {
|
2013-07-03 00:58:56 +01:00
|
|
|
return finishedSum30(h.clientMD5, h.client, masterSecret, ssl3ClientFinishedMagic)
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 00:58:56 +01:00
|
|
|
out := make([]byte, finishedVerifyLength)
|
|
|
|
if h.version >= VersionTLS12 {
|
|
|
|
seed := h.client.Sum(nil)
|
2015-02-04 00:15:18 +00:00
|
|
|
h.prf(out, masterSecret, clientFinishedLabel, seed)
|
2013-07-03 00:58:56 +01:00
|
|
|
} else {
|
|
|
|
seed := make([]byte, 0, md5.Size+sha1.Size)
|
|
|
|
seed = h.clientMD5.Sum(seed)
|
|
|
|
seed = h.client.Sum(seed)
|
2015-02-04 00:15:18 +00:00
|
|
|
h.prf(out, masterSecret, clientFinishedLabel, seed)
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
|
|
|
return out
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverSum returns the contents of the verify_data member of a server's
|
|
|
|
// Finished message.
|
|
|
|
func (h finishedHash) serverSum(masterSecret []byte) []byte {
|
2013-06-05 01:02:22 +01:00
|
|
|
if h.version == VersionSSL30 {
|
2013-07-03 00:58:56 +01:00
|
|
|
return finishedSum30(h.serverMD5, h.server, masterSecret, ssl3ServerFinishedMagic)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]byte, finishedVerifyLength)
|
|
|
|
if h.version >= VersionTLS12 {
|
|
|
|
seed := h.server.Sum(nil)
|
2015-02-04 00:15:18 +00:00
|
|
|
h.prf(out, masterSecret, serverFinishedLabel, seed)
|
2013-07-03 00:58:56 +01:00
|
|
|
} else {
|
|
|
|
seed := make([]byte, 0, md5.Size+sha1.Size)
|
|
|
|
seed = h.serverMD5.Sum(seed)
|
|
|
|
seed = h.server.Sum(seed)
|
2015-02-04 00:15:18 +00:00
|
|
|
h.prf(out, masterSecret, serverFinishedLabel, seed)
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:39:42 +01:00
|
|
|
// hashForClientCertificate returns a digest, hash function, and TLS 1.2 hash
|
|
|
|
// id suitable for signing by a TLS client certificate.
|
|
|
|
func (h finishedHash) hashForClientCertificate(sigType uint8) ([]byte, crypto.Hash, uint8) {
|
2013-07-03 00:58:56 +01:00
|
|
|
if h.version >= VersionTLS12 {
|
|
|
|
digest := h.server.Sum(nil)
|
2013-09-16 21:39:42 +01:00
|
|
|
return digest, crypto.SHA256, hashSHA256
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
2013-07-17 17:33:16 +01:00
|
|
|
if sigType == signatureECDSA {
|
|
|
|
digest := h.server.Sum(nil)
|
2013-09-16 21:39:42 +01:00
|
|
|
return digest, crypto.SHA1, hashSHA1
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
2011-09-14 20:32:19 +01:00
|
|
|
|
2013-07-03 00:58:56 +01:00
|
|
|
digest := make([]byte, 0, 36)
|
|
|
|
digest = h.serverMD5.Sum(digest)
|
|
|
|
digest = h.server.Sum(digest)
|
2013-09-16 21:39:42 +01:00
|
|
|
return digest, crypto.MD5SHA1, 0 /* not specified in TLS 1.2. */
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|