Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MacOS: Current master fails to unlock the Yubikey token #1725

Closed
mouse07410 opened this issue Jul 3, 2019 · 36 comments · Fixed by #1752
Closed

MacOS: Current master fails to unlock the Yubikey token #1725

mouse07410 opened this issue Jul 3, 2019 · 36 comments · Fixed by #1752

Comments

@mouse07410
Copy link
Contributor

Problem Description

This started with MacOS Mojave 10.14.5. Native PCSC, current OpenSC master.

I recall something similar a couple of years ago, but not since - until now.

OpenSC fails to unlock the token.

it seems that OpenSC (tokend, specifically?) loses the state of the token, and/or loses the ability to communicate with it.

Proposed Resolution

Don't know yet.

Steps to reproduce

  1. Boot up computer && start VPN
  2. Select Yubikey cert when prompted
  3. Enter PIN and enjoy connection
  4. Click Disconnect
  5. Click Connect and select yubikey cert
  6. Enter PIN when prompted
  7. Immediately see a Timeout error message.
  8. Click Retry
  9. Repeat steps 5-8 till you get bored
  10. Remove Yubikey and reinsert
  11. Click Connect and select yubikey cert
  12. Enter PIN and enjoy connection

Logs

. . . . . [First - successful login/signature computation] . . . . .
/Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCRecord.cpp:237:getAcl:   retuning 2 ACL entries
P:7220; T:0x4631541184 14:28:25.252 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:77:generateSignature: In OpenSCKeyHandle::generateSignature()
P:7220; T:0x4631541184 14:28:25.252 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:80:generateSignature:   type == CSSM_ALGCLASS_SIGNATURE
P:7220; T:0x4631541184 14:28:25.252 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:88:generateSignature:   algorithm == CSSM_ALGID_RSA
P:7220; T:0x4631541184 14:28:25.252 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:120:generateSignature:   Using SHA512, length is 64 bytes
P:7220; T:0x4631541184 14:28:25.252 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:186:generateSignature:   PKCS#1 padding
P:7220; T:0x4631541184 14:28:25.252 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:210:generateSignature:   Signing buffers: inlen=64, outlen=256
P:7220; T:0x4631541184 14:28:25.252 [tokend] pkcs15-sec.c:572:sc_pkcs15_compute_signature: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] pkcs15-sec.c:620:sc_pkcs15_compute_signature: supported algorithm flags 0x1, private key usage 0x2E
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:470:sc_get_encoding_flags: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:474:sc_get_encoding_flags: iFlags 0x8002, card capabilities 0x1
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:525:sc_get_encoding_flags: pad flags 0x8002, secure algorithm flags 0x1
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:526:sc_get_encoding_flags: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.252 [tokend] pkcs15-sec.c:677:sc_pkcs15_compute_signature: DEE flags:0x00008002 alg_info->flags:0x00000001 pad:0x00008002 sec:0x00000001
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:409:sc_pkcs1_encode: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:415:sc_pkcs1_encode: hash algorithm 0x8000, pad algorithm 0x2
P:7220; T:0x4631541184 14:28:25.252 [tokend] padding.c:439:sc_pkcs1_encode: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] reader-pcsc.c:640:pcsc_lock: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card-piv.c:3757:piv_card_reader_lock_obtained: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card-piv.c:2751:piv_find_discovery: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card-piv.c:910:piv_get_data: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card-piv.c:911:piv_get_data: #10
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.252 [tokend] card-piv.c:961:piv_get_data: buffer for #10 *buf=0x0x7ffee3a50980 len=256
P:7220; T:0x4631541184 14:28:25.252 [tokend] card-piv.c:533:piv_general_io: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.252 [tokend] apdu.c:546:sc_transmit_apdu: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:28:25.252 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.253 [tokend] apdu.c:513:sc_transmit: called
P:7220; T:0x4631541184 14:28:25.253 [tokend] apdu.c:363:sc_single_transmit: called
P:7220; T:0x4631541184 14:28:25.253 [tokend] apdu.c:370:sc_single_transmit: CLA:0, INS:CB, P1:3F, P2:FF, data(3) 0x7ffee3a50928
P:7220; T:0x4631541184 14:28:25.253 [tokend] reader-pcsc.c:285:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:7220; T:0x4631541184 14:28:25.253 [tokend] reader-pcsc.c:286:pcsc_transmit: 
. . . . .
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:3777:piv_card_reader_lock_obtained: PIV_MATCH piv_find_discovery card->type:14004 r:0
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:3798:piv_card_reader_lock_obtained: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.254 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.254 [tokend] sec.c:105:sc_set_security_env: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:2287:piv_set_security_env: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:2292:piv_set_security_env: flags=00000014 op=2 alg=0 algf=00000001 algr=00000000 kr0=9a, krfl=1
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:2319:piv_set_security_env: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.254 [tokend] sec.c:109:sc_set_security_env: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.254 [tokend] sec.c:59:sc_compute_signature: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:2420:piv_compute_signature: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:2348:piv_validate_general_authentication: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card-piv.c:533:piv_general_io: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.254 [tokend] apdu.c:546:sc_transmit_apdu: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:25.254 [tokend] apdu.c:513:sc_transmit: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] apdu.c:363:sc_single_transmit: called
P:7220; T:0x4631541184 14:28:25.254 [tokend] apdu.c:370:sc_single_transmit: CLA:10, INS:87, P1:7, P2:9A, data(255) 0x7ffee3a4fae0
P:7220; T:0x4631541184 14:28:25.254 [tokend] reader-pcsc.c:285:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:7220; T:0x4631541184 14:28:25.254 [tokend] reader-pcsc.c:286:pcsc_transmit: 
Outgoing APDU (260 bytes):
. . . . .
P:7220; T:0x4631541184 14:28:26.928 [tokend] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:26.928 [tokend] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:26.928 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:28:26.928 [tokend] apdu.c:497:sc_get_response: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:26.928 [tokend] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:7220; T:0x4631541184 14:28:26.928 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:28:26.928 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:28:26.928 [tokend] card-piv.c:596:piv_general_io: returning with: 264
P:7220; T:0x4631541184 14:28:26.928 [tokend] card-piv.c:2401:piv_validate_general_authentication: returning with: 256
P:7220; T:0x4631541184 14:28:26.928 [tokend] card-piv.c:2478:piv_compute_signature: returning with: 256
P:7220; T:0x4631541184 14:28:26.928 [tokend] sec.c:63:sc_compute_signature: returning with: 256
P:7220; T:0x4631541184 14:28:26.928 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:28:26.928 [tokend] reader-pcsc.c:690:pcsc_unlock: called
P:7220; T:0x4631541184 14:28:26.928 [tokend] pkcs15-sec.c:725:sc_pkcs15_compute_signature: returning with: 256
P:7220; T:0x4631541184 14:28:26.928 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:218:generateSignature:   sc_pkcs15_compute_signature(): rv = 256
P:7220; T:0x4631541184 14:28:26.929 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:230:generateSignature:   Completed RSA signature, len=256
. . . . .
. . . . . [ now - failure to sign ] . . . . .
P:7220; T:0x4631541184 14:33:54.229 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:77:generateSignature: In OpenSCKeyHandle::generateSignature()
P:7220; T:0x4631541184 14:33:54.229 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:80:generateSignature:   type == CSSM_ALGCLASS_SIGNATURE
P:7220; T:0x4631541184 14:33:54.229 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:88:generateSignature:   algorithm == CSSM_ALGID_RSA
P:7220; T:0x4631541184 14:33:54.229 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:120:generateSignature:   Using SHA512, length is 64 bytes
P:7220; T:0x4631541184 14:33:54.229 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:186:generateSignature:   PKCS#1 padding
P:7220; T:0x4631541184 14:33:54.229 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:210:generateSignature:   Signing buffers: inlen=64, outlen=256
P:7220; T:0x4631541184 14:33:54.229 [tokend] pkcs15-sec.c:572:sc_pkcs15_compute_signature: called
P:7220; T:0x4631541184 14:33:54.229 [tokend] pkcs15-sec.c:620:sc_pkcs15_compute_signature: supported algorithm flags 0x1, private key usage 0x2E
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:470:sc_get_encoding_flags: called
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:474:sc_get_encoding_flags: iFlags 0x8002, card capabilities 0x1
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:525:sc_get_encoding_flags: pad flags 0x8002, secure algorithm flags 0x1
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:526:sc_get_encoding_flags: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.229 [tokend] pkcs15-sec.c:677:sc_pkcs15_compute_signature: DEE flags:0x00008002 alg_info->flags:0x00000001 pad:0x00008002 sec:0x00000001
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:409:sc_pkcs1_encode: called
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:415:sc_pkcs1_encode: hash algorithm 0x8000, pad algorithm 0x2
P:7220; T:0x4631541184 14:33:54.229 [tokend] padding.c:439:sc_pkcs1_encode: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.229 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:33:54.229 [tokend] reader-pcsc.c:640:pcsc_lock: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:3757:piv_card_reader_lock_obtained: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:2751:piv_find_discovery: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:910:piv_get_data: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:911:piv_get_data: #10
P:7220; T:0x4631541184 14:33:54.232 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:961:piv_get_data: buffer for #10 *buf=0x0x7ffee3a50980 len=256
P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:533:piv_general_io: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.232 [tokend] apdu.c:546:sc_transmit_apdu: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.232 [tokend] apdu.c:513:sc_transmit: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] apdu.c:363:sc_single_transmit: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] apdu.c:370:sc_single_transmit: CLA:0, INS:CB, P1:3F, P2:FF, data(3) 0x7ffee3a50928
P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:285:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:286:pcsc_transmit: 
Outgoing APDU (9 bytes):
. . . . .
P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:214:pcsc_internal_transmit: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:243:pcsc_internal_transmit: Yubico Yubikey 4 OTP+U2F+CCID:SCardTransmit/Control failed: 0x80100068
P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:428:pcsc_detect_card_presence: called
P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:321:refresh_attributes: Yubico Yubikey 4 OTP+U2F+CCID check
P:7220; T:0x4631541184 14:33:54.233 [tokend] reader-pcsc.c:341:refresh_attributes: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.233 [tokend] reader-pcsc.c:433:pcsc_detect_card_presence: returning with: 5
P:7220; T:0x4631541184 14:33:54.233 [tokend] reader-pcsc.c:428:pcsc_detect_card_presence: called
P:7220; T:0x4631541184 14:33:54.233 [tokend] reader-pcsc.c:321:refresh_attributes: Yubico Yubikey 4 OTP+U2F+CCID check
P:7220; T:0x4631541184 14:33:54.234 [tokend] reader-pcsc.c:341:refresh_attributes: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.234 [tokend] reader-pcsc.c:433:pcsc_detect_card_presence: returning with: 5
P:7220; T:0x4631541184 14:33:54.234 [tokend] reader-pcsc.c:292:pcsc_transmit: unable to transmit
P:7220; T:0x4631541184 14:33:54.234 [tokend] apdu.c:380:sc_single_transmit: unable to transmit APDU: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.234 [tokend] apdu.c:516:sc_transmit: transmit APDU failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.234 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:570:piv_general_io: Transmit failed
P:7220; T:0x4631541184 14:33:54.234 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:596:piv_general_io: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.234 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:974:piv_get_data: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:2770:piv_find_discovery: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:3777:piv_card_reader_lock_obtained: PIV_MATCH piv_find_discovery card->type:14004 r:-1107
P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:724:piv_select_aid: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] apdu.c:546:sc_transmit_apdu: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] card.c:477:sc_lock: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.234 [tokend] apdu.c:513:sc_transmit: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] apdu.c:363:sc_single_transmit: called
P:7220; T:0x4631541184 14:33:54.234 [tokend] apdu.c:370:sc_single_transmit: CLA:0, INS:A4, P1:4, P2:0, data(9) 0x10c480490
P:7220; T:0x4631541184 14:33:54.234 [tokend] reader-pcsc.c:285:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:7220; T:0x4631541184 14:33:54.234 [tokend] reader-pcsc.c:286:pcsc_transmit: 
Outgoing APDU (15 bytes):
. . . . .
P:7220; T:0x4631541184 14:33:54.234 [tokend] reader-pcsc.c:214:pcsc_internal_transmit: called
P:7220; T:0x4631541184 14:33:54.235 [tokend] reader-pcsc.c:243:pcsc_internal_transmit: Yubico Yubikey 4 OTP+U2F+CCID:SCardTransmit/Control failed: 0x80100068
P:7220; T:0x4631541184 14:33:54.235 [tokend] reader-pcsc.c:428:pcsc_detect_card_presence: called
P:7220; T:0x4631541184 14:33:54.235 [tokend] reader-pcsc.c:321:refresh_attributes: Yubico Yubikey 4 OTP+U2F+CCID check
P:7220; T:0x4631541184 14:33:54.236 [tokend] reader-pcsc.c:341:refresh_attributes: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.236 [tokend] reader-pcsc.c:433:pcsc_detect_card_presence: returning with: 5
P:7220; T:0x4631541184 14:33:54.236 [tokend] reader-pcsc.c:428:pcsc_detect_card_presence: called
P:7220; T:0x4631541184 14:33:54.236 [tokend] reader-pcsc.c:321:refresh_attributes: Yubico Yubikey 4 OTP+U2F+CCID check
P:7220; T:0x4631541184 14:33:54.237 [tokend] reader-pcsc.c:341:refresh_attributes: returning with: 0 (Success)
P:7220; T:0x4631541184 14:33:54.237 [tokend] reader-pcsc.c:433:pcsc_detect_card_presence: returning with: 5
P:7220; T:0x4631541184 14:33:54.237 [tokend] reader-pcsc.c:292:pcsc_transmit: unable to transmit
P:7220; T:0x4631541184 14:33:54.237 [tokend] apdu.c:380:sc_single_transmit: unable to transmit APDU: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] apdu.c:516:sc_transmit: transmit APDU failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:33:54.237 [tokend] card-piv.c:738:piv_select_aid: PIV select failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] card-piv.c:3783:piv_card_reader_lock_obtained: PIV_MATCH piv_select_aid card->type:14004 r:-1107
P:7220; T:0x4631541184 14:33:54.237 [tokend] card-piv.c:3798:piv_card_reader_lock_obtained: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] card.c:517:sc_lock: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] pkcs15-sec.c:140:use_key: sc_lock() failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] pkcs15-sec.c:713:sc_pkcs15_compute_signature: use_key() failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:33:54.237 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:218:generateSignature:   sc_pkcs15_compute_signature(): rv = -1107
. . . . . [ more failures] . . . . .
P:7220; T:0x4631541184 14:34:13.648 [tokend] reader-pcsc.c:214:pcsc_internal_transmit: called
P:7220; T:0x4631541184 14:34:13.648 [tokend] reader-pcsc.c:243:pcsc_internal_transmit: Yubico Yubikey 4 OTP+U2F+CCID:SCardTransmit/Control failed: 0x80100068
P:7220; T:0x4631541184 14:34:13.648 [tokend] reader-pcsc.c:428:pcsc_detect_card_presence: called
P:7220; T:0x4631541184 14:34:13.648 [tokend] reader-pcsc.c:321:refresh_attributes: Yubico Yubikey 4 OTP+U2F+CCID check
P:7220; T:0x4631541184 14:34:13.649 [tokend] reader-pcsc.c:341:refresh_attributes: returning with: 0 (Success)
P:7220; T:0x4631541184 14:34:13.649 [tokend] reader-pcsc.c:433:pcsc_detect_card_presence: returning with: 5
P:7220; T:0x4631541184 14:34:13.649 [tokend] reader-pcsc.c:428:pcsc_detect_card_presence: called
P:7220; T:0x4631541184 14:34:13.649 [tokend] reader-pcsc.c:321:refresh_attributes: Yubico Yubikey 4 OTP+U2F+CCID check
P:7220; T:0x4631541184 14:34:13.650 [tokend] reader-pcsc.c:341:refresh_attributes: returning with: 0 (Success)
P:7220; T:0x4631541184 14:34:13.650 [tokend] reader-pcsc.c:433:pcsc_detect_card_presence: returning with: 5
P:7220; T:0x4631541184 14:34:13.650 [tokend] reader-pcsc.c:292:pcsc_transmit: unable to transmit
P:7220; T:0x4631541184 14:34:13.650 [tokend] apdu.c:380:sc_single_transmit: unable to transmit APDU: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] apdu.c:516:sc_transmit: transmit APDU failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:34:13.650 [tokend] card-piv.c:570:piv_general_io: Transmit failed
P:7220; T:0x4631541184 14:34:13.650 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:34:13.650 [tokend] card-piv.c:596:piv_general_io: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] card-piv.c:2401:piv_validate_general_authentication: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] card-piv.c:2478:piv_compute_signature: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] sec.c:63:sc_compute_signature: returning with: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] card.c:527:sc_unlock: called
P:7220; T:0x4631541184 14:34:13.650 [tokend] pkcs15-sec.c:713:sc_pkcs15_compute_signature: use_key() failed: -1107 (Transmit failed)
P:7220; T:0x4631541184 14:34:13.650 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:218:generateSignature:   sc_pkcs15_compute_signature(): rv = -1107
. . . . .

