EVP_get_cipherbyname 和“未定义的结构/联合 evp_cipher_st"在 OpenSSL 1.1.0 中 [英] EVP_get_cipherbyname and "undefined struct/union evp_cipher_st" in OpenSSL 1.1.0
问题描述
我正在尝试将 openssl 与 Visual Studio C 项目一起使用.
I'm trying to use openssl with visual studio c project.
我使用 Visual Studio nmake 命令编译了 openssl,然后使用以下命令将所有内容安装到预定义文件夹 (C:openssl):
I compiled openssl using visual studio nmake command then installed everything to a predefined folder (C:openssl) using:
nmake install
文件夹结构如下:
bin
包含/openssl
lib
在include/openssl里面有.h头文件.
Inside include/openssl there are .h header files.
在我的 Visual Studio 2012 中,我创建了一个空的通用 c++ 项目并包含 C:opensslinclude
In my Visual studio 2012, I created an empty general c++ project and included C:opensslinclude
[Project properties -> C/C++ -> General -> Additional Include Directories]
我还添加了 lib 目录和 .lib 文件.
I also added lib directory and .lib files.
但是当我编译代码时,我得到了
But when I compile the code, I get
left of 'key_len' specifies undefined struct/union 'evp_cipher_st'
在我的代码中我有这些行
Inside my code I have these lines
const EVP_CIPHER *cipher = EVP_get_cipherbyname("aes-256-cbc");
//some other code
return cipher->key_len;
查看 ossl_typ.h 文件中的 evp_cipher_st 定义,它被声明为
Looking into ossl_typ.h file for evp_cipher_st definition, it is declared as
typedef struct evp_cipher_st EVP_CIPHER;
结构体没有定义!
深入挖掘源代码树,evp_cipher_st 定义在 cryptoincludeinternalevp_int.h 中,它不包含在 openssl 安装文件夹的 include 文件夹中.
Digging more into the source tree, evp_cipher_st is defined in cryptoincludeinternalevp_int.h that is not included in the include folder of openssl install folder.
我还尝试将 cryptoincludeinternalevp_int.h 包含在内,但它会导致更多问题.
I also tried to include cryptoincludeinternalevp_int.h out of the box but it leads to more problems.
知道如何解决这个问题吗?
Any idea how to fix that?
更新:
这是完整的功能和我的主要功能,包括所有包括:
Here is the complete function and my main including all the includes:
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <mswsock.h>
#include <windows.h>
#include <minwindef.h>
#include <malloc.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef int socklen_t;
#include "wingetopt.h"
#include <Wincrypt.h>
#include <iphlpapi.h>
#include "Shlwapi.h"
#include <Bcrypt.h>
#define inline __inline
#define STATUS_SUCCESS 0
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Shlwapi.lib")
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
int generate_aes_key(const unsigned char *input_key, uint16_t input_key_size,
unsigned char *output_key, uint16_t *outputkey_size) {
const EVP_MD *dgst = NULL;
unsigned char iv[EVP_MAX_IV_LENGTH];
const EVP_CIPHER *cipher = EVP_get_cipherbyname("aes-256-cbc");
if(!cipher) {
return -1;
}
dgst = EVP_get_digestbyname("sha256");
if(!dgst) {
fprintf(stderr, "no such digest
");
return -2;
}
if(!EVP_BytesToKey(cipher, dgst, NULL, input_key, input_key_size, 1, output_key, iv)) {
return -3;
}
*outputkey_size = (uint16_t)cipher->key_len;
return 0;
}
int main(int argc, char *argv[]) {
char secret[] = "MIIFBTCCAu2gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwTjELMAkGA1UEBhMCR0Ix";
unsigned char shared_secret[500];
uint16_t aes_keylen = 0;
//bunch of codes
if(generate_aes_key((unsigned char *)secret, strlen(secret), shared_secret, &aes_keylen) < 0) {
fprintf(stderr, "Could not get initial shared secret
");
return 0;
}
//other codes
}
Openssl 版本是:
Openssl version is:
OpenSSL_1_1_0-pre6-1266-g487d3a726
OpenSSL_1_1_0-pre6-1266-g487d3a726
以上版本是来自 git 的最新标签名称,我认为它是迄今为止最新鲜的标签名称.最新的提交版本和日期如下:
Above version is the latest tag name from git, I think its the most fresh one till now. The latest commit version and date is as following:
487d3a726a1970e84853434561d88cb4ac212d15
487d3a726a1970e84853434561d88cb4ac212d15
作者:EasySec 日期:2017 年 1 月 17 日星期二 17:21:55 +0100
Author: EasySec Date: Tue Jan 17 17:21:55 2017 +0100
最后是 Visual Studio 2012 的编译输出:
And finally here is the Visual Studio 2012 compile output:
Build started 1/28/2017 8:23:02 PM.
1>Project "C:CODEmycode.vcxproj" on node 2 (Build target(s)).
1>ClCompile:
C:Program Files (x86)Microsoft Visual Studio 11.0VCinCL.exe /c /I"c:opensslinclude" /ZI /nologo /W3 /WX- /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\" /Fd"Debugvc110.pdb" /Gd /TC /analyze- /errorReport:prompt srcmain.c ...
wingetopt.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
crypto.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
1>c:CODEsrccrypto.c(517): error C2037: left of 'key_len' specifies undefined struct/union 'evp_cipher_st'
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
client.c
1>c:opensslincludeopenssllhash.h(198): warning C4090: 'function' : different 'const' qualifiers
Generating Code...
1>Done Building Project "C:CODEmycode.vcxproj" (Build target(s)) -- FAILED.
Build FAILED.
Time Elapsed 00:00:08.55
推荐答案
您可以使用以下方法获取密钥长度:
You can get the key length using:
*outputkey_size = EVP_CIPHER_key_length(cipher);
将下一行放在代码的末尾:
Putting in the end of your code the next lines:
printf("Key-size: %d
", aes_keylen);
printf("Key: "); for (int i = 0; i<aes_keylen; ++i) { printf("%02x", shared_secret[i]); } printf("
");
它打印正确的输出,接下来是:
It prints the correct output, which is next:
Key-size: 32
Key: 51ae3ac4721439302cc5f90313f440bd9ca714c9a80b2213d034c87c00a700a0
我不确定 key_len 是否在以前的版本中可用,但在 openssl-1.10 发行说明 你可以阅读:
I'm not sure if key_len was available on previous versions but in the openssl-1.10 release notes you can read:
- 大多数 libcrypto 和 libssl 公共结构是不透明的,包括:BIGNUM 和相关类型、EC_KEY 和 EC_KEY_METHOD、DH和 DH_METHOD、DSA 和 DSA_METHOD、RSA 和 RSA_METHOD、BIO 和BIO_METHOD、EVP_MD_CTX、EVP_MD、EVP_CIPHER_CTX、EVP_CIPHER、EVP_PKEY和相关的类型,HMAC_CTX,X509,X509_CRL,X509_OBJECT,X509_STORE_CTX、X509_STORE、X509_LOOKUP、X509_LOOKUP_METHOD
- libssl 内部结构变得不透明
- Most libcrypto and libssl public structures were made opaque, including: BIGNUM and associated types, EC_KEY and EC_KEY_METHOD, DH and DH_METHOD, DSA and DSA_METHOD, RSA and RSA_METHOD, BIO and BIO_METHOD, EVP_MD_CTX, EVP_MD, EVP_CIPHER_CTX, EVP_CIPHER, EVP_PKEY and associated types, HMAC_CTX, X509, X509_CRL, X509_OBJECT, X509_STORE_CTX, X509_STORE, X509_LOOKUP, X509_LOOKUP_METHOD
- libssl internal structures made opaque
这意味着不再允许应用程序查看结构的变量内部.这就是 _key_len(和其他)显示未定义的原因.
which means that the applications are no longer allowed to look inside the variables of the structures. This is the reason that _key_len (and others) shows undefined.
这篇关于EVP_get_cipherbyname 和“未定义的结构/联合 evp_cipher_st"在 OpenSSL 1.1.0 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!