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

OpenSSH: Sign and Send Public Key Failed #1460

Closed
mtoconno opened this issue Aug 27, 2018 · 46 comments
Closed

OpenSSH: Sign and Send Public Key Failed #1460

mtoconno opened this issue Aug 27, 2018 · 46 comments

Comments

@mtoconno
Copy link

mtoconno commented Aug 27, 2018

When trying to ssh using a US PIV card in OpenSC version 0.18 and 0.19 (nightly 2018-08-27_b5a6f9aa) on Mac OSX High Sierra, I receive the following error:

debug1: Offering public key: RSA SHA256:...... /usr/local/lib/opensc-pkcs11.so
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug2: input_userauth_pk_ok: fp SHA256:......
debug3: sign_and_send_pubkey: RSA SHA256:.....
sign_and_send_pubkey: signing failed: agent refused operation

However, when logging in using 0.16, I am able to login, and receive the following verbose output:

debug1: Offering public key: RSA SHA256:.... /usr/local/lib/opensc-pkcs11.so
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug2: input_userauth_pk_ok: fp SHA256:....
debug3: sign_and_send_pubkey: RSA SHA256:....
debug3: send packet: type 50
debug3: receive packet: type 52
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).

So it seems that there was some change to the code base that changed the sign_and_send_pubkey function between 0.16 and 0.18 that broke the functionality for US PIV cards. Can anyone shed some light on this?

Thanks!

@dengert
Copy link
Member

dengert commented Aug 27, 2018

What version of OpenSSH?

Is your card a real PIV card, a dual mode CAC/PIV card or some other PIV like card/device?

Which certificate/key are you using? "X.509 Certificate for PIV Authentication" or "X.509 Certificate for Digital Signature". ("X.509 Certificate for Digital Signature" requires PIN Always, i.e. pin must be entered every time the key is used, or must be cached and opensc.conf set to tell it to ignore user_consent)

Did anything else change other then the version of OpenSC?

A pkcs11-spy trace and opensc-debug.log would be helpful and would show what type of card and which certificate/key was being used.

https://github.com/OpenSC/OpenSC/wiki/Using-OpenSC

@mtoconno
Copy link
Author

mtoconno commented Aug 27, 2018

The version of OpenSSH I'm using:
OpenSSH_7.6p1, LibreSSL 2.6.2 (has not changed)

The card I'm using is a real PIV card, and it always requires a PIN to be entered.

Here's some additional info I was able to pull from the card:

Running OPENSC_DEBUG=9 pkcs11-tool --list-slots provides this via STDOUT:

Slot 0 (0x0): Identiv SCR3500 A Contact Reader
token label : My Name
token manufacturer : piv_II
token model : PKCS#15 emulated
token flags : login required, rng, token initialized, PIN initialized
hardware version : 0.0
firmware version : 0.0
serial num : #######
pin min/max : 4/8

Running this command:
OPENSC_DEBUG=9 pkcs11-tool -v -s -m RSA-PKCS -i test.file -o test.output

I get about 5000 lines on STDERR, Here's the last 60 or so. If you need some other part, let me know, and I can upload that as well.

0x7fffb6c0f380 14:50:20.191 [opensc-pkcs11] apdu.c:390:sc_single_transmit: returning with: 0 (Success)
0x7fffb6c0f380 14:50:20.191 [opensc-pkcs11] apdu.c:543:sc_transmit: returning with: 0 (Success)
0x7fffb6c0f380 14:50:20.191 [opensc-pkcs11] iso7816.c:128:iso7816_check_sw: Security status not satisfied
0x7fffb6c0f380 14:50:20.191 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:50:20.191 [opensc-pkcs11] card-piv.c:547:piv_general_io: DEE r=-1211 apdu.resplen=4096 sw1=0 0 sw2=00
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card-piv.c:549:piv_general_io: Transmit failed
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card-piv.c:605:piv_general_io: returning with: -1211 (Security status not satisfied)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card-piv.c:2389:piv_validate_general_authentication: returning wit h: -1211 (Security status not satisfied)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card-piv.c:2468:piv_compute_signature: returning with: -1211 (Security status not satisfied)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] sec.c:63:sc_compute_signature: returning with: -1211 (Security status not satisfied)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs15-pin.c:791:sc_pkcs15_pincache_revalidate: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs15-sec.c:458:sc_pkcs15_compute_signature: use_key() failed: -1 211 (Security status not satisfied)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] reader-pcsc.c:663:pcsc_unlock: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] framework-pkcs15.c:3853:pkcs15_prkey_sign: Sign complete. Result - 1211.
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: -12 11 (Security status not satisfied)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] mechanism.c:462:sc_pkcs11_signature_final: returning with: 257
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] mechanism.c:327:sc_pkcs11_sign_final: returning with: 257
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs11-global.c:315:C_Finalize: C_Finalize()
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] ctx.c:881:sc_cancel: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] reader-pcsc.c:713:pcsc_cancel: called
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] slot.c:180:card_removed: Identiv SCR3500 A Contact Reader: card removed
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] slot.c:480:slot_token_removed: slot_token_removed(0x0)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs11-session.c:140:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x0) 1
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs11-session.c:109:sc_pkcs11_close_session: real C_CloseSession(0x7fe8a6403970)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] framework-pkcs15.c:1499:pkcs15_release_token: pkcs15_release_token() not implemented
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] slot.c:480:slot_token_removed: slot_token_removed(0x1)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs11-session.c:140:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x1) 0
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] slot.c:480:slot_token_removed: slot_token_removed(0x2)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs11-session.c:140:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x2) 0
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] slot.c:480:slot_token_removed: slot_token_removed(0x3)
0x7fffb6c0f380 14:50:20.192 [opensc-pkcs11] pkcs11-session.c:140:sc_pkcs11_close_all_sessions: real C_CloseAllSessions(0x3) 0
0x7fffb6c0f380 14:50:20.193 [opensc-pkcs11] sc.c:275:sc_detect_card_presence: called
0x7fffb6c0f380 14:50:20.193 [opensc-pkcs11] reader-pcsc.c:412:pcsc_detect_card_presence: called
0x7fffb6c0f380 14:50:20.193 [opensc-pkcs11] reader-pcsc.c:320:refresh_attributes: Identiv SCR3500 A Contact Reader check
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] reader-pcsc.c:340:refresh_attributes: returning with: 0 (Success)
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] reader-pcsc.c:417:pcsc_detect_card_presence: returning with: 5
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] sc.c:280:sc_detect_card_presence: returning with: 5
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] pkcs15.c:1267:sc_pkcs15_unbind: called
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] pkcs15-pin.c:838:sc_pkcs15_pincache_clear: called
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: 0 (Success)
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] card.c:356:sc_disconnect_card: called
0x7fffb6c0f380 14:50:20.194 [opensc-pkcs11] card-piv.c:2922:piv_finish: called
0x7fffb6c0f380 14:50:20 0x7fffb6c0f380 14:50:20.195 [opensc-pkcs11] card-piv.c:2935:piv_finish:
DEE freeing #0, 0x00 0x0:0 0x0:0
...
...
DEE freeing until #55
...
...
0x7fffb6c0f380 14:50:20.195 [opensc-pkcs11] reader-pcsc.c:598:pcsc_disconnect: Identiv SCR3500 A Contact Reader:SCardDisconnect returned: 0x00000000
0x7fffb6c0f380 14:50:20.195 [opensc-pkcs11] card.c:378:sc_disconnect_card: returning with: 0 (Success)
0x7fffb6c0f380 14:50:20.195 [opensc-pkcs11] ctx.c:906:sc_release_context: called
0x7fffb6c0f380 14:50:20.195 [opensc-pkcs11] reader-pcsc.c:900:pcsc_finish: called
error: PKCS11 function C_SignFinal failed: rv = CKR_USER_NOT_LOGGED_IN (0x101)
Aborting.

