-
Notifications
You must be signed in to change notification settings - Fork 44
/
signatures.go
320 lines (298 loc) · 8.21 KB
/
signatures.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
/*
* Copyright (c) SAS Institute Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpmutils
import (
"bytes"
"crypto"
"hash"
"io"
"os"
"path"
"time"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/sassoftware/go-rpmutils/fileutil"
)
// SignatureOptions describes additional configuration for SignRpm methods
type SignatureOptions struct {
// Hash algorithm for the signature. If not set, defaults to SHA-256
Hash crypto.Hash
// CreationTime for the signature. If not set, defaults to the current time
CreationTime time.Time
}
func (opts *SignatureOptions) hash() crypto.Hash {
if opts != nil {
return opts.Hash
}
return crypto.SHA256
}
func (opts *SignatureOptions) creationTime() time.Time {
if opts != nil {
return opts.CreationTime
}
return time.Now()
}
func makeSignature(h hash.Hash, key *packet.PrivateKey, opts *SignatureOptions) ([]byte, error) {
hashType := opts.hash()
sig := &packet.Signature{
SigType: packet.SigTypeBinary,
CreationTime: opts.creationTime(),
PubKeyAlgo: key.PublicKey.PubKeyAlgo,
Hash: hashType,
IssuerKeyId: &key.KeyId,
}
err := sig.Sign(h, key, nil)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = sig.Serialize(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func insertSignature(sigHeader *rpmHeader, tag int, value []byte) {
sigHeader.entries[tag] = entry{
dataType: RPM_BIN_TYPE,
count: int32(len(value)),
contents: value,
}
}
func insertSignatures(sigHeader *rpmHeader, sigPgp, sigRsa []byte) {
insertSignature(sigHeader, SIG_PGP-_SIGHEADER_TAG_BASE, sigPgp)
insertSignature(sigHeader, SIG_RSA, sigRsa)
delete(sigHeader.entries, SIG_GPG-_SIGHEADER_TAG_BASE)
delete(sigHeader.entries, SIG_DSA)
}
func getSha1(sigHeader *rpmHeader) string {
vals, err := sigHeader.GetStrings(SIG_SHA1)
if err != nil {
return ""
}
return vals[0]
}
func getSha256(sigHeader *rpmHeader) string {
vals, err := sigHeader.GetStrings(SIG_SHA256)
if err != nil {
return ""
}
return vals[0]
}
func getHashAndType(sigHeader *rpmHeader) (string, crypto.Hash) {
// RPM v4 introduced SHA256 header signatures, prefer them over the
// previous SHA1
if h := getSha256(sigHeader); h != "" {
return h, crypto.SHA256
}
return getSha1(sigHeader), crypto.SHA1
}
func digestForSigning(sigHeader, genHeader *rpmHeader, payloadReader io.Reader, opts *SignatureOptions) (genHash, combinedHash hash.Hash, err error) {
genHash, combinedHash = opts.hash().New(), opts.hash().New()
// write header
genHash.Write(genHeader.orig)
combinedHash.Write(genHeader.orig)
// write and verify payload
err = digestPayload(sigHeader, genHeader, payloadReader, []io.Writer{combinedHash})
return genHash, combinedHash, err
}
// SignRpmStream reads an RPM and signs it, returning the set of headers updated with the new signature.
func SignRpmStream(stream io.Reader, key *packet.PrivateKey, opts *SignatureOptions) (header *RpmHeader, err error) {
lead, sigHeader, err := readSignatureHeader(stream)
if err != nil {
return nil, err
}
// parse the general header
headerDigestValue, headerDigestType := getHashAndType(sigHeader)
genHeader, err := readHeader(stream, headerDigestValue, headerDigestType, sigHeader.isSource, false)
if err != nil {
return nil, err
}
// hash and sign header
genHash, combinedHash, err := digestForSigning(sigHeader, genHeader, stream, opts)
if err != nil {
return nil, err
}
// sign header and payload
sigPgp, err := makeSignature(combinedHash, key, opts)
if err != nil {
return
}
sigRsa, err := makeSignature(genHash, key, opts)
if err != nil {
return nil, err
}
insertSignatures(sigHeader, sigPgp, sigRsa)
return &RpmHeader{
lead: lead,
sigHeader: sigHeader,
genHeader: genHeader,
isSource: sigHeader.isSource,
}, nil
}
func getPayloadDigest(header *rpmHeader) (string, crypto.Hash) {
digests, err := header.GetStrings(PAYLOADDIGEST)
if err != nil || len(digests) == 0 {
// no payload digest
return "", 0
}
digest := digests[0]
algos, err := header.GetUint32s(PAYLOADDIGESTALGO)
if err != nil || len(algos) == 0 {
return "", 0
}
switch algos[0] {
case HASH_MD5:
return digest, crypto.MD5
case HASH_SHA1:
return digest, crypto.SHA1
case HASH_SHA256:
return digest, crypto.SHA256
case HASH_SHA384:
return digest, crypto.SHA384
case HASH_SHA512:
return digest, crypto.SHA512
case HASH_SHA224:
return digest, crypto.SHA224
}
return "", 0
}
func canOverwrite(ininfo, outinfo os.FileInfo) bool {
if !outinfo.Mode().IsRegular() {
return false
}
if !os.SameFile(ininfo, outinfo) {
return false
}
if fileutil.HasLinks(outinfo) {
return false
}
return true
}
// SignRpmFile signs infile and writes it to outpath, which may be the same file
func SignRpmFile(infile *os.File, outpath string, key *packet.PrivateKey, opts *SignatureOptions) (header *RpmHeader, err error) {
header, err = SignRpmStream(infile, key, opts)
if err != nil {
return
}
return header, rewriteRpm(infile, outpath, header)
}
// RewriteWithSignatures inserts raw signatures into a RPM header.
//
// DEPRECATED: To perform a detached signature, use SignRpmStream and call
// DumpSignatureHeader to export the result.
func RewriteWithSignatures(infile *os.File, outpath string, sigPgp, sigRsa []byte) (*RpmHeader, error) {
header, err := ReadHeader(infile)
if err != nil {
return nil, err
}
insertSignatures(header.sigHeader, sigPgp, sigRsa)
err = rewriteRpm(infile, outpath, header)
if err != nil {
return nil, err
}
return header, nil
}
func rewriteRpm(infile *os.File, outpath string, header *RpmHeader) error {
delete(header.sigHeader.entries, SIG_RESERVEDSPACE-_SIGHEADER_TAG_BASE)
ininfo, err := infile.Stat()
if err != nil {
return err
}
var outstream io.Writer
if outpath == "-" {
outstream = os.Stdout
} else {
outinfo, err := os.Lstat(outpath)
if err == nil && canOverwrite(ininfo, outinfo) {
ok, err := writeInPlace(outpath, header)
if err != nil || ok {
return err
}
// in-place didn't work; fallback to rewrite
} else if err == nil && !outinfo.Mode().IsRegular() {
// pipe or something else. open for writing.
outfile, err := os.Create(outpath)
if err != nil {
return err
}
defer outfile.Close()
outstream = outfile
}
if outstream == nil {
// write-rename
tempfile, err := os.CreateTemp(path.Dir(outpath), path.Base(outpath))
if err != nil {
return err
}
defer func() {
if err != nil {
os.Remove(tempfile.Name())
} else {
_ = tempfile.Chmod(0644)
if err = tempfile.Close(); err != nil {
return
}
err = os.Rename(tempfile.Name(), outpath)
}
}()
outstream = tempfile
}
}
return writeRpm(infile, outstream, header.sigHeader)
}
func writeInPlace(path string, header *RpmHeader) (ok bool, err error) {
blob, err := header.DumpSignatureHeader(true)
if err != nil {
return false, err
}
orig := header.OriginalSignatureHeaderSize()
if orig != len(blob) {
// size changed; can't rewrite in place
return false, nil
}
outfile, err := os.OpenFile(path, os.O_RDWR, 0)
if err != nil {
return
}
defer outfile.Close()
n, err := outfile.Write(blob)
if err != nil {
return false, err
} else if n != len(blob) {
return false, io.ErrShortWrite
}
return true, nil
}
func writeRpm(infile io.ReadSeeker, outstream io.Writer, sigHeader *rpmHeader) error {
if _, err := infile.Seek(0, 0); err != nil {
return err
}
lead, err := readExact(infile, 96)
if err != nil {
return err
}
if _, err = outstream.Write(lead); err != nil {
return err
}
if err = sigHeader.WriteTo(outstream, RPMTAG_HEADERSIGNATURES); err != nil {
return err
}
if _, err := infile.Seek(int64(len(lead)+len(sigHeader.orig)), 0); err != nil {
return err
}
_, err = io.Copy(outstream, infile)
return err
}