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