Thanks again

@dengert
Copy link
Member

dengert commented Aug 27, 2018

Al the above shows is you did not provide the PIN to pkcs11-tool. It does not show the ATR of the card, or which certificate/key was selected.

Are there any other applications running, including tokend that might also be accessing the card?
Depending how what they do, and how the opensc.conf file is setup, one application could enter the
pin i.e. login the user into teh card, and then other applications could use that fact. This may be the way you where running OpenSSH. But if something changed, some application could cause the login state to be lost. ( Security status not satisfied).

P.S. I am not a MacOS person.

@mtoconno
Copy link
Author

mtoconno commented Aug 27, 2018

That's odd, because it did ask me my PIN and I entered it.

I don't have any other applications running or accessing it. My colleague and I are both running identical laptops with High Sierra, with the only difference is he has 0.16 installed, and I've tried 0.18 and the 0.19 nightly. Yet, the PIV works on his machine.

I also found out that another colleague of mine running Sierra and 0.18 and I'm able to use my PIV on his machine.

I think my problem may be OS X related. I just find the different configurations that work to be very strange.

UPDATE:
Looking through the error log, I just saw this:

0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:547:piv_general_io: DEE r=0 apdu.resplen=0 sw1=6d sw2=00
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] iso7816.c:128:iso7816_check_sw: Instruction code not supported or invalid
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:558:piv_general_io: Card returned error
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:605:piv_general_io: returning with: -1204 (Unsupported INS byte in APDU)
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:982:piv_get_data: returning with: -1204 (Unsupported INS byte in APDU)
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:1064:piv_get_cached_data: returning with: -1204 (Unsupported INS byte in APDU)
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:2653:piv_process_discovery: returning with: -1204 (Unsupported INS byte in APDU)
0x7fffb6c0f380 14:43:51.569 [opensc-pkcs11] card-piv.c:2684:piv_find_discovery: returning with: -1204 (Unsupported INS byte in APDU)

and this:

fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:547:piv_general_io: DEE r=0 apdu.resplen=0 sw1=6a sw2=80
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] iso7816.c:128:iso7816_check_sw: Incorrect parameters in the data field
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:558:piv_general_io: Card returned error
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:605:piv_general_io: returning with: -1205 (Incorrect parameters in APDU)
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:982:piv_get_data: returning with: -1205 (Incorrect parameters in APDU)
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:1064:piv_get_cached_data: returning with: -1205 (Incorrect parameters in APDU)
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:2653:piv_process_discovery: returning with: -1205 (Incorrect parameters in APDU)
0x7fffb6c0f380 14:43:51.647 [opensc-pkcs11] card-piv.c:2684:piv_find_discovery: returning with: -1205 (Incorrect pa
rameters in APDU)

@dengert
Copy link
Member

dengert commented Aug 27, 2018

Can you send the full opensc-debug.log? You can obfuscate the PIN (look for it in apdu sending as hex digits followed by FFs) and maybe other information like certificate content and or chuid content.

When did you start seeing problems on your computer?

You tried 0.18 nightly. Did you try the released 0.18?
0.19 has not yet been released, so any input on it is welcome.

@mouse07410
Copy link
Contributor

I have a problem with the current master that may be related. It manifests itself with Pulse Secure VPN. The current master does not pick the cert, tokend log showing churning in calling getAcl() that every time returns 1 entry. After some time more than a minute or would find the correct cert and prompt for a PIN - but by then the server time-outs. It worked on Sierra, and with older OpenSC. If be happy to email the OpenSC debug log, if it's ok (and maybe Pulse Secure log, if I get permission).

P.S. Released 0.18 works on one machine, didn't work on others.

@frankmorgner frankmorgner changed the title Sign and Send Public Key Failed OpenSSH: Sign and Send Public Key Failed Aug 29, 2018
@mtoconno
Copy link
Author

@dengert, sorry for taking to song in replying. I've attached the filed like you asked. I've redacted any identifying info (I think), and replaced it with ".......". Thanks for taking the time to look at the file.

Just so you know, this is output when I run:
echo test > t2048.dat

OPENSC_DEBUG=9 pkcs11-tool -v -s -m RSA-PKCS -i t2048.dat -o t2048.dat.sig 2> log

t2048.dat.sig is never created.

I've tried running this on .18 stable(same error), and ran the command on 0.19 (nightly 2018-08-27_b5a6f9aa). 0.19 works fine on my yubikey. The only problem I'm having is with my PIV.
opensclog.txt

@frankmorgner
Copy link
Member

Between the PIN verification (search for logged_in=1 in the log) and the signature creation (which returns Security status not satisfied), there are two APDUs:

Outgoing APDU (14 bytes):
00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 .¤... ........
[opensc-pkcs11] reader-pcsc.c:213:pcsc_internal_transmit: called
[opensc-pkcs11] reader-pcsc.c:294:pcsc_transmit: 
Incoming APDU (2 bytes):
61 18 a.
...
Outgoing APDU (5 bytes):
00 C0 00 00 18 .À...
[opensc-pkcs11] reader-pcsc.c:213:pcsc_internal_transmit: called
[opensc-pkcs11] reader-pcsc.c:294:pcsc_transmit: 
Incoming APDU (26 bytes):
............

If I remember correct, then the signature creation should immediately follow the PIN verification, right?

@mouse07410
Copy link
Contributor

Right.

@dengert
Copy link
Member

dengert commented Aug 30, 2018

