You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

326 lines
11 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 <algorithm>
  15. #include <string>
  16. #include <gtest/gtest.h>
  17. #include <openssl/bio.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/err.h>
  20. #include <openssl/mem.h>
  21. #include "../internal.h"
  22. #include "../test/test_util.h"
  23. #if !defined(OPENSSL_WINDOWS)
  24. #include <arpa/inet.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <netinet/in.h>
  28. #include <string.h>
  29. #include <sys/socket.h>
  30. #include <unistd.h>
  31. #else
  32. #include <io.h>
  33. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  34. #include <winsock2.h>
  35. #include <ws2tcpip.h>
  36. OPENSSL_MSVC_PRAGMA(warning(pop))
  37. #endif
  38. #if !defined(OPENSSL_WINDOWS)
  39. static int closesocket(int sock) { return close(sock); }
  40. static std::string LastSocketError() { return strerror(errno); }
  41. #else
  42. static std::string LastSocketError() {
  43. char buf[DECIMAL_SIZE(int) + 1];
  44. BIO_snprintf(buf, sizeof(buf), "%d", WSAGetLastError());
  45. return buf;
  46. }
  47. #endif
  48. class ScopedSocket {
  49. public:
  50. explicit ScopedSocket(int sock) : sock_(sock) {}
  51. ~ScopedSocket() {
  52. closesocket(sock_);
  53. }
  54. private:
  55. const int sock_;
  56. };
  57. TEST(BIOTest, SocketConnect) {
  58. static const char kTestMessage[] = "test";
  59. int listening_sock = -1;
  60. socklen_t len = 0;
  61. sockaddr_storage ss;
  62. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
  63. struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
  64. OPENSSL_memset(&ss, 0, sizeof(ss));
  65. ss.ss_family = AF_INET6;
  66. listening_sock = socket(AF_INET6, SOCK_STREAM, 0);
  67. ASSERT_NE(-1, listening_sock) << LastSocketError();
  68. len = sizeof(*sin6);
  69. ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &sin6->sin6_addr))
  70. << LastSocketError();
  71. if (bind(listening_sock, (struct sockaddr *)sin6, sizeof(*sin6)) == -1) {
  72. closesocket(listening_sock);
  73. ss.ss_family = AF_INET;
  74. listening_sock = socket(AF_INET, SOCK_STREAM, 0);
  75. ASSERT_NE(-1, listening_sock) << LastSocketError();
  76. len = sizeof(*sin);
  77. ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &sin->sin_addr))
  78. << LastSocketError();
  79. ASSERT_EQ(0, bind(listening_sock, (struct sockaddr *)sin, sizeof(*sin)))
  80. << LastSocketError();
  81. }
  82. ScopedSocket listening_sock_closer(listening_sock);
  83. ASSERT_EQ(0, listen(listening_sock, 1)) << LastSocketError();
  84. ASSERT_EQ(0, getsockname(listening_sock, (struct sockaddr *)&ss, &len))
  85. << LastSocketError();
  86. char hostname[80];
  87. if (ss.ss_family == AF_INET6) {
  88. BIO_snprintf(hostname, sizeof(hostname), "[::1]:%d",
  89. ntohs(sin6->sin6_port));
  90. } else if (ss.ss_family == AF_INET) {
  91. BIO_snprintf(hostname, sizeof(hostname), "127.0.0.1:%d",
  92. ntohs(sin->sin_port));
  93. }
  94. // Connect to it with a connect BIO.
  95. bssl::UniquePtr<BIO> bio(BIO_new_connect(hostname));
  96. ASSERT_TRUE(bio);
  97. // Write a test message to the BIO.
  98. ASSERT_EQ(static_cast<int>(sizeof(kTestMessage)),
  99. BIO_write(bio.get(), kTestMessage, sizeof(kTestMessage)));
  100. // Accept the socket.
  101. int sock = accept(listening_sock, (struct sockaddr *) &ss, &len);
  102. ASSERT_NE(-1, sock) << LastSocketError();
  103. ScopedSocket sock_closer(sock);
  104. // Check the same message is read back out.
  105. char buf[sizeof(kTestMessage)];
  106. ASSERT_EQ(static_cast<int>(sizeof(kTestMessage)),
  107. recv(sock, buf, sizeof(buf), 0))
  108. << LastSocketError();
  109. EXPECT_EQ(Bytes(kTestMessage, sizeof(kTestMessage)), Bytes(buf, sizeof(buf)));
  110. }
  111. TEST(BIOTest, Printf) {
  112. // Test a short output, a very long one, and various sizes around
  113. // 256 (the size of the buffer) to ensure edge cases are correct.
  114. static const size_t kLengths[] = {5, 250, 251, 252, 253, 254, 1023};
  115. bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
  116. ASSERT_TRUE(bio);
  117. for (size_t length : kLengths) {
  118. SCOPED_TRACE(length);
  119. std::string in(length, 'a');
  120. int ret = BIO_printf(bio.get(), "test %s", in.c_str());
  121. ASSERT_GE(ret, 0);
  122. EXPECT_EQ(5 + length, static_cast<size_t>(ret));
  123. const uint8_t *contents;
  124. size_t len;
  125. ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
  126. EXPECT_EQ("test " + in,
  127. std::string(reinterpret_cast<const char *>(contents), len));
  128. ASSERT_TRUE(BIO_reset(bio.get()));
  129. }
  130. }
  131. static const size_t kLargeASN1PayloadLen = 8000;
  132. struct ASN1TestParam {
  133. bool should_succeed;
  134. std::vector<uint8_t> input;
  135. // suffix_len is the number of zeros to append to |input|.
  136. size_t suffix_len;
  137. // expected_len, if |should_succeed| is true, is the expected length of the
  138. // ASN.1 element.
  139. size_t expected_len;
  140. size_t max_len;
  141. } kASN1TestParams[] = {
  142. {true, {0x30, 2, 1, 2, 0, 0}, 0, 4, 100},
  143. {false /* truncated */, {0x30, 3, 1, 2}, 0, 0, 100},
  144. {false /* should be short len */, {0x30, 0x81, 1, 1}, 0, 0, 100},
  145. {false /* zero padded */, {0x30, 0x82, 0, 1, 1}, 0, 0, 100},
  146. // Test a large payload.
  147. {true,
  148. {0x30, 0x82, kLargeASN1PayloadLen >> 8, kLargeASN1PayloadLen & 0xff},
  149. kLargeASN1PayloadLen,
  150. 4 + kLargeASN1PayloadLen,
  151. kLargeASN1PayloadLen * 2},
  152. {false /* max_len too short */,
  153. {0x30, 0x82, kLargeASN1PayloadLen >> 8, kLargeASN1PayloadLen & 0xff},
  154. kLargeASN1PayloadLen,
  155. 4 + kLargeASN1PayloadLen,
  156. 3 + kLargeASN1PayloadLen},
  157. // Test an indefinite-length input.
  158. {true,
  159. {0x30, 0x80},
  160. kLargeASN1PayloadLen + 2,
  161. 2 + kLargeASN1PayloadLen + 2,
  162. kLargeASN1PayloadLen * 2},
  163. {false /* max_len too short */,
  164. {0x30, 0x80},
  165. kLargeASN1PayloadLen + 2,
  166. 2 + kLargeASN1PayloadLen + 2,
  167. 2 + kLargeASN1PayloadLen + 1},
  168. };
  169. class BIOASN1Test : public testing::TestWithParam<ASN1TestParam> {};
  170. TEST_P(BIOASN1Test, ReadASN1) {
  171. const ASN1TestParam& param = GetParam();
  172. std::vector<uint8_t> input = param.input;
  173. input.resize(input.size() + param.suffix_len, 0);
  174. bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input.data(), input.size()));
  175. ASSERT_TRUE(bio);
  176. uint8_t *out;
  177. size_t out_len;
  178. int ok = BIO_read_asn1(bio.get(), &out, &out_len, param.max_len);
  179. if (!ok) {
  180. out = nullptr;
  181. }
  182. bssl::UniquePtr<uint8_t> out_storage(out);
  183. ASSERT_EQ(param.should_succeed, (ok == 1));
  184. if (param.should_succeed) {
  185. EXPECT_EQ(Bytes(input.data(), param.expected_len), Bytes(out, out_len));
  186. }
  187. }
  188. INSTANTIATE_TEST_CASE_P(, BIOASN1Test, testing::ValuesIn(kASN1TestParams));
  189. // Run through the tests twice, swapping |bio1| and |bio2|, for symmetry.
  190. class BIOPairTest : public testing::TestWithParam<bool> {};
  191. TEST_P(BIOPairTest, TestPair) {
  192. BIO *bio1, *bio2;
  193. ASSERT_TRUE(BIO_new_bio_pair(&bio1, 10, &bio2, 10));
  194. bssl::UniquePtr<BIO> free_bio1(bio1), free_bio2(bio2);
  195. if (GetParam()) {
  196. std::swap(bio1, bio2);
  197. }
  198. // Check initial states.
  199. EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
  200. EXPECT_EQ(0u, BIO_ctrl_get_read_request(bio1));
  201. // Data written in one end may be read out the other.
  202. uint8_t buf[20];
  203. EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
  204. EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1));
  205. ASSERT_EQ(5, BIO_read(bio2, buf, sizeof(buf)));
  206. EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
  207. EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
  208. // Attempting to write more than 10 bytes will write partially.
  209. EXPECT_EQ(10, BIO_write(bio1, "1234567890___", 13));
  210. EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
  211. EXPECT_EQ(-1, BIO_write(bio1, "z", 1));
  212. EXPECT_TRUE(BIO_should_write(bio1));
  213. ASSERT_EQ(10, BIO_read(bio2, buf, sizeof(buf)));
  214. EXPECT_EQ(Bytes("1234567890"), Bytes(buf, 10));
  215. EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
  216. // Unsuccessful reads update the read request.
  217. EXPECT_EQ(-1, BIO_read(bio2, buf, 5));
  218. EXPECT_TRUE(BIO_should_read(bio2));
  219. EXPECT_EQ(5u, BIO_ctrl_get_read_request(bio1));
  220. // The read request is clamped to the size of the buffer.
  221. EXPECT_EQ(-1, BIO_read(bio2, buf, 20));
  222. EXPECT_TRUE(BIO_should_read(bio2));
  223. EXPECT_EQ(10u, BIO_ctrl_get_read_request(bio1));
  224. // Data may be written and read in chunks.
  225. EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
  226. EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1));
  227. EXPECT_EQ(5, BIO_write(bio1, "67890___", 8));
  228. EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
  229. ASSERT_EQ(3, BIO_read(bio2, buf, 3));
  230. EXPECT_EQ(Bytes("123"), Bytes(buf, 3));
  231. EXPECT_EQ(3u, BIO_ctrl_get_write_guarantee(bio1));
  232. ASSERT_EQ(7, BIO_read(bio2, buf, sizeof(buf)));
  233. EXPECT_EQ(Bytes("4567890"), Bytes(buf, 7));
  234. EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
  235. // Successful reads reset the read request.
  236. EXPECT_EQ(0u, BIO_ctrl_get_read_request(bio1));
  237. // Test writes and reads starting in the middle of the ring buffer and
  238. // wrapping to front.
  239. EXPECT_EQ(8, BIO_write(bio1, "abcdefgh", 8));
  240. EXPECT_EQ(2u, BIO_ctrl_get_write_guarantee(bio1));
  241. ASSERT_EQ(3, BIO_read(bio2, buf, 3));
  242. EXPECT_EQ(Bytes("abc"), Bytes(buf, 3));
  243. EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1));
  244. EXPECT_EQ(5, BIO_write(bio1, "ijklm___", 8));
  245. EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
  246. ASSERT_EQ(10, BIO_read(bio2, buf, sizeof(buf)));
  247. EXPECT_EQ(Bytes("defghijklm"), Bytes(buf, 10));
  248. EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
  249. // Data may flow from both ends in parallel.
  250. EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
  251. EXPECT_EQ(5, BIO_write(bio2, "67890", 5));
  252. ASSERT_EQ(5, BIO_read(bio2, buf, sizeof(buf)));
  253. EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
  254. ASSERT_EQ(5, BIO_read(bio1, buf, sizeof(buf)));
  255. EXPECT_EQ(Bytes("67890"), Bytes(buf, 5));
  256. // Closing the write end causes an EOF on the read half, after draining.
  257. EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
  258. EXPECT_TRUE(BIO_shutdown_wr(bio1));
  259. ASSERT_EQ(5, BIO_read(bio2, buf, sizeof(buf)));
  260. EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
  261. EXPECT_EQ(0, BIO_read(bio2, buf, sizeof(buf)));
  262. // A closed write end may not be written to.
  263. EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
  264. EXPECT_EQ(-1, BIO_write(bio1, "_____", 5));
  265. uint32_t err = ERR_get_error();
  266. EXPECT_EQ(ERR_LIB_BIO, ERR_GET_LIB(err));
  267. EXPECT_EQ(BIO_R_BROKEN_PIPE, ERR_GET_REASON(err));
  268. // The other end is still functional.
  269. EXPECT_EQ(5, BIO_write(bio2, "12345", 5));
  270. ASSERT_EQ(5, BIO_read(bio1, buf, sizeof(buf)));
  271. EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
  272. }
  273. INSTANTIATE_TEST_CASE_P(, BIOPairTest, testing::Values(false, true));