简体   繁体   English

HTTPS/SSL 连接与 C 中的 winsock

[英]HTTPS/SSL connection with winsock in C

im trying to make any HTTPS request from my winsock program.我试图从我的 winsock 程序发出任何HTTPS请求。
I came to the conclusion that I must use some SSL connection, but its too hard to do it by scratch.我得出的结论是我必须使用一些 SSL 连接,但是从头开始做它太难了。
So, I found the <\\openssl.h> lib, got an example for linux and tried use it.所以,我找到了 <\\openssl.h> 库,得到了一个 linux 示例并尝试使用它。
Downloaded the openssl folder from https://github.com/openssl/openssl/tree/master/include and paste it in C:\\MinGW\\include , but when compile (gcc clientHTTPS.c -o clientHTTPS.exe -l Ws2_32) get some errors from includes.https://github.com/openssl/openssl/tree/master/include下载openssl文件夹并将其粘贴到C:\\MinGW\\include ,但是在编译时(gcc clientHTTPS.c -o clientHTTPS.exe -l Ws2_32)从包含中获取一些错误。

There are some win socket io example using another SSL lib?有一些使用另一个 SSL 库的win socket io 示例? About the errors, how do I could fix?关于错误,我该如何解决?

errors:错误:

gcc clientHTTP.c -o clientHTTP.exe -l Ws2_32
In file included from c:\mingw\include\openssl\opensslconf.h:13,
                 from c:\mingw\include\openssl\macros.h:10,
                 from c:\mingw\include\openssl\ssl.h:16,
                 from clientHTTP.c:3:
