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 "openssl_bio_compat.h"
25 #include "ustream-internal.h"
31 BIO_set_data(b
, NULL
);
32 BIO_clear_flags(b
, ~0);
37 s_ustream_free(BIO
*b
)
42 BIO_set_data(b
, NULL
);
44 BIO_clear_flags(b
, ~0);
49 s_ustream_read(BIO
*b
, char *buf
, int len
)
58 ctx
= (struct bio_ctx
*)BIO_get_data(b
);
59 if (!ctx
|| !ctx
->stream
)
62 sbuf
= ustream_get_read_buf(ctx
->stream
, &slen
);
64 BIO_clear_retry_flags(b
);
66 BIO_set_retry_read(b
);
73 memcpy(buf
, sbuf
, slen
);
74 ustream_consume(ctx
->stream
, slen
);
80 s_ustream_write(BIO
*b
, const char *buf
, int len
)
87 ctx
= (struct bio_ctx
*)BIO_get_data(b
);
88 if (!ctx
|| !ctx
->stream
)
91 if (ctx
->stream
->write_error
)
94 return ustream_write(ctx
->stream
, buf
, len
, false);
98 s_ustream_gets(BIO
*b
, char *buf
, int len
)
104 s_ustream_puts(BIO
*b
, const char *str
)
106 return s_ustream_write(b
, str
, strlen(str
));
109 static long s_ustream_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
119 static BIO
*ustream_bio_new(struct ustream
*s
)
122 struct bio_ctx
*ctx
= calloc(1, sizeof(struct bio_ctx
));
125 ctx
->meth
= BIO_meth_new(100 | BIO_TYPE_SOURCE_SINK
, "ustream");
127 BIO_meth_set_write(ctx
->meth
, s_ustream_write
);
128 BIO_meth_set_read(ctx
->meth
, s_ustream_read
);
129 BIO_meth_set_puts(ctx
->meth
, s_ustream_puts
);
130 BIO_meth_set_gets(ctx
->meth
, s_ustream_gets
);
131 BIO_meth_set_ctrl(ctx
->meth
, s_ustream_ctrl
);
132 BIO_meth_set_create(ctx
->meth
, s_ustream_new
);
133 BIO_meth_set_destroy(ctx
->meth
, s_ustream_free
);
134 bio
= BIO_new(ctx
->meth
);
135 BIO_set_data(bio
, ctx
);
140 __hidden
void ustream_set_io(struct ustream_ssl_ctx
*ctx
, void *ssl
, struct ustream
*conn
)
142 BIO
*bio
= ustream_bio_new(conn
);
143 SSL_set_bio(ssl
, bio
, bio
);