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

SC-HSM: OpenSC 0.23.0 broken on Cygwin current #2944

Closed
clauspruefer opened this issue Nov 27, 2023 · 82 comments · Fixed by #2978
Closed

SC-HSM: OpenSC 0.23.0 broken on Cygwin current #2944

clauspruefer opened this issue Nov 27, 2023 · 82 comments · Fixed by #2978

Comments

@clauspruefer
Copy link

clauspruefer commented Nov 27, 2023

Problem Description

OpenSC 0.23.0 (current git) and 0.24.0 broken on Cygwin (Win11_64 current). Under OpenSC 0.22 everything is working fine.

Steps to reproduce

  1. Install
  • Download Cygwin Win11_64 setup-x86_64.exe and install
  • Open Cygwin64 Terminal
  • Clone latest OpenSC git repo (git clone https://github.com/OpenSC/OpenSC.git)
  • ./bootstrap
  • ./configure (--strict obsolete)
  • make (-jX)
  • make install
  1. Run pkcs-11-tool, pkcs15-tool, opensc-tool, sc-hsm-tool
  • Every pkcs-11 and pkcs-15 command returns "rv = CKR_DATA_INVALID (0x20)" :(

Proof (Compile OpenSC 0.22 with declaration-errors disabled)

  • Open Cygwin64 Terminal
  • Download OpenSC 0.22 source, depack and cd into dir
  • ./bootstrap
  • env "CFLAGS=-Wno-error=deprecated-declarations" ./configure
  • make (-jX)
  • make install

=> Result: working

Log Results

  1. Without inserted card (pkcs11-tool -L)

Available slots:
Slot 0 (0x0): Alcor Micro USB Smart Card Reader 0
(empty)

  1. With inserted card (pkcs11-tool -L)

Available slots:
Slot 0 (0x0): (GetSlotInfo failed, CKR_DATA_INVALID)

Logs for 2.) attached as file.

It seems that the used smart-card (SC-HSM) has been detected (line 406) and then stuck in endless loop Could not decode card verifiable certificate: -1401 (Invalid ASN.1 object). I am not too deep in pkcs15 protocol, also it could be a SC-HSM issue, but why just on CygWin, On my native Win11 (same machine) works fine.

opensc.log

Remarks

OpenSC 0.23.0 on Win11_x64 installed is working fine (no errors with pkcs11 and pkcs15).

Also the Cygwin documentation should be updated (--strict compiler flag not needed anymore, also uncommenting / setting CPPFLAGS in configure.ac obslete).

@Jakuje
Copy link
Member

Jakuje commented Nov 27, 2023

We will probably need some more logs to see what is going on there and why it fails.

If you mean the wiki by the documnetation, you can fix it already using a PR to https://github.com/OpenSC/Wiki/blob/master/Compiling-on-Cygwin.md as described in the footer of wiki.

@clauspruefer
Copy link
Author

I will provide the logs ASAP.

@clauspruefer
Copy link
Author

Is a PR possible by just creating a new branch and pull or do i need special permission?

@Jakuje
Copy link
Member

Jakuje commented Nov 27, 2023

Is a PR possible by just creating a new branch and pull or do i need special permission?

With normal GIthub workflow you should be good. You should be able to do the simple edits through the web UI too, which will end up in forking the project, creating a new branch with the changes and opening a new PR there.

@clauspruefer
Copy link
Author

Ah, ok: need to fork, that was the question.

@clauspruefer
Copy link
Author

Ok, PR has been created, logs following.

@Jakuje
Copy link
Member

Jakuje commented Nov 28, 2023

Thanks! What about the debug logs? Do they show something useful?

@clauspruefer
Copy link
Author

Sorry, due to usage of 0.22 on my current Win11 system (work) i have to compile on another machine. I suppose logs coming late this evening.

@clauspruefer
Copy link
Author

Apollogizes. SC-reader in second laptop unexpected not working anymore. Very annoying. Logs following, sorry again.

@clauspruefer
Copy link
Author

Apollogizes. SC-reader in second laptop unexpected not working anymore. Very annoying. Logs following, sorry again.

Still no progress, somehow reader seems broken :( ASAP logs follow.

@clauspruefer
Copy link
Author

@Jakuje Attached the logs.

@Jakuje
Copy link
Member

Jakuje commented Dec 4, 2023

Thanks! @CardContact do you see something obvious in the log that could be incompatible with SC-HSM in recent OpenSC versions?
From the log, I see this is version 3.4

P:841; T:0x42949672976 13:34:46.036 [opensc-pkcs11] card.c:382:sc_connect_card: card info name:'SmartCard-HSM version 3.4', type:26000, flags:0x0, max_send/recv_size:238/254

and what is failing is in pkcs15 emulator pulling of the CVC:

P:841; T:0x42949672976 13:34:46.066 [opensc-pkcs11] pkcs15-sc-hsm.c:1283:sc_pkcs15emu_sc_hsm_init: called
P:841; T:0x42949672976 13:34:46.066 [opensc-pkcs11] card.c:850:sc_select_file: called; type=1, path=e82b0601040181c31f0201::
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] card.c:885:sc_select_file: returning with: 0 (Success)
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] card.c:850:sc_select_file: called; type=0, path=2f02
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] apdu.c:550:sc_transmit_apdu: called
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] card.c:471:sc_lock: called
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] card.c:513:sc_lock: returning with: 0 (Success)
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] apdu.c:515:sc_transmit: called
P:841; T:0x42949672976 13:34:46.067 [opensc-pkcs11] apdu.c:363:sc_single_transmit: called
P:841; T:0x42949672976 13:34:46.068 [opensc-pkcs11] apdu.c:367:sc_single_transmit: CLA:0, INS:A4, P1:0, P2:0, data(2) 0x7ffff79d0
P:841; T:0x42949672976 13:34:46.068 [opensc-pkcs11] reader-pcsc.c:325:pcsc_transmit: reader 'Alcor Micro USB Smart Card Reader 0'
P:841; T:0x42949672976 13:34:46.068 [opensc-pkcs11] reader-pcsc.c:326:pcsc_transmit: 
Outgoing APDU (8 bytes):
00 A4 00 00 02 2F 02 FE ...../..

