forked from sigstore/gitsign
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_verify.go
137 lines (116 loc) · 3.33 KB
/
command_verify.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
//
// Copyright 2022 The Sigstore Authors.
//
// 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 main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"github.com/sigstore/gitsign/internal"
"github.com/sigstore/gitsign/internal/git"
)
func commandVerify() error {
ctx := context.Background()
sNewSig.emit()
var (
data, sig []byte
err error
)
detached := len(fileArgs) >= 2
if detached {
data, sig, err = readDetached()
} else {
sig, err = readAttached()
}
if err != nil {
return fmt.Errorf("failed to read signature data (detached: %T): %w", detached, err)
}
rekor, err := newRekorClient()
if err != nil {
return fmt.Errorf("failed to create rekor client: %w", err)
}
summary, err := git.Verify(ctx, rekor, data, sig, detached)
if err != nil {
if summary != nil && summary.Cert != nil {
emitBadSig(summary.Cert)
} else {
// TODO: We're omitting a bunch of arguments here.
sErrSig.emit()
}
return fmt.Errorf("failed to verify signature: %w", err)
}
fpr := internal.CertHexFingerprint(summary.Cert)
fmt.Fprintln(stderr, "tlog index:", *summary.LogEntry.LogIndex)
fmt.Fprintf(stderr, "gitsign: Signature made using certificate ID 0x%s | %v\n", fpr, summary.Cert.Issuer)
emitGoodSig(summary.Cert)
// TODO: Maybe split up signature checking and certificate checking so we can
// output something more meaningful.
fmt.Fprintf(stderr, "gitsign: Good signature from %v\n", summary.Cert.EmailAddresses)
for _, c := range summary.Claims {
fmt.Fprintf(stderr, "%s: %t\n", string(c.Key), c.Value)
}
emitTrustFully()
return nil
}
func readAttached() ([]byte, error) {
var (
f io.ReadCloser
err error
)
// Read in signature
if len(fileArgs) == 1 {
if f, err = os.Open(fileArgs[0]); err != nil {
return nil, fmt.Errorf("failed to open signature file (%s): %w", fileArgs[0], err)
}
defer f.Close()
} else {
f = stdin
}
sig := new(bytes.Buffer)
if _, err = io.Copy(sig, f); err != nil {
return nil, fmt.Errorf("failed to read signature: %w", err)
}
return sig.Bytes(), nil
}
func readDetached() ([]byte, []byte, error) {
var (
f io.ReadCloser
err error
)
// Read in signature
if f, err = os.Open(fileArgs[0]); err != nil {
return nil, nil, fmt.Errorf("failed to open signature file (%s): %w", fileArgs[0], err)
}
defer f.Close()
sig := new(bytes.Buffer)
if _, err = io.Copy(sig, f); err != nil {
return nil, nil, fmt.Errorf("failed to read signature file: %w", err)
}
// Read in signed data
if fileArgs[1] == "-" {
f = stdin
} else {
if f, err = os.Open(fileArgs[1]); err != nil {
return nil, nil, fmt.Errorf("failed to open message file (%s): %w", fileArgs[1], err)
}
defer f.Close()
}
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, f); err != nil {
return nil, nil, fmt.Errorf("failed to read message file: %w", err)
}
return buf.Bytes(), sig.Bytes(), nil
}