-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkey.go
215 lines (184 loc) · 4.67 KB
/
tkey.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
package tkey
import (
"bytes"
"crypto/sha512"
_ "embed"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/quite/tkeyx25519"
"github.com/tillitis/tkeyclient"
"golang.org/x/crypto/curve25519"
)
const (
ErrWrongDeviceApp = constError("wrong device app")
)
var (
AppHash string
AppFile = "unknown"
)
const (
pluginDomain = "tillitis.se/tkey"
verbose = false
)
var (
le = log.New(os.Stderr, "", 0)
//go:embed x25519-v0.0.2.bin
appBin []byte
//go:embed x25519-hashes.sha512
hashes []byte
)
func init() {
ah := sha512.Sum512(appBin)
AppHash = hex.EncodeToString(ah[:])
lines := strings.Split(string(hashes), "\n")
for _, l := range lines {
ss := strings.Split(l, " ")
if len(ss) != 3 {
continue
}
h, err := hex.DecodeString(ss[0])
if err != nil {
log.Fatal(err)
}
if bytes.Equal(h, ah[:]) {
AppFile = ss[2]
}
}
}
func GetPubKey(userSecret []byte, requireTouch bool) (*tkeyclient.UDI, []byte, error) {
if l := len(userSecret); l != tkeyx25519.UserSecretSize {
return nil, nil, fmt.Errorf("userSecret is %d bytes, expected %d", l, tkeyx25519.UserSecretSize)
}
t := tkey{}
udi, err := t.connect(verbose)
if err != nil {
return nil, nil, fmt.Errorf("connect failed: %w", err)
}
defer t.disconnect()
pubKey, err := t.x25519.GetPubKey(pluginDomain, [tkeyx25519.UserSecretSize]byte(userSecret),
requireTouch)
if err != nil {
return nil, nil, fmt.Errorf("GetPubKey failed: %w", err)
}
return udi, pubKey, nil
}
func DoECDH(userSecret []byte, requireTouch bool, theirPubKey []byte) ([]byte, error) {
if l := len(userSecret); l != tkeyx25519.UserSecretSize {
return nil, fmt.Errorf("userSecret is %d bytes, expected %d", l, tkeyx25519.UserSecretSize)
}
if l := len(theirPubKey); l != curve25519.PointSize {
return nil, fmt.Errorf("theirPubKey is %d bytes, expected %d", l, curve25519.PointSize)
}
t := tkey{}
if _, err := t.connect(verbose); err != nil {
return nil, fmt.Errorf("connect failed: %w", err)
}
defer t.disconnect()
shared, err := t.x25519.DoECDH(pluginDomain, [tkeyx25519.UserSecretSize]byte(userSecret),
requireTouch, [curve25519.PointSize]byte(theirPubKey))
if err != nil {
return nil, fmt.Errorf("DoECDH failed: %w", err)
}
return shared, nil
}
type tkey struct {
x25519 tkeyx25519.X25519
}
const (
wantFWName0 = "tk1 "
wantFWName1 = "mkdf"
wantAppName0 = "x255"
wantAppName1 = "19 "
)
func (t *tkey) connect(verbose bool) (*tkeyclient.UDI, error) {
tkeyclient.SilenceLogging()
devPath := os.Getenv("AGE_TKEY_PORT")
if devPath == "" {
var err error
devPath, err = tkeyclient.DetectSerialPort(verbose)
if err != nil {
return nil, fmt.Errorf("DetectSerialPort failed: %w", err)
}
}
tk := tkeyclient.New()
if verbose {
le.Printf("Connecting to device on serial port %s...\n", devPath)
}
if err := tk.Connect(devPath); err != nil {
return nil, fmt.Errorf("Connect %s failed: %w", devPath, err)
}
t.x25519 = tkeyx25519.New(tk)
// TODO handleSignals(func() { exit(1) }, os.Interrupt, syscall.SIGTERM)
var udi *tkeyclient.UDI
if isFirmwareMode(tk) {
if verbose {
le.Printf("Device is in firmware mode. Loading app...\n")
}
var err error
udi, err = tk.GetUDI()
if err != nil {
return nil, fmt.Errorf("GetUDI failed: %w", err)
}
if err := tk.LoadApp(appBin, []byte{}); err != nil {
t.disconnect()
return nil, fmt.Errorf("LoadApp failed: %w", err)
}
}
if !isWantedApp(t.x25519) {
if verbose {
le.Printf("The TKey may already be running an app, but not the expected x25519-app.\n" +
"Please unplug and plug it in again.\n")
}
t.disconnect()
return nil, ErrWrongDeviceApp
}
return udi, nil
}
func (t *tkey) disconnect() {
if err := t.x25519.Close(); err != nil {
le.Printf("%s\n", err)
}
}
// func handleSignals(action func(), sig ...os.Signal) {
// ch := make(chan os.Signal, 1)
// signal.Notify(ch, sig...)
// go func() {
// for {
// <-ch
// action()
// }
// }()
// }
func isFirmwareMode(tk *tkeyclient.TillitisKey) bool {
nameVer, err := tk.GetNameVersion()
if err != nil {
if !errors.Is(err, io.EOF) && !errors.Is(err, tkeyclient.ErrResponseStatusNotOK) {
le.Printf("GetNameVersion failed: %s\n", err)
}
return false
}
// not caring about nameVer.Version
return nameVer.Name0 == wantFWName0 &&
nameVer.Name1 == wantFWName1
}
func isWantedApp(x25519 tkeyx25519.X25519) bool {
nameVer, err := x25519.GetAppNameVersion()
if err != nil {
if !errors.Is(err, io.EOF) {
le.Printf("GetAppNameVersion: %s\n", err)
}
return false
}
// not caring about nameVer.Version
return nameVer.Name0 == wantAppName0 &&
nameVer.Name1 == wantAppName1
}
type constError string
func (err constError) Error() string {
return string(err)
}