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

OpenSC Minidriver Does Not Display the Second Key Container of JPKI Card When certutil -scinfo Is Executed #3169

Open
sdy623 opened this issue Jun 3, 2024 · 30 comments · May be fixed by #3182

Comments

@sdy623
Copy link

sdy623 commented Jun 3, 2024

Problem Description

Overview:

When using the OpenSC minidriver with a JPKI card, pkcs15-tool --list-keys correctly displays both key containers. However, when running certutil -scinfo, only the first key container is displayed, and the second key (Digital Signature Key) is not recognized. This issue may relate to the security handling or the minidriver's implementation for the JPKI card.

Using reader with a card: Hitachi/Maxell M-500U/M-520U 0
Private RSA Key [User Authentication Key]
        Object Flags   : [0x01], private
        Usage          : [0x04], sign
        Access Flags   : [0x1D], sensitive, alwaysSensitive, neverExtract, local
        Algo_refs      : 0
        ModLength      : 2048
        Key ref        : 1 (0x01)
        Native         : yes
        Auth ID        : 01
        ID             : 01
        MD:guid        : c5a0a252-9d2d-eb60-fec0-41b4fbd722a2

Private RSA Key [Digital Signature Key]
        Object Flags   : [0x01], private
        Usage          : [0x204], sign, nonRepudiation
        Access Flags   : [0x1D], sensitive, alwaysSensitive, neverExtract, local
        Algo_refs      : 0
        ModLength      : 2048
        Key ref        : 2 (0x02)
        Native         : yes
        Auth ID        : 02
        ID             : 02
        MD:guid        : e1bc1dae-59f1-16ab-b43f-9dafbb2acc9b

Details:

Reader and Card Used: Hitachi/Maxell M-500U/M-520U 0
Operating System and Version: Windows 11 22631.3672
OpenSC Version: 0.25.1.0
Other Tools/Aplications: Windows certutil
Error Messages:

certutil -scinfo does not display the Digital Signature Key, although pkcs15-tool --list-keys lists it as available.

The Microsoft Smart Card Resource Manager is running.
Current reader/card status:
Readers: 2
  0: Hitachi/Maxell M-500U/M-520U 0
--- Reader: Hitachi/Maxell M-500U/M-520U 0
--- Status: SCARD_STATE_PRESENT | SCARD_STATE_UNPOWERED
--- Status: The card is available for use.
---   Card: JPKI-2
---    ATR:
        3b da 13 ff 81 31 fb 46  80 12 39 2f 31 c1 73 c6   ;....1.F..9/1.s.
        01 c0 3b                                           ..;

=======================================================
Analyzing card in reader: Hitachi/Maxell M-500U/M-520U 0

--------------===========================--------------
================ Certificate 0 ================
--- Reader: Hitachi/Maxell M-500U/M-520U 0
---   Card: JPKI-2
Provider = Microsoft Base Smart Card Crypto Provider
Key Container = (null) [Default Container]

Serial Number: 06d90f3b
Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
 NotBefore: 2023/08/17 1:31
 NotAfter: 2024/10/01 23:59
Subject: CN=28*****, C=JP
Non-root Certificate
Cert Hash(sha1): 7c3**********

Performing AT_SIGNATURE public key matching test...
Public key matching test succeeded
  Key Container = c5a0a252-9d2d-eb60-fec0-41b4fbd722a2
  Provider = Microsoft Base Smart Card Crypto Provider
  ProviderType = 1
  Flags = 1
    0x1 (1)
  KeySpec = 2 -- AT_SIGNATURE
Private key verifies

Performing cert chain verification...
CertGetCertificateChain(dwErrorStatus) = 0x1010040
Chain on smart card is invalid
dwFlags = CA_VERIFY_FLAGS_CONSOLE_TRACE (0x20000000)
dwFlags = CA_VERIFY_FLAGS_DUMP_CHAIN (0x40000000)
ChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT (0x40000000)
HCCE_LOCAL_MACHINE
CERT_CHAIN_POLICY_BASE
-------- CERT_CHAIN_CONTEXT --------
ChainContext.dwErrorStatus = CERT_TRUST_REVOCATION_STATUS_UNKNOWN (0x40)
ChainContext.dwErrorStatus = CERT_TRUST_IS_OFFLINE_REVOCATION (0x1000000)
ChainContext.dwErrorStatus = CERT_TRUST_IS_PARTIAL_CHAIN (0x10000)
SimpleChain.dwErrorStatus = CERT_TRUST_REVOCATION_STATUS_UNKNOWN (0x40)
SimpleChain.dwErrorStatus = CERT_TRUST_IS_OFFLINE_REVOCATION (0x1000000)
SimpleChain.dwErrorStatus = CERT_TRUST_IS_PARTIAL_CHAIN (0x10000)