That would be true for the 9C key that needs the PIN_Always/user_consent/CKA_ALWAYS_AUTHENTICATE but this sign operation is with th 9A key and that should not be enforced by the card:

[opensc-pkcs11] pkcs11-object.c:237:C_GetAttributeValue: Object 140585116668224: CKA_ALWAYS_AUTHENTICATE = FALSE

[opensc-pkcs11] apdu.c:378:sc_single_transmit: CLA:10, INS:87, P1:7, P2:**9A**, data(255) 0x7ffeee698d80

The two extra commands are the SELECT_AID and get response.

NIST 800-73 says selecting the AID will not changes the security status. But this appears to be a dual CAC/PIV card issued be a gov agency.

The user says this card works on other Macs with 0.18.0, but not on his. So it does not look like the card is different.

I suspect there is some other application is running that did CAC operation causes the PIV security status to be reset.
I don't see any strange SCard return codes which could indicate a reset or a power down of the card.

The sign operation is tried twice, and use_pin_cache=1:

[opensc-pkcs11] pkcs15.c:1215:sc_pkcs15_bind: PKCS#15 options: use_file_cache=0 use_pin_cache=1 pin_cache_counter=10 pin_cache_ignore_user_consent=0

I see: [opensc-pkcs11] pkcs15-pin.c:791:sc_pkcs15_pincache_revalidate: called
But don't see any attempt to try and use the cached pin (or if it cached or not.)

@mtoconno are the readers the same on the machine were it works and your where it fails?
Could the reader be powering down the card, or MacOS powering down the reader?

@mtoconno
Copy link
Author

mtoconno commented Aug 30, 2018

@dengert, after some further investigation, I found that it does NOT work with 0.18 on other computers. My coworker had downgraded back to 0.16 (he checked his openSC version thru a Mac GUI which caused the confusion). So to be clear, the only release that I know works is 0.16. Sorry for the confusion.

Regarding the reader: we've used the same reader on all the machines, so I don't think it's the reader.

After looking at the logs on my computer, and a computer with 0.16 installed, I noticed a few areas where there are some pretty big differences:

----opensc .19 nightly----
3899 [opensc-pkcs11] apdu.c:390:sc_single_transmit: returning with: 0 (Success)
3900 [opensc-pkcs11] apdu.c:543:sc_transmit: returning with: 0 (Success)
3901 [opensc-pkcs11] card.c:465:sc_unlock: called
3902 [opensc-pkcs11] iso7816.c:123:iso7816_check_sw: PIN not verified (remaining tries: 7)
3903 [opensc-pkcs11] card-piv.c:3338:piv_check_protected_objects: called
3904 [opensc-pkcs11] card-piv.c:922:piv_get_data: called
3905 [opensc-pkcs11] card-piv.c:923:piv_get_data: #4
3906 [opensc-pkcs11] card.c:415:sc_lock: called
3907 [opensc-pkcs11] card.c:455:sc_lock: returning with: 0 (Success)
3908 [opensc-pkcs11] card-piv.c:969:piv_get_data: buffer for #4 *buf=0x0x7ffeee698e40 len=8
3909 [opensc-pkcs11] card-piv.c:494:piv_general_io: called
3910 [opensc-pkcs11] card-piv.c:499:piv_general_io: cb 3f ff 5 : 255 256
3911 [opensc-pkcs11] card.c:415:sc_lock: called
3912 [opensc-pkcs11] card.c:455:sc_lock: returning with: 0 (Success)
3913 [opensc-pkcs11] card-piv.c:540:piv_general_io: calling sc_transmit_apdu flags=1 le=8, resplen=8, resp=0x7ffeee698e40
3914 [opensc-pkcs11] apdu.c:554:sc_transmit_apdu: called
3915 [opensc-pkcs11] card.c:415:sc_lock: called
3916 [opensc-pkcs11] card.c:455:sc_lock: returning with: 0 (Success)
3917 [opensc-pkcs11] apdu.c:521:sc_transmit: called
3918 [opensc-pkcs11] apdu.c:371:sc_single_transmit: called
3919 [opensc-pkcs11] apdu.c:378:sc_single_transmit: CLA:0, INS:CB, P1:3F, P2:FF, data(5) 0x7ffeee698dc8
3920 [opensc-pkcs11] reader-pcsc.c:284:pcsc_transmit: reader 'Identiv SCR3500 A Contact Reader'
3921 [opensc-pkcs11] reader-pcsc.c:285:pcsc_transmit:
3922 Outgoing APDU (10 bytes):
3923 00 CB 3F FF 05 5C 03 5F C1 09 .Ë?ÿ.\._Á.
-------
----opensc .16----
4167 [opensc-pkcs11] apdu.c:386:sc_single_transmit: returning with: 0 (Success)
4168 [opensc-pkcs11] apdu.c:539:sc_transmit: returning with: 0 (Success)
4169 [opensc-pkcs11] card.c:434:sc_unlock: called
4170 [opensc-pkcs11] reader-pcsc.c:587:pcsc_unlock: called
4171 [opensc-pkcs11] iso7816.c:116:iso7816_check_sw: Verification failed (remaining tries: 7)
4172 [opensc-pkcs11] sec.c:206:sc_pin_cmd: returning with: 0 (Success)
4173 [opensc-pkcs11] framework-pkcs15.c:527:C_GetTokenInfo: C_GetTokenInfo(0) returns 0x0
4174 [opensc-pkcs11] pkcs11-session.c:263:C_Login: C_Login(0x7fa5e0700a10, 1)
4175 [opensc-pkcs11] pkcs11-session.c:285:C_Login: C_Login() slot->login_user <userid>
4176 [opensc-pkcs11] pkcs11-session.c:296:C_Login: C_Login() userType 1
4177 [opensc-pkcs11] framework-pkcs15.c:1386:pkcs15_login: pkcs15-login: userType 0x1, PIN length 6
4178 [opensc-pkcs11] pkcs15-pin.c:295:sc_pkcs15_verify_pin: called
4179 [opensc-pkcs11] pkcs15-pin.c:296:sc_pkcs15_verify_pin: PIN(type:0;method:1;len:)
4180 [opensc-pkcs11] card.c:394:sc_lock: called
4181 [opensc-pkcs11] reader-pcsc.c:547:pcsc_lock: called
4182 [opensc-pkcs11] sec.c:159:sc_pin_cmd: called
4183 [opensc-pkcs11] apdu.c:550:sc_transmit_apdu: called
4184 [opensc-pkcs11] card.c:394:sc_lock: called
4185 [opensc-pkcs11] apdu.c:517:sc_transmit: called
4186 [opensc-pkcs11] apdu.c:371:sc_single_transmit: called
4187 [opensc-pkcs11] apdu.c:376:sc_single_transmit: CLA:0, INS:20, P1:0, P2:80, data(8) 0x7ffee00de8f0
4188 [opensc-pkcs11] reader-pcsc.c:269:pcsc_transmit: reader 'Identiv SCR3500 A Contact Reader'
4189 [opensc-pkcs11] reader-pcsc.c:270:pcsc_transmit:
4190 Outgoing APDU (13 bytes):
4191 <HEX FOLLOWED BY FF FF>  ...<PIN>..
------

