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