2012-03-27 23:13:14 +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 (
|
2014-09-21 17:33:12 +00:00
|
|
|
"crypto"
|
2012-03-27 23:13:14 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
2014-09-21 17:33:12 +00:00
|
|
|
"crypto/sha256"
|
2017-04-10 11:32:00 +00:00
|
|
|
"crypto/sha512"
|
|
|
|
"errors"
|
2012-03-27 23:13:14 +00:00
|
|
|
"hash"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Split a premaster secret in two as specified in RFC 4346, section 5.
|
|
|
|
func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {
|
|
|
|
s1 = secret[0 : (len(secret)+1)/2]
|
|
|
|
s2 = secret[len(secret)/2:]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// pHash implements the P_hash function, as defined in RFC 4346, section 5.
|
|
|
|
func pHash(result, secret, seed []byte, hash func() hash.Hash) {
|
|
|
|
h := hmac.New(hash, secret)
|
|
|
|
h.Write(seed)
|
|
|
|
a := h.Sum(nil)
|
|
|
|
|
|
|
|
j := 0
|
|
|
|
for j < len(result) {
|
|
|
|
h.Reset()
|
|
|
|
h.Write(a)
|
|
|
|
h.Write(seed)
|
|
|
|
b := h.Sum(nil)
|
|
|
|
todo := len(b)
|
|
|
|
if j+todo > len(result) {
|
|
|
|
todo = len(result) - j
|
|
|
|
}
|
|
|
|
copy(result[j:j+todo], b)
|
|
|
|
j += todo
|
|
|
|
|
|
|
|
h.Reset()
|
|
|
|
h.Write(a)
|
|
|
|
a = h.Sum(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// prf10 implements the TLS 1.0 pseudo-random function, as defined in RFC 2246, section 5.
|
|
|
|
func prf10(result, secret, label, seed []byte) {
|
2012-03-27 23:13:14 +00:00
|
|
|
hashSHA1 := sha1.New
|
|
|
|
hashMD5 := md5.New
|
|
|
|
|
|
|
|
labelAndSeed := make([]byte, len(label)+len(seed))
|
|
|
|
copy(labelAndSeed, label)
|
|
|
|
copy(labelAndSeed[len(label):], seed)
|
|
|
|
|
|
|
|
s1, s2 := splitPreMasterSecret(secret)
|
|
|
|
pHash(result, s1, labelAndSeed, hashMD5)
|
|
|
|
result2 := make([]byte, len(result))
|
|
|
|
pHash(result2, s2, labelAndSeed, hashSHA1)
|
|
|
|
|
|
|
|
for i, b := range result2 {
|
|
|
|
result[i] ^= b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// prf12 implements the TLS 1.2 pseudo-random function, as defined in RFC 5246, section 5.
|
2017-04-10 11:32:00 +00:00
|
|
|
func prf12(hashFunc func() hash.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)
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
pHash(result, secret, labelAndSeed, hashFunc)
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prf30 implements the SSL 3.0 pseudo-random function, as defined in
|
2012-03-27 23:13:14 +00:00
|
|
|
// www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 6.
|
2014-09-21 17:33:12 +00:00
|
|
|
func prf30(result, secret, label, seed []byte) {
|
2012-03-27 23:13:14 +00:00
|
|
|
hashSHA1 := sha1.New()
|
|
|
|
hashMD5 := md5.New()
|
|
|
|
|
|
|
|
done := 0
|
|
|
|
i := 0
|
2017-10-07 00:16:47 +00:00
|
|
|
// RFC 5246 section 6.3 says that the largest PRF output needed is 128
|
2012-03-27 23:13:14 +00:00
|
|
|
// 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)
|
|
|
|
digest := hashSHA1.Sum(nil)
|
|
|
|
|
|
|
|
hashMD5.Reset()
|
|
|
|
hashMD5.Write(secret)
|
|
|
|
hashMD5.Write(digest)
|
|
|
|
|
|
|
|
done += copy(result[done:], hashMD5.Sum(nil))
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
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.
|
|
|
|
)
|
|
|
|
|
|
|
|
var masterSecretLabel = []byte("master secret")
|
|
|
|
var keyExpansionLabel = []byte("key expansion")
|
|
|
|
var clientFinishedLabel = []byte("client finished")
|
|
|
|
var serverFinishedLabel = []byte("server finished")
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func prfAndHashForVersion(version uint16, suite *cipherSuite) (func(result, secret, label, seed []byte), crypto.Hash) {
|
2014-09-21 17:33:12 +00:00
|
|
|
switch version {
|
|
|
|
case VersionSSL30:
|
2017-04-10 11:32:00 +00:00
|
|
|
return prf30, crypto.Hash(0)
|
2014-09-21 17:33:12 +00:00
|
|
|
case VersionTLS10, VersionTLS11:
|
2017-04-10 11:32:00 +00:00
|
|
|
return prf10, crypto.Hash(0)
|
2014-09-21 17:33:12 +00:00
|
|
|
case VersionTLS12:
|
2017-04-10 11:32:00 +00:00
|
|
|
if suite.flags&suiteSHA384 != 0 {
|
|
|
|
return prf12(sha512.New384), crypto.SHA384
|
|
|
|
}
|
|
|
|
return prf12(sha256.New), crypto.SHA256
|
2014-09-21 17:33:12 +00:00
|
|
|
default:
|
|
|
|
panic("unknown version")
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func prfForVersion(version uint16, suite *cipherSuite) func(result, secret, label, seed []byte) {
|
|
|
|
prf, _ := prfAndHashForVersion(version, suite)
|
|
|
|
return prf
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// masterFromPreMasterSecret generates the master secret from the pre-master
|
|
|
|
// secret. See http://tools.ietf.org/html/rfc5246#section-8.1
|
2017-04-10 11:32:00 +00:00
|
|
|
func masterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret, clientRandom, serverRandom []byte) []byte {
|
|
|
|
seed := make([]byte, 0, len(clientRandom)+len(serverRandom))
|
|
|
|
seed = append(seed, clientRandom...)
|
|
|
|
seed = append(seed, serverRandom...)
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
masterSecret := make([]byte, masterSecretLength)
|
2017-04-10 11:32:00 +00:00
|
|
|
prfForVersion(version, suite)(masterSecret, preMasterSecret, masterSecretLabel, seed)
|
2014-09-21 17:33:12 +00:00
|
|
|
return masterSecret
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2014-09-21 17:33:12 +00: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.
|
2017-04-10 11:32:00 +00:00
|
|
|
func keysFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) {
|
|
|
|
seed := make([]byte, 0, len(serverRandom)+len(clientRandom))
|
|
|
|
seed = append(seed, serverRandom...)
|
|
|
|
seed = append(seed, clientRandom...)
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
n := 2*macLen + 2*keyLen + 2*ivLen
|
|
|
|
keyMaterial := make([]byte, n)
|
2017-04-10 11:32:00 +00:00
|
|
|
prfForVersion(version, suite)(keyMaterial, masterSecret, keyExpansionLabel, seed)
|
2012-03-27 23:13:14 +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]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// lookupTLSHash looks up the corresponding crypto.Hash for a given
|
|
|
|
// TLS hash identifier.
|
|
|
|
func lookupTLSHash(hash uint8) (crypto.Hash, error) {
|
|
|
|
switch hash {
|
|
|
|
case hashSHA1:
|
|
|
|
return crypto.SHA1, nil
|
|
|
|
case hashSHA256:
|
|
|
|
return crypto.SHA256, nil
|
|
|
|
case hashSHA384:
|
|
|
|
return crypto.SHA384, nil
|
|
|
|
default:
|
|
|
|
return 0, errors.New("tls: unsupported hash algorithm")
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFinishedHash(version uint16, cipherSuite *cipherSuite) finishedHash {
|
|
|
|
var buffer []byte
|
|
|
|
if version == VersionSSL30 || version >= VersionTLS12 {
|
|
|
|
buffer = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
prf, hash := prfAndHashForVersion(version, cipherSuite)
|
|
|
|
if hash != 0 {
|
|
|
|
return finishedHash{hash.New(), hash.New(), nil, nil, buffer, version, prf}
|
|
|
|
}
|
|
|
|
|
|
|
|
return finishedHash{sha1.New(), sha1.New(), md5.New(), md5.New(), buffer, version, prf}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A finishedHash calculates the hash of a set of handshake messages suitable
|
|
|
|
// for including in a Finished message.
|
|
|
|
type finishedHash struct {
|
2014-09-21 17:33:12 +00:00
|
|
|
client hash.Hash
|
|
|
|
server hash.Hash
|
|
|
|
|
|
|
|
// Prior to TLS 1.2, an additional MD5 hash is required.
|
|
|
|
clientMD5 hash.Hash
|
|
|
|
serverMD5 hash.Hash
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// In TLS 1.2, a full buffer is sadly required.
|
|
|
|
buffer []byte
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
version uint16
|
2017-04-10 11:32:00 +00:00
|
|
|
prf func(result, secret, label, seed []byte)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func (h *finishedHash) Write(msg []byte) (n int, err error) {
|
2014-09-21 17:33:12 +00:00
|
|
|
h.client.Write(msg)
|
|
|
|
h.server.Write(msg)
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
if h.version < VersionTLS12 {
|
|
|
|
h.clientMD5.Write(msg)
|
|
|
|
h.serverMD5.Write(msg)
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
if h.buffer != nil {
|
|
|
|
h.buffer = append(h.buffer, msg...)
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
return len(msg), nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func (h finishedHash) Sum() []byte {
|
|
|
|
if h.version >= VersionTLS12 {
|
|
|
|
return h.client.Sum(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]byte, 0, md5.Size+sha1.Size)
|
|
|
|
out = h.clientMD5.Sum(out)
|
|
|
|
return h.client.Sum(out)
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00: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.
|
2017-04-10 11:32:00 +00:00
|
|
|
func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic []byte) []byte {
|
|
|
|
md5.Write(magic)
|
2012-03-27 23:13:14 +00:00
|
|
|
md5.Write(masterSecret)
|
|
|
|
md5.Write(ssl30Pad1[:])
|
|
|
|
md5Digest := md5.Sum(nil)
|
|
|
|
|
|
|
|
md5.Reset()
|
|
|
|
md5.Write(masterSecret)
|
|
|
|
md5.Write(ssl30Pad2[:])
|
|
|
|
md5.Write(md5Digest)
|
|
|
|
md5Digest = md5.Sum(nil)
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
sha1.Write(magic)
|
2012-03-27 23:13:14 +00:00
|
|
|
sha1.Write(masterSecret)
|
|
|
|
sha1.Write(ssl30Pad1[:40])
|
|
|
|
sha1Digest := sha1.Sum(nil)
|
|
|
|
|
|
|
|
sha1.Reset()
|
|
|
|
sha1.Write(masterSecret)
|
|
|
|
sha1.Write(ssl30Pad2[:40])
|
|
|
|
sha1.Write(sha1Digest)
|
|
|
|
sha1Digest = sha1.Sum(nil)
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
// clientSum returns the contents of the verify_data member of a client's
|
|
|
|
// Finished message.
|
|
|
|
func (h finishedHash) clientSum(masterSecret []byte) []byte {
|
2014-09-21 17:33:12 +00:00
|
|
|
if h.version == VersionSSL30 {
|
2017-04-10 11:32:00 +00:00
|
|
|
return finishedSum30(h.clientMD5, h.client, masterSecret, ssl3ClientFinishedMagic[:])
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
out := make([]byte, finishedVerifyLength)
|
2017-04-10 11:32:00 +00:00
|
|
|
h.prf(out, masterSecret, clientFinishedLabel, h.Sum())
|
2014-09-21 17:33:12 +00:00
|
|
|
return out
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverSum returns the contents of the verify_data member of a server's
|
|
|
|
// Finished message.
|
|
|
|
func (h finishedHash) serverSum(masterSecret []byte) []byte {
|
2014-09-21 17:33:12 +00:00
|
|
|
if h.version == VersionSSL30 {
|
2017-04-10 11:32:00 +00:00
|
|
|
return finishedSum30(h.serverMD5, h.server, masterSecret, ssl3ServerFinishedMagic[:])
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]byte, finishedVerifyLength)
|
2017-04-10 11:32:00 +00:00
|
|
|
h.prf(out, masterSecret, serverFinishedLabel, h.Sum())
|
2014-09-21 17:33:12 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// selectClientCertSignatureAlgorithm returns a signatureAndHash to sign a
|
|
|
|
// client's CertificateVerify with, or an error if none can be found.
|
|
|
|
func (h finishedHash) selectClientCertSignatureAlgorithm(serverList []signatureAndHash, sigType uint8) (signatureAndHash, error) {
|
|
|
|
if h.version < VersionTLS12 {
|
|
|
|
// Nothing to negotiate before TLS 1.2.
|
|
|
|
return signatureAndHash{signature: sigType}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range serverList {
|
|
|
|
if v.signature == sigType && isSupportedSignatureAndHash(v, supportedSignatureAlgorithms) {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return signatureAndHash{}, errors.New("tls: no supported signature algorithm found for signing client certificate")
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// hashForClientCertificate returns a digest, hash function, and TLS 1.2 hash
|
|
|
|
// id suitable for signing by a TLS client certificate.
|
2017-04-10 11:32:00 +00:00
|
|
|
func (h finishedHash) hashForClientCertificate(signatureAndHash signatureAndHash, masterSecret []byte) ([]byte, crypto.Hash, error) {
|
|
|
|
if (h.version == VersionSSL30 || h.version >= VersionTLS12) && h.buffer == nil {
|
|
|
|
panic("a handshake hash for a client-certificate was requested after discarding the handshake buffer")
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.version == VersionSSL30 {
|
|
|
|
if signatureAndHash.signature != signatureRSA {
|
|
|
|
return nil, 0, errors.New("tls: unsupported signature type for client certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
md5Hash := md5.New()
|
|
|
|
md5Hash.Write(h.buffer)
|
|
|
|
sha1Hash := sha1.New()
|
|
|
|
sha1Hash.Write(h.buffer)
|
|
|
|
return finishedSum30(md5Hash, sha1Hash, masterSecret, nil), crypto.MD5SHA1, nil
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if h.version >= VersionTLS12 {
|
2017-04-10 11:32:00 +00:00
|
|
|
hashAlg, err := lookupTLSHash(signatureAndHash.hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
hash := hashAlg.New()
|
|
|
|
hash.Write(h.buffer)
|
|
|
|
return hash.Sum(nil), hashAlg, nil
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
if signatureAndHash.signature == signatureECDSA {
|
|
|
|
return h.server.Sum(nil), crypto.SHA1, nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
return h.Sum(), crypto.MD5SHA1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// discardHandshakeBuffer is called when there is no more need to
|
|
|
|
// buffer the entirety of the handshake messages.
|
|
|
|
func (h *finishedHash) discardHandshakeBuffer() {
|
|
|
|
h.buffer = nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|