summaryrefslogtreecommitdiff
path: root/ustream-io-cyassl.c
blob: d97d55ef3ffb50743a96ad1358f14bc2656f9293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * ustream-ssl - library for SSL over ustream
 *
 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <string.h>

#include <libubox/ustream.h>

#include "ustream-ssl.h"
#include "ustream-internal.h"

#ifdef HAVE_CYASSL_VERSION_H
#include <cyassl/version.h>
#else
#define LIBCYASSL_VERSION_HEX 0
#endif

static int s_ustream_read(char *buf, int len, void *ctx)
{
	struct ustream *s = ctx;
	char *sbuf;
	int slen;

	if (s->eof)
		return -3;

	sbuf = ustream_get_read_buf(s, &slen);
	if (slen > len)
		slen = len;

	if (!slen)
		return -2;

	memcpy(buf, sbuf, slen);
	ustream_consume(s, slen);

	return slen;
}

static int s_ustream_write(char *buf, int len, void *ctx)
{
	struct ustream *s = ctx;

	if (s->write_error)
		return len;

	return ustream_write(s, buf, len, false);
}

#if (LIBCYASSL_VERSION_HEX > 0)
static int io_recv_cb(SSL* ssl, char *buf, int sz, void *ctx)
{
	return s_ustream_read(buf, sz, ctx);
}

static int io_send_cb(SSL* ssl, char *buf, int sz, void *ctx)
{
	return s_ustream_write(buf, sz, ctx);
}
#else
/* not defined in the header file */
typedef int (*CallbackIORecv)(char *buf, int sz, void *ctx);
typedef int (*CallbackIOSend)(char *buf, int sz, void *ctx);

void SetCallbackIORecv_Ctx(SSL_CTX*, CallbackIORecv);
void SetCallbackIOSend_Ctx(SSL_CTX*, CallbackIOSend);
void SetCallbackIO_ReadCtx(SSL* ssl, void *rctx);
void SetCallbackIO_WriteCtx(SSL* ssl, void *wctx);

#define CyaSSL_SetIOReadCtx SetCallbackIO_ReadCtx
#define CyaSSL_SetIOWriteCtx SetCallbackIO_WriteCtx
#define CyaSSL_SetIORecv SetCallbackIORecv_Ctx
#define CyaSSL_SetIOSend SetCallbackIOSend_Ctx

static int io_recv_cb(char *buf, int sz, void *ctx)
{
	return s_ustream_read(buf, sz, ctx);
}

static int io_send_cb(char *buf, int sz, void *ctx)
{
	return s_ustream_write(buf, sz, ctx);
}
#endif

__hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
{
	CyaSSL_SetIOReadCtx(ssl, conn);
	CyaSSL_SetIOWriteCtx(ssl, conn);
	CyaSSL_SetIORecv((void *) ctx, io_recv_cb);
	CyaSSL_SetIOSend((void *) ctx, io_send_cb);
}