forked from sigstore/gitsign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
153 lines (134 loc) · 3.89 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// 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 root
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"github.com/sigstore/gitsign/internal/commands/verify"
"github.com/sigstore/gitsign/internal/gitsign"
"github.com/sigstore/gitsign/internal/gpg"
gsio "github.com/sigstore/gitsign/internal/io"
)
// commandSign implements gitsign commit verification.
// This is implemented as a root command so that user can specify the
// gitsign binary directly in their gitconfigs.
func commandVerify(o *options, s *gsio.Streams, args ...string) error {
ctx := context.Background()
// Flag validation
if o.FlagSign {
return errors.New("specify --help, --sign, or --verify")
}
if len(o.FlagLocalUser) > 0 {
return errors.New("local-user cannot be specified for verification")
}
if o.FlagDetachedSignature {
return errors.New("detach-sign cannot be specified for verification")
}
if o.FlagArmor {
return errors.New("armor cannot be specified for verification")
}
gpgout := gpg.NewStatusWriterFromFD(uintptr(o.FlagStatusFD))
gpgout.Emit(gpg.StatusNewSig)
var (
data, sig []byte
err error
)
detached := len(args) >= 2
if detached {
data, sig, err = readDetached(s, args...)
} else {
sig, err = readAttached(s, args...)
}
if err != nil {
return fmt.Errorf("failed to read signature data (detached: %T): %w", detached, err)
}
v, err := gitsign.NewVerifierWithCosignOpts(ctx, o.Config, nil)
if err != nil {
return err
}
summary, err := v.Verify(ctx, data, sig, true)
if err != nil {
return err
}
if err != nil {
if summary != nil && summary.Cert != nil {
gpgout.EmitBadSig(summary.Cert)
} else {
// TODO: We're omitting a bunch of arguments here.
gpgout.Emit(gpg.StatusErrSig)
}
return fmt.Errorf("failed to verify signature: %w", err)
}
verify.PrintSummary(s.Err, summary)
fmt.Fprintln(s.Err, "WARNING: git verify-commit does not verify cert claims. Prefer using `gitsign verify` instead.")
gpgout.EmitGoodSig(summary.Cert)
gpgout.EmitTrustFully()
return nil
}
func readAttached(s *gsio.Streams, args ...string) ([]byte, error) {
var (
f io.Reader
err error
)
// Read in signature
if len(args) == 1 {
f2, err := os.Open(args[0])
if err != nil {
return nil, fmt.Errorf("failed to open signature file (%s): %w", args[0], err)
}
defer f2.Close()
f = f2
} else {
f = s.In
}
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(s *gsio.Streams, args ...string) ([]byte, []byte, error) {
// Read in signature
sigFile, err := os.Open(args[0])
if err != nil {
return nil, nil, fmt.Errorf("failed to open signature file (%s): %w", args[0], err)
}
defer sigFile.Close()
sig := new(bytes.Buffer)
if _, err = io.Copy(sig, sigFile); err != nil {
return nil, nil, fmt.Errorf("failed to read signature file: %w", err)
}
var dataFile io.Reader
// Read in signed data
if args[1] == "-" {
dataFile = s.In
} else {
f2, err := os.Open(args[1])
if err != nil {
return nil, nil, fmt.Errorf("failed to open message file (%s): %w", args[1], err)
}
defer f2.Close()
dataFile = f2
}
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, dataFile); err != nil {
return nil, nil, fmt.Errorf("failed to read message file: %w", err)
}
return buf.Bytes(), sig.Bytes(), nil
}