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

PKCS11.C_SignFinal(Native Method) is failing with message CKR_DATA_INVALID and losing session , asking pin again and again in JRE>=jre8.1_161 version #1966

Closed
jalajpachouly opened this issue Feb 29, 2020 · 48 comments

Comments

@jalajpachouly
Copy link

This is how I am loading the pkcs11 dll.

String configName = "/opt/bar/cfg/pkcs11.cfg";
Provider p = Security.getProvider("SunPKCS11");
p = p.configure(configName);
Security.addProvider(p);

I am able to use smart card authentication , but after little usage suddenly gets logout from the application and also see smart card pin popup, looks like smart card session is lost.

Error is like this -

Feb 27, 2020 10:17:00 PM STDERR: javax.net.ssl.SSLException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_DATA_INVALID
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:133)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1314)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:408)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1362)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1337)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:246)

Feb 27, 2020 10:17:00 PM STDERR: at java.base/java.lang.Thread.run(Thread.java:834)
Feb 27, 2020 10:17:00 PM STDERR: Caused by: java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_DATA_INVALID
Feb 27, 2020 10:17:00 PM STDERR: at jdk.crypto.cryptoki/sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:646)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/java.security.Signature$Delegate.engineSign(Signature.java:1402)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/java.security.Signature.sign(Signature.java:711)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.CertificateVerify$T12CertificateVerifyMessage.(CertificateVerify.java:582)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.CertificateVerify$T12CertificateVerifyProducer.produce(CertificateVerify.java:740)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLHandshake.produce(SSLHandshake.java:436)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.ServerHelloDone$ServerHelloDoneConsumer.consume(ServerHelloDone.java:182)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:392)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:450)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:427)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:178)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1152)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1063)
Feb 27, 2020 10:17:00 PM STDERR: at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:402)
Feb 27, 2020 10:17:00 PM STDERR: ... 20 more
Feb 27, 2020 10:17:00 PM STDERR: Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_DATA_INVALID
Feb 27, 2020 10:17:00 PM STDERR: at jdk.crypto.cryptoki/sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
Feb 27, 2020 10:17:00 PM STDERR: at jdk.crypto.cryptoki/sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:604)

@jalajpachouly
Copy link
Author

Please note i am using the latest .20 version of opensc-pkcs11 dll.

@dengert
Copy link
Member

dengert commented Feb 29, 2020

The above does not show any use of OpenSC.
Please provide OpenSC PKCS11_SPY and/or OpenSC debug log. https://github.com/OpenSC/OpenSC/wiki/Using-OpenSC

What type of card?

@jalajpachouly
Copy link
Author

Hi Dengert,
Thanks for comment, Yes I am trying to generate the detailed log, although please see at the bottom of the log -
jdk.crypto.cryptoki/sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
Feb 27, 2020 10:17:00 PM STDERR: at jdk.crypto.cryptoki/sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:604)

Here C_SignFinal is a native invocation.

I am new to using opensc, can you please tell me the exact windows64, registry entries , which are required to generate the log? Is it enough to add the registry on environment variable, or I need to load the PKCS11_SPY using java program.
Some sample if you have , it will be of great help.

@jalajpachouly
Copy link
Author

PIV card-type=14004

@dengert
Copy link
Member

dengert commented Mar 1, 2020

The environment variable OPENSC_DEBUG=3 should have created a log to stderr. You found the card type of 14004, so I assume you found it in the log or stderr?

jdk.crypto.cryptoki/sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method) does not help much. It does not show OpenSC was used at all, or why OpenSC failed.

The PKCS11_SPY is a PKCS11 module that writes a log file and then calls the real PKCS11 module that does the work. So you can point the Java PKCS11 configuration code at the Spy module:

C:\Program Files\OpenSC Project\PKCS11-Spy\pkcs11-spy.dll
if using 32 bit version on Windows 64bit also change:
C:\Program Files (x86)\OpenSC Project\PKCS11-Spy\pkcs11-spy.dll
The pkcs11-spy will use the registry to find the opensc-pkcs11.dll and the output file.

You can use regedit to modify or find out what is going on.
start regedit, then go to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSC Project
there are 2 sub-entries which OpenSC uses to find parameters: OpenSC and PKCS11-Spy. PKCS11-spy\module points the real PKCS11 module location i.e. full path to opensc-pkcs11.dll

Note: when running on a 64 but machine there is also this location used by the 32 bit versions:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\OpenSC ProjectIf you make changes to one you need to make the same changes to the other to make sure both 64 and 32 bit programs work as expected.

