2017-01-28 70 views
1

我正在嘗試在Visual Studio c項目中使用openssl。OpenSSL 1.1.0中的EVP_get_cipherbyname和「undefined struct/union evp_cipher_st」

我使用視覺工作室NMAKE命令然後被安裝一切到預定的文件夾(C:\ OpenSSL的)編譯OpenSSL的使用:

nmake install 

的文件夾結構是如下:

  • bin

  • include/openssl

  • lib

裏面包括/ OpenSSL的有.H頭文件。

在我的Visual Studio 2012,我創建了一個空的一般C++項目,其中包括C:\ OpenSSL的\包括

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

我還添加了lib目錄和.lib文件。

但是,當我編譯代碼時,我得到

left of 'key_len' specifies undefined struct/union 'evp_cipher_st' 

在我的代碼,我有這些行

const EVP_CIPHER *cipher = EVP_get_cipherbyname("aes-256-cbc"); 
//some other code 
return cipher->key_len; 

展望ossl_typ.h文件evp_cipher_st定義,它被聲明爲

typedef struct evp_cipher_st EVP_CIPHER; 

並且結構體沒有定義!

深入研究源代碼樹evp_cipher_st在crypto \ include \ internal \ evp_int.h中定義,它不包含在openssl安裝文件夾的include文件夾中。

我也試過在包裝盒中加入crypto \ include \ internal \ evp_int.h,但它會導致更多的問題。

任何想法如何解決這個問題?

UPDATE:

下面是完整的功能,我的主要包括所有的包括:

#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的版本是:

OpenSSL_1_1_0-pre6-1266-g487d3a726

以上的版本是從Git的最新標籤名,我認爲它是迄今爲止最新鮮的一個。最新提交的版本和日期如下:

487d3a726a1970e84853434561d88cb4ac212d15

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

最後這裏是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 
+0

你應該包括['evp.h'](https://github.com/openssl/openssl/blob/master/include/openssl/evp.h)。你在做那個嗎?否則,請提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。實際上,我們需要用你正在調用的函數來看到一個完整的'main'。我們可能需要看到編譯命令,所以你也應該提供它。最後,說明你正在使用的OpenSSL的版本。 – jww

+0

我也包含了evp.h,但它沒有做任何事情。好的,我會用更多的信息更新這個問題,謝謝 – madz

+0

從編譯命令中看起來很好:'I「c:\ openssl \ include」'。程序中沒有'evp.h'。你應該包含''。 – jww

回答

1

可以使用得到密鑰長度

​​

在代碼末尾添加下一行:

printf("Key-size: %d\n", aes_keylen); 
printf("Key: "); for (int i = 0; i<aes_keylen; ++i) { printf("%02x", shared_secret[i]); } printf("\n"); 

它打印正確的輸出,這是下一個:

Key-size: 32 
Key: 51ae3ac4721439302cc5f90313f440bd9ca714c9a80b2213d034c87c00a700a0 

我不知道如果key_len是可在以前的版本,但在openssl-1.10 release notes你可以閱讀:

  • 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(和其他)顯示未定義的原因。

+0

謝謝@jgorosdev解決方案運作良好。聽起來這個結構已經變成了一個不透明的類型,用於更新的openssl版本 – madz

+0

@pendrive - 它看起來像在[OpenSSL 1.1.0]中遇到[Error:「invalid use of incomplete type'RSA {aka struct rsa_st}](http:/ /stackoverflow.com/q/40549318)和[Visual Studio和錯誤C2027:使用未定義類型'rsa_st'](http://stackoverflow.com/q/41348281) – jww