P:841; T:0x42949672976 13:34:46.068 [opensc-pkcs11] reader-pcsc.c:244:pcsc_internal_transmit: called
P:841; T:0x42949672976 13:34:46.082 [opensc-pkcs11] reader-pcsc.c:335:pcsc_transmit: 
Incoming APDU (2 bytes):
67 00 g.

P:841; T:0x42949672976 13:34:46.082 [opensc-pkcs11] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:841; T:0x42949672976 13:34:46.082 [opensc-pkcs11] apdu.c:539:sc_transmit: returning with: 0 (Success)
P:841; T:0x42949672976 13:34:46.082 [opensc-pkcs11] card.c:523:sc_unlock: called
P:841; T:0x42949672976 13:34:46.082 [opensc-pkcs11] iso7816.c:128:iso7816_check_sw: Wrong length
P:841; T:0x42949672976 13:34:46.082 [opensc-pkcs11] iso7816.c:741:iso7816_select_file: returning with: -1206 (Wrong length)
P:841; T:0x42949672976 13:34:46.083 [opensc-pkcs11] card.c:872:sc_select_file: 'SELECT' error: -1206 (Wrong length)
P:841; T:0x42949672976 13:34:46.083 [opensc-pkcs11] pkcs15-sc-hsm.c:256:read_file: Could not select EF
P:841; T:0x42949672976 13:34:46.083 [opensc-pkcs11] pkcs15-sc-hsm.c:262:read_file: Could not read EF
P:841; T:0x42949672976 13:34:46.083 [opensc-pkcs11] pkcs15-sc-hsm.c:418:sc_pkcs15emu_sc_hsm_decode_cvc: Could not decode card verifiable certificate: -1401 (Invalid ASN.1 object)
P:841; T:0x42949672976 13:34:46.083 [opensc-pkcs11] pkcs15-sc-hsm.c:1333:sc_pkcs15emu_sc_hsm_init: Could not decode EF.C_DevAut: -1401 (Invalid ASN.1 object)

@clauspruefer
Copy link
Author

clauspruefer commented Dec 5, 2023

But as i stated out "OpenSC 0.23.0 on Windows 11_x64" works like expected with the same SC-HSM card.
Maybe forgot setting an extended APDU length on CygWin build, as i remember there was a problem when the APDU size was too small with some SCs / Readers.

Could someone else please check with another SC-vendor on CygWin, thanx!

@Jakuje
Copy link
Member

Jakuje commented Dec 5, 2023

oh, missed that part that it works with different build. The logs looks like communication with the card is working ok. Could it be some missing dependency in the cygwin making some functionality unusable (zlib? openssl?). Can you compare output of the configure or config.h from our CI builds and from you cygwin?

@clauspruefer
Copy link
Author

oh, missed that part that it works with different build. The logs looks like communication with the card is working ok. Could it be some missing dependency in the cygwin making some functionality unusable (zlib? openssl?). Can you compare output of the configure or config.h from our CI builds and from you cygwin?

I will do so, i assume something is wrong with openssl changing to new api calls. APX 1-2 days...

@clauspruefer
Copy link
Author

oh, missed that part that it works with different build. The logs looks like communication with the card is working ok. Could it be some missing dependency in the cygwin making some functionality unusable (zlib? openssl?). Can you compare output of the configure or config.h from our CI builds and from you cygwin?

I will do so, i assume something is wrong with openssl changing to new api calls. APX 1-2 days...

Still in the pipe ... Too much projects in parallel ...

@clauspruefer
Copy link
Author

oh, missed that part that it works with different build. The logs looks like communication with the card is working ok. Could it be some missing dependency in the cygwin making some functionality unusable (zlib? openssl?). Can you compare output of the configure or config.h from our CI builds and from you cygwin?

