2017-09-23 00:56:58 -05:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-07-08 08:32:31 -05:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
2016-07-19 12:48:39 -05:00
|
|
|
"crypto/tls"
|
2016-07-08 08:32:31 -05:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-05-17 11:45:17 -05:00
|
|
|
|
2018-02-09 12:39:21 -05:00
|
|
|
"github.com/klauspost/cpuid"
|
2016-07-08 08:32:31 -05:00
|
|
|
)
|
|
|
|
|
2017-02-21 11:49:22 -05:00
|
|
|
func TestConvertTLSConfigProtocolVersions(t *testing.T) {
|
2016-07-19 12:48:39 -05:00
|
|
|
// same min and max protocol versions
|
2017-02-21 11:49:22 -05:00
|
|
|
config := &Config{
|
2017-02-18 17:26:23 -05:00
|
|
|
Enabled: true,
|
|
|
|
ProtocolMinVersion: tls.VersionTLS12,
|
|
|
|
ProtocolMaxVersion: tls.VersionTLS12,
|
2016-07-19 12:48:39 -05:00
|
|
|
}
|
2017-02-21 11:49:22 -05:00
|
|
|
err := config.buildStandardTLSConfig()
|
2016-07-19 12:48:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Did not expect an error, but got %v", err)
|
|
|
|
}
|
2017-02-21 11:49:22 -05:00
|
|
|
if got, want := config.tlsConfig.MinVersion, uint16(tls.VersionTLS12); got != want {
|
2016-07-19 12:48:39 -05:00
|
|
|
t.Errorf("Expected min version to be %x, got %x", want, got)
|
|
|
|
}
|
2017-02-21 11:49:22 -05:00
|
|
|
if got, want := config.tlsConfig.MaxVersion, uint16(tls.VersionTLS12); got != want {
|
2016-07-19 12:48:39 -05:00
|
|
|
t.Errorf("Expected max version to be %x, got %x", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 11:49:22 -05:00
|
|
|
func TestConvertTLSConfigPreferServerCipherSuites(t *testing.T) {
|
2016-08-25 18:13:27 -05:00
|
|
|
// prefer server cipher suites
|
2017-02-18 17:26:23 -05:00
|
|
|
config := Config{Enabled: true, PreferServerCipherSuites: true}
|
2017-02-21 11:49:22 -05:00
|
|
|
err := config.buildStandardTLSConfig()
|
2016-08-25 18:13:27 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Did not expect an error, but got %v", err)
|
|
|
|
}
|
2017-02-21 11:49:22 -05:00
|
|
|
if got, want := config.tlsConfig.PreferServerCipherSuites, true; got != want {
|
2016-08-25 18:13:27 -05:00
|
|
|
t.Errorf("Expected PreferServerCipherSuites==%v but got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 11:49:22 -05:00
|
|
|
func TestMakeTLSConfigTLSEnabledDisabledError(t *testing.T) {
|
2016-08-25 18:13:27 -05:00
|
|
|
// verify handling when Enabled is true and false
|
|
|
|
configs := []*Config{
|
|
|
|
{Enabled: true},
|
|
|
|
{Enabled: false},
|
|
|
|
}
|
2017-02-21 11:49:22 -05:00
|
|
|
_, err := MakeTLSConfig(configs)
|
2016-08-25 18:13:27 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected an error, but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 11:49:22 -05:00
|
|
|
func TestConvertTLSConfigCipherSuites(t *testing.T) {
|
2016-08-25 18:13:27 -05:00
|
|
|
// ensure cipher suites are unioned and
|
|
|
|
// that TLS_FALLBACK_SCSV is prepended
|
|
|
|
configs := []*Config{
|
|
|
|
{Enabled: true, Ciphers: []uint16{0xc02c, 0xc030}},
|
|
|
|
{Enabled: true, Ciphers: []uint16{0xc012, 0xc030, 0xc00a}},
|
2017-02-18 17:26:23 -05:00
|
|
|
{Enabled: true, Ciphers: nil},
|
2016-08-25 18:13:27 -05:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:45:17 -05:00
|
|
|
defaultCiphersExpected := getPreferredDefaultCiphers()
|
2017-02-18 17:26:23 -05:00
|
|
|
expectedCiphers := [][]uint16{
|
|
|
|
{tls.TLS_FALLBACK_SCSV, 0xc02c, 0xc030},
|
|
|
|
{tls.TLS_FALLBACK_SCSV, 0xc012, 0xc030, 0xc00a},
|
2017-05-17 11:45:17 -05:00
|
|
|
append([]uint16{tls.TLS_FALLBACK_SCSV}, defaultCiphersExpected...),
|
2016-08-25 18:13:27 -05:00
|
|
|
}
|
2017-02-18 17:26:23 -05:00
|
|
|
|
|
|
|
for i, config := range configs {
|
2017-02-21 11:49:22 -05:00
|
|
|
err := config.buildStandardTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: Expected no error, got: %v", i, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(config.tlsConfig.CipherSuites, expectedCiphers[i]) {
|
|
|
|
t.Errorf("Test %d: Expected ciphers %v but got %v",
|
|
|
|
i, expectedCiphers[i], config.tlsConfig.CipherSuites)
|
2017-02-18 17:26:23 -05:00
|
|
|
}
|
|
|
|
|
2016-08-25 18:13:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:45:17 -05:00
|
|
|
func TestGetPreferredDefaultCiphers(t *testing.T) {
|
|
|
|
expectedCiphers := defaultCiphers
|
2018-02-09 12:39:21 -05:00
|
|
|
if !cpuid.CPU.AesNi() {
|
2017-05-17 11:45:17 -05:00
|
|
|
expectedCiphers = defaultCiphersNonAESNI
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure ordering is correct and ciphers are what we expected.
|
|
|
|
result := getPreferredDefaultCiphers()
|
|
|
|
for i, actual := range result {
|
|
|
|
if actual != expectedCiphers[i] {
|
|
|
|
t.Errorf("Expected cipher in position %d to be %0x, got %0x", i, expectedCiphers[i], actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|