3 * Copyright (C) 2006-2015 wolfSSL Inc.
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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.
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.
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-1301, USA
26 #include <wolfssl/wolfcrypt/settings.h>
32 #include <wolfssl/ssl.h>
33 #include <wolfssl/wolfcrypt/rsa.h>
34 #include <wolfssl/wolfcrypt/asn.h>
44 #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <sys/ioctl.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
66 #define SOCKET_T unsigned int
70 /* disable conversion warning */
71 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
72 #pragma warning(disable:4244 4996)
75 #if defined(__MACH__) || defined(_WIN32)
77 typedef int socklen_t;
82 /* HPUX doesn't use socklent_t for third parameter to accept */
83 #if !defined(__hpux__)
84 typedef socklen_t* ACCEPT_THIRD_T;
86 typedef int* ACCEPT_THIRD_T;
91 #define CloseSocket(s) closesocket(s)
92 #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
94 #define CloseSocket(s) close(s)
100 typedef struct sockaddr_in6 SOCKADDR_IN_T;
101 #define AF_INET_V AF_INET6
103 typedef struct sockaddr_in SOCKADDR_IN_T;
104 #define AF_INET_V AF_INET
114 static int tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr, const char* peer,
117 const char* host = peer;
119 /* peer could be in human readable form */
120 if (isalpha(peer[0])) {
121 struct hostent* entry = gethostbyname(peer);
124 struct sockaddr_in tmp;
125 memset(&tmp, 0, sizeof(struct sockaddr_in));
126 memcpy(&tmp.sin_addr.s_addr, entry->h_addr_list[0],entry->h_length);
127 host = inet_ntoa(tmp.sin_addr);
130 return -1; /* no entry for host */
133 *sockfd = socket(AF_INET, SOCK_STREAM, 0);
134 memset(addr, 0, sizeof(SOCKADDR_IN_T));
136 addr->sin_family = AF_INET;
137 addr->sin_port = htons(port);
138 addr->sin_addr.s_addr = inet_addr(host);
143 socklen_t len = sizeof(on);
144 setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
152 static int tcp_connect(SOCKET_T* sockfd, const char* ip, short port)
155 int ret = tcp_socket(sockfd, &addr, ip, port);
156 if (ret != 0) return ret;
158 if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
159 return -2; /* can't connect */
165 int wolfSSL_swig_connect(WOLFSSL* ssl, const char* server, int port)
168 int ret = tcp_connect(&sockfd, server, port);
169 if (ret != 0) return ret;
171 wolfSSL_set_fd(ssl, sockfd);
173 return wolfSSL_connect(ssl);
177 char* wolfSSL_error_string(int err)
179 static char buffer[WOLFSSL_MAX_ERROR_SZ];
181 return wolfSSL_ERR_error_string(err, buffer);
187 RNG* rng = (RNG*)malloc(sizeof(RNG));
190 if (wc_InitRng(rng) != 0) {
199 RsaKey* GetRsaPrivateKey(const char* keyFile)
201 RsaKey* key = (RsaKey*)malloc(sizeof(RsaKey));
208 FILE* file = fopen(keyFile, "rb");
215 bytes = fread(tmp, 1, sizeof(tmp), file);
217 wc_InitRsaKey(key, 0);
219 ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
230 void FillSignStr(unsigned char* dst, const char* src, int size)
232 memcpy(dst, src, size);