David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include "async_bio.h" |
| 16 | |
| 17 | #include <errno.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 20 | #include <openssl/bio.h> |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 21 | #include <openssl/mem.h> |
| 22 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 23 | #include "../../crypto/internal.h" |
| 24 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 25 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 26 | namespace { |
| 27 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 28 | extern const BIO_METHOD g_async_bio_method; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 29 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 30 | struct AsyncBio { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 31 | bool datagram; |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 32 | bool enforce_write_quota; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 33 | size_t read_quota; |
| 34 | size_t write_quota; |
| 35 | }; |
| 36 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 37 | AsyncBio *GetData(BIO *bio) { |
| 38 | if (bio->method != &g_async_bio_method) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 39 | return NULL; |
| 40 | } |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 41 | return (AsyncBio *)bio->ptr; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 42 | } |
| 43 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 44 | static int AsyncWrite(BIO *bio, const char *in, int inl) { |
| 45 | AsyncBio *a = GetData(bio); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 46 | if (a == NULL || bio->next_bio == NULL) { |
| 47 | return 0; |
| 48 | } |
| 49 | |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 50 | if (!a->enforce_write_quota) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 51 | return BIO_write(bio->next_bio, in, inl); |
| 52 | } |
| 53 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 54 | BIO_clear_retry_flags(bio); |
| 55 | |
| 56 | if (a->write_quota == 0) { |
| 57 | BIO_set_retry_write(bio); |
| 58 | errno = EAGAIN; |
| 59 | return -1; |
| 60 | } |
| 61 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 62 | if (!a->datagram && (size_t)inl > a->write_quota) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 63 | inl = a->write_quota; |
| 64 | } |
| 65 | int ret = BIO_write(bio->next_bio, in, inl); |
| 66 | if (ret <= 0) { |
| 67 | BIO_copy_next_retry(bio); |
| 68 | } else { |
David Benjamin | af19de3 | 2015-01-11 19:50:31 -0500 | [diff] [blame] | 69 | a->write_quota -= (a->datagram ? 1 : ret); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 70 | } |
| 71 | return ret; |
| 72 | } |
| 73 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 74 | static int AsyncRead(BIO *bio, char *out, int outl) { |
| 75 | AsyncBio *a = GetData(bio); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 76 | if (a == NULL || bio->next_bio == NULL) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | BIO_clear_retry_flags(bio); |
| 81 | |
| 82 | if (a->read_quota == 0) { |
| 83 | BIO_set_retry_read(bio); |
| 84 | errno = EAGAIN; |
| 85 | return -1; |
| 86 | } |
| 87 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 88 | if (!a->datagram && (size_t)outl > a->read_quota) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 89 | outl = a->read_quota; |
| 90 | } |
| 91 | int ret = BIO_read(bio->next_bio, out, outl); |
| 92 | if (ret <= 0) { |
| 93 | BIO_copy_next_retry(bio); |
| 94 | } else { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 95 | a->read_quota -= (a->datagram ? 1 : ret); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 96 | } |
| 97 | return ret; |
| 98 | } |
| 99 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 100 | static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 101 | if (bio->next_bio == NULL) { |
| 102 | return 0; |
| 103 | } |
| 104 | BIO_clear_retry_flags(bio); |
| 105 | int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 106 | BIO_copy_next_retry(bio); |
| 107 | return ret; |
| 108 | } |
| 109 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 110 | static int AsyncNew(BIO *bio) { |
| 111 | AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a)); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 112 | if (a == NULL) { |
| 113 | return 0; |
| 114 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 115 | OPENSSL_memset(a, 0, sizeof(*a)); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 116 | a->enforce_write_quota = true; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 117 | bio->init = 1; |
| 118 | bio->ptr = (char *)a; |
| 119 | return 1; |
| 120 | } |
| 121 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 122 | static int AsyncFree(BIO *bio) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 123 | if (bio == NULL) { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | OPENSSL_free(bio->ptr); |
| 128 | bio->ptr = NULL; |
| 129 | bio->init = 0; |
| 130 | bio->flags = 0; |
| 131 | return 1; |
| 132 | } |
| 133 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 134 | static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 135 | if (bio->next_bio == NULL) { |
| 136 | return 0; |
| 137 | } |
| 138 | return BIO_callback_ctrl(bio->next_bio, cmd, fp); |
| 139 | } |
| 140 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 141 | const BIO_METHOD g_async_bio_method = { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 142 | BIO_TYPE_FILTER, |
| 143 | "async bio", |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 144 | AsyncWrite, |
| 145 | AsyncRead, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 146 | NULL /* puts */, |
| 147 | NULL /* gets */, |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 148 | AsyncCtrl, |
| 149 | AsyncNew, |
| 150 | AsyncFree, |
| 151 | AsyncCallbackCtrl, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // namespace |
| 155 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 156 | bssl::UniquePtr<BIO> AsyncBioCreate() { |
| 157 | return bssl::UniquePtr<BIO>(BIO_new(&g_async_bio_method)); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 160 | bssl::UniquePtr<BIO> AsyncBioCreateDatagram() { |
| 161 | bssl::UniquePtr<BIO> ret(BIO_new(&g_async_bio_method)); |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 162 | if (!ret) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 163 | return nullptr; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 164 | } |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 165 | GetData(ret.get())->datagram = true; |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 166 | return ret; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 167 | } |
| 168 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 169 | void AsyncBioAllowRead(BIO *bio, size_t count) { |
| 170 | AsyncBio *a = GetData(bio); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 171 | if (a == NULL) { |
| 172 | return; |
| 173 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 174 | a->read_quota += count; |
| 175 | } |
| 176 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 177 | void AsyncBioAllowWrite(BIO *bio, size_t count) { |
| 178 | AsyncBio *a = GetData(bio); |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 179 | if (a == NULL) { |
| 180 | return; |
| 181 | } |
| 182 | a->write_quota += count; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 183 | } |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 184 | |
| 185 | void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) { |
| 186 | AsyncBio *a = GetData(bio); |
| 187 | if (a == NULL) { |
| 188 | return; |
| 189 | } |
| 190 | a->enforce_write_quota = enforce; |
| 191 | } |