summaryrefslogtreecommitdiff
path: root/daemon/kmd/wallet/driver/ledger.go
blob: fbe5367ba36d5879ca8982380dc89815566c20c1 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package driver

import (
	"bytes"
	"crypto/sha512"
	"encoding/binary"
	"errors"
	"fmt"
	"os"
	"sort"
	"strings"

	"github.com/algorand/go-deadlock"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/daemon/kmd/config"
	"github.com/algorand/go-algorand/daemon/kmd/wallet"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
)

const (
	ledgerWalletDriverName    = "ledger"
	ledgerWalletDriverVersion = 1
	ledgerIDLen               = 16

	ledgerClass            = uint8(0x80)
	ledgerInsGetPublicKey  = uint8(0x03)
	ledgerInsSignPaymentV2 = uint8(0x04)
	ledgerInsSignKeyregV2  = uint8(0x05)
	ledgerInsSignMsgpack   = uint8(0x08)
	ledgerP1first          = uint8(0x00)
	ledgerP1more           = uint8(0x80)
	ledgerP2last           = uint8(0x00)
	ledgerP2more           = uint8(0x80)
)

var ledgerWalletSupportedTxs = []protocol.TxType{protocol.PaymentTx, protocol.KeyRegistrationTx}

// LedgerWalletDriver provides access to a hardware wallet on the
// Ledger Nano S device.  The device must run the Algorand wallet
// application from https://github.com/algorand/ledger-app-algorand
type LedgerWalletDriver struct {
	mu      deadlock.Mutex
	wallets map[string]*LedgerWallet
	log     logging.Logger
	cfg     config.LedgerWalletDriverConfig
}

// LedgerWallet represents a particular wallet under the
// LedgerWalletDriver.  The lock prevents concurrent access
// to the USB device.
type LedgerWallet struct {
	mu  deadlock.Mutex
	dev LedgerUSB
}

// CreateWallet implements the Driver interface.  There is
// currently no way to create new wallet keys; there is one
// key in a hardware wallet, derived from the device master
// secret.  We could, in principle, derive multiple keys.
// This is not supported at the moment.
func (lwd *LedgerWalletDriver) CreateWallet(name []byte, id []byte, pw []byte, mdk crypto.MasterDerivationKey) error {
	return errNotSupported
}

// FetchWallet looks up a wallet by ID and returns it, failing if there's more
// than one wallet with the given ID
func (lwd *LedgerWalletDriver) FetchWallet(id []byte) (w wallet.Wallet, err error) {
	lwd.mu.Lock()
	defer lwd.mu.Unlock()

	lw, ok := lwd.wallets[string(id)]
	if !ok {
		return nil, errWalletNotFound
	}

	return lw, nil
}

// scanWalletsLocked enumerates attached ledger devices and stores them.
// lwd.mu must be held
func (lwd *LedgerWalletDriver) scanWalletsLocked() error {

	if os.Getenv("KMD_NOUSB") != "" {
		return nil
	}

	// Initialize wallets map
	if lwd.wallets == nil {
		lwd.wallets = make(map[string]*LedgerWallet)
	}

	if lwd.cfg.Disable {
		return nil
	}

	// Enumerate attached wallet devices
	infos, err := LedgerEnumerate()
	if err != nil {
		return err
	}

	// Make map of existing device paths. We will pop each one that we
	// are able to scan for, meaning anything left over is dead, and we
	// should remove it
	var curPaths map[string]bool
	curPaths = make(map[string]bool)
	for k := range lwd.wallets {
		curPaths[k] = true
	}

	// Try to open each new device, skipping ones that are already open.
	var newDevs []LedgerUSB
	for _, info := range infos {
		walletID := pathToID(info.Path)
		if curPaths[walletID] {
			delete(curPaths, walletID)
			continue
		}

		dev, err := info.Open()
		if err != nil {
			lwd.log.Warnf("enumerated but failed to open ledger %s %x: %v", info.Path, info.ProductID, err)
			continue
		}

		newDevs = append(newDevs, LedgerUSB{
			hiddev: dev,
			info:   info,
		})
	}

	// Anything left in curPaths is no longer scanning. Close and remove
	for deadPath := range curPaths {
		err = lwd.wallets[deadPath].dev.hiddev.Close()
		if err != nil {
			lwd.log.Warnf("failed to close '%s': %v", deadPath, err)
		}
		delete(lwd.wallets, deadPath)
	}

	// Add in new ledger wallets if they appear valid
	for _, dev := range newDevs {
		newWallet := &LedgerWallet{
			dev: dev,
		}

		// Check that device responds to Algorand app requests
		_, err := newWallet.ListKeys()
		if err != nil {
			continue
		}

		id := pathToID(dev.USBInfo().Path)
		lwd.wallets[id] = newWallet
	}

	return nil
}

