-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
75 lines (63 loc) · 1.71 KB
/
verify.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
package core
import (
"os"
"strings"
"github.com/SierraSoftworks/shig/internal/publickeys"
"github.com/SierraSoftworks/sshsign-go"
"golang.org/x/crypto/ssh"
)
type Verifier struct {
output Output
sigFile string
verifier sshsign.Verifier
validator publickeys.Validator
}
func NewVerifier(
output Output,
namespace, hash, sigFile string,
validator publickeys.Validator,
) *Verifier {
verifier := sshsign.DefaultVerifier(namespace, hash)
return &Verifier{
output: output,
sigFile: sigFile,
verifier: verifier,
validator: validator,
}
}
func (v *Verifier) Verify(file string) error {
f, err := os.Open(file)
if err != nil {
v.output.Printf("FAIL: '%s' could not be opened\n", file)
return err
}
defer f.Close()
sigFile := strings.ReplaceAll(v.sigFile, "%f", file)
sf, err := os.ReadFile(sigFile)
if err != nil {
v.output.Printf("FAIL: '%s' does not have a corresponding signature file '%s'\n", file, sigFile)
return err
}
sig, _, err := sshsign.UnmarshalArmoured(sf)
if err != nil {
v.output.Printf("FAIL: '%s' is not a well-formatted signature file.\n", sigFile)
return err
}
if err := v.verifier.Verify(f, sig); err != nil {
v.output.Printf("FAIL: '%s' does not match the signature file '%s'\n", file, sigFile)
return err
}
key, err := sig.GetPublicKey()
if err != nil {
v.output.Printf("FAIL: '%s' does not contain a valid public key in its signature\n", file)
return err
}
if v.validator != nil {
if err := v.validator.Validate(key); err != nil {
v.output.Printf("FAIL: '%s' is signed by an untrusted key: %s\n", file, ssh.FingerprintSHA256(key))
return err
}
}
v.output.Printf("PASS: '%s' is signed by '%s'\n", file, ssh.FingerprintSHA256(key))
return nil
}