3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL 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 * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30 #include <cyassl/ssl.h>
31 #include <cyassl/ctaocrypt/rsa.h>
32 #include <cyassl/ctaocrypt/asn.h>
42 #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <sys/ioctl.h>
55 #include <sys/types.h>
56 #include <sys/socket.h>
64 #define SOCKET_T unsigned int
68 /* disable conversion warning */
69 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
70 #pragma warning(disable:4244 4996)
73 #if defined(__MACH__) || defined(_WIN32)
75 typedef int socklen_t;
80 /* HPUX doesn't use socklent_t for third parameter to accept */
81 #if !defined(__hpux__)
82 typedef socklen_t* ACCEPT_THIRD_T;
84 typedef int* ACCEPT_THIRD_T;
89 #define CloseSocket(s) closesocket(s)
90 #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
92 #define CloseSocket(s) close(s)
98 typedef struct sockaddr_in6 SOCKADDR_IN_T;
99 #define AF_INET_V AF_INET6
101 typedef struct sockaddr_in SOCKADDR_IN_T;
102 #define AF_INET_V AF_INET
112 static int tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr, const char* peer,
115 const char* host = peer;
117 /* peer could be in human readable form */
118 if (isalpha(peer[0])) {
119 struct hostent* entry = gethostbyname(peer);
122 struct sockaddr_in tmp;
123 memset(&tmp, 0, sizeof(struct sockaddr_in));
124 memcpy(&tmp.sin_addr.s_addr, entry->h_addr_list[0],entry->h_length);
125 host = inet_ntoa(tmp.sin_addr);
128 return -1; /* no entry for host */
131 *sockfd = socket(AF_INET, SOCK_STREAM, 0);
132 memset(addr, 0, sizeof(SOCKADDR_IN_T));
134 addr->sin_family = AF_INET;
135 addr->sin_port = htons(port);
136 addr->sin_addr.s_addr = inet_addr(host);
141 socklen_t len = sizeof(on);
142 setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
150 static int tcp_connect(SOCKET_T* sockfd, const char* ip, short port)
153 int ret = tcp_socket(sockfd, &addr, ip, port);
154 if (ret != 0) return ret;
156 if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
157 return -2; /* can't connect */
163 int CyaSSL_swig_connect(CYASSL* ssl, const char* server, int port)
166 int ret = tcp_connect(&sockfd, server, port);
167 if (ret != 0) return ret;
169 CyaSSL_set_fd(ssl, sockfd);
171 return CyaSSL_connect(ssl);
175 char* CyaSSL_error_string(int err)
177 static char buffer[80];
179 return CyaSSL_ERR_error_string(err, buffer);
185 RNG* rng = (RNG*)malloc(sizeof(RNG));
188 if (InitRng(rng) != 0) {
197 RsaKey* GetRsaPrivateKey(const char* keyFile)
199 RsaKey* key = (RsaKey*)malloc(sizeof(RsaKey));
206 FILE* file = fopen(keyFile, "rb");
213 bytes = fread(tmp, 1, sizeof(tmp), file);
217 ret = RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
228 void FillSignStr(unsigned char* dst, const char* src, int size)
230 memcpy(dst, src, size);