2009-11-05 23:44:32 +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 (
|
2009-12-15 23:33:31 +00:00
|
|
|
"bytes"
|
|
|
|
"big"
|
|
|
|
"crypto/rsa"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"testing/script"
|
2009-11-05 23:44:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type zeroSource struct{}
|
|
|
|
|
|
|
|
func (zeroSource) Read(b []byte) (n int, err os.Error) {
|
|
|
|
for i := range b {
|
2009-11-09 20:07:39 +00:00
|
|
|
b[i] = 0
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
return len(b), nil
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var testConfig *Config
|
|
|
|
|
|
|
|
func init() {
|
2009-12-15 23:33:31 +00:00
|
|
|
testConfig = new(Config)
|
|
|
|
testConfig.Time = func() int64 { return 0 }
|
|
|
|
testConfig.Rand = zeroSource{}
|
|
|
|
testConfig.Certificates = make([]Certificate, 1)
|
|
|
|
testConfig.Certificates[0].Certificate = [][]byte{testCertificate}
|
|
|
|
testConfig.Certificates[0].PrivateKey = testPrivateKey
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupServerHandshake() (writeChan chan interface{}, controlChan chan interface{}, msgChan chan interface{}) {
|
2009-12-15 23:33:31 +00:00
|
|
|
sh := new(serverHandshake)
|
|
|
|
writeChan = make(chan interface{})
|
|
|
|
controlChan = make(chan interface{})
|
|
|
|
msgChan = make(chan interface{})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
go sh.loop(writeChan, controlChan, msgChan, testConfig)
|
|
|
|
return
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testClientHelloFailure(t *testing.T, clientHello interface{}, expectedAlert alertType) {
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan, controlChan, msgChan := setupServerHandshake()
|
|
|
|
defer close(msgChan)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
send := script.NewEvent("send", nil, script.Send{msgChan, clientHello})
|
|
|
|
recvAlert := script.NewEvent("recv alert", []*script.Event{send}, script.Recv{writeChan, alert{alertLevelError, expectedAlert}})
|
|
|
|
close1 := script.NewEvent("msgChan close", []*script.Event{recvAlert}, script.Closed{writeChan})
|
2009-12-23 19:13:09 +00:00
|
|
|
recvState := script.NewEvent("recv state", []*script.Event{send}, script.Recv{controlChan, ConnectionState{false, "", expectedAlert, ""}})
|
2009-12-15 23:33:31 +00:00
|
|
|
close2 := script.NewEvent("controlChan close", []*script.Event{recvState}, script.Closed{controlChan})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
err := script.Perform(0, []*script.Event{send, recvAlert, close1, recvState, close2})
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-11-09 20:07:39 +00:00
|
|
|
t.Errorf("Got error: %s", err)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleError(t *testing.T) {
|
2009-11-09 20:07:39 +00:00
|
|
|
testClientHelloFailure(t, &serverHelloDoneMsg{}, alertUnexpectedMessage)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var badProtocolVersions = []uint8{0, 0, 0, 5, 1, 0, 1, 5, 2, 0, 2, 5, 3, 0}
|
|
|
|
|
|
|
|
func TestRejectBadProtocolVersion(t *testing.T) {
|
2009-12-15 23:33:31 +00:00
|
|
|
clientHello := new(clientHelloMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(badProtocolVersions); i += 2 {
|
2009-12-15 23:33:31 +00:00
|
|
|
clientHello.major = badProtocolVersions[i]
|
|
|
|
clientHello.minor = badProtocolVersions[i+1]
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
testClientHelloFailure(t, clientHello, alertProtocolVersion)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoSuiteOverlap(t *testing.T) {
|
2009-12-23 19:13:09 +00:00
|
|
|
clientHello := &clientHelloMsg{nil, 3, 1, nil, nil, []uint16{0xff00}, []uint8{0}, false, ""}
|
2009-12-15 23:33:31 +00:00
|
|
|
testClientHelloFailure(t, clientHello, alertHandshakeFailure)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoCompressionOverlap(t *testing.T) {
|
2009-12-23 19:13:09 +00:00
|
|
|
clientHello := &clientHelloMsg{nil, 3, 1, nil, nil, []uint16{TLS_RSA_WITH_RC4_128_SHA}, []uint8{0xff}, false, ""}
|
2009-12-15 23:33:31 +00:00
|
|
|
testClientHelloFailure(t, clientHello, alertHandshakeFailure)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchServerHello(v interface{}) bool {
|
2009-12-15 23:33:31 +00:00
|
|
|
serverHello, ok := v.(*serverHelloMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-11-09 20:07:39 +00:00
|
|
|
return false
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
return serverHello.major == 3 &&
|
|
|
|
serverHello.minor == 2 &&
|
|
|
|
serverHello.cipherSuite == TLS_RSA_WITH_RC4_128_SHA &&
|
2009-12-15 23:33:31 +00:00
|
|
|
serverHello.compressionMethod == compressionNone
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertForwarding(t *testing.T) {
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan, controlChan, msgChan := setupServerHandshake()
|
|
|
|
defer close(msgChan)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
a := alert{alertLevelError, alertNoRenegotiation}
|
|
|
|
sendAlert := script.NewEvent("send alert", nil, script.Send{msgChan, a})
|
|
|
|
recvAlert := script.NewEvent("recv alert", []*script.Event{sendAlert}, script.Recv{writeChan, a})
|
|
|
|
closeWriter := script.NewEvent("close writer", []*script.Event{recvAlert}, script.Closed{writeChan})
|
|
|
|
closeControl := script.NewEvent("close control", []*script.Event{recvAlert}, script.Closed{controlChan})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
err := script.Perform(0, []*script.Event{sendAlert, recvAlert, closeWriter, closeControl})
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-11-09 20:07:39 +00:00
|
|
|
t.Errorf("Got error: %s", err)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan, controlChan, msgChan := setupServerHandshake()
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
close := script.NewEvent("close", nil, script.Close{msgChan})
|
|
|
|
closed1 := script.NewEvent("closed1", []*script.Event{close}, script.Closed{writeChan})
|
|
|
|
closed2 := script.NewEvent("closed2", []*script.Event{close}, script.Closed{controlChan})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
err := script.Perform(0, []*script.Event{close, closed1, closed2})
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-11-09 20:07:39 +00:00
|
|
|
t.Errorf("Got error: %s", err)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchCertificate(v interface{}) bool {
|
2009-12-15 23:33:31 +00:00
|
|
|
cert, ok := v.(*certificateMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-11-09 20:07:39 +00:00
|
|
|
return false
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
return len(cert.certificates) == 1 &&
|
2009-12-15 23:33:31 +00:00
|
|
|
bytes.Compare(cert.certificates[0], testCertificate) == 0
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchSetCipher(v interface{}) bool {
|
2009-12-15 23:33:31 +00:00
|
|
|
_, ok := v.(writerChangeCipherSpec)
|
|
|
|
return ok
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchDone(v interface{}) bool {
|
2009-12-15 23:33:31 +00:00
|
|
|
_, ok := v.(*serverHelloDoneMsg)
|
|
|
|
return ok
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchFinished(v interface{}) bool {
|
2009-12-15 23:33:31 +00:00
|
|
|
finished, ok := v.(*finishedMsg)
|
2009-11-05 23:44:32 +00:00
|
|
|
if !ok {
|
2009-11-09 20:07:39 +00:00
|
|
|
return false
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
return bytes.Compare(finished.verifyData, fromHex("29122ae11453e631487b02ed")) == 0
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchNewCipherSpec(v interface{}) bool {
|
2009-12-15 23:33:31 +00:00
|
|
|
_, ok := v.(*newCipherSpec)
|
|
|
|
return ok
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFullHandshake(t *testing.T) {
|
2009-12-15 23:33:31 +00:00
|
|
|
writeChan, controlChan, msgChan := setupServerHandshake()
|
|
|
|
defer close(msgChan)
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
// The values for this test were obtained from running `gnutls-cli --insecure --debug 9`
|
2009-12-23 19:13:09 +00:00
|
|
|
clientHello := &clientHelloMsg{fromHex("0100007603024aef7d77e4686d5dfd9d953dfe280788759ffd440867d687670216da45516b310000340033004500390088001600320044003800870013006600900091008f008e002f004100350084000a00050004008c008d008b008a01000019000900030200010000000e000c0000093132372e302e302e31"), 3, 2, fromHex("4aef7d77e4686d5dfd9d953dfe280788759ffd440867d687670216da45516b31"), nil, []uint16{0x33, 0x45, 0x39, 0x88, 0x16, 0x32, 0x44, 0x38, 0x87, 0x13, 0x66, 0x90, 0x91, 0x8f, 0x8e, 0x2f, 0x41, 0x35, 0x84, 0xa, 0x5, 0x4, 0x8c, 0x8d, 0x8b, 0x8a}, []uint8{0x0}, false, ""}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
sendHello := script.NewEvent("send hello", nil, script.Send{msgChan, clientHello})
|
|
|
|
setVersion := script.NewEvent("set version", []*script.Event{sendHello}, script.Recv{writeChan, writerSetVersion{3, 2}})
|
|
|
|
recvHello := script.NewEvent("recv hello", []*script.Event{setVersion}, script.RecvMatch{writeChan, matchServerHello})
|
|
|
|
recvCert := script.NewEvent("recv cert", []*script.Event{recvHello}, script.RecvMatch{writeChan, matchCertificate})
|
|
|
|
recvDone := script.NewEvent("recv done", []*script.Event{recvCert}, script.RecvMatch{writeChan, matchDone})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
ckx := &clientKeyExchangeMsg{nil, fromHex("872e1fee5f37dd86f3215938ac8de20b302b90074e9fb93097e6b7d1286d0f45abf2daf179deb618bb3c70ed0afee6ee24476ee4649e5a23358143c0f1d9c251")}
|
|
|
|
sendCKX := script.NewEvent("send ckx", []*script.Event{recvDone}, script.Send{msgChan, ckx})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
sendCCS := script.NewEvent("send ccs", []*script.Event{sendCKX}, script.Send{msgChan, changeCipherSpec{}})
|
|
|
|
recvNCS := script.NewEvent("recv done", []*script.Event{sendCCS}, script.RecvMatch{controlChan, matchNewCipherSpec})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
finished := &finishedMsg{nil, fromHex("c8faca5d242f4423325c5b1a")}
|
|
|
|
sendFinished := script.NewEvent("send finished", []*script.Event{recvNCS}, script.Send{msgChan, finished})
|
|
|
|
recvFinished := script.NewEvent("recv finished", []*script.Event{sendFinished}, script.RecvMatch{writeChan, matchFinished})
|
|
|
|
setCipher := script.NewEvent("set cipher", []*script.Event{sendFinished}, script.RecvMatch{writeChan, matchSetCipher})
|
2009-12-23 19:13:09 +00:00
|
|
|
recvConnectionState := script.NewEvent("recv state", []*script.Event{sendFinished}, script.Recv{controlChan, ConnectionState{true, "TLS_RSA_WITH_RC4_128_SHA", 0, ""}})
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
err := script.Perform(0, []*script.Event{sendHello, setVersion, recvHello, recvCert, recvDone, sendCKX, sendCCS, recvNCS, sendFinished, setCipher, recvConnectionState, recvFinished})
|
2009-11-05 23:44:32 +00:00
|
|
|
if err != nil {
|
2009-11-09 20:07:39 +00:00
|
|
|
t.Errorf("Got error: %s", err)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCertificate = fromHex("3082025930820203a003020102020900c2ec326b95228959300d06092a864886f70d01010505003054310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464310d300b0603550403130474657374301e170d3039313032303232323434355a170d3130313032303232323434355a3054310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464310d300b0603550403130474657374305c300d06092a864886f70d0101010500034b003048024100b2990f49c47dfa8cd400ae6a4d1b8a3b6a13642b23f28b003bfb97790ade9a4cc82b8b2a81747ddec08b6296e53a08c331687ef25c4bf4936ba1c0e6041e9d150203010001a381b73081b4301d0603551d0e0416041478a06086837c9293a8c9b70c0bdabdb9d77eeedf3081840603551d23047d307b801478a06086837c9293a8c9b70c0bdabdb9d77eeedfa158a4563054310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464310d300b0603550403130474657374820900c2ec326b95228959300c0603551d13040530030101ff300d06092a864886f70d0101050500034100ac23761ae1349d85a439caad4d0b932b09ea96de1917c3e0507c446f4838cb3076fb4d431db8c1987e96f1d7a8a2054dea3a64ec99a3f0eda4d47a163bf1f6ac")
|
|
|
|
|
|
|
|
func bigFromString(s string) *big.Int {
|
2009-12-15 23:33:31 +00:00
|
|
|
ret := new(big.Int)
|
|
|
|
ret.SetString(s, 10)
|
|
|
|
return ret
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var testPrivateKey = &rsa.PrivateKey{
|
|
|
|
PublicKey: rsa.PublicKey{
|
|
|
|
N: bigFromString("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077"),
|
|
|
|
E: 65537,
|
|
|
|
},
|
|
|
|
D: bigFromString("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861"),
|
|
|
|
P: bigFromString("98920366548084643601728869055592650835572950932266967461790948584315647051443"),
|
|
|
|
Q: bigFromString("94560208308847015747498523884063394671606671904944666360068158221458669711639"),
|
|
|
|
}
|