This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
356 lines (307 loc) · 9.47 KB
/
main.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright © 2018 Heptio
// 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 implements a PGP-compatible signing interface backed by Google KMS.
package main
import (
"context"
"crypto"
_ "crypto/sha256"
"fmt"
"io"
"os"
"golang.org/x/crypto/openpgp/clearsign"
"golang.org/x/crypto/openpgp/s2k"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"golang.org/x/oauth2/google"
cloudkms "google.golang.org/api/cloudkms/v1"
"github.com/heptiolabs/google-kms-pgp/kmssigner"
)
var (
cfg = packet.Config{}
)
type options struct {
export bool
armor bool
localUser string
defaultKey string
key string
detachedSignature bool
sign bool
clearSign bool
input string
output string
name string
comment string
email string
}
func main() {
var options options
// common options
pflag.BoolVarP(&options.armor, "armor", "a", options.armor, "output in ascii armor")
pflag.StringVarP(&options.output, "output", "o", options.output, "write output to file (use - for stdout)")
// export options
pflag.BoolVar(&options.export, "export", options.export, "export public key")
pflag.StringVar(&options.name, "name", options.name, "name associated with the key")
pflag.StringVar(&options.comment, "comment", options.comment, "comment associated with the key")
pflag.StringVar(&options.email, "email", options.email, "email associated with the key")
// sign options
pflag.BoolVarP(&options.sign, "sign", "s", options.sign, "sign a message")
pflag.BoolVar(&options.clearSign, "clearsign", options.sign, "sign a message in clear text")
pflag.StringVarP(&options.localUser, "local-user", "u", options.localUser, "name of key to sign with")
pflag.StringVar(&options.defaultKey, "default-key", options.defaultKey, "name of key to sign with")
pflag.BoolVarP(&options.detachedSignature, "detach-sign", "b", options.detachedSignature, "make a detached signature")
pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
pflag.Parse()
// local-user and default-key are mutually exclusive, for our purposes
if options.localUser != "" && options.defaultKey != "" {
fmt.Fprintln(os.Stderr, "you may set either local-user or default-key, but not both")
os.Exit(1)
}
if options.localUser != "" {
options.key = options.localUser
}
if options.defaultKey != "" {
options.key = options.defaultKey
}
var err error
switch {
case options.export:
args := pflag.Args()
if len(args) != 1 {
usage("--export --name NAME [--comment COMMENT] --email EMAIL [--armor] [--output OUTPUT] KEY")
}
options.key = args[0]
err = runExport(options)
case options.sign, options.clearSign, options.detachedSignature:
if options.key == "" {
usage("--sign|--clearsign --local-user KEY [--detach-sign] [--armor] [--output OUTPUT] [INPUT]")
}
args := pflag.Args()
if len(args) == 1 {
options.input = args[0]
}
err = runSign(options)
default:
usage("--export|--sign|--clearsign")
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func usage(msg string) {
fmt.Fprintf(os.Stderr, "usage: %s %s\n", os.Args[0], msg)
pflag.CommandLine.PrintDefaults()
os.Exit(1)
}
func runExport(options options) error {
if options.key == "" {
return errors.New("key is required")
}
if options.name == "" {
return errors.New("name is required")
}
if options.email == "" {
return errors.New("email is required")
}
entity, err := getEntity(options.key)
if err != nil {
return err
}
uid := packet.NewUserId(options.name, options.comment, options.email)
if uid == nil {
return errors.Errorf("could not generate PGP user ID metadata")
}
isPrimary := true
entity.Identities[uid.Id] = &openpgp.Identity{
Name: uid.Id,
UserId: uid,
SelfSignature: &packet.Signature{
CreationTime: entity.PrimaryKey.CreationTime,
SigType: packet.SigTypePositiveCert,
PubKeyAlgo: packet.PubKeyAlgoRSA,
Hash: cfg.Hash(),
IsPrimaryId: &isPrimary,
FlagsValid: true,
FlagSign: true,
FlagCertify: true,
IssuerKeyId: &entity.PrimaryKey.KeyId,
},
}
if err := entity.Identities[uid.Id].SelfSignature.SignUserId(uid.Id, entity.PrimaryKey, entity.PrivateKey, &cfg); err != nil {
return errors.WithMessage(err, "could not self-sign public key")
}
var out io.Writer
switch options.output {
case "", "-":
out = os.Stdout
default:
outFile, err := os.Create(options.output)
if err != nil {
return errors.Wrapf(err, "unable to create output file %q", options.output)
}
defer outFile.Close()
out = outFile
}
if options.armor {
armoredWriter, err := armor.Encode(out, "PGP PUBLIC KEY BLOCK", nil)
if err != nil {
return errors.Wrap(err, "could not create ASCII-armored writer")
}
if err := entity.Serialize(armoredWriter); err != nil {
return errors.Wrap(err, "could not serialize public key")
}
if err := armoredWriter.Close(); err != nil {
return errors.Wrap(err, "error closing ASCII-armored writer")
}
// Always add a blank line to the end of the raw output
fmt.Fprintf(out, "\n")
} else {
if err := entity.Serialize(out); err != nil {
return errors.Wrap(err, "could not serialize public key")
}
}
return nil
}
func runSign(options options) error {
if options.key == "" {
return errors.New("key is required")
}
entity, err := getEntity(options.key)
if err != nil {
return err
}
if options.output == "" {
if options.detachedSignature || options.clearSign {
options.output = options.input + ".asc"
} else {
options.output = options.input + ".gpg"
}
}
var output io.Writer = os.Stdout
var input io.Reader = os.Stdin
if options.output != "-" {
outputFile, err := os.Create(options.output)
if err != nil {
return err
}
defer outputFile.Close()
output = outputFile
}
if options.input != "" {
inputFile, err := os.Open(options.input)
if err != nil {
return err
}
defer inputFile.Close()
input = inputFile
}
switch {
case options.detachedSignature && options.armor:
if err := openpgp.ArmoredDetachSign(output, entity, input, &cfg); err != nil {
return err
}
fmt.Fprintf(output, "\n")
return nil
case options.detachedSignature && !options.armor:
return openpgp.DetachSign(output, entity, input, &cfg)
case !options.detachedSignature:
// Set up an "identity" so we can control the hash algorithm
hashIdSha256, ok := s2k.HashToHashId(crypto.SHA256)
if !ok {
return errors.New("unable to get pgp hash id for sha256")
}
entity.Identities["HACK"] = &openpgp.Identity{
SelfSignature: &packet.Signature{
PreferredHash: []uint8{
hashIdSha256,
},
},
}
// If we're doing a real file, set up hints for it
fileHints := &openpgp.FileHints{}
if options.input != "" {
fileHints.FileName = options.input
fileInfo, err := os.Stat(options.input)
if err != nil {
return err
}
fileHints.ModTime = fileInfo.ModTime()
}
// Get ready to sign
var (
writeCloser io.WriteCloser
addNewline bool
)
if options.clearSign {
addNewline = true
writeCloser, err = clearsign.Encode(output, entity.PrivateKey, &cfg)
} else {
writeCloser, err = openpgp.Sign(output, entity, fileHints, &cfg)
}
if err != nil {
return err
}
// Copy the data from input to writeCloser so it will perform signing
_, copyErr := io.Copy(writeCloser, input)
// Always try to close
closeErr := writeCloser.Close()
if copyErr != nil {
return copyErr
}
if closeErr != nil {
return closeErr
}
if addNewline {
fmt.Fprintf(output, "\n")
}
return nil
}
return nil
}
func getEntity(key string) (*openpgp.Entity, error) {
if key == "" {
return nil, errors.New("key is required")
}
// Connect to the Google Cloud KMS API
ctx := context.Background()
oauthClient, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope)
if err != nil {
return nil, errors.Wrap(err, "could not create Google Cloud OAuth client")
}
svc, err := cloudkms.New(oauthClient)
if err != nil {
return nil, errors.Wrap(err, "could not create Google Cloud KMS client")
}
// Initialize a crypto.Signer backed by the configured Cloud KMS key.
signer, err := kmssigner.New(svc, key)
if err != nil {
return nil, errors.Wrap(err, "could not get KMS signer")
}
// Create an openpgp Entity, which holds the PGP-related information about our key.
entity := &openpgp.Entity{
PrimaryKey: packet.NewRSAPublicKey(signer.CreationTime(), signer.RSAPublicKey()),
PrivateKey: packet.NewSignerPrivateKey(cfg.Now(), signer),
Identities: make(map[string]*openpgp.Identity),
}
// The PubKeyAlgo defaults to packet.PubKeyAlgoRSASignOnly, and that doesn't work for RPM
entity.PrivateKey.PubKeyAlgo = packet.PubKeyAlgoRSA
// TODO: this may be a bug in the openpgp library? Without this, my signatures
// end up with a key ID that doesn't match the primary key.
entity.PrivateKey.KeyId = entity.PrimaryKey.KeyId
return entity, nil
}