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.

830 lines
25 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. * Portions Copyright 2011, RTFM, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  25. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <algorithm>
  29. #include <set>
  30. #include <string>
  31. #include "talk/base/gunit.h"
  32. #include "talk/base/helpers.h"
  33. #include "talk/base/ssladapter.h"
  34. #include "talk/base/sslconfig.h"
  35. #include "talk/base/sslidentity.h"
  36. #include "talk/base/sslstreamadapter.h"
  37. #include "talk/base/stream.h"
  38. static const int kBlockSize = 4096;
  39. static const char kAES_CM_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80";
  40. static const char kAES_CM_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32";
  41. static const char kExporterLabel[] = "label";
  42. static const unsigned char kExporterContext[] = "context";
  43. static int kExporterContextLen = sizeof(kExporterContext);
  44. #define MAYBE_SKIP_TEST(feature) \
  45. if (!(talk_base::SSLStreamAdapter::feature())) { \
  46. LOG(LS_INFO) << "Feature disabled... skipping"; \
  47. return; \
  48. }
  49. class SSLStreamAdapterTestBase;
  50. class SSLDummyStream : public talk_base::StreamInterface,
  51. public sigslot::has_slots<> {
  52. public:
  53. explicit SSLDummyStream(SSLStreamAdapterTestBase *test,
  54. const std::string &side,
  55. talk_base::FifoBuffer *in,
  56. talk_base::FifoBuffer *out) :
  57. test_(test),
  58. side_(side),
  59. in_(in),
  60. out_(out),
  61. first_packet_(true) {
  62. in_->SignalEvent.connect(this, &SSLDummyStream::OnEventIn);
  63. out_->SignalEvent.connect(this, &SSLDummyStream::OnEventOut);
  64. }
  65. virtual talk_base::StreamState GetState() const { return talk_base::SS_OPEN; }
  66. virtual talk_base::StreamResult Read(void* buffer, size_t buffer_len,
  67. size_t* read, int* error) {
  68. talk_base::StreamResult r;
  69. r = in_->Read(buffer, buffer_len, read, error);
  70. if (r == talk_base::SR_BLOCK)
  71. return talk_base::SR_BLOCK;
  72. if (r == talk_base::SR_EOS)
  73. return talk_base::SR_EOS;
  74. if (r != talk_base::SR_SUCCESS) {
  75. ADD_FAILURE();
  76. return talk_base::SR_ERROR;
  77. }
  78. return talk_base::SR_SUCCESS;
  79. }
  80. // Catch readability events on in and pass them up.
  81. virtual void OnEventIn(talk_base::StreamInterface *stream, int sig,
  82. int err) {
  83. int mask = (talk_base::SE_READ | talk_base::SE_CLOSE);
  84. if (sig & mask) {
  85. LOG(LS_INFO) << "SSLDummyStream::OnEvent side=" << side_ << " sig="
  86. << sig << " forwarding upward";
  87. PostEvent(sig & mask, 0);
  88. }
  89. }
  90. // Catch writeability events on out and pass them up.
  91. virtual void OnEventOut(talk_base::StreamInterface *stream, int sig,
  92. int err) {
  93. if (sig & talk_base::SE_WRITE) {
  94. LOG(LS_INFO) << "SSLDummyStream::OnEvent side=" << side_ << " sig="
  95. << sig << " forwarding upward";
  96. PostEvent(sig & talk_base::SE_WRITE, 0);
  97. }
  98. }
  99. // Write to the outgoing FifoBuffer
  100. talk_base::StreamResult WriteData(const void* data, size_t data_len,
  101. size_t* written, int* error) {
  102. return out_->Write(data, data_len, written, error);
  103. }
  104. // Defined later
  105. virtual talk_base::StreamResult Write(const void* data, size_t data_len,
  106. size_t* written, int* error);
  107. virtual void Close() {
  108. LOG(LS_INFO) << "Closing outbound stream";
  109. out_->Close();
  110. }
  111. private:
  112. SSLStreamAdapterTestBase *test_;
  113. const std::string side_;
  114. talk_base::FifoBuffer *in_;
  115. talk_base::FifoBuffer *out_;
  116. bool first_packet_;
  117. };
  118. class SSLStreamAdapterTestBase : public testing::Test,
  119. public sigslot::has_slots<> {
  120. public:
  121. explicit SSLStreamAdapterTestBase(bool dtls) :
  122. client_buffer_(4096), server_buffer_(4096),
  123. client_stream_(
  124. new SSLDummyStream(this, "c2s", &client_buffer_, &server_buffer_)),
  125. server_stream_(
  126. new SSLDummyStream(this, "s2c", &server_buffer_, &client_buffer_)),
  127. client_ssl_(talk_base::SSLStreamAdapter::Create(client_stream_)),
  128. server_ssl_(talk_base::SSLStreamAdapter::Create(server_stream_)),
  129. client_identity_(NULL), server_identity_(NULL),
  130. delay_(0), mtu_(1460), loss_(0), lose_first_packet_(false),
  131. damage_(false), dtls_(dtls),
  132. handshake_wait_(5000), identities_set_(false) {
  133. // Set use of the test RNG to get predictable loss patterns.
  134. talk_base::SetRandomTestMode(true);
  135. // Set up the slots
  136. client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
  137. server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
  138. client_identity_ = talk_base::SSLIdentity::Generate("client");
  139. server_identity_ = talk_base::SSLIdentity::Generate("server");
  140. client_ssl_->SetIdentity(client_identity_);
  141. server_ssl_->SetIdentity(server_identity_);
  142. }
  143. ~SSLStreamAdapterTestBase() {
  144. // Put it back for the next test.
  145. talk_base::SetRandomTestMode(false);
  146. }
  147. static void SetUpTestCase() {
  148. talk_base::InitializeSSL();
  149. }
  150. virtual void OnEvent(talk_base::StreamInterface *stream, int sig, int err) {
  151. LOG(LS_INFO) << "SSLStreamAdapterTestBase::OnEvent sig=" << sig;
  152. if (sig & talk_base::SE_READ) {
  153. ReadData(stream);
  154. }
  155. if ((stream == client_ssl_.get()) && (sig & talk_base::SE_WRITE)) {
  156. WriteData();
  157. }
  158. }
  159. void SetPeerIdentitiesByCertificate(bool correct) {
  160. LOG(LS_INFO) << "Setting peer identities by certificate";
  161. if (correct) {
  162. client_ssl_->SetPeerCertificate(server_identity_->certificate().
  163. GetReference());
  164. server_ssl_->SetPeerCertificate(client_identity_->certificate().
  165. GetReference());
  166. } else {
  167. // If incorrect, set up to expect our own certificate at the peer
  168. client_ssl_->SetPeerCertificate(client_identity_->certificate().
  169. GetReference());
  170. server_ssl_->SetPeerCertificate(server_identity_->certificate().
  171. GetReference());
  172. }
  173. identities_set_ = true;
  174. }
  175. void SetPeerIdentitiesByDigest(bool correct) {
  176. unsigned char digest[20];
  177. size_t digest_len;
  178. bool rv;
  179. LOG(LS_INFO) << "Setting peer identities by digest";
  180. rv = server_identity_->certificate().ComputeDigest(talk_base::DIGEST_SHA_1,
  181. digest, 20,
  182. &digest_len);
  183. ASSERT_TRUE(rv);
  184. if (!correct) {
  185. LOG(LS_INFO) << "Setting bogus digest for server cert";
  186. digest[0]++;
  187. }
  188. rv = client_ssl_->SetPeerCertificateDigest(talk_base::DIGEST_SHA_1, digest,
  189. digest_len);
  190. ASSERT_TRUE(rv);
  191. rv = client_identity_->certificate().ComputeDigest(talk_base::DIGEST_SHA_1,
  192. digest, 20, &digest_len);
  193. ASSERT_TRUE(rv);
  194. if (!correct) {
  195. LOG(LS_INFO) << "Setting bogus digest for client cert";
  196. digest[0]++;
  197. }
  198. rv = server_ssl_->SetPeerCertificateDigest(talk_base::DIGEST_SHA_1, digest,
  199. digest_len);
  200. ASSERT_TRUE(rv);
  201. identities_set_ = true;
  202. }
  203. void TestHandshake(bool expect_success = true) {
  204. server_ssl_->SetMode(dtls_ ? talk_base::SSL_MODE_DTLS :
  205. talk_base::SSL_MODE_TLS);
  206. client_ssl_->SetMode(dtls_ ? talk_base::SSL_MODE_DTLS :
  207. talk_base::SSL_MODE_TLS);
  208. if (!dtls_) {
  209. // Make sure we simulate a reliable network for TLS.
  210. // This is just a check to make sure that people don't write wrong
  211. // tests.
  212. ASSERT((mtu_ == 1460) && (loss_ == 0) && (lose_first_packet_ == 0));
  213. }
  214. if (!identities_set_)
  215. SetPeerIdentitiesByDigest(true);
  216. // Start the handshake
  217. int rv;
  218. server_ssl_->SetServerRole();
  219. rv = server_ssl_->StartSSLWithPeer();
  220. ASSERT_EQ(0, rv);
  221. rv = client_ssl_->StartSSLWithPeer();
  222. ASSERT_EQ(0, rv);
  223. // Now run the handshake
  224. if (expect_success) {
  225. EXPECT_TRUE_WAIT((client_ssl_->GetState() == talk_base::SS_OPEN)
  226. && (server_ssl_->GetState() == talk_base::SS_OPEN),
  227. handshake_wait_);
  228. } else {
  229. EXPECT_TRUE_WAIT(client_ssl_->GetState() == talk_base::SS_CLOSED,
  230. handshake_wait_);
  231. }
  232. }
  233. talk_base::StreamResult DataWritten(SSLDummyStream *from, const void *data,
  234. size_t data_len, size_t *written,
  235. int *error) {
  236. // Randomly drop loss_ percent of packets
  237. if (talk_base::CreateRandomId() % 100 < static_cast<uint32>(loss_)) {
  238. LOG(LS_INFO) << "Randomly dropping packet, size=" << data_len;
  239. *written = data_len;
  240. return talk_base::SR_SUCCESS;
  241. }
  242. if (dtls_ && (data_len > mtu_)) {
  243. LOG(LS_INFO) << "Dropping packet > mtu, size=" << data_len;
  244. *written = data_len;
  245. return talk_base::SR_SUCCESS;
  246. }
  247. // Optionally damage application data (type 23). Note that we don't damage
  248. // handshake packets and we damage the last byte to keep the header
  249. // intact but break the MAC.
  250. if (damage_ && (*static_cast<const unsigned char *>(data) == 23)) {
  251. std::vector<char> buf(data_len);
  252. LOG(LS_INFO) << "Damaging packet";
  253. memcpy(&buf[0], data, data_len);
  254. buf[data_len - 1]++;
  255. return from->WriteData(&buf[0], data_len, written, error);
  256. }
  257. return from->WriteData(data, data_len, written, error);
  258. }
  259. void SetDelay(int delay) {
  260. delay_ = delay;
  261. }
  262. int GetDelay() { return delay_; }
  263. void SetLoseFirstPacket(bool lose) {
  264. lose_first_packet_ = lose;
  265. }
  266. bool GetLoseFirstPacket() { return lose_first_packet_; }
  267. void SetLoss(int percent) {
  268. loss_ = percent;
  269. }
  270. void SetDamage() {
  271. damage_ = true;
  272. }
  273. void SetMtu(size_t mtu) {
  274. mtu_ = mtu;
  275. }
  276. void SetHandshakeWait(int wait) {
  277. handshake_wait_ = wait;
  278. }
  279. void SetDtlsSrtpCiphers(const std::vector<std::string> &ciphers,
  280. bool client) {
  281. if (client)
  282. client_ssl_->SetDtlsSrtpCiphers(ciphers);
  283. else
  284. server_ssl_->SetDtlsSrtpCiphers(ciphers);
  285. }
  286. bool GetDtlsSrtpCipher(bool client, std::string *retval) {
  287. if (client)
  288. return client_ssl_->GetDtlsSrtpCipher(retval);
  289. else
  290. return server_ssl_->GetDtlsSrtpCipher(retval);
  291. }
  292. bool ExportKeyingMaterial(const char *label,
  293. const unsigned char *context,
  294. size_t context_len,
  295. bool use_context,
  296. bool client,
  297. unsigned char *result,
  298. size_t result_len) {
  299. if (client)
  300. return client_ssl_->ExportKeyingMaterial(label,
  301. context, context_len,
  302. use_context,
  303. result, result_len);
  304. else
  305. return server_ssl_->ExportKeyingMaterial(label,
  306. context, context_len,
  307. use_context,
  308. result, result_len);
  309. }
  310. // To be implemented by subclasses.
  311. virtual void WriteData() = 0;
  312. virtual void ReadData(talk_base::StreamInterface *stream) = 0;
  313. virtual void TestTransfer(int size) = 0;
  314. protected:
  315. talk_base::FifoBuffer client_buffer_;
  316. talk_base::FifoBuffer server_buffer_;
  317. SSLDummyStream *client_stream_; // freed by client_ssl_ destructor
  318. SSLDummyStream *server_stream_; // freed by server_ssl_ destructor
  319. talk_base::scoped_ptr<talk_base::SSLStreamAdapter> client_ssl_;
  320. talk_base::scoped_ptr<talk_base::SSLStreamAdapter> server_ssl_;
  321. talk_base::SSLIdentity *client_identity_; // freed by client_ssl_ destructor
  322. talk_base::SSLIdentity *server_identity_; // freed by server_ssl_ destructor
  323. int delay_;
  324. size_t mtu_;
  325. int loss_;
  326. bool lose_first_packet_;
  327. bool damage_;
  328. bool dtls_;
  329. int handshake_wait_;
  330. bool identities_set_;
  331. };
  332. class SSLStreamAdapterTestTLS : public SSLStreamAdapterTestBase {
  333. public:
  334. SSLStreamAdapterTestTLS() :
  335. SSLStreamAdapterTestBase(false) {
  336. };
  337. // Test data transfer for TLS
  338. virtual void TestTransfer(int size) {
  339. LOG(LS_INFO) << "Starting transfer test with " << size << " bytes";
  340. // Create some dummy data to send.
  341. size_t received;
  342. send_stream_.ReserveSize(size);
  343. for (int i = 0; i < size; ++i) {
  344. char ch = static_cast<char>(i);
  345. send_stream_.Write(&ch, 1, NULL, NULL);
  346. }
  347. send_stream_.Rewind();
  348. // Prepare the receive stream.
  349. recv_stream_.ReserveSize(size);
  350. // Start sending
  351. WriteData();
  352. // Wait for the client to close
  353. EXPECT_TRUE_WAIT(server_ssl_->GetState() == talk_base::SS_CLOSED, 10000);
  354. // Now check the data
  355. recv_stream_.GetSize(&received);
  356. EXPECT_EQ(static_cast<size_t>(size), received);
  357. EXPECT_EQ(0, memcmp(send_stream_.GetBuffer(),
  358. recv_stream_.GetBuffer(), size));
  359. }
  360. void WriteData() {
  361. size_t position, tosend, size;
  362. talk_base::StreamResult rv;
  363. size_t sent;
  364. char block[kBlockSize];
  365. send_stream_.GetSize(&size);
  366. if (!size)
  367. return;
  368. for (;;) {
  369. send_stream_.GetPosition(&position);
  370. if (send_stream_.Read(block, sizeof(block), &tosend, NULL) !=
  371. talk_base::SR_EOS) {
  372. rv = client_ssl_->Write(block, tosend, &sent, 0);
  373. if (rv == talk_base::SR_SUCCESS) {
  374. send_stream_.SetPosition(position + sent);
  375. LOG(LS_VERBOSE) << "Sent: " << position + sent;
  376. } else if (rv == talk_base::SR_BLOCK) {
  377. LOG(LS_VERBOSE) << "Blocked...";
  378. send_stream_.SetPosition(position);
  379. break;
  380. } else {
  381. ADD_FAILURE();
  382. break;
  383. }
  384. } else {
  385. // Now close
  386. LOG(LS_INFO) << "Wrote " << position << " bytes. Closing";
  387. client_ssl_->Close();
  388. break;
  389. }
  390. }
  391. };
  392. virtual void ReadData(talk_base::StreamInterface *stream) {
  393. char buffer[1600];
  394. size_t bread;
  395. int err2;
  396. talk_base::StreamResult r;
  397. for (;;) {
  398. r = stream->Read(buffer, sizeof(buffer), &bread, &err2);
  399. if (r == talk_base::SR_ERROR || r == talk_base::SR_EOS) {
  400. // Unfortunately, errors are the way that the stream adapter
  401. // signals close in OpenSSL
  402. stream->Close();
  403. return;
  404. }
  405. if (r == talk_base::SR_BLOCK)
  406. break;
  407. ASSERT_EQ(talk_base::SR_SUCCESS, r);
  408. LOG(LS_INFO) << "Read " << bread;
  409. recv_stream_.Write(buffer, bread, NULL, NULL);
  410. }
  411. }
  412. private:
  413. talk_base::MemoryStream send_stream_;
  414. talk_base::MemoryStream recv_stream_;
  415. };
  416. class SSLStreamAdapterTestDTLS : public SSLStreamAdapterTestBase {
  417. public:
  418. SSLStreamAdapterTestDTLS() :
  419. SSLStreamAdapterTestBase(true),
  420. packet_size_(1000), count_(0), sent_(0) {
  421. }
  422. virtual void WriteData() {
  423. unsigned char *packet = new unsigned char[1600];
  424. do {
  425. memset(packet, sent_ & 0xff, packet_size_);
  426. *(reinterpret_cast<uint32_t *>(packet)) = sent_;
  427. size_t sent;
  428. int rv = client_ssl_->Write(packet, packet_size_, &sent, 0);
  429. if (rv == talk_base::SR_SUCCESS) {
  430. LOG(LS_VERBOSE) << "Sent: " << sent_;
  431. sent_++;
  432. } else if (rv == talk_base::SR_BLOCK) {
  433. LOG(LS_VERBOSE) << "Blocked...";
  434. break;
  435. } else {
  436. ADD_FAILURE();
  437. break;
  438. }
  439. } while (sent_ < count_);
  440. delete [] packet;
  441. }
  442. virtual void ReadData(talk_base::StreamInterface *stream) {
  443. unsigned char *buffer = new unsigned char[2000];
  444. size_t bread;
  445. int err2;
  446. talk_base::StreamResult r;
  447. for (;;) {
  448. r = stream->Read(buffer, 2000,
  449. &bread, &err2);
  450. if (r == talk_base::SR_ERROR) {
  451. // Unfortunately, errors are the way that the stream adapter
  452. // signals close right now
  453. stream->Close();
  454. return;
  455. }
  456. if (r == talk_base::SR_BLOCK)
  457. break;
  458. ASSERT_EQ(talk_base::SR_SUCCESS, r);
  459. LOG(LS_INFO) << "Read " << bread;
  460. // Now parse the datagram
  461. ASSERT_EQ(packet_size_, bread);
  462. uint32_t packet_num = *(reinterpret_cast<uint32_t *>(buffer));
  463. for (size_t i = 4; i < packet_size_; i++) {
  464. ASSERT_EQ((packet_num & 0xff), buffer[i]);
  465. }
  466. received_.insert(packet_num);
  467. }
  468. }
  469. virtual void TestTransfer(int count) {
  470. count_ = count;
  471. WriteData();
  472. EXPECT_TRUE_WAIT(sent_ == count_, 10000);
  473. LOG(LS_INFO) << "sent_ == " << sent_;
  474. if (damage_) {
  475. WAIT(false, 2000);
  476. EXPECT_EQ(0U, received_.size());
  477. } else if (loss_ == 0) {
  478. EXPECT_EQ_WAIT(static_cast<size_t>(sent_), received_.size(), 1000);
  479. } else {
  480. LOG(LS_INFO) << "Sent " << sent_ << " packets; received " <<
  481. received_.size();
  482. }
  483. };
  484. private:
  485. size_t packet_size_;
  486. int count_;
  487. int sent_;
  488. std::set<int> received_;
  489. };
  490. talk_base::StreamResult SSLDummyStream::Write(const void* data, size_t data_len,
  491. size_t* written, int* error) {
  492. *written = data_len;
  493. LOG(LS_INFO) << "Writing to loopback " << data_len;
  494. if (first_packet_) {
  495. first_packet_ = false;
  496. if (test_->GetLoseFirstPacket()) {
  497. LOG(LS_INFO) << "Losing initial packet of length " << data_len;
  498. return talk_base::SR_SUCCESS;
  499. }
  500. }
  501. return test_->DataWritten(this, data, data_len, written, error);
  502. return talk_base::SR_SUCCESS;
  503. };
  504. // Basic tests: TLS
  505. // Test that we cannot read/write if we have not yet handshaked.
  506. // This test only applies to NSS because OpenSSL has passthrough
  507. // semantics for I/O before the handshake is started.
  508. #if SSL_USE_NSS
  509. TEST_F(SSLStreamAdapterTestTLS, TestNoReadWriteBeforeConnect) {
  510. talk_base::StreamResult rv;
  511. char block[kBlockSize];
  512. size_t dummy;
  513. rv = client_ssl_->Write(block, sizeof(block), &dummy, NULL);
  514. ASSERT_EQ(talk_base::SR_BLOCK, rv);
  515. rv = client_ssl_->Read(block, sizeof(block), &dummy, NULL);
  516. ASSERT_EQ(talk_base::SR_BLOCK, rv);
  517. }
  518. #endif
  519. // Test that we can make a handshake work
  520. TEST_F(SSLStreamAdapterTestTLS, TestTLSConnect) {
  521. TestHandshake();
  522. };
  523. // Test transfer -- trivial
  524. TEST_F(SSLStreamAdapterTestTLS, TestTLSTransfer) {
  525. TestHandshake();
  526. TestTransfer(100000);
  527. };
  528. // Test read-write after close.
  529. TEST_F(SSLStreamAdapterTestTLS, ReadWriteAfterClose) {
  530. TestHandshake();
  531. TestTransfer(100000);
  532. client_ssl_->Close();
  533. talk_base::StreamResult rv;
  534. char block[kBlockSize];
  535. size_t dummy;
  536. // It's an error to write after closed.
  537. rv = client_ssl_->Write(block, sizeof(block), &dummy, NULL);
  538. ASSERT_EQ(talk_base::SR_ERROR, rv);
  539. // But after closed read gives you EOS.
  540. rv = client_ssl_->Read(block, sizeof(block), &dummy, NULL);
  541. ASSERT_EQ(talk_base::SR_EOS, rv);
  542. };
  543. // Test a handshake with a bogus peer digest
  544. TEST_F(SSLStreamAdapterTestTLS, TestTLSBogusDigest) {
  545. SetPeerIdentitiesByDigest(false);
  546. TestHandshake(false);
  547. };
  548. // Test a handshake with a peer certificate
  549. TEST_F(SSLStreamAdapterTestTLS, TestTLSPeerCertificate) {
  550. SetPeerIdentitiesByCertificate(true);
  551. TestHandshake();
  552. };
  553. // Test a handshake with a bogus peer certificate
  554. TEST_F(SSLStreamAdapterTestTLS, TestTLSBogusPeerCertificate) {
  555. SetPeerIdentitiesByCertificate(false);
  556. TestHandshake(false);
  557. };
  558. // Test moving a bunch of data
  559. // Basic tests: DTLS
  560. // Test that we can make a handshake work
  561. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSConnect) {
  562. MAYBE_SKIP_TEST(HaveDtls);
  563. TestHandshake();
  564. };
  565. // Test that we can make a handshake work if the first packet in
  566. // each direction is lost. This gives us predictable loss
  567. // rather than having to tune random
  568. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacket) {
  569. MAYBE_SKIP_TEST(HaveDtls);
  570. SetLoseFirstPacket(true);
  571. TestHandshake();
  572. };
  573. // Test a handshake with loss and delay
  574. TEST_F(SSLStreamAdapterTestDTLS,
  575. TestDTLSConnectWithLostFirstPacketDelay2s) {
  576. MAYBE_SKIP_TEST(HaveDtls);
  577. SetLoseFirstPacket(true);
  578. SetDelay(2000);
  579. SetHandshakeWait(20000);
  580. TestHandshake();
  581. };
  582. // Test a handshake with small MTU
  583. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSConnectWithSmallMtu) {
  584. MAYBE_SKIP_TEST(HaveDtls);
  585. SetMtu(700);
  586. SetHandshakeWait(20000);
  587. TestHandshake();
  588. };
  589. // Test transfer -- trivial
  590. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSTransfer) {
  591. MAYBE_SKIP_TEST(HaveDtls);
  592. TestHandshake();
  593. TestTransfer(100);
  594. };
  595. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSTransferWithLoss) {
  596. MAYBE_SKIP_TEST(HaveDtls);
  597. TestHandshake();
  598. SetLoss(10);
  599. TestTransfer(100);
  600. };
  601. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSTransferWithDamage) {
  602. MAYBE_SKIP_TEST(HaveDtls);
  603. SetDamage(); // Must be called first because first packet
  604. // write happens at end of handshake.
  605. TestHandshake();
  606. TestTransfer(100);
  607. };
  608. // Test DTLS-SRTP with all high ciphers
  609. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpHigh) {
  610. MAYBE_SKIP_TEST(HaveDtlsSrtp);
  611. std::vector<std::string> high;
  612. high.push_back(kAES_CM_HMAC_SHA1_80);
  613. SetDtlsSrtpCiphers(high, true);
  614. SetDtlsSrtpCiphers(high, false);
  615. TestHandshake();
  616. std::string client_cipher;
  617. ASSERT_TRUE(GetDtlsSrtpCipher(true, &client_cipher));
  618. std::string server_cipher;
  619. ASSERT_TRUE(GetDtlsSrtpCipher(false, &server_cipher));
  620. ASSERT_EQ(client_cipher, server_cipher);
  621. ASSERT_EQ(client_cipher, kAES_CM_HMAC_SHA1_80);
  622. };
  623. // Test DTLS-SRTP with all low ciphers
  624. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpLow) {
  625. MAYBE_SKIP_TEST(HaveDtlsSrtp);
  626. std::vector<std::string> low;
  627. low.push_back(kAES_CM_HMAC_SHA1_32);
  628. SetDtlsSrtpCiphers(low, true);
  629. SetDtlsSrtpCiphers(low, false);
  630. TestHandshake();
  631. std::string client_cipher;
  632. ASSERT_TRUE(GetDtlsSrtpCipher(true, &client_cipher));
  633. std::string server_cipher;
  634. ASSERT_TRUE(GetDtlsSrtpCipher(false, &server_cipher));
  635. ASSERT_EQ(client_cipher, server_cipher);
  636. ASSERT_EQ(client_cipher, kAES_CM_HMAC_SHA1_32);
  637. };
  638. // Test DTLS-SRTP with a mismatch -- should not converge
  639. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpHighLow) {
  640. MAYBE_SKIP_TEST(HaveDtlsSrtp);
  641. std::vector<std::string> high;
  642. high.push_back(kAES_CM_HMAC_SHA1_80);
  643. std::vector<std::string> low;
  644. low.push_back(kAES_CM_HMAC_SHA1_32);
  645. SetDtlsSrtpCiphers(high, true);
  646. SetDtlsSrtpCiphers(low, false);
  647. TestHandshake();
  648. std::string client_cipher;
  649. ASSERT_FALSE(GetDtlsSrtpCipher(true, &client_cipher));
  650. std::string server_cipher;
  651. ASSERT_FALSE(GetDtlsSrtpCipher(false, &server_cipher));
  652. };
  653. // Test DTLS-SRTP with each side being mixed -- should select high
  654. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpMixed) {
  655. MAYBE_SKIP_TEST(HaveDtlsSrtp);
  656. std::vector<std::string> mixed;
  657. mixed.push_back(kAES_CM_HMAC_SHA1_80);
  658. mixed.push_back(kAES_CM_HMAC_SHA1_32);
  659. SetDtlsSrtpCiphers(mixed, true);
  660. SetDtlsSrtpCiphers(mixed, false);
  661. TestHandshake();
  662. std::string client_cipher;
  663. ASSERT_TRUE(GetDtlsSrtpCipher(true, &client_cipher));
  664. std::string server_cipher;
  665. ASSERT_TRUE(GetDtlsSrtpCipher(false, &server_cipher));
  666. ASSERT_EQ(client_cipher, server_cipher);
  667. ASSERT_EQ(client_cipher, kAES_CM_HMAC_SHA1_80);
  668. };
  669. // Test an exporter
  670. TEST_F(SSLStreamAdapterTestDTLS, TestDTLSExporter) {
  671. MAYBE_SKIP_TEST(HaveExporter);
  672. TestHandshake();
  673. unsigned char client_out[20];
  674. unsigned char server_out[20];
  675. bool result;
  676. result = ExportKeyingMaterial(kExporterLabel,
  677. kExporterContext, kExporterContextLen,
  678. true, true,
  679. client_out, sizeof(client_out));
  680. ASSERT_TRUE(result);
  681. result = ExportKeyingMaterial(kExporterLabel,
  682. kExporterContext, kExporterContextLen,
  683. true, false,
  684. server_out, sizeof(server_out));
  685. ASSERT_TRUE(result);
  686. ASSERT_TRUE(!memcmp(client_out, server_out, sizeof(client_out)));
  687. }