// InitWithConfig accepts a driver configuration.  Currently, the Ledger
// driver does not have any configuration parameters.  However, we use
// this to enumerate the USB devices.
func (lwd *LedgerWalletDriver) InitWithConfig(cfg config.KMDConfig, log logging.Logger) error {
	lwd.mu.Lock()
	defer lwd.mu.Unlock()

	lwd.log = log
	lwd.cfg = cfg.DriverConfig.LedgerWalletDriverConfig

	return lwd.scanWalletsLocked()
}

// ListWalletMetadatas returns all wallets supported by this driver.
func (lwd *LedgerWalletDriver) ListWalletMetadatas() (metadatas []wallet.Metadata, err error) {
	lwd.mu.Lock()
	defer lwd.mu.Unlock()

	err = lwd.scanWalletsLocked()
	if err != nil {
		return
	}

	for _, w := range lwd.wallets {
		md, err := w.Metadata()
		if err != nil {
			return nil, err
		}

		metadatas = append(metadatas, md)
	}

	metaSort := func(i, j int) bool {
		return bytes.Compare(metadatas[i].ID, metadatas[j].ID) < 0
	}

	// Sort metadatas by ID
	sort.Slice(metadatas, metaSort)

	return metadatas, nil
}

// RenameWallet implements the Driver interface.
func (lwd *LedgerWalletDriver) RenameWallet(newName []byte, id []byte, pw []byte) error {
	return errNotSupported
}

// Init implements the wallet interface.
func (lw *LedgerWallet) Init(pw []byte) error {
	return nil
}

// CheckPassword implements the Wallet interface.
func (lw *LedgerWallet) CheckPassword(pw []byte) error {
	return nil
}

// ExportMasterDerivationKey implements the Wallet interface.
func (lw *LedgerWallet) ExportMasterDerivationKey(pw []byte) (crypto.MasterDerivationKey, error) {
	return crypto.MasterDerivationKey{}, errNotSupported
}

func pathToID(path string) string {
	// The Path USB info field is platform-dependent and sometimes
	// very long. We hash it to make the wallet name/ID less unwieldy
	pathHashFull := sha512.Sum512_256([]byte(path))
	return fmt.Sprintf("%x", pathHashFull[:ledgerIDLen])
}

// Metadata implements the Wallet interface.
func (lw *LedgerWallet) Metadata() (wallet.Metadata, error) {
	lw.mu.Lock()
	defer lw.mu.Unlock()

	info := lw.dev.USBInfo()

	walletID := pathToID(info.Path)
	walletName := fmt.Sprintf("%s-%s-%s-%s", info.Manufacturer, info.Product, info.Serial, walletID)
	walletName = strings.Replace(walletName, " ", "-", -1)

	return wallet.Metadata{
		ID:                    []byte(walletID),
		Name:                  []byte(walletName),
		DriverName:            ledgerWalletDriverName,
		DriverVersion:         ledgerWalletDriverVersion,
		SupportedTransactions: ledgerWalletSupportedTxs,
	}, nil
}

// ListKeys implements the Wallet interface.
func (lw *LedgerWallet) ListKeys() ([]crypto.Digest, error) {
	lw.mu.Lock()
	defer lw.mu.Unlock()

	reply, err := lw.dev.Exchange([]byte{ledgerClass, ledgerInsGetPublicKey, 0x00, 0x00, 0x00})
	if err != nil {
		return nil, err
	}

	var addr crypto.Digest
	copy(addr[:], reply)
	return []crypto.Digest{addr}, nil
}

// ImportKey implements the Wallet interface.
func (lw *LedgerWallet) ImportKey(sk crypto.PrivateKey) (crypto.Digest, error) {
	return crypto.Digest{}, errNotSupported
}

// ExportKey implements the Wallet interface.
func (lw *LedgerWallet) ExportKey(pk crypto.Digest, pw []byte) (crypto.PrivateKey, error) {
	return crypto.PrivateKey{}, errNotSupported
}