There is one other thing about the PIV card. The card enforces a "PIN Always" policy on the card that requires the PIN to be entered just before a Sign operation when using the "Sign key".
PIV docs refer to this as "X.509 Certificate for Digital Signature" and the "9C" key. OpenSC PKCS11 uses CK_ID = 02 for the certificate, public key and private key.

Some applications, and maybe your Java application do not know how to handle the PIN Always policy which PKCS11 presents as a key attribute of CKA_ALWAYS_AUTHENTICATE. An application should then ask the user again for PIN and call C_Login again.

The PKCS11-Spy log would show if the application understands CKA_ALWAYS_AUTHENTICATE.
If the application does not query for this attribute it does not support this policy. The SPY trace would show if it did and asked user for PIN, and called C_Login again just be for call to C_Login again.

Try using your card with pkcs11-tool --test --login. This will try to use all the keys on the card and prompt you several times for you PIN. pkcs11-tool handles the CKA_ALWAYS_AUTHENTICATE attribute.

If the failure is Java does not support CKA_ALWAYS_AUTHENTICATE, and you and your employer are willing to violate the NIST PIV policy of having user's enter PIN before every use of the Sign key OpenSC does have a workaround. (But this does not work when using a PIN pad reader which further protect the PIN.)

See https://github.com/OpenSC/OpenSC/blob/master/etc/opensc.conf.example.in#L894 and add the framework pkcs15 { } stanza to the opensc-conf file with line 897 uncommentted pin_cache_ignore_user_consent = true;. Read the lines above about pin caching too.

@mouse07410
Copy link
Contributor

Actually, last I checked Java did not understand ALWAYS_AUTHENTICATE.

That's one reason why I maintain my own fork of OpenSC , which has that %#^# software-level enforcement of ALWAYS_AUTHENTICATE removed.

Re. Policy - it's the card's job to enforce it or not. Not the middleware's.

@jalajpachouly
Copy link
Author

regarding - See https://github.com/OpenSC/OpenSC/blob/master/etc/opensc.conf.example.in#L894 and add the framework pkcs15 { } stanza to the opensc-conf file with line 897 uncommentted pin_cache_ignore_user_consent = true;

--> As I am loading the opensc-pkcs11.dll using java program, and I am not sure how should I pass the parameters CKA_ALWAYS_AUTHENTICATE, & pin_cache_ignore_user_consent = true .
I tried these parameters passing , in the configuration file (pkcs11.cfg) , but these parameters are not accepted and gets invalid parameter error from - sun.security.pkcs11.Config.parse() method.

And the type of card I got by running the tools provided by the opensc. some how logs are not getting generated when executing from Java program.
So To generate the logs -
I should load the PKCS11-Spy.dll rather than opensc-pkcs11.dll from java program while configuring the PKCS provider, which I believe will lookup for the opensc-pkcs11.dll from regsistry and also figure out the location of the log file from the registry. Please confirm.

@dengert
Copy link
Member

dengert commented Mar 1, 2020

From what Moose says, Java does not understand CKA_ALWAYS_AUTHENTICATE. skip that for now,
unless you are writing the Java program and calling PKCS11 calls from your own code.

In the two opensc.conf files most likely these:
C:\Program Files (x86)\OpenSC Project\OpenSC\opensc.conf
C:\Program Files\OpenSC Project\OpenSC\opensc.conf

This should work. If not uncomment the two debug lines and add a path if you want to debug_file:

app default {
	# debug = 3;
	# debug_file =opensc-debug.txt;
	
	framework pkcs15 {
		use_file_caching = true;
		pin_cache_ignore_user_consent = true;
	}
}

Don't run in production with the debug lines uncommented. PINs will show up in the debug_file and SPY output. You may want to XXXXX them out before posting on the list.

"I should load the PKCS11-Spy.dll rather than opensc-pkcs11.dll from java program while configuring the PKCS provider" - Yes.

@jalajpachouly
Copy link
Author

Thanks for the valuable inputs -
Here are the logs from spy logs ,
this looks improper response , as the [out] pSignature[*pulSignatureLen] is missing where as i can see the proper [out] pSignature[*pulSignatureLen] in other calls-
2054: C_SignFinal
2020-03-01 22:37:18.233
[in] hSession = 0x6457b470

2055: C_GetSessionInfo
2020-03-01 22:37:18.233
[in] hSession = 0x6593fee0
[out] pInfo:
slotID: 0
state: ' CKS_RW_USER_FUNCTIONS'
flags: 6
CKF_RW_SESSION
CKF_SERIAL_SESSION
ulDeviceError: 0
Returned: 0 CKR_OK

and after this there is another call - with following data
2134: C_SignFinal
2020-03-01 22:37:18.296
[in] hSession = 0x6593fee0
Returned: 33 CKR_DATA_LEN_RANGE
Returned: 33 CKR_DATA_LEN_RANGE
Returned: 32 CKR_DATA_INVALID

@jalajpachouly
Copy link
Author

logs from opensc-debug.txt -
P:9320; T:8452 2020-03-02 03:42:45.084 [opensc-pkcs11] iso7816.c:760:iso7816_get_response: returning with: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.084 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.084 [opensc-pkcs11] apdu.c:471:sc_get_response:
SM response data (256 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
P:9320; T:10080 2020-03-02 03:42:45.084 Object 0/1717589728: Attribute 0x0 does NOT match.
P:9320; T:8452 2020-03-02 03:42:45.084 called, left=256, depth 0
P:9320; T:10080 2020-03-02 03:42:45.084 Object with handle 0x66605740
P:9320; T:8452 2020-03-02 03:42:45.084 [opensc-pkcs11] apdu.c:475:sc_get_response: GET RESPONSE error: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.085 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.085 [opensc-pkcs11] apdu.c:541:sc_transmit: cannot get all data with 'GET RESPONSE': -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.085 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.085 [opensc-pkcs11] card.c:465:sc_unlock: called
P:9320; T:10080 2020-03-02 03:42:45.085 Object 0/1717589824: Attribute 0x1 matches.
P:9320; T:8452 2020-03-02 03:42:45.085 DEE r=-1204 apdu.resplen=4096 sw1=00 sw2=00
P:9320; T:10080 2020-03-02 03:42:45.086 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.086 Transmit failed
P:9320; T:10080 2020-03-02 03:42:45.086 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.086 [opensc-pkcs11] card.c:465:sc_unlock: called
P:9320; T:10080 2020-03-02 03:42:45.086 Object 0/1717589824: Attribute 0x0 does NOT match.
P:9320; T:8452 2020-03-02 03:42:45.086 [opensc-pkcs11] card-piv.c:605:piv_general_io: returning with: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.086 Object with handle 0x666057a0
P:9320; T:8452 2020-03-02 03:42:45.087 [opensc-pkcs11] card-piv.c:2389:piv_validate_general_authentication: returning with: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.087 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.087 [opensc-pkcs11] card-piv.c:2468:piv_compute_signature: returning with: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.087 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.087 [opensc-pkcs11] sec.c:63:sc_compute_signature: returning with: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.088 Object 0/1717589920: Attribute 0x1 matches.
P:9320; T:8452 2020-03-02 03:42:45.088 [opensc-pkcs11] card.c:465:sc_unlock: called
P:9320; T:10080 2020-03-02 03:42:45.088 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.088 [opensc-pkcs11] pkcs15-sec.c:458:sc_pkcs15_compute_signature: use_key() failed: -1204 (Unsupported INS byte in APDU)
P:9320; T:10080 2020-03-02 03:42:45.088 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.088 [opensc-pkcs11] pkcs15-sec.c:320:sc_pkcs15_compute_signature: called
P:9320; T:10080 2020-03-02 03:42:45.089 Object 0/1717589920: Attribute 0x0 does NOT match.
P:9320; T:8452 2020-03-02 03:42:45.089 supported algorithm flags 0x1, private key usage 0x2E
P:9320; T:10080 2020-03-02 03:42:45.089 Object with handle 0x66605800
P:9320; T:8452 2020-03-02 03:42:45.089 [opensc-pkcs11] padding.c:284:sc_get_encoding_flags: called
P:9320; T:10080 2020-03-02 03:42:45.089 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.089 iFlags 0x8002, card capabilities 0x1
P:9320; T:10080 2020-03-02 03:42:45.089 pkcs15_dobj_get_attribute() called
P:9320; T:8452 2020-03-02 03:42:45.090 pad flags 0x8002, secure algorithm flags 0x0
P:9320; T:10080 2020-03-02 03:42:45.090 Object 0/1717590016: Attribute 0x1 matches.
P:9320; T:8452 2020-03-02 03:42:45.090 [opensc-pkcs11] padding.c:323:sc_get_encoding_flags: returning with: 0 (Success)

@jalajpachouly
Copy link
Author

Actually, last I checked Java did not understand ALWAYS_AUTHENTICATE.

That's one reason why I maintain my own fork of OpenSC , which has that %#^# software-level enforcement of ALWAYS_AUTHENTICATE removed.

Re. Policy - it's the card's job to enforce it or not. Not the middleware's.

HI Mouse,
I maintain my own fork of OpenSC , does it mean you maintain your own version of opensc dll?
what changes have you made for the same?
can you please provide some more details what exactly you have changed?
Regrads
Jalaj

@jalajpachouly
Copy link
Author

Hi Dengert,
can you please explain the relation of #1966 and #1962 ? Please also explain the last paragraph of your comment that it is not an issue for #1966?

@mouse07410
Copy link
Contributor

I myself don't work on Windows, so I don't build DLLs. It should be possible to build Windows binaries, including DLL, the same way as this repo does it. https://GitHub.com/mouse07410/OpenSC.git

@dengert
Copy link
Member

dengert commented Mar 3, 2020

Both were dealing with accessing PIV cards. At one time it looked like one or both might be uing teh Sign cert that requires special handling because of the "PIN Always" - "CKA_ALWAYS_AUTHENTICATE" issue.

@jalajpachouly
I need more of your log. If the card is PIV it should not have "SM response data (256 bytes):" in the log. Past it as a file, not inline. You can XX out any PIN data look for PIN like 3x 3x 3x 3x FF FF FF FF
digits in hex padded with FF to 8 bytes.

@dengert
Copy link
Member

dengert commented Mar 3, 2020

I also need more of the SPY log, to see how it got to the C_SignFinal.

@dengert
Copy link
Member

dengert commented Mar 3, 2020

Two more tests. pkcs11-tool -O --login and pkcs11-tool --tests --login

@jalajpachouly
Copy link
Author

As issue was working in JRE 8.1_RU151 and not working in JRE 8.1 RU221, hence i started downgrading the JRE to narrow down the issue. Hope I could have started in ascending order , but finally I was able to track down the culprit JRE version, that is JRE 8.1 RU 161 , which has introduced this issue. There are some significant changes related to security. I am suspecting the below mentioned changes which seems causing the issue -

security-libs/javax.crypto RSA public key validation
In 8u161, the RSA implementation in the SunRsaSign provider will reject any RSA public key that has an exponent that is not in the valid range as defined by PKCS#1 version 2.2. This change will affect JSSE connections as well as applications built on JCE.

Here is the release notes for JRE8.1 RU 161
https://www.oracle.com/technetwork/java/javase/8all-relnotes-2226344.html#R180_161

@dengert
Copy link
Member

dengert commented Mar 3, 2020

Are using using SHA1? Its out of date, and Java might reject it. Check there release notes.
In shows up 16 times in that release note. And taks about January 1, 2018.

That still does not help us to see if OpenSC has a problem or your card has a bad key.

Spy log, and or opensc-debug log would help. If it can not produce these logs, it could be as you said it is rejecting something in the java code.

If you get the private key and certificate out of sync that could be your problem.

How did you generate the keys on the card?
Or loaded key and on the card?
How did you create the certificate?
How did you load certificate?

The pkcs11-tool runs would show if the keys match the certificate and can be verified.

PIV specs only say how to generate a key pair on the card and this is the only time it will return the public key. Some vendors provide a way to write a private key on the card. PIV driver parses certificate to find public key to determine algorithm, key size, and namedCurve.

@jalajpachouly
Copy link
Author

Hi Dengert,
Here are the card details -
3.1 Test PIV Card 1

Card Capability Container:
data model number = 0x10. All other data elements have length value set to zero bytes.
CHUID:
FASC-N: D6501858289D6DCACC9325A16859A46927C9D45C86501843E2
(Agency Code = 3201, System Code = 0295, Credential Number = 759494,
CS=1, ICI=1, PI=6464979587, OC=1, OI=3201, POA=1)
GUID: all 0x00
Expiration Date: 20301231
Asymmetric signature: RSA PKCS #1 v1.5 with SHA-256, signed by PIV Content Signer 1
PIV Authentication Certificate:
PIV Test Card 1: PIV Authentication Certificate, GZIP compressed
Card Authentication Certificate:
PIV Test Card 1: Card Authentication Certificate, GZIP compressed
Digital Signature Certificate:
PIV Test Card 1: Digital Signature Certificate, GZIP compressed
Key Management Certificate:
PIV Test Card 1: Key Management Certificate, GZIP compressed
Cardholder Fingerprints:
dummy fingerprints

RSA PKCS #1 v1.5 with SHA-256, signed by PIV Content Signer 1

Security Object:
RSA PKCS #1 v1.5 with SHA-256, signed by PIV Content Signer 1
Cardholder Facial Image:
RSA PKCS #1 v1.5 with SHA-256, signed by PIV Content Signer 1
Printed Information:
Name: Test Cardholder

Employee Affiliation: Employee

Expiration date: 20301231 (2030DEC31)

Agency Card Serial Number: 0000012345

Issuer Identification: TSTISR320161719

Discovery Object:
PIV Card Application AID: '4F 0B A0 …'. PIV Usage Policy: 0x40 0x00 (no global PIN)
Key History Object: Not present
Cardholder Iris Images: Not present

@jalajpachouly
Copy link
Author

Spy -dll logs.
syl-dll.txt

opensc-debug logs
opensc-debug.txt

Please see if this is similar to - #1531
Thanks for your help.
Regards
Jalaj

@dengert
Copy link
Member

dengert commented Mar 5, 2020

You may have a defective reader.
opensc-debug.log line 15:
Identive SCR33xx v2.0 USB SC Reader 0:SCardControl failed: 0x00000001
This leads to using protocol T=0

A valid looking crypto operations in your opensc-debug.log
starting at line 9479:
Outgoing APDU (260 bytes): 10 87 07 9A FF 7C 82 01 06 82 00 81 82 01 00 00 ....
Incoming APDU (2 bytes): 90 00
Outgoing APDU (16 bytes): 00 87 07 9A 0B B7 83 41 CE 32 58 41 ED A5 19 55
Incoming APDU (2 bytes): 61 00 (256 or more bytes need to get read)
Outgoing APDU (5 bytes): 00 C0 00 00 00 (get response to start reading the results.)
Incoming APDU (258 bytes): .... ending in 6108 (eight more byes need to be read )
Outgoing APDU (5 bytes):00 C0 00 00 08 (reaqd last 8 bytes)
Incoming APDU (10 bytes): EF 7F 47 D1 63 44 86 32 90 00 (90 00 show this completed)

A line 29104 is a crypto operation that fails:
Outgoing APDU (260 bytes): 10 87 07 9A FF 7C 82 01 06 82 00 81 82 01 00 00 ...
Incoming APDU (2 bytes): 61 14

The "10" says this is a chained operation, so 90 00 should have been received, not 61 14
which says there are 14 more bytes. It looks like the card or reader lost track of the chaining.

Outgoing APDU (5 bytes): 00 C0 00 00 14 (get response that should not be needed here)
Incoming APDU (2 bytes): 90 00 (completed OK with no data)
Outgoing APDU (16 bytes):00 87 07 9A 0B 17 38 DD 03 CE D4 A1 C2 FD B5 7E (sending reset of input not chaining bit is off)
Incoming APDU (2 bytes):6D 00 (Failure)

There is a chance there is a bug in OpenSC or the card when using T=0, but based on line 15 and the following reviews of the device which appears to be the same as yours:
https://www.amazon.com/Identiv-SCR3310v2-0-Smart-Card-Reader/dp/B002N3MM6W?th=1

Customer reviews that sound like the same problem:
https://www.amazon.com/gp/customer-reviews/R1SCPSA1BQHGPE/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B002N3MM6W

https://www.amazon.com/gp/customer-reviews/R2EML9CPS645GL/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B002N3MM6W

Do you have any other readers?
If you can reproduce the problem with a different reader that does not fail as in line 15, I can look further. Please send opensc-debug.log in that case.

@jalajpachouly
Copy link
Author

Hi Dengert,

Thanks for the updates
I will surely try with different reader, although just curious the same reader works well with JRE8.1.151 and starts behaving erroneous with JRE8.1.161.
Below is the link which talks about the update in JRE8.1_161.
https://www.oracle.com/technetwork/java/javase/8all-relnotes-2226344.html#R180_161
It does have changes related to RSA public key validation sa per PKCS#1 version 2.2.

Regards
Jalaj

@dengert
Copy link
Member

dengert commented Mar 5, 2020

As I noted, some of the crypto operations worked. Some did not. So this could be an intermittent problem.

I don't think this is a java problem, the failed crypto chaining operation looks strange.

I dont thing it is a login problem. 6D 00 is SC_ERROR_INS_NOT_SUPPORTED.

Also note that the opensc-debug.log says you are using OpenSC 0.19.0
Line 2: P:2528; T:6312 2020-03-02 03:27:33.597 opensc version: 0.19.0

Can you try with 0.20.0?

@jalajpachouly
Copy link
Author

we have seen this issue in .20 also, when we downgrade the JRE version to 8.1_151 , it works smooth.

@dengert
Copy link
Member

dengert commented Mar 5, 2020

With the reader you have, try `force_protocol = "T1";' in opensc.conf See example:
https://github.com/OpenSC/OpenSC/blob/master/etc/opensc.conf.example.in#L281

Please try 'pkcs11-tool --test --login` to see if this fails or not. If it fails can you send the opensc-debug.log.

@dengert
Copy link
Member

dengert commented Mar 6, 2020

Looking closer at the failing sequence, this may be a thread locking problem, as it appears
line 29103: is from thread 8552 to do a signature:
P:2528; T:8552 2020-03-02 03:29:01.892 [opensc-pkcs11] reader-pcsc.c:285:pcsc_transmit:
then line 29123 is from thread 10160
P:2528; T:10160 2020-03-02 03:29:01.910 [opensc-pkcs11] reader-pcsc.c:294:pcsc_transmit:

line 29040 is is the start of the apdu that is returning the 61 14 in the middle of the chained signature operation. which then fails.

Some how these commands are being intermixed, and the card cant handle the sequence.
So you comments about the different versions of Java may be correct.

Any other developer any other ideas?

@jalajpachouly
Copy link
Author

Case 1 - Log for 'pkcs11-tool --test --login
opensc-debug-case1.txt

Case 2- Running application with new reader , I see java crash in all the runs , i also tried to set orce_protocol = "T1"; but getting crash, even removing the token , still we see the crash with new Reader.

Capture

Crash logs -
opensc-debug-java -crash.txt

@jalajpachouly
Copy link
Author

I also used the opensc .20 in above runs.

@jalajpachouly jalajpachouly changed the title PKCS11.C_SignFinal(Native Method) is failing with message CKR_DATA_INVALID in JRE11 PKCS11.C_SignFinal(Native Method) is failing with message CKR_DATA_INVALID and losing session , asking pin in JRE>=jre8.1_161 version Mar 6, 2020
@jalajpachouly jalajpachouly changed the title PKCS11.C_SignFinal(Native Method) is failing with message CKR_DATA_INVALID and losing session , asking pin in JRE>=jre8.1_161 version PKCS11.C_SignFinal(Native Method) is failing with message CKR_DATA_INVALID and losing session , asking pin again and again in JRE>=jre8.1_161 version Mar 6, 2020
@dengert
Copy link
Member

dengert commented Mar 6, 2020

Did you see #1966 (comment) ?

This looks like a thread issue.

OpenSC supports threads, but it does not write much in the debug log. It is not clear if Java made some change in jre8.1_161 that would cause it to misuse threads in PKCS11 or if OpenSC is not doing something correctly.

OpenSC can be configured without threads...
What OS are you running on?
Where did you get the the OpenSC libs?
Did you build them yourself?
Do you have output of configure? It should have "thread locking support: yes"

What would it take to reproduce the problem?
Can you say anything about the Java application you are trying to run?

Can you do some additional debugging. See https://docs.oracle.com/javase/8/docs/technotes/guides/security/p11guide.html#TroubleShoot

@jalajpachouly
Copy link
Author

For general SunPKCS11 provider debugging info:

-Djava.security.debug=sunpkcs11
For PKCS11 keystore specific debugging info:

-Djava.security.debug=pkcs11keystore
passing this to java command line arguments is not generating any logs , or I am not able to find where it is generating logs.
OS is - Windows server 2008 R2 enterprise
location of the opensc lib -
https://github.com/OpenSC/OpenSC/wiki-
OpenSC-0.20.0_win64.msi for 64 bit programs

I am not building the dll.

OpenSC can be configured without threads...
Can you please provide sample for setting without threads?

@jalajpachouly
Copy link
Author

Reproducing the issue - Load the pkcs11 provider using opensc-pkcs11 dll, using stand alone java client program and use JRE 8.1_161 version or higher version , use https communication to the tomcat remote server , you will be able to login and later connection will be destroyed as your session becomes invalid in few seconds, due to failure in the opensc dll SignFinal method.

@mouse07410
Copy link
Contributor

Would you like to try my fork to see if it's any better for Java access?

@dengert
Copy link
Member

dengert commented Mar 7, 2020

I have been looking at opensc-debug.log from 2020-03-02 -3:29:01.877. around line 29036 you will see threads 10160 and 8552 issuing APDUs. 10160 is doing a C_GetSessionInfo that will endup quering the card to check the login state, and if it is still inserted. 8552 is doing a signature operation that consists of requires multiple APDUs. The two are intermingling the APDUs, and causeing havic!

This should not have happened OpenSC should have been using thread locking to not allow this to happen. The sc_plcs11_lock and sc_pkcs11_unlock should prevent 2 threads from running at the same time using opensc code. But there is not enough debugging to see what is happening.

It could be it used to work as expected

It could be java changed what it passed to OpenSC to handle locking.

It could be it never worked as expected, but java did not use multiple threads like it does in the latest release.

I have been working on a test code to add debugging so we can see what is going on. and will submit it as a PR so it will also get built, and you can then download the .msi files to try. Should have it in a hour or so.

dengert added a commit to dengert/OpenSC that referenced this issue Mar 7, 2020
this is test code to help debut issue OpenSC#1966
it is being submitted as a PR to get the windows artifacts
so user can test it.

It will show what is passed to C_Initialize by Java and
show enter and leave log messages for sc_pkcs11_lock and sc_pkcs11_unlock
if thay are actualling using teh mutex.

 On branch PKCS11-lock
 Changes to be committed:
	modified:   pkcs11-global.c
@dengert
Copy link
Member

dengert commented Mar 7, 2020

@jalajpachouly Can you test the change from #1975
You can find the ".msi" files by going to "All Checks have passed" and click on "show all checks"
Then on "continuous-integration/appveyor/pr — AppVeyor build succeeded" click on "details"
That will go to https://ci.appveyor.com/project/LudovicRousseau/opensc/builds/31317077

Find the image you want, most likely 2017 release 64 bit, and maybe the 32 bit too.
Click on these, then click on Artifacts to get to these:
https://ci.appveyor.com/api/buildjobs/xe0ybv8f9bbn49in/artifacts/OpenSC-0.20.0_win64.msi
https://ci.appveyor.com/api/buildjobs/79oo1lxqmqghciv4/artifacts/OpenSC-0.20.0_win32.msi

If you want to run a windows debuger these might be helpful too:
https://ci.appveyor.com/api/buildjobs/xe0ybv8f9bbn49in/artifacts/OpenSC-0.20.0_win64-Debug.zip

https://ci.appveyor.com/api/buildjobs/79oo1lxqmqghciv4/artifacts/OpenSC-0.20.0_win32-Debug.zip

Then send another opensc-debug.log

@jalajpachouly
Copy link
Author

OK,
I have used 0.20.0.205 version , somehow most of the time , application was getting hanged with debug=3, but somehow i was able to capture erroneous flows with debug, please see the attached logs below-
opensc-debug.txt

@jalajpachouly
Copy link
Author

P:8464; T:8232 2020-03-08 01:23:45.256 [opensc-pkcs11] pkcs15-sec.c:707:sc_pkcs15_compute_signature: use_key() failed: -1211 (Security status not satisfied)

is this card is no more valid with JRE starting 8.1_161 ?

@dengert
Copy link
Member

dengert commented Mar 8, 2020

This is a threads issue and not a "card is no more valid" issue.
It looks like JRE starting 8.1_161 there is something else going on, or something started in 0.19 as you said you had a problem with it too.

The debugging code in this PR shows in opensc-debug.txt from above, line 18 shows:
P:8464; T:8304 2020-03-08 01:22:23.853 [opensc-pkcs11] global_lock:0000000000000000, global_locking:default_mutex_funcs

The global_lock should not be null.
every call like:
pkcs11-global.c:844:sc_pkcs11_lock: called
should should have a matching
pkcs11-global.c:853:sc_pkcs11_lock: returning with 0 There are none
implying the routine returned here:
line 847 if (!global_lock)
line 848 return CKR_OK;

And if locking was working, every call to sc_pkcs11_lock,
should have a pkcs11-global.c:862:__sc_pkcs11_unlock: called
and pkcs11-global.c:868:unlocking There are none, implying the routine returned here:
860 if (!lock)
861 return;

The trick with using the debug log is sc_pkcs11_init_lock is called before sc_context_create that sets context which is used when writing to the log.

I have pushed another commit for testing. It is being compiled. I will see it it works under FireFox windows 10.

@jalajpachouly
Copy link
Author

Hi Dengert,
With the build 0.20.0.207 , i am not seeing the issue, although when I enable debugging, java application is hanging most of the time, hence i could took limited log, please analyse and see if you think every thing is in place now, or need some more changes.
opensc-debug.txt

@dengert
Copy link
Member

dengert commented Mar 9, 2020

The opensc-debug.txt looks good. lines 17-21 are as expected. Java is asking the PKCS11 module to do OS locking internally, and OpenSC was built and is using its internal version for Windows in the default_mutex_funcs

P:1864; T:8408 2020-03-09 04:21:54.219 [opensc-pkcs11] has _WIN32
P:1864; T:8408 2020-03-09 04:21:54.220 [opensc-pkcs11] has HAVE_OS_LOCKING
P:1864; T:8408 2020-03-09 04:21:54.220 [opensc-pkcs11] has PKCS11_THREAD_LOCKING
P:1864; T:8408 2020-03-09 04:21:54.220 [opensc-pkcs11] args:0000000000000000 0000000000000000 0000000000000000 0000000000000000 0x00000002 0000000000000000
P:1864; T:8408 2020-03-09 04:21:54.221 [opensc-pkcs11] global_lock:000000003436BD10, global_locking:000007FED8BF8228 default_mutex_funcs:000007FED8BF8228

sc_pkcs11_lock and __sc_pkcs11_unlock are being called as expected.

When you say "when I enable debugging" do you mean enabling Java debugging, or do you mean enabling OpenSC debugging?

@jalajpachouly
Copy link
Author

I mean OpenSC debugging
app default {
debug = 3;
debug_file = c:\opensc-debug.txt;
framework pkcs15 {
# use_file_caching = true;
}
}

@jalajpachouly
Copy link
Author

Hi Dengert,
Please also let me know if 0.20.0.207 version can be deployed in production ?
Regards
Jalaj

@dengert
Copy link
Member

dengert commented Mar 9, 2020

The version you have is built on top of master from 2020-03-06. It contains 206 commits since 0.20.0 was released. So the choice is yours as to when you deploy in production.

I would wait at least until the PR is committed. So far we have shown it fixes your problem.

You should also make a comment in #1975 that it fixes your problem.

I would also change the name of the issue to something like: "Java JRE>=jre8.1_161 exposes OpenSC PKCS11 failure to use thread locking on Windows is some cases"

@jalajpachouly
Copy link
Author

Can you please share the link from where I can get the fixed version , which I can deploy to production environment? By when PR commit will complete?

@frankmorgner
Copy link
Member

I've (accidentally) pushed a fix to master, binaries should soon be available at
https://github.com/OpenSC/Nightly/archive/2020-03-10_b91cfa8c.zip

@jalajpachouly
Copy link
Author

Thanks for the prompt response -as per the above link I have installed version 0.20.0.206, and it is working fine, I am using the same for the production, please let me know if you have any concerns?
Thanks for the prompt help, it was great to see prompt and to the point reply, with accurate analysis :-).
Regards
Jalaj

@Jakuje
Copy link
Member

Jakuje commented Sep 29, 2022

@dengert what is the version 0.20.0.206 or .207? The content of the #1975? Is it something we should merge? The subset of that excluding the logging?
@jalajpachouly Or is it something that is already fixed with 0.21.0 release?

@dengert
Copy link
Member

dengert commented Sep 29, 2022

@dengert what is the version 0.20.0.206 or .207?

I think he was counting number of commits past 0.20.0.

The content of the #1975?
Is it something we should merge? The subset of that excluding the logging?

@frankmorgner said in: #1966 (comment) that he accidentally committed a fix. i.e. f1bcadfbe
f1bcadfbe#diff-be076b25c71c277110d7d90b904e561ab939e7a3493eaca7ed52f868952b2d4dR106

He commented in: #1975 (comment)
and I agreed in:#1975 (comment)

So #1975 in not needed and the logging is not really needed either.

@Jakuje
Copy link
Member

Jakuje commented Sep 30, 2022

ok, so we should be good and this issue can be closed. Thanks for clarifications!

@Jakuje Jakuje closed this as completed Sep 30, 2022
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

No branches or pull requests

5 participants