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