-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeb.go
435 lines (353 loc) · 10.3 KB
/
deb.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"reflect"
"strconv"
"strings"
"code.google.com/p/go.crypto/openpgp"
"code.google.com/p/go.crypto/openpgp/clearsign"
"github.com/blakesmith/ar"
)
// DebPackageInfoer for describing extracting information relating to
// a debian package
type DebPackageInfoer interface {
Name() (string, error)
Version() (DebVersion, error)
Description() (string, error)
Maintainer() (string, error)
Architecture() (string, error)
Control() (*ControlFile, error) // Map of all the package metadata
IsSigned() (bool, error) // This package contains a dpkg-sig signature
IsVerified() (bool, error) // The signature has been verified against provided keyring
SignedBy() (*openpgp.Entity, error) // The entity that signed the package
Md5() ([]byte, error)
Sha1() ([]byte, error)
Sha256() ([]byte, error)
Size() (int64, error)
}
// A set of signatures as contained in a dpkg-gig signed package
type debSigs struct {
md5 string
sha1 string
}
// This describes the contains of one signature file contained in a dpkg-sig
// signed package. Several of these may be present in one package
type debSigsFile struct {
version int
signatures map[string]debSigs // A mapping of filenames to signatures
signedBy *openpgp.Entity
}
// This contains the details of a package, along with calculated hashes of the
// contents of hte packag, and the signature details contained in any _gpg signature
// files contained within.
type debPackage struct {
reader io.Reader // the reader pointing to the file
keyRing openpgp.EntityList // Keyring for verification, nil if no signature is to be verified
parsed bool // Have we already parsed this file.
signed bool // Whether this changes file signed
verified bool // Whether the signature is valid
signatures map[string]*debSigsFile // Details of any signature files contained
calcedSigs map[string]debSigs // Calculated signature of the contained files
signedBy *openpgp.Entity // The verified siger of the package
md5 []byte // Package md5
sha1 []byte // Package sha1
sha256 []byte // Package sha256
size int64 // Package size
controlMap *ControlParagraph // This content of the debian/control file
}
// This parses a dpkg-deb signature file. These are files named _gpg.*
// within the ar archive
func parseSigsFile(sig string, kr openpgp.EntityList) (*debSigsFile, error) {
// An _gpg file contains a clear signed "debian conrol" style text blob.
// Version 4 contains lines with an md5, sha1 and size for each file contained
// within the ar archive that makes up the debian package.
result := &debSigsFile{}
result.signatures = make(map[string]debSigs)
msg, _ := clearsign.Decode([]byte(sig))
bsig := bytes.NewReader(msg.Bytes)
result.signedBy, _ = openpgp.CheckDetachedSignature(kr, bsig, msg.ArmoredSignature.Body)
sigDataReader := bytes.NewReader(msg.Plaintext)
controlFile, _ := ParseDebianControl(sigDataReader, nil)
control := controlFile.Data[0]
strVersion, _ := control.GetValue("Version")
result.version, _ = strconv.Atoi(strVersion)
fileData, _ := control.GetValues("Files")
for i := range fileData {
if *fileData[i] == "" {
continue
}
fields := strings.Fields(
strings.TrimSpace(
*fileData[i]))
md5 := fields[0]
sha1 := fields[1]
fileName := fields[3]
result.signatures[fileName] = debSigs{
md5: md5,
sha1: sha1,
}
}
return result, nil
}
// NewDebPackage creates a debian package description, which will be lazily parsed and
// verified against the provided keyring
func NewDebPackage(r io.Reader, kr openpgp.EntityList) DebPackageInfoer {
return &debPackage{
reader: r,
keyRing: kr,
}
}
// Does this package contain signatures
func (d *debPackage) IsSigned() (bool, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return false, err
}
}
return d.signed, nil
}
// Are any of the packages verified by our provided
// keyring
func (d *debPackage) IsVerified() (bool, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return false, err
}
}
return d.verified, nil
}
// Who signed the package
func (d *debPackage) SignedBy() (*openpgp.Entity, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return nil, err
}
}
return d.signedBy, nil
}
// Generic access to the metadata for the package
func (d *debPackage) Control() (*ControlFile, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return nil, err
}
}
return &ControlFile{
Data: []*ControlParagraph{d.controlMap},
}, nil
}
// The package name
func (d *debPackage) Name() (string, error) {
return d.getMandatoryControl("Package")
}
// The package version string
func (d *debPackage) Version() (debver DebVersion, err error) {
verStr, err := d.getMandatoryControl("Version")
if err != nil {
return
}
return ParseDebVersion(verStr)
}
// The package description
func (d *debPackage) Description() (string, error) {
return d.getMandatoryControl("Description")
}
// The package maintainer contact details
func (d *debPackage) Maintainer() (string, error) {
return d.getMandatoryControl("Maintainer")
}
// The package maintainer contact details
func (d *debPackage) Architecture() (string, error) {
return d.getMandatoryControl("Architecture")
}
// Retried any peace of metadata that is mandatory
func (d *debPackage) getMandatoryControl(key string) (string, error) {
res, ok, err := d.getControl(key)
if err != nil {
return "", err
}
if !ok {
return "", fmt.Errorf("key %s not found in package metadata", key)
}
return res, nil
}
// Retrieve any emtadata from the control infrormation
func (d *debPackage) getControl(key string) (string, bool, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return "", false, err
}
}
ctrl, err := d.Control()
result, ok := ctrl.Data[0].GetValue(key)
return result, ok, nil
}
func (d *debPackage) Md5() ([]byte, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return nil, err
}
}
return d.md5, nil
}
func (d *debPackage) Sha1() ([]byte, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return nil, err
}
}
return d.sha1, nil
}
func (d *debPackage) Sha256() ([]byte, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return nil, err
}
}
return d.sha256, nil
}
func (d *debPackage) Size() (int64, error) {
var err error
if !d.parsed {
err = d.parseDebPackage()
if err != nil {
return 0, err
}
}
return d.size, nil
}
// parse a debian pacakge.. Debian packages are ar archives containing;
// data.tar.gz - The actual package content - changelog is in here
// contro.tar.gz - The control data from when the package was built
// debian-binary - Package version info
// _gpg* - dpkg-sig signatures covering hashes of the other files
func (d *debPackage) parseDebPackage() (err error) {
pkghasher := MakeWriteHasher(ioutil.Discard)
pkgtee := io.TeeReader(d.reader, pkghasher)
arReader := ar.NewReader(pkgtee)
d.signatures = make(map[string]*debSigsFile)
var controlBytes bytes.Buffer
var controlReader io.Reader
// We'll keep a running set of file signatures to
// compare with any included signatures
d.calcedSigs = make(map[string]debSigs)
d.signed = false
d.verified = false
// We walk over each file in the ar archive, parsing
// as we go
for {
header, err := arReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
hasher := MakeWriteHasher(ioutil.Discard)
arfn := strings.TrimRight(header.Name, "/")
switch {
case strings.HasPrefix(arfn, "_gpg"):
// This file is a dpkg-sig signature file
d.signed = true
var sig bytes.Buffer
io.Copy(
io.MultiWriter(&sig, hasher),
arReader)
d.signatures[arfn], _ = parseSigsFile(sig.String(), d.keyRing)
case arfn == "control.tar.gz":
// This file is the control.tar.gz, we can get the
// package metadata from here in a file called ./control
tee := io.TeeReader(arReader, hasher)
gzReader, err := gzip.NewReader(tee)
if err != nil {
return err
}
tarReader := tar.NewReader(gzReader)
if err != nil {
return err
}
for {
tarHeader, err := tarReader.Next()
if err == io.EOF {
break
}
if tarHeader.Name == "./control" || tarHeader.Name == "control" {
io.Copy(&controlBytes, tarReader)
}
}
default:
// We copy other files so that the hasher builds the hash
// that we can then verify against the sigs in any sigs file
io.Copy(hasher, arReader)
}
d.calcedSigs[header.Name] = debSigs{
md5: hex.EncodeToString(hasher.MD5Sum()),
sha1: hex.EncodeToString(hasher.SHA1Sum()),
}
}
d.md5 = pkghasher.MD5Sum()
d.sha1 = pkghasher.SHA1Sum()
d.sha256 = pkghasher.SHA256Sum()
d.size = pkghasher.Count()
if controlBytes.Len() == 0 {
return errors.New("did not find control file in debian archive")
}
controlReader = bytes.NewReader(controlBytes.Bytes())
controlFile, err := ParseDebianControl(controlReader, nil)
if err != nil {
return errors.New("parsing control failed, " + err.Error())
}
control := controlFile.Data[0]
// For each signature file we find, we'll check if we can
// verify it, and if it covers all included files
d.verified = false
for s := range d.signatures {
sigs := d.signatures[s].signatures
testSigs := make(map[string]debSigs)
// The calculated signature list includes the file containing
// the actual signature we are verifying, we must filter it out
for k, v := range d.calcedSigs {
if k != s {
testSigs[k] = v
}
}
if reflect.DeepEqual(testSigs, sigs) {
d.verified = true
d.signedBy = d.signatures[s].signedBy
break
}
}
d.controlMap = control
d.parsed = true
return nil
}
// FormatDpkgControlFile outputs a debian control file, with some commong fields in
// a sensible order
func FormatDpkgControlFile(ctrlWriter io.Writer, paragraphs ControlFile) {
debStartFields := []string{"Package", "Version", "Filename", "Directory", "Size"}
debEndFields := []string{"MD5Sum", "MD5sum", "SHA1", "SHA256", "Description"}
WriteDebianControl(ctrlWriter, paragraphs, debStartFields, debEndFields)
}