Above, it looks like the two versions diverge after check_sw. 0.16 does some pin verification, whereas 0.19 tries to get some data? The outgoing APDU for 0.19 gives a random string, whereas the outgoing APDU for 0.16 is the user pin.

As well as this:

----openSC 0.19 nightly-----
4292 [opensc-pkcs11] framework-pkcs15.c:3749:pkcs15_prkey_sign: Initiating signing operation, mechanism 0x1.
4293 [opensc-pkcs11] card.c:415:sc_lock: called
4294 [opensc-pkcs11] reader-pcsc.c:613:pcsc_lock: called
4295 [opensc-pkcs11] card-piv.c:3563:piv_card_reader_lock_obtained: called
4296 [opensc-pkcs11] card-piv.c:733:piv_select_aid: called
4297 [opensc-pkcs11] card-piv.c:736:piv_select_aid: Got args: aid=0x101ae1004, aidlen=9, response=0x7ffeee69a810, responselen=256
4298 [opensc-pkcs11] apdu.c:554:sc_transmit_apdu: called
4299 [opensc-pkcs11] card.c:415:sc_lock: called
------
----openSC 0.16----
4346 [opensc-pkcs11] framework-pkcs15.c:3494:pkcs15_prkey_sign: Initiating signing operation, mechanism 0x1.
4347 [opensc-pkcs11] card.c:394:sc_lock: called
4348 [opensc-pkcs11] reader-pcsc.c:547:pcsc_lock: called
4349 [opensc-pkcs11] framework-pkcs15.c:3555:pkcs15_prkey_sign: Selected flags 12. Now computing signature for 5 bytes. 512 bytes reserved.
4350 [opensc-pkcs11] pkcs15-sec.c:314:sc_pkcs15_compute_signature: called
4351 [opensc-pkcs11] pkcs15-sec.c:362:sc_pkcs15_compute_signature: supported algorithm flags 0x1, private key usage 0x2E
4352 [opensc-pkcs11] padding.c:283:sc_get_encoding_flags: called
4353 [opensc-pkcs11] padding.c:287:sc_get_encoding_flags: iFlags 0x12, card capabilities 0x1
4354 [opensc-pkcs11] padding.c:316:sc_get_encoding_flags: pad flags 0x12, secure algorithm flags 0x0
4355 [opensc-pkcs11] padding.c:317:sc_get_encoding_flags: returning with: 0 (Success)
4356 [opensc-pkcs11] pkcs15-sec.c:413:sc_pkcs15_compute_signature: DEE flags:0x00000012 alg_info->flags:0x00000001 pad:0x00000012 sec:0x00000000
4357 [opensc-pkcs11] padding.c:242:sc_pkcs1_encode: called
4358 [opensc-pkcs11] padding.c:246:sc_pkcs1_encode: hash algorithm 0x10, pad algorithm 0x2
4359 [opensc-pkcs11] padding.c:269:sc_pkcs1_encode: returning with: 0 (Success)
4360 [opensc-pkcs11] card.c:394:sc_lock: called
------

The two blocks above are at the same part, but whereas 0.16 computes a signature and does some encoding stuff before sc_lock, 0.19 calls SELECT_AID.

Later, you can see that openSC 0.16 correctly reads the file(which only contains the word 'test'), whereas 0.19 does not.

----openSC 0.19----
312 [opensc-pkcs11] apdu.c:390:sc_single_transmit: returning with: 0 (Success)
4313 [opensc-pkcs11] apdu.c:434:sc_get_response: called
4314 [opensc-pkcs11] apdu.c:554:sc_transmit_apdu: called
4315 [opensc-pkcs11] card.c:415:sc_lock: called
4316 [opensc-pkcs11] card.c:455:sc_lock: returning with: 0 (Success)
4317 [opensc-pkcs11] apdu.c:521:sc_transmit: called
4318 [opensc-pkcs11] apdu.c:371:sc_single_transmit: called
4319 [opensc-pkcs11] apdu.c:378:sc_single_transmit: CLA:0, INS:C0, P1:0, P2:0, data(0) 0x0
4320 [opensc-pkcs11] reader-pcsc.c:284:pcsc_transmit: reader 'Identiv SCR3500 A Contact Reader'
4321 [opensc-pkcs11] reader-pcsc.c:285:pcsc_transmit:
4322 Outgoing APDU (5 bytes):
4323 00 C0 00 00 18 .À...
-------
---openSC 0.16----
4402 [opensc-pkcs11] apdu.c:386:sc_single_transmit: returning with: 0 (Success)
4403 [opensc-pkcs11] apdu.c:539:sc_transmit: returning with: 0 (Success)
4404 [opensc-pkcs11] apdu.c:517:sc_transmit: called
4405 [opensc-pkcs11] apdu.c:371:sc_single_transmit: called
4406 [opensc-pkcs11] apdu.c:376:sc_single_transmit: CLA:0, INS:87, P1:7, P2:9A, data(11) 0x7ffee00dedef
4407 [opensc-pkcs11] reader-pcsc.c:269:pcsc_transmit: reader 'Identiv SCR3500 A Contact Reader'
4408 [opensc-pkcs11] reader-pcsc.c:270:pcsc_transmit:
4409 Outgoing APDU (16 bytes):
4410 00 87 07 9A 0B FF FF FF FF FF 00 74 65 73 74 0A ...........test.
-------

There's a bunch more divergences, most of which are similar to the ones I posted above. If you think it would help you, I can post the log generated by 0.16

@dengert
Copy link
Member

dengert commented Aug 31, 2018

Yes the 0.16 debug log would help.

The keyUsage extension in the certificate might also give a clue.

Another possibility:
If this is a dual CAC/PIV, it could be the the card is enforcing the PIN_Always/user_consent/CKA_ALWAYS_AUTHENTICATE on the card, because the 9A key (and cert) are being mapped to what the CAC card calls the Sign Key and cert. (This does not meet the
NIST documentation that says PIN_Always is only on the 9C key.

If you want to test this out, you can modify libopensc/pkcs15-piv.c line 510:
"", 0x9A, "01", SC_PKCS15_CO_FLAG_PRIVATE, 0},
and change the last 0 to a 1;
See line 518 which is the 9C "SIGN key"

