2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-02-21 05:59:18 +00:00
|
|
|
//go:generate tlsconfig
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
2020-06-23 10:34:08 +01:00
|
|
|
"crypto/tls"
|
2016-07-20 11:02:01 +01:00
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2021-07-27 21:03:59 +01:00
|
|
|
"go/types"
|
|
|
|
"strconv"
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
type insecureConfigTLS struct {
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.MetaData
|
2021-03-03 09:05:33 +00:00
|
|
|
MinVersion int64
|
|
|
|
MaxVersion int64
|
2020-06-23 10:34:08 +01:00
|
|
|
requiredType string
|
|
|
|
goodCiphers []string
|
2021-03-03 09:05:33 +00:00
|
|
|
actualMinVersion int64
|
|
|
|
actualMaxVersion int64
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
func (t *insecureConfigTLS) ID() string {
|
|
|
|
return t.MetaData.ID
|
|
|
|
}
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
func stringInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
func (t *insecureConfigTLS) processTLSCipherSuites(n ast.Node, c *gosec.Context) *gosec.Issue {
|
2017-12-28 06:54:10 +00:00
|
|
|
if ciphers, ok := n.(*ast.CompositeLit); ok {
|
|
|
|
for _, cipher := range ciphers.Elts {
|
|
|
|
if ident, ok := cipher.(*ast.SelectorExpr); ok {
|
2016-07-20 11:02:01 +01:00
|
|
|
if !stringInSlice(ident.Sel.Name, t.goodCiphers) {
|
2017-12-28 06:54:10 +00:00
|
|
|
err := fmt.Sprintf("TLS Bad Cipher Suite: %s", ident.Sel.Name)
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec.NewIssue(c, ident, t.ID(), err, gosec.High, gosec.High)
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Context) *gosec.Issue {
|
2016-07-20 11:02:01 +01:00
|
|
|
if ident, ok := n.Key.(*ast.Ident); ok {
|
|
|
|
switch ident.Name {
|
|
|
|
case "InsecureSkipVerify":
|
|
|
|
if node, ok := n.Value.(*ast.Ident); ok {
|
|
|
|
if node.Name != "false" {
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec.NewIssue(c, n, t.ID(), "TLS InsecureSkipVerify set true.", gosec.High, gosec.High)
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO(tk): symbol tab look up to get the actual value
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec.NewIssue(c, n, t.ID(), "TLS InsecureSkipVerify may be true.", gosec.High, gosec.Low)
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 14:05:44 +00:00
|
|
|
case "PreferServerCipherSuites":
|
|
|
|
if node, ok := n.Value.(*ast.Ident); ok {
|
|
|
|
if node.Name == "false" {
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec.NewIssue(c, n, t.ID(), "TLS PreferServerCipherSuites set false.", gosec.Medium, gosec.High)
|
2017-03-15 14:05:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO(tk): symbol tab look up to get the actual value
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec.NewIssue(c, n, t.ID(), "TLS PreferServerCipherSuites may be false.", gosec.Medium, gosec.Low)
|
2017-03-15 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
case "MinVersion":
|
2022-01-26 18:21:32 +00:00
|
|
|
if d, ok := n.Value.(*ast.Ident); ok {
|
|
|
|
obj := d.Obj
|
|
|
|
if obj == nil {
|
|
|
|
for _, f := range c.PkgFiles {
|
|
|
|
obj = f.Scope.Lookup(d.Name)
|
|
|
|
if obj != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vs, ok := obj.Decl.(*ast.ValueSpec); ok && len(vs.Values) > 0 {
|
2021-07-27 21:03:59 +01:00
|
|
|
if s, ok := vs.Values[0].(*ast.SelectorExpr); ok {
|
|
|
|
x := s.X.(*ast.Ident).Name
|
|
|
|
sel := s.Sel.Name
|
|
|
|
|
|
|
|
for _, imp := range c.Pkg.Imports() {
|
|
|
|
if imp.Name() == x {
|
|
|
|
tObj := imp.Scope().Lookup(sel)
|
|
|
|
if cst, ok := tObj.(*types.Const); ok {
|
|
|
|
// ..got the value check if this can be translated
|
2022-12-12 08:30:06 +00:00
|
|
|
if minVersion, err := strconv.ParseInt(cst.Val().String(), 0, 64); err == nil {
|
2021-07-27 21:03:59 +01:00
|
|
|
t.actualMinVersion = minVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ival, ierr := gosec.GetInt(vs.Values[0]); ierr == nil {
|
|
|
|
t.actualMinVersion = ival
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ival, ierr := gosec.GetInt(n.Value); ierr == nil {
|
2021-03-03 09:05:33 +00:00
|
|
|
t.actualMinVersion = ival
|
2020-06-23 10:34:08 +01:00
|
|
|
} else {
|
|
|
|
if se, ok := n.Value.(*ast.SelectorExpr); ok {
|
2022-07-28 07:51:30 +01:00
|
|
|
if pkg, ok := se.X.(*ast.Ident); ok {
|
|
|
|
if ip, ok := gosec.GetImportPath(pkg.Name, c); ok && ip == "crypto/tls" {
|
|
|
|
t.actualMinVersion = t.mapVersion(se.Sel.Name)
|
|
|
|
}
|
2020-06-23 10:34:08 +01:00
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "MaxVersion":
|
2018-07-19 17:42:25 +01:00
|
|
|
if ival, ierr := gosec.GetInt(n.Value); ierr == nil {
|
2021-03-03 09:05:33 +00:00
|
|
|
t.actualMaxVersion = ival
|
2020-06-23 10:34:08 +01:00
|
|
|
} else {
|
|
|
|
if se, ok := n.Value.(*ast.SelectorExpr); ok {
|
2022-07-28 07:51:30 +01:00
|
|
|
if pkg, ok := se.X.(*ast.Ident); ok {
|
|
|
|
if ip, ok := gosec.GetImportPath(pkg.Name, c); ok && ip == "crypto/tls" {
|
|
|
|
t.actualMaxVersion = t.mapVersion(se.Sel.Name)
|
|
|
|
}
|
2020-06-23 10:34:08 +01:00
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "CipherSuites":
|
2017-12-28 06:54:10 +00:00
|
|
|
if ret := t.processTLSCipherSuites(n.Value, c); ret != nil {
|
2016-07-20 11:02:01 +01:00
|
|
|
return ret
|
|
|
|
}
|
2017-03-15 14:05:44 +00:00
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:05:33 +00:00
|
|
|
func (t *insecureConfigTLS) mapVersion(version string) int64 {
|
|
|
|
var v int64
|
2020-06-23 10:34:08 +01:00
|
|
|
switch version {
|
|
|
|
case "VersionTLS13":
|
|
|
|
v = tls.VersionTLS13
|
|
|
|
case "VersionTLS12":
|
|
|
|
v = tls.VersionTLS12
|
|
|
|
case "VersionTLS11":
|
|
|
|
v = tls.VersionTLS11
|
|
|
|
case "VersionTLS10":
|
|
|
|
v = tls.VersionTLS10
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *insecureConfigTLS) checkVersion(n ast.Node, c *gosec.Context) *gosec.Issue {
|
|
|
|
if t.actualMaxVersion == 0 && t.actualMinVersion >= t.MinVersion {
|
2020-12-11 08:34:38 +00:00
|
|
|
// no warning is generated since the min version is greater than the secure min version
|
2020-06-23 10:34:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if t.actualMinVersion < t.MinVersion {
|
|
|
|
return gosec.NewIssue(c, n, t.ID(), "TLS MinVersion too low.", gosec.High, gosec.High)
|
|
|
|
}
|
|
|
|
if t.actualMaxVersion < t.MaxVersion {
|
|
|
|
return gosec.NewIssue(c, n, t.ID(), "TLS MaxVersion too low.", gosec.High, gosec.High)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 09:52:16 +00:00
|
|
|
func (t *insecureConfigTLS) resetVersion() {
|
|
|
|
t.actualMaxVersion = 0
|
|
|
|
t.actualMinVersion = 0
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
func (t *insecureConfigTLS) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
|
2018-02-27 18:29:25 +00:00
|
|
|
if complit, ok := n.(*ast.CompositeLit); ok && complit.Type != nil {
|
|
|
|
actualType := c.Info.TypeOf(complit.Type)
|
|
|
|
if actualType != nil && actualType.String() == t.requiredType {
|
|
|
|
for _, elt := range complit.Elts {
|
|
|
|
if kve, ok := elt.(*ast.KeyValueExpr); ok {
|
|
|
|
issue := t.processTLSConfVal(kve, c)
|
|
|
|
if issue != nil {
|
|
|
|
return issue, nil
|
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 09:52:16 +00:00
|
|
|
issue := t.checkVersion(complit, c)
|
|
|
|
t.resetVersion()
|
|
|
|
return issue, nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-28 06:54:10 +00:00
|
|
|
return nil, nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|