2014-06-20 20:00:00 +01: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.
|
|
|
|
|
2015-09-29 23:21:04 +01:00
|
|
|
package runner
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-07-26 00:16:28 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2017-07-14 23:40:34 +01:00
|
|
|
"fmt"
|
2016-07-26 00:16:28 +01:00
|
|
|
)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
func writeLen(buf []byte, v, size int) {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
buf[size-i-1] = byte(v)
|
|
|
|
v >>= 8
|
|
|
|
}
|
|
|
|
if v != 0 {
|
|
|
|
panic("length is too long")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type byteBuilder struct {
|
|
|
|
buf *[]byte
|
|
|
|
start int
|
|
|
|
prefixLen int
|
|
|
|
child *byteBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
func newByteBuilder() *byteBuilder {
|
|
|
|
buf := make([]byte, 0, 32)
|
|
|
|
return &byteBuilder{buf: &buf}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) len() int {
|
|
|
|
return len(*bb.buf) - bb.start - bb.prefixLen
|
|
|
|
}
|
|
|
|
|
2017-07-14 23:40:34 +01:00
|
|
|
func (bb *byteBuilder) data() []byte {
|
|
|
|
bb.flush()
|
|
|
|
return (*bb.buf)[bb.start+bb.prefixLen:]
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
func (bb *byteBuilder) flush() {
|
|
|
|
if bb.child == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bb.child.flush()
|
|
|
|
writeLen((*bb.buf)[bb.child.start:], bb.child.len(), bb.child.prefixLen)
|
|
|
|
bb.child = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) finish() []byte {
|
|
|
|
bb.flush()
|
|
|
|
return *bb.buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) addU8(u uint8) {
|
|
|
|
bb.flush()
|
|
|
|
*bb.buf = append(*bb.buf, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) addU16(u uint16) {
|
|
|
|
bb.flush()
|
|
|
|
*bb.buf = append(*bb.buf, byte(u>>8), byte(u))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) addU24(u int) {
|
|
|
|
bb.flush()
|
|
|
|
*bb.buf = append(*bb.buf, byte(u>>16), byte(u>>8), byte(u))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) addU32(u uint32) {
|
|
|
|
bb.flush()
|
|
|
|
*bb.buf = append(*bb.buf, byte(u>>24), byte(u>>16), byte(u>>8), byte(u))
|
|
|
|
}
|
|
|
|
|
2016-07-26 00:16:28 +01:00
|
|
|
func (bb *byteBuilder) addU64(u uint64) {
|
|
|
|
bb.flush()
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], u)
|
|
|
|
*bb.buf = append(*bb.buf, b[:]...)
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
func (bb *byteBuilder) addU8LengthPrefixed() *byteBuilder {
|
|
|
|
return bb.createChild(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) addU16LengthPrefixed() *byteBuilder {
|
|
|
|
return bb.createChild(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) addU24LengthPrefixed() *byteBuilder {
|
|
|
|
return bb.createChild(3)
|
|
|
|
}
|
|
|
|
|
2016-07-26 00:16:28 +01:00
|
|
|
func (bb *byteBuilder) addU32LengthPrefixed() *byteBuilder {
|
|
|
|
return bb.createChild(4)
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
func (bb *byteBuilder) addBytes(b []byte) {
|
|
|
|
bb.flush()
|
|
|
|
*bb.buf = append(*bb.buf, b...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) createChild(lengthPrefixSize int) *byteBuilder {
|
|
|
|
bb.flush()
|
|
|
|
bb.child = &byteBuilder{
|
|
|
|
buf: bb.buf,
|
|
|
|
start: len(*bb.buf),
|
|
|
|
prefixLen: lengthPrefixSize,
|
|
|
|
}
|
|
|
|
for i := 0; i < lengthPrefixSize; i++ {
|
|
|
|
*bb.buf = append(*bb.buf, 0)
|
|
|
|
}
|
|
|
|
return bb.child
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *byteBuilder) discardChild() {
|
2017-07-14 23:40:34 +01:00
|
|
|
if bb.child == nil {
|
2016-06-30 23:51:11 +01:00
|
|
|
return
|
|
|
|
}
|
2017-07-14 23:40:34 +01:00
|
|
|
*bb.buf = (*bb.buf)[:bb.child.start]
|
2016-06-30 23:51:11 +01:00
|
|
|
bb.child = nil
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:00:39 +00:00
|
|
|
type byteReader []byte
|
|
|
|
|
|
|
|
func (br *byteReader) readInternal(out *byteReader, n int) bool {
|
|
|
|
if len(*br) < n {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = (*br)[:n]
|
|
|
|
*br = (*br)[n:]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readBytes(out *[]byte, n int) bool {
|
|
|
|
var child byteReader
|
|
|
|
if !br.readInternal(&child, n) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = []byte(child)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readUint(out *uint64, n int) bool {
|
|
|
|
var b []byte
|
|
|
|
if !br.readBytes(&b, n) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = 0
|
|
|
|
for _, v := range b {
|
|
|
|
*out <<= 8
|
|
|
|
*out |= uint64(v)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU8(out *uint8) bool {
|
|
|
|
var b []byte
|
|
|
|
if !br.readBytes(&b, 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = b[0]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU16(out *uint16) bool {
|
|
|
|
var v uint64
|
|
|
|
if !br.readUint(&v, 2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = uint16(v)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU24(out *uint32) bool {
|
|
|
|
var v uint64
|
|
|
|
if !br.readUint(&v, 3) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = uint32(v)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU32(out *uint32) bool {
|
|
|
|
var v uint64
|
|
|
|
if !br.readUint(&v, 4) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = uint32(v)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU64(out *uint64) bool {
|
|
|
|
return br.readUint(out, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readLengthPrefixed(out *byteReader, n int) bool {
|
|
|
|
var length uint64
|
|
|
|
return br.readUint(&length, n) &&
|
|
|
|
uint64(len(*br)) >= length &&
|
|
|
|
br.readInternal(out, int(length))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readLengthPrefixedBytes(out *[]byte, n int) bool {
|
|
|
|
var length uint64
|
|
|
|
return br.readUint(&length, n) &&
|
|
|
|
uint64(len(*br)) >= length &&
|
|
|
|
br.readBytes(out, int(length))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU8LengthPrefixed(out *byteReader) bool {
|
|
|
|
return br.readLengthPrefixed(out, 1)
|
|
|
|
}
|
|
|
|
func (br *byteReader) readU8LengthPrefixedBytes(out *[]byte) bool {
|
|
|
|
return br.readLengthPrefixedBytes(out, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU16LengthPrefixed(out *byteReader) bool {
|
|
|
|
return br.readLengthPrefixed(out, 2)
|
|
|
|
}
|
|
|
|
func (br *byteReader) readU16LengthPrefixedBytes(out *[]byte) bool {
|
|
|
|
return br.readLengthPrefixedBytes(out, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU24LengthPrefixed(out *byteReader) bool {
|
|
|
|
return br.readLengthPrefixed(out, 3)
|
|
|
|
}
|
|
|
|
func (br *byteReader) readU24LengthPrefixedBytes(out *[]byte) bool {
|
|
|
|
return br.readLengthPrefixedBytes(out, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *byteReader) readU32LengthPrefixed(out *byteReader) bool {
|
|
|
|
return br.readLengthPrefixed(out, 4)
|
|
|
|
}
|
|
|
|
func (br *byteReader) readU32LengthPrefixedBytes(out *[]byte) bool {
|
|
|
|
return br.readLengthPrefixedBytes(out, 4)
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:59:01 +01:00
|
|
|
type keyShareEntry struct {
|
|
|
|
group CurveID
|
|
|
|
keyExchange []byte
|
|
|
|
}
|
|
|
|
|
2016-09-01 17:29:49 +01:00
|
|
|
type pskIdentity struct {
|
2016-11-01 17:39:36 +00:00
|
|
|
ticket []uint8
|
|
|
|
obfuscatedTicketAge uint32
|
2016-09-01 17:29:49 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
type clientHelloMsg struct {
|
2016-10-08 02:10:38 +01:00
|
|
|
raw []byte
|
|
|
|
isDTLS bool
|
|
|
|
vers uint16
|
|
|
|
random []byte
|
|
|
|
sessionId []byte
|
2014-11-16 00:06:08 +00:00
|
|
|
cookie []byte
|
|
|
|
cipherSuites []uint16
|
|
|
|
compressionMethods []uint8
|
|
|
|
nextProtoNeg bool
|
|
|
|
serverName string
|
|
|
|
ocspStapling bool
|
|
|
|
supportedCurves []CurveID
|
|
|
|
supportedPoints []uint8
|
2016-07-16 16:47:31 +01:00
|
|
|
hasKeyShares bool
|
2016-07-01 00:59:01 +01:00
|
|
|
keyShares []keyShareEntry
|
2018-11-19 21:49:56 +00:00
|
|
|
keySharesRaw []byte
|
2016-09-21 00:24:40 +01:00
|
|
|
trailingKeyShareData bool
|
2016-09-01 17:29:49 +01:00
|
|
|
pskIdentities []pskIdentity
|
2016-11-01 17:39:36 +00:00
|
|
|
pskKEModes []byte
|
|
|
|
pskBinders [][]uint8
|
2016-07-01 00:59:01 +01:00
|
|
|
hasEarlyData bool
|
2016-10-08 02:10:38 +01:00
|
|
|
tls13Cookie []byte
|
2014-11-16 00:06:08 +00:00
|
|
|
ticketSupported bool
|
|
|
|
sessionTicket []uint8
|
2016-06-21 23:19:24 +01:00
|
|
|
signatureAlgorithms []signatureAlgorithm
|
2018-04-08 18:59:25 +01:00
|
|
|
signatureAlgorithmsCert []signatureAlgorithm
|
2016-09-15 21:27:05 +01:00
|
|
|
supportedVersions []uint16
|
2014-11-16 00:06:08 +00:00
|
|
|
secureRenegotiation []byte
|
|
|
|
alpnProtocols []string
|
2017-12-22 23:50:43 +00:00
|
|
|
quicTransportParams []byte
|
2014-11-16 00:06:08 +00:00
|
|
|
duplicateExtension bool
|
|
|
|
channelIDSupported bool
|
2017-09-21 23:02:22 +01:00
|
|
|
tokenBindingParams []byte
|
|
|
|
tokenBindingVersion uint16
|
2016-11-01 17:39:36 +00:00
|
|
|
npnAfterAlpn bool
|
2014-11-16 00:06:08 +00:00
|
|
|
extendedMasterSecret bool
|
|
|
|
srtpProtectionProfiles []uint16
|
|
|
|
srtpMasterKeyIdentifier string
|
2014-11-25 06:55:35 +00:00
|
|
|
sctListSupported bool
|
2015-07-31 02:10:13 +01:00
|
|
|
customExtension string
|
2016-09-03 02:35:25 +01:00
|
|
|
hasGREASEExtension bool
|
2016-11-01 17:39:36 +00:00
|
|
|
pskBinderFirst bool
|
2017-07-14 23:40:34 +01:00
|
|
|
omitExtensions bool
|
|
|
|
emptyExtensions bool
|
2017-09-07 16:51:46 +01:00
|
|
|
pad int
|
2018-05-02 17:06:48 +01:00
|
|
|
compressedCertAlgs []uint16
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *clientHelloMsg) equal(i interface{}) bool {
|
|
|
|
m1, ok := i.(*clientHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.Equal(m.raw, m1.raw) &&
|
2014-08-04 06:23:53 +01:00
|
|
|
m.isDTLS == m1.isDTLS &&
|
2014-06-20 20:00:00 +01:00
|
|
|
m.vers == m1.vers &&
|
|
|
|
bytes.Equal(m.random, m1.random) &&
|
|
|
|
bytes.Equal(m.sessionId, m1.sessionId) &&
|
2014-08-04 06:23:53 +01:00
|
|
|
bytes.Equal(m.cookie, m1.cookie) &&
|
2014-06-20 20:00:00 +01:00
|
|
|
eqUint16s(m.cipherSuites, m1.cipherSuites) &&
|
|
|
|
bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
|
|
|
|
m.nextProtoNeg == m1.nextProtoNeg &&
|
|
|
|
m.serverName == m1.serverName &&
|
|
|
|
m.ocspStapling == m1.ocspStapling &&
|
|
|
|
eqCurveIDs(m.supportedCurves, m1.supportedCurves) &&
|
|
|
|
bytes.Equal(m.supportedPoints, m1.supportedPoints) &&
|
2016-07-16 16:47:31 +01:00
|
|
|
m.hasKeyShares == m1.hasKeyShares &&
|
2016-07-01 00:59:01 +01:00
|
|
|
eqKeyShareEntryLists(m.keyShares, m1.keyShares) &&
|
2016-09-21 00:24:40 +01:00
|
|
|
m.trailingKeyShareData == m1.trailingKeyShareData &&
|
2016-09-01 17:29:49 +01:00
|
|
|
eqPSKIdentityLists(m.pskIdentities, m1.pskIdentities) &&
|
2016-11-01 17:39:36 +00:00
|
|
|
bytes.Equal(m.pskKEModes, m1.pskKEModes) &&
|
|
|
|
eqByteSlices(m.pskBinders, m1.pskBinders) &&
|
2016-07-01 00:59:01 +01:00
|
|
|
m.hasEarlyData == m1.hasEarlyData &&
|
2016-10-08 02:10:38 +01:00
|
|
|
bytes.Equal(m.tls13Cookie, m1.tls13Cookie) &&
|
2014-06-20 20:00:00 +01:00
|
|
|
m.ticketSupported == m1.ticketSupported &&
|
|
|
|
bytes.Equal(m.sessionTicket, m1.sessionTicket) &&
|
2016-06-21 23:19:24 +01:00
|
|
|
eqSignatureAlgorithms(m.signatureAlgorithms, m1.signatureAlgorithms) &&
|
2018-04-08 18:59:25 +01:00
|
|
|
eqSignatureAlgorithms(m.signatureAlgorithmsCert, m1.signatureAlgorithmsCert) &&
|
2016-09-15 21:27:05 +01:00
|
|
|
eqUint16s(m.supportedVersions, m1.supportedVersions) &&
|
2014-10-29 00:29:33 +00:00
|
|
|
bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
|
|
|
|
(m.secureRenegotiation == nil) == (m1.secureRenegotiation == nil) &&
|
2014-09-15 21:51:51 +01:00
|
|
|
eqStrings(m.alpnProtocols, m1.alpnProtocols) &&
|
2017-12-22 23:50:43 +00:00
|
|
|
bytes.Equal(m.quicTransportParams, m1.quicTransportParams) &&
|
2014-08-24 06:44:23 +01:00
|
|
|
m.duplicateExtension == m1.duplicateExtension &&
|
2014-09-06 18:21:53 +01:00
|
|
|
m.channelIDSupported == m1.channelIDSupported &&
|
2017-09-21 23:02:22 +01:00
|
|
|
bytes.Equal(m.tokenBindingParams, m1.tokenBindingParams) &&
|
|
|
|
m.tokenBindingVersion == m1.tokenBindingVersion &&
|
2016-11-01 17:39:36 +00:00
|
|
|
m.npnAfterAlpn == m1.npnAfterAlpn &&
|
2014-11-16 00:06:08 +00:00
|
|
|
m.extendedMasterSecret == m1.extendedMasterSecret &&
|
|
|
|
eqUint16s(m.srtpProtectionProfiles, m1.srtpProtectionProfiles) &&
|
2014-11-25 06:55:35 +00:00
|
|
|
m.srtpMasterKeyIdentifier == m1.srtpMasterKeyIdentifier &&
|
2015-07-31 02:10:13 +01:00
|
|
|
m.sctListSupported == m1.sctListSupported &&
|
2016-09-03 02:35:25 +01:00
|
|
|
m.customExtension == m1.customExtension &&
|
2016-11-01 17:39:36 +00:00
|
|
|
m.hasGREASEExtension == m1.hasGREASEExtension &&
|
2017-07-14 23:40:34 +01:00
|
|
|
m.pskBinderFirst == m1.pskBinderFirst &&
|
|
|
|
m.omitExtensions == m1.omitExtensions &&
|
2017-09-07 16:51:46 +01:00
|
|
|
m.emptyExtensions == m1.emptyExtensions &&
|
2017-12-30 16:04:39 +00:00
|
|
|
m.pad == m1.pad &&
|
2018-05-02 17:06:48 +01:00
|
|
|
eqUint16s(m.compressedCertAlgs, m1.compressedCertAlgs)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2018-11-19 21:49:56 +00:00
|
|
|
func (m *clientHelloMsg) marshalKeyShares(bb *byteBuilder) {
|
|
|
|
keyShares := bb.addU16LengthPrefixed()
|
|
|
|
for _, keyShare := range m.keyShares {
|
|
|
|
keyShares.addU16(uint16(keyShare.group))
|
|
|
|
keyExchange := keyShares.addU16LengthPrefixed()
|
|
|
|
keyExchange.addBytes(keyShare.keyExchange)
|
|
|
|
}
|
|
|
|
if m.trailingKeyShareData {
|
|
|
|
keyShares.addU8(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
func (m *clientHelloMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
handshakeMsg := newByteBuilder()
|
|
|
|
handshakeMsg.addU8(typeClientHello)
|
|
|
|
hello := handshakeMsg.addU24LengthPrefixed()
|
2016-09-26 23:30:05 +01:00
|
|
|
hello.addU16(m.vers)
|
2016-06-30 23:51:11 +01:00
|
|
|
hello.addBytes(m.random)
|
|
|
|
sessionId := hello.addU8LengthPrefixed()
|
|
|
|
sessionId.addBytes(m.sessionId)
|
2014-08-04 06:23:53 +01:00
|
|
|
if m.isDTLS {
|
2016-06-30 23:51:11 +01:00
|
|
|
cookie := hello.addU8LengthPrefixed()
|
|
|
|
cookie.addBytes(m.cookie)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-06-30 23:51:11 +01:00
|
|
|
cipherSuites := hello.addU16LengthPrefixed()
|
|
|
|
for _, suite := range m.cipherSuites {
|
|
|
|
cipherSuites.addU16(suite)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-06-30 23:51:11 +01:00
|
|
|
compressionMethods := hello.addU8LengthPrefixed()
|
|
|
|
compressionMethods.addBytes(m.compressionMethods)
|
|
|
|
|
|
|
|
extensions := hello.addU16LengthPrefixed()
|
2016-11-01 17:39:36 +00:00
|
|
|
if len(m.pskIdentities) > 0 && m.pskBinderFirst {
|
|
|
|
extensions.addU16(extensionPreSharedKey)
|
|
|
|
pskExtension := extensions.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
pskIdentities := pskExtension.addU16LengthPrefixed()
|
|
|
|
for _, psk := range m.pskIdentities {
|
|
|
|
pskIdentities.addU16LengthPrefixed().addBytes(psk.ticket)
|
|
|
|
pskIdentities.addU32(psk.obfuscatedTicketAge)
|
|
|
|
}
|
|
|
|
pskBinders := pskExtension.addU16LengthPrefixed()
|
|
|
|
for _, binder := range m.pskBinders {
|
|
|
|
pskBinders.addU8LengthPrefixed().addBytes(binder)
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 05:23:20 +01:00
|
|
|
if m.duplicateExtension {
|
|
|
|
// Add a duplicate bogus extension at the beginning and end.
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(0xffff)
|
|
|
|
extensions.addU16(0) // 0-length for empty extension
|
2014-07-05 05:23:20 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if m.nextProtoNeg && !m.npnAfterAlpn {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionNextProtoNeg)
|
|
|
|
extensions.addU16(0) // The length is always 0
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if len(m.serverName) > 0 {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionServerName)
|
|
|
|
serverNameList := extensions.addU16LengthPrefixed()
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
// RFC 3546, section 3.1
|
|
|
|
//
|
|
|
|
// struct {
|
|
|
|
// NameType name_type;
|
|
|
|
// select (name_type) {
|
|
|
|
// case host_name: HostName;
|
|
|
|
// } name;
|
|
|
|
// } ServerName;
|
|
|
|
//
|
|
|
|
// enum {
|
|
|
|
// host_name(0), (255)
|
|
|
|
// } NameType;
|
|
|
|
//
|
|
|
|
// opaque HostName<1..2^16-1>;
|
|
|
|
//
|
|
|
|
// struct {
|
|
|
|
// ServerName server_name_list<1..2^16-1>
|
|
|
|
// } ServerNameList;
|
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
serverName := serverNameList.addU16LengthPrefixed()
|
|
|
|
serverName.addU8(0) // NameType host_name(0)
|
|
|
|
hostName := serverName.addU16LengthPrefixed()
|
|
|
|
hostName.addBytes([]byte(m.serverName))
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if m.ocspStapling {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionStatusRequest)
|
|
|
|
certificateStatusRequest := extensions.addU16LengthPrefixed()
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// RFC 4366, section 3.6
|
2016-06-30 23:51:11 +01:00
|
|
|
certificateStatusRequest.addU8(1) // OCSP type
|
2014-06-20 20:00:00 +01:00
|
|
|
// Two zero valued uint16s for the two lengths.
|
2016-06-30 23:51:11 +01:00
|
|
|
certificateStatusRequest.addU16(0) // ResponderID length
|
|
|
|
certificateStatusRequest.addU16(0) // Extensions length
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2017-09-26 22:46:58 +01:00
|
|
|
if len(m.supportedCurves) > 0 {
|
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.1.1
|
|
|
|
extensions.addU16(extensionSupportedCurves)
|
|
|
|
supportedCurvesList := extensions.addU16LengthPrefixed()
|
|
|
|
supportedCurves := supportedCurvesList.addU16LengthPrefixed()
|
|
|
|
for _, curve := range m.supportedCurves {
|
|
|
|
supportedCurves.addU16(uint16(curve))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m.supportedPoints) > 0 {
|
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.1.2
|
|
|
|
extensions.addU16(extensionSupportedPoints)
|
|
|
|
supportedPointsList := extensions.addU16LengthPrefixed()
|
|
|
|
supportedPoints := supportedPointsList.addU8LengthPrefixed()
|
|
|
|
supportedPoints.addBytes(m.supportedPoints)
|
|
|
|
}
|
2016-07-16 16:47:31 +01:00
|
|
|
if m.hasKeyShares {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionKeyShare)
|
2016-07-01 00:59:01 +01:00
|
|
|
keyShareList := extensions.addU16LengthPrefixed()
|
2018-11-19 21:49:56 +00:00
|
|
|
m.marshalKeyShares(keyShareList)
|
2016-07-01 00:59:01 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if len(m.pskKEModes) > 0 {
|
|
|
|
extensions.addU16(extensionPSKKeyExchangeModes)
|
|
|
|
pskModesExtension := extensions.addU16LengthPrefixed()
|
|
|
|
pskModesExtension.addU8LengthPrefixed().addBytes(m.pskKEModes)
|
2016-07-01 00:59:01 +01:00
|
|
|
}
|
|
|
|
if m.hasEarlyData {
|
|
|
|
extensions.addU16(extensionEarlyData)
|
2016-11-15 09:12:58 +00:00
|
|
|
extensions.addU16(0) // The length is zero.
|
2016-07-01 00:59:01 +01:00
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
if len(m.tls13Cookie) > 0 {
|
|
|
|
extensions.addU16(extensionCookie)
|
|
|
|
body := extensions.addU16LengthPrefixed()
|
|
|
|
body.addU16LengthPrefixed().addBytes(m.tls13Cookie)
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
if m.ticketSupported {
|
|
|
|
// http://tools.ietf.org/html/rfc5077#section-3.2
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionSessionTicket)
|
|
|
|
sessionTicketExtension := extensions.addU16LengthPrefixed()
|
|
|
|
sessionTicketExtension.addBytes(m.sessionTicket)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-06-21 23:19:24 +01:00
|
|
|
if len(m.signatureAlgorithms) > 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionSignatureAlgorithms)
|
|
|
|
signatureAlgorithmsExtension := extensions.addU16LengthPrefixed()
|
|
|
|
signatureAlgorithms := signatureAlgorithmsExtension.addU16LengthPrefixed()
|
2016-06-21 23:19:24 +01:00
|
|
|
for _, sigAlg := range m.signatureAlgorithms {
|
2016-06-30 23:51:11 +01:00
|
|
|
signatureAlgorithms.addU16(uint16(sigAlg))
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-08 18:59:25 +01:00
|
|
|
if len(m.signatureAlgorithmsCert) > 0 {
|
|
|
|
extensions.addU16(extensionSignatureAlgorithmsCert)
|
|
|
|
signatureAlgorithmsCertExtension := extensions.addU16LengthPrefixed()
|
|
|
|
signatureAlgorithmsCert := signatureAlgorithmsCertExtension.addU16LengthPrefixed()
|
|
|
|
for _, sigAlg := range m.signatureAlgorithmsCert {
|
|
|
|
signatureAlgorithmsCert.addU16(uint16(sigAlg))
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 21:27:05 +01:00
|
|
|
if len(m.supportedVersions) > 0 {
|
|
|
|
extensions.addU16(extensionSupportedVersions)
|
|
|
|
supportedVersionsExtension := extensions.addU16LengthPrefixed()
|
|
|
|
supportedVersions := supportedVersionsExtension.addU8LengthPrefixed()
|
|
|
|
for _, version := range m.supportedVersions {
|
|
|
|
supportedVersions.addU16(uint16(version))
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
if m.secureRenegotiation != nil {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionRenegotiationInfo)
|
|
|
|
secureRenegoExt := extensions.addU16LengthPrefixed()
|
|
|
|
secureRenego := secureRenegoExt.addU8LengthPrefixed()
|
|
|
|
secureRenego.addBytes(m.secureRenegotiation)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-09-15 21:51:51 +01:00
|
|
|
if len(m.alpnProtocols) > 0 {
|
2016-06-30 23:51:11 +01:00
|
|
|
// https://tools.ietf.org/html/rfc7301#section-3.1
|
|
|
|
extensions.addU16(extensionALPN)
|
|
|
|
alpnExtension := extensions.addU16LengthPrefixed()
|
2014-09-15 21:51:51 +01:00
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
protocolNameList := alpnExtension.addU16LengthPrefixed()
|
2014-09-15 21:51:51 +01:00
|
|
|
for _, s := range m.alpnProtocols {
|
2016-06-30 23:51:11 +01:00
|
|
|
protocolName := protocolNameList.addU8LengthPrefixed()
|
|
|
|
protocolName.addBytes([]byte(s))
|
2014-09-15 21:51:51 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-22 23:50:43 +00:00
|
|
|
if len(m.quicTransportParams) > 0 {
|
|
|
|
extensions.addU16(extensionQUICTransportParams)
|
|
|
|
params := extensions.addU16LengthPrefixed()
|
|
|
|
params.addBytes(m.quicTransportParams)
|
|
|
|
}
|
2014-08-24 06:44:23 +01:00
|
|
|
if m.channelIDSupported {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionChannelID)
|
|
|
|
extensions.addU16(0) // Length is always 0
|
2014-08-24 06:44:23 +01:00
|
|
|
}
|
2017-09-21 23:02:22 +01:00
|
|
|
if m.tokenBindingParams != nil {
|
|
|
|
extensions.addU16(extensionTokenBinding)
|
|
|
|
tokbindExtension := extensions.addU16LengthPrefixed()
|
|
|
|
tokbindExtension.addU16(m.tokenBindingVersion)
|
|
|
|
tokbindParams := tokbindExtension.addU8LengthPrefixed()
|
|
|
|
tokbindParams.addBytes(m.tokenBindingParams)
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if m.nextProtoNeg && m.npnAfterAlpn {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionNextProtoNeg)
|
|
|
|
extensions.addU16(0) // Length is always 0
|
2014-09-06 18:21:53 +01:00
|
|
|
}
|
2014-07-05 05:23:20 +01:00
|
|
|
if m.duplicateExtension {
|
|
|
|
// Add a duplicate bogus extension at the beginning and end.
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(0xffff)
|
|
|
|
extensions.addU16(0)
|
2014-07-05 05:23:20 +01:00
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
if m.extendedMasterSecret {
|
2016-02-01 13:42:19 +00:00
|
|
|
// https://tools.ietf.org/html/rfc7627
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionExtendedMasterSecret)
|
|
|
|
extensions.addU16(0) // Length is always 0
|
2014-10-11 00:23:43 +01:00
|
|
|
}
|
2014-11-16 00:06:08 +00:00
|
|
|
if len(m.srtpProtectionProfiles) > 0 {
|
2016-06-30 23:51:11 +01:00
|
|
|
// https://tools.ietf.org/html/rfc5764#section-4.1.1
|
|
|
|
extensions.addU16(extensionUseSRTP)
|
|
|
|
useSrtpExt := extensions.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
srtpProtectionProfiles := useSrtpExt.addU16LengthPrefixed()
|
2014-11-16 00:06:08 +00:00
|
|
|
for _, p := range m.srtpProtectionProfiles {
|
2017-11-24 15:12:15 +00:00
|
|
|
srtpProtectionProfiles.addU16(p)
|
2014-11-16 00:06:08 +00:00
|
|
|
}
|
2016-06-30 23:51:11 +01:00
|
|
|
srtpMki := useSrtpExt.addU8LengthPrefixed()
|
|
|
|
srtpMki.addBytes([]byte(m.srtpMasterKeyIdentifier))
|
2014-11-16 00:06:08 +00:00
|
|
|
}
|
2014-11-25 06:55:35 +00:00
|
|
|
if m.sctListSupported {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionSignedCertificateTimestamp)
|
|
|
|
extensions.addU16(0) // Length is always 0
|
2014-11-25 06:55:35 +00:00
|
|
|
}
|
2015-07-31 02:10:13 +01:00
|
|
|
if l := len(m.customExtension); l > 0 {
|
2016-06-30 23:51:11 +01:00
|
|
|
extensions.addU16(extensionCustom)
|
|
|
|
customExt := extensions.addU16LengthPrefixed()
|
|
|
|
customExt.addBytes([]byte(m.customExtension))
|
2015-07-31 02:10:13 +01:00
|
|
|
}
|
2018-05-02 17:06:48 +01:00
|
|
|
if len(m.compressedCertAlgs) > 0 {
|
|
|
|
extensions.addU16(extensionCompressedCertAlgs)
|
|
|
|
body := extensions.addU16LengthPrefixed()
|
|
|
|
algIDs := body.addU8LengthPrefixed()
|
|
|
|
for _, v := range m.compressedCertAlgs {
|
|
|
|
algIDs.addU16(v)
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 04:26:39 +01:00
|
|
|
// The PSK extension must be last. See https://tools.ietf.org/html/rfc8446#section-4.2.11
|
2016-11-01 17:39:36 +00:00
|
|
|
if len(m.pskIdentities) > 0 && !m.pskBinderFirst {
|
|
|
|
extensions.addU16(extensionPreSharedKey)
|
|
|
|
pskExtension := extensions.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
pskIdentities := pskExtension.addU16LengthPrefixed()
|
|
|
|
for _, psk := range m.pskIdentities {
|
|
|
|
pskIdentities.addU16LengthPrefixed().addBytes(psk.ticket)
|
|
|
|
pskIdentities.addU32(psk.obfuscatedTicketAge)
|
|
|
|
}
|
|
|
|
pskBinders := pskExtension.addU16LengthPrefixed()
|
|
|
|
for _, binder := range m.pskBinders {
|
|
|
|
pskBinders.addU8LengthPrefixed().addBytes(binder)
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-09-07 16:51:46 +01:00
|
|
|
if m.pad != 0 && hello.len()%m.pad != 0 {
|
|
|
|
extensions.addU16(extensionPadding)
|
|
|
|
padding := extensions.addU16LengthPrefixed()
|
|
|
|
// Note hello.len() has changed at this point from the length
|
|
|
|
// prefix.
|
|
|
|
if l := hello.len() % m.pad; l != 0 {
|
|
|
|
padding.addBytes(make([]byte, m.pad-l))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 23:40:34 +01:00
|
|
|
if m.omitExtensions || m.emptyExtensions {
|
|
|
|
// Silently erase any extensions which were sent.
|
2016-06-30 23:51:11 +01:00
|
|
|
hello.discardChild()
|
2017-07-14 23:40:34 +01:00
|
|
|
if m.emptyExtensions {
|
|
|
|
hello.addU16(0)
|
|
|
|
}
|
2016-06-30 23:51:11 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-06-30 23:51:11 +01:00
|
|
|
m.raw = handshakeMsg.finish()
|
2017-09-07 16:51:46 +01:00
|
|
|
// Sanity-check padding.
|
|
|
|
if m.pad != 0 && (len(m.raw)-4)%m.pad != 0 {
|
|
|
|
panic(fmt.Sprintf("%d is not a multiple of %d", len(m.raw)-4, m.pad))
|
|
|
|
}
|
2016-06-30 23:51:11 +01:00
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2018-04-08 18:59:25 +01:00
|
|
|
func parseSignatureAlgorithms(reader *byteReader, out *[]signatureAlgorithm, allowEmpty bool) bool {
|
2017-11-24 15:12:15 +00:00
|
|
|
var sigAlgs byteReader
|
|
|
|
if !reader.readU16LengthPrefixed(&sigAlgs) {
|
|
|
|
return false
|
|
|
|
}
|
2018-04-08 18:59:25 +01:00
|
|
|
if !allowEmpty && len(sigAlgs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
*out = make([]signatureAlgorithm, 0, len(sigAlgs)/2)
|
|
|
|
for len(sigAlgs) > 0 {
|
|
|
|
var v uint16
|
|
|
|
if !sigAlgs.readU16(&v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*out = append(*out, signatureAlgorithm(v))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-22 17:04:07 +00:00
|
|
|
func checkDuplicateExtensions(extensions byteReader) bool {
|
|
|
|
seen := make(map[uint16]struct{})
|
|
|
|
for len(extensions) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensions.readU16(&extension) ||
|
|
|
|
!extensions.readU16LengthPrefixed(&body) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := seen[extension]; ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
seen[extension] = struct{}{}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
func (m *clientHelloMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
2017-11-24 15:00:39 +00:00
|
|
|
reader := byteReader(data[4:])
|
|
|
|
if !reader.readU16(&m.vers) ||
|
|
|
|
!reader.readBytes(&m.random, 32) ||
|
|
|
|
!reader.readU8LengthPrefixedBytes(&m.sessionId) ||
|
|
|
|
len(m.sessionId) > 32 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2014-08-04 06:23:53 +01:00
|
|
|
if m.isDTLS {
|
2017-11-24 15:00:39 +00:00
|
|
|
if !reader.readU8LengthPrefixedBytes(&m.cookie) ||
|
|
|
|
len(m.cookie) > 32 {
|
2014-08-04 06:23:53 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
var cipherSuites byteReader
|
|
|
|
if !reader.readU16LengthPrefixed(&cipherSuites) ||
|
|
|
|
!reader.readU8LengthPrefixedBytes(&m.compressionMethods) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
|
|
|
|
m.cipherSuites = make([]uint16, 0, len(cipherSuites)/2)
|
|
|
|
for len(cipherSuites) > 0 {
|
|
|
|
var v uint16
|
|
|
|
if !cipherSuites.readU16(&v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.cipherSuites = append(m.cipherSuites, v)
|
|
|
|
if v == scsvRenegotiation {
|
2014-10-29 00:29:33 +00:00
|
|
|
m.secureRenegotiation = []byte{}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.nextProtoNeg = false
|
|
|
|
m.serverName = ""
|
|
|
|
m.ocspStapling = false
|
2016-07-01 00:59:01 +01:00
|
|
|
m.keyShares = nil
|
|
|
|
m.pskIdentities = nil
|
|
|
|
m.hasEarlyData = false
|
2014-06-20 20:00:00 +01:00
|
|
|
m.ticketSupported = false
|
|
|
|
m.sessionTicket = nil
|
2016-06-21 23:19:24 +01:00
|
|
|
m.signatureAlgorithms = nil
|
2018-04-08 18:59:25 +01:00
|
|
|
m.signatureAlgorithmsCert = nil
|
2016-09-15 21:27:05 +01:00
|
|
|
m.supportedVersions = nil
|
2014-09-15 21:51:51 +01:00
|
|
|
m.alpnProtocols = nil
|
2014-10-11 00:23:43 +01:00
|
|
|
m.extendedMasterSecret = false
|
2015-07-31 02:10:13 +01:00
|
|
|
m.customExtension = ""
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-11-24 15:00:39 +00:00
|
|
|
if len(reader) == 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
// ClientHello is optionally followed by extension data
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:00:39 +00:00
|
|
|
var extensions byteReader
|
2018-11-22 17:04:07 +00:00
|
|
|
if !reader.readU16LengthPrefixed(&extensions) || len(reader) != 0 || !checkDuplicateExtensions(extensions) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
for len(extensions) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensions.readU16(&extension) ||
|
|
|
|
!extensions.readU16LengthPrefixed(&body) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch extension {
|
|
|
|
case extensionServerName:
|
2017-11-24 15:00:39 +00:00
|
|
|
var names byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&names) || len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
for len(names) > 0 {
|
|
|
|
var nameType byte
|
|
|
|
var name []byte
|
|
|
|
if !names.readU8(&nameType) ||
|
|
|
|
!names.readU16LengthPrefixedBytes(&name) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if nameType == 0 {
|
2017-11-24 15:00:39 +00:00
|
|
|
m.serverName = string(name)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case extensionNextProtoNeg:
|
2017-11-24 15:00:39 +00:00
|
|
|
if len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.nextProtoNeg = true
|
|
|
|
case extensionStatusRequest:
|
2017-11-24 15:00:39 +00:00
|
|
|
m.ocspStapling = len(body) > 0 && body[0] == statusTypeOCSP
|
2014-06-20 20:00:00 +01:00
|
|
|
case extensionSupportedCurves:
|
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.5.1
|
2017-11-24 15:00:39 +00:00
|
|
|
var curves byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&curves) || len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
m.supportedCurves = make([]CurveID, 0, len(curves)/2)
|
|
|
|
for len(curves) > 0 {
|
|
|
|
var v uint16
|
|
|
|
if !curves.readU16(&v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.supportedCurves = append(m.supportedCurves, CurveID(v))
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
case extensionSupportedPoints:
|
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.5.2
|
2017-11-24 15:00:39 +00:00
|
|
|
if !body.readU8LengthPrefixedBytes(&m.supportedPoints) || len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case extensionSessionTicket:
|
|
|
|
// http://tools.ietf.org/html/rfc5077#section-3.2
|
|
|
|
m.ticketSupported = true
|
2017-11-24 15:00:39 +00:00
|
|
|
m.sessionTicket = []byte(body)
|
2018-01-22 20:20:31 +00:00
|
|
|
case extensionKeyShare:
|
2018-08-15 04:26:39 +01:00
|
|
|
// https://tools.ietf.org/html/rfc8446#section-4.2.8
|
2018-11-19 21:49:56 +00:00
|
|
|
m.hasKeyShares = true
|
|
|
|
m.keySharesRaw = body
|
2017-11-24 15:00:39 +00:00
|
|
|
var keyShares byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&keyShares) || len(body) != 0 {
|
2016-07-01 00:59:01 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
for len(keyShares) > 0 {
|
|
|
|
var entry keyShareEntry
|
|
|
|
var group uint16
|
|
|
|
if !keyShares.readU16(&group) ||
|
|
|
|
!keyShares.readU16LengthPrefixedBytes(&entry.keyExchange) {
|
2016-07-01 00:59:01 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
entry.group = CurveID(group)
|
2016-07-01 00:59:01 +01:00
|
|
|
m.keyShares = append(m.keyShares, entry)
|
|
|
|
}
|
|
|
|
case extensionPreSharedKey:
|
2018-08-15 04:26:39 +01:00
|
|
|
// https://tools.ietf.org/html/rfc8446#section-4.2.11
|
2017-11-24 15:00:39 +00:00
|
|
|
var psks, binders byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&psks) ||
|
|
|
|
!body.readU16LengthPrefixed(&binders) ||
|
|
|
|
len(body) != 0 {
|
2016-07-01 00:59:01 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
for len(psks) > 0 {
|
|
|
|
var psk pskIdentity
|
|
|
|
if !psks.readU16LengthPrefixedBytes(&psk.ticket) ||
|
|
|
|
!psks.readU32(&psk.obfuscatedTicketAge) {
|
2016-09-01 17:29:49 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
m.pskIdentities = append(m.pskIdentities, psk)
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
for len(binders) > 0 {
|
|
|
|
var binder []byte
|
|
|
|
if !binders.readU8LengthPrefixedBytes(&binder) {
|
2016-09-01 17:29:49 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
m.pskBinders = append(m.pskBinders, binder)
|
2016-11-01 17:39:36 +00:00
|
|
|
}
|
2016-09-01 17:29:49 +01:00
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
// There must be the same number of identities as binders.
|
|
|
|
if len(m.pskIdentities) != len(m.pskBinders) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case extensionPSKKeyExchangeModes:
|
2018-08-15 04:26:39 +01:00
|
|
|
// https://tools.ietf.org/html/rfc8446#section-4.2.9
|
2017-11-24 15:00:39 +00:00
|
|
|
if !body.readU8LengthPrefixedBytes(&m.pskKEModes) || len(body) != 0 {
|
2016-11-01 17:39:36 +00:00
|
|
|
return false
|
2016-07-01 00:59:01 +01:00
|
|
|
}
|
|
|
|
case extensionEarlyData:
|
2018-08-15 04:26:39 +01:00
|
|
|
// https://tools.ietf.org/html/rfc8446#section-4.2.10
|
2017-11-24 15:00:39 +00:00
|
|
|
if len(body) != 0 {
|
2016-07-01 00:59:01 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.hasEarlyData = true
|
2016-10-08 02:10:38 +01:00
|
|
|
case extensionCookie:
|
2017-11-24 15:00:39 +00:00
|
|
|
if !body.readU16LengthPrefixedBytes(&m.tls13Cookie) || len(body) != 0 {
|
2016-10-08 02:10:38 +01:00
|
|
|
return false
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
case extensionSignatureAlgorithms:
|
|
|
|
// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
|
2018-04-08 18:59:25 +01:00
|
|
|
if !parseSignatureAlgorithms(&body, &m.signatureAlgorithms, false) || len(body) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case extensionSignatureAlgorithmsCert:
|
|
|
|
if !parseSignatureAlgorithms(&body, &m.signatureAlgorithmsCert, false) || len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-09-15 21:27:05 +01:00
|
|
|
case extensionSupportedVersions:
|
2017-11-24 15:00:39 +00:00
|
|
|
var versions byteReader
|
|
|
|
if !body.readU8LengthPrefixed(&versions) || len(body) != 0 {
|
2016-09-15 21:27:05 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
m.supportedVersions = make([]uint16, 0, len(versions)/2)
|
|
|
|
for len(versions) > 0 {
|
|
|
|
var v uint16
|
|
|
|
if !versions.readU16(&v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.supportedVersions = append(m.supportedVersions, v)
|
2016-09-15 21:27:05 +01:00
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
case extensionRenegotiationInfo:
|
2017-11-24 15:00:39 +00:00
|
|
|
if !body.readU8LengthPrefixedBytes(&m.secureRenegotiation) || len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2014-09-15 21:51:51 +01:00
|
|
|
case extensionALPN:
|
2017-11-24 15:00:39 +00:00
|
|
|
var protocols byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&protocols) || len(body) != 0 {
|
2014-09-15 21:51:51 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
for len(protocols) > 0 {
|
|
|
|
var protocol []byte
|
|
|
|
if !protocols.readU8LengthPrefixedBytes(&protocol) {
|
2014-09-15 21:51:51 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
m.alpnProtocols = append(m.alpnProtocols, string(protocol))
|
2014-09-15 21:51:51 +01:00
|
|
|
}
|
2017-12-22 23:50:43 +00:00
|
|
|
case extensionQUICTransportParams:
|
|
|
|
m.quicTransportParams = body
|
2014-08-24 06:44:23 +01:00
|
|
|
case extensionChannelID:
|
2017-11-24 15:00:39 +00:00
|
|
|
if len(body) != 0 {
|
2014-08-24 06:44:23 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.channelIDSupported = true
|
2017-09-21 23:02:22 +01:00
|
|
|
case extensionTokenBinding:
|
|
|
|
if !body.readU16(&m.tokenBindingVersion) ||
|
|
|
|
!body.readU8LengthPrefixedBytes(&m.tokenBindingParams) ||
|
|
|
|
len(body) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
case extensionExtendedMasterSecret:
|
2017-11-24 15:00:39 +00:00
|
|
|
if len(body) != 0 {
|
2014-10-11 00:23:43 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.extendedMasterSecret = true
|
2014-11-16 00:06:08 +00:00
|
|
|
case extensionUseSRTP:
|
2017-11-24 15:00:39 +00:00
|
|
|
var profiles byteReader
|
|
|
|
var mki []byte
|
|
|
|
if !body.readU16LengthPrefixed(&profiles) ||
|
|
|
|
!body.readU8LengthPrefixedBytes(&mki) ||
|
|
|
|
len(body) != 0 {
|
2014-11-16 00:06:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
m.srtpProtectionProfiles = make([]uint16, 0, len(profiles)/2)
|
|
|
|
for len(profiles) > 0 {
|
|
|
|
var v uint16
|
|
|
|
if !profiles.readU16(&v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.srtpProtectionProfiles = append(m.srtpProtectionProfiles, v)
|
2014-11-16 00:06:08 +00:00
|
|
|
}
|
2017-11-24 15:00:39 +00:00
|
|
|
m.srtpMasterKeyIdentifier = string(mki)
|
2014-11-25 06:55:35 +00:00
|
|
|
case extensionSignedCertificateTimestamp:
|
2017-11-24 15:00:39 +00:00
|
|
|
if len(body) != 0 {
|
2014-11-25 06:55:35 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.sctListSupported = true
|
2015-07-31 02:10:13 +01:00
|
|
|
case extensionCustom:
|
2017-11-24 15:00:39 +00:00
|
|
|
m.customExtension = string(body)
|
2018-05-02 17:06:48 +01:00
|
|
|
case extensionCompressedCertAlgs:
|
|
|
|
var algIDs byteReader
|
|
|
|
if !body.readU8LengthPrefixed(&algIDs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
seen := make(map[uint16]struct{})
|
|
|
|
for len(algIDs) > 0 {
|
|
|
|
var algID uint16
|
|
|
|
if !algIDs.readU16(&algID) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := seen[algID]; ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
seen[algID] = struct{}{}
|
|
|
|
m.compressedCertAlgs = append(m.compressedCertAlgs, algID)
|
|
|
|
}
|
2018-11-22 17:04:07 +00:00
|
|
|
case extensionPadding:
|
|
|
|
// Padding bytes must be all zero.
|
|
|
|
for _, b := range body {
|
|
|
|
if b != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-09-03 02:35:25 +01:00
|
|
|
|
|
|
|
if isGREASEValue(extension) {
|
|
|
|
m.hasGREASEExtension = true
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverHelloMsg struct {
|
2017-07-10 17:57:25 +01:00
|
|
|
raw []byte
|
|
|
|
isDTLS bool
|
|
|
|
vers uint16
|
|
|
|
versOverride uint16
|
|
|
|
supportedVersOverride uint16
|
2018-05-05 03:00:33 +01:00
|
|
|
omitSupportedVers bool
|
2017-07-10 17:57:25 +01:00
|
|
|
random []byte
|
|
|
|
sessionId []byte
|
|
|
|
cipherSuite uint16
|
|
|
|
hasKeyShare bool
|
|
|
|
keyShare keyShareEntry
|
|
|
|
hasPSKIdentity bool
|
|
|
|
pskIdentity uint16
|
|
|
|
compressionMethod uint8
|
|
|
|
customExtension string
|
|
|
|
unencryptedALPN string
|
2017-07-14 23:40:34 +01:00
|
|
|
omitExtensions bool
|
|
|
|
emptyExtensions bool
|
2017-07-10 17:57:25 +01:00
|
|
|
extensions serverExtensions
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *serverHelloMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:26:07 +01:00
|
|
|
handshakeMsg := newByteBuilder()
|
|
|
|
handshakeMsg.addU8(typeServerHello)
|
|
|
|
hello := handshakeMsg.addU24LengthPrefixed()
|
2016-09-27 00:20:48 +01:00
|
|
|
|
|
|
|
// m.vers is used both to determine the format of the rest of the
|
|
|
|
// ServerHello and to override the value, so include a second version
|
|
|
|
// field.
|
|
|
|
vers, ok := wireToVersion(m.vers, m.isDTLS)
|
|
|
|
if !ok {
|
|
|
|
panic("unknown version")
|
|
|
|
}
|
|
|
|
if m.versOverride != 0 {
|
|
|
|
hello.addU16(m.versOverride)
|
2017-12-04 16:15:37 +00:00
|
|
|
} else if vers >= VersionTLS13 {
|
2017-07-10 17:57:25 +01:00
|
|
|
hello.addU16(VersionTLS12)
|
2016-09-27 00:20:48 +01:00
|
|
|
} else {
|
|
|
|
hello.addU16(m.vers)
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:26:07 +01:00
|
|
|
hello.addBytes(m.random)
|
2017-12-04 16:15:37 +00:00
|
|
|
sessionId := hello.addU8LengthPrefixed()
|
|
|
|
sessionId.addBytes(m.sessionId)
|
2016-07-01 00:26:07 +01:00
|
|
|
hello.addU16(m.cipherSuite)
|
2017-12-04 16:15:37 +00:00
|
|
|
hello.addU8(m.compressionMethod)
|
2016-07-01 00:26:07 +01:00
|
|
|
|
|
|
|
extensions := hello.addU16LengthPrefixed()
|
2016-07-01 16:43:18 +01:00
|
|
|
|
2016-09-26 23:30:05 +01:00
|
|
|
if vers >= VersionTLS13 {
|
2016-07-01 22:50:32 +01:00
|
|
|
if m.hasKeyShare {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionKeyShare)
|
2016-07-01 22:50:32 +01:00
|
|
|
keyShare := extensions.addU16LengthPrefixed()
|
|
|
|
keyShare.addU16(uint16(m.keyShare.group))
|
|
|
|
keyExchange := keyShare.addU16LengthPrefixed()
|
|
|
|
keyExchange.addBytes(m.keyShare.keyExchange)
|
|
|
|
}
|
|
|
|
if m.hasPSKIdentity {
|
|
|
|
extensions.addU16(extensionPreSharedKey)
|
|
|
|
extensions.addU16(2) // Length
|
|
|
|
extensions.addU16(m.pskIdentity)
|
|
|
|
}
|
2018-05-05 03:00:33 +01:00
|
|
|
if !m.omitSupportedVers {
|
|
|
|
extensions.addU16(extensionSupportedVersions)
|
|
|
|
extensions.addU16(2) // Length
|
|
|
|
if m.supportedVersOverride != 0 {
|
|
|
|
extensions.addU16(m.supportedVersOverride)
|
|
|
|
} else {
|
|
|
|
extensions.addU16(m.vers)
|
|
|
|
}
|
2017-07-10 17:57:25 +01:00
|
|
|
}
|
2016-10-06 03:44:38 +01:00
|
|
|
if len(m.customExtension) > 0 {
|
|
|
|
extensions.addU16(extensionCustom)
|
|
|
|
customExt := extensions.addU16LengthPrefixed()
|
|
|
|
customExt.addBytes([]byte(m.customExtension))
|
|
|
|
}
|
|
|
|
if len(m.unencryptedALPN) > 0 {
|
|
|
|
extensions.addU16(extensionALPN)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
protocolNameList := extension.addU16LengthPrefixed()
|
|
|
|
protocolName := protocolNameList.addU8LengthPrefixed()
|
|
|
|
protocolName.addBytes([]byte(m.unencryptedALPN))
|
|
|
|
}
|
2016-07-01 22:50:32 +01:00
|
|
|
} else {
|
2016-11-01 17:39:36 +00:00
|
|
|
m.extensions.marshal(extensions)
|
2017-07-14 23:40:34 +01:00
|
|
|
if m.omitExtensions || m.emptyExtensions {
|
|
|
|
// Silently erasing server extensions will break the handshake. Instead,
|
|
|
|
// assert that tests which use this field also disable all features which
|
|
|
|
// would write an extension.
|
|
|
|
if extensions.len() != 0 {
|
|
|
|
panic(fmt.Sprintf("ServerHello unexpectedly contained extensions: %x, %+v", extensions.data(), m))
|
|
|
|
}
|
2016-07-01 22:50:32 +01:00
|
|
|
hello.discardChild()
|
2017-07-14 23:40:34 +01:00
|
|
|
if m.emptyExtensions {
|
|
|
|
hello.addU16(0)
|
|
|
|
}
|
2016-07-01 22:50:32 +01:00
|
|
|
}
|
2016-07-01 16:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.raw = handshakeMsg.finish()
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *serverHelloMsg) unmarshal(data []byte) bool {
|
2017-11-24 15:12:15 +00:00
|
|
|
m.raw = data
|
|
|
|
reader := byteReader(data[4:])
|
|
|
|
if !reader.readU16(&m.vers) ||
|
|
|
|
!reader.readBytes(&m.random, 32) {
|
2016-07-01 16:43:18 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-09-27 00:20:48 +01:00
|
|
|
vers, ok := wireToVersion(m.vers, m.isDTLS)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2017-12-04 16:15:37 +00:00
|
|
|
if !reader.readU8LengthPrefixedBytes(&m.sessionId) ||
|
|
|
|
!reader.readU16(&m.cipherSuite) ||
|
|
|
|
!reader.readU8(&m.compressionMethod) {
|
2016-07-01 16:43:18 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:12:15 +00:00
|
|
|
if len(reader) == 0 && m.vers < VersionTLS13 {
|
2016-07-01 22:50:32 +01:00
|
|
|
// Extension data is optional before TLS 1.3.
|
2016-07-01 16:43:18 +01:00
|
|
|
m.extensions = serverExtensions{}
|
2017-08-31 05:19:57 +01:00
|
|
|
m.omitExtensions = true
|
2016-07-01 16:43:18 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:12:15 +00:00
|
|
|
var extensions byteReader
|
2018-11-22 17:04:07 +00:00
|
|
|
if !reader.readU16LengthPrefixed(&extensions) || len(reader) != 0 || !checkDuplicateExtensions(extensions) {
|
2016-07-01 16:43:18 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-10 17:57:25 +01:00
|
|
|
// Parse out the version from supported_versions if available.
|
|
|
|
if m.vers == VersionTLS12 {
|
2017-11-24 15:12:15 +00:00
|
|
|
extensionsCopy := extensions
|
|
|
|
for len(extensionsCopy) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensionsCopy.readU16(&extension) ||
|
|
|
|
!extensionsCopy.readU16LengthPrefixed(&body) {
|
2017-07-10 17:57:25 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if extension == extensionSupportedVersions {
|
2017-11-24 15:12:15 +00:00
|
|
|
if !body.readU16(&m.vers) || len(body) != 0 {
|
2017-07-10 17:57:25 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
vers, ok = wireToVersion(m.vers, m.isDTLS)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 23:30:05 +01:00
|
|
|
if vers >= VersionTLS13 {
|
2017-11-24 15:12:15 +00:00
|
|
|
for len(extensions) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensions.readU16(&extension) ||
|
|
|
|
!extensions.readU16LengthPrefixed(&body) {
|
2016-07-01 22:50:32 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch extension {
|
|
|
|
case extensionKeyShare:
|
|
|
|
m.hasKeyShare = true
|
2017-11-24 15:12:15 +00:00
|
|
|
var group uint16
|
|
|
|
if !body.readU16(&group) ||
|
|
|
|
!body.readU16LengthPrefixedBytes(&m.keyShare.keyExchange) ||
|
|
|
|
len(body) != 0 {
|
2016-07-01 22:50:32 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
m.keyShare.group = CurveID(group)
|
2016-07-01 22:50:32 +01:00
|
|
|
case extensionPreSharedKey:
|
2017-11-24 15:12:15 +00:00
|
|
|
if !body.readU16(&m.pskIdentity) || len(body) != 0 {
|
2016-07-01 22:50:32 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.hasPSKIdentity = true
|
2017-07-10 17:57:25 +01:00
|
|
|
case extensionSupportedVersions:
|
2017-12-04 16:15:37 +00:00
|
|
|
// Parsed above.
|
2016-07-01 22:50:32 +01:00
|
|
|
default:
|
|
|
|
// Only allow the 3 extensions that are sent in
|
|
|
|
// the clear in TLS 1.3.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
} else if !m.extensions.unmarshal(extensions, vers) {
|
2016-07-01 16:43:18 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:50:32 +01:00
|
|
|
type encryptedExtensionsMsg struct {
|
|
|
|
raw []byte
|
|
|
|
extensions serverExtensions
|
2016-07-11 18:19:03 +01:00
|
|
|
empty bool
|
2016-07-01 22:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *encryptedExtensionsMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedExtensionsMsg := newByteBuilder()
|
|
|
|
encryptedExtensionsMsg.addU8(typeEncryptedExtensions)
|
|
|
|
encryptedExtensions := encryptedExtensionsMsg.addU24LengthPrefixed()
|
2016-07-11 18:19:03 +01:00
|
|
|
if !m.empty {
|
|
|
|
extensions := encryptedExtensions.addU16LengthPrefixed()
|
2016-11-01 17:39:36 +00:00
|
|
|
m.extensions.marshal(extensions)
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
2016-07-01 22:50:32 +01:00
|
|
|
|
|
|
|
m.raw = encryptedExtensionsMsg.finish()
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *encryptedExtensionsMsg) unmarshal(data []byte) bool {
|
2016-07-13 21:42:36 +01:00
|
|
|
m.raw = data
|
2017-11-24 15:12:15 +00:00
|
|
|
reader := byteReader(data[4:])
|
|
|
|
var extensions byteReader
|
|
|
|
if !reader.readU16LengthPrefixed(&extensions) || len(reader) != 0 {
|
2016-07-01 22:50:32 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
return m.extensions.unmarshal(extensions, VersionTLS13)
|
2016-07-01 22:50:32 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 16:43:18 +01:00
|
|
|
type serverExtensions struct {
|
|
|
|
nextProtoNeg bool
|
|
|
|
nextProtos []string
|
|
|
|
ocspStapling bool
|
|
|
|
ticketSupported bool
|
|
|
|
secureRenegotiation []byte
|
|
|
|
alpnProtocol string
|
|
|
|
alpnProtocolEmpty bool
|
|
|
|
duplicateExtension bool
|
|
|
|
channelIDRequested bool
|
2017-09-21 23:02:22 +01:00
|
|
|
tokenBindingParams []byte
|
|
|
|
tokenBindingVersion uint16
|
2016-07-01 16:43:18 +01:00
|
|
|
extendedMasterSecret bool
|
|
|
|
srtpProtectionProfile uint16
|
|
|
|
srtpMasterKeyIdentifier string
|
|
|
|
sctList []byte
|
|
|
|
customExtension string
|
2016-11-01 17:39:36 +00:00
|
|
|
npnAfterAlpn bool
|
2016-07-11 18:19:03 +01:00
|
|
|
hasKeyShare bool
|
2016-12-07 00:02:31 +00:00
|
|
|
hasEarlyData bool
|
2016-07-11 18:19:03 +01:00
|
|
|
keyShare keyShareEntry
|
2017-07-10 17:57:25 +01:00
|
|
|
supportedVersion uint16
|
2016-12-22 14:16:57 +00:00
|
|
|
supportedPoints []uint8
|
2017-10-04 22:01:22 +01:00
|
|
|
supportedCurves []CurveID
|
2017-12-22 23:50:43 +00:00
|
|
|
quicTransportParams []byte
|
2017-02-06 18:49:07 +00:00
|
|
|
serverNameAck bool
|
2016-07-01 16:43:18 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
func (m *serverExtensions) marshal(extensions *byteBuilder) {
|
2014-07-05 05:23:20 +01:00
|
|
|
if m.duplicateExtension {
|
|
|
|
// Add a duplicate bogus extension at the beginning and end.
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(0xffff)
|
|
|
|
extensions.addU16(0) // length = 0 for empty extension
|
2014-07-05 05:23:20 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if m.nextProtoNeg && !m.npnAfterAlpn {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionNextProtoNeg)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
for _, v := range m.nextProtos {
|
2016-07-01 00:26:07 +01:00
|
|
|
if len(v) > 255 {
|
|
|
|
v = v[:255]
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-07-01 00:26:07 +01:00
|
|
|
npn := extension.addU8LengthPrefixed()
|
|
|
|
npn.addBytes([]byte(v))
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if m.ocspStapling {
|
|
|
|
extensions.addU16(extensionStatusRequest)
|
|
|
|
extensions.addU16(0)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if m.ticketSupported {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionSessionTicket)
|
|
|
|
extensions.addU16(0)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-10-29 00:29:33 +00:00
|
|
|
if m.secureRenegotiation != nil {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionRenegotiationInfo)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
|
|
|
secureRenego := extension.addU8LengthPrefixed()
|
|
|
|
secureRenego.addBytes(m.secureRenegotiation)
|
|
|
|
}
|
|
|
|
if len(m.alpnProtocol) > 0 || m.alpnProtocolEmpty {
|
|
|
|
extensions.addU16(extensionALPN)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
protocolNameList := extension.addU16LengthPrefixed()
|
|
|
|
protocolName := protocolNameList.addU8LengthPrefixed()
|
|
|
|
protocolName.addBytes([]byte(m.alpnProtocol))
|
2014-09-15 21:51:51 +01:00
|
|
|
}
|
2014-08-24 06:44:23 +01:00
|
|
|
if m.channelIDRequested {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionChannelID)
|
|
|
|
extensions.addU16(0)
|
2014-08-24 06:44:23 +01:00
|
|
|
}
|
2017-09-21 23:02:22 +01:00
|
|
|
if m.tokenBindingParams != nil {
|
|
|
|
extensions.addU16(extensionTokenBinding)
|
|
|
|
tokbindExtension := extensions.addU16LengthPrefixed()
|
|
|
|
tokbindExtension.addU16(m.tokenBindingVersion)
|
|
|
|
tokbindParams := tokbindExtension.addU8LengthPrefixed()
|
|
|
|
tokbindParams.addBytes(m.tokenBindingParams)
|
|
|
|
}
|
2014-07-05 05:23:20 +01:00
|
|
|
if m.duplicateExtension {
|
|
|
|
// Add a duplicate bogus extension at the beginning and end.
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(0xffff)
|
|
|
|
extensions.addU16(0)
|
2014-07-05 05:23:20 +01:00
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
if m.extendedMasterSecret {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionExtendedMasterSecret)
|
|
|
|
extensions.addU16(0)
|
2014-10-11 00:23:43 +01:00
|
|
|
}
|
2014-11-16 00:06:08 +00:00
|
|
|
if m.srtpProtectionProfile != 0 {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionUseSRTP)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
srtpProtectionProfiles := extension.addU16LengthPrefixed()
|
2017-11-24 15:12:15 +00:00
|
|
|
srtpProtectionProfiles.addU16(m.srtpProtectionProfile)
|
2016-07-01 00:26:07 +01:00
|
|
|
srtpMki := extension.addU8LengthPrefixed()
|
|
|
|
srtpMki.addBytes([]byte(m.srtpMasterKeyIdentifier))
|
2014-11-16 00:06:08 +00:00
|
|
|
}
|
2014-11-25 06:55:35 +00:00
|
|
|
if m.sctList != nil {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionSignedCertificateTimestamp)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
|
|
|
extension.addBytes(m.sctList)
|
2014-11-25 06:55:35 +00:00
|
|
|
}
|
2015-07-31 02:10:13 +01:00
|
|
|
if l := len(m.customExtension); l > 0 {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionCustom)
|
|
|
|
customExt := extensions.addU16LengthPrefixed()
|
|
|
|
customExt.addBytes([]byte(m.customExtension))
|
2015-07-31 02:10:13 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if m.nextProtoNeg && m.npnAfterAlpn {
|
2016-07-01 00:26:07 +01:00
|
|
|
extensions.addU16(extensionNextProtoNeg)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
2015-08-31 19:24:29 +01:00
|
|
|
|
|
|
|
for _, v := range m.nextProtos {
|
2016-07-01 00:26:07 +01:00
|
|
|
if len(v) > 255 {
|
|
|
|
v = v[0:255]
|
2015-08-31 19:24:29 +01:00
|
|
|
}
|
2016-07-01 00:26:07 +01:00
|
|
|
npn := extension.addU8LengthPrefixed()
|
|
|
|
npn.addBytes([]byte(v))
|
2015-08-31 19:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-11 18:19:03 +01:00
|
|
|
if m.hasKeyShare {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionKeyShare)
|
2016-07-11 18:19:03 +01:00
|
|
|
keyShare := extensions.addU16LengthPrefixed()
|
|
|
|
keyShare.addU16(uint16(m.keyShare.group))
|
|
|
|
keyExchange := keyShare.addU16LengthPrefixed()
|
|
|
|
keyExchange.addBytes(m.keyShare.keyExchange)
|
|
|
|
}
|
2017-07-10 17:57:25 +01:00
|
|
|
if m.supportedVersion != 0 {
|
|
|
|
extensions.addU16(extensionSupportedVersions)
|
|
|
|
extensions.addU16(2) // Length
|
|
|
|
extensions.addU16(m.supportedVersion)
|
|
|
|
}
|
2016-12-22 14:16:57 +00:00
|
|
|
if len(m.supportedPoints) > 0 {
|
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.1.2
|
|
|
|
extensions.addU16(extensionSupportedPoints)
|
|
|
|
supportedPointsList := extensions.addU16LengthPrefixed()
|
|
|
|
supportedPoints := supportedPointsList.addU8LengthPrefixed()
|
|
|
|
supportedPoints.addBytes(m.supportedPoints)
|
|
|
|
}
|
2017-10-04 22:01:22 +01:00
|
|
|
if len(m.supportedCurves) > 0 {
|
2018-08-15 04:26:39 +01:00
|
|
|
// https://tools.ietf.org/html/rfc8446#section-4.2.7
|
2017-10-04 22:01:22 +01:00
|
|
|
extensions.addU16(extensionSupportedCurves)
|
|
|
|
supportedCurvesList := extensions.addU16LengthPrefixed()
|
|
|
|
supportedCurves := supportedCurvesList.addU16LengthPrefixed()
|
|
|
|
for _, curve := range m.supportedCurves {
|
|
|
|
supportedCurves.addU16(uint16(curve))
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 23:50:43 +00:00
|
|
|
if len(m.quicTransportParams) > 0 {
|
|
|
|
extensions.addU16(extensionQUICTransportParams)
|
|
|
|
params := extensions.addU16LengthPrefixed()
|
|
|
|
params.addBytes(m.quicTransportParams)
|
|
|
|
}
|
2016-12-07 00:02:31 +00:00
|
|
|
if m.hasEarlyData {
|
|
|
|
extensions.addU16(extensionEarlyData)
|
|
|
|
extensions.addBytes([]byte{0, 0})
|
|
|
|
}
|
2017-02-06 18:49:07 +00:00
|
|
|
if m.serverNameAck {
|
|
|
|
extensions.addU16(extensionServerName)
|
|
|
|
extensions.addU16(0) // zero length
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 15:12:15 +00:00
|
|
|
func (m *serverExtensions) unmarshal(data byteReader, version uint16) bool {
|
2016-07-01 16:43:18 +01:00
|
|
|
// Reset all fields.
|
|
|
|
*m = serverExtensions{}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-11-22 17:04:07 +00:00
|
|
|
if !checkDuplicateExtensions(data) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:12:15 +00:00
|
|
|
for len(data) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !data.readU16(&extension) ||
|
|
|
|
!data.readU16LengthPrefixed(&body) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch extension {
|
|
|
|
case extensionNextProtoNeg:
|
|
|
|
m.nextProtoNeg = true
|
2017-11-24 15:12:15 +00:00
|
|
|
for len(body) > 0 {
|
|
|
|
var protocol []byte
|
|
|
|
if !body.readU8LengthPrefixedBytes(&protocol) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
m.nextProtos = append(m.nextProtos, string(protocol))
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
case extensionStatusRequest:
|
2017-11-24 15:12:15 +00:00
|
|
|
if len(body) != 0 {
|
2016-11-01 17:39:36 +00:00
|
|
|
return false
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
m.ocspStapling = true
|
2014-06-20 20:00:00 +01:00
|
|
|
case extensionSessionTicket:
|
2017-11-24 15:12:15 +00:00
|
|
|
if len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.ticketSupported = true
|
|
|
|
case extensionRenegotiationInfo:
|
2017-11-24 15:12:15 +00:00
|
|
|
if !body.readU8LengthPrefixedBytes(&m.secureRenegotiation) || len(body) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2014-09-15 21:51:51 +01:00
|
|
|
case extensionALPN:
|
2017-11-24 15:12:15 +00:00
|
|
|
var protocols, protocol byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&protocols) ||
|
|
|
|
len(body) != 0 ||
|
|
|
|
!protocols.readU8LengthPrefixed(&protocol) ||
|
|
|
|
len(protocols) != 0 {
|
2014-09-15 21:51:51 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
m.alpnProtocol = string(protocol)
|
|
|
|
m.alpnProtocolEmpty = len(protocol) == 0
|
2014-08-24 06:44:23 +01:00
|
|
|
case extensionChannelID:
|
2017-11-24 15:12:15 +00:00
|
|
|
if len(body) != 0 {
|
2014-08-24 06:44:23 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.channelIDRequested = true
|
2017-09-21 23:02:22 +01:00
|
|
|
case extensionTokenBinding:
|
|
|
|
if !body.readU16(&m.tokenBindingVersion) ||
|
|
|
|
!body.readU8LengthPrefixedBytes(&m.tokenBindingParams) ||
|
|
|
|
len(m.tokenBindingParams) != 1 ||
|
|
|
|
len(body) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
case extensionExtendedMasterSecret:
|
2017-11-24 15:12:15 +00:00
|
|
|
if len(body) != 0 {
|
2014-10-11 00:23:43 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.extendedMasterSecret = true
|
2014-11-16 00:06:08 +00:00
|
|
|
case extensionUseSRTP:
|
2017-11-24 15:12:15 +00:00
|
|
|
var profiles, mki byteReader
|
|
|
|
if !body.readU16LengthPrefixed(&profiles) ||
|
|
|
|
!profiles.readU16(&m.srtpProtectionProfile) ||
|
|
|
|
len(profiles) != 0 ||
|
|
|
|
!body.readU8LengthPrefixed(&mki) ||
|
|
|
|
len(body) != 0 {
|
2014-11-16 00:06:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
m.srtpMasterKeyIdentifier = string(mki)
|
2014-11-25 06:55:35 +00:00
|
|
|
case extensionSignedCertificateTimestamp:
|
2017-11-24 15:12:15 +00:00
|
|
|
m.sctList = []byte(body)
|
2015-07-31 02:10:13 +01:00
|
|
|
case extensionCustom:
|
2017-11-24 15:12:15 +00:00
|
|
|
m.customExtension = string(body)
|
2016-07-14 21:43:37 +01:00
|
|
|
case extensionServerName:
|
2017-11-24 15:12:15 +00:00
|
|
|
if len(body) != 0 {
|
2016-07-14 21:43:37 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-02-06 18:49:07 +00:00
|
|
|
m.serverNameAck = true
|
2016-07-14 21:43:37 +01:00
|
|
|
case extensionSupportedPoints:
|
|
|
|
// supported_points is illegal in TLS 1.3.
|
2016-07-18 00:03:18 +01:00
|
|
|
if version >= VersionTLS13 {
|
2016-07-14 21:43:37 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-12-22 14:16:57 +00:00
|
|
|
// http://tools.ietf.org/html/rfc4492#section-5.5.2
|
2017-11-24 15:12:15 +00:00
|
|
|
if !body.readU8LengthPrefixedBytes(&m.supportedPoints) || len(body) != 0 {
|
2016-12-22 14:16:57 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-07-17 11:34:41 +01:00
|
|
|
case extensionSupportedCurves:
|
|
|
|
// The server can only send supported_curves in TLS 1.3.
|
2016-07-18 00:03:18 +01:00
|
|
|
if version < VersionTLS13 {
|
2016-07-17 11:34:41 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-12-22 23:50:43 +00:00
|
|
|
case extensionQUICTransportParams:
|
|
|
|
m.quicTransportParams = body
|
2016-12-07 00:02:31 +00:00
|
|
|
case extensionEarlyData:
|
2017-11-24 15:12:15 +00:00
|
|
|
if version < VersionTLS13 || len(body) != 0 {
|
2016-12-07 00:02:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.hasEarlyData = true
|
2016-07-14 21:43:37 +01:00
|
|
|
default:
|
|
|
|
// Unknown extensions are illegal from the server.
|
|
|
|
return false
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-16 16:47:31 +01:00
|
|
|
type helloRetryRequestMsg struct {
|
2016-10-08 02:10:38 +01:00
|
|
|
raw []byte
|
|
|
|
vers uint16
|
2017-11-07 22:09:52 +00:00
|
|
|
isServerHello bool
|
|
|
|
sessionId []byte
|
2017-10-11 17:29:36 +01:00
|
|
|
cipherSuite uint16
|
2017-11-12 01:34:06 +00:00
|
|
|
compressionMethod uint8
|
2016-10-08 02:10:38 +01:00
|
|
|
hasSelectedGroup bool
|
|
|
|
selectedGroup CurveID
|
|
|
|
cookie []byte
|
|
|
|
customExtension string
|
|
|
|
duplicateExtensions bool
|
2016-07-16 16:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *helloRetryRequestMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
retryRequestMsg := newByteBuilder()
|
2018-01-22 20:20:31 +00:00
|
|
|
retryRequestMsg.addU8(typeServerHello)
|
2016-07-16 16:47:31 +01:00
|
|
|
retryRequest := retryRequestMsg.addU24LengthPrefixed()
|
2018-01-22 20:20:31 +00:00
|
|
|
retryRequest.addU16(VersionTLS12)
|
|
|
|
retryRequest.addBytes(tls13HelloRetryRequest)
|
|
|
|
sessionId := retryRequest.addU8LengthPrefixed()
|
|
|
|
sessionId.addBytes(m.sessionId)
|
|
|
|
retryRequest.addU16(m.cipherSuite)
|
|
|
|
retryRequest.addU8(m.compressionMethod)
|
2017-11-07 22:09:52 +00:00
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
extensions := retryRequest.addU16LengthPrefixed()
|
|
|
|
|
|
|
|
count := 1
|
|
|
|
if m.duplicateExtensions {
|
|
|
|
count = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionSupportedVersions)
|
|
|
|
extensions.addU16(2) // Length
|
|
|
|
extensions.addU16(m.vers)
|
2016-10-08 02:10:38 +01:00
|
|
|
if m.hasSelectedGroup {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionKeyShare)
|
2016-10-08 02:10:38 +01:00
|
|
|
extensions.addU16(2) // length
|
|
|
|
extensions.addU16(uint16(m.selectedGroup))
|
|
|
|
}
|
2017-11-16 23:36:38 +00:00
|
|
|
// m.cookie may be a non-nil empty slice for empty cookie tests.
|
|
|
|
if m.cookie != nil {
|
2016-10-08 02:10:38 +01:00
|
|
|
extensions.addU16(extensionCookie)
|
|
|
|
body := extensions.addU16LengthPrefixed()
|
|
|
|
body.addU16LengthPrefixed().addBytes(m.cookie)
|
|
|
|
}
|
|
|
|
if len(m.customExtension) > 0 {
|
|
|
|
extensions.addU16(extensionCustom)
|
|
|
|
extensions.addU16LengthPrefixed().addBytes([]byte(m.customExtension))
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 16:47:31 +01:00
|
|
|
|
|
|
|
m.raw = retryRequestMsg.finish()
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *helloRetryRequestMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
2017-11-24 15:12:15 +00:00
|
|
|
reader := byteReader(data[4:])
|
|
|
|
if !reader.readU16(&m.vers) {
|
2016-07-16 16:47:31 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-07 22:09:52 +00:00
|
|
|
if m.isServerHello {
|
2017-11-24 15:12:15 +00:00
|
|
|
var random []byte
|
|
|
|
var compressionMethod byte
|
|
|
|
if !reader.readBytes(&random, 32) ||
|
|
|
|
!reader.readU8LengthPrefixedBytes(&m.sessionId) ||
|
|
|
|
!reader.readU16(&m.cipherSuite) ||
|
|
|
|
!reader.readU8(&compressionMethod) ||
|
|
|
|
compressionMethod != 0 {
|
2017-11-07 22:09:52 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-01-22 20:20:31 +00:00
|
|
|
} else if !reader.readU16(&m.cipherSuite) {
|
2017-11-24 15:12:15 +00:00
|
|
|
return false
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
var extensions byteReader
|
|
|
|
if !reader.readU16LengthPrefixed(&extensions) || len(reader) != 0 {
|
2016-07-16 16:47:31 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-01-09 11:18:36 +00:00
|
|
|
extensionsCopy := extensions
|
|
|
|
for len(extensionsCopy) > 0 {
|
2017-11-24 15:12:15 +00:00
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
2018-01-09 11:18:36 +00:00
|
|
|
if !extensionsCopy.readU16(&extension) ||
|
|
|
|
!extensionsCopy.readU16LengthPrefixed(&body) {
|
2016-10-08 02:10:38 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch extension {
|
2017-11-07 22:09:52 +00:00
|
|
|
case extensionSupportedVersions:
|
2017-11-24 15:12:15 +00:00
|
|
|
if !m.isServerHello ||
|
|
|
|
!body.readU16(&m.vers) ||
|
|
|
|
len(body) != 0 {
|
2017-11-07 22:09:52 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-01-09 11:18:36 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for len(extensions) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensions.readU16(&extension) ||
|
|
|
|
!extensions.readU16LengthPrefixed(&body) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch extension {
|
|
|
|
case extensionSupportedVersions:
|
|
|
|
// Parsed above.
|
2016-10-08 02:10:38 +01:00
|
|
|
case extensionKeyShare:
|
2017-11-24 15:12:15 +00:00
|
|
|
var v uint16
|
|
|
|
if !body.readU16(&v) || len(body) != 0 {
|
2016-10-08 02:10:38 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.hasSelectedGroup = true
|
2017-11-24 15:12:15 +00:00
|
|
|
m.selectedGroup = CurveID(v)
|
2016-10-08 02:10:38 +01:00
|
|
|
case extensionCookie:
|
2017-11-24 15:12:15 +00:00
|
|
|
if !body.readU16LengthPrefixedBytes(&m.cookie) || len(body) != 0 {
|
2016-10-08 02:10:38 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Unknown extensions are illegal from the server.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 16:47:31 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
type certificateEntry struct {
|
|
|
|
data []byte
|
|
|
|
ocspResponse []byte
|
|
|
|
sctList []byte
|
|
|
|
duplicateExtensions bool
|
|
|
|
extraExtension []byte
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
type certificateMsg struct {
|
2016-07-01 22:50:32 +01:00
|
|
|
raw []byte
|
|
|
|
hasRequestContext bool
|
|
|
|
requestContext []byte
|
2016-11-01 17:39:36 +00:00
|
|
|
certificates []certificateEntry
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateMsg) marshal() (x []byte) {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:40:09 +01:00
|
|
|
certMsg := newByteBuilder()
|
|
|
|
certMsg.addU8(typeCertificate)
|
|
|
|
certificate := certMsg.addU24LengthPrefixed()
|
2016-07-01 22:50:32 +01:00
|
|
|
if m.hasRequestContext {
|
|
|
|
context := certificate.addU8LengthPrefixed()
|
|
|
|
context.addBytes(m.requestContext)
|
|
|
|
}
|
2016-07-01 22:40:09 +01:00
|
|
|
certificateList := certificate.addU24LengthPrefixed()
|
|
|
|
for _, cert := range m.certificates {
|
|
|
|
certEntry := certificateList.addU24LengthPrefixed()
|
2016-11-01 17:39:36 +00:00
|
|
|
certEntry.addBytes(cert.data)
|
|
|
|
if m.hasRequestContext {
|
|
|
|
extensions := certificateList.addU16LengthPrefixed()
|
|
|
|
count := 1
|
|
|
|
if cert.duplicateExtensions {
|
|
|
|
count = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
if cert.ocspResponse != nil {
|
|
|
|
extensions.addU16(extensionStatusRequest)
|
|
|
|
body := extensions.addU16LengthPrefixed()
|
|
|
|
body.addU8(statusTypeOCSP)
|
|
|
|
response := body.addU24LengthPrefixed()
|
|
|
|
response.addBytes(cert.ocspResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cert.sctList != nil {
|
|
|
|
extensions.addU16(extensionSignedCertificateTimestamp)
|
|
|
|
extension := extensions.addU16LengthPrefixed()
|
|
|
|
extension.addBytes(cert.sctList)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cert.extraExtension != nil {
|
|
|
|
extensions.addBytes(cert.extraExtension)
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 22:40:09 +01:00
|
|
|
m.raw = certMsg.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
2017-11-25 13:07:33 +00:00
|
|
|
reader := byteReader(data[4:])
|
2016-07-01 22:50:32 +01:00
|
|
|
|
2017-11-25 13:07:33 +00:00
|
|
|
if m.hasRequestContext && !reader.readU8LengthPrefixedBytes(&m.requestContext) {
|
2016-07-01 22:50:32 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-25 13:07:33 +00:00
|
|
|
|
|
|
|
var certs byteReader
|
|
|
|
if !reader.readU24LengthPrefixed(&certs) || len(reader) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
m.certificates = nil
|
2017-11-25 13:07:33 +00:00
|
|
|
for len(certs) > 0 {
|
|
|
|
var cert certificateEntry
|
|
|
|
if !certs.readU24LengthPrefixedBytes(&cert.data) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
if m.hasRequestContext {
|
2017-11-25 13:07:33 +00:00
|
|
|
var extensions byteReader
|
2018-11-22 17:04:07 +00:00
|
|
|
if !certs.readU16LengthPrefixed(&extensions) || !checkDuplicateExtensions(extensions) {
|
2016-11-01 17:39:36 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-11-25 13:07:33 +00:00
|
|
|
for len(extensions) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensions.readU16(&extension) ||
|
|
|
|
!extensions.readU16LengthPrefixed(&body) {
|
2016-11-01 17:39:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch extension {
|
|
|
|
case extensionStatusRequest:
|
2017-11-25 13:07:33 +00:00
|
|
|
var statusType byte
|
|
|
|
if !body.readU8(&statusType) ||
|
|
|
|
statusType != statusTypeOCSP ||
|
|
|
|
!body.readU24LengthPrefixedBytes(&cert.ocspResponse) ||
|
|
|
|
len(body) != 0 {
|
2016-11-01 17:39:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case extensionSignedCertificateTimestamp:
|
2017-11-25 13:07:33 +00:00
|
|
|
cert.sctList = []byte(body)
|
2016-11-01 17:39:36 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.certificates = append(m.certificates, cert)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-05-02 17:06:48 +01:00
|
|
|
type compressedCertificateMsg struct {
|
|
|
|
raw []byte
|
|
|
|
algID uint16
|
|
|
|
uncompressedLength uint32
|
|
|
|
compressed []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *compressedCertificateMsg) marshal() (x []byte) {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
certMsg := newByteBuilder()
|
2018-06-08 00:39:49 +01:00
|
|
|
certMsg.addU8(typeCompressedCertificate)
|
2018-05-02 17:06:48 +01:00
|
|
|
certificate := certMsg.addU24LengthPrefixed()
|
|
|
|
certificate.addU16(m.algID)
|
|
|
|
certificate.addU24(int(m.uncompressedLength))
|
|
|
|
compressed := certificate.addU24LengthPrefixed()
|
|
|
|
compressed.addBytes(m.compressed)
|
|
|
|
|
|
|
|
m.raw = certMsg.finish()
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *compressedCertificateMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
reader := byteReader(data[4:])
|
|
|
|
|
|
|
|
if !reader.readU16(&m.algID) ||
|
|
|
|
!reader.readU24(&m.uncompressedLength) ||
|
|
|
|
!reader.readU24LengthPrefixedBytes(&m.compressed) ||
|
|
|
|
len(reader) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.uncompressedLength >= 1<<17 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
type serverKeyExchangeMsg struct {
|
|
|
|
raw []byte
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *serverKeyExchangeMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
2017-11-30 00:11:36 +00:00
|
|
|
msg := newByteBuilder()
|
|
|
|
msg.addU8(typeServerKeyExchange)
|
|
|
|
msg.addU24LengthPrefixed().addBytes(m.key)
|
|
|
|
m.raw = msg.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
if len(data) < 4 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.key = data[4:]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type certificateStatusMsg struct {
|
|
|
|
raw []byte
|
|
|
|
statusType uint8
|
|
|
|
response []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateStatusMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
var x []byte
|
|
|
|
if m.statusType == statusTypeOCSP {
|
2017-11-30 00:11:36 +00:00
|
|
|
msg := newByteBuilder()
|
|
|
|
msg.addU8(typeCertificateStatus)
|
|
|
|
body := msg.addU24LengthPrefixed()
|
|
|
|
body.addU8(statusTypeOCSP)
|
|
|
|
body.addU24LengthPrefixed().addBytes(m.response)
|
|
|
|
x = msg.finish()
|
2014-06-20 20:00:00 +01:00
|
|
|
} else {
|
|
|
|
x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.raw = x
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateStatusMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
2017-11-30 00:11:36 +00:00
|
|
|
reader := byteReader(data[4:])
|
|
|
|
if !reader.readU8(&m.statusType) ||
|
|
|
|
m.statusType != statusTypeOCSP ||
|
|
|
|
!reader.readU24LengthPrefixedBytes(&m.response) ||
|
|
|
|
len(reader) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverHelloDoneMsg struct{}
|
|
|
|
|
|
|
|
func (m *serverHelloDoneMsg) marshal() []byte {
|
|
|
|
x := make([]byte, 4)
|
|
|
|
x[0] = typeServerHelloDone
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
|
|
|
|
return len(data) == 4
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientKeyExchangeMsg struct {
|
|
|
|
raw []byte
|
|
|
|
ciphertext []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *clientKeyExchangeMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
2017-11-30 00:11:36 +00:00
|
|
|
msg := newByteBuilder()
|
|
|
|
msg.addU8(typeClientKeyExchange)
|
|
|
|
msg.addU24LengthPrefixed().addBytes(m.ciphertext)
|
|
|
|
m.raw = msg.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
if len(data) < 4 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
|
|
|
|
if l != len(data)-4 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.ciphertext = data[4:]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type finishedMsg struct {
|
|
|
|
raw []byte
|
|
|
|
verifyData []byte
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:11:36 +00:00
|
|
|
func (m *finishedMsg) marshal() []byte {
|
2014-06-20 20:00:00 +01:00
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:11:36 +00:00
|
|
|
msg := newByteBuilder()
|
|
|
|
msg.addU8(typeFinished)
|
|
|
|
msg.addU24LengthPrefixed().addBytes(m.verifyData)
|
|
|
|
m.raw = msg.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *finishedMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
if len(data) < 4 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.verifyData = data[4:]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type nextProtoMsg struct {
|
|
|
|
raw []byte
|
|
|
|
proto string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nextProtoMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:11:36 +00:00
|
|
|
padding := 32 - (len(m.proto)+2)%32
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-11-30 00:11:36 +00:00
|
|
|
msg := newByteBuilder()
|
|
|
|
msg.addU8(typeNextProtocol)
|
|
|
|
body := msg.addU24LengthPrefixed()
|
|
|
|
body.addU8LengthPrefixed().addBytes([]byte(m.proto))
|
|
|
|
body.addU8LengthPrefixed().addBytes(make([]byte, padding))
|
|
|
|
m.raw = msg.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nextProtoMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
2017-11-30 00:11:36 +00:00
|
|
|
reader := byteReader(data[4:])
|
|
|
|
var proto, padding []byte
|
|
|
|
if !reader.readU8LengthPrefixedBytes(&proto) ||
|
|
|
|
!reader.readU8LengthPrefixedBytes(&padding) ||
|
|
|
|
len(reader) != 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-30 00:11:36 +00:00
|
|
|
m.proto = string(proto)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-11-30 00:11:36 +00:00
|
|
|
// Padding is not meant to be checked normally, but as this is a testing
|
|
|
|
// implementation, we check the padding is as expected.
|
|
|
|
if len(padding) != 32-(len(m.proto)+2)%32 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-30 00:11:36 +00:00
|
|
|
for _, v := range padding {
|
|
|
|
if v != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type certificateRequestMsg struct {
|
2017-10-11 17:29:36 +01:00
|
|
|
raw []byte
|
|
|
|
vers uint16
|
2016-06-21 23:19:24 +01:00
|
|
|
// hasSignatureAlgorithm indicates whether this message includes a list
|
2014-06-20 20:00:00 +01:00
|
|
|
// of signature and hash functions. This change was introduced with TLS
|
|
|
|
// 1.2.
|
2016-06-21 23:19:24 +01:00
|
|
|
hasSignatureAlgorithm bool
|
2016-07-01 22:50:32 +01:00
|
|
|
// hasRequestContext indicates whether this message includes a context
|
|
|
|
// field instead of certificateTypes. This change was introduced with
|
|
|
|
// TLS 1.3.
|
|
|
|
hasRequestContext bool
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-04-08 18:59:25 +01:00
|
|
|
certificateTypes []byte
|
|
|
|
requestContext []byte
|
|
|
|
signatureAlgorithms []signatureAlgorithm
|
|
|
|
signatureAlgorithmsCert []signatureAlgorithm
|
|
|
|
certificateAuthorities [][]byte
|
|
|
|
hasCAExtension bool
|
|
|
|
customExtension uint16
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 22:40:09 +01:00
|
|
|
func (m *certificateRequestMsg) marshal() []byte {
|
2014-06-20 20:00:00 +01:00
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
// See http://tools.ietf.org/html/rfc4346#section-7.4.4
|
2016-07-01 22:40:09 +01:00
|
|
|
builder := newByteBuilder()
|
|
|
|
builder.addU8(typeCertificateRequest)
|
|
|
|
body := builder.addU24LengthPrefixed()
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-07-01 22:50:32 +01:00
|
|
|
if m.hasRequestContext {
|
|
|
|
requestContext := body.addU8LengthPrefixed()
|
|
|
|
requestContext.addBytes(m.requestContext)
|
2017-10-11 17:29:36 +01:00
|
|
|
extensions := newByteBuilder()
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions = body.addU16LengthPrefixed()
|
|
|
|
if m.hasSignatureAlgorithm {
|
|
|
|
extensions.addU16(extensionSignatureAlgorithms)
|
|
|
|
signatureAlgorithms := extensions.addU16LengthPrefixed().addU16LengthPrefixed()
|
|
|
|
for _, sigAlg := range m.signatureAlgorithms {
|
|
|
|
signatureAlgorithms.addU16(uint16(sigAlg))
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
2018-01-22 20:20:31 +00:00
|
|
|
}
|
2018-04-08 18:59:25 +01:00
|
|
|
if len(m.signatureAlgorithmsCert) > 0 {
|
|
|
|
extensions.addU16(extensionSignatureAlgorithmsCert)
|
|
|
|
signatureAlgorithmsCert := extensions.addU16LengthPrefixed().addU16LengthPrefixed()
|
|
|
|
for _, sigAlg := range m.signatureAlgorithmsCert {
|
|
|
|
signatureAlgorithmsCert.addU16(uint16(sigAlg))
|
|
|
|
}
|
|
|
|
}
|
2018-01-22 20:20:31 +00:00
|
|
|
if len(m.certificateAuthorities) > 0 {
|
|
|
|
extensions.addU16(extensionCertificateAuthorities)
|
|
|
|
certificateAuthorities := extensions.addU16LengthPrefixed().addU16LengthPrefixed()
|
2017-10-11 17:29:36 +01:00
|
|
|
for _, ca := range m.certificateAuthorities {
|
|
|
|
caEntry := certificateAuthorities.addU16LengthPrefixed()
|
|
|
|
caEntry.addBytes(ca)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.customExtension > 0 {
|
|
|
|
extensions.addU16(m.customExtension)
|
|
|
|
extensions.addU16LengthPrefixed()
|
|
|
|
}
|
2016-07-01 22:50:32 +01:00
|
|
|
} else {
|
|
|
|
certificateTypes := body.addU8LengthPrefixed()
|
|
|
|
certificateTypes.addBytes(m.certificateTypes)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if m.hasSignatureAlgorithm {
|
|
|
|
signatureAlgorithms := body.addU16LengthPrefixed()
|
|
|
|
for _, sigAlg := range m.signatureAlgorithms {
|
|
|
|
signatureAlgorithms.addU16(uint16(sigAlg))
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
certificateAuthorities := body.addU16LengthPrefixed()
|
|
|
|
for _, ca := range m.certificateAuthorities {
|
|
|
|
caEntry := certificateAuthorities.addU16LengthPrefixed()
|
|
|
|
caEntry.addBytes(ca)
|
|
|
|
}
|
2016-07-09 22:26:01 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 22:40:09 +01:00
|
|
|
m.raw = builder.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 15:12:15 +00:00
|
|
|
func parseCAs(reader *byteReader, out *[][]byte) bool {
|
|
|
|
var cas byteReader
|
|
|
|
if !reader.readU16LengthPrefixed(&cas) {
|
|
|
|
return false
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
for len(cas) > 0 {
|
2017-11-24 15:12:15 +00:00
|
|
|
var ca []byte
|
|
|
|
if !cas.readU16LengthPrefixedBytes(&ca) {
|
|
|
|
return false
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
*out = append(*out, ca)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
return true
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateRequestMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
2017-11-24 15:12:15 +00:00
|
|
|
reader := byteReader(data[4:])
|
2017-10-11 17:29:36 +01:00
|
|
|
|
2018-01-22 20:20:31 +00:00
|
|
|
if m.hasRequestContext {
|
2017-11-24 15:12:15 +00:00
|
|
|
var extensions byteReader
|
|
|
|
if !reader.readU8LengthPrefixedBytes(&m.requestContext) ||
|
|
|
|
!reader.readU16LengthPrefixed(&extensions) ||
|
2018-11-22 17:04:07 +00:00
|
|
|
len(reader) != 0 ||
|
|
|
|
!checkDuplicateExtensions(extensions) {
|
2016-07-09 22:26:01 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
for len(extensions) > 0 {
|
|
|
|
var extension uint16
|
|
|
|
var body byteReader
|
|
|
|
if !extensions.readU16(&extension) ||
|
|
|
|
!extensions.readU16LengthPrefixed(&body) {
|
2017-10-11 17:29:36 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
switch extension {
|
|
|
|
case extensionSignatureAlgorithms:
|
2018-04-08 18:59:25 +01:00
|
|
|
if !parseSignatureAlgorithms(&body, &m.signatureAlgorithms, false) || len(body) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case extensionSignatureAlgorithmsCert:
|
|
|
|
if !parseSignatureAlgorithms(&body, &m.signatureAlgorithmsCert, false) || len(body) != 0 {
|
2017-10-11 17:29:36 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
case extensionCertificateAuthorities:
|
|
|
|
if !parseCAs(&body, &m.certificateAuthorities) || len(body) != 0 {
|
2017-10-11 17:29:36 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
m.hasCAExtension = true
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-24 15:12:15 +00:00
|
|
|
if !reader.readU8LengthPrefixedBytes(&m.certificateTypes) {
|
2017-10-11 17:29:36 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-04-08 18:59:25 +01:00
|
|
|
// In TLS 1.2, the supported_signature_algorithms field in
|
|
|
|
// CertificateRequest may be empty.
|
|
|
|
if m.hasSignatureAlgorithm && !parseSignatureAlgorithms(&reader, &m.signatureAlgorithms, true) {
|
2017-11-24 15:12:15 +00:00
|
|
|
return false
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
2017-11-24 15:12:15 +00:00
|
|
|
if !parseCAs(&reader, &m.certificateAuthorities) ||
|
|
|
|
len(reader) != 0 {
|
2016-07-09 22:26:01 +01:00
|
|
|
return false
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type certificateVerifyMsg struct {
|
2016-06-21 23:19:24 +01:00
|
|
|
raw []byte
|
|
|
|
hasSignatureAlgorithm bool
|
|
|
|
signatureAlgorithm signatureAlgorithm
|
|
|
|
signature []byte
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateVerifyMsg) marshal() (x []byte) {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
// See http://tools.ietf.org/html/rfc4346#section-7.4.8
|
|
|
|
siglength := len(m.signature)
|
|
|
|
length := 2 + siglength
|
2016-06-21 23:19:24 +01:00
|
|
|
if m.hasSignatureAlgorithm {
|
2014-06-20 20:00:00 +01:00
|
|
|
length += 2
|
|
|
|
}
|
|
|
|
x = make([]byte, 4+length)
|
|
|
|
x[0] = typeCertificateVerify
|
|
|
|
x[1] = uint8(length >> 16)
|
|
|
|
x[2] = uint8(length >> 8)
|
|
|
|
x[3] = uint8(length)
|
|
|
|
y := x[4:]
|
2016-06-21 23:19:24 +01:00
|
|
|
if m.hasSignatureAlgorithm {
|
|
|
|
y[0] = byte(m.signatureAlgorithm >> 8)
|
|
|
|
y[1] = byte(m.signatureAlgorithm)
|
2014-06-20 20:00:00 +01:00
|
|
|
y = y[2:]
|
|
|
|
}
|
|
|
|
y[0] = uint8(siglength >> 8)
|
|
|
|
y[1] = uint8(siglength)
|
|
|
|
copy(y[2:], m.signature)
|
|
|
|
|
|
|
|
m.raw = x
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *certificateVerifyMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
|
|
|
|
if len(data) < 6 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
|
|
|
|
if uint32(len(data))-4 != length {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
data = data[4:]
|
2016-06-21 23:19:24 +01:00
|
|
|
if m.hasSignatureAlgorithm {
|
|
|
|
m.signatureAlgorithm = signatureAlgorithm(data[0])<<8 | signatureAlgorithm(data[1])
|
2014-06-20 20:00:00 +01:00
|
|
|
data = data[2:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
siglength := int(data[0])<<8 + int(data[1])
|
|
|
|
data = data[2:]
|
|
|
|
if len(data) != siglength {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.signature = data
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type newSessionTicketMsg struct {
|
2017-10-11 17:29:36 +01:00
|
|
|
raw []byte
|
|
|
|
vers uint16
|
|
|
|
isDTLS bool
|
|
|
|
ticketLifetime uint32
|
|
|
|
ticketAgeAdd uint32
|
|
|
|
ticketNonce []byte
|
|
|
|
ticket []byte
|
|
|
|
maxEarlyDataSize uint32
|
|
|
|
customExtension string
|
|
|
|
duplicateEarlyDataExtension bool
|
|
|
|
hasGREASEExtension bool
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 00:25:41 +01:00
|
|
|
func (m *newSessionTicketMsg) marshal() []byte {
|
2014-06-20 20:00:00 +01:00
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
version, ok := wireToVersion(m.vers, m.isDTLS)
|
|
|
|
if !ok {
|
|
|
|
panic("unknown version")
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// See http://tools.ietf.org/html/rfc5077#section-3.3
|
2016-07-18 00:25:41 +01:00
|
|
|
ticketMsg := newByteBuilder()
|
|
|
|
ticketMsg.addU8(typeNewSessionTicket)
|
|
|
|
body := ticketMsg.addU24LengthPrefixed()
|
|
|
|
body.addU32(m.ticketLifetime)
|
2017-10-11 17:29:36 +01:00
|
|
|
if version >= VersionTLS13 {
|
2016-11-01 17:39:36 +00:00
|
|
|
body.addU32(m.ticketAgeAdd)
|
2018-01-22 20:20:31 +00:00
|
|
|
body.addU8LengthPrefixed().addBytes(m.ticketNonce)
|
2016-09-01 17:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ticket := body.addU16LengthPrefixed()
|
|
|
|
ticket.addBytes(m.ticket)
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if version >= VersionTLS13 {
|
2016-10-07 20:25:06 +01:00
|
|
|
extensions := body.addU16LengthPrefixed()
|
2016-12-07 00:02:31 +00:00
|
|
|
if m.maxEarlyDataSize > 0 {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionEarlyData)
|
2016-12-07 00:02:31 +00:00
|
|
|
extensions.addU16LengthPrefixed().addU32(m.maxEarlyDataSize)
|
2017-10-11 17:29:36 +01:00
|
|
|
if m.duplicateEarlyDataExtension {
|
2018-01-22 20:20:31 +00:00
|
|
|
extensions.addU16(extensionEarlyData)
|
2016-12-07 00:02:31 +00:00
|
|
|
extensions.addU16LengthPrefixed().addU32(m.maxEarlyDataSize)
|
2017-01-08 11:04:43 +00:00
|
|
|
}
|
2016-12-07 20:29:45 +00:00
|
|
|
}
|
2016-10-07 20:25:06 +01:00
|
|
|
if len(m.customExtension) > 0 {
|
2016-12-07 20:29:45 +00:00
|
|
|
extensions.addU16(extensionCustom)
|
2016-10-07 20:25:06 +01:00
|
|
|
extensions.addU16LengthPrefixed().addBytes([]byte(m.customExtension))
|
|
|
|
}
|
2016-07-18 00:25:41 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-07-18 00:25:41 +01:00
|
|
|
m.raw = ticketMsg.finish()
|
|
|
|
return m.raw
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *newSessionTicketMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
version, ok := wireToVersion(m.vers, m.isDTLS)
|
|
|
|
if !ok {
|
|
|
|
panic("unknown version")
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:25:41 +01:00
|
|
|
if len(data) < 8 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-07-18 00:25:41 +01:00
|
|
|
m.ticketLifetime = uint32(data[4])<<24 | uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
|
|
|
|
data = data[8:]
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if version >= VersionTLS13 {
|
2016-11-01 17:39:36 +00:00
|
|
|
if len(data) < 4 {
|
2016-09-01 17:29:49 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-11-01 17:39:36 +00:00
|
|
|
m.ticketAgeAdd = uint32(data[0])<<24 | uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
|
|
|
|
data = data[4:]
|
2018-01-22 20:20:31 +00:00
|
|
|
nonceLen := int(data[0])
|
|
|
|
data = data[1:]
|
|
|
|
if len(data) < nonceLen {
|
|
|
|
return false
|
2017-10-11 17:29:36 +01:00
|
|
|
}
|
2018-01-22 20:20:31 +00:00
|
|
|
m.ticketNonce = data[:nonceLen]
|
|
|
|
data = data[nonceLen:]
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 00:25:41 +01:00
|
|
|
if len(data) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ticketLen := int(data[0])<<8 + int(data[1])
|
2016-09-01 17:29:49 +01:00
|
|
|
data = data[2:]
|
|
|
|
if len(data) < ticketLen {
|
2016-07-18 00:25:41 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-09-01 17:29:49 +01:00
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if version >= VersionTLS13 && ticketLen == 0 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-01 17:29:49 +01:00
|
|
|
m.ticket = data[:ticketLen]
|
|
|
|
data = data[ticketLen:]
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if version >= VersionTLS13 {
|
2016-09-01 17:29:49 +01:00
|
|
|
if len(data) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-07 20:29:45 +00:00
|
|
|
|
|
|
|
extensionsLength := int(data[0])<<8 | int(data[1])
|
2016-09-01 17:29:49 +01:00
|
|
|
data = data[2:]
|
2016-12-07 20:29:45 +00:00
|
|
|
if extensionsLength != len(data) {
|
2016-09-01 17:29:49 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-10-07 20:19:18 +01:00
|
|
|
|
2016-12-07 20:29:45 +00:00
|
|
|
for len(data) != 0 {
|
|
|
|
if len(data) < 4 {
|
2016-10-07 20:19:18 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-12-07 20:29:45 +00:00
|
|
|
extension := uint16(data[0])<<8 | uint16(data[1])
|
|
|
|
length := int(data[2])<<8 | int(data[3])
|
|
|
|
data = data[4:]
|
|
|
|
if len(data) < length {
|
2016-10-07 20:19:18 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:29:45 +00:00
|
|
|
switch extension {
|
2018-01-22 20:20:31 +00:00
|
|
|
case extensionEarlyData:
|
2016-12-07 20:29:45 +00:00
|
|
|
if length != 4 {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-07 00:02:31 +00:00
|
|
|
m.maxEarlyDataSize = uint32(data[0])<<24 | uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
|
2016-12-07 20:29:45 +00:00
|
|
|
default:
|
|
|
|
if isGREASEValue(extension) {
|
|
|
|
m.hasGREASEExtension = true
|
|
|
|
}
|
2016-10-07 20:19:18 +01:00
|
|
|
}
|
2016-12-07 20:29:45 +00:00
|
|
|
|
|
|
|
data = data[length:]
|
2016-10-07 20:19:18 +01:00
|
|
|
}
|
2016-09-01 17:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-08-02 09:07:12 +01:00
|
|
|
type v2ClientHelloMsg struct {
|
|
|
|
raw []byte
|
|
|
|
vers uint16
|
|
|
|
cipherSuites []uint16
|
|
|
|
sessionId []byte
|
|
|
|
challenge []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *v2ClientHelloMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
length := 1 + 2 + 2 + 2 + 2 + len(m.cipherSuites)*3 + len(m.sessionId) + len(m.challenge)
|
|
|
|
|
|
|
|
x := make([]byte, length)
|
|
|
|
x[0] = 1
|
|
|
|
x[1] = uint8(m.vers >> 8)
|
|
|
|
x[2] = uint8(m.vers)
|
|
|
|
x[3] = uint8((len(m.cipherSuites) * 3) >> 8)
|
|
|
|
x[4] = uint8(len(m.cipherSuites) * 3)
|
|
|
|
x[5] = uint8(len(m.sessionId) >> 8)
|
|
|
|
x[6] = uint8(len(m.sessionId))
|
|
|
|
x[7] = uint8(len(m.challenge) >> 8)
|
|
|
|
x[8] = uint8(len(m.challenge))
|
|
|
|
y := x[9:]
|
|
|
|
for i, spec := range m.cipherSuites {
|
|
|
|
y[i*3] = 0
|
|
|
|
y[i*3+1] = uint8(spec >> 8)
|
|
|
|
y[i*3+2] = uint8(spec)
|
|
|
|
}
|
|
|
|
y = y[len(m.cipherSuites)*3:]
|
|
|
|
copy(y, m.sessionId)
|
|
|
|
y = y[len(m.sessionId):]
|
|
|
|
copy(y, m.challenge)
|
|
|
|
|
|
|
|
m.raw = x
|
|
|
|
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2014-08-04 06:23:53 +01:00
|
|
|
type helloVerifyRequestMsg struct {
|
|
|
|
raw []byte
|
|
|
|
vers uint16
|
|
|
|
cookie []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *helloVerifyRequestMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
length := 2 + 1 + len(m.cookie)
|
|
|
|
|
|
|
|
x := make([]byte, 4+length)
|
|
|
|
x[0] = typeHelloVerifyRequest
|
|
|
|
x[1] = uint8(length >> 16)
|
|
|
|
x[2] = uint8(length >> 8)
|
|
|
|
x[3] = uint8(length)
|
2016-09-26 23:30:05 +01:00
|
|
|
vers := m.vers
|
2014-08-04 06:23:53 +01:00
|
|
|
x[4] = uint8(vers >> 8)
|
|
|
|
x[5] = uint8(vers)
|
|
|
|
x[6] = uint8(len(m.cookie))
|
|
|
|
copy(x[7:7+len(m.cookie)], m.cookie)
|
|
|
|
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *helloVerifyRequestMsg) unmarshal(data []byte) bool {
|
|
|
|
if len(data) < 4+2+1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.raw = data
|
2016-09-26 23:30:05 +01:00
|
|
|
m.vers = uint16(data[4])<<8 | uint16(data[5])
|
2014-08-04 06:23:53 +01:00
|
|
|
cookieLen := int(data[6])
|
|
|
|
if cookieLen > 32 || len(data) != 7+cookieLen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.cookie = data[7 : 7+cookieLen]
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:56:53 +01:00
|
|
|
type channelIDMsg struct {
|
2014-08-24 06:44:23 +01:00
|
|
|
raw []byte
|
|
|
|
channelID []byte
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:56:53 +01:00
|
|
|
func (m *channelIDMsg) marshal() []byte {
|
2014-08-24 06:44:23 +01:00
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
length := 2 + 2 + len(m.channelID)
|
|
|
|
|
|
|
|
x := make([]byte, 4+length)
|
2016-06-30 23:56:53 +01:00
|
|
|
x[0] = typeChannelID
|
2014-08-24 06:44:23 +01:00
|
|
|
x[1] = uint8(length >> 16)
|
|
|
|
x[2] = uint8(length >> 8)
|
|
|
|
x[3] = uint8(length)
|
|
|
|
x[4] = uint8(extensionChannelID >> 8)
|
|
|
|
x[5] = uint8(extensionChannelID & 0xff)
|
|
|
|
x[6] = uint8(len(m.channelID) >> 8)
|
|
|
|
x[7] = uint8(len(m.channelID) & 0xff)
|
|
|
|
copy(x[8:], m.channelID)
|
|
|
|
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:56:53 +01:00
|
|
|
func (m *channelIDMsg) unmarshal(data []byte) bool {
|
2014-08-24 06:44:23 +01:00
|
|
|
if len(data) != 4+2+2+128 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.raw = data
|
|
|
|
if (uint16(data[4])<<8)|uint16(data[5]) != extensionChannelID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if int(data[6])<<8|int(data[7]) != 128 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.channelID = data[4+2+2:]
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-10-29 00:29:33 +00:00
|
|
|
type helloRequestMsg struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*helloRequestMsg) marshal() []byte {
|
|
|
|
return []byte{typeHelloRequest, 0, 0, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*helloRequestMsg) unmarshal(data []byte) bool {
|
|
|
|
return len(data) == 4
|
|
|
|
}
|
|
|
|
|
2016-07-18 20:56:23 +01:00
|
|
|
type keyUpdateMsg struct {
|
2016-10-03 17:25:56 +01:00
|
|
|
raw []byte
|
|
|
|
keyUpdateRequest byte
|
2016-07-18 20:56:23 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:25:56 +01:00
|
|
|
func (m *keyUpdateMsg) marshal() []byte {
|
|
|
|
if m.raw != nil {
|
|
|
|
return m.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte{typeKeyUpdate, 0, 0, 1, m.keyUpdateRequest}
|
2016-07-18 20:56:23 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:25:56 +01:00
|
|
|
func (m *keyUpdateMsg) unmarshal(data []byte) bool {
|
|
|
|
m.raw = data
|
|
|
|
|
|
|
|
if len(data) != 5 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
length := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
|
|
|
|
if len(data)-4 != length {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.keyUpdateRequest = data[4]
|
|
|
|
return m.keyUpdateRequest == keyUpdateNotRequested || m.keyUpdateRequest == keyUpdateRequested
|
2016-07-18 20:56:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
type endOfEarlyDataMsg struct {
|
|
|
|
nonEmpty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *endOfEarlyDataMsg) marshal() []byte {
|
|
|
|
if m.nonEmpty {
|
|
|
|
return []byte{typeEndOfEarlyData, 0, 0, 1, 42}
|
|
|
|
}
|
|
|
|
return []byte{typeEndOfEarlyData, 0, 0, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*endOfEarlyDataMsg) unmarshal(data []byte) bool {
|
|
|
|
return len(data) == 4
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:30:36 +00:00
|
|
|
// ssl3NoCertificateMsg is a dummy message to handle SSL 3.0 using a warning
|
|
|
|
// alert in the handshake.
|
|
|
|
type ssl3NoCertificateMsg struct{}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
func eqUint16s(x, y []uint16) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
|
|
|
if y[i] != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func eqCurveIDs(x, y []CurveID) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
|
|
|
if y[i] != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func eqStrings(x, y []string) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
|
|
|
if y[i] != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func eqByteSlices(x, y [][]byte) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
|
|
|
if !bytes.Equal(v, y[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:19:24 +01:00
|
|
|
func eqSignatureAlgorithms(x, y []signatureAlgorithm) bool {
|
2014-06-20 20:00:00 +01:00
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
|
|
|
v2 := y[i]
|
2016-06-21 23:19:24 +01:00
|
|
|
if v != v2 {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2016-07-01 00:59:01 +01:00
|
|
|
|
|
|
|
func eqKeyShareEntryLists(x, y []keyShareEntry) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
|
|
|
if y[i].group != v.group || !bytes.Equal(y[i].keyExchange, v.keyExchange) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
2016-09-01 17:29:49 +01:00
|
|
|
|
|
|
|
func eqPSKIdentityLists(x, y []pskIdentity) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range x {
|
2016-11-01 17:39:36 +00:00
|
|
|
if !bytes.Equal(y[i].ticket, v.ticket) || y[i].obfuscatedTicketAge != v.obfuscatedTicketAge {
|
2016-09-01 17:29:49 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|