首页 > 解决方案 > C 中的 OpenSSL 1.0.0 RSA 参数

问题描述

我意识到我无法使用OpenSSL 1.0.0RSA_get0_key中的函数通过从文件中读取私钥并将其作为参数传递给上述函数来提取 的值。n, e, d

这不是编程问题,我的意思是,我知道如何使用这些功能,但我不知道是否有替代方法。

实际上,在编译操作期间阻止我的警告如下:

warning: implicit declaration of function ‘RSA_get0_key’; did you mean ‘RSA_check_key’? [-Wimplicit-function-declaration]

你知道怎么做吗?我在这里查看手册(https://www.openssl.org/docs/man1.0.2/man3/),但似乎没有适当的功能可以做到这一点。此外,我需要符合 OpenSSL 1.0.0。

代码

#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/obj_mac.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/pem.h>

int main()
{
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    RSA *privkey = RSA_new();
    FILE *privkeyfile = fopen("private.key", "rb");

    PEM_read_RSAPrivateKey(privkeyfile, &privkey, NULL, NULL);
    fclose(privkeyfile);

    BIGNUM *n, *e, *d = NULL;
    RSA_get0_key(privkey,&n,&e,&d);
    
    return 0;
}

标签: copensslrsa

解决方案


RSA_get0_key函数作为抽象添加到 OpenSSL 1.1.0 中,用于检索 RSA 密钥的ned值。对于早期版本,您需要直接访问这些字段。

n = privkey->n;
e = privkey->e;
d = privkey->d;

如果您希望您的代码能够处理 1.0.x 和 1.1.x,您可以检查以下值OPENSSL_VERSION_NUMBER

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
    RSA_get0_key(privkey, &n, &e, &d);
#else
    n = privkey->n;
    e = privkey->e;
    d = privkey->d;
#endif

推荐阅读