openSSL 中的 BN_hex2bn 神奇地出现段错误

发布于 2024-08-27 00:31:35 字数 1657 浏览 7 评论 0原文

大家好,这是我在 stackoverflow 上的第一篇文章,如果有点长,我很抱歉。

我正在尝试为自己的项目构建握手协议,但在服务器将客户端 RSA 公钥转换为 Bignum 时遇到问题。它在我的客户端代码中工作,但是当尝试将客户端公共 RSA 的十六进制值转换为 bignum 时,服务器会出现段错误。

我已经检查过RSA数据之前或之后没有垃圾,并且在网上查看过,但我被卡住了。

标头段:

typedef struct KEYS {  
    RSA *serv;  
    char* serv_pub;  
    int pub_size;  
    RSA *clnt;  
} KEYS;

KEYS keys;

初始化函数:

// Generates and validates the servers key

/* code for generating server RSA left out, it's working */
//Set client exponent
keys.clnt = 0;
keys.clnt = RSA_new();
BN_dec2bn(&keys.clnt->e, RSA_E_S); // RSA_E_S contains the public exponent

问题代码(在 Network::server_handshake 中):

// *Recieved an encrypted message from the network and decrypt into 'buffer' (1024 byte long)*
cout << "Assigning clients RSA" << endl;
// I have verified that 'buffer' contains the proper key
if (BN_hex2bn(&keys.clnt->n, buffer) < 0) { 
    Error("ERROR reading server RSA");
}
cout << "clients RSA has been assigned" << endl;

程序出现

BN_hex2bn(&keys.clnt->n, buffer)

错误(valgrind 输出)

大小 8 的读取无效 在 0x50DBF9F:BN_hex2bn(在 /usr/lib/libcrypto.so.0.9.8 中) 通过 0x40F23E:网络::server_handshake() (Network.cpp:177) 通过 0x40EF42:网络::startNet() (Network.cpp:126) 通过 0x403C38:主要(server.cpp:51) 地址 0x20 未堆栈、分配或(最近)释放

进程以信号 11 (SIGSEGV) 的默认操作终止 访问不在地址 0x20 处的映射区域内 在 0x50DBF9F:BN_hex2bn(在 /usr/lib/libcrypto.so.0.9.8 中)

我不知道为什么会这样,我在客户端程序中使用完全相同的代码,并且它工作得很好。任何输入都会受到极大的欢迎!

Greetings, this is my first post on stackoverflow, and i'm sorry if its a bit long.

I'm trying to build a handshake protocol for my own project and am having issues with the server converting the clients RSA's public key to a Bignum. It works in my clent code, but the server segfaults when attempting to convert the hex value of the clients public RSA to a bignum.

I have already checked that there is no garbidge before or after the RSA data, and have looked online, but i'm stuck.

header segment:

typedef struct KEYS {  
    RSA *serv;  
    char* serv_pub;  
    int pub_size;  
    RSA *clnt;  
} KEYS;

KEYS keys;

Initializing function:

// Generates and validates the servers key

/* code for generating server RSA left out, it's working */
//Set client exponent
keys.clnt = 0;
keys.clnt = RSA_new();
BN_dec2bn(&keys.clnt->e, RSA_E_S); // RSA_E_S contains the public exponent

Problem code (in Network::server_handshake):

// *Recieved an encrypted message from the network and decrypt into 'buffer' (1024 byte long)*
cout << "Assigning clients RSA" << endl;
// I have verified that 'buffer' contains the proper key
if (BN_hex2bn(&keys.clnt->n, buffer) < 0) { 
    Error("ERROR reading server RSA");
}
cout << "clients RSA has been assigned" << endl;

The program segfaults at

BN_hex2bn(&keys.clnt->n, buffer)

with the error (valgrind output)

Invalid read of size 8
at 0x50DBF9F: BN_hex2bn (in /usr/lib/libcrypto.so.0.9.8)
by 0x40F23E: Network::server_handshake() (Network.cpp:177)
by 0x40EF42: Network::startNet() (Network.cpp:126)
by 0x403C38: main (server.cpp:51)
Address 0x20 is not stack'd, malloc'd or (recently) free'd

Process terminating with default action of signal 11 (SIGSEGV)
Access not within mapped region at address 0x20
at 0x50DBF9F: BN_hex2bn (in /usr/lib/libcrypto.so.0.9.8)

And I don't know why it is, Im using the exact same code in the client program, and it works just fine. Any input is greatly appriciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-09-03 00:31:35

RSA_new() 仅创建 RSA 结构,它不会在该结构内创建任何 bignum 对象,例如 ne 字段。您必须使用 BN_new() 自己创建这些,或者更有可能您需要找到正确的 openssl 函数来生成或读取 RSA 密钥。

RSA_new() only creates the RSA struct, it does not create any of the bignum objects inside that struct, like the n and e fields. You must create these yourself using BN_new(), or more likely you need to find the right openssl function to generate or read in your RSA key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文