forked from skeeto/passphrase2pgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinentry.go
168 lines (157 loc) · 3.54 KB
/
pinentry.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
)
var (
// errPinentryProtocol means pinentry responded incorrectly.
errPinentryProtocol = errors.New("pinentry protocol error")
// errPinentryCancel means the user canceled the input.
errPinentryCancel = errors.New("pinentry input canceled")
// errPinentryMismatch means the confirmation passphrase did not match.
errPinentryMismatch = errors.New("passphrases do not match")
)
// pinentry represents a running, interactive pinentry process used for
// prompting users for passwords.
type pinentry struct {
cmd *exec.Cmd
in io.WriteCloser
out *bufio.Scanner
err error
}
// Return a handle for a newly initialized pinentry process.
func runPinentry(command string) *pinentry {
pe := new(pinentry)
cmd := exec.Command(command)
in, err := cmd.StdinPipe()
if err != nil {
pe.err = err
return pe
}
pe.in = in
out, err := cmd.StdoutPipe()
if err != nil {
pe.err = err
return pe
}
pe.out = bufio.NewScanner(out)
if err := cmd.Start(); err != nil {
pe.err = err
return pe
}
pe.wait()
return pe
}
// Wait for the next "OK" from pinentry, returning any "D" data also sent.
func (p *pinentry) wait() []byte {
if p.err != nil {
return nil
}
var data []byte
for p.out.Scan() {
line := p.out.Text()
if strings.HasPrefix(line, "ERR ") {
errstr := line[4:]
if strings.HasPrefix(errstr, "83886179 ") {
p.err = errPinentryCancel
return data
}
p.err = errors.New(errstr)
return nil
} else if strings.HasPrefix(line, "OK") {
return data
} else if strings.HasPrefix(line, "D ") {
if data != nil {
p.err = errPinentryProtocol
return nil
}
var ok bool
data, ok = pinentryDecode(p.out.Text()[2:])
if !ok {
p.err = errPinentryProtocol
return nil
}
} else {
p.err = errPinentryProtocol
return data
}
}
if err := p.out.Err(); err != nil {
p.err = err
} else {
p.err = errPinentryProtocol
}
return data
}
// Send message to pinentry and wait for its response with possible data.
// Note: strings must be passed encoded.
func (p *pinentry) Send(args ...string) []byte {
if p.err != nil {
return nil
}
var buf bytes.Buffer
for i, arg := range args {
if i > 0 {
buf.WriteByte(' ')
}
buf.WriteString(arg)
}
buf.WriteByte('\n')
if _, err := p.in.Write(buf.Bytes()); err != nil {
p.err = err
return nil
}
return p.wait()
}
// Close the communication channel with pinentry so that it exits.
func (p *pinentry) Close() {
p.in.Close()
}
// Decode a pinentry data string.
func pinentryDecode(s string) (decoded []byte, valid bool) {
buf := make([]byte, 0, len(s))
for i := 0; i < len(s); {
b := s[i]
if b == '%' {
if len(s[i:]) < 3 {
return nil, false
}
v, err := strconv.ParseUint(s[i+1:i+3], 16, 8)
if err != nil {
return nil, false
}
buf = append(buf, byte(v))
i += 3
} else {
buf = append(buf, b)
i++
}
}
return buf, true
}
// Read, confirm, and return a passphrase from the user via pinentry.
func pinentryPassphrase(command, hint string, repeat int) ([]byte, error) {
pe := runPinentry(command)
defer pe.Close()
if hint == "" {
pe.Send("SETDESC", "passphrase2pgp")
pe.Send("SETTITLE", "passphrase2pgp")
} else {
pe.Send("SETDESC", fmt.Sprintf("passphrase2pgp [%s]", hint))
pe.Send("SETTITLE", fmt.Sprintf("passphrase2pgp [%s]", hint))
}
passphrase := pe.Send("GETPIN")
for i := 0; i < repeat; i++ {
again := pe.Send("GETPIN")
if !bytes.Equal(passphrase, again) {
return nil, errPinentryMismatch
}
}
return passphrase, pe.err
}