Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

298 rader
7.2 KiB

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