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.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
|
2018-07-19 08:40:28 +01:00
|
|
|
"github.com/securego/gas"
|
2016-08-05 12:58:27 +01:00
|
|
|
)
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
type usesWeakCryptography struct {
|
2016-07-20 11:02:01 +01:00
|
|
|
gas.MetaData
|
2016-11-07 17:13:20 +00:00
|
|
|
blacklist map[string][]string
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
func (r *usesWeakCryptography) ID() string {
|
|
|
|
return r.MetaData.ID
|
|
|
|
}
|
2016-11-07 17:13:20 +00:00
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
func (r *usesWeakCryptography) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
|
2016-11-07 17:13:20 +00:00
|
|
|
for pkg, funcs := range r.blacklist {
|
|
|
|
if _, matched := gas.MatchCallByPackage(n, c, pkg, funcs...); matched {
|
2018-03-12 08:18:44 +00:00
|
|
|
return gas.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2016-11-07 17:13:20 +00:00
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewUsesWeakCryptography detects uses of des.* md5.* or rc4.*
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewUsesWeakCryptography(id string, conf gas.Config) (gas.Rule, []ast.Node) {
|
2016-11-07 17:13:20 +00:00
|
|
|
calls := make(map[string][]string)
|
|
|
|
calls["crypto/des"] = []string{"NewCipher", "NewTripleDESCipher"}
|
|
|
|
calls["crypto/md5"] = []string{"New", "Sum"}
|
|
|
|
calls["crypto/rc4"] = []string{"NewCipher"}
|
2017-12-13 12:35:47 +00:00
|
|
|
rule := &usesWeakCryptography{
|
2016-11-07 17:13:20 +00:00
|
|
|
blacklist: calls,
|
2016-07-20 11:02:01 +01:00
|
|
|
MetaData: gas.MetaData{
|
2017-10-05 22:32:03 +01:00
|
|
|
ID: id,
|
2016-07-20 11:02:01 +01:00
|
|
|
Severity: gas.Medium,
|
|
|
|
Confidence: gas.High,
|
|
|
|
What: "Use of weak cryptographic primitive",
|
|
|
|
},
|
|
|
|
}
|
2016-11-13 20:55:31 +00:00
|
|
|
return rule, []ast.Node{(*ast.CallExpr)(nil)}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|