CertContext[0][0]: dwInfoStatus=1 dwErrorStatus=1000040
  Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
  NotBefore: 2023/08/17 1:31
  NotAfter: 2024/10/01 23:59
  Subject: CN=2867*********, C=JP
  Serial: 06d90f3b
  Cert: 7c339b********
  Element.dwInfoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER (0x1)
  Element.dwErrorStatus = CERT_TRUST_REVOCATION_STATUS_UNKNOWN (0x40)
  Element.dwErrorStatus = CERT_TRUST_IS_OFFLINE_REVOCATION (0x1000000)
  Issuance[0] = 1.2.392.200149.8.5.1.3.30
  Application[0] = 1.3.6.1.5.5.7.3.2 Client Authentication

Exclude leaf cert:
  Chain: da*******
Full chain:
  Chain: 7c339b********
Missing Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
  Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
  NotBefore: 2023/08/17 1:31
  NotAfter: 2024/10/01 23:59
  Subject: CN=2867*******, C=JP
  Serial: 06d90f3b
  Cert: 7c339b********
A certificate chain could not be built to a trusted root authority. 0x800b010a (-2146762486 CERT_E_CHAINING)
------------------------------------
Incomplete certificate chain
Cannot find certificate:
    OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
Displayed AT_SIGNATURE cert for reader: Hitachi/Maxell M-500U/M-520U 0
No AT_KEYEXCHANGE key for reader: Hitachi/Maxell M-500U/M-520U 0

--------------===========================--------------
================ Certificate 0 ================
--- Reader: Hitachi/Maxell M-500U/M-520U 0
---   Card: JPKI-2
Provider = Microsoft Smart Card Key Storage Provider
Key Container = c5a0a252-9d2d-eb60-fec0-41b4fbd722a2

Serial Number: 06d90f3b
Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
 NotBefore: 2023/08/17 1:31
 NotAfter: 2024/10/01 23:59
Subject: CN=286794E78GACEN13116003A, C=JP
Non-root Certificate
Cert Hash(sha1): 7c339b********

Performing  public key matching test...
Public key matching test succeeded
  Key Container = c5a0a252-9d2d-eb60-fec0-41b4fbd722a2
  Provider = Microsoft Smart Card Key Storage Provider
  ProviderType = 0
  Flags = 1
    0x1 (1)
  KeySpec = 0 -- XCN_AT_NONE
Private key verifies
Microsoft Smart Card Key Storage Provider: KeySpec=0
AES256+RSAES_OAEP(RSA:CNG) test skipped

Performing cert chain verification...
CertGetCertificateChain(dwErrorStatus) = 0x1010040
Chain on smart card is invalid
dwFlags = CA_VERIFY_FLAGS_CONSOLE_TRACE (0x20000000)
dwFlags = CA_VERIFY_FLAGS_DUMP_CHAIN (0x40000000)
ChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT (0x40000000)
HCCE_LOCAL_MACHINE
CERT_CHAIN_POLICY_BASE
-------- CERT_CHAIN_CONTEXT --------
ChainContext.dwErrorStatus = CERT_TRUST_REVOCATION_STATUS_UNKNOWN (0x40)
ChainContext.dwErrorStatus = CERT_TRUST_IS_OFFLINE_REVOCATION (0x1000000)
ChainContext.dwErrorStatus = CERT_TRUST_IS_PARTIAL_CHAIN (0x10000)
SimpleChain.dwErrorStatus = CERT_TRUST_REVOCATION_STATUS_UNKNOWN (0x40)
SimpleChain.dwErrorStatus = CERT_TRUST_IS_OFFLINE_REVOCATION (0x1000000)
SimpleChain.dwErrorStatus = CERT_TRUST_IS_PARTIAL_CHAIN (0x10000)

CertContext[0][0]: dwInfoStatus=1 dwErrorStatus=1000040
  Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
  NotBefore: 2023/08/17 1:31
  NotAfter: 2024/10/01 23:59
  Subject: CN=286794E78GACEN13116003A, C=JP
  Serial: 06d90f3b
  Cert: 7c339b********
  Element.dwInfoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER (0x1)
  Element.dwErrorStatus = CERT_TRUST_REVOCATION_STATUS_UNKNOWN (0x40)
  Element.dwErrorStatus = CERT_TRUST_IS_OFFLINE_REVOCATION (0x1000000)
  Issuance[0] = 1.2.392.200149.8.5.1.3.30
  Application[0] = 1.3.6.1.5.5.7.3.2 Client Authentication

Exclude leaf cert:
  Chain: da39*****
Full chain:
  Chain: 7c339b********
