Skip to content

Commit

Permalink
Fix: refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adedayo committed Apr 19, 2020
1 parent 2552c61 commit f49cd00
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
5 changes: 3 additions & 2 deletions pkg/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,8 @@ type HumanScanResult struct {
SecureRenegotiationSupportedByProtocol map[string]bool
CipherSuiteByProtocol map[string][]string
// ServerHelloMessageByProtocolByCipher map[string]map[string]ServerHelloMessage
CertificatesPerProtocol map[string][]HumanCertificate
CertificatesPerProtocol map[string][]HumanCertificate
CertificatesWithChainIssue map[string]bool
// KeyExchangeByProtocolByCipher map[string]map[string]ServerKeyExchangeMsg
IsSTARTLS bool
IsSSH bool
Expand Down Expand Up @@ -1105,7 +1106,6 @@ func (s ScanResult) ToHumanScanResult() (out HumanScanResult) {
}
out.CipherSuiteByProtocol[tlsdefs.TLSVersionMap[k]] = ciphers
}

out.CertificatesPerProtocol = make(map[string][]HumanCertificate)
for p, c := range s.CertificatesPerProtocol {
certs, err := c.GetCertificates()
Expand Down Expand Up @@ -1157,6 +1157,7 @@ func (s ScanResult) ToHumanScanResult() (out HumanScanResult) {

}
}
out.CertificatesWithChainIssue = s.CertificatesWithChainIssue
out.IsSTARTLS = s.IsSTARTLS
out.IsSSH = s.IsSSH
out.SupportsTLSFallbackSCSV = s.SupportsTLSFallbackSCSV
Expand Down
8 changes: 4 additions & 4 deletions pkg/tlsscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ScanCIDRTLS(cidr string, config tlsmodel.ScanConfig) []tlsmodel.ScanResult
scan := make(map[string]tlsmodel.ScanResult)
results := []<-chan tlsmodel.ScanResult{}
results = append(results, scanCIDRTLS(cidr, config))
for result := range MergeResultChannels(results...) {
for result := range mergeResultChannels(results...) {
key := result.Server + result.Port
if _, present := scan[key]; !present {
scan[key] = result
Expand Down Expand Up @@ -125,7 +125,7 @@ func scanCIDRTLS(cidr string, config tlsmodel.ScanConfig) <-chan tlsmodel.ScanRe
}
}
}
for res := range MergeResultChannels(resultChannels...) {
for res := range mergeResultChannels(resultChannels...) {
res.HostName = originalDomain
scanResults <- res
}
Expand Down Expand Up @@ -180,8 +180,8 @@ func mergeACKChannels(ackChannels ...<-chan portscan.PortACK) <-chan portscan.Po
return out
}

//MergeResultChannels as suggested
func MergeResultChannels(channels ...<-chan tlsmodel.ScanResult) <-chan tlsmodel.ScanResult {
//mergeResultChannels as suggested
func mergeResultChannels(channels ...<-chan tlsmodel.ScanResult) <-chan tlsmodel.ScanResult {
var wg sync.WaitGroup
out := make(chan tlsmodel.ScanResult)
output := func(c <-chan tlsmodel.ScanResult) {
Expand Down
63 changes: 26 additions & 37 deletions pkg/tlsscan_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tlsaudit

//TODO implement tests
import (
"strings"
"testing"
Expand All @@ -9,35 +8,25 @@ import (
)

var (
config = tlsmodel.ScanConfig{}
config = tlsmodel.ScanConfig{
Timeout: 5,
}
)

// func TestIncompleteChain(t *testing.T) {
// for scan := range ScanCIDRTLS("incomplete-chain.badssl.com:443", config) {
// if len(scan.CertificatesWithChainIssue) == 0 {
// t.Errorf("Expected to find a chain issue")
// }
// }
// }
func TestIncompleteChain(t *testing.T) {
for _, scan := range ScanCIDRTLS("incomplete-chain.badssl.com:443", config) {
hs := scan.ToHumanScanResult()
if len(hs.CertificatesWithChainIssue) == 0 {
t.Errorf("Expected to find a chain issue %#v", hs)
}
}
}

func TestRSA8192(t *testing.T) {
// results := []<-chan tlsmodel.ScanResult{}
// scans := make(map[string]tlsmodel.ScanResult)

// results = append(results, ScanCIDRTLS("rsa8192.badssl.com:443", config))
// for result := range MergeResultChannels(results...) {
// key := result.Server + result.Port
// if _, present := scans[key]; !present {
// scans[key] = result
// }
// }

t.Logf("\nStarted scan\n")
for _, scan := range ScanCIDRTLS("rsa8192.badssl.com:443", config) {
t.Log("Got a scan")
for _, certChain := range scan.ToHumanScanResult().CertificatesPerProtocol {
cert := certChain[0]
if cert.PublicKeyAlgorithm != "RSAs" {
if cert.PublicKeyAlgorithm != "RSA" {
t.Errorf("Expecting an RSA public key algorithm but got %s", cert.PublicKeyAlgorithm)
}
kl := strings.Split(cert.Key, " ")[0]
Expand All @@ -48,17 +37,17 @@ func TestRSA8192(t *testing.T) {
}
}

// func TestECDSA384(t *testing.T) {
// for scan := range ScanCIDRTLS("ecc384.badssl.com:443", config) {
// for _, certChain := range scan.ToHumanScanResult().CertificatesPerProtocol {
// cert := certChain[0]
// if cert.PublicKeyAlgorithm != "ECDSA" {
// t.Errorf("Expecting an ECDSA public key algorithm but got %s", cert.PublicKeyAlgorithm)
// }
// kl := strings.Split(cert.Key, " ")[1]
// if kl != "384" {
// t.Errorf("Expecting cert key length of 384, but got %s", kl)
// }
// }
// }
// }
func TestECDSA384(t *testing.T) {
for _, scan := range ScanCIDRTLS("ecc384.badssl.com:443", config) {
for _, certChain := range scan.ToHumanScanResult().CertificatesPerProtocol {
cert := certChain[0]
if cert.PublicKeyAlgorithm != "ECDSA" {
t.Errorf("Expecting an ECDSA public key algorithm but got %s", cert.PublicKeyAlgorithm)
}
kl := strings.Split(cert.Key, " ")[1]
if kl != "384" {
t.Errorf("Expecting cert key length of 384, but got %s", kl)
}
}
}
}

0 comments on commit f49cd00

Please sign in to comment.