Can you please state out where the CI build takes place / source / config files are located? I think i will write you some .md README files afterwards. I found the docker-container for building but no description how thw CI builds are triggered (or maybe i am blind).

@clauspruefer
Copy link
Author

oh, missed that part that it works with different build. The logs looks like communication with the card is working ok. Could it be some missing dependency in the cygwin making some functionality unusable (zlib? openssl?). Can you compare output of the configure or config.h from our CI builds and from you cygwin?

Can you please state out where the CI build takes place / source / config files are located? I think i will write you some .md README files afterwards. I found the docker-container for building but no description how thw CI builds are triggered (or maybe i am blind).

Ha, found it, just familiar with GitLab CI, github uses .github folder which is at the first position where programmers never have a look at 💣

@clauspruefer
Copy link
Author

@Jakuje Went through the CI build process (scripts), i have too less knowledge about github actions / CI. In which environment (Linux VM / Docker) the build-process is initiated?

I have some questions about the Mingw CI building.

  • What exactly does the Windown Minidriver? I could not find information on github.
  • Some questions about build.sh regarding Mingw integration
if [ "$1" == "mingw" -o "$1" == "mingw32" ]; then
	mkdir -p src/minidriver/CNG
	wget https://raw.githubusercontent.com/open-eid/minidriver/master/cardmod.h -O src/minidriver/CNG/cardmod.h
	if [ "$1" == "mingw" ]; then
		HOST=x86_64-w64-mingw32
	elif [ "$1" == "mingw32" ]; then
		HOST=i686-w64-mingw32
	fi
	unset CC
	unset CXX
	CFLAGS="-I$PWD/src/minidriver/CNG -Wno-error=unknown-pragmas" \
	CPPFLAGS="-DNTDDI_VERSION=0x06010000" \
	./configure --host=$HOST --with-completiondir=/tmp --disable-openssl --disable-readline --disable-zlib --enable-minidriver --enable-notify --prefix=$PWD/win32/opensc || cat config.log;
	make -j 4 V=1
	# no point in running tests on mingw

Why build with openssl and zlib disabled? ./configure --host=$HOST --with-completiondir=/tmp --disable-openssl --disable-readline --disable-zlib --enable-minidriver

if [ "$1" == "mingw" -o "$1" == "mingw32" ]; then
	# pack installed files
	wine "C:/Program Files/Inno Setup 5/ISCC.exe" win32/OpenSC.iss
fi

Whats happening here?

If you could provide me the information i am able to process much faster.

@Jakuje
Copy link
Member

Jakuje commented Dec 15, 2023

What exactly does the Windown Minidriver? I could not find information on github.

This is how opensc is integrated into the windows authentication system and tools. If you use pkcs11-tool, you will not get in touch with this. Generally windows provides some of its own, but allows third party to install their own, which is what OpenSC do:
https://learn.microsoft.com/en-us/windows-hardware/drivers/smartcard/smart-card-minidriver-overview

Why build with openssl and zlib disabled?

We likely do not have the dependencies under mingw under ubuntu.

Whats happening here?

This is Inno Setup, which craetes Windows installers. Its downloaded in here

if [ ! -f "$(winepath 'C:/Program Files/Inno Setup 5/ISCC.exe')" ]; then
/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16
export DISPLAY=:99.0
[ -d isetup ] || mkdir isetup
pushd isetup
[ -f isetup-5.5.6.exe ] || wget http:https://files.jrsoftware.org/is/5/isetup-5.5.6.exe
sleep 5 # make sure the X server is ready ?
wine isetup-5.5.6.exe /SILENT /VERYSILENT /SP- /SUPPRESSMSGBOXES /NORESTART
popd

@clauspruefer
Copy link
Author

clauspruefer commented Dec 15, 2023

What exactly does the Windown Minidriver? I could not find information on github.

This is how opensc is integrated into the windows authentication system and tools. If you use pkcs11-tool, you will not get in touch with this. Generally windows provides some of its own, but allows third party to install their own, which is what OpenSC do: https://learn.microsoft.com/en-us/windows-hardware/drivers/smartcard/smart-card-minidriver-overview

Understood.

But, can you please state out where and which container (dockerfile) will be started for the build / CI process. I do not find somehow. Additionally provide me with some helpful documentation links about the github CI so i am able to understand faster.

@Jakuje
Copy link
Member

Jakuje commented Dec 15, 2023

The mingw builds are defined here in the syntax defined for github actions, they run in ubuntu-latest container:

https://github.com/OpenSC/OpenSC/blob/master/.github/workflows/linux.yml#L76

But the scripts are independent from the github actions and could be executed locally with docker/podman as described here:

https://github.com/OpenSC/OpenSC/blob/master/containers/README.md

There is opensc-mingw Containerfile containing basically the same instructions, running the same scripts as github actions do run:

https://github.com/OpenSC/OpenSC/blob/master/containers/opensc-mingw/Containerfile

@dengert
Copy link
Member

dengert commented Dec 15, 2023