Missing Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
  Issuer: OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
  NotBefore: 2023/08/17 1:31
  NotAfter: 2024/10/01 23:59
  Subject: CN=286794E78GACEN13116003A, C=JP
  Serial: 06d90f3b
  Cert: 7c339b4c8f5d33dfc8081f3a504fe027852f1858
A certificate chain could not be built to a trusted root authority. 0x800b010a (-2146762486 CERT_E_CHAINING)
------------------------------------
Incomplete certificate chain
Cannot find certificate:
    OU=Japan Agency for Local Authority Information Systems, OU=JPKI for user authentication, O=JPKI, C=JP
Displayed  cert for reader: Hitachi/Maxell M-500U/M-520U 0

--------------===========================---------------

Done.
CertUtil: -SCInfo command completed successfully.

Steps to Reproduce

Setup Environment: Insert JPKI card into the reader (Hitachi/Maxell M-500U/M-520U 0).
List Keys Using OpenSC Tool: Run pkcs15-tool --list-keys. Both keys should be displayed as listed in the problem description.
Verify with Windows Tool: Run certutil -scinfo on the same setup.
Observe the Output: Note that only the first key (User Authentication Key) is displayed, and the second key (Digital Signature Key) is missing from the output.
Expected Behavior
certutil -scinfo should display both two JPKI certs as does pkcs15-tool --list-keys, indicating proper interaction and recognition by the minidriver and the OS.

Actual Behavior

Only the first cert is displayed in the certutil -scinfo output, indicating a potential issue in the minidriver’s handling of multiple keys or specific security protocols for the second key container.
Screenshot 2024-06-03 095735_edit

Additional Info:

According to the JPKI protocol, reading the Digital Signature Public Key (bContainerIndex=1) before READ BINARY: 00 B0 00 00 04 requires a PIN challenge:

SELECT FILE: Public Personal Authentication Application

Command: 00 A4 04 0C 0A D3 92 F0 00 26 01 00 00 00 01
Response: 90 00
SELECT FILE: Signature PIN

Command: 00 A4 02 0C 02 00 1B
Response: 90 00
VERIFY: Signature PIN (Password=123456)

Command: 00 20 00 80 06 31 32 33 34 35 36
Response: 90 00
SELECT FILE: Signature Certificate

Command: 00 A4 02 0C 02 00 01
Response: 90 00
READ BINARY: Read the first 4 bytes to determine the certificate's byte length

Command: 00 B0 00 00 04
Response: 30 82 06 CA 90 00
READ BINARY: Read the full certificate data (excluding the first 4 bytes, remaining 0x06CA bytes)

Command: 00 B0 00 04 00 06 CA
Response: 30 82 ...certificate data... 90 00

This discrepancy in behavior between pkcs15-tool and certutil -scinfo may indicate an issue with how the OpenSC minidriver is handling the card's security protocols or with the implementation of the JPKI card support.

Logs

https://pastebin.com/fuRSSLeY

Outgoing APDU (5 bytes):
00 B0 00 00 04 .....

P:39236; T:5172 2024-06-03 01:53:11.516 [cardmod] reader-pcsc.c:244:pcsc_internal_transmit: called
P:39236; T:5172 2024-06-03 01:53:11.529 [cardmod] reader-pcsc.c:334:pcsc_transmit:
Incoming APDU (2 bytes):
69 82 i.

P:39236; T:5172 2024-06-03 01:53:11.532 [cardmod] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:39236; T:5172 2024-06-03 01:53:11.533 [cardmod] apdu.c:539:sc_transmit: returning with: 0 (Success)
P:39236; T:5172 2024-06-03 01:53:11.535 [cardmod] card.c:523:sc_unlock: called
P:39236; T:5172 2024-06-03 01:53:11.537 [cardmod] Security status not satisfied
P:39236; T:5172 2024-06-03 01:53:11.539 [cardmod] iso7816.c:162:iso7816_read_binary: Check SW error: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.541 [cardmod] card.c:523:sc_unlock: called
P:39236; T:5172 2024-06-03 01:53:11.542 [cardmod] card.c:663:sc_read_binary: returning with: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.545 [cardmod] card-jpki.c:197:jpki_select_file: SW Check failed: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.547 [cardmod] card.c:872:sc_select_file: 'SELECT' error: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.550 [cardmod] card.c:523:sc_unlock: called
P:39236; T:5172 2024-06-03 01:53:11.552 [cardmod] pkcs15.c:2634:sc_pkcs15_read_file: returning with: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.554 [cardmod] pkcs15-pubkey.c:963:sc_pkcs15_read_pubkey: Failed to read public key file.: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.557 [cardmod] pkcs15-pubkey.c:984:sc_pkcs15_read_pubkey: returning with: -1211 (Security status not satisfied)
P:39236; T:5172 2024-06-03 01:53:11.561 [cardmod] public key read error -1211
P:39236; T:5172 2024-06-03 01:53:11.562 [cardmod] now read certificate 'Digital Signature Certificate'
@frankmorgner
Copy link
Member

