gpt4 book ai didi

c - OpenSSL RSA_sign() 返回零错误代码

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:56 25 4
gpt4 key购买 nike

我正在玩 OpenSSL 1.0.2o 版本。我只从 OpenSSL 编译静态 libcrypto。我使用了这个配置标志:

no-demos, no-bugs, no-apps, no-ssl, no-test, no-shared, no-zlib, no-zlib-dynamic, no-ssl-trace, no-unit-test, no-ec_nistp_64_gcc_128, no-libunbound, no-ssl1, no-ssl2, no-ssl3, no-asm, no-dtls, no-dtls1, no-threads, no-npn, no-weak-ssl-ciphers, no-rfc3779, no-sctp, no-ui, no-async, no-dgram, no-posix-io, no-sock, no-des, no-dso, no-srp, no-store, no-ts, no-txt_db, no-hw, no-ec, no-gmp, -DOPENSSL_NO_STDIO, -DOPENSSL_NO_FP_API, -DOPENSSL_NO_DYNAMIC_ENGINE,-UOPENSSL_FIPS.

我将 OpenSSL 用于小型嵌入式设备。 (无文件操作,无操作系统,无libc)。

我从 PEM 字符串的内存中导入 RSA 公钥和私钥,然后我想将其用于签名/验证,但 RSA_sign() 函数返回零。可能是我做错了吗?

导入 key :

#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/bn.h>

typedef RSA *(*read_bio2rsa_f)(BIO *, RSA **, pem_password_cb *, void *);

static BIO *pub_bio;
static RSA *pub_key;

static BIO *prv_bio;
static RSA *prv_key;

static RSA *openssl_read_key_rsa(int rsa_type, BIO **bio)
{
RSA *rsa;
char *pem_str;
int pem_str_len;
read_bio2rsa_f read_bio2rsa;

if (rsa_type == PUB_KEY_TYPE) {
pem_str = (char *)pem_pub_key;
pem_str_len = (int)sizeof(pem_pub_key);
read_bio2rsa = PEM_read_bio_RSA_PUBKEY;
} else {
pem_str = (char *)pem_prv_key;
pem_str_len = (int)sizeof(pem_prv_key);
read_bio2rsa = PEM_read_bio_RSAPrivateKey;
}

if ((*bio = BIO_new_mem_buf((const void *)pem_str,
pem_str_len)) == NULL) {
EMSG("BIO_new_mem_buf() FAILED read PEM key");

return NULL;
}

if ((rsa = RSA_new()) == NULL) {
EMSG("RSA_new() FAILED");

return NULL;
}

read_bio2rsa(*bio, &rsa, NULL, NULL);

return rsa;
}

static int check_rsa_key_pair(RSA *pub, RSA *priv)
{
if (BN_cmp(pub->n, priv->n) != 0)
return CRYPTO_ERR;

return CRYPTO_OK;
}

/* extrnal function for import RSA-keys */
int openssl_rsa_init_key(void)
{
ERR_load_crypto_strings();
OPENSSL_add_all_algorithms_noconf();

if ((prv_key = openssl_read_key_rsa(PRV_KEY_TYPE, &prv_bio)) == NULL) {
EMSG("Importing the private key FAILED!");

return CRYPTO_ERR;
}

if ((pub_key = openssl_read_key_rsa(PUB_KEY_TYPE, &pub_bio)) == NULL) {
EMSG("Importing the public key FAILED!");

return CRYPTO_ERR;
}

if (!check_rsa_key_pair(pub_key, prv_key)) {
EMSG("Key pair don't match");

return CRYPTO_ERR;
}

EMSG("Import KEYs is successful!");

return CRYPTO_OK;
}

以上代码全部执行成功。在此之后,理论上,我可以自由地将 rsa key 用于任何需要 RSA 类型的 OpenSSL 函数。

我试过这样签名:

int openssl_rsa_sign_hash(uint8_t *hash, unsigned int hash_len,
uint8_t *sig, int *sig_len)
{
if (!RSA_sign(NID_sha256, (const unsigned char *)hash, hash_len,
(unsigned char *)sig, (unsigned int *)sig_len,
prv_key)) {
EMSG("RSA signature FAILED with %s",
ERR_error_string(ERR_get_error(), NULL));

return CRYPTO_ERR;
}

EMSG("RSA signature success!");

return CRYPTO_OK;
}

但是,我在错误输出中得到了“RSA signature FAILED with error:00000000:lib(0):func(0):reason(0)”这个字符串。

谁能帮我解释一下错误,好吗?

最佳答案

But, I got "RSA signature FAILED with error:00000000:lib(0):func(0):reason(0)" this string into my error output.

Could anyone explain me the mistakes, please?

我没有看到 main 函数,所以这只是推测...

main 中添加对 SSL_library_initERR_load_crypto_strings 的调用。您可能还需要调用 OpenSSL_add_all_ciphers

由于您没有得到正确的错误字符串,您可以尝试打印 ERR_get_error() 的结果。获得 ERR_get_error() 的结果后,您可以通过 openssl errstr 命令运行它:

$ openssl errstr 0406506C
error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len

另见 Library Initialization在 OpenSSL wiki 上。

关于c - OpenSSL RSA_sign() 返回零错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58302636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com