简体   繁体   中英

HTTPS/SSL connection with winsock in C

im trying to make any HTTPS request from my winsock program.
I came to the conclusion that I must use some SSL connection, but its too hard to do it by scratch.
So, I found the <\\openssl.h> lib, got an example for linux and tried use it.
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.

There are some win socket io example using another SSL lib? 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. This should be covered no later than the second week in any course on C (or C++) programming.

For OpenSSL specifically, because it is portable to a large number of varied architectures, you can't use the upstream source as-is. 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. (IINM Cygwin is 32-bit even on 64-bit Windows, but check for yourself.) To do this either:

  • 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)

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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