This is deliberate, because the DS certificate requires authentication before read out is permitted:

SC_PKCS15_CO_FLAG_PRIVATE,

Not sure if this is supported by Windows in general, but you can try to set the option pin_protected_certificate to declassify.

@sdy623
Copy link
Author

sdy623 commented Jun 5, 2024

How can I edit the minidriver.c to make a PIN challenge before read.

@frankmorgner
Copy link
Member

Have you tried setting pin_protected_certificate (was previously name private_certificate) in the config file?

@sdy623
Copy link
Author

sdy623 commented Jun 5, 2024

where is the config file?

@dengert
Copy link
Member

dengert commented Jun 5, 2024

Does it prompt for the user PIN at all? In your comment you say: Command: 00 20 00 80 06 31 32 33 34 35 36 which looks like the card authentication PIN. Can you post the full opensc-debug log as there is a lot more information and even more if you set opensc_debug = 9.

As noted in the code the user sign key has user_consent = 1, but minidriver in not handling this, and it looks like card is enforcing reading of certificate without some PIN authentication. If the vendor's minidriver can work correctly, we can get OpenSC minidriver to work.

As I said in some other comment this is very similar to #3159.

@dengert

This comment was marked as duplicate.

1 similar comment
@dengert

This comment was marked as duplicate.

@dengert
Copy link
Member

dengert commented Jun 5, 2024

Try pin_protected_certificate first.

In your comment you say: "Command: 00 20 00 80 06 31 32 33 34 35 36which looks like the card authentication PIN. Can you post the full opensc-debug log as there is a lot more information and even more if you setopensc_debug = 9`.

As noted in the code the user sign key has user_consent = 1, but minidriver in not handling this, and it looks like card is enforcing reading of certificate without some PIN authentication. If the vendor's minidriver can work correctly, we can get OpenSC minidriver to work.

As I said in some other comment this is very similar to #3159.

@dengert

This comment was marked as duplicate.

@dengert

This comment was marked as duplicate.

@dengert

This comment was marked as duplicate.

@dengert
Copy link
Member

dengert commented Jun 5, 2024

Sorry about multiple posts. Github was very slow and said: "There was a problem saving your comment. Please try again."
looks like it sent the mail...

@sdy623
Copy link
Author

sdy623 commented Jun 6, 2024

Try pin_protected_certificate first.

In your comment you say: "Command: 00 [20 00 80 06 31 32 33](tel:20 00 80 06 31 32 33) 34 35 36which looks like the card authentication PIN. Can you post the full opensc-debug log as there is a lot more information and even more if you setopensc_debug = 9`.

As noted in the code the user sign key has user_consent = 1, but minidriver in not handling this, and it looks like card is enforcing reading of certificate without some PIN authentication. If the vendor's minidriver can work correctly, we can get OpenSC minidriver to work.

As I said in some other comment this is very similar to #3159.

Yes the PIN is a example of 123456 the second slot requires the PIN before reading, the command was copied from another bogs showing the process of auth in Japanese.

@sdy623 sdy623 closed this as completed Jun 6, 2024
@sdy623 sdy623 reopened this Jun 6, 2024
@sdy623
Copy link
Author

sdy623 commented Jun 8, 2024

@dengert I edited the opensc.conf, but it is not work.

app default {
    framework pkcs15 {
        card_drivers JPKI {
            pin_protected_certificate = protect;
        }
    }
}

@dengert
Copy link
Member

dengert commented Jun 8, 2024

@sdy623 please have a look at: #3167 (comment)

@sdy623
Copy link
Author

sdy623 commented Jun 9, 2024

Thank you, I want to try download your form and compile it

@dengert
Copy link
Member

dengert commented Jun 9, 2024

You can use git clone to download the source from my branch: https://github.com/dengert/OpenSC/tree/minidriver-PinCacheAlwaysPrompt

I normally do not build on windows, but use the the builds built on github and download 2 msi files for win32 and win64. See #3159 (comment)
and look at:
#3167
the latest jobs are at https://ci.appveyor.com/project/frankmorgner/opensc/builds/49980739

@sdy623
Copy link
Author

sdy623 commented Jun 10, 2024

@dengert I test with this, unfortunately it didn't work. Same error of I posted

