【问题标题】:Botan Loading Existing RSA Private KeyBotan 加载现有的 RSA 私钥
【发布时间】:2018-09-22 12:56:44
【问题描述】:

我正在使用 Botan C++ 库来签署和验证一些 license.ini 文件。我已经设置 Botan PK_Signer 使用 RSA 算法来加密使用 PKCS v1.5 创建的散列。这是我的代码:

uint8_t private_key[] = "private key I already have generated."

// Read file content that needs to be signed.
std::string licensePath = argv[1];
std::string fileContents = readFileContent(licensePath);

// Prepare Botan RSA signer. PKCS1 v1.5 is used.
Botan::AutoSeeded_RNG rng;
Botan::secure_vector<uint8_t> secure_key_vector(private_key, private_key + sizeof(private_key) / sizeof(private_key[0]));

/////// NEXT LINE THROWS EXCEPTION!
Botan::RSA_PrivateKey rsa_priv_key(Botan::AlgorithmIdentifier(), secure_key_vector);
//////////////////////////////////////

Botan::PK_Signer signer(rsa_priv_key, rng, "EMSA-PKCS1-v1_5");

// Create signature.
signer.update(cleanStr(fileContents));
std::vector<uint8_t> signature = signer.signature(rng);
std::string hexSignature = Botan::hex_encode(signature);

生成RSA_PrivateKey对象的标记线抛出异常:

在 0x00007FF85D2BC3C7 (vcruntime140.dll) 处引发异常 license_signer.exe: 0xC0000005: 访问冲突读取位置 0x0000021A8FA07000.

我以前从未使用过 Botan 库。如果有人知道为什么会发生这种情况或知道如何实施,请提供帮助。谢谢。

【问题讨论】:

  • 确保库和您的应用程序是使用相同的 MSVC 运行时选项构建的。如果它们不匹配,则症状通常是崩溃。 Botan 构建默认为/MD

标签: c++ botan


【解决方案1】:

使用Botan 2.12.1

#include <string>
#include <botan/auto_rng.h>
#include <botan/auto_rng.h>
#include <botan/base64.h>
#include <botan/pkcs8.h>
#include <botan/rsa.h>
#include <botan/x509_key.h>
#include <botan/data_src.h>
#include <botan/pubkey.h>

//Assuming the private and public keys are in Base64 encoded BER.

Botan::AutoSeeded_RNG rng;
Botan::RSA_PrivateKey keyPair(rng, static_cast<size_t>(4096));
std::string privateKey = Botan::base64_encode(Botan::PKCS8::BER_encode(keyPair));
std::string publicKey = Botan::base64_encode(Botan::X509::BER_encode(keyPair));

//Loading the public key

Botan::SecureVector<uint8_t> keyBytes(Botan::base64_decode(publicKey));
std::unique_ptr<Botan::Public_Key> pbk(Botan::X509::load_key(std::vector(keyBytes.begin(), keyBytes.end())));

//Loading the private key

Botan::SecureVector<uint8_t> keyBytes(Botan::base64_decode(privateKey));
Botan::DataSource_Memory source(keyBytes);
std::unique_ptr<Botan::Private_Key> pvk(Botan::PKCS8::load_key(source));

【讨论】:

    【解决方案2】:

    private_key 进入您尝试使用的构造函数的数据必须经过 DER 编码。确保它是。但是,一般来说,您最好使用适当的loadKey 方法。

    【讨论】:

      最近更新 更多