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/mem.h>
24 #include "../../crypto/internal.h"
29 extern const BIO_METHOD g_packeted_bio_method;
31 const uint8_t kOpcodePacket = 'P';
32 const uint8_t kOpcodeTimeout = 'T';
33 const uint8_t kOpcodeTimeoutAck = 't';
36 explicit PacketedBio(timeval *clock_arg)
38 OPENSSL_memset(&timeout, 0, sizeof(timeout));
41 bool HasTimeout() const {
42 return timeout.tv_sec != 0 || timeout.tv_usec != 0;
49 PacketedBio *GetData(BIO *bio) {
50 if (bio->method != &g_packeted_bio_method) {
53 return (PacketedBio *)bio->ptr;
56 // ReadAll reads |len| bytes from |bio| into |out|. It returns 1 on success and
58 static int ReadAll(BIO *bio, uint8_t *out, size_t len) {
60 int chunk_len = INT_MAX;
64 int ret = BIO_read(bio, out, chunk_len);
74 static int PacketedWrite(BIO *bio, const char *in, int inl) {
75 if (bio->next_bio == NULL) {
79 BIO_clear_retry_flags(bio);
83 header[0] = kOpcodePacket;
84 header[1] = (inl >> 24) & 0xff;
85 header[2] = (inl >> 16) & 0xff;
86 header[3] = (inl >> 8) & 0xff;
87 header[4] = inl & 0xff;
88 int ret = BIO_write(bio->next_bio, header, sizeof(header));
90 BIO_copy_next_retry(bio);
95 ret = BIO_write(bio->next_bio, in, inl);
96 if (ret < 0 || (inl > 0 && ret == 0)) {
97 BIO_copy_next_retry(bio);
104 static int PacketedRead(BIO *bio, char *out, int outl) {
105 PacketedBio *data = GetData(bio);
106 if (bio->next_bio == NULL) {
110 BIO_clear_retry_flags(bio);
114 int ret = ReadAll(bio->next_bio, &opcode, sizeof(opcode));
116 BIO_copy_next_retry(bio);
120 if (opcode == kOpcodeTimeout) {
121 // The caller is required to advance any pending timeouts before continuing.
122 if (data->HasTimeout()) {
123 fprintf(stderr, "Unprocessed timeout!\n");
127 // Process the timeout.
129 ret = ReadAll(bio->next_bio, buf, sizeof(buf));
131 BIO_copy_next_retry(bio);
134 uint64_t timeout = (static_cast<uint64_t>(buf[0]) << 56) |
135 (static_cast<uint64_t>(buf[1]) << 48) |
136 (static_cast<uint64_t>(buf[2]) << 40) |
137 (static_cast<uint64_t>(buf[3]) << 32) |
138 (static_cast<uint64_t>(buf[4]) << 24) |
139 (static_cast<uint64_t>(buf[5]) << 16) |
140 (static_cast<uint64_t>(buf[6]) << 8) |
141 static_cast<uint64_t>(buf[7]);
142 timeout /= 1000; // Convert nanoseconds to microseconds.
144 data->timeout.tv_usec = timeout % 1000000;
145 data->timeout.tv_sec = timeout / 1000000;
147 // Send an ACK to the peer.
148 ret = BIO_write(bio->next_bio, &kOpcodeTimeoutAck, 1);
154 // Signal to the caller to retry the read, after advancing the clock.
155 BIO_set_retry_read(bio);
159 if (opcode != kOpcodePacket) {
160 fprintf(stderr, "Unknown opcode, %u\n", opcode);
164 // Read the length prefix.
165 uint8_t len_bytes[4];
166 ret = ReadAll(bio->next_bio, len_bytes, sizeof(len_bytes));
168 BIO_copy_next_retry(bio);
172 uint32_t len = (len_bytes[0] << 24) | (len_bytes[1] << 16) |
173 (len_bytes[2] << 8) | len_bytes[3];
174 uint8_t *buf = (uint8_t *)OPENSSL_malloc(len);
178 ret = ReadAll(bio->next_bio, buf, len);
180 fprintf(stderr, "Packeted BIO was truncated\n");
184 if (outl > (int)len) {
187 OPENSSL_memcpy(out, buf, outl);
192 static long PacketedCtrl(BIO *bio, int cmd, long num, void *ptr) {
193 if (bio->next_bio == NULL) {
197 BIO_clear_retry_flags(bio);
198 int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr);
199 BIO_copy_next_retry(bio);
203 static int PacketedNew(BIO *bio) {
208 static int PacketedFree(BIO *bio) {
218 static long PacketedCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
219 if (bio->next_bio == NULL) {
222 return BIO_callback_ctrl(bio->next_bio, cmd, fp);
225 const BIO_METHOD g_packeted_bio_method = {
235 PacketedCallbackCtrl,
240 bssl::UniquePtr<BIO> PacketedBioCreate(timeval *clock) {
241 bssl::UniquePtr<BIO> bio(BIO_new(&g_packeted_bio_method));
245 bio->ptr = new PacketedBio(clock);
249 bool PacketedBioAdvanceClock(BIO *bio) {
250 PacketedBio *data = GetData(bio);
251 if (data == nullptr) {
255 if (!data->HasTimeout()) {
259 data->clock->tv_usec += data->timeout.tv_usec;
260 data->clock->tv_sec += data->clock->tv_usec / 1000000;
261 data->clock->tv_usec %= 1000000;
262 data->clock->tv_sec += data->timeout.tv_sec;
263 OPENSSL_memset(&data->timeout, 0, sizeof(data->timeout));