-
Notifications
You must be signed in to change notification settings - Fork 88
/
evCheckerWorker.go
130 lines (117 loc) · 3.38 KB
/
evCheckerWorker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package evCheckerWorker
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"encoding/base64"
"encoding/pem"
"github.com/mozilla/tls-observatory/logger"
"github.com/mozilla/tls-observatory/worker"
)
var workerName = "ev-checker"
var workerDesc = `Determines if a given EV policy fulfills the requirements of Mozilla's Root CA program.`
var log = logger.GetLogger()
func init() {
runner := new(evWorker)
worker.RegisterPrinter(workerName, worker.Info{Runner: runner, Description: workerDesc})
if path := os.Getenv("TLSOBS_EVCHECKERPATH"); path != "" {
EvCheckerBinaryName = path
}
_, err := exec.LookPath(EvCheckerBinaryName)
if err != nil {
log.Warn("Could not find ev-checker binary, " + workerName + " disabled.")
return
}
worker.RegisterWorker(workerName, worker.Info{Runner: runner, Description: workerDesc})
}
type evWorker struct {
Binary string
}
type params struct {
OID string
RootCertificate string
}
var portRe = regexp.MustCompile("(.*):([0-9]{1,})$")
func (w evWorker) Run(in worker.Input, res chan worker.Result) {
if portRe.MatchString(in.Target) {
w.error(res, "EV check does not run on target that have a port specified")
return
}
scan, err := in.DBHandle.GetScanByID(in.Scanid)
if err != nil {
w.error(res, "Could not get scan: %s", err)
return
}
out, err := json.Marshal(in.Params)
if err != nil {
w.error(res, "Could not marshal parameters to JSON: %s", err)
}
var params params
err = json.Unmarshal(out, ¶ms)
if err != nil {
w.error(res, "Could not map parameters to struct: %s", err)
}
if params.OID == "" {
w.error(res, "Missing OID parameter, skipping EV check")
return
}
file, err := ioutil.TempFile("", "")
for _, cert := range in.CertificateChain.Certs {
cert, err := base64.StdEncoding.DecodeString(cert)
if err != nil {
w.error(res, "Could not base64-decode certificate: %s", err)
return
}
err = pem.Encode(file, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
if err != nil {
w.error(res, "Could not pem encode certificate: %s", err)
return
}
}
file.Write([]byte(params.RootCertificate))
if err != nil {
w.error(res, "Could not create temporary file to write certificates: %s", err)
return
}
defer file.Close()
if err != nil {
w.error(res, "Could not write certificates to temporary file: %s", err)
return
}
cmd := exec.Command(EvCheckerBinaryName, "-o", params.OID, "-h", scan.Target, "-c", file.Name())
out, err = cmd.Output()
if exitErr, ok := err.(*exec.ExitError); ok {
// if the command returned with a failure, consider that a success from the worker
// point of view, and return the failure back to the client
out, _ = json.Marshal(fmt.Sprintf("%s, Stderr: %s", exitErr, string(exitErr.Stderr)))
res <- worker.Result{
Success: true,
WorkerName: workerName,
Errors: nil,
Result: out,
}
return
} else if err != nil {
w.error(res, "Could not get output from ev-checker: %+v", err)
return
}
out, _ = json.Marshal(string(out))
res <- worker.Result{
Success: true,
WorkerName: workerName,
Errors: nil,
Result: out,
}
}
func (w evWorker) error(res chan worker.Result, messageFormat string, args ...interface{}) {
out, _ := json.Marshal(fmt.Sprintf(messageFormat, args...))
res <- worker.Result{
Success: false,
WorkerName: workerName,
Result: out,
Errors: []string{fmt.Sprintf(messageFormat, args...)},
}
}