此数字签名代码能否与合格数字证书一起使用?
Will this digital signing code work with a Qualified Digital Certificate?
我最近被问及我们现有的签名代码是否可以使用 European Qualified Digital Certificate 代替我们通常使用的 RSA 签名证书。据我所知应该是这样,但我还没有找到任何能真正证实这一理论的东西。
在弄清楚如何获得合格证书并实际测试它之前,我不确定如何明确地回答这个问题。有人碰巧有这方面的经验吗?
有问题的代码如下所示。这是一个基于 Windows 的 C++ 应用程序,使用 Microsoft 的加密 API 进行签名。
int SignData(
const std::string &data, // Data to be signed
const char *containerName, // Name of key container to use
std::string &signature) // Returns the signature
{
HCRYPTPROV hProv = NULL;
HCRYPTHASH hHash = NULL;
HCRYPTKEY hKey = NULL;
DWORD dwLength;
int status = 0;
// Attempt to open the key container as a LocalMachine key.
if (CryptAcquireContext(
&hProv,
containerName,
NULL,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET | CRYPT_SILENT))
{
// Create a SHA-1 hash context.
if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
// Calculate hash of the buffer.
if (CryptHashData(hHash, (const BYTE *)data.data(), (DWORD)data.size(), 0))
{
// Determine the size of the signature and allocate memory.
if (CryptSignHash(hHash, AT_SIGNATURE, 0, 0, NULL, &dwLength))
{
signature.resize(dwLength);
// Sign the hash object.
if (!CryptSignHash(hHash, AT_SIGNATURE, 0, 0, (BYTE*)&signature[0], &dwLength))
status = HandleCryptError("CryptSignHash failed");
}
else
status = HandleCryptError("CryptSignHash failed");
}
else
status = HandleCryptError("CryptHashData failed");
}
else
status = HandleCryptError("CryptCreateHash failed");
}
else
status = HandleCryptError("CryptAcquireContext failed");
return status;
}
以上代码使用了不符合 eIDAS 要求的默认 Microsoft 软件加密提供程序。
如果您在上面的代码中将默认的 Microsoft 提供程序替换为符合 eIDAS 标准的提供程序,那么这种执行签名的方法是可以的。在幕后,合规的 CSP 将处理密钥使用的不可否认性要求,例如通过显示 PIN 弹出窗口,或者如果密钥远程存储在服务器中,则通过向密钥所有者发送 2FA 通知。
我最近被问及我们现有的签名代码是否可以使用 European Qualified Digital Certificate 代替我们通常使用的 RSA 签名证书。据我所知应该是这样,但我还没有找到任何能真正证实这一理论的东西。
在弄清楚如何获得合格证书并实际测试它之前,我不确定如何明确地回答这个问题。有人碰巧有这方面的经验吗?
有问题的代码如下所示。这是一个基于 Windows 的 C++ 应用程序,使用 Microsoft 的加密 API 进行签名。
int SignData(
const std::string &data, // Data to be signed
const char *containerName, // Name of key container to use
std::string &signature) // Returns the signature
{
HCRYPTPROV hProv = NULL;
HCRYPTHASH hHash = NULL;
HCRYPTKEY hKey = NULL;
DWORD dwLength;
int status = 0;
// Attempt to open the key container as a LocalMachine key.
if (CryptAcquireContext(
&hProv,
containerName,
NULL,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET | CRYPT_SILENT))
{
// Create a SHA-1 hash context.
if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
// Calculate hash of the buffer.
if (CryptHashData(hHash, (const BYTE *)data.data(), (DWORD)data.size(), 0))
{
// Determine the size of the signature and allocate memory.
if (CryptSignHash(hHash, AT_SIGNATURE, 0, 0, NULL, &dwLength))
{
signature.resize(dwLength);
// Sign the hash object.
if (!CryptSignHash(hHash, AT_SIGNATURE, 0, 0, (BYTE*)&signature[0], &dwLength))
status = HandleCryptError("CryptSignHash failed");
}
else
status = HandleCryptError("CryptSignHash failed");
}
else
status = HandleCryptError("CryptHashData failed");
}
else
status = HandleCryptError("CryptCreateHash failed");
}
else
status = HandleCryptError("CryptAcquireContext failed");
return status;
}
以上代码使用了不符合 eIDAS 要求的默认 Microsoft 软件加密提供程序。
如果您在上面的代码中将默认的 Microsoft 提供程序替换为符合 eIDAS 标准的提供程序,那么这种执行签名的方法是可以的。在幕后,合规的 CSP 将处理密钥使用的不可否认性要求,例如通过显示 PIN 弹出窗口,或者如果密钥远程存储在服务器中,则通过向密钥所有者发送 2FA 通知。