P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] pkcs15.c:2998:sc_pkcs15_get_object_guid: called
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] pkcs15.c:3076:sc_pkcs15_aux_get_md_guid: called
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] pkcs15.c:3085:sc_pkcs15_aux_get_md_guid: returning with: -1408 (Not supported)
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] pkcs15.c:3063:sc_pkcs15_get_object_guid: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] Container[0] is 'User Authentication Key' guid=c5a0a252-9d2d-eb60-fec0-41b4fbd722a2
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] Container[0] is secured by User PIN
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] Container[0]'s key-exchange:0, sign:2048
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] found certificate friend 'User Authentication Certificate'
P:17092; T:6420 2024-06-10 11:48:24.805 [cardmod] found public key friend 'User Authentication Public Key'
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Is GUID has to be used as ID of crypto objects?
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Match ATR:
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] --- 00000007FF27DD90:19
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod]  0000  3BDA13FF 8131FB46 8012392F 31C173C6  01C03B
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] flag_name:md_guid_as_id:FALSE
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Is GUID has to be used as label of crypto objects?
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Match ATR:
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] --- 00000007FF27DD90:19
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod]  0000  3BDA13FF 8131FB46 8012392F 31C173C6  01C03B
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] flag_name:md_guid_as_label:FALSE
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] pkcs15.c:2998:sc_pkcs15_get_object_guid: called
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] pkcs15.c:3076:sc_pkcs15_aux_get_md_guid: called
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] pkcs15.c:3085:sc_pkcs15_aux_get_md_guid: returning with: -1408 (Not supported)
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] pkcs15.c:3063:sc_pkcs15_get_object_guid: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Container[1] is 'Digital Signature Key' guid=e1bc1dae-59f1-16ab-b43f-9dafbb2acc9b
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Container[1] is secured by Sign PIN
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] key_obj->user_consent: 1
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] vs->need_pin_always 1
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] Container[1]'s key-exchange:0, sign:2048
P:17092; T:6420 2024-06-10 11:48:24.806 [cardmod] found certificate friend 'Digital Signature Certificate'
P:17092; T:6420 2024-06-10 11:48:24.807 [cardmod] found public key friend 'Digital Signature Public Key'
P:17092; T:6420 2024-06-10 11:48:24.807 [cardmod] Container[0] promoted to default
P:17092; T:6420 2024-06-10 11:48:09.852 [cardmod] CardGetContainerInfo bContainerIndex=1, dwFlags=0x00000000, dwVersion=1, cbSigPublicKey=0, cbKeyExPublicKey=0
P:17092; T:6420 2024-06-10 11:48:09.852 [cardmod] now read public key 'Digital Signature Public Key'
P:17092; T:6420 2024-06-10 11:48:09.852 [cardmod] pkcs15-pubkey.c:900:sc_pkcs15_read_pubkey: called
P:17092; T:6420 2024-06-10 11:48:09.852 [cardmod] Public key type 0x201
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] Read from EF and decode
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] pkcs15.c:2482:sc_pkcs15_read_file: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] path=0001, index=0, count=-1
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card.c:471:sc_lock: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card-jpki.c:358:jpki_card_reader_lock_obtained: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card-jpki.c:364:jpki_card_reader_lock_obtained: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] called; type=0, path=0001
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card-jpki.c:149:jpki_select_file: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] jpki_select_file: path=0001, len=2
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] apdu.c:550:sc_transmit_apdu: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card.c:471:sc_lock: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] apdu.c:515:sc_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] apdu.c:363:sc_single_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] CLA:0, INS:A4, P1:2, P2:C, data(2) 000001F8B7D09D70
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] reader 'Hitachi/Maxell M-500U/M-520U 0'
P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] reader-pcsc.c:328:pcsc_transmit: 
Outgoing APDU (7 bytes):
00 A4 02 0C 02 00 01 .......

P:17092; T:6420 2024-06-10 11:48:09.853 [cardmod] reader-pcsc.c:245:pcsc_internal_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] reader-pcsc.c:337:pcsc_transmit: 
Incoming APDU (2 bytes):
90 00 ..

P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] apdu.c:539:sc_transmit: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] card.c:523:sc_unlock: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] called; 4 bytes at index 0
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] card.c:471:sc_lock: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] apdu.c:550:sc_transmit_apdu: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] card.c:471:sc_lock: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] apdu.c:515:sc_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] apdu.c:363:sc_single_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] CLA:0, INS:B0, P1:0, P2:0, data(0) 0000000000000000
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] reader 'Hitachi/Maxell M-500U/M-520U 0'
P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] reader-pcsc.c:328:pcsc_transmit: 
Outgoing APDU (5 bytes):
00 B0 00 00 04 .....

P:17092; T:6420 2024-06-10 11:48:09.867 [cardmod] reader-pcsc.c:245:pcsc_internal_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] reader-pcsc.c:337:pcsc_transmit: 
Incoming APDU (2 bytes):
69 82 i.

