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