polarssl: fix memory leak
[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 #include <string.h>
24
25 #include "ustream-ssl.h"
26 #include "ustream-internal.h"
27
28 static int urandom_fd = -1;
29
30 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
31 {
32 struct ustream *s = ctx;
33 char *sbuf;
34 int slen;
35
36 if (s->eof)
37 return 0;
38
39 sbuf = ustream_get_read_buf(s, &slen);
40 if (slen > len)
41 slen = len;
42
43 if (!slen)
44 return POLARSSL_ERR_NET_WANT_READ;
45
46 memcpy(buf, sbuf, slen);
47 ustream_consume(s, slen);
48
49 return slen;
50 }
51
52 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
53 {
54 struct ustream *s = ctx;
55 int ret;
56
57 ret = ustream_write(s, (const char *) buf, len, false);
58 if (ret < 0 || s->write_error)
59 return POLARSSL_ERR_NET_SEND_FAILED;
60
61 return ret;
62 }
63
64 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
65 {
66 ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
67 }
68
69 static bool urandom_init(void)
70 {
71 if (urandom_fd > -1)
72 return true;
73
74 urandom_fd = open("/dev/urandom", O_RDONLY);
75 if (urandom_fd < 0)
76 return false;
77
78 return true;
79 }
80
81 static int _urandom(void *ctx, unsigned char *out, size_t len)
82 {
83 if (read(urandom_fd, out, len) < 0)
84 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
85
86 return 0;
87 }
88
89 __hidden struct ustream_ssl_ctx *
90 __ustream_ssl_context_new(bool server)
91 {
92 struct ustream_ssl_ctx *ctx;
93
94 if (!urandom_init())
95 return NULL;
96
97 ctx = calloc(1, sizeof(*ctx));
98 if (!ctx)
99 return NULL;
100
101 ctx->server = server;
102 pk_init(&ctx->key);
103
104 return ctx;
105 }
106
107 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
108 {
109 int ret;
110
111 ret = x509_crt_parse_file(&ctx->ca_cert, file);
112 if (ret)
113 return -1;
114
115 return 0;
116 }
117
118 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
119 {
120 int ret;
121
122 ret = x509_crt_parse_file(&ctx->cert, file);
123 if (ret)
124 return -1;
125
126 return 0;
127 }
128
129 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
130 {
131 int ret;
132
133 ret = pk_parse_keyfile(&ctx->key, file, NULL);
134 if (ret)
135 return -1;
136
137 return 0;
138 }
139
140 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
141 {
142 pk_free(&ctx->key);
143 x509_crt_free(&ctx->ca_cert);
144 x509_crt_free(&ctx->cert);
145 free(ctx);
146 }
147
148 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
149 {
150 us->error = ret;
151 uloop_timeout_set(&us->error_timer, 0);
152 }
153
154 static bool ssl_do_wait(int ret)
155 {
156 switch(ret) {
157 case POLARSSL_ERR_NET_WANT_READ:
158 case POLARSSL_ERR_NET_WANT_WRITE:
159 return true;
160 default:
161 return false;
162 }
163 }
164
165 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
166 {
167 void *ssl = us->ssl;
168 const char *msg = NULL;
169 bool cn_mismatch;
170 int r;
171
172 r = ssl_get_verify_result(ssl);
173 cn_mismatch = r & BADCERT_CN_MISMATCH;
174 r &= ~BADCERT_CN_MISMATCH;
175
176 if (r & BADCERT_EXPIRED)
177 msg = "certificate has expired";
178 else if (r & BADCERT_REVOKED)
179 msg = "certificate has been revoked";
180 else if (r & BADCERT_NOT_TRUSTED)
181 msg = "certificate is self-signed or not signed by a trusted CA";
182 else
183 msg = "unknown error";
184
185 if (r) {
186 if (us->notify_verify_error)
187 us->notify_verify_error(us, r, msg);
188 return;
189 }
190
191 if (!cn_mismatch)
192 us->valid_cn = true;
193 }
194
195 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
196 {
197 void *ssl = us->ssl;
198 int r;
199
200 r = ssl_handshake(ssl);
201 if (r == 0) {
202 ustream_ssl_verify_cert(us);
203 return U_SSL_OK;
204 }
205
206 if (ssl_do_wait(r))
207 return U_SSL_PENDING;
208
209 ustream_ssl_error(us, r);
210 return U_SSL_ERROR;
211 }
212
213 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
214 {
215 void *ssl = us->ssl;
216 int done = 0, ret = 0;
217
218 while (done != len) {
219 ret = ssl_write(ssl, (const unsigned char *) buf + done, len - done);
220
221 if (ret < 0) {
222 if (ssl_do_wait(ret))
223 return done;
224
225 ustream_ssl_error(us, ret);
226 return -1;
227 }
228
229 done += ret;
230 }
231
232 return done;
233 }
234
235 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
236 {
237 int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
238
239 if (ret < 0) {
240 if (ssl_do_wait(ret))
241 return U_SSL_PENDING;
242
243 ustream_ssl_error(us, ret);
244 return U_SSL_ERROR;
245 }
246
247 return ret;
248 }
249
250 static const int default_ciphersuites[] =
251 {
252 #if defined(POLARSSL_AES_C)
253 #if defined(POLARSSL_SHA2_C)
254 TLS_RSA_WITH_AES_256_CBC_SHA256,
255 #endif /* POLARSSL_SHA2_C */
256 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
257 TLS_RSA_WITH_AES_256_GCM_SHA384,
258 #endif /* POLARSSL_SHA2_C */
259 TLS_RSA_WITH_AES_256_CBC_SHA,
260 #endif
261 #if defined(POLARSSL_CAMELLIA_C)
262 #if defined(POLARSSL_SHA2_C)
263 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
264 #endif /* POLARSSL_SHA2_C */
265 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
266 #endif
267 #if defined(POLARSSL_AES_C)
268 #if defined(POLARSSL_SHA2_C)
269 TLS_RSA_WITH_AES_128_CBC_SHA256,
270 #endif /* POLARSSL_SHA2_C */
271 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
272 TLS_RSA_WITH_AES_128_GCM_SHA256,
273 #endif /* POLARSSL_SHA2_C */
274 TLS_RSA_WITH_AES_128_CBC_SHA,
275 #endif
276 #if defined(POLARSSL_CAMELLIA_C)
277 #if defined(POLARSSL_SHA2_C)
278 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
279 #endif /* POLARSSL_SHA2_C */
280 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
281 #endif
282 #if defined(POLARSSL_DES_C)
283 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
284 #endif
285 0
286 };
287
288 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
289 {
290 ssl_context *ssl;
291 int auth;
292 int ep;
293
294 ssl = calloc(1, sizeof(ssl_context));
295 if (!ssl)
296 return NULL;
297
298 if (ssl_init(ssl)) {
299 free(ssl);
300 return NULL;
301 }
302
303 if (ctx->server) {
304 ep = SSL_IS_SERVER;
305 auth = SSL_VERIFY_NONE;
306 } else {
307 ep = SSL_IS_CLIENT;
308 auth = SSL_VERIFY_OPTIONAL;
309 }
310
311 ssl_set_ciphersuites(ssl, default_ciphersuites);
312 ssl_set_endpoint(ssl, ep);
313 ssl_set_authmode(ssl, auth);
314 ssl_set_rng(ssl, _urandom, NULL);
315
316 if (ctx->server) {
317 if (ctx->cert.next)
318 ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
319 ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
320 } else {
321 ssl_set_ca_chain(ssl, &ctx->ca_cert, NULL, NULL);
322 }
323
324 ssl_session_reset(ssl);
325
326 return ssl;
327 }
328
329 __hidden void __ustream_ssl_session_free(void *ssl)
330 {
331 ssl_free(ssl);
332 free(ssl);
333 }
334
335 __hidden void __ustream_ssl_update_peer_cn(struct ustream_ssl *us)
336 {
337 struct ustream_ssl_ctx *ctx = us->ctx;
338
339 ssl_set_ca_chain(us->ssl, &ctx->ca_cert, NULL, us->peer_cn);
340 }