P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] apdu.c:539:sc_transmit: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] card.c:523:sc_unlock: called
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] Security status not satisfied
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] iso7816.c:162:iso7816_read_binary: Check SW error: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] card.c:523:sc_unlock: called
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] card.c:663:sc_read_binary: returning with: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] card-jpki.c:197:jpki_select_file: SW Check failed: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] card.c:872:sc_select_file: 'SELECT' error: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] card.c:523:sc_unlock: called
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] pkcs15.c:2644:sc_pkcs15_read_file: returning with: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] pkcs15-pubkey.c:963:sc_pkcs15_read_pubkey: Failed to read public key file.: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] pkcs15-pubkey.c:984:sc_pkcs15_read_pubkey: returning with: -1211 (Security status not satisfied)
P:17092; T:6420 2024-06-10 11:48:09.880 [cardmod] public key read error -1211
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] now read certificate 'Digital Signature Certificate'
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] pkcs15-cert.c:381:sc_pkcs15_read_certificate: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] pkcs15.c:2482:sc_pkcs15_read_file: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] path=0001, index=0, count=-1
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card.c:471:sc_lock: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card-jpki.c:358:jpki_card_reader_lock_obtained: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card-jpki.c:364:jpki_card_reader_lock_obtained: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] called; type=0, path=0001
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card-jpki.c:149:jpki_select_file: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] jpki_select_file: path=0001, len=2
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] apdu.c:550:sc_transmit_apdu: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card.c:471:sc_lock: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] apdu.c:515:sc_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] apdu.c:363:sc_single_transmit: called
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] CLA:0, INS:A4, P1:2, P2:C, data(2) 000001F8B7CBDF20
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] reader 'Hitachi/Maxell M-500U/M-520U 0'
P:17092; T:6420 2024-06-10 11:48:09.881 [cardmod] reader-pcsc.c:328:pcsc_transmit: 
P:17092; T:6420 2024-06-10 11:48:24.727 [cardmod] PKCS#15 options: use_file_cache=1 use_pin_cache=1 pin_cache_counter=10 pin_cache_ignore_user_consent=0 pin_protected_certificate=0

@dengert
Copy link
Member

dengert commented Jun 10, 2024

@hamano Can you comment on this issue with JPKI and OpenSC minidriver?
The card appears to have the sign pubkey pin protected?
Does the card also have the sign certificate protected?

The JPKI only card is the only card that not not make certificates and pubkeys readable without having to authenticate.

@sdy623 can you try running these commands:

pkcs15-tool -c 
pkcs15-tool --list-public-keys
pkcs15-tool --read-certificate 01
pkcs15-tool --read-certificate 01 --verify-pin
(remove card and reinsert to make sure logged out)

pkcs15-tool --read-certificate 02
pkcs15-tool --read-certificate 02 --verify-pin --auth_id 02

(remove card and reinsert to make sure logged out)
pkcs15-tool --read-public-key 01
pkcs15-tool --read-public-key 01--verify-pin

(remove card and reinsert to make sure logged out)
pkcs15-tool --read-public-key 02
pkcs15-tool --read-public-key 02 --verify-pin --auth_id 02

We are looking to see if you can read certificates and pubkeys without logging in.

@sdy623
Copy link
Author

sdy623 commented Jun 11, 2024

pkcs15-tool --read-certificate 01                                                       
Using reader with a card: Hitachi/Maxell M-500U/M-520U 0
Connecting to card in reader Hitachi/Maxell M-500U/M-520U 0...
Using card driver JPKI(Japanese Individual Number Cards).
-----BEGIN CERTIFICATE-----
 pkcs15-tool --read-certificate 02                                                       
Using reader with a card: Hitachi/Maxell M-500U/M-520U 0
Connecting to card in reader Hitachi/Maxell M-500U/M-520U 0...
Using card driver JPKI(Japanese Individual Number Cards).
Certificate read failed: Security status not satisfied

@sdy623
Copy link
Author

sdy623 commented Jun 11, 2024

 pkcs15-tool --read-certificate 02 --verify-pin
Using reader with a card: Hitachi/Maxell M-500U/M-520U 0
Connecting to card in reader Hitachi/Maxell M-500U/M-520U 0...
Using card driver JPKI(Japanese Individual Number Cards).
Please enter PIN [User Authentication PIN]: Certificate read failed: Security status not satisfied
 pkcs15-tool --read-public-key 01
Using reader with a card: Hitachi/Maxell M-500U/M-520U 0
Connecting to card in reader Hitachi/Maxell M-500U/M-520U 0...
Using card driver JPKI(Japanese Individual Number Cards).
Public key enumeration failed: Required ASN.1 object not found

@hamano
Copy link
Contributor

hamano commented Jun 11, 2024

Does the card also have the sign certificate protected?

Yes, the sign certificate is also protected by a PIN. This certificate contains the name, address, birth, and gender in subject alternative name, and it has a contactless interface.
I will read through the thread later.

