-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkgverify.go
209 lines (182 loc) · 4.37 KB
/
pkgverify.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
205
206
207
208
209
package main
import (
"crypto"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"go/scanner"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"time"
)
const filechunk = 8192
const (
certPEMBlockType = "BEGIN CERTIFICATE"
)
var (
file = flag.String("file", "", "the file to verify")
certificate = flag.String("cert", "", "the certificate to use for signature verification")
url = flag.String("url", "", "the URL to check for obtaining the signing certificate")
root_dir = flag.String("root-dir", "", "the path to a directory containing rootCAs")
exitCode = 0
)
var rootPool *x509.CertPool
type Manifest struct {
Company string `json:",omitempty"`
Product string `json:",omitempty"`
PackageName string
PackageSha1 string // use string so value easy to compare with shasum output
ReleaseDate time.Time
Signature []byte `json:",omitempty"`
}
func usage() {
fmt.Fprintf(os.Stderr, "\nusage: pkgverify -file [path] -cert [path]\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(2)
}
func report(err error) {
scanner.PrintError(os.Stderr, err)
exitCode = 2
}
func main() {
pkgVerifyMain()
os.Exit(exitCode)
}
func pkgVerifyMain() {
flag.Usage = usage
flag.Parse()
if *file == "" {
usage()
}
if *certificate == "" && *url == "" {
usage()
}
// open the file to verify and calculate the file's Sha1
fileToVerify, err := os.Open(*file)
if err != nil {
report(err)
return
}
defer fileToVerify.Close()
pkgSha1 := calcSha(fileToVerify)
// open and unmarshal the manifest
manifestToVerifyBytes, err := ioutil.ReadFile(*file + ".manifest")
if err != nil {
report(err)
return
}
var manifestToVerify Manifest
err = json.Unmarshal(manifestToVerifyBytes, &manifestToVerify)
if err != nil {
report(err)
return
}
// verify the SHA1s match
if fmt.Sprintf("%x", pkgSha1) != manifestToVerify.PackageSha1 {
report(fmt.Errorf("SHAs don't match: %x and %s", pkgSha1, manifestToVerify.PackageSha1))
return
}
// read the signing certificate from a file
cert, err := readCertFile(*certificate)
if err != nil {
report(err)
return
}
// load the root CAs from a directory
rootPool = x509.NewCertPool()
err = filepath.Walk(*root_dir, loadRootFiles)
if err != nil {
report(err)
return
}
// verify the certificate is signed by trusted roots
opts := x509.VerifyOptions{
Roots: rootPool,
}
_, err = cert.Verify(opts)
if err != nil {
report(err)
return
}
rsaPub, ok := cert.PublicKey.(*rsa.PublicKey)
if !ok {
report(fmt.Errorf("Not an RSA public key"))
return
}
// hold the signature from the manifest
manSig := make([]byte, len(manifestToVerify.Signature))
copy(manSig, manifestToVerify.Signature)
// clear the manifest signature
manifestToVerify.Signature = nil
// marshal the manifest without the signature
manifestNoSig, err := json.Marshal(manifestToVerify)
if err != nil {
report(err)
return
}
// calculate the SHA1 of the manifest
manifestSha1 := sha1.New()
manifestSha1.Write(manifestNoSig)
manShaVal := manifestSha1.Sum(nil)
var h crypto.Hash
err = rsa.VerifyPKCS1v15(rsaPub, h, manShaVal, manSig)
if err != nil {
report(err)
return
}
fmt.Println("\nSuccess!")
fmt.Printf("File %s verified against %s\n\n", fileToVerify.Name(),
fileToVerify.Name()+".manifest")
}
func readCertFile(path string) (*x509.Certificate, error) {
certBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
pemBlock, _ := pem.Decode(certBytes)
if pemBlock == nil {
return nil, err
}
cert, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}
// load all the certificates in a directory. This should include any
// intermediate CAs
func loadRootFiles(path string, info os.FileInfo, a_err error) error {
if a_err != nil {
return a_err
}
if info.IsDir() {
return nil
}
cert, err := readCertFile(path)
if err != nil {
return err
}
rootPool.AddCert(cert)
return nil
}
func calcSha(fileToVerify *os.File) []byte {
info, _ := fileToVerify.Stat()
filesize := info.Size()
blocks := uint64(math.Ceil(float64(filesize) / float64(filechunk)))
hash := sha1.New()
for i := uint64(0); i < blocks; i++ {
blocksize := int(math.Min(filechunk, float64(filesize-int64(i*filechunk))))
buf := make([]byte, blocksize)
fileToVerify.Read(buf)
io.WriteString(hash, string(buf))
}
return hash.Sum(nil)
}