头文件 dh.h (openssl 1.1.0f) 中缺少定义
Posted
技术标签:
【中文标题】头文件 dh.h (openssl 1.1.0f) 中缺少定义【英文标题】:Missing definitions in Headerfile dh.h (openssl 1.1.0f) 【发布时间】:2017-07-31 13:06:52 【问题描述】:由于某种原因,我找不到“struct dh_st”的定义+成员。它应该在 openssl/dh.h 中,但事实并非如此。但是在早期版本的 openssl(openssl-1.0/openssl/dh.h)中,有一个定义(不过我需要使用 1.1.0f)。
相关部分的代码片段:
DH *dh_obj;
// [...]
BIGNUM *temp_p = dh_obj->p; // p is not accessible/visible here!
// [...]
在 gcc 7.1.1 中编译期间的错误消息:
gcc -o dh dh.c -L/usr/lib -lssl -lcrypto && ./dh
dh.c:在函数“main”中:dh.c:57:26:错误:取消引用指向的指针 不完整类型‘DH aka struct dh_st’ BIGNUM *temp_p = dh_obj->p;
这就是结构的样子(在 openssl-1.0 中!!不在我当前的版本中,因为没有这样的定义)
struct dh_st
/*
* This first argument is used to pick up errors when a DH is passed
* instead of a EVP_PKEY
*/
int pad;
int version;
BIGNUM *p;
BIGNUM *g;
long length; /* optional */
BIGNUM *pub_key; /* g^x % p */
BIGNUM *priv_key; /* x */
int flags;
BN_MONT_CTX *method_mont_p;
/* Place holders if we want to do X9.42 DH */
BIGNUM *q;
BIGNUM *j;
unsigned char *seed;
int seedlen;
BIGNUM *counter;
int references;
CRYPTO_EX_DATA ex_data;
const DH_METHOD *meth;
ENGINE *engine;
;
任何帮助表示赞赏!
【问题讨论】:
也许DH
应该是opaque structure?喜欢标准的FILE
结构吗?
我从未听说过不透明的结构,感谢您的链接。实际上,DH 结构(以及大多数其他公共结构在 openssl 1.1.0 中已变得不透明)openssl.org/news/openssl-1.1.0-notes.html。我现在该怎么办?
我应该将struct dh_st
及其成员复制并粘贴到我的源文件中吗?或者常见的做法是什么?
IMO 的“惯例”是重构代码而不需要使用内部数据。最初可能需要做很多工作,但会让代码变得更好,更面向未来。
另请参阅 OpenSSL wiki 上的 OpenSSL 1.1.0 Changes | Backwards Compatibility。
【参考方案1】:
任何值 p、q、g、priv_key 和 pub_key 也可以通过相应的函数 DH_get0_p()、DH_get0_q()、DH_get0_g()、DH_get0_priv_key() 和 DH_get0_pub_key() 从 DH* 结构中单独检索, 分别
原型:
const BIGNUM *DH_get0_p(const DH *dh);
const BIGNUM *DH_get0_q(const DH *dh);
const BIGNUM *DH_get0_g(const DH *dh);
const BIGNUM *DH_get0_priv_key(const DH *dh);
const BIGNUM *DH_get0_pub_key(const DH *dh);
【讨论】:
【参考方案2】:所以因为我知道不透明结构(感谢@Some programmer dude),我发现openssl 提供了某种getter 和setter 函数。 我做了一个例子来打印出一个 BIGNUM,它是 openssl 1.1.0f 中不透明结构 DH aka dh_st 的成员:
// dh_obj has been previously initialized with setter function that openssl provides
const BIGNUM *member_p;
const BIGNUM *member_g;
DH_get0_pqg(dh_obj, &member_p, NULL, &member_g); // getter function to get p, q, g, q is NULL in this case
// print BIIIIIG NUMBERS
printf("len:%u\n%s\n",strlen(BN_bn2dec(member_p)),BN_bn2dec(member_p));
printf("len:%u\n%s\n",strlen(BN_bn2dec(member_g)),BN_bn2dec(member_g));
// [...]
【讨论】:
以上是关于头文件 dh.h (openssl 1.1.0f) 中缺少定义的主要内容,如果未能解决你的问题,请参考以下文章