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-io.h"
24 #include "ustream-ssl.h"
26 #ifdef CYASSL_OPENSSL_H_
28 /* not defined in the header file */
29 typedef int (*CallbackIORecv)(char *buf, int sz, void *ctx);
30 typedef int (*CallbackIOSend)(char *buf, int sz, void *ctx);
32 void SetCallbackIORecv_Ctx(SSL_CTX*, CallbackIORecv);
33 void SetCallbackIOSend_Ctx(SSL_CTX*, CallbackIOSend);
34 void SetCallbackIO_ReadCtx(SSL* ssl, void *rctx);
35 void SetCallbackIO_WriteCtx(SSL* ssl, void *wctx);
37 static int s_ustream_read(char *buf, int len, void *ctx)
39 struct ustream *s = ctx;
46 sbuf = ustream_get_read_buf(s, &slen);
53 memcpy(buf, sbuf, slen);
54 ustream_consume(s, slen);
59 static int s_ustream_write(char *buf, int len, void *ctx)
61 struct ustream *s = ctx;
63 return ustream_write(s, buf, len, false);
66 void ustream_set_io(SSL_CTX *ctx, SSL *ssl, struct ustream *conn)
68 SetCallbackIO_ReadCtx(ssl, conn);
69 SetCallbackIO_WriteCtx(ssl, conn);
70 SetCallbackIORecv_Ctx(ctx, s_ustream_read);
71 SetCallbackIOSend_Ctx(ctx, s_ustream_write);
87 s_ustream_free(BIO *b)
99 s_ustream_read(BIO *b, char *buf, int len)
105 if (!buf || len <= 0)
108 s = (struct ustream *)b->ptr;
112 sbuf = ustream_get_read_buf(s, &slen);
114 BIO_clear_retry_flags(b);
116 BIO_set_retry_read(b);
123 memcpy(buf, sbuf, slen);
124 ustream_consume(s, slen);
130 s_ustream_write(BIO *b, const char *buf, int len)
134 if (!buf || len <= 0)
137 s = (struct ustream *)b->ptr;
141 return ustream_write(s, buf, len, false);
145 s_ustream_gets(BIO *b, char *buf, int len)
151 s_ustream_puts(BIO *b, const char *str)
153 return s_ustream_write(b, str, strlen(str));
156 static long s_ustream_ctrl(BIO *b, int cmd, long num, void *ptr)
166 static BIO_METHOD methods_ustream = {
167 100 | BIO_TYPE_SOURCE_SINK,
179 static BIO *ustream_bio_new(struct ustream *s)
183 bio = BIO_new(&methods_ustream);
188 void ustream_set_io(SSL_CTX *ctx, SSL *ssl, struct ustream *conn)
190 BIO *bio = ustream_bio_new(conn);
191 SSL_set_bio(ssl, bio, bio);