If this works, we can work through how to identify such a card, and a CI_* in card-piv.c
to identify the issue. (Or add a opensc.conf flag based on the ATR.)

I note that the card returns a 11 byte piv AID but the PIV cards I have seen only return 9 bytes.
This might help identify the card.

@mtoconno
Copy link
Author

@dengert I've attached the 0.16 debug log for you.

I tried to compile opensc on my mac, but I couldn't figure it out. I checked the project wiki and mailing list to see if I could figure it out, but no luck.

@frankmorgner, if u could post some instructions on how to compile on OS X, I'd be happy to try the code modification proposed in the previous post.

opensc16.txt

@dengert
Copy link
Member

dengert commented Sep 1, 2018

@mtoconno have you tried using the CAC driver?

@dengert
Copy link
Member

dengert commented Sep 2, 2018

Can you try running this test script
piv-check-state.sh.txt
Edit it to set path as needed and set permissions, it is a bash script.
Run first time with only PIN, to be sure it the PIN is accepted, response will be 9000

then run with PIN and basic as parameters, then run with PIN and basic11.

Note output has your PIN, so before posting responses, set PIN to something like xx xx xx xx xx

The script will show the login state of the card after selected commands to see it matches NIST 800-73 requirements.

There is code in the PIV driver files card-piv.c and pkcs15-piv.c to use the keyusage from certificates if the card is not a gov issued card. This may need to be extended to gov issued cards like yours. But I would first need to know the keyUsage from your certificates.

PIV card ID=01 uses the 9A key, and ID=02 uses the 9C key. 9C requires PIN ALWAYS and enforced by card. I suspect that on your card the 9A key also require PIN ALWAYS but that would be non-standard by NIST 800-73.

pkcs15-tool -r 01 | openssl x509 -text -noout From a NIST Demo card #\2 it has these lines:

            X509v3 Key Usage: critical
                Digital Signature

pkcs15-tool -r 02 | openssl x509 -text -noout has:

            X509v3 Key Usage: critical
                Digital Signature, Non Repudiation

I am looking to see what certificate of yours has Non Repudiation.

@mouse07410
Copy link
Contributor

If his card is gov't-issued (like CAC), 9A can not have "ALWAYS_AUTHENTICATE" set (aka it can not require "PIN_ALWAYS").

@dengert
Copy link
Member

dengert commented Sep 2, 2018 via email

@mouse07410
Copy link
Contributor

Yeah I hear you. I've heck of a time trying to get CAC (dual) to work with the latest Firefox and OpenSC. Firefox insists on choosing the cert automatically (which for web auth inevitably is 9A), and DOD Enterprise Mail rejects it (because it requires 9C).

Of course there's a setting in Firefox, and of course whenever I set it to "always ask" it automatically switches back to "choose automatically"... Oh well.

@dengert
Copy link
Member

dengert commented Sep 3, 2018 via email

@mouse07410
Copy link
Contributor

Let me experiment after the Holiday and get back to you.

@dengert
Copy link
Member

dengert commented Sep 3, 2018

Thanks.
Look up the ATR of your CAC/PIV card and the ATR found in opensc16.txt

There are many PIV and CAC/PIV card manufactures, each has written their own PIV applet.

@dengert
Copy link
Member

dengert commented Sep 3, 2018

@mtoconno
As a temporary solution, add or uncommenting in opensc.conf
pin_cache_ignore_user_consent = true;
use_pin_caching = true; (This is the default)

Try with 0.18.0. or 0.19.0. With 0.19.0 you will need to add the framework pkcs15 { ... } if not in the opensc.conf. See the man pages or opensc.conf. example

I say "temporary" because it is still not clear why the security status is lost and I would like to resolve this issue. Current thought is the 9A key is enforcing the PIN_ALWAYS policy or the SELECT_AID is resetting the login state on the card. Both violate the PIV specifications.

pkcs15-pin.c sc_pkcs15_pincache_add lines 751-771:
https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/pkcs15-pin.c#L751
will not cache the PIN if any object is protected by that PIN, unless pin_cache_ignore_user_consent is true, then it will cache the pin. (PIV 9C key is always protected by the PIN, so it will not cache the PIN.)

Line 764 in master, line 618 in 0.16.0 produces this on the log:
opensc-pkcs11] pkcs15-pin.c:618:sc_pkcs15_pincache_add: caching refused (user consent)

The above code helps enforce the PIN_ALWAYS policy of the card issuer by default.
But OpenSC provides a circumvention of the policy by using the pin_cache_ignore_user_consent in cases where the calling application or card has issues.

@mtoconno
Copy link
Author

mtoconno commented Sep 4, 2018

@dengert thanks for your help. I'll send you the output from the script you posted earlier and the result of changing the conf file tomorrow.

@mtoconno
Copy link
Author

mtoconno commented Sep 5, 2018

@dengert, so I added the two lines you suggested to my opensc.conf file, and it works. I'm able to sign a file, and successfully ssh. Thanks! (I'm running opensc 0.19)

Of note though, I did try running the script you provided without the original conf file, and the script returned the following:

Using reader with a card: Identiv SCR3500 A Contact Reader
Sending: 00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00
Received (SW1=0x90, SW2=0x00):
61 16 4F 0B A0 00 00 03 08 00 00 10 00 01 00 79 a.O............y
07 4F 05 A0 00 00 03 08 .O......
Sending: xx xx xx xx xx xx xx xx xx xx xx FF FF
Received (SW1=0x90, SW2=0x00)
return 0
If above does not show 9000 the pin may be wrong
Do not run any more tests. Too many failures may lock the PIN
If the pin is OK you can modify the script to comments out this first test

I think it probably does this because of the PIN caching issue you outlined above.

Going forward, should I tell my coworkers to modify their opensc.conf files, since this seems to be against the NIST standard, and an issue for a very small subset of users?

Also, just for my own understanding of the issue, the following table is what you're referring to regarding the PIN policy?
screen shot 2018-09-05 at 9 43 06 am

@dengert
Copy link
Member

dengert commented Sep 5, 2018

Good to hear it works for you. The table above says"9C" is "PIN Always" but not "9A"

Based on editing the script and running it with 2 parameters: Your PIN and basic, would show if the problem is caused by the 9A key having "PIN Always" or the SELECT PIV AID command is losing the login state. There is at least one other PIV like card where the SELECT PIV AID losees the login state despite NIST 800-73 says it should not.

Depending on if you use other types of card or need to use a PIN PAD reader, you could run with the changes you have already made to the opensc.conf as it applies to all cards, and does not solve the problem if you are using a PIN PAD reader.

@mtoconno
Copy link
Author

mtoconno commented Sep 5, 2018