// GenerateKey implements the Wallet interface.
func (lw *LedgerWallet) GenerateKey(displayMnemonic bool) (crypto.Digest, error) {
	return crypto.Digest{}, errNotSupported
}

// DeleteKey implements the Wallet interface.
func (lw *LedgerWallet) DeleteKey(pk crypto.Digest, pw []byte) error {
	return errNotSupported
}

// ImportMultisigAddr implements the Wallet interface.
func (lw *LedgerWallet) ImportMultisigAddr(version, threshold uint8, pks []crypto.PublicKey) (crypto.Digest, error) {
	return crypto.Digest{}, errNotSupported
}

// LookupMultisigPreimage implements the Wallet interface.
func (lw *LedgerWallet) LookupMultisigPreimage(crypto.Digest) (version, threshold uint8, pks []crypto.PublicKey, err error) {
	return 0, 0, nil, errNotSupported
}

// ListMultisigAddrs implements the Wallet interface.
func (lw *LedgerWallet) ListMultisigAddrs() (addrs []crypto.Digest, err error) {
	return nil, nil
}

// DeleteMultisigAddr implements the Wallet interface.
func (lw *LedgerWallet) DeleteMultisigAddr(addr crypto.Digest, pw []byte) error {
	return errNotSupported
}

// SignTransaction implements the Wallet interface.
func (lw *LedgerWallet) SignTransaction(tx transactions.Transaction, pk crypto.PublicKey, pw []byte) ([]byte, error) {
	pks, err := lw.ListKeys()
	if err != nil {
		return nil, err
	}
	// Right now the device only supports one key
	if len(pks) > 1 {
		return nil, errors.New("LedgerWallet device only supports one key but ListKeys returned more than one")
	}
	if len(pks) < 1 {
		return nil, errKeyNotFound
	}
	if (pk != crypto.PublicKey{}) && pk != crypto.PublicKey(pks[0]) {
		// A specific key was requested; return an error if it's not the one on the device.
		return nil, errKeyNotFound
	}
	pk = crypto.PublicKey(pks[0])

	sig, err := lw.signTransactionHelper(tx)
	if err != nil {
		return nil, err
	}

	stxn := transactions.SignedTxn{
		Txn: tx,
		Sig: sig,
	}

	// Set the AuthAddr if the key we signed with doesn't match the txn sender
	if basics.Address(pk) != tx.Sender {
		stxn.AuthAddr = basics.Address(pk)
	}

	return protocol.Encode(&stxn), nil
}

// SignProgram implements the Wallet interface.
func (lw *LedgerWallet) SignProgram(data []byte, src crypto.Digest, pw []byte) ([]byte, error) {
	sig, err := lw.signProgramHelper(data)
	if err != nil {
		return nil, err
	}

	return sig[:], nil
}

// MultisigSignTransaction implements the Wallet interface.
func (lw *LedgerWallet) MultisigSignTransaction(tx transactions.Transaction, pk crypto.PublicKey, partial crypto.MultisigSig, pw []byte, signer crypto.Digest) (crypto.MultisigSig, error) {
	isValidKey := false
	for i := 0; i < len(partial.Subsigs); i++ {
		subsig := &partial.Subsigs[i]
		if subsig.Key == pk {
			isValidKey = true
			break
		}
	}

	if !isValidKey {
		return partial, errMsigWrongKey
	}

	sig, err := lw.signTransactionHelper(tx)
	if err != nil {
		return partial, err
	}

	for i := 0; i < len(partial.Subsigs); i++ {
		subsig := &partial.Subsigs[i]
		if subsig.Key == pk {
			subsig.Sig = sig
		}
	}

	return partial, nil
}

// MultisigSignProgram implements the Wallet interface.
func (lw *LedgerWallet) MultisigSignProgram(data []byte, src crypto.Digest, pk crypto.PublicKey, partial crypto.MultisigSig, pw []byte) (crypto.MultisigSig, error) {
	isValidKey := false
	for i := 0; i < len(partial.Subsigs); i++ {
		subsig := &partial.Subsigs[i]
		if subsig.Key == pk {
			isValidKey = true
			break
		}
	}

	if !isValidKey {
		return partial, errMsigWrongKey
	}

	sig, err := lw.signProgramHelper(data)
	if err != nil {
		return partial, err
	}

	for i := 0; i < len(partial.Subsigs); i++ {
		subsig := &partial.Subsigs[i]
		if subsig.Key == pk {
			subsig.Sig = sig
		}
	}

	return partial, nil
}

