RSA_private_decrypt第二次调用时崩溃

时间:2019-03-20 12:35:35

标签: c++ objective-c openssl rsa objective-c++

请查看以下方法:

int BCVirtualCard::decrypt(std::string from, std::string *to, int keyId, bool padding)
{
    if (to == nullptr)
    {
        NSCAssert(NO, @"Invalid params");
        return 0;
    }

    NSString* privateKey            = [m_storage privateKeyForSlot:keyId];
    NSArray<NSString*>* components  = [privateKey componentsSeparatedByString:@"_"];
    const NSInteger componentsCount = 4;

    if (components.count != componentsCount)
    {
        *to = "";
        return 0;
    }

    const char* d = [components[0] UTF8String];
    const char* n = [components[1] UTF8String];
    const char* p = [components[2] UTF8String];
    const char* q = [components[3] UTF8String];

    RSA* rsa = RSA_new();

    BN_hex2bn(&rsa->d, d);
    BN_hex2bn(&rsa->n, n);
    BN_hex2bn(&rsa->p, p);
    BN_hex2bn(&rsa->q, q);

    unsigned char* _to = (unsigned char *)calloc(1, sizeof(unsigned char));

    int decryptedSize = RSA_private_decrypt((int)from.length(), (unsigned char *)from.c_str(), _to, rsa, RSA_NO_PADDING);

    free(_to);

    if (decryptedSize <= 0)
    {
        ERR_print_errors_cb(test, NULL);

        *to = "";
        return 0;
    }

    _to = (unsigned char *)calloc(decryptedSize, sizeof(unsigned char));

    RSA_private_decrypt((int)from.length(), (unsigned char *)from.c_str(), _to, rsa, RSA_NO_PADDING);

    *to = std::string((char *)_to, strlen((char *)_to));

    free(_to);

    RSA_free(rsa);

    return 1;
}

这里应该将字符串from解密并写入字符串to。对于解密,我使用RSA_private_decrypt函数。我叫了两次。第一次是为了确定解密文本的大小,第二次是为了将解密文本写入_to缓冲区。当我第二次调用它时,它通常会像这样崩溃:

malloc: Heap corruption detected, free list is damaged at 0x280ff3d70
*** Incorrect guard value: 0
No1BCmail(2171,0x170efb000) malloc: *** set a breakpoint in malloc_error_break to debug

断点已打开,这使我可以找到崩溃的位置。但是我不明白它崩溃的原因。我尝试重新创建RSA结构并第二次使用为_to分配的大小,但是没有任何帮助。你能看到这里有什么问题吗?谢谢

1 个答案:

答案 0 :(得分:1)

RSA_private_decrypt要求to参数指向足够大小的缓冲区。您的第一个调用仅使用大小为1的缓冲区,该缓冲区太小且为未定义行为。您需要做的是使用rsaRSA_size(rsa)获取大小,然后可以使用它为_to分配空间。这意味着您不需要两次调用该函数,因为您第一次已经拥有该大小

如果要构建的字符串(而不是将decryptedSize用作strlen的结尾可能不是空终止的,则还应该使用_to作为长度。

将所有内容放在一起应该看起来像

int BCVirtualCard::decrypt(std::string from, std::string *to, int keyId, bool padding)
{
    if (to == nullptr)
    {
        NSCAssert(NO, @"Invalid params");
        return 0;
    }

    NSString* privateKey            = [m_storage privateKeyForSlot:keyId];
    NSArray<NSString*>* components  = [privateKey componentsSeparatedByString:@"_"];
    const NSInteger componentsCount = 4;

    if (components.count != componentsCount)
    {
        *to = "";
        return 0;
    }

    const char* d = [components[0] UTF8String];
    const char* n = [components[1] UTF8String];
    const char* p = [components[2] UTF8String];
    const char* q = [components[3] UTF8String];

    RSA* rsa = RSA_new();

    BN_hex2bn(&rsa->d, d);
    BN_hex2bn(&rsa->n, n);
    BN_hex2bn(&rsa->p, p);
    BN_hex2bn(&rsa->q, q);

    auto _to = std::make_unique<unsigned char[]>(RSA_size(rsa)); // use smart pointers so you don't have to worry about releasing the memory

    int decryptedSize = RSA_private_decrypt((int)from.length(), (unsigned char *)from.c_str(), _to.get(), rsa, RSA_NO_PADDING);

    if (decryptedSize <= 0)
    {
        ERR_print_errors_cb(test, NULL);    
        *to = "";
        return 0;
    }

    *to = std::string((char *)_to.get(), decryptedSize);
    return 1;
}