polarssl: fix return code of entropy function
[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         if (read(urandom_fd, out, len) < 0)
83                 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
84
85         return 0;
86 }
87
88 __hidden struct ustream_ssl_ctx *
89 __ustream_ssl_context_new(bool server)
90 {
91         struct ustream_ssl_ctx *ctx;
92
93         if (!urandom_init())
94                 return NULL;
95
96         ctx = calloc(1, sizeof(*ctx));
97         if (!ctx)
98                 return NULL;
99
100         ctx->server = server;
101 #ifdef USE_VERSION_1_3
102         pk_init(&ctx->key);
103 #else
104         rsa_init(&ctx->key, RSA_PKCS_V15, 0);
105 #endif
106
107         return ctx;
108 }
109
110 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
111 {
112         int ret;
113
114 #ifdef USE_VERSION_1_3
115         ret = x509_crt_parse_file(&ctx->ca_cert, file);
116 #else
117         ret = x509parse_crtfile(&ctx->ca_cert, file);
118 #endif
119         if (ret)
120                 return -1;
121
122         return 0;
123 }
124
125 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
126 {
127         int ret;
128
129 #ifdef USE_VERSION_1_3
130         ret = x509_crt_parse_file(&ctx->cert, file);
131 #else
132         ret = x509parse_crtfile(&ctx->cert, file);
133 #endif
134         if (ret)
135                 return -1;
136
137         return 0;
138 }
139
140 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
141 {
142         int ret;
143
144 #ifdef USE_VERSION_1_3
145         ret = pk_parse_keyfile(&ctx->key, file, NULL);
146 #else
147         ret = x509parse_keyfile(&ctx->key, file, NULL);
148 #endif
149         if (ret)
150                 return -1;
151
152         return 0;
153 }
154
155 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
156 {
157 #ifdef USE_VERSION_1_3
158         pk_free(&ctx->key);
159         x509_crt_free(&ctx->cert);
160 #else
161         rsa_free(&ctx->key);
162         x509_free(&ctx->cert);
163 #endif
164         free(ctx);
165 }
166
167 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
168 {
169         us->error = ret;
170         uloop_timeout_set(&us->error_timer, 0);
171 }
172
173 static bool ssl_do_wait(int ret)
174 {
175         switch(ret) {
176         case POLARSSL_ERR_NET_WANT_READ:
177         case POLARSSL_ERR_NET_WANT_WRITE:
178                 return true;
179         default:
180                 return false;
181         }
182 }
183
184 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
185 {
186         void *ssl = us->ssl;
187         const char *msg = NULL;
188         bool cn_mismatch;
189         int r;
190
191         r = ssl_get_verify_result(ssl);
192         cn_mismatch = r & BADCERT_CN_MISMATCH;
193         r &= ~BADCERT_CN_MISMATCH;
194
195         if (r & BADCERT_EXPIRED)
196                 msg = "certificate has expired";
197         else if (r & BADCERT_REVOKED)
198                 msg = "certificate has been revoked";
199         else if (r & BADCERT_NOT_TRUSTED)
200                 msg = "certificate is self-signed or not signed by a trusted CA";
201         else
202                 msg = "unknown error";
203
204         if (r) {
205                 if (us->notify_verify_error)
206                         us->notify_verify_error(us, r, msg);
207                 return;
208         }
209
210         if (!cn_mismatch)
211                 us->valid_cn = true;
212 }
213
214 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
215 {
216         void *ssl = us->ssl;
217         int r;
218
219         r = ssl_handshake(ssl);
220         if (r == 0) {
221                 ustream_ssl_verify_cert(us);
222                 return U_SSL_OK;
223         }
224
225         if (ssl_do_wait(r))
226                 return U_SSL_PENDING;
227
228         ustream_ssl_error(us, r);
229         return U_SSL_ERROR;
230 }
231
232 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
233 {
234         void *ssl = us->ssl;
235         int ret = ssl_write(ssl, (const unsigned char *) buf, len);
236
237         if (ret < 0) {
238                 if (ssl_do_wait(ret))
239                         return 0;
240
241                 ustream_ssl_error(us, ret);
242                 return -1;
243         }
244
245         return ret;
246 }
247
248 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
249 {
250         int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
251
252         if (ret < 0) {
253                 if (ssl_do_wait(ret))
254                         return U_SSL_PENDING;
255
256                 ustream_ssl_error(us, ret);
257                 return U_SSL_ERROR;
258         }
259
260         return ret;
261 }
262
263 static const int default_ciphersuites[] =
264 {
265 #if defined(POLARSSL_AES_C)
266 #if defined(POLARSSL_SHA2_C)
267     TLS_RSA_WITH_AES_256_CBC_SHA256,
268 #endif /* POLARSSL_SHA2_C */
269 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
270     TLS_RSA_WITH_AES_256_GCM_SHA384,
271 #endif /* POLARSSL_SHA2_C */
272     TLS_RSA_WITH_AES_256_CBC_SHA,
273 #endif
274 #if defined(POLARSSL_CAMELLIA_C)
275 #if defined(POLARSSL_SHA2_C)
276     TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
277 #endif /* POLARSSL_SHA2_C */
278     TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
279 #endif
280 #if defined(POLARSSL_AES_C)
281 #if defined(POLARSSL_SHA2_C)
282     TLS_RSA_WITH_AES_128_CBC_SHA256,
283 #endif /* POLARSSL_SHA2_C */
284 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
285     TLS_RSA_WITH_AES_128_GCM_SHA256,
286 #endif /* POLARSSL_SHA2_C */
287     TLS_RSA_WITH_AES_128_CBC_SHA,
288 #endif
289 #if defined(POLARSSL_CAMELLIA_C)
290 #if defined(POLARSSL_SHA2_C)
291     TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
292 #endif /* POLARSSL_SHA2_C */
293     TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
294 #endif
295 #if defined(POLARSSL_DES_C)
296     TLS_RSA_WITH_3DES_EDE_CBC_SHA,
297 #endif
298 #if defined(POLARSSL_ARC4_C)
299     TLS_RSA_WITH_RC4_128_SHA,
300     TLS_RSA_WITH_RC4_128_MD5,
301 #endif
302     0
303 };
304
305 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
306 {
307         ssl_context *ssl;
308         int auth;
309         int ep;
310
311         ssl = calloc(1, sizeof(ssl_context));
312         if (!ssl)
313                 return NULL;
314
315         if (ssl_init(ssl)) {
316                 free(ssl);
317                 return NULL;
318         }
319
320         if (ctx->server) {
321                 ep = SSL_IS_SERVER;
322                 auth = SSL_VERIFY_NONE;
323         } else {
324                 ep = SSL_IS_CLIENT;
325                 auth = SSL_VERIFY_OPTIONAL;
326         }
327
328         ssl_set_ciphersuites(ssl, default_ciphersuites);
329         ssl_set_endpoint(ssl, ep);
330         ssl_set_authmode(ssl, auth);
331         ssl_set_rng(ssl, _urandom, NULL);
332
333         if (ctx->server) {
334                 if (ctx->cert.next)
335                         ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
336                 ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
337         } else {
338                 ssl_set_ca_chain(ssl, &ctx->cert, NULL, NULL);
339         }
340
341         ssl_session_reset(ssl);
342
343         return ssl;
344 }
345
346 __hidden void __ustream_ssl_session_free(void *ssl)
347 {
348         ssl_free(ssl);
349         free(ssl);
350 }
351
352 __hidden void __ustream_ssl_update_peer_cn(struct ustream_ssl *us)
353 {
354         struct ustream_ssl_ctx *ctx = us->ctx;
355
356         ssl_set_ca_chain(us->ssl, &ctx->ca_cert, NULL, us->peer_cn);
357 }