| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "packeted_bio.h" |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
#include "../../crypto/internal.h" |
| 14 |
|
| 15 |
|
| 16 |
namespace { |
| 17 |
|
| 18 |
extern const BIO_METHOD g_packeted_bio_method; |
| 19 |
|
| 20 |
const uint8_t kOpcodePacket = 'P'; |
| 21 |
const uint8_t kOpcodeTimeout = 'T'; |
| 22 |
const uint8_t kOpcodeTimeoutAck = 't'; |
| 23 |
|
| 24 |
struct PacketedBio { |
| 25 |
explicit PacketedBio(timeval *clock_arg) |
| 26 |
: clock(clock_arg) { |
| 27 |
OPENSSL_memset(&timeout, 0, sizeof(timeout)); |
| 28 |
} |
| 29 |
|
| 30 |
bool HasTimeout() const { |
| 31 |
return timeout.tv_sec != 0 || timeout.tv_usec != 0; |
| 32 |
} |
| 33 |
|
| 34 |
timeval timeout; |
| 35 |
timeval *clock; |
| 36 |
}; |
| 37 |
|
| 38 |
PacketedBio *GetData(BIO *bio) { |
| 39 |
if (bio->method != &g_packeted_bio_method) { |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
return (PacketedBio *)bio->ptr; |
| 43 |
} |
| 44 |
|
| 45 |
// ReadAll reads |len| bytes from |bio| into |out|. It returns 1 on success and |
| 46 |
// 0 or -1 on error. |
| 47 |
static int ReadAll(BIO *bio, uint8_t *out, size_t len) { |
| 48 |
while (len > 0) { |
| 49 |
int chunk_len = INT_MAX; |
| 50 |
if (len <= INT_MAX) { |
| 51 |
chunk_len = (int)len; |
| 52 |
} |
| 53 |
int ret = BIO_read(bio, out, chunk_len); |
| 54 |
if (ret <= 0) { |
| 55 |
return ret; |
| 56 |
} |
| 57 |
out += ret; |
| 58 |
len -= ret; |
| 59 |
} |
| 60 |
return 1; |
| 61 |
} |
| 62 |
|
| 63 |
static int PacketedWrite(BIO *bio, const char *in, int inl) { |
| 64 |
if (bio->next_bio == NULL) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
BIO_clear_retry_flags(bio); |
| 69 |
|
| 70 |
// Write the header. |
| 71 |
uint8_t header[5]; |
| 72 |
header[0] = kOpcodePacket; |
| 73 |
header[1] = (inl >> 24) & 0xff; |
| 74 |
header[2] = (inl >> 16) & 0xff; |
| 75 |
header[3] = (inl >> 8) & 0xff; |
| 76 |
header[4] = inl & 0xff; |
| 77 |
int ret = BIO_write(bio->next_bio, header, sizeof(header)); |
| 78 |
if (ret <= 0) { |
| 79 |
BIO_copy_next_retry(bio); |
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
// Write the buffer. |
| 84 |
ret = BIO_write(bio->next_bio, in, inl); |
| 85 |
if (ret < 0 || (inl > 0 && ret == 0)) { |
| 86 |
BIO_copy_next_retry(bio); |
| 87 |
return ret; |
| 88 |
} |
| 89 |
assert(ret == inl); |
| 90 |
return ret; |
| 91 |
} |
| 92 |
|
| 93 |
static int PacketedRead(BIO *bio, char *out, int outl) { |
| 94 |
PacketedBio *data = GetData(bio); |
| 95 |
if (bio->next_bio == NULL) { |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
BIO_clear_retry_flags(bio); |
| 100 |
|
| 101 |
// Read the opcode. |
| 102 |
uint8_t opcode; |
| 103 |
int ret = ReadAll(bio->next_bio, &opcode, sizeof(opcode)); |
| 104 |
if (ret <= 0) { |
| 105 |
BIO_copy_next_retry(bio); |
| 106 |
return ret; |
| 107 |
} |
| 108 |
|
| 109 |
if (opcode == kOpcodeTimeout) { |
| 110 |
// The caller is required to advance any pending timeouts before continuing. |
| 111 |
if (data->HasTimeout()) { |
| 112 |
fprintf(stderr, "Unprocessed timeout!\n"); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
// Process the timeout. |
| 117 |
uint8_t buf[8]; |
| 118 |
ret = ReadAll(bio->next_bio, buf, sizeof(buf)); |
| 119 |
if (ret <= 0) { |
| 120 |
BIO_copy_next_retry(bio); |
| 121 |
return ret; |
| 122 |
} |
| 123 |
uint64_t timeout = (static_cast<uint64_t>(buf[0]) << 56) | |
| 124 |
(static_cast<uint64_t>(buf[1]) << 48) | |
| 125 |
(static_cast<uint64_t>(buf[2]) << 40) | |
| 126 |
(static_cast<uint64_t>(buf[3]) << 32) | |
| 127 |
(static_cast<uint64_t>(buf[4]) << 24) | |
| 128 |
(static_cast<uint64_t>(buf[5]) << 16) | |
| 129 |
(static_cast<uint64_t>(buf[6]) << 8) | |
| 130 |
static_cast<uint64_t>(buf[7]); |
| 131 |
timeout /= 1000; // Convert nanoseconds to microseconds. |
| 132 |
|
| 133 |
data->timeout.tv_usec = timeout % 1000000; |
| 134 |
data->timeout.tv_sec = timeout / 1000000; |
| 135 |
|
| 136 |
// Send an ACK to the peer. |
| 137 |
ret = BIO_write(bio->next_bio, &kOpcodeTimeoutAck, 1); |
| 138 |
if (ret <= 0) { |
| 139 |
return ret; |
| 140 |
} |
| 141 |
assert(ret == 1); |
| 142 |
|
| 143 |
// Signal to the caller to retry the read, after advancing the clock. |
| 144 |
BIO_set_retry_read(bio); |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
if (opcode != kOpcodePacket) { |
| 149 |
fprintf(stderr, "Unknown opcode, %u\n", opcode); |
| 150 |
return -1; |
| 151 |
} |
| 152 |
|
| 153 |
// Read the length prefix. |
| 154 |
uint8_t len_bytes[4]; |
| 155 |
ret = ReadAll(bio->next_bio, len_bytes, sizeof(len_bytes)); |
| 156 |
if (ret <= 0) { |
| 157 |
BIO_copy_next_retry(bio); |
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
uint32_t len = (len_bytes[0] << 24) | (len_bytes[1] << 16) | |
| 162 |
(len_bytes[2] << 8) | len_bytes[3]; |
| 163 |
uint8_t *buf = (uint8_t *)OPENSSL_malloc(len); |
| 164 |
if (buf == NULL) { |
| 165 |
return -1; |
| 166 |
} |
| 167 |
ret = ReadAll(bio->next_bio, buf, len); |
| 168 |
if (ret <= 0) { |
| 169 |
fprintf(stderr, "Packeted BIO was truncated\n"); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
|
| 173 |
if (outl > (int)len) { |
| 174 |
outl = len; |
| 175 |
} |
| 176 |
OPENSSL_memcpy(out, buf, outl); |
| 177 |
OPENSSL_free(buf); |
| 178 |
return outl; |
| 179 |
} |
| 180 |
|
| 181 |
static long PacketedCtrl(BIO *bio, int cmd, long num, void *ptr) { |
| 182 |
if (bio->next_bio == NULL) { |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
BIO_clear_retry_flags(bio); |
| 187 |
long ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 188 |
BIO_copy_next_retry(bio); |
| 189 |
return ret; |
| 190 |
} |
| 191 |
|
| 192 |
static int PacketedNew(BIO *bio) { |
| 193 |
bio->init = 1; |
| 194 |
return 1; |
| 195 |
} |
| 196 |
|
| 197 |
static int PacketedFree(BIO *bio) { |
| 198 |
if (bio == NULL) { |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
delete GetData(bio); |
| 203 |
bio->init = 0; |
| 204 |
return 1; |
| 205 |
} |
| 206 |
|
| 207 |
static long PacketedCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 208 |
if (bio->next_bio == NULL) { |
| 209 |
return 0; |
| 210 |
} |
| 211 |
return BIO_callback_ctrl(bio->next_bio, cmd, fp); |
| 212 |
} |
| 213 |
|
| 214 |
const BIO_METHOD g_packeted_bio_method = { |
| 215 |
BIO_TYPE_FILTER, |
| 216 |
"packeted bio", |
| 217 |
PacketedWrite, |
| 218 |
PacketedRead, |
| 219 |
NULL /* puts */, |
| 220 |
NULL /* gets */, |
| 221 |
PacketedCtrl, |
| 222 |
PacketedNew, |
| 223 |
PacketedFree, |
| 224 |
PacketedCallbackCtrl, |
| 225 |
}; |
| 226 |
|
| 227 |
} // namespace |
| 228 |
|
| 229 |
bssl::UniquePtr<BIO> PacketedBioCreate(timeval *clock) { |
| 230 |
bssl::UniquePtr<BIO> bio(BIO_new(&g_packeted_bio_method)); |
| 231 |
if (!bio) { |
| 232 |
return nullptr; |
| 233 |
} |
| 234 |
bio->ptr = new PacketedBio(clock); |
| 235 |
return bio; |
| 236 |
} |
| 237 |
|
| 238 |
bool PacketedBioAdvanceClock(BIO *bio) { |
| 239 |
PacketedBio *data = GetData(bio); |
| 240 |
if (data == nullptr) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
if (!data->HasTimeout()) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
data->clock->tv_usec += data->timeout.tv_usec; |
| 249 |
data->clock->tv_sec += data->clock->tv_usec / 1000000; |
| 250 |
data->clock->tv_usec %= 1000000; |
| 251 |
data->clock->tv_sec += data->timeout.tv_sec; |
| 252 |
OPENSSL_memset(&data->timeout, 0, sizeof(data->timeout)); |
| 253 |
return true; |
| 254 |
} |