forked from keybase/client
/
pgp_sign.go
147 lines (126 loc) · 3.31 KB
/
pgp_sign.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"fmt"
"io"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-crypto/openpgp"
"github.com/keybase/go-crypto/openpgp/clearsign"
)
type PGPSignEngine struct {
arg *PGPSignArg
libkb.Contextified
warnings libkb.HashSecurityWarnings
}
type PGPSignArg struct {
Sink io.WriteCloser
Source io.ReadCloser
Opts keybase1.PGPSignOptions
}
func (p *PGPSignEngine) Prereqs() Prereqs {
return Prereqs{
Device: true,
}
}
func (p *PGPSignEngine) Name() string {
return "PGPSignEngine"
}
func (p *PGPSignEngine) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{
libkb.SecretUIKind,
libkb.PgpUIKind,
}
}
func (p *PGPSignEngine) SubConsumers() []libkb.UIConsumer {
return nil
}
func NewPGPSignEngine(g *libkb.GlobalContext, arg *PGPSignArg) *PGPSignEngine {
return &PGPSignEngine{
arg: arg,
Contextified: libkb.NewContextified(g),
}
}
func (p *PGPSignEngine) Run(m libkb.MetaContext) (err error) {
var key libkb.GenericKey
var pgp *libkb.PGPKeyBundle
var ok bool
var dumpTo io.WriteCloser
var written int64
defer func() {
if dumpTo != nil {
if e := dumpTo.Close(); e != nil {
p.G().Log.Warning("error closing dumpTo: %s", e)
}
}
if e := p.arg.Sink.Close(); e != nil {
p.G().Log.Warning("error closing Sink: %s", e)
}
if e := p.arg.Source.Close(); e != nil {
p.G().Log.Warning("error closing Source: %s", e)
}
}()
me, err := libkb.LoadMe(libkb.NewLoadUserArg(p.G()))
if err != nil {
return err
}
ska := libkb.SecretKeyArg{
Me: me,
KeyType: libkb.PGPKeyType,
KeyQuery: p.arg.Opts.KeyQuery,
}
key, err = p.G().Keyrings.GetSecretKeyWithPrompt(m, m.SecretKeyPromptArg(ska, "command-line signature"))
if err != nil {
return
} else if pgp, ok = key.(*libkb.PGPKeyBundle); !ok {
err = fmt.Errorf("Can only sign with PGP keys")
return
}
p.warnings = libkb.HashSecurityWarnings{}
if w := pgp.SecurityWarnings(
libkb.HashSecurityWarningOurIdentityHash,
); len(w) > 0 {
p.warnings = append(p.warnings, w...)
}
for _, warning := range p.warnings.Strings() {
if err := m.UIs().PgpUI.OutputPGPWarning(m.Ctx(), keybase1.OutputPGPWarningArg{
Warning: warning,
}); err != nil {
return err
}
}
bo := p.arg.Opts.BinaryOut
bi := p.arg.Opts.BinaryIn
pgpe := pgp.Entity
mode := p.arg.Opts.Mode
switch mode {
case keybase1.SignMode_ATTACHED:
dumpTo, err = libkb.AttachedSignWrapper(p.arg.Sink, *pgp, !bo)
case keybase1.SignMode_DETACHED:
switch {
case bi && bo:
err = openpgp.DetachSign(p.arg.Sink, pgpe, p.arg.Source, nil)
case bi && !bo:
err = openpgp.ArmoredDetachSign(p.arg.Sink, pgpe, p.arg.Source, nil)
case !bi && bo:
err = openpgp.DetachSignText(p.arg.Sink, pgpe, p.arg.Source, nil)
default:
err = openpgp.ArmoredDetachSignText(p.arg.Sink, pgpe, p.arg.Source, nil)
}
case keybase1.SignMode_CLEAR:
dumpTo, err = clearsign.Encode(p.arg.Sink, pgp.PrivateKey, nil)
default:
err = fmt.Errorf("unrecognized sign mode: %d", int(mode))
}
if err != nil {
return
}
if dumpTo != nil {
written, err = io.Copy(dumpTo, p.arg.Source)
if err == nil && written == 0 {
p.G().Log.Debug("Empty source file.")
}
}
return
}