LedgerHQ / app-openpgp

OpenPGP Card Application
Apache License 2.0
123 stars 21 forks source link

backup.py "TypeError: Expected a list of bytes" #22

Closed chozian closed 5 years ago

chozian commented 6 years ago

The following error is received when executing the original backup.py or the modified code at https://github.com/LedgerHQ/blue-app-openpgp-card/issues/8#issuecomment-334121837.

# python backup.py 
Traceback (most recent call last):
  File "backup.py", line 20, in <module>
    gpgcard.get_all()
  File "/home/chozian/git/ledger/blue-app-openpgp-card/pytools/gpgcard/gpgcard.py", line 221, in get_all
    self.AID,sw                      = self.get_data(0x4f)
  File "/home/chozian/git/ledger/blue-app-openpgp-card/pytools/gpgcard/gpgcard.py", line 189, in get_data
    return self.exchange(apdu)
  File "/home/chozian/git/ledger/blue-app-openpgp-card/pytools/gpgcard/gpgcard.py", line 156, in _exchange_pcsc
    resp, sw1, sw2 = self.connection.transmit(apdu)
  File "/usr/lib/python2.7/dist-packages/smartcard/CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "/usr/lib/python2.7/dist-packages/smartcard/CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "/usr/lib/python2.7/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 200, in doTransmit
    self.hcard, pcscprotocolheader, bytes)
  File "/usr/lib/python2.7/dist-packages/smartcard/scard/scard.py", line 850, in SCardTransmit
    return _scard.SCardTransmit(hcard, pioSendPci, apducommand)
TypeError: Expected a list of bytes.

Versions:

This was tested using 2 different Ledger Nano S units with firmware 1.4.1. The results were the same for both.

The latest releases of blue-loader-python, blue-app-ssh-agent, blue-app-u2f, and trezor-agent all work as expected.

This was tested with and without PGP keys generated on the Ledgers.

gpg --card-status and --card-edit work as expected, except when generating 4096 RSA keys which results in a failure. 3072 RSA keys generate without issue though.

This was tested using Debian 8 on VirtualBox 5.2.8 and VMware Workstation 14 Player.

Debian 8 Package Versions:

Debian 8 python virtualenv:

This was also tested using Ubuntu 16.04 LTS on a bare metal Dell Latitude with built-in smart card reader and Dell Inspiron with no other smart card reader.

Ubuntu Package Versions:

Ubuntu python virtualenv:

pcsc_scan displays the following.

PC/SC device scanner
V 1.4.25 (c) 2001-2011, Ludovic Rousseau <ludovic.rousseau@free.fr>
Compiled with PC/SC lite version: 1.8.14
Using reader plug'n play mechanism
Scanning present readers...
0: Ledger Nano S [Nano S] (0001) 00 00
1: Broadcom Corp 5880 [Contacted SmartCard] (0123456789ABCD) 01 00

Sun Apr 15 22:02:08 2018
Reader 0: Ledger Nano S [Nano S] (0001) 00 00
  Card state: Card inserted, 
  ATR: 3B 00

ATR: 3B 00
+ TS = 3B --> Direct Convention
+ T0 = 00, Y(1): 0000, K: 0 (historical bytes)
Reader 1: Broadcom Corp 5880 [Contacted SmartCard] (0123456789ABCD) 01 00
  Card state: Card removed,

I am not sure if the following dmesg output is relevant to this issue. The two lines containing "interface number" as shown below occur when switching to the OpenPGP app on the Ledger.

[ 7084.929065] usb 2-1.4: new full-speed USB device number 5 using ehci-pci
[ 7085.043198] usb 2-1.4: config 1 has an invalid interface number: 2 but max is 1
[ 7085.043206] usb 2-1.4: config 1 has no interface number 1
[ 7085.047244] usb 2-1.4: New USB device found, idVendor=2c97, idProduct=0001
[ 7085.047253] usb 2-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 7085.047258] usb 2-1.4: Product: Nano S
[ 7085.047262] usb 2-1.4: Manufacturer: Ledger
[ 7085.047266] usb 2-1.4: SerialNumber: 0001
[ 7085.054911] hid-generic 0003:2C97:0001.0003: hiddev0,hidraw0: USB HID v1.11 Device [Ledger Nano S] on usb-0000:00:1d.0-1.4/input0

Please let me know if any additional information would be useful. I would like to know if the error with backup.py is due to a bug. Am I simply overlooking something? I feel like I have thoroughly exhausted all troubleshooting options based on my knowledge and experience at this point. Anyone's assistance would be greatly appreciated. Thanks!

chozian commented 6 years ago

The reason I would really like to get this working is to have a way to restore my seed-generated keys after a Ledger Nano S firmware upgrade. If there is a better method, please let me know. Thanks!

cslashm commented 6 years ago

Did you try python3?