1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/* cyassl_adds.c */
#ifndef _WIN32
#define HAVE_CONFIG_H
#endif
#include "openssl/ssl.h"
#include "rsa.h"
#include "asn.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#ifdef _WIN32
#include <winsock2.h>
#include <process.h>
#ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
#include <ws2tcpip.h>
#include <wspiapi.h>
#endif
#define SOCKET_T int
#else
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#ifdef NON_BLOCKING
#include <fcntl.h>
#endif
#ifdef TEST_IPV6
#include <netdb.h>
#endif
#define SOCKET_T unsigned int
#endif /* _WIN32 */
#ifdef _MSC_VER
/* disable conversion warning */
/* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
#pragma warning(disable:4244 4996)
#endif
#if defined(__MACH__) || defined(_WIN32)
#ifndef _SOCKLEN_T
typedef int socklen_t;
#endif
#endif
/* HPUX doesn't use socklent_t for third parameter to accept */
#if !defined(__hpux__)
typedef socklen_t* ACCEPT_THIRD_T;
#else
typedef int* ACCEPT_THIRD_T;
#endif
#ifdef _WIN32
#define CloseSocket(s) closesocket(s)
#define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
#else
#define CloseSocket(s) close(s)
#define StartTCP()
#endif
#ifdef TEST_IPV6
typedef struct sockaddr_in6 SOCKADDR_IN_T;
#define AF_INET_V AF_INET6
#else
typedef struct sockaddr_in SOCKADDR_IN_T;
#define AF_INET_V AF_INET
#endif
enum {
SSL_BLOCKING = 2,
SSL_NONBLOCKING = 4
};
static int tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr, const char* peer,
short port)
{
const char* host = peer;
/* peer could be in human readable form */
if (isalpha(peer[0])) {
struct hostent* entry = gethostbyname(peer);
if (entry) {
struct sockaddr_in tmp;
memset(&tmp, 0, sizeof(struct sockaddr_in));
memcpy(&tmp.sin_addr.s_addr, entry->h_addr_list[0],entry->h_length);
host = inet_ntoa(tmp.sin_addr);
}
else
return -1; /* no entry for host */
}
*sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(addr, 0, sizeof(SOCKADDR_IN_T));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = inet_addr(host);
#ifdef SO_NOSIGPIPE
{
int on = 1;
socklen_t len = sizeof(on);
setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
}
#endif
return 0;
}
static int tcp_connect(SOCKET_T* sockfd, const char* ip, short port)
{
SOCKADDR_IN_T addr;
int ret = tcp_socket(sockfd, &addr, ip, port);
if (ret != 0) return ret;
if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
return -2; /* can't connect */
return 0;
}
int CyaSSL_connect(SSL* ssl, const char* server, int port)
{
SOCKET_T sockfd;
int ret = tcp_connect(&sockfd, server, port);
if (ret != 0) return ret;
SSL_set_fd(ssl, sockfd);
return SSL_connect(ssl);
}
char* CyaSSL_error_string(int err)
{
static char buffer[80];
return ERR_error_string(err, buffer);
}
RNG* GetRng(void)
{
RNG* rng = (RNG*)malloc(sizeof(RNG));
if (rng)
if (InitRng(rng) != 0) {
free(rng);
rng = 0;
}
return rng;
}
RsaKey* GetRsaPrivateKey(const char* keyFile)
{
RsaKey* key = (RsaKey*)malloc(sizeof(RsaKey));
if (key) {
byte tmp[1024];
size_t bytes;
int ret;
word32 idx = 0;
FILE* file = fopen(keyFile, "rb");
if (!file) {
free(key);
return 0;
}
bytes = fread(tmp, 1, sizeof(tmp), file);
fclose(file);
InitRsaKey(key, 0);
ret = RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
if (ret != 0) {
FreeRsaKey(key);
free(key);
return 0;
}
}
return key;
}
void FillSignStr(unsigned char* dst, const char* src, int size)
{
memcpy(dst, src, size);
}
|