【问题标题】:HTTPS/SSL connection with winsock in CHTTPS/SSL 与 C 中的 Winsock 连接
【发布时间】:2023-03-05 17:48:01
【问题描述】:

我正在尝试从我的 winsock 程序发出任何 HTTPS 请求。
我得出的结论是我必须使用一些 SSL 连接,但是从头开始很难做到。
所以,我找到了 库,找到了一个 linux 示例并尝试使用它。
https://github.com/openssl/openssl/tree/master/include 下载了 openssl 文件夹并将其粘贴到 C:\MinGW\include,但是编译 (gcc clientHTTPS.c -o clientHTTPS.exe -l Ws2_32) 时会从包含中获取一些错误。

有一些使用另一个 SSL 库的 winsocket io 示例吗? 关于错误,我该如何解决?

错误:

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

【问题讨论】:

  • 你不仅需要头文件。
  • 我还需要什么?
  • 如果你想使用openssl,你需要openssl。不仅仅是 openssl 的头文件。

标签: c sockets https openssl winsock


【解决方案1】:

一般要使用任何 C 库,您需要用于编译代码的头文件目标文件,通常打包为一个库,包含用于链接和运行代码的库代码。这应该在任何 C(或 C++)编程课程的第二周内完成。

特别是对于 OpenSSL,因为它可以移植到大量不同的架构中,所以您不能按原样使用上游源。您需要为您的系统类型量身定制的头文件为您的系统类型编译的对象库,即Windows 32 位或64 位。 (即使在 64 位 Windows 上,IINM Cygwin 也是 32 位的,但请自行检查。)要执行此操作:

  • 下载整个上游树(您可以不使用测试部分,但不会节省太多)并按照文件 INSTALL(或 INSTALL.W32 或 INSTALL.W64)中的说明运行构建过程适用于 1.1.0 以下的版本,但这些现在都已过时,尽管您仍然可以根据需要获取它们)

  • 获取其他人已经完成的构建。我喜欢(并使用)http://www.slproweb.com/products/Win32OpenSSL.html,它为 Windows 提供了最新的 OpenSSL 版本的免费构建,打包到一个不错的安装程序中,该安装程序遵循 Windows 关于放置文件、注册表项、环境变量等的约定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2017-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多