Can you provide opensc-debug log using the working version of OpenSC?
Your comment: #2944 (comment) may be close to the problem.

It could be a config option or some change in OpenSC that is being more conservative in 0.23.0 on setting the sizes.

In you log at line 1698:
card.c:382:sc_connect_card: card info name:'SmartCard-HSM version 3.4', type:26000, flags:0x0, max_send/recv_size:238/254

The max_send/recv_size:238/254 looks way to small.

On an older device with builtin reader on Ubuntu i see:
card.c:382:sc_connect_card: card info name:'SmartCard-HSM version 1.2', type:26000, flags:0x0, max_send/recv_size:1232/65534
The card and the reader can use extended APDUs i.e. greater then 255/256

Do you have a different reader?

Can you try try the same code on Windows 10? (Help isolate if w10 to w11 changed something about reasder capabilities)

@clauspruefer
Copy link
Author

Can you provide opensc-debug log using the working version of OpenSC? Your comment: #2944 (comment) may be close to the problem.

It could be a config option or some change in OpenSC that is being more conservative in 0.23.0 on setting the sizes.

In you log at line 1698: card.c:382:sc_connect_card: card info name:'SmartCard-HSM version 3.4', type:26000, flags:0x0, max_send/recv_size:238/254

The max_send/recv_size:238/254 looks way to small.

On an older device with builtin reader on Ubuntu i see: card.c:382:sc_connect_card: card info name:'SmartCard-HSM version 1.2', type:26000, flags:0x0, max_send/recv_size:1232/65534 The card and the reader can use extended APDUs i.e. greater then 255/256

I remember this is reader-driver-specific (i did this on a very old windows machine). The reader-hw settings are handled by OpenSC in Linux and CygWin similar? And if no what is the difference?

Do you have a different reader?

Yes, imported from our office, also i am able to compile on a different machine or VM. Results following.

Can you try try the same code on Windows 10? (Help isolate if w10 to w11 changed something about reasder capabilities)

Queued.

@dengert
Copy link
Member

dengert commented Dec 15, 2023

Google for: "alcor micro usb smart card reader"
There appears to be some updates for this reader for Windows.

Or try using device manager to look for new Windows 11 driver.

ALso try setting env OPENSC_DRIVER=sc-hsm to make sure OpenSC code trying to determine a card driver causes the the max_send_recv and max_recv_size to be set with with the strange numbers before the sc-hsm driver it tested.

@clauspruefer
Copy link
Author

clauspruefer commented Dec 15, 2023

Google for: "alcor micro usb smart card reader" There appears to be some updates for this reader for Windows.

Or try using device manager to look for new Windows 11 driver.

ALso try setting env OPENSC_DRIVER=sc-hsm to make sure OpenSC code trying to determine a card driver causes the the max_send_recv and max_recv_size to be set with with the strange numbers before the sc-hsm driver it tested.

On Win11 with with CygWin installed in parallel on Win11 native OpenSC 0.23.0 works fine, CygWin OpenSC 0.23.0 compiles without problems but does not work, OpenSC version before works with compatibility OpenSSL (old) API calls only.

@dengert
Copy link
Member

dengert commented Dec 15, 2023

That's good. By current, you mean 0.24.0 that was just released Dec 13, 2023?

If there is nothing else, you can close the ticket.

@dengert
Copy link
Member

dengert commented Dec 15, 2023

Here is something else to try to see if problems are with reader sizes:

card-max_sizes.diff.txt

It will list the sizes in opensc-debug log will trying to match a driver.

OPENSC_DRIVER=sc-hsm is a run time option to tell OpenSC to only try the sc-hsm driver.

@dengert
Copy link
Member

dengert commented Dec 29, 2023

Found some time. Works with CygWin with this patch: sc-hsm-cygwin.diff.txt
when running: pkcs11-tool --test --login

card-sc-hsm.c now set the max_send_size to at least 256, so the card can return data, When it was 254, the card would return errors, as suggested in #2944 (comment)

The change to reader-pcsc.c was to make sure some of the tests where still run even if 0x0000001 was returned.
The card driver will still use extended APDUs in some cases. for example sign. as the reader still supports them and can handle 64K packets.

I would like @istr and @clauspruefer to give this a try.

I still don't understand all of what aadd82b is trying to do.

@clauspruefer
Copy link
Author

I will give a try and report back.

@clauspruefer
Copy link
Author

Correct Version 0.24

$ pkcs11-tool -I
Cryptoki version 3.0
Manufacturer     OpenSC Project
Library          OpenSC smartcard framework (ver 0.24)
Using slot 0 with a present token (0x0)

