VS2017 OpenSSL x64 Compile & Test Source Code

Prepare

Windows 10 Pro 1709
Visual Studio 2017
openssl-1.1.0h.tar.gz
ActivePerl-5.26.1.2601-MSWin32-x64-404865.exe

Install dmake

  1. ppm install dmake

Compile OpenSSL x64 Release DLL

  1. C:\>cd openssl-1.1.0h
  2. C:\openssl-1.1.0h>perl Configure VC-WIN64A no-asm --prefix=C:/openssl --openssldir=C:/openssl/ssl
  3. C:\openssl-1.1.0h>nmake
  4. C:\openssl-1.1.0h>nmake test
  5. C:\openssl-1.1.0h>nmake install
  6. C:\openssl-1.1.0h>nmake clean

Have A Look

C:\openssl>
.
├─bin
│ libcrypto-1_1-x64.dll
│ libssl-1_1-x64.dll

├─include
│ └─openssl
│ *.h

├─lib
│ │ libcrypto.lib
│ │ libssl.lib

VS2017 Project Properties

Test Source Code

  1. #include<tchar.h>
  2. #include<WinSock2.h>
  3. #include<WS2tcpip.h>
  4. #include<iostream>
  5.  
  6. #include <openssl\ssl.h>
  7.  
  8. const int RECV_SIZE = 8192;
  9.  
  10. //Delete the UnionPay Merchant Information
  11.  
  12. int main()
  13. {
  14. 	WSADATA wsadData;
  15. 	WSAStartup(MAKEWORD(2, 2), &wsadData);
  16.  
  17. 	ADDRINFOT aiHints;
  18. 	ZeroMemory(&aiHints, sizeof(ADDRINFOT));
  19. 	aiHints.ai_family = AF_INET;
  20. 	aiHints.ai_flags = AI_PASSIVE;
  21. 	aiHints.ai_protocol = 0;
  22. 	aiHints.ai_socktype = SOCK_STREAM;
  23.  
  24. 	PADDRINFOT paiResult;
  25. 	GetAddrInfo(wstrHost.c_str(), NULL, &aiHints, &paiResult);
  26.  
  27. 	SOCKET sSocket = socket(AF_INET, SOCK_STREAM, 0);
  28. 	if (sSocket == SOCKET_ERROR)
  29. 	{
  30. 		std::wcout << "Error socket" << std::endl;
  31. 		return -1;
  32. 	}
  33.  
  34. 	struct sockaddr_in sinHost;
  35. 	sinHost.sin_addr.s_addr = inet_addr(ip);
  36. 	sinHost.sin_family = AF_INET;
  37. 	sinHost.sin_port = htons(port);
  38. 	if (connect(sSocket, (LPSOCKADDR)&sinHost, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
  39. 	{
  40. 		std::wcout << "Error connect" << std::endl;
  41. 		return -1;
  42. 	}
  43.  
  44. 	SSL_library_init();
  45. 	SSLeay_add_ssl_algorithms();
  46. 	SSL_load_error_strings();
  47.  
  48. 	SSL_CTX *pctxSSL = SSL_CTX_new(TLSv1_2_client_method());
  49. 	if (pctxSSL == NULL)
  50. 	{
  51. 		std::wcout << "Error SSL_CTX_new" << std::endl;
  52. 		return -1;
  53. 	}
  54.  
  55.  
  56. 	if (SSL_CTX_use_certificate_file(pctxSSL, "./o(* ̄︶ ̄*)o.pem", SSL_FILETYPE_PEM) <= 0)
  57. 	{
  58. 		exit(1);
  59. 	}
  60.  
  61. 	SSL *psslSSL = SSL_new(pctxSSL);
  62. 	if (psslSSL == NULL)
  63. 	{
  64. 		std::wcout << "Error SSL_new" << std::endl;
  65. 		return -1;
  66. 	}
  67. 	SSL_set_fd(psslSSL, sSocket);
  68. 	INT iErrorConnect = SSL_connect(psslSSL);
  69. 	if (iErrorConnect < 0)
  70. 	{
  71. 		std::wcout << "Error SSL_connect, iErrorConnect=" << iErrorConnect << std::endl;
  72. 		return -1;
  73. 	}
  74. 	std::wcout << "SSL connection using " << SSL_get_cipher(psslSSL) << std::endl;
  75.  
  76. 	//Delete the HTTP Header
  77.  
  78. 	INT iErrorWrite = SSL_write(psslSSL, req, req_len) < 0;
  79. 	if (iErrorWrite < 0)
  80. 	{
  81. 		std::wcout << "Error SSL_write" << std::endl;
  82. 		return -1;
  83. 	}
  84.  
  85. 	LPSTR lpszRead = new CHAR[RECV_SIZE];
  86. 	INT iLength = 1;
  87. 	//while (iLength >= 1)
  88. 	{
  89. 		iLength = SSL_read(psslSSL, lpszRead, RECV_SIZE - 1);
  90. 		if (iLength < 0)
  91. 		{
  92. 			std::wcout << "Error SSL_read" << std::endl;
  93. 			delete[] lpszRead;
  94. 			return -1;
  95. 		}
  96. 		lpszRead[iLength] = TEXT('\0');
  97. 		std::wcout << lpszRead;
  98.  
  99. 		{
  100. 			char *token = strtok(lpszRead, "\n");
  101. 			char *p_Length = NULL;
  102. 			while (token != NULL) {
  103. 				printf("%s\n", token);
  104. 				if (p_Length = strstr(token, "Content-Length: ")) {
  105. 					recv_len = atoi(&p_Length[strlen("Content-Length: ")]);
  106. 					memcpy(recv_data, &lpszRead[iLength - recv_len], recv_len);
  107. 					break;
  108. 				}
  109. 				token = strtok(NULL, "\n");
  110. 			}
  111. 		}
  112.  
  113. 		{
  114. 			int i;
  115. 			printf("<--- (%d): \n", recv_len);
  116. 			for (i = 0; i < recv_len; i++) {
  117. 				printf("%02X ", recv_data[i]);
  118. 			}
  119. 			printf("\n");
  120. 		}
  121.  
  122. 	}
  123. 	delete[] lpszRead;
  124.  
  125. 	return 0;
  126. }

Test Code