@dengert do you see anything in the above that could explain what the problem is, and how to fix it, or at least work-around?

@dengert
Copy link
Member

dengert commented Jul 3, 2019

Lines 119, 151, 173 all return P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:243:pcsc_internal_transmit: Yubico Yubikey 4 OTP+U2F+CCID:SCardTransmit/Control failed: 0x80100068

line 98 P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:3757:piv_card_reader_lock_obtained: called indicates that this is a start of a lock. It then does:
line 99 card-piv.c:2751:piv_find_discovery: called to read the discovery object to make sure the PIV applet is active.

line 119: P:7220; T:0x4631541184 14:33:54.232 [tokend] reader-pcsc.c:243:pcsc_internal_transmit: Yubico Yubikey 4 OTP+U2F+CCID:SCardTransmit/Control failed: 0x80100068

line 137: P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:2770:piv_find_discovery: returning with: -1107 (Transmit failed)

So something happened between line 98 and 119. Is there some other application running that could have reset the card?

Is the Mac PCSC failing to reset the reset?
Grep for "lock: called" which would show all the sc_lock, sc_unlock, pcsc_lock and pcsc_unlock.
That may give you a clue.

Have to go...

@dengert
Copy link
Member

dengert commented Jul 4, 2019

There is a time difference between
lines 97: P:7220; T:0x4631541184 14:33:54.229 [tokend] reader-pcsc.c:640:pcsc_lock: called
and line 98: P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:3757:piv_card_reader_lock_obtained: called
which could indicate some other app was accessing the card, and the pcsc_lock had to wait.