c:\mingw\include\openssl\configuration.h:27:1: error: expected identifier or '(' before '{' token
 {- if (@{$config{openssl_sys_defines}}) {
 ^
c:\mingw\include\openssl\configuration.h:27:8: error: stray '@' in program
 {- if (@{$config{openssl_sys_defines}}) {
        ^
c:\mingw\include\openssl\configuration.h:28:16: error: stray '@' in program
       foreach (@{$config{openssl_sys_defines}}) {
                ^
c:\mingw\include\openssl\configuration.h:34:14: error: stray '@' in program
     foreach (@{$config{openssl_api_defines}}) {
              ^
c:\mingw\include\openssl\configuration.h:38:9: error: stray '@' in program
     if (@{$config{openssl_feature_defines}}) {
         ^
c:\mingw\include\openssl\configuration.h:39:16: error: stray '@' in program
       foreach (@{$config{openssl_feature_defines}}) {
                ^
c:\mingw\include\openssl\configuration.h:49:1: error: expected identifier or '(' before '{' token
 {- $config{processor} eq "386" ? "# define" : "# undef" -} I386_ONLY
 ^
c:\mingw\include\openssl\configuration.h:55:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 {- $config{bn_ll} ? "#  define" : "#  undef" -} BN_LLONG
 ^
c:\mingw\include\openssl\configuration.h:57:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 {- $config{b64l} ? "#  define" : "#  undef" -} SIXTY_FOUR_BIT_LONG
 ^
c:\mingw\include\openssl\configuration.h:58:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 {- $config{b64}  ? "#  define" : "#  undef" -} SIXTY_FOUR_BIT
 ^
c:\mingw\include\openssl\configuration.h:59:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 {- $config{b32}  ? "#  define" : "#  undef" -} THIRTY_TWO_BIT
 ^
In file included from c:\mingw\include\openssl\macros.h:11,
                 from c:\mingw\include\openssl\opensslconf.h:14,
                 from c:\mingw\include\openssl\macros.h:10,
                 from c:\mingw\include\openssl\ssl.h:16,
                 from clientHTTP.c:3:
c:\mingw\include\openssl\macros.h:104:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL > OPENSSL_CONFIGURED_API
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:112:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL > (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:116:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL < 30000 && OPENSSL_API_LEVEL >= 20000
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:120:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL < 908
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:139:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 30000
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:149:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 10101
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:159:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 10100
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:169:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 10002
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:179:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 10001
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:189:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 10000
      ^~~~~~~~~~~~~~~~~
c:\mingw\include\openssl\macros.h:199:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL >= 908
      ^~~~~~~~~~~~~~~~~
In file included from c:\mingw\include\openssl\opensslconf.h:13,
                 from c:\mingw\include\openssl\macros.h:10,
                 from c:\mingw\include\openssl\ssl.h:16,
                 from clientHTTP.c:3:
c:\mingw\include\openssl\configuration.h:59:62: error: expected ';' before 'typedef'
 {- $config{b32}  ? "#  define" : "#  undef" -} THIRTY_TWO_BIT
                                                              ^
                                                              ;
In file included from c:\mingw\include\openssl\params.h:15,
                 from c:\mingw\include\openssl\evp.h:27,
                 from c:\mingw\include\openssl\x509.h:24,
                 from c:\mingw\include\openssl\ssl.h:26,
                 from clientHTTP.c:3:
c:\mingw\include\openssl\bn.h:191:43: error: unknown type name 'BN_ULONG'
 int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w);
                                           ^~~~~~~~
c:\mingw\include\openssl\bn.h:194:39: error: unknown type name 'BN_ULONG'
 int BN_is_word(const BIGNUM *a, const BN_ULONG w);
                                       ^~~~~~~~
In file included from c:\mingw\include\openssl\macros.h:11,
                 from c:\mingw\include\openssl\opensslconf.h:14,
                 from c:\mingw\include\openssl\macros.h:10,
                 from c:\mingw\include\openssl\ssl.h:16,
                 from clientHTTP.c:3:
c:\mingw\include\openssl\bn.h:201:6: error: token "{" is not valid in preprocessor expressions
 # if OPENSSL_API_LEVEL > 908
      ^~~~~~~~~~~~~~~~~
In file included from c:\mingw\include\openssl\params.h:15,
                 from c:\mingw\include\openssl\evp.h:27,
                 from c:\mingw\include\openssl\x509.h:24,
                 from c:\mingw\include\openssl\ssl.h:26,
                 from clientHTTP.c:3:
c:\mingw\include\openssl\bn.h:228:22: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 int BN_num_bits_word(BN_ULONG l);
                      ^~~~~~~~
                      PULONG
c:\mingw\include\openssl\bn.h:282:1: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
 ^~~~~~~~
 PULONG
c:\mingw\include\openssl\bn.h:282:39: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
                                       ^~~~~~~~
                                       PULONG
c:\mingw\include\openssl\bn.h:283:1: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
 ^~~~~~~~
 PULONG
c:\mingw\include\openssl\bn.h:283:33: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
                                 ^~~~~~~~
                                 PULONG
c:\mingw\include\openssl\bn.h:284:28: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 int BN_mul_word(BIGNUM *a, BN_ULONG w);
                            ^~~~~~~~
                            PULONG
c:\mingw\include\openssl\bn.h:285:28: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 int BN_add_word(BIGNUM *a, BN_ULONG w);
                            ^~~~~~~~
                            PULONG
c:\mingw\include\openssl\bn.h:286:28: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 int BN_sub_word(BIGNUM *a, BN_ULONG w);
                            ^~~~~~~~
                            PULONG
c:\mingw\include\openssl\bn.h:287:28: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 int BN_set_word(BIGNUM *a, BN_ULONG w);
                            ^~~~~~~~
                            PULONG
c:\mingw\include\openssl\bn.h:288:1: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 BN_ULONG BN_get_word(const BIGNUM *a);
 ^~~~~~~~
 PULONG
c:\mingw\include\openssl\bn.h:304:37: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
                                     ^~~~~~~~
                                     PULONG
c:\mingw\include\openssl\bn.h:339:24: error: unknown type name 'BN_ULONG'; did you mean 'PULONG'?
 void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
                        ^~~~~~~~
                        PULONG

In general to use any C library you need header files that are used to compile your code, and object files , usually packaged as a library, containing the library code that are used to link and run your code.一般来说,要使用任何 C 库,您需要用于编译代码的头文件目标文件,通常打包为库,其中包含用于链接和运行代码的库代码。 This should be covered no later than the second week in any course on C (or C++) programming.这应该在任何 C(或 C++)编程课程的第二周内完成。

For OpenSSL specifically, because it is portable to a large number of varied architectures, you can't use the upstream source as-is.特别是对于 OpenSSL,因为它可以移植到大量不同的体系结构,所以您不能按原样使用上游源。 You need header files tailored to your type of system and object libraries compiled for your type of system , namely Windows 32-bit or 64-bit.您需要为您的系统类型定制的头文件和为您的系统类型编译的对象库,即 Windows 32 位或 64 位。 (IINM Cygwin is 32-bit even on 64-bit Windows, but check for yourself.) To do this either: (即使在 64 位 Windows 上,IINM Cygwin 也是 32 位,但请自行检查。)要执行此操作:

  • download the entire upstream tree (you can possibly do without the test parts, but it doesn't save much) and run the build process following the instructions in the file INSTALL (or INSTALL.W32 or INSTALL.W64 as applicable for releases below 1.1.0, but those are all obsolete now, although you can still get them if you want)下载整个上游树(您可以不使用测试部分,但它不会节省太多)并按照文件 INSTALL(或 INSTALL.W32 或 INSTALL.W64 适用于 1.1 以下的版本)中的说明运行构建过程.0,但这些现在都已经过时了,尽管您仍然可以根据需要获得它们)

  • get a build already done by someone else.获得其他人已经完成的构建。 I like (and use) http://www.slproweb.com/products/Win32OpenSSL.html which provides free builds of up-to-date OpenSSL versions for Windows, packaged into a nice installer that follows Windows conventions about where to put files, registry entries, environment variables, and so on.我喜欢(并使用) http://www.slproweb.com/products/Win32OpenSSL.html ,它为 Windows 提供免费构建的最新 OpenSSL 版本,打包成一个很好的安装程序,遵循 Windows 关于文件放置位置的约定、注册表项、环境变量等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM