-
Notifications
You must be signed in to change notification settings - Fork 3
/
verify.go
204 lines (172 loc) · 4.42 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"os"
"github.com/cashapp/pivit/cmd/pivit/status"
"github.com/cashapp/pivit/cmd/pivit/utils"
"github.com/cashapp/pivit/cmd/pivit/yubikey"
"github.com/certifi/gocertifi"
cms "github.com/github/ietf-cms"
"github.com/go-piv/piv-go/piv"
"github.com/pkg/errors"
)
// commandVerify verifies the data and signatures supplied in fileArgs
func commandVerify(fileArgs []string) error {
status.EmitNewSign()
if len(fileArgs) < 2 {
return verifyAttached(fileArgs)
}
return verifyDetached(fileArgs)
}
func verifyAttached(fileArgs []string) error {
var (
f io.ReadCloser
err error
)
// read in signature
if len(fileArgs) == 1 {
if f, err = os.Open(fileArgs[0]); err != nil {
return errors.Wrapf(err, "open signature file (%s)", fileArgs[0])
}
defer func() {
_ = f.Close()
}()
} else {
f = os.Stdin
}
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, f); err != nil {
return errors.Wrap(err, "read signature")
}
// try decoding as PEM
var ber []byte
if blk, _ := pem.Decode(buf.Bytes()); blk != nil {
ber = blk.Bytes
} else {
ber = buf.Bytes()
}
// parse signature
sd, err := cms.ParseSignedData(ber)
if err != nil {
return errors.Wrap(err, "parse signature")
}
// verify signature
chains, err := sd.Verify(verifyOpts())
if err != nil {
if len(chains) > 0 {
status.EmitBadSig(chains)
} else {
// TODO: We're omitting a bunch of arguments here.
status.EmitErrSig()
}
return errors.Wrap(err, "verify signature")
}
var (
cert = chains[0][0][0]
fpr = utils.CertHexFingerprint(cert)
subj = cert.Subject.String()
)
_, _ = fmt.Fprintf(os.Stderr, "pivit: Signature made using certificate ID 0x%s\n", fpr)
status.EmitGoodSig(chains)
// TODO: Maybe split up signature checking and certificate checking so we can
// output something more meaningful.
_, _ = fmt.Fprintf(os.Stderr, "pivit: Good signature from \"%s\"\n", subj)
status.EmitTrustFully()
return nil
}
func verifyDetached(fileArgs []string) error {
var (
f io.ReadCloser
err error
)
// read in signature
if f, err = os.Open(fileArgs[0]); err != nil {
return errors.Wrapf(err, "open signature file (%s)", fileArgs[0])
}
defer func() {
_ = f.Close()
}()
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, f); err != nil {
return errors.Wrap(err, "read signature file")
}
// try decoding as PEM
var ber []byte
if blk, _ := pem.Decode(buf.Bytes()); blk != nil {
ber = blk.Bytes
} else {
ber = buf.Bytes()
}
// parse signature
sd, err := cms.ParseSignedData(ber)
if err != nil {
return errors.Wrap(err, "parse signature")
}
// read in signed data
if fileArgs[1] == "-" {
f = os.Stdin
} else {
if f, err = os.Open(fileArgs[1]); err != nil {
return errors.Wrapf(err, "open message file (%s)", fileArgs[1])
}
defer func() {
_ = f.Close()
}()
}
// verify signature
buf.Reset()
if _, err = io.Copy(buf, f); err != nil {
return errors.Wrap(err, "read message file")
}
chains, err := sd.VerifyDetached(buf.Bytes(), verifyOpts())
if err != nil {
if len(chains) > 0 {
status.EmitBadSig(chains)
} else {
// TODO: We're omitting a bunch of arguments here.
status.EmitErrSig()
}
return errors.Wrap(err, "failed to verify signature")
}
var (
cert = chains[0][0][0]
fpr = utils.CertHexFingerprint(cert)
subj = cert.Subject.String()
)
_, _ = fmt.Fprintf(os.Stderr, "pivit: Signature made using certificate ID 0x%s\n", fpr)
status.EmitGoodSig(chains)
// TODO: Maybe split up signature checking and certificate checking so we can
// output something more meaningful.
_, _ = fmt.Fprintf(os.Stderr, "pivit: Good signature from \"%s\"\n", subj)
status.EmitTrustFully()
return nil
}
func verifyOpts() x509.VerifyOptions {
roots, err := x509.SystemCertPool()
if err != nil {
// SystemCertPool isn't implemented for Windows. fall back to mozilla trust store
roots, err = gocertifi.CACerts()
if err != nil {
// fall back to an empty store
// verification will likely fail
roots = x509.NewCertPool()
}
}
yk, err := yubikey.Yubikey()
if err == nil {
cert, err := yk.Certificate(piv.SlotCardAuthentication)
if err == nil {
roots.AddCert(cert)
}
_ = yk.Close()
}
return x509.VerifyOptions{
Roots: roots,
// TODO: we might want to limit signature verification to only certificates that have the right key usage extension
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}
}