card.c in sc_lock has:

 480                         while (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
 481                                 sc_invalidate_cache(card);
 482                                 if (was_reset++ > 4) /* TODO retry a few times */
 483                                         break;
 484                                 r = card->reader->ops->lock(card->reader);
 485                         }

It looks like was_reset would be 1, but it does not show up in the log.

Can you use a debugger and step through piv_card_reader_lock_obtained what is the was_reset parameter value? It looks like it is > 0 the code in card-piv.c

779         if (r < 0) {
3780                 if (was_reset > 0 || !(priv->card_issues & CI_PIV_AID_LOSE_STATE)) {
3781                         r = piv_select_aid(card, piv_aids[0].value, piv_aids[0].len_short, temp, &templen);
3782                         sc_debug(card->ctx,SC_LOG_DEBUG_MATCH, "PIV_MATCH piv_select_aid card->type:%d r:%d\n", card->type, r);

is executed and piv_select_aid returns
line 164:P:7220; T:0x4631541184 14:33:54.237 [tokend] card-piv.c:738:piv_select_aid: PIV select failed: -1107 (Transmit failed)

@dengert
Copy link
Member

dengert commented Jul 4, 2019

What does your step "4 Click Disconnect" actually do?

@mouse07410
Copy link
Contributor Author

First, thank you, and happy 4th of July!

No, I can't use a debugger - this is not my machine, among other reasons. I'm just getting logs. But I can add another sc_debug() or such to the driver source (especially if you tell me what you'd like to see, and where), rebuild the code, and have the user re-try.

Step "Click 'Disconnect'" means "issue a command to the VPN app to drop secure network connection". The VPN app in question is Pulse Secure, in case it matters.

Also, when the tokend is in this state, the user can SSH to the machine in question, and perform operations on the token, e.g., do pkcs11-tool --login --test. So, the token is alive, and probably the low-lever OS drivers too...

Maybe not related to this, but I often observe that OpenSC.tokend doesn't detect reliably that the token has been removed. The daemon (tokend) keeps running, as if the card was still inserted (though of course it can't perform any command). The only way to kick it out from this stupor (without reboot) is to re-insert the token, and remove it again in a minute or less. Then tokend usually gets that the token has been removed, and exits.

@frankmorgner
Copy link
Member

Is it possible to reproduce the problem with core utils of macOS or OpenSC?

Try to avoid concurrency in opensc.conf via:

	reader_driver pcsc {
		connect_exclusive = true;

@dengert
Copy link
Member

dengert commented Jul 4, 2019

So a VPN is involved.
Where is the token, with the user on the user client?
Where is the tokend running, on the server?
Is the VPN tunneling the PCSC packets much like Windows Remote Desktop Connection smart card?

@mouse07410
Copy link
Contributor Author

I don't know if it's reproducible - the complaints I'm getting are "screen lock is engaged, and when I come back to unlock it, no matter what PIN I enter, it's rejected with that shaking popup", and "I cannot reconnect my VPN - it prompts me for my PIN, and then says Timeout".

I'll try to add the extra logging that @dengert mentioned, and the concurrency-avoiding config change that you showed - and report results.

@mouse07410
Copy link
Contributor Author

Both token and tokend are on the client machine, physically. The VPN accesses the token via tokend to PKI-authenticate itself to the remote end (to the server). I don't know PCSC tunneling, but rather doubt it because this VPN app doesn't appear that sofisticated...

@dengert
Copy link
Member

dengert commented Jul 4, 2019

Can you get a PCSC debug log? It appears that once it sets the card was reset, it is not cleared. Or it is getting reset multiple times.

@frankmorgner
Copy link
Member

Please, again try to confirm if exclusive locking solves the problem. If so, please try #1727 if it solves the problem without exclusive locking.

@mouse07410
Copy link
Contributor Author

mouse07410 commented Jul 5, 2019

Thanks, will try as soon as we are back from the July 4 long weekend (until then I've no access to those computers and people who had problems).

Update

Delayed utill Jul 15th - people who have this problem are vacationing. ;-)

@mouse07410
Copy link
Contributor Author

The patches work on my machines, but the problem machines still fail. The failure is observed only with Pulse Secure VPN, and only after upgrading macOS to 10.14.4 (persists on 10.14.5).

We tried only #1727 - our IT is rather hesitant to do exclusive locking. Perhaps I don't understand fully what "exclusive locking" means, and it's not so bad? We cannot allow one application to usurp the token, unless it's "grab the lock, (quickly) perform an operation, release the lock".

@dengert
Copy link
Member

dengert commented Jul 16, 2019

You said: "I don't know if it's reproducible - the complaints I'm getting are "screen lock is engaged, and when I come back to unlock it, no matter what PIN I enter, it's rejected with that shaking popup", and "I cannot reconnect my VPN - it prompts me for my PIN, and then says Timeout".

That sounds like a misconfigured VPN or screensaver that has locked the screen or PAM (or whatever MacOS is using). Has the screen saver changed in macOS 0.14.4? Does it know how to use the smart card to unlock the screen? Is the VPN holding the lock on the smartcard in exclusive mode?

Can you ssh to the machine while locked and look around?

@mouse07410
Copy link
Contributor Author

mouse07410 commented Jul 16, 2019

That sounds like a misconfigured VPN or screensaver that has locked the screen or PAM (or whatever MacOS is using).

IMHO, unlikely. Also, the current "reproducer" is only Pulse Secure VPN app. It looks like screen unlock works smoothly with the new OpenSC (applied #1723 and #1727).

Has the screen saver changed in macOS 0.14.4?

AFAIK, it hasn't.

Does it [screen saver] know how to use the smart card to unlock the screen?

Yep. And on my machines (thankfully) I experience no problems. It's on machines that belong to others (and so I have limited access to them).

Is the VPN holding the lock on the smartcard in exclusive mode?

No clue. I personally think that particular VPN app sucks big. But nobody asked for my opinion. ;-)

Can you ssh to the machine while locked and look around?

Don't know. Probably... Although now the biggest issue is that VPN app. Screensaver seems to be behaving fine...

Also, the current workaround is removing and re-inserting the token. Does it tell anything about what might be going on?

@dengert
Copy link
Member

dengert commented Jul 16, 2019

Looking closer at the debug log and response #1725 (comment) The problem may not have occurred between 98 and 119 as stated in #1725 (comment) A card reset may be what is new in macOS 0.14.4. This could have the triggered an issue in the handling of the reset in sc_lock when it calls r = card->ops->card_reader_lock_obtained(card, was_reset); in this case in piv_card_reader_lock_obtained.

100 P:7220; T:0x4631541184 14:33:54.232 [tokend] card-piv.c:3757:piv_card_reader_lock_obtained: called
140 P:7220; T:0x4631541184 14:33:54.234 [tokend] card-piv.c:3777:piv_card_reader_lock_obtained:  PIV_MATCH piv_find_discovery card->type:14004 r:-1107
167 P:7220; T:0x4631541184 14:33:54.237 [tokend] card-piv.c:3783:piv_card_reader_lock_obtained: PIV_MATCH piv_select_aid card->type:14004 r:-1107
168 P:7220; T:0x4631541184 14:33:54.237 [tokend] card-piv.c:3798:piv_card_reader_lock_obtained: returning with: -1107 (Transmit failed)

Can you try this patch:
patch-1725.diff.txt

It is not perfect, but might allow it to get further.

If piv_card_reader_lock_obtained (or any of the other *_card_reader_lock_obtained returns r < 0 the error is passed on as the sc_lock failed. It is not really sc_lock failed but a command failed and maybe it is not needed as stated in the "/* cant do anything with this card, hope there was no interference */"

@mouse07410
Copy link
Contributor Author

mouse07410 commented Jul 16, 2019

Can you try this patch: patch-1725.diff.txt

On top of what I already have, or instead of it?

What should I expect as a result? Should it start working?

@dengert
Copy link
Member

dengert commented Jul 17, 2019

Either. The problem maybe how the reset is being handled. I would expect it would get further and might even work.

@mouse07410
Copy link
Contributor Author

Sent the modified binaries to the people involved - will report results.

@dengert
Copy link
Member

dengert commented Jul 19, 2019

@mouse07410 I see this difference between your clone and current master in card.c
This appears to be from 4e81a56 and bb41d37 in your clone (i.e. the 3 "-" lines):

diff -u $M/card.c card.c
--- /afs/anl.gov/appl/OpenSC-dev/build/opensc-git-mouse7410/src/src/libopensc/card.c	2019-07-16 06:34:39.912227253 -0500
+++ card.c	2019-07-14 17:40:56.982621555 -0500
@@ -450,9 +450,6 @@
 
 	r = card->reader->ops->reset(card->reader, do_cold_reset);
 	sc_invalidate_cache(card);
-	if (r == SC_SUCCESS && card->ops->card_reader_lock_obtained != NULL) {
-		r = card->ops->card_reader_lock_obtained(card, 1);
-	}
 
 	r2 = sc_mutex_unlock(card->ctx, card->mutex);
 	if (r2 != SC_SUCCESS) {

That does not look correct to me. card_reader_lock_obtained is meant to be called just after a pcsc lock has been called from sc_lock. The driver's *_reader_lock_obtained may then do some commands to the card, but the card may not be connected as expected. card_reader_lock_obtained is expected to be only called from sc_lock just after the `pcsc_lock is obtained.

I can not tell from your debug log if this directly related to this issue. But if you or your tokend are calling sc_reset the fact it was reset should be found by the normal flow that would call card_reader_lock_obtained at the correct time after a pcsc_lock was obtained.

@mouse07410
Copy link
Contributor Author

I see this difference ...

I don't know, as (unfortunately!) I did not record the reason/purpose of that change. Since it does not seem right to you, I'm removing this change - and will test the resulting binary.

@mouse07410
Copy link
Contributor Author

I don't know, as (unfortunately!) I did not record the reason/purpose of that change...

Well, I re-built the library with the lines in question removed - and immediately [re-]discovered why they were there. :-(

Without these lines and when pin_cache_user_consent_ignore=true is set (which is necessary for other reasons), screensaver does not prompt for the PIN any more when user tries to unlock the screen.

I think we tried to address that problem a year ago, and these lines were the best we came up with. Needless to say, I'm open to alternatives.

@dengert
Copy link
Member

dengert commented Jul 21, 2019

You said: "screensaver does not prompt for the PIN any more when user tries to unlock the screen."
This could have something to do with pin_info.logged_in being set to SC_PIN_STATE_LOGGED_IN before the reset, and not being reset to SC_PIN_STATE_LOGGED_OUT or at least SC_PIN_STATE_UNKNOWN after the reset. It sounds like that was the intent of calling card_reader_lock_obtained and may have been a side benefit.

As I said: "card_reader_lock_obtained is meant to be called just after a pcsc lock has been called from sc_lock." At this point if some other process has reset the card. And it is possible some other process may have gotten control prompted the user for pin and reestablished the login state.

Problem is some Yubkey do not handle queering of the login state of the card. See in card-piv.c CI_VERIFY_LC0_FAIL, CI_CANT_USE_GETDATA_FOR_STATE and CI_PIV_AID_LOSE_STATE.

Can I ask your to put the call to card_reader_lock_obtained back in, then add some debugging code to piv_card_reader_lock_obtained after both lines:
r = 0; /* cant do anything with this card, hope there was no interference */
to print to log priv->logged_in and priv->priv->card_issues`
And remove the lines:

if (r < 0) /* bad error return will show up in sc_lock as error*/
goto err;

@mouse07410
Copy link
Contributor Author

...it is possible some other process may have gotten control prompted the user for pin and reestablished the login state...

Possible, but I don't think it's likely. Especially on a machine that's sitting around idle with no user app running that might have any interest in keychains, except for the Screensaver. And the pin_cache_user_consent_ignore=true only affects OpenSC, so even if the native pivtoken is running, it cannot cache the PIN (AFAIK). Regardless, this doesn't explain why with the lines in question added, the problem stops manifesting itself (i.e., screensaver always prompts for the PIN).

Can I ask you to ...

Updated the code as you suggested, will run and post the log. What's the best way to get logging from tokend? It's trivial with OpenSC commands (e.g., OPENSC_DEBUG=9 pkcs11-tool ..., but with tokend - especially if I'd rather not log everything?)

@mouse07410
Copy link
Contributor Author

@dengert please find the log attached.
opensc-1725-log-truncated.txt

I assume I need to restore the if (r < 0) goto err; code for production build?

@dengert
Copy link
Member

dengert commented Jul 22, 2019

I don't see any signs of a reset.
pcsc_internal_transmit: Yubico Yubikey 4 OTP+U2F+CCID:SCardTransmit/Control failed: 0x80100068

Do you still do the card reset? If not can you put it back in?

Please leave the if (r < 0) goto err; out. The reason is if there is a failure in piv_card_lock_obtained and it is returned the sc_lock as a really bad failure which it should not. The two commands to read the discovery object and/or select the AID could fail. The normal code will catch any other failures.

Note the logged_in is reset to -1 if was_reset is set. So a pin prompt should be done. I assume it was?

@mouse07410
Copy link
Contributor Author

mouse07410 commented Jul 22, 2019

I don't see any signs of a reset...

Can't comment - beyond my competence.

... SCardTransmit/Control failed: 0x80100068

What does it mean? Comm failure on PCSC level...? Something unrelated to OpenSC?

Do you still do the card reset? If not can you put it back in?

Yes, I put that code back in, and the card/system now behaves as expected.
Update: I don't know if the reset was done, or if it was some other side-effect of those code lines I restored. All I can tell is that the card/middleware now behaves as expected.

Please leave the if (r < 0) goto err; out.

Done.

Note the logged_in is reset to -1 if was_reset is set. So a pin prompt should be done. I assume it was?

Yes, it was. Everything seems fine - except that I haven't heard yet from the people with that Pulse Secure issue.

Ready for more experiments/tests.

@frankmorgner
Copy link
Member

The error code from the transmit is SCARD_W_RESET_CARD, which OpenSC handles only correctly in sc_lock():

case SCARD_W_RESET_CARD:
/* try to reconnect if the card was reset by some other application */
PCSC_TRACE(reader, "SCardBeginTransaction calling pcsc_reconnect", rv);
r = pcsc_reconnect(reader, SCARD_LEAVE_CARD);
if (r != SC_SUCCESS) {
sc_log(reader->ctx,
"pcsc_reconnect failed (%d)", r);
return r;
}
/* return failure so that upper layers will be notified and try to lock again */
return SC_ERROR_CARD_RESET;

I think the best way to tackle this problem would be to reconnect and retransmit in sc_transmit_apdu() as well.

Ludovic has a number of bugs related to SCARD_W_RESET_CARD in Apple's implementation...

@frankmorgner
Copy link
Member

You could try something like this:

diff --git a/src/libopensc/reader-pcsc.c b/src/libopensc/reader-pcsc.c
index a058c71f7..6dbca7721 100644
--- a/src/libopensc/reader-pcsc.c
+++ b/src/libopensc/reader-pcsc.c
@@ -200,6 +200,7 @@ static DWORD opensc_proto_to_pcsc(unsigned int proto)
        }
 }
 
+static int pcsc_reconnect(sc_reader_t * reader, DWORD action);
 static int pcsc_internal_transmit(sc_reader_t *reader,
                         const u8 *sendbuf, size_t sendsize,
                         u8 *recvbuf, size_t *recvsize,
@@ -228,6 +229,13 @@ static int pcsc_internal_transmit(sc_reader_t *reader,
        if (!control) {
                rv = priv->gpriv->SCardTransmit(card, &sSendPci, sendbuf, dwSendLength,
                                   &sRecvPci, recvbuf, &dwRecvLength);
+               if (rv == SCARD_W_RESET_CARD) {
+                       PCSC_TRACE(reader, "SCardTransmit calling pcsc_reconnect", rv);
+                       if (SC_SUCCESS == pcsc_reconnect(reader, SCARD_LEAVE_CARD)) {
+                               rv = priv->gpriv->SCardTransmit(card, &sSendPci, sendbuf, dwSendLength,
+                                               &sRecvPci, recvbuf, &dwRecvLength);
+                       }
+               }
        } else {
                if (priv->gpriv->SCardControlOLD != NULL) {
                        rv = priv->gpriv->SCardControlOLD(card, sendbuf, dwSendLength,

@mouse07410
Copy link
Contributor Author

With the proposed change, I do not observe the error 0x80100068 any more. The only visible errors in the log are (and the code behaves as expected, at least on my machines):

. . . . .
P:16153; T:0x4422677952 10:38:39.148 [tokend] reader-pcsc.c:293:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:16153; T:0x4422677952 10:38:39.148 [tokend] reader-pcsc.c:294:pcsc_transmit:
Outgoing APDU (11 bytes):
00 CB 3F FF 05 5C 03 5F C1 0C 00 ..?..\._...
P:16153; T:0x4422677952 10:38:39.148 [tokend] reader-pcsc.c:215:pcsc_internal_transmit: called
P:16153; T:0x4422677952 10:38:39.149 [tokend] reader-pcsc.c:303:pcsc_transmit:
Incoming APDU (2 bytes):
6A 82 j.
P:16153; T:0x4422677952 10:38:39.149 [tokend] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:39.149 [tokend] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:39.149 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:39.149 [tokend] iso7816.c:128:iso7816_check_sw: File or application not found
P:16153; T:0x4422677952 10:38:39.149 [tokend] card-piv.c:643:piv_general_io: Card returned error: -1201 (File not found)
P:16153; T:0x4422677952 10:38:39.149 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:39.149 [tokend] card-piv.c:737:piv_general_io: returning with: -1201 (File not found)
P:16153; T:0x4422677952 10:38:39.149 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:39.149 [tokend] card-piv.c:1072:piv_get_data: returning with: -1201 (File not found)
P:16153; T:0x4422677952 10:38:39.149 [tokend] card-piv.c:1157:piv_get_cached_data: returning with: -1201 (File not found)
P:16153; T:0x4422677952 10:38:39.149 [tokend] card-piv.c:3096:piv_process_history: returning with: 0 (Success)
. . . . .
P:16153; T:0x4422677952 10:38:48.618 [tokend] apdu.c:363:sc_single_transmit: called
P:16153; T:0x4422677952 10:38:48.618 [tokend] apdu.c:370:sc_single_transmit: CLA:0, INS:CB, P1:3F, P2:FF, data(3) 0x7ffeed065f78
P:16153; T:0x4422677952 10:38:48.618 [tokend] reader-pcsc.c:293:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:16153; T:0x4422677952 10:38:48.618 [tokend] reader-pcsc.c:294:pcsc_transmit:
Outgoing APDU (9 bytes):
00 CB 3F FF 03 5C 01 7E 00 ..?..\.~.
P:16153; T:0x4422677952 10:38:48.618 [tokend] reader-pcsc.c:215:pcsc_internal_transmit: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] reader-pcsc.c:303:pcsc_transmit:
Incoming APDU (2 bytes):
6D 00 m.
P:16153; T:0x4422677952 10:38:48.619 [tokend] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.619 [tokend] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.619 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] iso7816.c:128:iso7816_check_sw: Instruction code not supported or invalid
P:16153; T:0x4422677952 10:38:48.619 [tokend] card-piv.c:643:piv_general_io: Card returned error: -1204 (Unsupported INS byte in APDU)
P:16153; T:0x4422677952 10:38:48.619 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] card-piv.c:737:piv_general_io: returning with: -1204 (Unsupported INS byte in APDU)
P:16153; T:0x4422677952 10:38:48.619 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] card-piv.c:1072:piv_get_data: returning with: -1204 (Unsupported INS byte in APDU)
P:16153; T:0x4422677952 10:38:48.619 [tokend] card-piv.c:2868:piv_find_discovery: returning with: -1204 (Unsupported INS byte in APDU)
P:16153; T:0x4422677952 10:38:48.619 [tokend] card-piv.c:3875:piv_card_reader_lock_obtained: PIV_MATCH piv_find_discovery card->type:14004 r:-1204
P:16153; T:0x4422677952 10:38:48.619 [tokend] card-piv.c:865:piv_select_aid: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] apdu.c:546:sc_transmit_apdu: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] card.c:477:sc_lock: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.619 [tokend] apdu.c:513:sc_transmit: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] apdu.c:363:sc_single_transmit: called
P:16153; T:0x4422677952 10:38:48.619 [tokend] apdu.c:370:sc_single_transmit: CLA:0, INS:A4, P1:4, P2:0, data(9) 0x102e8a420
P:16153; T:0x4422677952 10:38:48.619 [tokend] reader-pcsc.c:293:pcsc_transmit: reader 'Yubico Yubikey 4 OTP+U2F+CCID'
P:16153; T:0x4422677952 10:38:48.620 [tokend] reader-pcsc.c:294:pcsc_transmit:
Outgoing APDU (15 bytes):
00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00 ...............
P:16153; T:0x4422677952 10:38:48.620 [tokend] reader-pcsc.c:215:pcsc_internal_transmit: called
P:16153; T:0x4422677952 10:38:48.621 [tokend] reader-pcsc.c:303:pcsc_transmit:
Incoming APDU (21 bytes):
61 11 4F 06 00 00 10 00 01 00 79 07 4F 05 A0 00 a.O.......y.O...
00 03 08 90 00                                  .....
P:16153; T:0x4422677952 10:38:48.621 [tokend] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.621 [tokend] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.621 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:48.621 [tokend] card-piv.c:881:piv_select_aid: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.621 [tokend] card-piv.c:3881:piv_card_reader_lock_obtained: PIV_MATCH piv_select_aid card->type:14004 r:0
P:16153; T:0x4422677952 10:38:48.621 [tokend] card-piv.c:3884:piv_card_reader_lock_obtained: priv->logged_in:0 priv->card_issues:00000110
P:16153; T:0x4422677952 10:38:48.621 [tokend] card-piv.c:3901:piv_card_reader_lock_obtained: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.621 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.621 [tokend] card-piv.c:599:piv_general_io: called
P:16153; T:0x4422677952 10:38:48.621 [tokend] card.c:477:sc_lock: called
P:16153; T:0x4422677952 10:38:48.621 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:48.621 [tokend] apdu.c:546:sc_transmit_apdu: called
P:16153; T:0x4422677952 10:38:48.621 [tokend] card.c:477:sc_lock: called
P:16153; T:0x4422677952 10:38:48.621 [tokend] card.c:517:sc_lock: returning with: 0 (Success)
. . . . .

getting correctly to

. . . . .
Incoming APDU (10 bytes):
A1 19 34 96 3D 55 AE BD 90 00 ..4.=U....
P:16153; T:0x4422677952 10:38:49.271 [tokend] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:49.271 [tokend] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:16153; T:0x4422677952 10:38:49.271 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:49.271 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:49.271 [tokend] card-piv.c:731:piv_general_io: returning with: 264
P:16153; T:0x4422677952 10:38:49.271 [tokend] card-piv.c:2499:piv_validate_general_authentication: returning with: 256
P:16153; T:0x4422677952 10:38:49.271 [tokend] card-piv.c:2576:piv_compute_signature: returning with: 256
P:16153; T:0x4422677952 10:38:49.271 [tokend] sec.c:63:sc_compute_signature: returning with: 256
P:16153; T:0x4422677952 10:38:49.271 [tokend] card.c:527:sc_unlock: called
P:16153; T:0x4422677952 10:38:49.271 [tokend] reader-pcsc.c:709:pcsc_unlock: called
P:16153; T:0x4422677952 10:38:49.271 [tokend] pkcs15-sec.c:725:sc_pkcs15_compute_signature: returning with: 256
P:16153; T:0x4422677952 10:38:49.271 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:218:generateSignature:   sc_pkcs15_compute_signature(): rv = 256
P:16153; T:0x4422677952 10:38:49.271 [tokend] /Users/ur20980/src/OpenSC/OpenSC.tokend/OpenSC/OpenSCKeyHandle.cpp:230:generateSignature:   Completed RSA signature, len=256

Complete log:
opensc-1725-log.txt

@frankmorgner
Copy link
Member

Good to finally have found the root cause of this problem! I think we need to generalize the above idea and to take care about possible endless loops. Could you please try the below diff instead?

diff --git a/src/libopensc/apdu.c b/src/libopensc/apdu.c
index d32d44040..f4101269e 100644
--- a/src/libopensc/apdu.c
+++ b/src/libopensc/apdu.c
@@ -619,9 +619,18 @@ int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu)
 			len -= plen;
 			buf += plen;
 		}
-	} else
+	} else {
 		/* transmit single APDU */
 		r = sc_transmit(card, apdu);
+	}
+
+	if (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
+		sc_invalidate_cache(card);
+		/* give card driver a chance to react on resets */
+		if (card->ops->card_reader_lock_obtained)
+			card->ops->card_reader_lock_obtained(card, 1);
+	}
+
 	/* all done => release lock */
 	if (sc_unlock(card) != SC_SUCCESS)
 		sc_log(card->ctx, "sc_unlock failed");
diff --git a/src/libopensc/reader-pcsc.c b/src/libopensc/reader-pcsc.c
index a058c71f7..819ca5266 100644
--- a/src/libopensc/reader-pcsc.c
+++ b/src/libopensc/reader-pcsc.c
@@ -132,6 +132,8 @@ struct pcsc_private_data {
 };
 
 static int pcsc_detect_card_presence(sc_reader_t *reader);
+static int pcsc_reconnect(sc_reader_t * reader, DWORD action);
+static int pcsc_connect(sc_reader_t *reader);
 
 static DWORD pcsc_reset_action(const char *str)
 {
@@ -244,6 +246,25 @@ static int pcsc_internal_transmit(sc_reader_t *reader,
 		switch (rv) {
 		case SCARD_W_REMOVED_CARD:
 			return SC_ERROR_CARD_REMOVED;
+		case SCARD_E_INVALID_HANDLE:
+		case SCARD_E_READER_UNAVAILABLE:
+			r = pcsc_connect(reader);
+			if (r != SC_SUCCESS) {
+				sc_log(reader->ctx, "pcsc_connect failed (%d)",
+						r);
+				return r;
+			}
+			/* return failure so that upper layers will be notified */
+			return SC_ERROR_READER_REATTACHED;
+		case SCARD_W_RESET_CARD:
+			r = pcsc_reconnect(reader, SCARD_LEAVE_CARD);
+			if (r != SC_SUCCESS) {
+				sc_log(reader->ctx,
+						"pcsc_reconnect failed (%d)", r);
+				return r;
+			}
+			/* return failure so that upper layers will be notified */
+			return SC_ERROR_CARD_RESET;
 		default:
 			/* Translate strange errors from card removal to a proper return code */
 			pcsc_detect_card_presence(reader);

@mouse07410
Copy link
Contributor Author

Good to finally have found the root cause of this problem!

I really hope so.

I still have one machine that belongs to somebody else, whose owner was one of those complaining, and I haven't heard back from him yet. I'll ask again.

I think we need to generalize the above idea and to take care about possible endless loops.

Sure.

Could you please try the below diff instead?

Re-building as we speak. Will report here as soon as I get something.

And thank you for helping with this!

@mouse07410
Copy link
Contributor Author

mouse07410 commented Jul 26, 2019

ersions/Current/Headers -O2 -g -Wall -Wextra -Wno-unused-parameter -Werror -c reader-openct.c -o libopensc_la-reader-openct.o >/dev/null 2>&1
reader-pcsc.c:251:4: error: use of undeclared identifier 'r'
                        r = pcsc_connect(reader);
                        ^
reader-pcsc.c:252:8: error: use of undeclared identifier 'r'
                        if (r != SC_SUCCESS) {
                            ^
reader-pcsc.c:254:7: error: use of undeclared identifier 'r'
                                                r);
                                                ^
reader-pcsc.c:255:12: error: use of undeclared identifier 'r'
                                return r;
                                       ^
reader-pcsc.c:260:4: error: use of undeclared identifier 'r'
                        r = pcsc_reconnect(reader, SCARD_LEAVE_CARD);
                        ^
reader-pcsc.c:261:8: error: use of undeclared identifier 'r'
                        if (r != SC_SUCCESS) {
                            ^
reader-pcsc.c:263:37: error: use of undeclared identifier 'r'
                                                "pcsc_reconnect failed (%d)", r);
                                                                              ^
reader-pcsc.c:264:12: error: use of undeclared identifier 'r'
                                return r;
                                       ^
8 errors generated.

Should r be rv?

Update

Of course, it should be rv. Fixed that, and the resulting code seems to be working fine.

I'm still waiting for the confirmation from the "sufferers". ;-)

@frankmorgner
Copy link
Member

Sorry, I was a bit too quick. See #1752 for a fix

@mouse07410
Copy link
Contributor Author

No problem. Replaced the above patch with #1752, and it seems to work.
I still need to hear from the folks who complained, though. :-(

@mouse07410
Copy link
Contributor Author

mouse07410 commented Aug 2, 2019

Perhaps we can merge #1752 ...?

@frankmorgner
Copy link
Member

@mouse07410 you wanted to confirm from your users if the change really fixes the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants