Repository: pion/dtls
Pull request #200 (2020-02-21 22:22:35)

Fix encodeCipherSuites ordering
Before encodeCipherSuites would improperly encode backwards, putting
our least preferred suite first. Fix this function so it properly encodes
and update TestHandshakeMessageClientHello so it has multiple CipherSuites
to make sure this doesn't regress.

Relates to #199



Patch:
diff --git a/cipher_suite.go b/cipher_suite.go
index bf31487..63ca1f5 100644
--- a/cipher_suite.go
+++ b/cipher_suite.go
@@ -115,10 +115,10 @@ func cipherSuiteForID(id CipherSuiteID) cipherSuite {
 // CipherSuites we support in order of preference
 func defaultCipherSuites() []cipherSuite {
 	return []cipherSuite{
-		&cipherSuiteTLSEcdheRsaWithAes256CbcSha{},
-		&cipherSuiteTLSEcdheEcdsaWithAes256CbcSha{},
-		&cipherSuiteTLSEcdheRsaWithAes128GcmSha256{},
 		&cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256{},
+		&cipherSuiteTLSEcdheRsaWithAes128GcmSha256{},
+		&cipherSuiteTLSEcdheEcdsaWithAes256CbcSha{},
+		&cipherSuiteTLSEcdheRsaWithAes256CbcSha{},
 		newCipherSuiteTLSEcdheEcdsaWithAes128Ccm(),
 		newCipherSuiteTLSEcdheEcdsaWithAes128Ccm8(),
 	}
@@ -156,14 +156,13 @@ func decodeCipherSuites(buf []byte) ([]cipherSuite, error) {
 	return rtrn, nil
 }
 
-func encodeCipherSuites(c []cipherSuite) []byte {
+func encodeCipherSuites(cipherSuites []cipherSuite) []byte {
 	out := []byte{0x00, 0x00}
-	binary.BigEndian.PutUint16(out[len(out)-2:], uint16(len(c)*2))
-	for i := len(c); i > 0; i-- {
+	binary.BigEndian.PutUint16(out[len(out)-2:], uint16(len(cipherSuites)*2))
+	for _, c := range cipherSuites {
 		out = append(out, []byte{0x00, 0x00}...)
-		binary.BigEndian.PutUint16(out[len(out)-2:], uint16(c[i-1].ID()))
+		binary.BigEndian.PutUint16(out[len(out)-2:], uint16(c.ID()))
 	}
-
 	return out
 }
 


Test patch:
diff --git a/handshake_message_client_hello_test.go b/handshake_message_client_hello_test.go
index db7aacd..3899d7f 100644
--- a/handshake_message_client_hello_test.go
+++ b/handshake_message_client_hello_test.go
@@ -11,8 +11,8 @@ func TestHandshakeMessageClientHello(t *testing.T) {
 		0xfe, 0xfd, 0xb6, 0x2f, 0xce, 0x5c, 0x42, 0x54, 0xff, 0x86, 0xe1, 0x24, 0x41, 0x91, 0x42,
 		0x62, 0x15, 0xad, 0x16, 0xc9, 0x15, 0x8d, 0x95, 0x71, 0x8a, 0xbb, 0x22, 0xd7, 0x47, 0xec,
 		0xd8, 0x3d, 0xdc, 0x4b, 0x00, 0x14, 0xe6, 0x14, 0x3a, 0x1b, 0x04, 0xea, 0x9e, 0x7a, 0x14,
-		0xd6, 0x6c, 0x57, 0xd0, 0x0e, 0x32, 0x85, 0x76, 0x18, 0xde, 0xd8, 0x00, 0x02, 0xc0, 0x2b,
-		0x01, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x04, 0x00, 0x02, 0x00, 0x1d,
+		0xd6, 0x6c, 0x57, 0xd0, 0x0e, 0x32, 0x85, 0x76, 0x18, 0xde, 0xd8, 0x00, 0x04, 0xc0, 0x2b,
+		0xc0, 0x0a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x04, 0x00, 0x02, 0x00, 0x1d,
 	}
 	parsedClientHello := &handshakeMessageClientHello{
 		version: protocolVersion{0xFE, 0xFD},
@@ -23,6 +23,7 @@ func TestHandshakeMessageClientHello(t *testing.T) {
 		cookie: []byte{0xe6, 0x14, 0x3a, 0x1b, 0x04, 0xea, 0x9e, 0x7a, 0x14, 0xd6, 0x6c, 0x57, 0xd0, 0x0e, 0x32, 0x85, 0x76, 0x18, 0xde, 0xd8},
 		cipherSuites: []cipherSuite{
 			&cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256{},
+			&cipherSuiteTLSEcdheEcdsaWithAes256CbcSha{},
 		},
 		compressionMethods: []*compressionMethod{
 			compressionMethods[compressionMethodNull],