certspotter/cmd/common.go

232 lines
6.9 KiB
Go
Raw Normal View History

2016-02-05 03:45:37 +01:00
package cmd
import (
"flag"
"fmt"
"log"
"os"
"bytes"
2016-02-05 05:16:25 +01:00
"os/user"
2016-02-18 01:03:49 +01:00
"encoding/json"
2016-02-05 03:45:37 +01:00
"sync"
2016-02-05 05:16:25 +01:00
"strings"
"path/filepath"
"time"
"strconv"
2016-02-05 03:45:37 +01:00
"src.agwa.name/ctwatch"
"src.agwa.name/ctwatch/ct"
2016-02-05 03:45:37 +01:00
)
var batchSize = flag.Int("batch_size", 1000, "Max number of entries to request at per call to get-entries")
var numWorkers = flag.Int("num_workers", 2, "Number of concurrent matchers")
var parallelFetch = flag.Int("parallel_fetch", 2, "Number of concurrent GetEntries fetches")
var script = flag.String("script", "", "Script to execute when a matching certificate is found")
2016-02-18 01:03:49 +01:00
var logsFilename = flag.String("logs", "", "JSON file containing log URLs")
var underwater = flag.Bool("underwater", false, "Monitor certificates from distrusted CAs")
2016-02-05 05:16:25 +01:00
var noSave = flag.Bool("no_save", false, "Do not save a copy of matching certificates")
2016-02-05 03:45:37 +01:00
var verbose = flag.Bool("verbose", false, "Be verbose")
var allTime = flag.Bool("all_time", false, "Scan certs from all time, not just since last scan")
2016-02-05 05:16:25 +01:00
var stateDir string
2016-02-05 03:45:37 +01:00
var printMutex sync.Mutex
2016-02-05 05:16:25 +01:00
func isRoot () bool {
return os.Geteuid() == 0
}
func homedir () string {
home := os.Getenv("HOME")
if home != "" {
return home
}
user, err := user.Current()
if err == nil {
return user.HomeDir
}
panic("Unable to determine home directory")
}
func DefaultStateDir (programName string) string {
if isRoot() {
return filepath.Join("/var/lib", programName)
} else {
return filepath.Join(homedir(), "." + programName)
}
}
func LogEntry (info *ctwatch.EntryInfo) {
2016-02-05 05:16:25 +01:00
if !*noSave {
var alreadyPresent bool
var err error
alreadyPresent, info.Filename, err = ctwatch.WriteCertRepository(filepath.Join(stateDir, "certs"), info.Entry)
2016-02-05 03:45:37 +01:00
if err != nil {
log.Print(err)
}
if alreadyPresent {
return
}
}
if *script != "" {
if err := info.InvokeHookScript(*script); err != nil {
2016-02-05 03:45:37 +01:00
log.Print(err)
}
} else {
printMutex.Lock()
info.Write(os.Stdout)
2016-02-05 03:45:37 +01:00
fmt.Fprintf(os.Stdout, "\n")
printMutex.Unlock()
}
}
2016-02-05 05:16:25 +01:00
func defangLogUri (logUri string) string {
return strings.Replace(strings.Replace(logUri, "://", "_", 1), "/", "_", -1)
}
2016-02-05 03:45:37 +01:00
func saveEvidence (logUri string, firstSTH *ct.SignedTreeHead, secondSTH *ct.SignedTreeHead) (string, string, error) {
now := strconv.FormatInt(time.Now().Unix(), 10)
firstFilename := filepath.Join(stateDir, "evidence", defangLogUri(logUri) + ".inconsistent." + now + ".first")
if err := ctwatch.WriteSTHFile(firstFilename, firstSTH); err != nil {
return "", "", err
}
secondFilename := filepath.Join(stateDir, "evidence", defangLogUri(logUri) + ".inconsistent." + now + ".second")
if err := ctwatch.WriteSTHFile(secondFilename, secondSTH); err != nil {
return "", "", err
}
return firstFilename, secondFilename, nil
}
func Main (argStateDir string, processCallback ctwatch.ProcessCallback) {
2016-02-05 05:16:25 +01:00
stateDir = argStateDir
2016-02-05 03:45:37 +01:00
2016-02-18 01:03:49 +01:00
var logs []ctwatch.LogInfo
2016-02-05 05:16:25 +01:00
if *logsFilename != "" {
logFile, err := os.Open(*logsFilename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: Error opening logs file for reading: %s: %s\n", os.Args[0], *logsFilename, err)
os.Exit(1)
2016-02-05 05:16:25 +01:00
}
defer logFile.Close()
2016-02-18 01:03:49 +01:00
var logFileObj ctwatch.LogInfoFile
if err := json.NewDecoder(logFile).Decode(&logFileObj); err != nil {
fmt.Fprintf(os.Stderr, "%s: Error decoding logs file: %s: %s\n", os.Args[0], *logsFilename, err)
os.Exit(1)
2016-02-05 05:16:25 +01:00
}
2016-02-18 01:03:49 +01:00
logs = logFileObj.Logs
} else if *underwater {
logs = ctwatch.UnderwaterLogs
2016-02-05 05:16:25 +01:00
} else {
2016-02-18 01:03:49 +01:00
logs = ctwatch.DefaultLogs
2016-02-05 03:45:37 +01:00
}
2016-02-05 05:16:25 +01:00
if err := os.Mkdir(stateDir, 0777); err != nil && !os.IsExist(err) {
fmt.Fprintf(os.Stderr, "%s: Error creating state directory: %s: %s\n", os.Args[0], stateDir, err)
os.Exit(1)
2016-02-05 03:45:37 +01:00
}
for _, subdir := range []string{"certs", "sths", "evidence"} {
2016-02-05 05:16:25 +01:00
path := filepath.Join(stateDir, subdir)
if err := os.Mkdir(path, 0777); err != nil && !os.IsExist(err) {
fmt.Fprintf(os.Stderr, "%s: Error creating state directory: %s: %s\n", os.Args[0], path, err)
os.Exit(1)
2016-02-05 03:45:37 +01:00
}
}
/*
* Exit code bits:
* 1 = initialization/system error
* 2 = usage error
* 4 = error communicating with log
* 8 = log misbehavior
*/
2016-02-05 05:16:25 +01:00
exitCode := 0
2016-02-18 01:03:49 +01:00
for _, logInfo := range logs {
logUri := logInfo.FullURI()
logKey, err := logInfo.ParsedPublicKey()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s: Bad public key: %s\n", os.Args[0], logUri, err)
os.Exit(1)
2016-02-18 01:03:49 +01:00
}
stateFilename := filepath.Join(stateDir, "sths", defangLogUri(logUri))
2016-02-18 19:09:33 +01:00
prevSTH, err := ctwatch.ReadSTHFile(stateFilename)
2016-02-05 05:16:25 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, "%s: Error reading state file: %s: %s\n", os.Args[0], stateFilename, err)
os.Exit(1)
2016-02-05 05:16:25 +01:00
}
opts := ctwatch.ScannerOptions{
BatchSize: *batchSize,
NumWorkers: *numWorkers,
ParallelFetch: *parallelFetch,
Quiet: !*verbose,
}
2016-02-18 19:23:07 +01:00
scanner := ctwatch.NewScanner(logUri, logKey, opts)
2016-02-05 05:16:25 +01:00
latestSTH, err := scanner.GetSTH()
2016-02-05 05:16:25 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, "%s: Error retrieving STH from log: %s: %s\n", os.Args[0], logUri, err)
exitCode |= 4
2016-02-05 05:16:25 +01:00
continue
}
var startIndex uint64
if prevSTH != nil {
startIndex = prevSTH.TreeSize
} else if *allTime {
startIndex = 0
} else {
startIndex = latestSTH.TreeSize
}
if latestSTH.TreeSize > startIndex {
var treeBuilder *ctwatch.MerkleTreeBuilder
if prevSTH != nil {
2016-02-18 19:23:07 +01:00
var valid bool
var err error
valid, treeBuilder, err = scanner.CheckConsistency(prevSTH, latestSTH)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: Error fetching consistency proof: %s: %s\n", os.Args[0], logUri, err)
exitCode |= 4
continue
}
if !valid {
firstFilename, secondFilename, err := saveEvidence(logUri, prevSTH, latestSTH)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s: Consistency proof failed - the log has misbehaved! Saving evidence of misbehavior failed: %s\n", os.Args[0], logUri, err)
} else {
fmt.Fprintf(os.Stderr, "%s: %s: Consistency proof failed - the log has misbehaved! Evidence of misbehavior has been saved to '%s' and '%s'.\n", os.Args[0], logUri, firstFilename, secondFilename)
}
exitCode |= 8
continue
}
} else {
treeBuilder = &ctwatch.MerkleTreeBuilder{}
}
if err := scanner.Scan(int64(startIndex), int64(latestSTH.TreeSize), processCallback, treeBuilder); err != nil {
2016-02-05 05:16:25 +01:00
fmt.Fprintf(os.Stderr, "%s: Error scanning log: %s: %s\n", os.Args[0], logUri, err)
exitCode |= 4
2016-02-05 05:16:25 +01:00
continue
}
rootHash := treeBuilder.Finish()
if !bytes.Equal(rootHash, latestSTH.SHA256RootHash[:]) {
fmt.Fprintf(os.Stderr, "%s: %s: Validation of log entries failed - calculated tree root (%x) does not match signed tree root (%s). If this error persists for an extended period, it should be construed as misbehavior by the log.\n", os.Args[0], logUri, rootHash, latestSTH.SHA256RootHash)
exitCode |= 8
continue
}
2016-02-05 05:16:25 +01:00
}
2016-02-18 19:09:33 +01:00
if err := ctwatch.WriteSTHFile(stateFilename, latestSTH); err != nil {
2016-02-05 05:16:25 +01:00
fmt.Fprintf(os.Stderr, "%s: Error writing state file: %s: %s\n", os.Args[0], stateFilename, err)
os.Exit(1)
2016-02-05 05:16:25 +01:00
}
2016-02-05 03:45:37 +01:00
}
2016-02-05 05:16:25 +01:00
os.Exit(exitCode)
2016-02-05 03:45:37 +01:00
}