forked from github/smimesign
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_verify.go
189 lines (157 loc) · 3.87 KB
/
command_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
package main
import (
"bytes"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"os"
"github.com/certifi/gocertifi"
"github.com/mastahyeti/cms"
"github.com/pkg/errors"
)
func commandVerify() error {
sNewSig.emit()
if len(fileArgs) < 2 {
return verifyAttached()
}
return verifyDetached()
}
func verifyAttached() 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, "failed to open signature file (%s)", fileArgs[0])
}
defer f.Close()
} else {
f = stdin
}
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, f); err != nil {
return errors.Wrap(err, "failed to read signature")
}
// Try decoding as PEM
var der []byte
if blk, _ := pem.Decode(buf.Bytes()); blk != nil {
der = blk.Bytes
} else {
der = buf.Bytes()
}
// Parse signature
sd, err := cms.ParseSignedData(der)
if err != nil {
return errors.Wrap(err, "failed to parse signature")
}
// Verify signature
chains, err := sd.Verify(verifyOpts())
if err != nil {
if len(chains) > 0 {
emitBadSig(chains)
} else {
// TODO: We're omitting a bunch of arguments here.
sErrSig.emit()
}
return errors.Wrap(err, "failed to verify signature")
}
var (
cert = chains[0][0][0]
fpr = certHexFingerprint(cert)
subj = cert.Subject.String()
)
fmt.Fprintf(stderr, "smimesign: Signature made using certificate ID 0x%s\n", fpr)
emitGoodSig(chains)
// TODO: Maybe split up signature checking and certificate checking so we can
// output something more meaningful.
fmt.Fprintf(stderr, "smimesign: Good signature from \"%s\"\n", subj)
emitTrustFully()
return nil
}
func verifyDetached() error {
var (
f io.ReadCloser
err error
)
// Read in signature
if f, err = os.Open(fileArgs[0]); err != nil {
return errors.Wrapf(err, "failed to open signature file (%s)", fileArgs[0])
}
defer f.Close()
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, f); err != nil {
return errors.Wrap(err, "failed to read signature file")
}
// Try decoding as PEM
var der []byte
if blk, _ := pem.Decode(buf.Bytes()); blk != nil {
der = blk.Bytes
} else {
der = buf.Bytes()
}
// Parse signature
sd, err := cms.ParseSignedData(der)
if err != nil {
return errors.Wrap(err, "failed to parse signature")
}
// Read in signed data
if fileArgs[1] == "-" {
f = stdin
} else {
if f, err = os.Open(fileArgs[1]); err != nil {
errors.Wrapf(err, "failed to open message file (%s)", fileArgs[1])
}
defer f.Close()
}
// Verify signature
buf.Reset()
if _, err = io.Copy(buf, f); err != nil {
return errors.Wrap(err, "failed to read message file")
}
chains, err := sd.VerifyDetached(buf.Bytes(), verifyOpts())
if err != nil {
if len(chains) > 0 {
emitBadSig(chains)
} else {
// TODO: We're omitting a bunch of arguments here.
sErrSig.emit()
}
return errors.Wrap(err, "failed to verify signature")
}
var (
cert = chains[0][0][0]
fpr = certHexFingerprint(cert)
subj = cert.Subject.String()
)
fmt.Fprintf(stderr, "smimesign: Signature made using certificate ID 0x%s\n", fpr)
emitGoodSig(chains)
// TODO: Maybe split up signature checking and certificate checking so we can
// output something more meaningful.
fmt.Fprintf(stderr, "smimesign: Good signature from \"%s\"\n", subj)
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()
}
}
for _, ident := range idents {
if cert, err := ident.Certificate(); err == nil {
roots.AddCert(cert)
}
}
return x509.VerifyOptions{
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}
}