clion 产生sigabrt_在EVP_SignFinal生成RSA签名时如何避免SIGABRT(How to avoid SIGABRT when generating RSA Signature...

I'm trying to generate a RSA Signature with libopenssl for c++: But when I run my code, I get a SIGABRT. I did some deep debugging into libopenssl internal stuff to see where the Segfault comes from. I'll come to this later on.

First I want to make clear, that the RSA PrivateKey was successfully loaded from a .pem file. So Im pretty sure that's not the problem's origin.

So my question is: How to avoid the SIGABRT and what is the cause of it ?

I'm doing this for my B.Sc. Thesis so I really appreciate your help :)

Signature Generation Function:

DocumentSignature* RSASignatureGenerator::generateSignature(ContentHash* ch, CryptographicKey* pK) throw(PDVSException) {

OpenSSL_add_all_algorithms();

OpenSSL_add_all_ciphers();

OpenSSL_add_all_digests();

if(pK == nullptr)

throw MissingPrivateKeyException();

if(pK->getKeyType() != CryptographicKey::KeyType::RSA_PRIVATE || !dynamic_cast(pK))

throw KeyTypeMissmatchException(pK->getPem()->getPath().string(), "Generate RSA Signature");

//get msg to encrypt

const char* msg = ch->getStringHash().c_str();

//get openssl rsa key

RSA* rsaPK = dynamic_cast(pK)->createOpenSSLRSAKeyObject();

//create openssl signing context

EVP_MD_CTX* rsaSignCtx = EVP_MD_CTX_create();

EVP_PKEY* priKey = EVP_PKEY_new();

EVP_PKEY_assign_RSA(priKey, rsaPK);

//init ctxt

if (EVP_SignInit(rsaSignCtx, EVP_sha256()) <=0)

throw RSASignatureGenerationException();

//add data to sign

if (EVP_SignUpdate(rsaSignCtx, msg, std::strlen(msg)) <= 0) {

throw RSASignatureGenerationException();

}

//create result byte signature struct

DocumentSignature::ByteSignature* byteSig = new DocumentSignature::ByteSignature();

//set size to max possible

byteSig->size = EVP_MAX_MD_SIZE;

//alloc buffer memory

byteSig->data = (unsigned char*)malloc(byteSig->size);

//do signing

if (EVP_SignFinal(rsaSignCtx, byteSig->data, (unsigned int*) &byteSig->size, priKey) <= 0)

throw RSASignatureGenerationException();

DocumentSignature* res = new DocumentSignature(ch);

res->setByteSignature(byteSig);

EVP_MD_CTX_destroy(rsaSignCtx);

//TODO open SSL Memory leaks -> where to free open ssl stuff?!

return res;

}

RSA* rsaPK = dynamic_cast(pK)->createOpenSSLRSAKeyObject();

virtual RSA* createOpenSSLRSAKeyObject() throw (PDVSException) override {

RSA* rsa = NULL;

const char* c_string = _pem->getContent().c_str();

BIO * keybio = BIO_new_mem_buf((void*)c_string, -1);

if (keybio==NULL)

throw OpenSSLRSAPrivateKeyObjectCreationException(_pem->getPath());

rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);

if(rsa == nullptr)

throw OpenSSLRSAPrivateKeyObjectCreationException(_pem->getPath());

//BIO_free(keybio);

return rsa;

}

SigAbrt origin in file openssl/crypto/mem.c

void CRYPTO_free(void *str, const char *file, int line)

{

if (free_impl != NULL && free_impl != &CRYPTO_free) {

free_impl(str, file, line);

return;

}

#ifndef OPENSSL_NO_CRYPTO_MDEBUG

if (call_malloc_debug) {

CRYPTO_mem_debug_free(str, 0, file, line);

free(str);

CRYPTO_mem_debug_free(str, 1, file, line);

} else {

free(str);

}

#else

free(str); // <<<<<<< HERE

#endif

}

the stacktrace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值