Ok. Sorry, I misunderstood the output from the bash script (thought exit code should be 9000 initially). Here's the output after running basic:

  • opensc-tool -c default -s '00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00' -s PIN -s 00:20:00:80 -s '00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00' -s 00:20:00:80 -s '00 A4 04 00 0F D2 33 00 00 00 45 73 74 45 49 44 20 76 33 35' -s 00:20:00:80 -s '00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00' -s 00:20:00:80
    Using reader with a card: Identiv SCR3500 A Contact Reader
    Sending: 00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00
    Received (SW1=0x90, SW2=0x00):
    61 16 4F 0B A0 00 00 03 08 00 00 10 00 01 00 79 a.O............y
    07 4F 05 A0 00 00 03 08 .O......
    Sending: PIN
    Received (SW1=0x90, SW2=0x00)
    Sending: 00 20 00 80
    Received (SW1=0x90, SW2=0x00)
    Sending: 00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00
    Received (SW1=0x90, SW2=0x00):
    61 16 4F 0B A0 00 00 03 08 00 00 10 00 01 00 79 a.O............y
    07 4F 05 A0 00 00 03 08 .O......
    Sending: 00 20 00 80
    Received (SW1=0x63, SW2=0xC7)
    Sending: 00 A4 04 00 0F D2 33 00 00 00 45 73 74 45 49 44 20 76 33 35
    Received (SW1=0x6A, SW2=0x82)
    Sending: 00 20 00 80
    Received (SW1=0x63, SW2=0xC7)
    Sending: 00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00
    Received (SW1=0x90, SW2=0x00):
    61 16 4F 0B A0 00 00 03 08 00 00 10 00 01 00 79 a.O............y
    07 4F 05 A0 00 00 03 08 .O......
    Sending: 00 20 00 80
    Received (SW1=0x63, SW2=0xC7)<

@dengert
Copy link
Member

dengert commented Sep 5, 2018

With comments:

Sending: 00 20 00 80                      Check the login state
Received (SW1=0x90, SW2=0x00)  User is logged in  Good.

Sending: 00 A4 04 00 09 A0 00 00 03 08 00 00 10 00 00  Selecting PIV AID
Received (SW1=0x90, SW2=0x00):   PIV AID selected, and returned application property template:
61 16 4F 0B A0 00 00 03 08 00 00 10 00 01 00 79 a.O............y
07 4F 05 A0 00 00 03 08 .O......
Good so far. 

Sending: 00 20 00 80          Check the login state again
Received (SW1=0x63, SW2=0xC7)   User is not logged in, Not good, (and has 7 retries attempts) 

