packeted_bio.cc 6.4 KB

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