2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
21 #include <libubox/ustream.h>
23 #include "ustream-ssl.h"
24 #include "ustream-internal.h"
26 static void ustream_ssl_error_cb(struct uloop_timeout *t)
28 struct ustream_ssl *us = container_of(t, struct ustream_ssl, error_timer);
29 static char buffer[128];
30 int error = us->error;
33 us->notify_error(us, error, __ustream_ssl_strerror(us->error, buffer, sizeof(buffer)));
36 static void ustream_ssl_check_conn(struct ustream_ssl *us)
38 if (us->connected || us->error)
41 if (__ustream_ssl_connect(us) == U_SSL_OK) {
43 if (us->notify_connected)
44 us->notify_connected(us);
45 ustream_write_pending(&us->stream);
49 static bool __ustream_ssl_poll(struct ustream *s)
51 struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
56 ustream_ssl_check_conn(us);
57 if (!us->connected || us->error)
61 buf = ustream_reserve(&us->stream, 1, &len);
65 ret = __ustream_ssl_read(us, buf, len);
72 us->stream.eof = true;
73 ustream_state_change(&us->stream);
76 ustream_fill_read(&us->stream, ret);
85 static void ustream_ssl_notify_read(struct ustream *s, int bytes)
87 __ustream_ssl_poll(s);
90 static void ustream_ssl_notify_write(struct ustream *s, int bytes)
92 struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
94 ustream_ssl_check_conn(us);
95 ustream_write_pending(s->next);
98 static void ustream_ssl_notify_state(struct ustream *s)
100 s->next->write_error = true;
101 ustream_state_change(s->next);
104 static int ustream_ssl_write(struct ustream *s, const char *buf, int len, bool more)
106 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
108 if (!us->connected || us->error)
111 if (us->conn->w.data_bytes)
114 return __ustream_ssl_write(us, buf, len);
117 static void ustream_ssl_set_read_blocked(struct ustream *s)
119 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
121 ustream_set_read_blocked(us->conn, !!s->read_blocked);
124 static void ustream_ssl_free(struct ustream *s)
126 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
129 us->conn->next = NULL;
130 us->conn->notify_read = NULL;
131 us->conn->notify_write = NULL;
132 us->conn->notify_state = NULL;
135 uloop_timeout_cancel(&us->error_timer);
136 __ustream_ssl_session_free(us->ssl);
143 us->connected = false;
145 us->valid_cert = false;
146 us->valid_cn = false;
149 static bool ustream_ssl_poll(struct ustream *s)
151 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
154 fd_poll = ustream_poll(us->conn);
155 return __ustream_ssl_poll(s) || fd_poll;
158 static void ustream_ssl_stream_init(struct ustream_ssl *us)
160 struct ustream *conn = us->conn;
161 struct ustream *s = &us->stream;
163 conn->notify_read = ustream_ssl_notify_read;
164 conn->notify_write = ustream_ssl_notify_write;
165 conn->notify_state = ustream_ssl_notify_state;
167 s->free = ustream_ssl_free;
168 s->write = ustream_ssl_write;
169 s->poll = ustream_ssl_poll;
170 s->set_read_blocked = ustream_ssl_set_read_blocked;
171 ustream_init_defaults(s);
174 static int _ustream_ssl_init(struct ustream_ssl *us, struct ustream *conn, struct ustream_ssl_ctx *ctx, bool server)
176 us->error_timer.cb = ustream_ssl_error_cb;
181 us->ssl = __ustream_ssl_session_new(us->ctx);
185 conn->next = &us->stream;
186 ustream_set_io(ctx, us->ssl, conn);
187 ustream_ssl_stream_init(us);
188 ustream_ssl_check_conn(us);
193 static int _ustream_ssl_set_peer_cn(struct ustream_ssl *us, const char *name)
195 us->peer_cn = strdup(name);
196 __ustream_ssl_update_peer_cn(us);
201 const struct ustream_ssl_ops ustream_ssl_ops = {
202 .context_new = __ustream_ssl_context_new,
203 .context_set_crt_file = __ustream_ssl_set_crt_file,
204 .context_set_key_file = __ustream_ssl_set_key_file,
205 .context_add_ca_crt_file = __ustream_ssl_add_ca_crt_file,
206 .context_free = __ustream_ssl_context_free,
207 .init = _ustream_ssl_init,
208 .set_peer_cn = _ustream_ssl_set_peer_cn,