3

我们最近更新到 OpenSC 0.15.0,由于某种原因,我们不能再使用它来使用智能卡中的私钥解密消息。

显然,无论我们使用 pkcs11 工具(随 OpenSC 提供)和 OpenSSL 引擎,都会发生同样的情况。

下面是我们所做的一个例子:

pkcs11-工具:

% pkcs11-tool --module /usr/local/lib/opensc-pkcs11.so --decrypt -v -l --input-file encrypted.bin --id 9352
Using slot 1 with a present token (0x1)
Logging in to "OpenSC Card".
Please enter User PIN: 
Using decrypt algorithm RSA-PKCS
error: PKCS11 function C_Decrypt failed: rv = CKR_DATA_LEN_RANGE (0x21)

Aborting.

或者通过使用 OpenSSL 引擎,这里有一个小示例程序:

#include <iostream>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/crypto.h>

using namespace std;

int main()
{
    OpenSSL_add_all_algorithms();
    ENGINE_load_dynamic();

    // Setup OpenSSL engine
    ENGINE* engine = ENGINE_by_id("dynamic");
    string enginePath = "/usr/local/lib/engines/pkcs11.so";
    string modulePath = "/usr/local/lib/opensc-pkcs11.so";

    ENGINE_ctrl_cmd_string(engine, "SO_PATH", enginePath.c_str(), 0);
    ENGINE_ctrl_cmd_string(engine, "LIST_ADD", "1", 0);
    ENGINE_ctrl_cmd_string(engine, "LOAD", NULL, 0);
    ENGINE_ctrl_cmd_string(engine, "MODULE_PATH", modulePath.c_str(), 0);

    string pin = "123456";
    ENGINE_ctrl_cmd_string(engine, "PIN", pin.c_str(), 0);
    ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0);
    ENGINE_init(engine);
    ENGINE_set_default(engine, ENGINE_METHOD_ALL);

    string keyName = "id_9352";
    EVP_PKEY *evp = ENGINE_load_private_key(engine, keyName.c_str(), NULL, NULL);

    // Read encrypted file
    long unsigned int length = 128;
    unsigned char buf[length];
    FILE* f = fopen("encrypted.bin", "r");
    fread(buf, 1, length, f);
    fclose(f);

    // Try to decrypt
    unsigned char output[length];
    unsigned char* p = output;
    RSA *rsa = EVP_PKEY_get1_RSA(evp);
    int outputLen = RSA_private_decrypt(length, buf, p, rsa, RSA_PKCS1_PADDING);
    if (outputLen == -1) {
        long err = ERR_get_error();
        cout << "Error decrypting: " << ERR_error_string(err, NULL) << endl;
        return 1;
    }

    cout << output << endl;
    return 0;
}

但是当我运行它时:

% ./sc-decrypt
PKCS#11: Initializing the engine
Found 2 slots
Loading private key "slot_1-id_9352"
Looking in slot 1 for key: 9352
[18446744073709551615] Virtual hotplug slot       no tok          
[1] Gemalto PC Twin Reader 00  login             (OpenSC Card)
Found slot:  Gemalto PC Twin Reader 00 00
Found token: OpenSC Card
Found 0 certificate:
Found 1 private key:
   1 P  Private Key
Error decrypting: error:80008021:Vendor defined:PKCS11_rsa_decrypt:Data len range

显然,在这两种情况下都是相同的错误,关于“数据长度范围”

更奇怪的是,当使用 pkcs11-tool 进行签名操作(也使用私钥)时工作正常:

% echo "test signing" > input.txt
% pkcs11-tool --module /usr/local/lib/opensc-pkcs11.so --sign -v --input-file input.txt --output-file out.sign --id 9352 -l
Using slot 1 with a present token (0x1)
Logging in to "OpenSC Card".
Please enter User PIN: 
Using signature algorithm RSA-PKCS
% ls -l out.sign 
-rw-------  1 user  user  128 May 18 10:08 out.sign

所以,如果有人能指出我做错了什么,我会非常感激

4

1 回答 1

1

我知道这来得很晚(问题发生后 3 年),但它可能会对某人有所帮助。

我遇到了同样的问题。但是当我列出 OpenSC 支持的所有机制时,我可以看到 RSA_PKCS 的最小密钥大小是512. 实际上,这是模数大小。对应的密钥大小为4096。因此,只需确保您生成的密钥对至少有4096位长。就我而言,它起作用了(使用pkcs11-tool命令。我还没有实现C代码)。

于 2019-04-26T10:44:23.463 回答