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 #ifdef HAVE_CYASSL_VERSION_H
27 #include <cyassl/version.h>
29 #define LIBCYASSL_VERSION_HEX 0
32 static int s_ustream_read(char *buf, int len, void *ctx)
34 struct ustream *s = ctx;
41 sbuf = ustream_get_read_buf(s, &slen);
48 memcpy(buf, sbuf, slen);
49 ustream_consume(s, slen);
54 static int s_ustream_write(char *buf, int len, void *ctx)
56 struct ustream *s = ctx;
61 return ustream_write(s, buf, len, false);
64 #if (LIBCYASSL_VERSION_HEX > 0)
65 static int io_recv_cb(SSL* ssl, char *buf, int sz, void *ctx)
67 return s_ustream_read(buf, sz, ctx);
70 static int io_send_cb(SSL* ssl, char *buf, int sz, void *ctx)
72 return s_ustream_write(buf, sz, ctx);
75 /* not defined in the header file */
76 typedef int (*CallbackIORecv)(char *buf, int sz, void *ctx);
77 typedef int (*CallbackIOSend)(char *buf, int sz, void *ctx);
79 void SetCallbackIORecv_Ctx(SSL_CTX*, CallbackIORecv);
80 void SetCallbackIOSend_Ctx(SSL_CTX*, CallbackIOSend);
81 void SetCallbackIO_ReadCtx(SSL* ssl, void *rctx);
82 void SetCallbackIO_WriteCtx(SSL* ssl, void *wctx);
84 #define CyaSSL_SetIOReadCtx SetCallbackIO_ReadCtx
85 #define CyaSSL_SetIOWriteCtx SetCallbackIO_WriteCtx
86 #define CyaSSL_SetIORecv SetCallbackIORecv_Ctx
87 #define CyaSSL_SetIOSend SetCallbackIOSend_Ctx
89 static int io_recv_cb(char *buf, int sz, void *ctx)
91 return s_ustream_read(buf, sz, ctx);
94 static int io_send_cb(char *buf, int sz, void *ctx)
96 return s_ustream_write(buf, sz, ctx);
100 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
102 CyaSSL_SetIOReadCtx(ssl, conn);
103 CyaSSL_SetIOWriteCtx(ssl, conn);
104 CyaSSL_SetIORecv((void *) ctx, io_recv_cb);
105 CyaSSL_SetIOSend((void *) ctx, io_send_cb);