Object List ok

  • one x.509 cert (for use with MIcrosoft CrytoAPI
  • one public SSH key (private hidden, no login given)
$ pkcs11-tool -O
Using slot 0 with a present token (0x0)
Certificate Object; type = X.509 cert
  label:      CP-RSA4096-sshKey
  subject:    DN: C=DE, ST=BW, L=Mannheim, O=clickIT, OU=Security, CN=c.pruefer.ssh/[email protected]
  serial:     8B65287CC95FDC3E
  ID:         01
Public Key Object; RSA 4096 bits
  label:      CP-RSA4096-sshKey
  ID:         01
  Usage:      encrypt, verify, verifyRecover
  Access:     local
Profile object 340144
  profile_id:          CKP_PUBLIC_CERTIFICATES_TOKEN (4)

@dengert Close issue or wait for istr reply?

@dengert
Copy link
Member

dengert commented Dec 30, 2023

The patch just proves one problem is making the max_recv_size to small and many commands would fail because the card would treat the 254 as an error. The card sill assumes it can use extended APDU and it does and the reader supports it. The problem on Windows is it can return dwMaxAPDUDataSize and the code then tries to force the 255/256 limits and further limited max_recv_size to 254 in sm-hsm.c

In other words, on windows OpenSC can not detect the reader sizes and should not set artificial limits. In the sc-hsm case the card drive to the most part assumes the reader will support extended APDU large enough for its needs. Other card drivers may fail back to using the 255/256 and use command chaining and get response to handle larger outgoing commands and incoming data.

Wait for @istr as there are still unanswered questions as to how to fix the symmetric key issue with read/write binary operations with odd number APDU instructions.

@clauspruefer
If you run with an opensc debug log and use pkcs11-tool --test --login, can you look for a simple sign operation.
With a 2048 bit RSA key which I have on my older token the easiest one would look like this:

Outgoing APDU (265 bytes):
80 68 02 20 00 01 00 00 01 FF FF FF FF FF FF FF .h. ............
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
...
4A 74 73 A1 C0 0B C1 02 00 

where 80 68 02 20 is the APDU, Lc 00 means extended APDU with length 01 00 for 256 bytes and the last two bytes are the extended Le 02 00 the max size to return.

With your 4096 bit key, the outgoing would be 521 bytes with Lc length of 02 00 and Le would still be 02 00 as it looks like the code just has a fixed size buffer and the card will return the actual size.

The response would look like:

Incoming APDU (258 bytes):
53 B5 AD 07 8B 71 8C F8 D1 A4 D6 57 6C 29 45 34 S....q.....Wl)E4
7B 1D 87 A5 82 67 E0 AC 6B 31 F0 07 AC F0 D4 8F {....g..k1......
...
90 00

But for 514 bytes different bytes, ending in 90 00

@clauspruefer
Copy link
Author

clauspruefer commented Dec 30, 2023

If you run with an opensc debug log and use pkcs11-tool --test --login, can you look for a simple sign operation.

On the run. I think it might also be helpful to check if i can gen a new key with sc-hsm-tool on another card, so i will try this first.

Important

Just using different card with existing RSA 4096 bit key.

Card / Key info

$ pkcs11-tool -l -O
Using slot 0 with a present token (0x0)
Logging in to "SmartCard-HSM (UserPIN)".
Please enter User PIN:
Private Key Object; RSA
label: Private Key
ID: 3b27dc55868a5802ddea5c32f46251d4c826341a
Usage: decrypt, sign, signRecover
Access: sensitive, always sensitive, never extractable, local
Public Key Object; RSA 4096 bits
label: Private Key
ID: 3b27dc55868a5802ddea5c32f46251d4c826341a
Usage: encrypt, verify, wrap
Access: none
Profile object 334720
profile_id: CKP_PUBLIC_CERTIFICATES_TOKEN (4)

Get signing mechs (Hardware)

$ pkcs11-tool -M

Using slot 0 with a present token (0x0)
Supported mechanisms:
RSA-PKCS, keySize={1024,4096}, hw, decrypt, sign, verify
RSA-PKCS-PSS, keySize={1024,4096}, hw, sign, verify

Sign with default (RSA-PKCS)

$ pkcs11-tool -l -s -m RSA-PKCS -d 3b27dc55868a5802ddea5c32f46251d4c826341a -i test-long.txt -o ./test.signed

test-long.txt

Outgoing APDU

Outgoing APDU (521 bytes):
80 68 01 20 00 02 00 00 01 FF FF FF FF FF FF FF .h. ............
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
FF FF FF FF FF FF FF FF 00 74 65 73 74 2D 64 61 .........test-da
74 61 20 74 6F 20 62 65 20 73 69 67 6E 65 64 20 ta to be signed
66 6F 72 20 4F 70 65 6E 53 43 20 43 79 67 77 69 for OpenSC Cygwi
6E 20 74 65 73 74 2E 2E 2E 20 23 31 32 33 34 35 n test... #12345
36 37 38 39 30 0D 0A 02 00                      67890....

Incoming APDU

Incoming APDU (514 bytes):
4B BF E4 EC 76 E3 99 AD DE 3E B9 A3 F3 73 70 35 K...v....>...sp5
7F 7C F8 F0 68 B5 A6 5F 9F 24 F3 A1 7F 0C F8 22 .|..h.._.$....."
4B 89 23 80 78 28 05 B5 FC 7B 88 ED 6A 18 8A 64 K.#.x(...{..j..d
0B 21 81 90 8D 0A 32 C2 E0 D8 75 D8 7D D5 96 6B .!....2...u.}..k
DE B2 9B 28 16 48 63 B3 FC CB A7 DD 26 FE 0D 99 ...(.Hc.....&...
00 83 15 34 3B 74 3B 38 B8 AE 17 F8 53 72 33 A6 ...4;t;8....Sr3.
EB 05 07 62 18 0D EF B6 8A 8F 81 D3 FF C1 4A 52 ...b..........JR
76 8A E3 A0 C5 B7 B3 B2 7F 6C FA E2 24 65 40 11 v........l..$e@.
0B 44 CF 1C 5F 8C BF 5B 66 BC 6F BF E4 81 F6 42 .D.._..[f.o....B
D4 22 E7 F4 EA 02 5F A2 4A B1 93 06 0A 08 0E 96 ."...._.J.......
70 F5 05 0D 9D 04 2E 11 A7 2E 8B BA C4 B3 44 3B p.............D;
A5 CB 98 47 62 A4 D5 2C F7 19 2C 56 47 87 96 D9 ...Gb..,..,VG...
B5 F7 1C 9C 9E 59 5B D3 3A CA 0E 5D 45 72 4D 2A .....Y[.:..]ErM*
70 8E 2C 31 32 34 90 6E 28 6E CB 64 39 4E 49 CE p.,124.n(n.d9NI.
92 75 F4 5C 91 48 03 EC 88 39 5F 4B E7 CC B3 CB .u.\.H...9_K....
E7 32 DC 7F 08 21 5A 94 3F B5 C2 1B 39 66 BD 3B .2...!Z.?...9f.;
8F 56 59 28 40 9D F5 7B CC E1 F3 46 A0 71 BB 50 .VY(@..{...F.q.P
18 98 80 2C 6B 22 DD 24 33 2E 87 7D F2 8F 3C ED ...,k".$3..}..<.
9D 20 AE 83 21 66 FA 2B 78 C7 E2 F1 5D 4E AC DD . ..!f.+x...]N..
62 39 D8 0A EB 2F 84 7B 68 A3 AA 36 F3 81 64 29 b9.../.{h..6..d)
71 00 64 7B 7A 39 48 5C BE 23 53 55 C4 6B A5 85 q.d{z9H\.#SU.k..
3C 49 8D 17 87 E8 67 AA 87 9F 00 D5 D3 23 E8 F4 <I....g......#..
8B 00 19 F1 80 A0 FE 34 CA 95 63 13 E5 38 A1 C4 .......4..c..8..
AF 12 6A D0 BD 17 38 29 76 22 7C 12 7E 9F 4B E9 ..j...8)v"|.~.K.
EC 21 45 17 8C 93 41 04 D5 30 08 A2 1D AC 30 4B .!E...A..0....0K
C7 69 C3 EF 0F 7F 94 45 1A 6C 6B 1F 84 72 99 FB .i.....E.lk..r..
75 EF DD E0 B7 FE 76 90 B2 72 26 DD 79 70 47 C8 u.....v..r&.ypG.
9D DC 02 8F C7 30 4E 4E EE 44 0A 5E 91 95 3C C1 .....0NN.D.^..<.
95 4F D0 6E 88 7B A6 4C EB AB A9 21 54 C4 46 B9 .O.n.{.L...!T.F.
D7 C4 C8 96 88 5B B6 23 75 40 06 62 6D FE 24 DB .....[.#[email protected].$.
3E D1 1B D3 56 52 5F 34 E3 B4 A9 C1 C1 E6 EC 6A >...VR_4.......j
AE 0A D4 D8 74 3B D5 01 E2 22 49 65 90 FE 09 72 ....t;..."Ie...r
90 00

@dengert
Copy link
Member

dengert commented Jan 4, 2024

@istr and @CardContact

Thanks for running the test with 4K key. So we are close.

The main problem is Windows does not support gpriv->SCardControl(card_handle, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0, buf, sizeof(buf), &rcount); and combined with aadd82b this causes CygWin commands certutil.exe /v /SCinfo to fail.

C:\Users\doug>certutil.exe /v -SCinfo
The Microsoft Smart Card Resource Manager is running.
Current reader/card status:
Readers: 1
  0: SCM Microsystems SCx35xx v2.0 USB SC Reader 0
--- Reader: SCM Microsystems SCx35xx v2.0 USB SC Reader 0
--- Status: SCARD_STATE_PRESENT | SCARD_STATE_UNPOWERED
--- Status: The card is available for use.
---   Card: SmartCard-HSM
---    ATR:
        3b fe 18 00 00 81 31 fe  45 80 31 81 54 48 53 4d   ;.....1.E.1.THSM
        31 73 80 21 40 81 07 fa                            1s.!@...


=======================================================
Analyzing card in reader: SCM Microsystems SCx35xx v2.0 USB SC Reader 0
P:21636; T:26224 2024-01-04 10:59:35.388 [cardmod] PKCS#15 init failed.
P:21636; T:26224 2024-01-04 10:59:35.640 [cardmod] PKCS#15 init failed.
P:21636; T:26224 2024-01-04 10:59:35.892 [cardmod] PKCS#15 init failed.
...

Changes in aadd82b in reader-pcsc.c cause the defaults of the card max_send_size and max_recv_size to be set to zero.

I have some questions about aadd82b and existing card-sc-hsm.c code:

  • OpenSC has routines sc_get_max_send_size(card) and sc_get_max_recv_size(card) why are these not used? They test if card driver has set SC_CARD_CAP_APDU_EXT.
  • Why the card->max_recv_size = card->reader->max_recv_size - 2; ? Three card drivers do this and one says for SW1 and SW2. But that is not the case, the send and receive sizes are size of data sent and received. reader-pcsc.c takes care of this.
  • // 17 byte header and TLV because of odd ins in UPDATE BINARY and card->max_send_size = card->reader->max_send_size - 17; Why the - 17? sc_hsm_write_ef takes care of the TLV header bytes. And I assume the extra 10 bytes is for SM?
  • In any case the card->max_recv_size should not be less then 256 as the card appears to have problems if it is less the 256.

@clauspruefer Thanks for running the test with 4K key. So we are close.

@frankmorgner
Copy link
Member

  • According to the SC-HSM user manual (I only had v2.4 at hand), SELECT doesn't seem to support a variable value for Le and MUST be 0x00 (or absent if P2 == 0x0C).
  • Le also MUST be exactly 0x00 for Generate Session PIN (INS=0x5A), General Authenticate (INS=86), Import DKEK (INS=52), Manage PKA (INS=54)
  • Interestingly, the Le for Enumerate Objects (sc_hsm_list_files), Generate Asymmetric Key Pair (sc_hsm_generate_keypair), Wrap Key (sc_hsm_wrap_key) similarly MUST be 0x000000 (i.e. extended length)

All of the above commands are explicitly setting the Le to 0 (and encode extended lenght if needed) except the select command, which instead calls the iso7816 driver for selecting. A quick way for fixing this, would be to force Le = 0 in the select:

diff --git a/src/libopensc/card-sc-hsm.c b/src/libopensc/card-sc-hsm.c
index e68975077..a236a2cbc 100644
--- a/src/libopensc/card-sc-hsm.c
+++ b/src/libopensc/card-sc-hsm.c
@@ -151,6 +151,8 @@ static int sc_hsm_select_file_ex(sc_card_t *card,
 	sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
 	sc_file_t *file = NULL;
 	sc_path_t cpath;
+	size_t card_max_recv_size = card->max_recv_size;
+	size_t reader_max_recv_size = card->reader->max_recv_size;
 
 	if (file_out == NULL) {				// Versions before 0.16 of the SmartCard-HSM do not support P2='0C'
 		rv = sc_hsm_select_file_ex(card, in_path, forceselect, &file);
@@ -184,7 +186,11 @@ static int sc_hsm_select_file_ex(sc_card_t *card,
 				&& in_path->aid.len == sc_hsm_aid.len
 				&& !memcmp(in_path->aid.value, sc_hsm_aid.value, sc_hsm_aid.len))) {
 		if (!priv || (priv->dffcp == NULL) || forceselect) {
+			/* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */
+			card->max_recv_size = card->reader->max_recv_size = 256;
 			rv = (*iso_ops->select_file)(card, in_path, file_out);
+			card->max_recv_size = card_max_recv_size;
+			card->reader->max_recv_size = reader_max_recv_size;
 			LOG_TEST_RET(card->ctx, rv, "Could not select SmartCard-HSM application");
 
 			if (priv) {
@@ -213,14 +219,24 @@ static int sc_hsm_select_file_ex(sc_card_t *card,
 			*file_out = file;
 			return SC_SUCCESS;
 		} else {
+			/* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */
+			card->max_recv_size = card->reader->max_recv_size = 256;
 			sc_path_t truncated;
 			memcpy(&truncated, in_path, sizeof truncated);
 			truncated.len = in_path->len - 2;
 			memcpy(truncated.value, in_path->value+2, truncated.len);
-			return (*iso_ops->select_file)(card, &truncated, file_out);
+			rv = (*iso_ops->select_file)(card, &truncated, file_out);
+			card->max_recv_size = card_max_recv_size;
+			card->reader->max_recv_size = reader_max_recv_size;
+			return rv;
 		}
 	}
-	return (*iso_ops->select_file)(card, in_path, file_out);
+	/* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */
+	card->max_recv_size = card->reader->max_recv_size = 256;
+	rv = (*iso_ops->select_file)(card, in_path, file_out);
+	card->max_recv_size = card_max_recv_size;
+	card->reader->max_recv_size = reader_max_recv_size;
+	return rv;
 }

An alternative would be to explicitly implement the APDU encoding for SELECT in sc-hsm instead of forwarding to iso7816.

I hope the above suggestion fixes the problem.

@dengert
Copy link
Member

dengert commented Jan 11, 2024

Looks reasonable. I don't have the manual. Does the SC-HSM manual talk about Le = 00 with short APDUs and use of get-response? In which case OpenSC apdu.c would do the get response. Or does SC-HSM require the use of an extended reader?
The reader in question does support extended APDUs, as the circumvention of setting 65535/65535 works.

The main problem is OpenSC has no way on Windows to query the reader for is max sizes and the card-sc-hsm.c sets max sizes less then 255/256

@frankmorgner
Copy link
Member

The manual does not mention GET RESPONSE and also doesn't mention command chaining for anything else than device authentication. If I remember correctly, @CardContact already confirmed that there is no support for this and that extended length APDUs are indeed required in cases with big chunks of data.

frankmorgner added a commit to frankmorgner/OpenSC that referenced this issue Jan 11, 2024
@frankmorgner
Copy link
Member

I've created a PR, so that you can pick binaries from the CI pipeline for testing #2978

@frankmorgner frankmorgner changed the title OpenSC 0.23.0 broken on Cygwin current SC-HSM: OpenSC 0.23.0 broken on Cygwin current Jan 12, 2024
@frankmorgner
Copy link
Member

@clauspruefer would you mind testing the suggested fix, please?

@clauspruefer
Copy link
Author

clauspruefer commented Jan 29, 2024 via email

@frankmorgner
Copy link
Member

With "the last patch" you mean #2978?

@clauspruefer
Copy link
Author

clauspruefer commented Jan 29, 2024 via email

@clauspruefer
Copy link
Author

The diff attached to the comment from dengert: #2944 (comment)

@frankmorgner
Copy link
Member

then please Test the above pull request or binaries
#2944 (comment)

This should be the correct fix

@dengert
Copy link
Member

dengert commented Jan 31, 2024

The real problem is part10_find_property_by_tag PCSCv2_PART10_PROPERTY_dwMaxAPDUDataSize is not from the reader hardware but from PCSC, CCID and/or IFD software. Microsoft has not implemented it. It is only in PCSClite.
The code in reader-pcsc.c does not handle the failure correctly and dies not check for a few known readers.

As @LudovicRousseau said in: #2944 (comment) PCSClite implements PCSCv2_PART10_PROPERTY_dwMaxAPDUDataSize

It is not just a Cygwin problem, it is a problem for OpenSC code run on windows both which use reader-pcsc.c which will fail on Windows. It may affect other card drivers too.
If we really wanted it to work, we would add other readers to the list in reader-pcsc.c.

See: https://github.com/OpenSC/OpenSC/files/13796437/sc-hsm-cygwin.diff.txt which handles the failure better.

@frankmorgner
Copy link
Member

Sorry, but I disagree. The real problem (tm) seems to be that SELECT is called with Le!=0, which is caused by not having PCSCv2_PART10_PROPERTY_dwMaxAPDUDataSize at hand. We should be able to cope with PCSCv2_PART10_PROPERTY_dwMaxAPDUDataSize unset or even set incorrectly on all PC/SC platforms, but SELECT should always be issued with Le=0, as required by the SC-HSM specification.

@dengert
Copy link
Member

dengert commented Jan 31, 2024

My point in https://github.com/OpenSC/OpenSC/files/13796437/sc-hsm-cygwin.diff.txt is that when
SCardControl(card_handle, CM_IOCTL_GET_FEATURE_REQUEST,... fails at:
https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/reader-pcsc.c#L1153

the 178 lines of code:
https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/reader-pcsc.c#L1158-L1337
are not executed, which includes:

  • code to test for specific readers
  • pin pad info (probably why pin pad readers don't work in Windows)
  • part10_detect_pace_capabilities(reader, card_handle);
  • max_send_size = max_recv_size = part10_detect_max_data(reader, card_handle);
  • other code to set max_send_size and max_recv_size which may end in "Assuming that the reader supports sending short length APDUs only"

This results in a card driver like card-sc-hsm.c expects the reader in the token to support extended APDUs that the reader does not.

This lack of the features from PCSClite in the Windows implementation could be addressed if OpenSC took some of the changes from PCSClite to fill in info for readers. Or at least look in opensc.conf

This problem may be the same problem as in #3004 and other card drivers may have similar problems.

(I actually found that building OpenSC from github using Cygwin was easy and could debug using gdb)

@frankmorgner
Copy link
Member

Thats indeed another Problem for which ive created a seperate issue

@Jakuje
Copy link
Member

Jakuje commented Feb 7, 2024

What is the status of this issue? Was it fixed with #3006 ? Is it fixed with #2978 ?

@dengert
Copy link
Member

dengert commented Feb 7, 2024

I don't know.

@frankmorgner
Copy link
Member

both patches are needed to reliably fix this in all situations

frankmorgner added a commit that referenced this issue Feb 13, 2024
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.

6 participants