@dengert
Copy link
Member

dengert commented Jun 11, 2024

@sdy623 Please try with --auth-id 02 " were 02 is the Sign PIN. (the Auth PIN is 01 on your card)

"--auth-id The auth ID of the PIN to use"
"--verify-pin Verify PIN after card binding (without 'auth-id' the first non-SO, non-Unblock PIN will be verified)"

pkcs15-tool --read-certificate 02 --verify-pin --auth-id 02
pkcs15-tool --read-public-key 02 --verify-pin --auth-id 02

And looking at the code closer, can you try reading the CA certificates
-tool --read-certificate 03 --verify-pin
pkcs15-tool --read-certificate 04 --verify-pin --auth-id 02

The debug logs shows certutil is trying to read the public key, but the code appears to define the public key is readable by ROLE_EVERYONE as the auth_id is 00.

@hamano
pkcs15-jpki.c may need something like this:

diff --git a/src/libopensc/pkcs15-jpki.c b/src/libopensc/pkcs15-jpki.c
index 8e0881c43..0d423a705 100644
--- a/src/libopensc/pkcs15-jpki.c
+++ b/src/libopensc/pkcs15-jpki.c
@@ -199,6 +199,11 @@ sc_pkcs15emu_jpki_init(sc_pkcs15_card_t * p15card)
                        "User Authentication Public Key",
                        "Digital Signature Public Key"
                };
+               static int jpki_pubkey_flags[2] = {
+                       0,
+                       SC_PKCS15_CO_FLAG_PRIVATE
+               };
+               static int jpki_pubkey_auth_id[2] = {0, 2};
                struct sc_pkcs15_pubkey_info pubkey_info;
                struct sc_pkcs15_object pubkey_obj;
                static char const *jpki_pubkey_paths[2] = {
@@ -217,6 +222,9 @@ sc_pkcs15emu_jpki_init(sc_pkcs15_card_t * p15card)
 
                sc_format_path(jpki_pubkey_paths[i], &pubkey_info.path);
                pubkey_info.path.type = SC_PATH_TYPE_FILE_ID;
+               pubkey_obj.flags = jpki_pubkey_flags[i];
+               pubkey_obj.auth_id.len = 1;
+               pubkey_obj.auth_id.value[0] = jpki_pubkey_auth_id[i];
 
                rc = sc_pkcs15emu_add_rsa_pubkey(p15card, &pubkey_obj, &pubkey_info);
                if (rc < 0) {

And I need to look at minidriver.c

dengert added a commit to dengert/OpenSC that referenced this issue Jun 11, 2024
Add SC_PKCS15_CO_FLAG_PRIVATE on "Digital Signature Public Key" and
set pubkey_obj.flags and pubkey_obj.auth_id to use the Sign KEY
so minidriver.c can request the pin before reading the public key.
Card enforces this as per specs.

Possible fix for OpenSC#3169

 On branch minidriver-PinCacheAlwaysPrompt
 Changes to be committed:
	modified:   libopensc/pkcs15-jpki.c
@sdy623
Copy link
Author

sdy623 commented Jun 11, 2024

For pkcs15-tool --read-certificate 02 --verify-pin --auth-id 02 The pem cert works.
but for pkcs15-tool --read-public-key 02 --verify-pin --auth-id 02
It went wrong

pkcs15-tool --read-public-key 02 --verify-pin --auth-id 02                 
Using reader with a card: Hitachi/Maxell M-500U/M-520U 0
Connecting to card in reader Hitachi/Maxell M-500U/M-520U 0...
Using card driver JPKI(Japanese Individual Number Cards).
Please enter PIN [Digital Signature PIN]: PIN code too short, try again.
Public key enumeration failed: Required ASN.1 object not found

For pkcs15-tool --read-certificate 03 --verify-pin It works correctly
pkcs15-tool --read-certificate 04 --verify-pin --auth-id 02 this also works.

@dengert
Copy link
Member

dengert commented Jun 11, 2024

I just finished building the minidriver code with the changes proposed in #3169 (comment)

See: #3167
and the MSI files under https://ci.appveyor.com/project/frankmorgner/opensc/builds/49997134
if you want to try it.

If you get the "Please enter PIN [Digital Signature PIN]: PIN code too short, try again."
a OpenSC-debug log would be helpful.

@sdy623
Copy link
Author

sdy623 commented Jun 12, 2024

I used the newer driver pkcs15-tool --read-public-key 02 --verify-pin --auth-id 02
but it still Please enter PIN [Digital Signature PIN]: Public key enumeration failed: Required ASN.1 object not found

@sdy623
Copy link
Author

sdy623 commented Jun 12, 2024

The certutil -scinfo still doesn't works

outgoing APDU (5 bytes):
00 B0 00 00 04 .....

P:5192; T:25796 2024-06-12 12:42:31.248 [cardmod] reader-pcsc.c:245:pcsc_internal_transmit: called
P:5192; T:25796 2024-06-12 12:42:31.260 [cardmod] reader-pcsc.c:337:pcsc_transmit: 
Incoming APDU (2 bytes):
69 82 i.

P:5192; T:25796 2024-06-12 12:42:31.260 [cardmod] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:5192; T:25796 2024-06-12 12:42:31.260 [cardmod] apdu.c:539:sc_transmit: returning with: 0 (Success)
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] card.c:523:sc_unlock: called
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] Security status not satisfied
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] iso7816.c:162:iso7816_read_binary: Check SW error: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] card.c:523:sc_unlock: called
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] card.c:663:sc_read_binary: returning with: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] card-jpki.c:197:jpki_select_file: SW Check failed: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] card.c:872:sc_select_file: 'SELECT' error: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] card.c:523:sc_unlock: called
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] pkcs15.c:2644:sc_pkcs15_read_file: returning with: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.261 [cardmod] pkcs15-pubkey.c:963:sc_pkcs15_read_pubkey: Failed to read public key file.: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] pkcs15-pubkey.c:984:sc_pkcs15_read_pubkey: returning with: -1211 (Security status not satisfied)
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] public key read error -1211
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] now read certificate 'Digital Signature Certificate'
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] pkcs15-cert.c:381:sc_pkcs15_read_certificate: called
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] pkcs15.c:2482:sc_pkcs15_read_file: called
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] path=0001, index=0, count=-1
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] card.c:471:sc_lock: called
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] card-jpki.c:358:jpki_card_reader_lock_obtained: called
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] card-jpki.c:364:jpki_card_reader_lock_obtained: returning with: 0 (Success)
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:5192; T:25796 2024-06-12 12:42:31.262 [cardmod] called; type=0, path=0001
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] card-jpki.c:149:jpki_select_file: called
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] jpki_select_file: path=0001, len=2
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] apdu.c:550:sc_transmit_apdu: called
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] card.c:471:sc_lock: called
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] card.c:513:sc_lock: returning with: 0 (Success)
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] apdu.c:515:sc_transmit: called
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] apdu.c:363:sc_single_transmit: called
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] CLA:0, INS:A4, P1:2, P2:C, data(2) 000001B00C6B8E80
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] reader 'Hitachi/Maxell M-500U/M-520U 0'
P:5192; T:25796 2024-06-12 12:42:31.263 [cardmod] reader-pcsc.c:328:pcsc_transmit: 
Outgoing APDU (7 bytes):
00 A4 02 0C 02 00 01 .......

