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