func uint64le(i uint64) []byte {
	var buf [8]byte
	binary.LittleEndian.PutUint64(buf[:], i)
	return buf[:]
}

func (lw *LedgerWallet) signTransactionHelper(tx transactions.Transaction) (sig crypto.Signature, err error) {
	lw.mu.Lock()
	defer lw.mu.Unlock()

	sig, err = lw.sendTransactionMsgpack(tx)
	if err == nil {
		return
	}

	ledgerErr, ok := err.(LedgerUSBError)
	if ok && ledgerErr == 0x6d00 {
		// We tried to send a msgpack-encoded transaction to the device,
		// but it doesn't support the new-style opcode, so fall back
		// to old-style encoding.
		sig, err = lw.sendTransactionOldStyle(tx)
	}

	return
}

func (lw *LedgerWallet) sendTransactionMsgpack(tx transactions.Transaction) (sig crypto.Signature, err error) {
	var reply []byte

	tosend := protocol.Encode(&tx)
	p1 := ledgerP1first
	p2 := ledgerP2more

	// As a precaution, make sure that chunk + 5-byte APDU header
	// fits in 8-bit length fields.
	const chunkSize = 250

	for p2 != ledgerP2last {
		var chunk []byte
		if len(tosend) > chunkSize {
			chunk = tosend[:chunkSize]
		} else {
			chunk = tosend
			p2 = ledgerP2last
		}

		var msg []byte
		msg = append(msg, ledgerClass, ledgerInsSignMsgpack, p1, p2, uint8(len(chunk)))
		msg = append(msg, chunk...)

		reply, err = lw.dev.Exchange(msg)
		if err != nil {
			return
		}

		tosend = tosend[len(chunk):]
		p1 = ledgerP1more
	}

	if len(reply) > len(sig) {
		// Error related to transaction decoding.
		errmsg := string(reply[len(sig)+1:])
		err = errors.New(errmsg)
		return
	}

	copy(sig[:], reply)
	return
}

func (lw *LedgerWallet) sendTransactionOldStyle(tx transactions.Transaction) (sig crypto.Signature, err error) {
	var msg []byte
	msg = append(msg, ledgerClass)

	switch tx.Type {
	case protocol.PaymentTx:
		msg = append(msg, ledgerInsSignPaymentV2)
	case protocol.KeyRegistrationTx:
		msg = append(msg, ledgerInsSignKeyregV2)
	default:
		err = fmt.Errorf("transaction type %s not supported", tx.Type)
		return
	}

	if len(tx.Note) != 0 {
		err = fmt.Errorf("transaction notes not supported")
		return
	}

	msg = append(msg, tx.Sender[:]...)
	msg = append(msg, uint64le(tx.Fee.Raw)...)
	msg = append(msg, uint64le(uint64(tx.FirstValid))...)
	msg = append(msg, uint64le(uint64(tx.LastValid))...)

	var genbuf [32]byte
	if len(tx.GenesisID) > len(genbuf) {
		err = fmt.Errorf("genesis ID %s too long (%d)", tx.GenesisID, len(tx.GenesisID))
		return
	}

	copy(genbuf[:], []byte(tx.GenesisID))
	msg = append(msg, genbuf[:]...)
	msg = append(msg, tx.GenesisHash[:]...)

	switch tx.Type {
	case protocol.PaymentTx:
		msg = append(msg, tx.Receiver[:]...)
		msg = append(msg, uint64le(tx.Amount.Raw)...)
		msg = append(msg, tx.CloseRemainderTo[:]...)
	case protocol.KeyRegistrationTx:
		msg = append(msg, tx.VotePK[:]...)
		msg = append(msg, tx.SelectionPK[:]...)
	}

	reply, err := lw.dev.Exchange(msg)
	if err != nil {
		return
	}

	copy(sig[:], reply)
	return
}

func (lw *LedgerWallet) signProgramHelper(data []byte) (sig crypto.Signature, err error) {
	// TODO: extend client side code for signing program
	err = errors.New("signing programs not yet implemented for ledger wallet")
	return
}