Debian Codegraph @

signers/rpm/signer.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 rpm
18
19 // Sign RedHat packages
20
21 import (
22 "encoding/hex"
23 "fmt"
24 "io"
25 "os"
26 "strings"
27 "time"
28
29 "github.com/rs/zerolog"
30 rpmutils "github.com/sassoftware/go-rpmutils"
31
32 "github.com/sassoftware/relic/v7/lib/audit"
33 "github.com/sassoftware/relic/v7/lib/binpatch"
34 "github.com/sassoftware/relic/v7/lib/certloader"
35 "github.com/sassoftware/relic/v7/lib/magic"
36 "github.com/sassoftware/relic/v7/lib/pgptools"
37 "github.com/sassoftware/relic/v7/signers"
38 "github.com/sassoftware/relic/v7/signers/sigerrors"
39 )
40
41 var RpmSigner = &signers.Signer{
42 Name: "rpm",
43 Magic: magic.FileTypeRPM,
44 CertTypes: signers.CertTypePgp,
45 FormatLog: formatLog,
46 Sign: sign,
47 Verify: verify,
48 }
49
50 func init() {
51 signers.Register(RpmSigner)
52 }
53
54 func formatLog(attrs *audit.Info) *zerolog.Event {
55 return attrs.AttrsForLog("rpm.")
56 }
57
58 func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
59 config := &rpmutils.SignatureOptions{
60 Hash: opts.Hash,
61 CreationTime: opts.Time.UTC().Round(time.Second),
62 }
63 header, err := rpmutils.SignRpmStream(r, cert.PgpKey.PrivateKey, config)
64 if err != nil {
65 return nil, err
66 }
67 blob, err := header.DumpSignatureHeader(true)
68 if err != nil {
69 return nil, err
70 }
71 patch := binpatch.New()
72 patch.Add(0, int64(header.OriginalSignatureHeaderSize()), blob)
73 md5, _ := header.GetBytes(rpmutils.SIG_MD5)
74 sha1, _ := header.GetString(rpmutils.SIG_SHA1)
75 opts.Audit.Attributes["rpm.nevra"] = nevra(header)
76 opts.Audit.Attributes["rpm.md5"] = hex.EncodeToString(md5)
77 opts.Audit.Attributes["rpm.sha1"] = sha1
78 return opts.SetBinPatch(patch)
79 }
80
81 func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) {
82 // TODO: add a flag to skip payload digest to rpmutils.Verify
83 header, sigs, err := rpmutils.Verify(f, opts.TrustedPgp)
84 if err != nil {
85 return nil, err
86 }
87 if len(sigs) == 0 {
88 return nil, sigerrors.NotSignedError{Type: "RPM"}
89 }
90 var ret []*signers.Signature
91 seen := make(map[uint64]bool)
92 for _, sig := range sigs {
93 if seen[sig.KeyId] {
94 continue
95 }
96 seen[sig.KeyId] = true
97 rsig := &signers.Signature{
98 Package: nevra(header),
99 CreationTime: sig.CreationTime,
100 Hash: sig.Hash,
101 }
102 if sig.Signer == nil {
103 if !opts.NoChain {
104 return nil, pgptools.ErrNoKey(sig.KeyId)
105 }
106 rsig.Signer = fmt.Sprintf("UNKNOWN(%x)", sig.KeyId)
107 } else {
108 rsig.SignerPgp = sig.Signer
109 }
110 ret = append(ret, rsig)
111 }
112 return ret, nil
113 }
114
115 func nevra(header *rpmutils.RpmHeader) string {
116 nevra, _ := header.GetNEVRA()
117 snevra := nevra.String()
118 // strip .rpm
119 snevra = snevra[:len(snevra)-4]
120 // strip zero epoch
121 snevra = strings.ReplaceAll(snevra, "-0:", "-")
122 return snevra
123 }