8516d7f08638b8b25956379e4d6154406b1ef3c7
[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 int ret;
55
56 ret = ustream_write(s, (const char *) buf, len, false);
57 if (ret < 0 || s->write_error)
58 return POLARSSL_ERR_NET_SEND_FAILED;
59
60 return ret;
61 }
62
63 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
64 {
65 ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
66 }
67
68 static bool urandom_init(void)
69 {
70 if (urandom_fd > -1)
71 return true;
72
73 urandom_fd = open("/dev/urandom", O_RDONLY);
74 if (urandom_fd < 0)
75 return false;
76
77 return true;
78 }
79
80 static int _urandom(void *ctx, unsigned char *out, size_t len)
81 {
82 read(urandom_fd, out, len);
83 return 0;
84 }
85
86 __hidden struct ustream_ssl_ctx *
87 __ustream_ssl_context_new(bool server)
88 {
89 struct ustream_ssl_ctx *ctx;
90
91 if (!urandom_init())
92 return NULL;
93
94 ctx = calloc(1, sizeof(*ctx));
95 if (!ctx)
96 return NULL;
97
98 ctx->auth = SSL_VERIFY_NONE;
99 ctx->server = server;
100 #ifdef USE_VERSION_1_3
101 pk_init(&ctx->key);
102 #else
103 rsa_init(&ctx->key, RSA_PKCS_V15, 0);
104 #endif
105
106 return ctx;
107 }
108
109 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
110 {
111 int ret;
112
113 #ifdef USE_VERSION_1_3
114 ret = x509_crt_parse_file(&ctx->cert, file);
115 #else
116 ret = x509parse_crtfile(&ctx->cert, file);
117 #endif
118 if (ret)
119 return -1;
120
121 if (!ctx->server)
122 ctx->auth = SSL_VERIFY_OPTIONAL;
123
124 return 0;
125 }
126
127 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
128 {
129 int ret;
130
131 #ifdef USE_VERSION_1_3
132 ret = pk_parse_keyfile(&ctx->key, file, NULL);
133 #else
134 ret = x509parse_keyfile(&ctx->key, file, NULL);
135 #endif
136 if (ret)
137 return -1;
138
139 return 0;
140 }
141
142 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
143 {
144 #ifdef USE_VERSION_1_3
145 pk_free(&ctx->key);
146 x509_crt_free(&ctx->cert);
147 #else
148 rsa_free(&ctx->key);
149 x509_free(&ctx->cert);
150 #endif
151 free(ctx);
152 }
153
154 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
155 {
156 us->error = ret;
157 uloop_timeout_set(&us->error_timer, 0);
158 }
159
160 static bool ssl_do_wait(int ret)
161 {
162 switch(ret) {
163 case POLARSSL_ERR_NET_WANT_READ:
164 case POLARSSL_ERR_NET_WANT_WRITE:
165 return true;
166 default:
167 return false;
168 }
169 }
170
171 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
172 {
173 void *ssl = us->ssl;
174 int r;
175
176 r = ssl_handshake(ssl);
177 if (r == 0)
178 return U_SSL_OK;
179
180 if (ssl_do_wait(r))
181 return U_SSL_PENDING;
182
183 ustream_ssl_error(us, r);
184 return U_SSL_ERROR;
185 }
186
187 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
188 {
189 void *ssl = us->ssl;
190 int ret = ssl_write(ssl, (const unsigned char *) buf, len);
191
192 if (ret < 0) {
193 if (ssl_do_wait(ret))
194 return 0;
195
196 ustream_ssl_error(us, ret);
197 return -1;
198 }
199
200 return ret;
201 }
202
203 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
204 {
205 int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
206
207 if (ret < 0) {
208 if (ssl_do_wait(ret))
209 return U_SSL_PENDING;
210
211 ustream_ssl_error(us, ret);
212 return U_SSL_ERROR;
213 }
214
215 return ret;
216 }
217
218 static const int default_ciphersuites[] =
219 {
220 #if defined(POLARSSL_AES_C)
221 #if defined(POLARSSL_SHA2_C)
222 TLS_RSA_WITH_AES_256_CBC_SHA256,
223 #endif /* POLARSSL_SHA2_C */
224 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
225 TLS_RSA_WITH_AES_256_GCM_SHA384,
226 #endif /* POLARSSL_SHA2_C */
227 TLS_RSA_WITH_AES_256_CBC_SHA,
228 #endif
229 #if defined(POLARSSL_CAMELLIA_C)
230 #if defined(POLARSSL_SHA2_C)
231 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
232 #endif /* POLARSSL_SHA2_C */
233 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
234 #endif
235 #if defined(POLARSSL_AES_C)
236 #if defined(POLARSSL_SHA2_C)
237 TLS_RSA_WITH_AES_128_CBC_SHA256,
238 #endif /* POLARSSL_SHA2_C */
239 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
240 TLS_RSA_WITH_AES_128_GCM_SHA256,
241 #endif /* POLARSSL_SHA2_C */
242 TLS_RSA_WITH_AES_128_CBC_SHA,
243 #endif
244 #if defined(POLARSSL_CAMELLIA_C)
245 #if defined(POLARSSL_SHA2_C)
246 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
247 #endif /* POLARSSL_SHA2_C */
248 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
249 #endif
250 #if defined(POLARSSL_DES_C)
251 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
252 #endif
253 #if defined(POLARSSL_ARC4_C)
254 TLS_RSA_WITH_RC4_128_SHA,
255 TLS_RSA_WITH_RC4_128_MD5,
256 #endif
257 0
258 };
259
260 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
261 {
262 ssl_context *ssl;
263 int ep;
264
265 ssl = calloc(1, sizeof(ssl_context));
266 if (!ssl)
267 return NULL;
268
269 if (ssl_init(ssl)) {
270 free(ssl);
271 return NULL;
272 }
273
274 if (ctx->server)
275 ep = SSL_IS_SERVER;
276 else
277 ep = SSL_IS_CLIENT;
278
279 ssl_set_ciphersuites(ssl, default_ciphersuites);
280 ssl_set_endpoint(ssl, ep);
281 ssl_set_authmode(ssl, ctx->auth);
282 ssl_set_rng(ssl, _urandom, NULL);
283
284 if (ctx->server) {
285 if (ctx->cert.next)
286 ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
287 ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
288 }
289
290 ssl_session_reset(ssl);
291
292 return ssl;
293 }
294
295 __hidden void __ustream_ssl_session_free(void *ssl)
296 {
297 ssl_free(ssl);
298 free(ssl);
299 }