avoid installing internal header files
[project/ustream-ssl.git] / ustream-polarssl.c
1 /*
2 * ustream-ssl - library for SSL over ustream
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23
24 #include "ustream-ssl.h"
25 #include "ustream-internal.h"
26
27 static int urandom_fd = -1;
28
29 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
30 {
31 struct ustream *s = ctx;
32 char *sbuf;
33 int slen;
34
35 if (s->eof)
36 return 0;
37
38 sbuf = ustream_get_read_buf(s, &slen);
39 if (slen > len)
40 slen = len;
41
42 if (!slen)
43 return POLARSSL_ERR_NET_WANT_READ;
44
45 memcpy(buf, sbuf, slen);
46 ustream_consume(s, slen);
47
48 return slen;
49 }
50
51 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
52 {
53 struct ustream *s = ctx;
54
55 len = ustream_write(s, (const char *) buf, len, false);
56 if (len < 0 || s->write_error)
57 return POLARSSL_ERR_NET_SEND_FAILED;
58
59 return len;
60 }
61
62 __hidden void ustream_set_io(void *ctx, void *ssl, struct ustream *conn)
63 {
64 ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
65 }
66
67 static bool urandom_init(void)
68 {
69 if (urandom_fd > -1)
70 return true;
71
72 urandom_fd = open("/dev/urandom", O_RDONLY);
73 if (urandom_fd < 0)
74 return false;
75
76 return true;
77 }
78
79 static int _urandom(void *ctx, unsigned char *out, size_t len)
80 {
81 read(urandom_fd, out, len);
82 return 0;
83 }
84
85 __hidden void * __ustream_ssl_context_new(bool server)
86 {
87 struct ustream_polarssl_ctx *uctx;
88
89 if (!urandom_init())
90 return NULL;
91
92 uctx = calloc(1, sizeof(*uctx));
93 if (!uctx)
94 return NULL;
95
96 uctx->server = server;
97 rsa_init(&uctx->key, RSA_PKCS_V15, 0);
98
99 return uctx;
100 }
101
102 __hidden int __ustream_ssl_set_crt_file(void *ctx, const char *file)
103 {
104 struct ustream_polarssl_ctx *uctx = ctx;
105
106 if (x509parse_crtfile(&uctx->cert, file))
107 return -1;
108
109 return 0;
110 }
111
112 __hidden int __ustream_ssl_set_key_file(void *ctx, const char *file)
113 {
114 struct ustream_polarssl_ctx *uctx = ctx;
115
116 if (x509parse_keyfile(&uctx->key, file, NULL))
117 return -1;
118
119 return 0;
120 }
121
122 __hidden void __ustream_ssl_context_free(void *ctx)
123 {
124 struct ustream_polarssl_ctx *uctx = ctx;
125
126 rsa_free(&uctx->key);
127 x509_free(&uctx->cert);
128 free(ctx);
129 }
130
131 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
132 {
133 us->error = ret;
134 uloop_timeout_set(&us->error_timer, 0);
135 }
136
137 static bool ssl_do_wait(int ret)
138 {
139 switch(ret) {
140 case POLARSSL_ERR_NET_WANT_READ:
141 case POLARSSL_ERR_NET_WANT_WRITE:
142 return true;
143 default:
144 return false;
145 }
146 }
147
148 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
149 {
150 void *ssl = us->ssl;
151 int r;
152
153 r = ssl_handshake(ssl);
154 if (r == 0)
155 return U_SSL_OK;
156
157 if (ssl_do_wait(r))
158 return U_SSL_PENDING;
159
160 ustream_ssl_error(us, r);
161 return U_SSL_ERROR;
162 }
163
164 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
165 {
166 void *ssl = us->ssl;
167 int ret = ssl_write(ssl, (const unsigned char *) buf, len);
168
169 if (ret < 0) {
170 if (ssl_do_wait(ret))
171 return 0;
172
173 ustream_ssl_error(us, ret);
174 return -1;
175 }
176
177 return ret;
178 }
179
180 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
181 {
182 int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
183
184 if (ret < 0) {
185 if (ssl_do_wait(ret))
186 return U_SSL_PENDING;
187
188 ustream_ssl_error(us, ret);
189 return U_SSL_ERROR;
190 }
191
192 return ret;
193 }
194
195 static const int default_ciphersuites[] =
196 {
197 #if defined(POLARSSL_AES_C)
198 #if defined(POLARSSL_SHA2_C)
199 TLS_RSA_WITH_AES_256_CBC_SHA256,
200 #endif /* POLARSSL_SHA2_C */
201 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
202 TLS_RSA_WITH_AES_256_GCM_SHA384,
203 #endif /* POLARSSL_SHA2_C */
204 TLS_RSA_WITH_AES_256_CBC_SHA,
205 #endif
206 #if defined(POLARSSL_CAMELLIA_C)
207 #if defined(POLARSSL_SHA2_C)
208 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
209 #endif /* POLARSSL_SHA2_C */
210 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
211 #endif
212 #if defined(POLARSSL_AES_C)
213 #if defined(POLARSSL_SHA2_C)
214 TLS_RSA_WITH_AES_128_CBC_SHA256,
215 #endif /* POLARSSL_SHA2_C */
216 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
217 TLS_RSA_WITH_AES_128_GCM_SHA256,
218 #endif /* POLARSSL_SHA2_C */
219 TLS_RSA_WITH_AES_128_CBC_SHA,
220 #endif
221 #if defined(POLARSSL_CAMELLIA_C)
222 #if defined(POLARSSL_SHA2_C)
223 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
224 #endif /* POLARSSL_SHA2_C */
225 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
226 #endif
227 #if defined(POLARSSL_DES_C)
228 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
229 #endif
230 #if defined(POLARSSL_ARC4_C)
231 TLS_RSA_WITH_RC4_128_SHA,
232 TLS_RSA_WITH_RC4_128_MD5,
233 #endif
234 0
235 };
236
237 __hidden void *__ustream_ssl_session_new(void *ctx)
238 {
239 struct ustream_polarssl_ctx *uctx = ctx;
240 ssl_context *ssl;
241 int ep, auth;
242
243 ssl = calloc(1, sizeof(ssl_context));
244 if (!ssl)
245 return NULL;
246
247 if (ssl_init(ssl)) {
248 free(ssl);
249 return NULL;
250 }
251
252 if (uctx->server) {
253 ep = SSL_IS_SERVER;
254 auth = SSL_VERIFY_NONE;
255 } else {
256 ep = SSL_IS_CLIENT;
257 auth = SSL_VERIFY_OPTIONAL;
258 }
259
260 ssl_set_ciphersuites(ssl, default_ciphersuites);
261 ssl_set_endpoint(ssl, ep);
262 ssl_set_authmode(ssl, auth);
263 ssl_set_rng(ssl, _urandom, NULL);
264
265 if (uctx->server) {
266 if (uctx->cert.next)
267 ssl_set_ca_chain(ssl, uctx->cert.next, NULL, NULL);
268 ssl_set_own_cert(ssl, &uctx->cert, &uctx->key);
269 }
270
271 ssl_session_reset(ssl);
272
273 return ssl;
274 }
275
276 __hidden void __ustream_ssl_session_free(void *ssl)
277 {
278 ssl_free(ssl);
279 free(ssl);
280 }