Debian Codegraph @

lib/signdeb/verify.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package signdeb
18
19 import (
20 "bufio"
21 "bytes"
22 "crypto"
23 "errors"
24 "fmt"
25 "io"
26 "io/ioutil"
27 "strings"
28
29 "github.com/qur/ar"
30 "golang.org/x/crypto/openpgp"
31
32 "github.com/sassoftware/relic/v7/lib/pgptools"
33 )
34
35 // Extract and verify signatures from a Debian package. A keyring of known PGP
36 // certificates must be provided to validate the signatures; if the needed key
37 // is missing then an ErrNoKey value is returned.
38 func Verify(r io.Reader, keyring openpgp.EntityList, skipDigest bool) (map[string]*pgptools.PgpSignature, error) {
39 reader := ar.NewReader(r)
40 digests := make(map[string]string)
41 sigs := make(map[string][]byte)
42 for {
43 hdr, err := reader.Next()
44 if err == io.EOF {
45 break
46 } else if err != nil {
47 return nil, err
48 }
49 if strings.HasPrefix(hdr.Name, "_gpg") {
50 role := hdr.Name[4:]
51 sigs[role], err = ioutil.ReadAll(reader)
52 if err != nil {
53 return nil, err
54 }
55 } else if !skipDigest {
56 md5 := crypto.MD5.New()
57 sha1 := crypto.SHA1.New()
58 if _, err := io.Copy(io.MultiWriter(md5, sha1), reader); err != nil {
59 return nil, err
60 }
61 digests[hdr.Name] = fmt.Sprintf("%x %x", md5.Sum(nil), sha1.Sum(nil))
62 }
63 }
64 ret := make(map[string]*pgptools.PgpSignature, len(sigs))
65 for role, sig := range sigs {
66 var body bytes.Buffer
67 info, err := pgptools.VerifyClearSign(bytes.NewReader(sig), &body, keyring)
68 if err != nil {
69 return nil, err
70 }
71 if !skipDigest {
72 if err := checkSig(role, &body, digests); err != nil {
73 return nil, err
74 }
75 }
76 ret[role] = info
77 }
78 return ret, nil
79 }
80
81 func checkSig(role string, body io.Reader, digests map[string]string) error {
82 sawFiles := false
83 scanner := bufio.NewScanner(body)
84 // read header
85 for scanner.Scan() {
86 line := scanner.Text()
87 if line == "Files:" {
88 sawFiles = true
89 break
90 }
91 }
92 if !sawFiles {
93 return errors.New("malformed signature")
94 }
95 // read digests
96 checked := make(map[string]bool, len(digests))
97 for scanner.Scan() {
98 line := scanner.Text()
99 if line == "" {
100 break
101 } else if line[0] != '\t' || len(line) < 76 {
102 return errors.New("malformed signature")
103 }
104 parts := strings.SplitN(line[1:], " ", 4)
105 sums := parts[0] + " " + parts[1]
106 name := parts[3]
107 calculated := digests[name]
108 if calculated == "" {
109 return fmt.Errorf("signature references unknown file %s", name)
110 } else if calculated != sums {
111 return fmt.Errorf("signature mismatch on file %s: (%s) != (%s)", name, calculated, sums)
112 }
113 checked[name] = true
114 }
115 // make sure everything was checked
116 for name := range digests {
117 if checked[name] {
118 continue
119 }
120 return fmt.Errorf("signature does not cover file: %s", name)
121 }
122 return nil
123 }