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