@dengert
Copy link
Member

dengert commented Jun 12, 2024

Looks like the pkcs15 code in pkcs15-jpki.c is now working, but when minidriver.c calls pkcs15 it does not work.
I am working on fixing the minidriver for pkcs15-piv.c has the same type of problem.

@dengert
Copy link
Member

dengert commented Jun 13, 2024

#3167 get further with the JPKI cards, but these cards are the only one I know of that does not make the public key readable.

I would suggest you use the card vendor's minidriver on Windows. The problem is there is no easy way to force a PIN verify before trying to read the public key in the minidriver.

#3167 does have the code in OpenSC to get the PKCS15 routines to work. #3167 has the code to force a pin prompt before trying to use the Sign KEY for keys marked with user_consent.

Maybe @hamano can look at this closer.

dengert added a commit to dengert/OpenSC that referenced this issue Jun 15, 2024
Add SC_PKCS15_CO_FLAG_PRIVATE on "Digital Signature Public Key" and
set pubkey_obj.flags and pubkey_obj.auth_id to use the Sign KEY
so minidriver.c can request the pin before reading the public key.
Card enforces this as perspecs.

Partial fix for OpenSC#3169 Only pkcs15-jpki.c is changed.

In addition to changes in OpenSC#3167 that address "user_consent" using
"PinCacheAlwaysPrompt", The JPKI card forces the user to verify the Sign PIN
before the public key is read. But to use the Sign KEY,
Windows minidriver specs V7.07 says: the "CCP_CONTAINER_INFO"
contains "cbSigPublicKey" and "pbSigPublicKey"
which is needed before the key is selected.

It might be possible to add bogus information in these and
substitute the real values at a later time. But this will require
someone with a working card.

 On branch minidriver-PinCacheAlwaysPrompt
 Changes to be committed:
	modified:   libopensc/pkcs15-jpki.c

 On branch JPKI-Improvments
 Changes to be committed:
	modified:   libopensc/pkcs15-jpki.c
@dengert dengert linked a pull request Jun 15, 2024 that will close this issue
5 tasks
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.

4 participants