It looks like the card does not follow NIST 800-73-4 Part 2, Section "3.1.1 SELECT Card Command":
" If the currently selected application is the PIV Card Application when the SELECT command is given and the AID in the data field of the SELECT command is either the AID of the PIV Card Application or the right-truncated version thereof, then the PIV Card Application shall continue to be the currently selected card application and the setting of all security status indicators in the PIV Card Application shall be unchanged." (Older version of NIST 800-73 have similar wording.

@mouse07410
Copy link
Contributor

I wonder - is it a government PIV card?

@frankmorgner
Copy link
Member

What's the output of

opensc-tool --send-apdu 00A404C07A0000000030000
opensc-tool --send-apdu 00A404C07A0000001510000

If it is something like ... 90 00, this would indicate a Java Card, which could explain why the PIV applet looses its state. (This depends on the implementation, but loosing the state would be the obvious thing to do for someone not reading every note in the PIV standard.)

@dengert
Copy link
Member

dengert commented Sep 6, 2018

card-piv.c also checks for a specific card type based on the historical bytes that has the same problem.
See: URL in card-piv.c. It looks like a Dual/CAC card and may be Java based.
If found this card_issues flags are set:

3222                 case SC_CARD_TYPE_PIV_II_GI_DE:
3223                         priv->card_issues |= CI_VERIFY_LC0_FAIL
3224                                 | CI_PIV_AID_LOSE_STATE
3225                                 | CI_OTHER_AID_LOSE_STATE;;

So another way to deal with the card in this issue in 0.18.0 (and maybe 0.17.0) would be to add to opensc.conf:

       card_atr 3b:7d:96:00:00:80:31:80:65:b0:83:11:17:d6:83:00:90:00 {
                driver = "PIV-II";
                type = 14005;
       }

card-piv.c tries to avoid doing an SELECT AIDs after the first one. Other applications may still cause interference.

@mtoconno
Copy link
Author

mtoconno commented Sep 10, 2018

Sorry for the late response everyone.

@frankmorgner, the output I get from my PIV when running both commands is:

Using reader with a card: Identiv SCR3500 A Contact Reader
Invalid APDU: Invalid data

@mouse07410, yes its a government agency issued PIV card.

@dengert
Copy link
Member

dengert commented Sep 10, 2018

Are the above commands correct? The "C" does not look correct it should be "00" to meet
ISO 7816-4 "8.2.2.2 Application selection using AID as DF name"
"The card shall support a SELECT command with CLA INS P1 P2 set to '00A4 0400' for the first selection with a given and preferably complete application identifier in the command data field (see Table 39)."

I would also add --card-driver to not try and select a card driver that may send other commands and
add "00" to each for Le=00 to retrieve the "8.2.1.3 Application template"
Try these:

opensc-tool --card-driver default --send-apdu 00A40400007A000000003000000
opensc-tool --card-driver default --send-apdu 00A40400007A000000151000000

@dengert
Copy link
Member

dengert commented Sep 11, 2018

I added an extra 0 above too. Sorry. This may be a little more readable and run against an Oberther ID-One PIV

opensc-tool --card-driver default --send-apdu "00:A4:04:00:07:A0000000030000:00"
Using reader with a card: SCM Microsystems Inc. SCR 355 [CCID Interface] 00 00
Sending: 00 A4 04 00 07 A0 00 00 00 03 00 00 00 
Received (SW1=0x6A, SW2=0x82)

opensc-tool --card-driver default --send-apdu "00:A4:04:00:07:A0000001510000:00"
Using reader with a card: SCM Microsystems Inc. SCR 355 [CCID Interface] 00 00
Sending: 00 A4 04 00 07 A0 00 00 01 51 00 00 00 
Received (SW1=0x90, SW2=0x00):
6F 6D 84 07 A0 00 00 01 51 00 00 A5 62 73 2F 06 om......Q...bs/.
07 2A 86 48 86 FC 6B 01 60 0C 06 0A 2A 86 48 86 .*.H..k.`...*.H.
FC 6B 02 02 01 01 63 09 06 07 2A 86 48 86 FC 6B .k....c...*.H..k
03 64 0B 06 09 2A 86 48 86 FC 6B 04 01 05 9F 6E .d...*.H..k....n
2A 48 20 50 2B 82 31 80 30 00 63 03 29 00 11 34 *H P+.1.0.c.)..4
88 20 07 11 42 13 00 11 43 13 00 11 44 13 00 13 . ..B...C...D...
03 00 00 00 00 00 00 00 00 00 00 9F 65 01 FF    ............e..

Then if I parsed this correctly...

6F 6D                                              (FCI template) length=6D          
    84 07    A0 00 00 01 51 00 00     DF Name i.e. AID length=7
	A5 62                                      Proprietary information encoded in BER-TLV length=-62
	    73 2F
        	06 07    2A 86 48 86 FC 6B 01 
			    60 0C 
				    06 0A     2A 86 48 86 FC 6B 02 02 01 01 
					63 09 
					    06 07    2A 86 48 86 FC 6B 03 
					64 0B 
					    06 09    2A 86 48 86 FC 6B 04 01 05
		9F 6E 2A 
		    48 20 50 2B 82 31 80 30 00 63 03 29 00 11 34 88 
			20 07 11 42 13 00 11 43 13 00 11 44 13 00 13 03
			00 00 00 00 00 00 00 00 00 00 
		9F 65 01    FF    

@mtoconno
Copy link
Author

@dengert and @frankmorgner , sorry again for the long delay in response. Here's the data you requested:

opensc-tool --card-driver default --send-apdu "00:A4:04:00:07:A0000000030000:00"
Using reader with a card: Identiv SCR3500 A Contact Reader
Sending: 00 A4 04 00 07 A0 00 00 00 03 00 00 00
Received (SW1=0x6A, SW2=0x82)
opensc-tool --card-driver default --send-apdu "00:A4:04:00:07:A0000001510000:00"
Using reader with a card: Identiv SCR3500 A Contact Reader
Sending: 00 A4 04 00 07 A0 00 00 01 51 00 00 00
Received (SW1=0x90, SW2=0x00):
6F 6D 84 07 A0 00 00 01 51 00 00 A5 62 73 2F 06 om......Q...bs/.
07 2A 86 48 86 FC 6B 01 60 0C 06 0A 2A 86 48 86 .*.H..k.`...*.H.
FC 6B 02 02 01 01 63 09 06 07 2A 86 48 86 FC 6B .k....c...*.H..k
03 64 0B 06 09 2A 86 48 86 FC 6B 04 01 05 9F 6E .d...*.H..k....n
2A 48 20 50 2B 82 31 70 74 00 61 80 75 03 01 73 *H P+.1pt.a.u..s
73 12 11 11 42 81 97 11 43 81 97 11 44 81 97 13 s...B...C...D...
0B 00 00 00 00 00 00 00 00 00 00 9F 65 01 FF    ............e..

@frankmorgner
Copy link
Member

So it is a Java Card Applet...

But I lost track... is there still a problem to solve? Is the problem still present in 0.19.0?

@dengert, do you think you need to check for the GP AIDs n card-piv.c to initialize priv->card_issues correctly or is looking at the historical bytes enough?

@dengert
Copy link
Member

dengert commented Nov 13, 2018

I would like to ask @mtoconno :

  • To try 0.19.0 (or a nightly build of master) to see if there are any changes.

  • I would also like to see more of the debug log, as only snipits and no APDU commands or responses can be misleading.

  • Have you tried the instructions in: OpenSSH: Sign and Send Public Key Failed #1460 (comment) ? If so we can look at adding something similar the code.

  • Have you tried to set the ignore_user_consent=true; and use_pin_cache=true; in the opensc.conf file?

It could be this dual CAC/PIV card is imposing the PIN_ALWAYS rule on the 9A key, which it should not be doing. One way to address that is to examine the keyUsage bits from the certificate. pkcs15-piv.c will use the keyUsage bits from a certificate if it not a GOV issued card. (Look for follows_nist_fascn in pkcs15-piv.c). But a mod could be added to set the user_consent from the keyUsage if this is the problem.

In response to question on setting priv->card_issues:

There are many cards with PIV applets. These include NIST approved cards and US- GSA approved.
There are many more not approved applets, including cards and tokens from Yubico, Taglio, MYEID. And there are a number of open source PIV applets including https://github.com/arekinath/PivApplet . They all have different card_issues.

And the above does not list the Dual mode CAC/PIV cards. The card in question may be one of these.

Many of these cards are Java/GP based, but there is no NIST requirement for JAVA or GP. Checking the GP AID would not help. Even checking the ATR is not very helpful unless it says the default AID is the PIV AID or has some vendor specific fields like Yubico and Safenet. (I have tried to avoid using a list of ATRs, as anyone could put a PIV applet on almost any card.)

Card-piv.c attempts to determine the type of card/token where there are multiple applets on the card and multiple running applications using one or more of the applets. It tries to not cause the current login state of the active application to be lost while trying to determine if there is a PIV applet and its issues. And at the same time test if the API is still active and the login state was not lost because some other application or card driver reset the PIV login state.

@mtoconno
Copy link
Author

mtoconno commented Nov 27, 2018

@dengert , sorry for not responding earlier, this got lost in my email.

  • To try 0.19.0 (or a nightly build of master) to see if there are any changes.

The most recent nightly build of 0.19 I tried was from Aug 27

  • I would also like to see more of the debug log, as only snipits and no APDU commands or responses can be misleading.

Is there a specific debug log you'd like me to post?

I was unable to get the nightly build to compile in OS X, so I couldn't test this out (I'm not very experienced in C development in OS X)

  • Have you tried to set the ignore_user_consent=true; and use_pin_cache=true; in the opensc.conf file?

Yes, adding those two comments got my PIV working for me.

EDIT: I just saw 0.19 was released. I'll download and it install it tonight. I'll let you know tomorrow if it works for me.

@dengert
Copy link
Member

dengert commented Nov 27, 2018

