【问题标题】:OpenSSL 1.0.0 RSA parameters in CC 中的 OpenSSL 1.0.0 RSA 参数
【发布时间】:2021-10-17 14:14:33
【问题描述】:

我意识到我不能使用 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;
}

【问题讨论】:

  • 向我们展示您的代码。
  • @RobertHarvey 问题已更新。
  • 嗯,rsa.h 的第 217 行说此功能已弃用。
  • FWIW,在 OpenSSL

标签: c openssl rsa


【解决方案1】:

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

【讨论】:

  • 可能有更好的方法。阅读Migration Guide;您可以在页面上搜索RSA_get0_key
  • @RobertHarvey 那是为了迁移到 OpenSSL 3.0.0。 OP 正在使用 1.0.x。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
相关资源
最近更新 更多