1*4724848cSchristos /*
2*4724848cSchristos * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include "async_bio.h"
11*4724848cSchristos
12*4724848cSchristos #include <errno.h>
13*4724848cSchristos #include <string.h>
14*4724848cSchristos
15*4724848cSchristos #include <openssl/bio.h>
16*4724848cSchristos #include <openssl/crypto.h>
17*4724848cSchristos
18*4724848cSchristos
19*4724848cSchristos namespace {
20*4724848cSchristos
21*4724848cSchristos struct AsyncBio {
22*4724848cSchristos bool datagram;
23*4724848cSchristos bool enforce_write_quota;
24*4724848cSchristos size_t read_quota;
25*4724848cSchristos size_t write_quota;
26*4724848cSchristos };
27*4724848cSchristos
GetData(BIO * bio)28*4724848cSchristos AsyncBio *GetData(BIO *bio) {
29*4724848cSchristos return (AsyncBio *)BIO_get_data(bio);
30*4724848cSchristos }
31*4724848cSchristos
AsyncWrite(BIO * bio,const char * in,int inl)32*4724848cSchristos static int AsyncWrite(BIO *bio, const char *in, int inl) {
33*4724848cSchristos AsyncBio *a = GetData(bio);
34*4724848cSchristos if (a == NULL || BIO_next(bio) == NULL) {
35*4724848cSchristos return 0;
36*4724848cSchristos }
37*4724848cSchristos
38*4724848cSchristos if (!a->enforce_write_quota) {
39*4724848cSchristos return BIO_write(BIO_next(bio), in, inl);
40*4724848cSchristos }
41*4724848cSchristos
42*4724848cSchristos BIO_clear_retry_flags(bio);
43*4724848cSchristos
44*4724848cSchristos if (a->write_quota == 0) {
45*4724848cSchristos BIO_set_retry_write(bio);
46*4724848cSchristos errno = EAGAIN;
47*4724848cSchristos return -1;
48*4724848cSchristos }
49*4724848cSchristos
50*4724848cSchristos if (!a->datagram && (size_t)inl > a->write_quota) {
51*4724848cSchristos inl = a->write_quota;
52*4724848cSchristos }
53*4724848cSchristos int ret = BIO_write(BIO_next(bio), in, inl);
54*4724848cSchristos if (ret <= 0) {
55*4724848cSchristos BIO_copy_next_retry(bio);
56*4724848cSchristos } else {
57*4724848cSchristos a->write_quota -= (a->datagram ? 1 : ret);
58*4724848cSchristos }
59*4724848cSchristos return ret;
60*4724848cSchristos }
61*4724848cSchristos
AsyncRead(BIO * bio,char * out,int outl)62*4724848cSchristos static int AsyncRead(BIO *bio, char *out, int outl) {
63*4724848cSchristos AsyncBio *a = GetData(bio);
64*4724848cSchristos if (a == NULL || BIO_next(bio) == NULL) {
65*4724848cSchristos return 0;
66*4724848cSchristos }
67*4724848cSchristos
68*4724848cSchristos BIO_clear_retry_flags(bio);
69*4724848cSchristos
70*4724848cSchristos if (a->read_quota == 0) {
71*4724848cSchristos BIO_set_retry_read(bio);
72*4724848cSchristos errno = EAGAIN;
73*4724848cSchristos return -1;
74*4724848cSchristos }
75*4724848cSchristos
76*4724848cSchristos if (!a->datagram && (size_t)outl > a->read_quota) {
77*4724848cSchristos outl = a->read_quota;
78*4724848cSchristos }
79*4724848cSchristos int ret = BIO_read(BIO_next(bio), out, outl);
80*4724848cSchristos if (ret <= 0) {
81*4724848cSchristos BIO_copy_next_retry(bio);
82*4724848cSchristos } else {
83*4724848cSchristos a->read_quota -= (a->datagram ? 1 : ret);
84*4724848cSchristos }
85*4724848cSchristos return ret;
86*4724848cSchristos }
87*4724848cSchristos
AsyncCtrl(BIO * bio,int cmd,long num,void * ptr)88*4724848cSchristos static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) {
89*4724848cSchristos if (BIO_next(bio) == NULL) {
90*4724848cSchristos return 0;
91*4724848cSchristos }
92*4724848cSchristos BIO_clear_retry_flags(bio);
93*4724848cSchristos int ret = BIO_ctrl(BIO_next(bio), cmd, num, ptr);
94*4724848cSchristos BIO_copy_next_retry(bio);
95*4724848cSchristos return ret;
96*4724848cSchristos }
97*4724848cSchristos
AsyncNew(BIO * bio)98*4724848cSchristos static int AsyncNew(BIO *bio) {
99*4724848cSchristos AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a));
100*4724848cSchristos if (a == NULL) {
101*4724848cSchristos return 0;
102*4724848cSchristos }
103*4724848cSchristos memset(a, 0, sizeof(*a));
104*4724848cSchristos a->enforce_write_quota = true;
105*4724848cSchristos BIO_set_init(bio, 1);
106*4724848cSchristos BIO_set_data(bio, a);
107*4724848cSchristos return 1;
108*4724848cSchristos }
109*4724848cSchristos
AsyncFree(BIO * bio)110*4724848cSchristos static int AsyncFree(BIO *bio) {
111*4724848cSchristos if (bio == NULL) {
112*4724848cSchristos return 0;
113*4724848cSchristos }
114*4724848cSchristos
115*4724848cSchristos OPENSSL_free(BIO_get_data(bio));
116*4724848cSchristos BIO_set_data(bio, NULL);
117*4724848cSchristos BIO_set_init(bio, 0);
118*4724848cSchristos return 1;
119*4724848cSchristos }
120*4724848cSchristos
AsyncCallbackCtrl(BIO * bio,int cmd,BIO_info_cb fp)121*4724848cSchristos static long AsyncCallbackCtrl(BIO *bio, int cmd, BIO_info_cb fp)
122*4724848cSchristos {
123*4724848cSchristos if (BIO_next(bio) == NULL)
124*4724848cSchristos return 0;
125*4724848cSchristos return BIO_callback_ctrl(BIO_next(bio), cmd, fp);
126*4724848cSchristos }
127*4724848cSchristos
128*4724848cSchristos static BIO_METHOD *g_async_bio_method = NULL;
129*4724848cSchristos
AsyncMethod(void)130*4724848cSchristos static const BIO_METHOD *AsyncMethod(void)
131*4724848cSchristos {
132*4724848cSchristos if (g_async_bio_method == NULL) {
133*4724848cSchristos g_async_bio_method = BIO_meth_new(BIO_TYPE_FILTER, "async bio");
134*4724848cSchristos if ( g_async_bio_method == NULL
135*4724848cSchristos || !BIO_meth_set_write(g_async_bio_method, AsyncWrite)
136*4724848cSchristos || !BIO_meth_set_read(g_async_bio_method, AsyncRead)
137*4724848cSchristos || !BIO_meth_set_ctrl(g_async_bio_method, AsyncCtrl)
138*4724848cSchristos || !BIO_meth_set_create(g_async_bio_method, AsyncNew)
139*4724848cSchristos || !BIO_meth_set_destroy(g_async_bio_method, AsyncFree)
140*4724848cSchristos || !BIO_meth_set_callback_ctrl(g_async_bio_method, AsyncCallbackCtrl))
141*4724848cSchristos return NULL;
142*4724848cSchristos }
143*4724848cSchristos return g_async_bio_method;
144*4724848cSchristos }
145*4724848cSchristos
146*4724848cSchristos } // namespace
147*4724848cSchristos
AsyncBioCreate()148*4724848cSchristos bssl::UniquePtr<BIO> AsyncBioCreate() {
149*4724848cSchristos return bssl::UniquePtr<BIO>(BIO_new(AsyncMethod()));
150*4724848cSchristos }
151*4724848cSchristos
AsyncBioCreateDatagram()152*4724848cSchristos bssl::UniquePtr<BIO> AsyncBioCreateDatagram() {
153*4724848cSchristos bssl::UniquePtr<BIO> ret(BIO_new(AsyncMethod()));
154*4724848cSchristos if (!ret) {
155*4724848cSchristos return nullptr;
156*4724848cSchristos }
157*4724848cSchristos GetData(ret.get())->datagram = true;
158*4724848cSchristos return ret;
159*4724848cSchristos }
160*4724848cSchristos
AsyncBioAllowRead(BIO * bio,size_t count)161*4724848cSchristos void AsyncBioAllowRead(BIO *bio, size_t count) {
162*4724848cSchristos AsyncBio *a = GetData(bio);
163*4724848cSchristos if (a == NULL) {
164*4724848cSchristos return;
165*4724848cSchristos }
166*4724848cSchristos a->read_quota += count;
167*4724848cSchristos }
168*4724848cSchristos
AsyncBioAllowWrite(BIO * bio,size_t count)169*4724848cSchristos void AsyncBioAllowWrite(BIO *bio, size_t count) {
170*4724848cSchristos AsyncBio *a = GetData(bio);
171*4724848cSchristos if (a == NULL) {
172*4724848cSchristos return;
173*4724848cSchristos }
174*4724848cSchristos a->write_quota += count;
175*4724848cSchristos }
176*4724848cSchristos
AsyncBioEnforceWriteQuota(BIO * bio,bool enforce)177*4724848cSchristos void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) {
178*4724848cSchristos AsyncBio *a = GetData(bio);
179*4724848cSchristos if (a == NULL) {
180*4724848cSchristos return;
181*4724848cSchristos }
182*4724848cSchristos a->enforce_write_quota = enforce;
183*4724848cSchristos }
184