1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 #include "packeted_bio.h"
22 #include <openssl/crypto.h>
27 const uint8_t kOpcodePacket = 'P';
28 const uint8_t kOpcodeTimeout = 'T';
29 const uint8_t kOpcodeTimeoutAck = 't';
32 explicit PacketedBio(bool advance_clock_arg)
33 : advance_clock(advance_clock_arg) {
34 memset(&timeout, 0, sizeof(timeout));
35 memset(&clock, 0, sizeof(clock));
36 memset(&read_deadline, 0, sizeof(read_deadline));
39 bool HasTimeout() const {
40 return timeout.tv_sec != 0 || timeout.tv_usec != 0;
43 bool CanRead() const {
44 if (read_deadline.tv_sec == 0 && read_deadline.tv_usec == 0) {
48 if (clock.tv_sec == read_deadline.tv_sec) {
49 return clock.tv_usec < read_deadline.tv_usec;
51 return clock.tv_sec < read_deadline.tv_sec;
56 timeval read_deadline;
60 PacketedBio *GetData(BIO *bio) {
62 /* Missing accessor BIO_get_method()?? Disabled for now */
63 if (bio->method != &g_packeted_bio_method) {
67 return (PacketedBio *)BIO_get_data(bio);
70 const PacketedBio *GetData(const BIO *bio) {
71 return GetData(const_cast<BIO*>(bio));
74 // ReadAll reads |len| bytes from |bio| into |out|. It returns 1 on success and
76 static int ReadAll(BIO *bio, uint8_t *out, size_t len) {
78 int chunk_len = INT_MAX;
82 int ret = BIO_read(bio, out, chunk_len);
92 static int PacketedWrite(BIO *bio, const char *in, int inl) {
93 if (BIO_next(bio) == NULL) {
97 BIO_clear_retry_flags(bio);
101 header[0] = kOpcodePacket;
102 header[1] = (inl >> 24) & 0xff;
103 header[2] = (inl >> 16) & 0xff;
104 header[3] = (inl >> 8) & 0xff;
105 header[4] = inl & 0xff;
106 int ret = BIO_write(BIO_next(bio), header, sizeof(header));
108 BIO_copy_next_retry(bio);
113 ret = BIO_write(BIO_next(bio), in, inl);
114 if (ret < 0 || (inl > 0 && ret == 0)) {
115 BIO_copy_next_retry(bio);
122 static int PacketedRead(BIO *bio, char *out, int outl) {
123 PacketedBio *data = GetData(bio);
124 if (BIO_next(bio) == NULL) {
128 BIO_clear_retry_flags(bio);
131 // Check if the read deadline has passed.
132 if (!data->CanRead()) {
133 BIO_set_retry_read(bio);
139 int ret = ReadAll(BIO_next(bio), &opcode, sizeof(opcode));
141 BIO_copy_next_retry(bio);
145 if (opcode == kOpcodeTimeout) {
146 // The caller is required to advance any pending timeouts before
148 if (data->HasTimeout()) {
149 fprintf(stderr, "Unprocessed timeout!\n");
153 // Process the timeout.
155 ret = ReadAll(BIO_next(bio), buf, sizeof(buf));
157 BIO_copy_next_retry(bio);
160 uint64_t timeout = (static_cast<uint64_t>(buf[0]) << 56) |
161 (static_cast<uint64_t>(buf[1]) << 48) |
162 (static_cast<uint64_t>(buf[2]) << 40) |
163 (static_cast<uint64_t>(buf[3]) << 32) |
164 (static_cast<uint64_t>(buf[4]) << 24) |
165 (static_cast<uint64_t>(buf[5]) << 16) |
166 (static_cast<uint64_t>(buf[6]) << 8) |
167 static_cast<uint64_t>(buf[7]);
168 timeout /= 1000; // Convert nanoseconds to microseconds.
170 data->timeout.tv_usec = timeout % 1000000;
171 data->timeout.tv_sec = timeout / 1000000;
173 // Send an ACK to the peer.
174 ret = BIO_write(BIO_next(bio), &kOpcodeTimeoutAck, 1);
180 if (!data->advance_clock) {
181 // Signal to the caller to retry the read, after advancing the clock.
182 BIO_set_retry_read(bio);
186 PacketedBioAdvanceClock(bio);
190 if (opcode != kOpcodePacket) {
191 fprintf(stderr, "Unknown opcode, %u\n", opcode);
195 // Read the length prefix.
196 uint8_t len_bytes[4];
197 ret = ReadAll(BIO_next(bio), len_bytes, sizeof(len_bytes));
199 BIO_copy_next_retry(bio);
203 uint32_t len = (len_bytes[0] << 24) | (len_bytes[1] << 16) |
204 (len_bytes[2] << 8) | len_bytes[3];
205 uint8_t *buf = (uint8_t *)OPENSSL_malloc(len);
209 ret = ReadAll(BIO_next(bio), buf, len);
211 fprintf(stderr, "Packeted BIO was truncated\n");
215 if (outl > (int)len) {
218 memcpy(out, buf, outl);
224 static long PacketedCtrl(BIO *bio, int cmd, long num, void *ptr) {
225 if (cmd == BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT) {
226 memcpy(&GetData(bio)->read_deadline, ptr, sizeof(timeval));
230 if (BIO_next(bio) == NULL) {
233 BIO_clear_retry_flags(bio);
234 int ret = BIO_ctrl(BIO_next(bio), cmd, num, ptr);
235 BIO_copy_next_retry(bio);
239 static int PacketedNew(BIO *bio) {
240 BIO_set_init(bio, 1);
244 static int PacketedFree(BIO *bio) {
250 BIO_set_init(bio, 0);
254 static long PacketedCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
255 if (BIO_next(bio) == NULL) {
258 return BIO_callback_ctrl(BIO_next(bio), cmd, fp);
261 static BIO_METHOD *g_packeted_bio_method = NULL;
263 static const BIO_METHOD *PacketedMethod(void)
265 if (g_packeted_bio_method == NULL) {
266 g_packeted_bio_method = BIO_meth_new(BIO_TYPE_FILTER, "packeted bio");
267 if ( g_packeted_bio_method == NULL
268 || !BIO_meth_set_write(g_packeted_bio_method, PacketedWrite)
269 || !BIO_meth_set_read(g_packeted_bio_method, PacketedRead)
270 || !BIO_meth_set_ctrl(g_packeted_bio_method, PacketedCtrl)
271 || !BIO_meth_set_create(g_packeted_bio_method, PacketedNew)
272 || !BIO_meth_set_destroy(g_packeted_bio_method, PacketedFree)
273 || !BIO_meth_set_callback_ctrl(g_packeted_bio_method,
274 PacketedCallbackCtrl))
277 return g_packeted_bio_method;
281 bssl::UniquePtr<BIO> PacketedBioCreate(bool advance_clock) {
282 bssl::UniquePtr<BIO> bio(BIO_new(PacketedMethod()));
286 BIO_set_data(bio.get(), new PacketedBio(advance_clock));
290 timeval PacketedBioGetClock(const BIO *bio) {
291 return GetData(bio)->clock;
294 bool PacketedBioAdvanceClock(BIO *bio) {
295 PacketedBio *data = GetData(bio);
296 if (data == nullptr) {
300 if (!data->HasTimeout()) {
304 data->clock.tv_usec += data->timeout.tv_usec;
305 data->clock.tv_sec += data->clock.tv_usec / 1000000;
306 data->clock.tv_usec %= 1000000;
307 data->clock.tv_sec += data->timeout.tv_sec;
308 memset(&data->timeout, 0, sizeof(data->timeout));