forked from tcz001/pinentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinentry.go
187 lines (165 loc) · 3.88 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package pinentry
import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"runtime"
)
var (
PinentryUtilityName = "pinentry"
)
type PinentryClient interface {
SetDesc(desc string)
SetPrompt(prompt string)
SetTitle(title string)
SetOK(ok string)
SetCancel(cancel string)
SetError(errorMsg string)
SetQualityBar()
SetQualityBarTT(tt string)
GetPin() (pin []byte, err error)
Confirm() bool
Close()
}
type pinentryClient struct {
in io.WriteCloser
pipe *bufio.Reader
}
// set descriptive text to display
func (c *pinentryClient) SetDesc(desc string) {
c.in.Write([]byte("SETDESC " + desc + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
// set desciption for user
func (c *pinentryClient) SetPrompt(prompt string) {
c.in.Write([]byte("SETPROMPT " + prompt + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) SetTitle(title string) {
c.in.Write([]byte("SETTITLE " + title + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) SetOK(okLabel string) {
c.in.Write([]byte("SETOK " + okLabel + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) SetCancel(cancelLabel string) {
c.in.Write([]byte("SETCANCEL " + cancelLabel + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) SetError(errorMsg string) {
c.in.Write([]byte("SETERROR " + errorMsg + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) SetQualityBar() {
c.in.Write([]byte("SETQUALITYBAR\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) SetQualityBarTT(tt string) {
c.in.Write([]byte("SETQUALITYBAR_TT" + tt + "\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
}
func (c *pinentryClient) Confirm() bool {
confirmed := false
c.in.Write([]byte("CONFIRM\n"))
// ok
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) == 0 {
confirmed = true
}
return confirmed
}
func (c *pinentryClient) GetPin() (pin []byte, err error) {
c.in.Write([]byte("GETPIN\n"))
// D pin
d_pin, _, err := c.pipe.ReadLine()
if bytes.Compare(d_pin[:2], []byte("D ")) == 0 {
ok, _, _ := c.pipe.ReadLine()
if bytes.Compare(ok, []byte("OK")) != 0 {
panic(string(ok))
}
return d_pin[2:], nil
} else if bytes.Compare(d_pin[:2], []byte("OK")) == 0 {
return nil, nil
}
return nil, fmt.Errorf("unexpected response for GetPin: %s", d_pin)
}
func (c *pinentryClient) Close() {
c.in.Close()
return
}
func startProcess(cmdName string) (io.WriteCloser, *bufio.Reader, error) {
if runtime.GOOS == "windows" {
cmdName += ".exe"
}
cmd := exec.Command(cmdName, "-T", "/dev/tty")
in, err := cmd.StdinPipe()
if err != nil {
return nil, nil, err
}
out, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
bufout := bufio.NewReader(out)
err = cmd.Start()
if err != nil {
return nil, nil, err
}
// welcome
welcome, _, err := bufout.ReadLine()
if err != nil {
return nil, nil, err
}
if bytes.Compare(welcome[:2], []byte("OK")) != 0 {
return nil, nil, fmt.Errorf("Invalid welcome message: %v", string(welcome))
}
return in, bufout, nil
}
func NewPinentryClient() (PinentryClient, error) {
in, bufout, err := startProcess(PinentryUtilityName)
if err != nil {
return nil, err
}
pinentry := &pinentryClient{in, bufout}
//Setup default layout
pinentry.SetTitle("pinentry")
pinentry.SetDesc("")
pinentry.SetPrompt("Enter the passphrase, please:")
pinentry.SetOK("Ok")
return pinentry, nil
}