EVP_get_cipherbyname和“未定义的struct/union evp_cipher_st”在OpenSSL 1.1.0中

时间:2021-06-21 18:27:17

I'm trying to use openssl with visual studio c project.

我正在尝试在visual studio c项目中使用openssl。

I compiled openssl using visual studio nmake command then installed everything to a predefined folder (C:\openssl) using:

我使用visual studio nmake命令编译了openssl,然后将所有内容安装到预定义的文件夹(C:\openssl),使用:

nmake install

The folder structure is as following:

该文件夹结构如下:

  • bin

  • include/openssl

    包括/ openssl

  • lib

    *

Inside include/openssl there are .h header files.

在include/openssl内部有.h头文件。

In my Visual studio 2012, I created an empty general c++ project and included C:\openssl\include

在我的Visual studio 2012中,我创建了一个空的通用c++项目,包括c:\openssl\include

[Project properties -> C/C++ -> General -> Additional Include Directories]

I also added lib directory and .lib files.

我还添加了lib目录和.lib文件。

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;

Looking into ossl_typ.h file for evp_cipher_st definition, it is declared as

调查ossl_typ。对于evp_cipher_st定义的h文件,它被声明为

typedef struct evp_cipher_st EVP_CIPHER;

and there is no definition for the struct body!

结构体没有定义!

Digging more into the source tree, evp_cipher_st is defined in crypto\include\internal\evp_int.h that is not included in the include folder of openssl install folder.

将更多的信息挖掘到源代码树中,evp_cipher_st被定义在crypto\包括内部\evp_int中。h,不包含在openssl安装文件夹的include文件夹中。

I also tried to include crypto\include\internal\evp_int.h out of the box but it leads to more problems.

我还尝试包括crypto\include内部\evp_int。从盒子里取出h,但它会导致更多的问题。

Any idea how to fix that?

你知道怎么解决吗?

UPDATE:

更新:

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\n");
            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\n");
        return 0;
    }

    //other codes
}

Openssl version is:

Openssl版本是:

OpenSSL_1_1_0-pre6-1266-g487d3a726

openssl_1_1_0 pre6 - 1266 g487d3a726

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:

以上版本是git最新的标签名,我认为是目前为止最新鲜的一个。最新提交版本和日期如下:

487d3a726a1970e84853434561d88cb4ac212d15

487年d3a726a1970e84853434561d88cb4ac212d15

Author: EasySec Date: Tue Jan 17 17:21:55 2017 +0100

作者:EasySec日期:Tue Jan 17 17:21:21 2017 +0100

And finally here is the Visual Studio 2012 compile output:

最后是Visual Studio 2012编译输出:

Build started 1/28/2017 8:23:02 PM.
 1>Project "C:\CODE\mycode.vcxproj" on node 2 (Build target(s)).
 1>ClCompile:
     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\CL.exe /c /I"c:\openssl\include" /ZI /nologo /W3 /WX- /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc110.pdb" /Gd /TC /analyze- /errorReport:prompt src\main.c ... 

     wingetopt.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     crypto.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
 1>c:\CODE\src\crypto.c(517): error C2037: left of 'key_len' specifies undefined struct/union 'evp_cipher_st'
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     client.c
 1>c:\openssl\include\openssl\lhash.h(198): warning C4090: 'function' : different 'const' qualifiers
     Generating Code...
 1>Done Building Project "C:\CODE\mycode.vcxproj" (Build target(s)) -- FAILED.

Build FAILED.

Time Elapsed 00:00:08.55

1 个解决方案

#1


1  

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\n", aes_keylen);
printf("Key: "); for (int i = 0; i<aes_keylen; ++i) { printf("%02x", shared_secret[i]); } printf("\n");

It prints the correct output, which is next:

它打印出正确的输出,如下所示:

Key-size: 32
Key: 51ae3ac4721439302cc5f90313f440bd9ca714c9a80b2213d034c87c00a700a0

I'm not sure if key_len was available on previous versions but in the openssl-1.10 release notes you can read:

我不确定key_len是否在以前的版本中可用,但是在openssl-1.10发布说明中可以看到:

  • 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
  • 大多数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_md、evp_mac950_ct_50_ct50_type、ev50_ct50_ct_ctx50_type、ev_ct50_ctx50_ctx、ev_ct_type、ev9、ev_ct50_ct50_type、ev9、ev_ct50_ct_ct50_ct_type
  • libssl internal structures made opaque
  • libssl内部结构变得不透明

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.

这意味着不再允许应用程序查看结构的变量。这就是_key_len(和其他)显示未定义的原因。

#1


1  

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\n", aes_keylen);
printf("Key: "); for (int i = 0; i<aes_keylen; ++i) { printf("%02x", shared_secret[i]); } printf("\n");

It prints the correct output, which is next:

它打印出正确的输出,如下所示:

Key-size: 32
Key: 51ae3ac4721439302cc5f90313f440bd9ca714c9a80b2213d034c87c00a700a0

I'm not sure if key_len was available on previous versions but in the openssl-1.10 release notes you can read:

我不确定key_len是否在以前的版本中可用,但是在openssl-1.10发布说明中可以看到:

  • 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
  • 大多数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_md、evp_mac950_ct_50_ct50_type、ev50_ct50_ct_ctx50_type、ev_ct50_ctx50_ctx、ev_ct_type、ev9、ev_ct50_ct50_type、ev9、ev_ct50_ct_ct50_ct_type
  • libssl internal structures made opaque
  • libssl内部结构变得不透明

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.

这意味着不再允许应用程序查看结构的变量。这就是_key_len(和其他)显示未定义的原因。