wolfssl_adds.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* wolfssl_adds.c
  2. *
  3. * Copyright (C) 2006-2021 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef _WIN32
  26. #define HAVE_CONFIG_H
  27. #endif
  28. #include <wolfssl/ssl.h>
  29. #include <wolfssl/wolfcrypt/rsa.h>
  30. #include <wolfssl/wolfcrypt/asn.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <assert.h>
  34. #include <ctype.h>
  35. #ifdef _WIN32
  36. #include <winsock2.h>
  37. #include <process.h>
  38. #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
  39. #include <ws2tcpip.h>
  40. #include <wspiapi.h>
  41. #endif
  42. #define SOCKET_T int
  43. #else
  44. #include <string.h>
  45. #include <unistd.h>
  46. #include <netdb.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <sys/ioctl.h>
  50. #include <sys/time.h>
  51. #include <sys/types.h>
  52. #include <sys/socket.h>
  53. #include <pthread.h>
  54. #ifdef NON_BLOCKING
  55. #include <fcntl.h>
  56. #endif
  57. #ifdef TEST_IPV6
  58. #include <netdb.h>
  59. #endif
  60. #define SOCKET_T unsigned int
  61. #endif /* _WIN32 */
  62. #ifdef _MSC_VER
  63. /* disable conversion warning */
  64. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  65. #pragma warning(disable:4244 4996)
  66. #endif
  67. #if defined(__MACH__) || defined(_WIN32)
  68. #ifndef _SOCKLEN_T
  69. typedef int socklen_t;
  70. #endif
  71. #endif
  72. /* HPUX doesn't use socklent_t for third parameter to accept */
  73. #if !defined(__hpux__)
  74. typedef socklen_t* ACCEPT_THIRD_T;
  75. #else
  76. typedef int* ACCEPT_THIRD_T;
  77. #endif
  78. #ifdef _WIN32
  79. #define CloseSocket(s) closesocket(s)
  80. #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
  81. #else
  82. #define CloseSocket(s) close(s)
  83. #define StartTCP()
  84. #endif
  85. #ifdef TEST_IPV6
  86. typedef struct sockaddr_in6 SOCKADDR_IN_T;
  87. #define AF_INET_V AF_INET6
  88. #else
  89. typedef struct sockaddr_in SOCKADDR_IN_T;
  90. #define AF_INET_V AF_INET
  91. #endif
  92. enum {
  93. SSL_BLOCKING = 2,
  94. SSL_NONBLOCKING = 4
  95. };
  96. static int tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr, const char* peer,
  97. short port)
  98. {
  99. const char* host = peer;
  100. /* peer could be in human readable form */
  101. if (isalpha(peer[0])) {
  102. struct hostent* entry = gethostbyname(peer);
  103. if (entry) {
  104. struct sockaddr_in tmp;
  105. memset(&tmp, 0, sizeof(struct sockaddr_in));
  106. memcpy(&tmp.sin_addr.s_addr, entry->h_addr_list[0],entry->h_length);
  107. host = inet_ntoa(tmp.sin_addr);
  108. }
  109. else
  110. return -1; /* no entry for host */
  111. }
  112. *sockfd = socket(AF_INET, SOCK_STREAM, 0);
  113. memset(addr, 0, sizeof(SOCKADDR_IN_T));
  114. addr->sin_family = AF_INET;
  115. addr->sin_port = htons(port);
  116. addr->sin_addr.s_addr = inet_addr(host);
  117. #ifdef SO_NOSIGPIPE
  118. {
  119. int on = 1;
  120. socklen_t len = sizeof(on);
  121. setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
  122. }
  123. #endif
  124. return 0;
  125. }
  126. static int tcp_connect(SOCKET_T* sockfd, const char* ip, short port)
  127. {
  128. SOCKADDR_IN_T addr;
  129. int ret = tcp_socket(sockfd, &addr, ip, port);
  130. if (ret != 0) return ret;
  131. if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  132. return -2; /* can't connect */
  133. return 0;
  134. }
  135. int wolfSSL_swig_connect(WOLFSSL* ssl, const char* server, int port)
  136. {
  137. SOCKET_T sockfd;
  138. int ret = tcp_connect(&sockfd, server, port);
  139. if (ret != 0) return ret;
  140. ret = wolfSSL_set_fd(ssl, sockfd);
  141. if (ret != SSL_SUCCESS) return ret;
  142. return wolfSSL_connect(ssl);
  143. }
  144. char* wolfSSL_error_string(int err)
  145. {
  146. static char buffer[WOLFSSL_MAX_ERROR_SZ];
  147. return wolfSSL_ERR_error_string(err, buffer);
  148. }
  149. WC_RNG* GetRng(void)
  150. {
  151. WC_RNG* rng = (WC_RNG*)malloc(sizeof(WC_RNG));
  152. if (rng)
  153. if (wc_InitRng(rng) != 0) {
  154. free(rng);
  155. rng = 0;
  156. }
  157. return rng;
  158. }
  159. RsaKey* GetRsaPrivateKey(const char* keyFile)
  160. {
  161. RsaKey* key = (RsaKey*)malloc(sizeof(RsaKey));
  162. if (key) {
  163. byte tmp[1024];
  164. size_t bytes;
  165. int ret;
  166. word32 idx = 0;
  167. XFILE file = XFOPEN(keyFile, "rb");
  168. if (file == XBADFILE)
  169. {
  170. free(key);
  171. return 0;
  172. }
  173. bytes = XFREAD(tmp, 1, sizeof(tmp), file);
  174. XFCLOSE(file);
  175. wc_InitRsaKey(key, 0);
  176. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  177. if (ret != 0) {
  178. wc_FreeRsaKey(key);
  179. free(key);
  180. return 0;
  181. }
  182. }
  183. return key;
  184. }
  185. void FillSignStr(unsigned char* dst, const char* src, int size)
  186. {
  187. memcpy(dst, src, size);
  188. }