Even with 0.19.0 you may still need the the ignore_user_consent=true; and use_pin_cache=true; as well as the the following using your ATR. (opensc-tool -a will show the ATR.

card_atr 3b:7d:96:00:00:80:31:80:65:b0:83:11:17:d6:83:00:90:00 {
                driver = "PIV-II";
                type = 14005;
       }

For those developers out there: We have been looking at Dual CAC/PIV cards and there are a number of issues, that I think we can address so the above will not be needed.

These include:

  1. Use of the Global PIN in the VERIFY command. It looks like all Dual CAC/PIV cards are Java based. This would then not drop the login state when a SELECT AID was done. This still need to be tested.
    NIST 800-73-3 defined the Discovery object that would show if the PIV applet PIN or the Global PIN should be used. And PIV Endpoint applet on Dual CAC/PIV are based on NIST 800-73-1 which is obsolete and does not support the Discovery Object.

  2. ALL Dual CAC/PIV cards have a CCC object. It is a way to access share data on the card from differnet drivers. This can be used to see if a PIV card is really a Dual CAC/PIV, and it also shows if the card is Java based. (It looks like they are. A PIV Endpoint applet on Dual CAC/PIV share data with the the proprietary CAC applet.

  3. The main difference between PIV and CAC (in teh way OpenSC uses them) is that the PIV has a PIV AUTH certificate/key which the CAC driver could access, @Jakuje maybe we need to look at this for the CAC driver. The CCC lists if it is present and how to access it. But in some CAC cards the Sign cert/key is also used as the PIV AUTH cert/key.

@Jakuje
Copy link
Member

Jakuje commented Nov 27, 2018

In CAC driver, the certificates have in first place specific paths and are already parsed and used from the CCC:

https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/card-cac.c#L1469-L1474

Did you have a look into the CCC checking in the PIV driver to unbreak it for the dual cards or do you still need some more information/help/testing?

@dengert
Copy link
Member

dengert commented Nov 27, 2018

Ok I see you pickup the AID from the CCC F3 entries including any PIV object with a0 00 00 01 16.

I have one other question. PIV specs 800-73-1 used for CAC PIV Endpoint, says VERIFY is 00 20 00 00... But in 800-73-3 says:
"Key reference '80' specific to the PIV Card Application (i.e., local key references) and, optionally, the Global PIN with key reference '00' are the only key references that may be verified by the PIV Card Application’s VERIFY command."
800-73-3 also introduced the Discovery object to say which PIN reference is preferred.

In other words NIST changed the default PIN reference from 00 to 80. The PIV driver is based on 800-73-3 so uses 80 as the default.
It never says they can be the same or different PINS. It looks like all CAC cards have a Global PIN and may not have a separate PIV Applet PIN.

the VERIFY with Lc=0 can be used to test the login state. So if the key reference is wrong, it may appear the card has lost the login state.

Can you please try a test where you change in card-piv.c
priv->pin_preference = 0x80; /* 800-73-3 part 1, table 3 */
to
priv->pin_preference = 0x00; /* 800-73-1 */

and try pkcs11-tool --test --login if the PIV driver still runs?

If so I will change to use pin reference 00 for cards not supporting 800-73-3.

@Jakuje
Copy link
Member

Jakuje commented Nov 28, 2018

This did not help. It actually broke the login completely. Here is the snippet of the debug log:

0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] sec.c:170:sc_pin_cmd: called
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] card-piv.c:3426:piv_pin_cmd: called
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] card-piv.c:3427:piv_pin_cmd: piv_pin_cmd tries_left=-1, logged_in=-1
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] apdu.c:554:sc_transmit_apdu: called
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] card.c:415:sc_lock: called
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] card.c:455:sc_lock: returning with: 0 (Success)
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] apdu.c:521:sc_transmit: called
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] apdu.c:371:sc_single_transmit: called
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] apdu.c:378:sc_single_transmit: CLA:0, INS:20, P1:0, P2:0, data(8) 0x7ffe742c8d20
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] reader-pcsc.c:284:pcsc_transmit: reader 'SCM Microsystems Inc. SCR 3310 [CCID Interface] (53311651713564) 00 00'
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] reader-pcsc.c:285:pcsc_transmit:
Outgoing APDU (13 bytes):
00 20 00 00 08 37 37 37 37 37 37 37 37 . ...77777777
0x7f9a22e2e740 03:32:11.092 [opensc-pkcs11] reader-pcsc.c:213:pcsc_internal_transmit: called
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] reader-pcsc.c:294:pcsc_transmit:
Incoming APDU (2 bytes):
6A 88 j.
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] apdu.c:390:sc_single_transmit: returning with: 0 (Success)
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] apdu.c:543:sc_transmit: returning with: 0 (Success)
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] iso7816.c:128:iso7816_check_sw: Referenced data not found
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] card-piv.c:3544:piv_pin_cmd: piv_pin_cmd tries_left=-1, logged_in=0
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] card-piv.c:3545:piv_pin_cmd: returning with: -1216 (Data object not found)
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] sec.c:217:sc_pin_cmd: returning with: -1216 (Data object not found)
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] pkcs15-pin.c:449:sc_pkcs15_verify_pin_with_session_pin: PIN cmd result -1216
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] card.c:465:sc_unlock: called
0x7f9a22e2e740 03:32:11.099 [opensc-pkcs11] reader-pcsc.c:663:pcsc_unlock: called
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] pkcs15-pin.c:471:sc_pkcs15_verify_pin_with_session_pin: returning with: -1216 (Data object not found)
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] pkcs15-pin.c:327:sc_pkcs15_verify_pin: returning with: -1216 (Data object not found)
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] framework-pkcs15.c:1631:pkcs15_login: PKCS15 verify PIN returned -1216
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] misc.c:61:sc_to_cryptoki_error_common: libopensc return value: -1216 (Data object not found)
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] pkcs11-session.c:311:C_Login: fLogin() rv 5
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] pkcs11-global.c:315:C_Finalize: C_Finalize()
0x7f9a22e2e740 03:32:11.105 [opensc-pkcs11] ctx.c:886:sc_cancel: called

So this is unfortunately not a way to go.

@Jakuje
Copy link
Member

Jakuje commented Dec 3, 2018

@dengert ping. Any progress here? Do you need some help with drafting a patch?

@dengert
Copy link
Member

dengert commented Dec 3, 2018

I have been working on a patch for this that uses ATRs for some known Dual CAC/PIV cards and some other cards. as well as parsing the CCC if present to identify any other Dual CAC/PIV cards.
For testing I took the CCC you sent and put it on a Oberthur PIV card to test reading and parsing.

I hope to have it ready in the next few days, In the meantime there is the circumvention of using.

card_atr 3b:7d:96:00:00:80:31:80:65:b0:83:11:17:d6:83:00:90:00 {
                driver = "PIV-II";
                type = 14005;
       }

@mtoconno
Copy link
Author

@dengert , I've been successfully using 0.19 release with the ignore_user_consent=true; and use_pin_cache=true; without any other modifications. My card atr is a bit different from the one you posted above though. Mine is:
3b:db:96:00:80:1f:03:00:31:c0:64:b0:f3:10:00:0f:90:00:88

@frankmorgner
Copy link
Member

Is there anything left to do?

@Jakuje
Copy link
Member

Jakuje commented Mar 6, 2019

I think this was solved with #1549

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

No branches or pull requests

5 participants