C 中的 OpenSSL 1.0.0 RSA 参数

OpenSSL 1.0.0 RSA parameters in C

我意识到我无法使用 OpenSSL 1.0.0 中的函数 RSA_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;
}

OpenSSL 1.1.0 中添加了 RSA_get0_key 函数作为抽象来检索 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