henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 11 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 12 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <set> |
| 14 | #include <string> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/bufferqueue.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/gunit.h" |
| 19 | #include "rtc_base/helpers.h" |
| 20 | #include "rtc_base/ssladapter.h" |
| 21 | #include "rtc_base/sslidentity.h" |
| 22 | #include "rtc_base/sslstreamadapter.h" |
| 23 | #include "rtc_base/stream.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 25 | using ::testing::WithParamInterface; |
| 26 | using ::testing::Values; |
| 27 | using ::testing::Combine; |
| 28 | using ::testing::tuple; |
| 29 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | static const int kBlockSize = 4096; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | static const char kExporterLabel[] = "label"; |
| 32 | static const unsigned char kExporterContext[] = "context"; |
| 33 | static int kExporterContextLen = sizeof(kExporterContext); |
| 34 | |
| 35 | static const char kRSA_PRIVATE_KEY_PEM[] = |
| 36 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 37 | "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/rM\n" |
| 38 | "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" |
| 39 | "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" |
| 40 | "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAECgYAvgOs4FJcgvp+TuREx7YtiYVsH\n" |
| 41 | "mwQPTum2z/8VzWGwR8BBHBvIpVe1MbD/Y4seyI2aco/7UaisatSgJhsU46/9Y4fq\n" |
| 42 | "2TwXH9QANf4at4d9n/R6rzwpAJOpgwZgKvdQjkfrKTtgLV+/dawvpxUYkRH4JZM1\n" |
| 43 | "CVGukMfKNrSVH4Ap4QJBAOJmGV1ASPnB4r4nc99at7JuIJmd7fmuVUwUgYi4XgaR\n" |
| 44 | "WhScBsgYwZ/JoywdyZJgnbcrTDuVcWG56B3vXbhdpMsCQQDf9zeJrjnPZ3Cqm79y\n" |
| 45 | "kdqANep0uwZciiNiWxsQrCHztywOvbFhdp8iYVFG9EK8DMY41Y5TxUwsHD+67zao\n" |
| 46 | "ZNqJAkEA1suLUP/GvL8IwuRneQd2tWDqqRQ/Td3qq03hP7e77XtF/buya3Ghclo5\n" |
| 47 | "54czUR89QyVfJEC6278nzA7n2h1uVQJAcG6mztNL6ja/dKZjYZye2CY44QjSlLo0\n" |
| 48 | "MTgTSjdfg/28fFn2Jjtqf9Pi/X+50LWI/RcYMC2no606wRk9kyOuIQJBAK6VSAim\n" |
| 49 | "1pOEjsYQn0X5KEIrz1G3bfCbB848Ime3U2/FWlCHMr6ch8kCZ5d1WUeJD3LbwMNG\n" |
| 50 | "UCXiYxSsu20QNVw=\n" |
| 51 | "-----END RSA PRIVATE KEY-----\n"; |
| 52 | |
| 53 | static const char kCERT_PEM[] = |
| 54 | "-----BEGIN CERTIFICATE-----\n" |
| 55 | "MIIBmTCCAQKgAwIBAgIEbzBSAjANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDEwZX\n" |
| 56 | "ZWJSVEMwHhcNMTQwMTAyMTgyNDQ3WhcNMTQwMjAxMTgyNDQ3WjARMQ8wDQYDVQQD\n" |
| 57 | "EwZXZWJSVEMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYRkbhmI7kVA/rM\n" |
| 58 | "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" |
| 59 | "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" |
| 60 | "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAUflI\n" |
| 61 | "VUe5Krqf5RVa5C3u/UTAOAUJBiDS3VANTCLBxjuMsvqOG0WvaYWP3HYPgrz0jXK2\n" |
| 62 | "LJE/mGw3MyFHEqi81jh95J+ypl6xKW6Rm8jKLR87gUvCaVYn/Z4/P3AqcQTB7wOv\n" |
| 63 | "UD0A8qfhfDM+LK6rPAnCsVN0NRDY3jvd6rzix9M=\n" |
| 64 | "-----END CERTIFICATE-----\n"; |
| 65 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 66 | static const char kIntCert1[] = |
| 67 | "-----BEGIN CERTIFICATE-----\n" |
| 68 | "MIIEUjCCAjqgAwIBAgIBAjANBgkqhkiG9w0BAQsFADCBljELMAkGA1UEBhMCVVMx\n" |
| 69 | "EzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxFDAS\n" |
| 70 | "BgNVBAoMC0dvb2dsZSwgSW5jMQwwCgYDVQQLDANHVFAxFzAVBgNVBAMMDnRlbGVw\n" |
| 71 | "aG9ueS5nb29nMR0wGwYJKoZIhvcNAQkBFg5ndHBAZ29vZ2xlLmNvbTAeFw0xNzA5\n" |
| 72 | "MjYwNDA5MDNaFw0yMDA2MjIwNDA5MDNaMGQxCzAJBgNVBAYTAlVTMQswCQYDVQQI\n" |
| 73 | "DAJDQTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEXMBUGA1UECgwOdGVsZXBob255\n" |
| 74 | "Lmdvb2cxFzAVBgNVBAMMDnRlbGVwaG9ueS5nb29nMIGfMA0GCSqGSIb3DQEBAQUA\n" |
| 75 | "A4GNADCBiQKBgQDJXWeeU1v1+wlqkVobzI3aN7Uh2iVQA9YCdq5suuabtiD/qoOD\n" |
| 76 | "NKpmQqsx7WZGGWSZTDFEBaUpvIK7Hb+nzRqk6iioPCFOFuarm6GxO1xVneImMuE6\n" |
| 77 | "tuWb3YZPr+ikChJbl11y5UcSbg0QsbeUc+jHl5umNvrL85Y+z8SP0rxbBwIDAQAB\n" |
| 78 | "o2AwXjAdBgNVHQ4EFgQU7tdZobqlN8R8V72FQnRxmqq8tKswHwYDVR0jBBgwFoAU\n" |
| 79 | "5GgKMUtcxkQ2dJrtNR5YOlIAPDswDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMC\n" |
| 80 | "AQYwDQYJKoZIhvcNAQELBQADggIBADObh9Z+z14FmP9zSenhFtq7hFnmNrSkklk8\n" |
| 81 | "eyYWXKfOuIriEQQBZsz76ZcnzStih8Rj+yQ0AXydk4fJ5LOwC2cUqQBar17g6Pd2\n" |
| 82 | "8g4SIL4azR9WvtiSvpuGlwp25b+yunaacDne6ebnf/MUiiKT5w61Xo3cEPVfl38e\n" |
| 83 | "/Up2l0bioid5enUTmg6LY6RxDO6tnZQkz3XD+nNSwT4ehtkqFpHYWjErj0BbkDM2\n" |
| 84 | "hiVc/JsYOZn3DmuOlHVHU6sKwqh3JEyvHO/d7DGzMGWHpHwv2mCTJq6l/sR95Tc2\n" |
| 85 | "GaQZgGDVNs9pdEouJCDm9e/PbQWRYhnat82PTkXx/6mDAAwdZlIi/pACzq8K4p7e\n" |
| 86 | "6hF0t8uKGnXJubHPXxlnJU6yxZ0yWmivAGjwWK4ur832gKlho4jeMDhiI/T3QPpl\n" |
| 87 | "iMNsIvxRhdD+GxJkQP1ezayw8s+Uc9KwKglrkBSRRDLCJUfPOvMmXLUDSTMX7kp4\n" |
| 88 | "/Ak1CA8dVLJIlfEjLBUuvAttlP7+7lsKNgxAjCxZkWLXIyGULzNPQwVWkGfCbrQs\n" |
| 89 | "XyMvSbFsSIb7blV7eLlmf9a+2RprUUkc2ALXLLCI9YQXmxm2beBfMyNmmebwBJzT\n" |
| 90 | "B0OR+5pFFNTJPoNlqpdrDsGrDu7JlUtk0ZLZzYyKXbgy2qXxfd4OWzXXjxpLMszZ\n" |
| 91 | "LDIpOAkj\n" |
| 92 | "-----END CERTIFICATE-----\n"; |
| 93 | |
| 94 | static const char kCACert[] = |
| 95 | "-----BEGIN CERTIFICATE-----\n" |
| 96 | "MIIGETCCA/mgAwIBAgIJAKN9r/BdbGUJMA0GCSqGSIb3DQEBCwUAMIGWMQswCQYD\n" |
| 97 | "VQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4g\n" |
| 98 | "VmlldzEUMBIGA1UECgwLR29vZ2xlLCBJbmMxDDAKBgNVBAsMA0dUUDEXMBUGA1UE\n" |
| 99 | "AwwOdGVsZXBob255Lmdvb2cxHTAbBgkqhkiG9w0BCQEWDmd0cEBnb29nbGUuY29t\n" |
| 100 | "MB4XDTE3MDcyNzIzMDE0NVoXDTE3MDgyNjIzMDE0NVowgZYxCzAJBgNVBAYTAlVT\n" |
| 101 | "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1Nb3VudGFpbiBWaWV3MRQw\n" |
| 102 | "EgYDVQQKDAtHb29nbGUsIEluYzEMMAoGA1UECwwDR1RQMRcwFQYDVQQDDA50ZWxl\n" |
| 103 | "cGhvbnkuZ29vZzEdMBsGCSqGSIb3DQEJARYOZ3RwQGdvb2dsZS5jb20wggIiMA0G\n" |
| 104 | "CSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCfvpF7aBV5Hp1EHsWoIlL3GeHwh8dS\n" |
| 105 | "lv9VQCegN9rD06Ny7MgcED5AiK2vqXmUmOVS+7NbATkdVYN/eozDhKtN3Q3n87kJ\n" |
| 106 | "Nt/TD/TcZZHOZIGsRPbrf2URK26E/5KzTzbzXVBOA1e+gSj+EBbltGqb01ZO5ErF\n" |
| 107 | "iPGViPM/HpYKdq6mfz2bS5PhU67XZMM2zvToyReQ/Fjm/6PJhwKSRXSgZF5djPhk\n" |
| 108 | "2LfOKMLS0AeZtd2C4DFsCU41lfLUkybioDgFuzTQ3TFi1K8A07KYTMmLY/yQppnf\n" |
| 109 | "SpNX58shlVhM+Ed37K1Z0rU0OfVCZ5P+KKaSSfMranjlU7zeUIhZYjqq/EYrEhbS\n" |
| 110 | "dLnNHwgJrqxzId3kq8uuLM6+VB7JZKnZLfT90GdAbX4+tutNe21smmogF9f80vEy\n" |
| 111 | "gM4tOp9rXrvz9vCwWHXVY9kdKemdLAsREoO6MS9k2ctK4jj80o2dROuFC6Q3e7mz\n" |
| 112 | "RjvZr5Tvi464c2o9o/jNlJ0O6q7V2eQzohD+7VnV5QPpRGXxlIeqpR2zoAg+WtRS\n" |
| 113 | "4OgHOVYiD3M6uAlggJA5pcDjMfkEZ+pkhtVcT4qMCEoruk6GbyPxS565oSHu16bH\n" |
| 114 | "EjeCqbZOVND5T3oA7nz6aQSs8sJabt0jmxUkGVnE+4ZDIuuRtkRma+0P/96Mtqor\n" |
| 115 | "OlpNWY1OBDY64QIDAQABo2AwXjAdBgNVHQ4EFgQU5GgKMUtcxkQ2dJrtNR5YOlIA\n" |
| 116 | "PDswHwYDVR0jBBgwFoAU5GgKMUtcxkQ2dJrtNR5YOlIAPDswDwYDVR0TAQH/BAUw\n" |
| 117 | "AwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQELBQADggIBAARQly5/bB6VUL2C\n" |
| 118 | "ykDYgWt48go407pAra6tL2kjpdfxV5PdL7iMZRkeht00vj+BVahIqZKrNOa/f5Fx\n" |
| 119 | "vlpahZFu0PDN436aQwRZ9qWut2qDOK0/z9Hhj6NWybquRFwMwqkPG/ivLMDU8Dmj\n" |
| 120 | "CIplpngPYNwXCs0KzdjSXYxqxJbwMjQXELD+/RcurY0oTtJMM1/2vKQMzw24UJqe\n" |
| 121 | "XLJAlsnd2AnWzWNUEviDZY89j9NdkHerBmV2gGzcU+X5lgOO5M8odBv0ZC9D+a6Z\n" |
| 122 | "QPZAOfdGVw60hhGvTW5s/s0dHwCpegRidhs0MD0fTmwwjYFBSmUx3Gztr4JTzOOr\n" |
| 123 | "7e5daJuak2ujQ5DqcGBvt1gePjSudb5brS7JQtN8tI/FyrnR4q/OuOwv1EvlC5RG\n" |
| 124 | "hLX+TXaWqFxB1Hd8ebKRR40mboFG6KcUI3lLBthDvQE7jnq48QfZMjlMQK0ZF1l7\n" |
| 125 | "SrlwRXWA74bU8CLJvnZKKo9p4TsTiDYGSYC6tNHKj5s3TGWL46oqGyZ0KdGNhrtC\n" |
| 126 | "rIGenMhth1vPYjyy0XuGBndXT85yi+IM2l8g8oU845+plxIhgpSI8bbC0oLwnhQ5\n" |
| 127 | "ARfsiYLkXDE7imSS0CSUmye76372mlzAIB1is4bBB/SzpPQtBuB9LDKtONgpSGHn\n" |
| 128 | "dGaXBy+qbVXVyGXaeEbIRjtJ6m92\n" |
| 129 | "-----END CERTIFICATE-----\n"; |
| 130 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 131 | class SSLStreamAdapterTestBase; |
| 132 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 133 | class SSLDummyStreamBase : public rtc::StreamInterface, |
| 134 | public sigslot::has_slots<> { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | public: |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 136 | SSLDummyStreamBase(SSLStreamAdapterTestBase* test, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 137 | const std::string& side, |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 138 | rtc::StreamInterface* in, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 139 | rtc::StreamInterface* out) |
| 140 | : test_base_(test), side_(side), in_(in), out_(out), first_packet_(true) { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 141 | in_->SignalEvent.connect(this, &SSLDummyStreamBase::OnEventIn); |
| 142 | out_->SignalEvent.connect(this, &SSLDummyStreamBase::OnEventOut); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | } |
| 144 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 145 | rtc::StreamState GetState() const override { return rtc::SS_OPEN; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 147 | rtc::StreamResult Read(void* buffer, |
| 148 | size_t buffer_len, |
| 149 | size_t* read, |
| 150 | int* error) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | rtc::StreamResult r; |
| 152 | |
| 153 | r = in_->Read(buffer, buffer_len, read, error); |
| 154 | if (r == rtc::SR_BLOCK) |
| 155 | return rtc::SR_BLOCK; |
| 156 | if (r == rtc::SR_EOS) |
| 157 | return rtc::SR_EOS; |
| 158 | |
| 159 | if (r != rtc::SR_SUCCESS) { |
| 160 | ADD_FAILURE(); |
| 161 | return rtc::SR_ERROR; |
| 162 | } |
| 163 | |
| 164 | return rtc::SR_SUCCESS; |
| 165 | } |
| 166 | |
| 167 | // Catch readability events on in and pass them up. |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 168 | void OnEventIn(rtc::StreamInterface* stream, int sig, int err) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 169 | int mask = (rtc::SE_READ | rtc::SE_CLOSE); |
| 170 | |
| 171 | if (sig & mask) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 172 | RTC_LOG(LS_VERBOSE) << "SSLDummyStreamBase::OnEvent side=" << side_ |
| 173 | << " sig=" << sig << " forwarding upward"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 174 | PostEvent(sig & mask, 0); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Catch writeability events on out and pass them up. |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 179 | void OnEventOut(rtc::StreamInterface* stream, int sig, int err) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | if (sig & rtc::SE_WRITE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 181 | RTC_LOG(LS_VERBOSE) << "SSLDummyStreamBase::OnEvent side=" << side_ |
| 182 | << " sig=" << sig << " forwarding upward"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | |
| 184 | PostEvent(sig & rtc::SE_WRITE, 0); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Write to the outgoing FifoBuffer |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 189 | rtc::StreamResult WriteData(const void* data, |
| 190 | size_t data_len, |
| 191 | size_t* written, |
| 192 | int* error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 193 | return out_->Write(data, data_len, written, error); |
| 194 | } |
| 195 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 196 | rtc::StreamResult Write(const void* data, |
| 197 | size_t data_len, |
| 198 | size_t* written, |
| 199 | int* error) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 200 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 201 | void Close() override { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 202 | RTC_LOG(LS_INFO) << "Closing outbound stream"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 203 | out_->Close(); |
| 204 | } |
| 205 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 206 | protected: |
| 207 | SSLStreamAdapterTestBase* test_base_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | const std::string side_; |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 209 | rtc::StreamInterface* in_; |
| 210 | rtc::StreamInterface* out_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 211 | bool first_packet_; |
| 212 | }; |
| 213 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 214 | class SSLDummyStreamTLS : public SSLDummyStreamBase { |
| 215 | public: |
| 216 | SSLDummyStreamTLS(SSLStreamAdapterTestBase* test, |
| 217 | const std::string& side, |
| 218 | rtc::FifoBuffer* in, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 219 | rtc::FifoBuffer* out) |
| 220 | : SSLDummyStreamBase(test, side, in, out) {} |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 221 | }; |
| 222 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 223 | class BufferQueueStream : public rtc::BufferQueue, public rtc::StreamInterface { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 224 | public: |
| 225 | BufferQueueStream(size_t capacity, size_t default_size) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 226 | : rtc::BufferQueue(capacity, default_size) {} |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 227 | |
| 228 | // Implementation of abstract StreamInterface methods. |
| 229 | |
| 230 | // A buffer queue stream is always "open". |
| 231 | rtc::StreamState GetState() const override { return rtc::SS_OPEN; } |
| 232 | |
| 233 | // Reading a buffer queue stream will either succeed or block. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 234 | rtc::StreamResult Read(void* buffer, |
| 235 | size_t buffer_len, |
| 236 | size_t* read, |
| 237 | int* error) override { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 238 | if (!ReadFront(buffer, buffer_len, read)) { |
| 239 | return rtc::SR_BLOCK; |
| 240 | } |
| 241 | return rtc::SR_SUCCESS; |
| 242 | } |
| 243 | |
| 244 | // Writing to a buffer queue stream will either succeed or block. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 245 | rtc::StreamResult Write(const void* data, |
| 246 | size_t data_len, |
| 247 | size_t* written, |
| 248 | int* error) override { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 249 | if (!WriteBack(data, data_len, written)) { |
| 250 | return rtc::SR_BLOCK; |
| 251 | } |
| 252 | return rtc::SR_SUCCESS; |
| 253 | } |
| 254 | |
| 255 | // A buffer queue stream can not be closed. |
| 256 | void Close() override {} |
| 257 | |
| 258 | protected: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 259 | void NotifyReadableForTest() override { PostEvent(rtc::SE_READ, 0); } |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 260 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 261 | void NotifyWritableForTest() override { PostEvent(rtc::SE_WRITE, 0); } |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | class SSLDummyStreamDTLS : public SSLDummyStreamBase { |
| 265 | public: |
| 266 | SSLDummyStreamDTLS(SSLStreamAdapterTestBase* test, |
| 267 | const std::string& side, |
| 268 | BufferQueueStream* in, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 269 | BufferQueueStream* out) |
| 270 | : SSLDummyStreamBase(test, side, in, out) {} |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 273 | static const int kFifoBufferSize = 4096; |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 274 | static const int kBufferCapacity = 1; |
| 275 | static const size_t kDefaultBufferSize = 2048; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 276 | |
| 277 | class SSLStreamAdapterTestBase : public testing::Test, |
| 278 | public sigslot::has_slots<> { |
| 279 | public: |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 280 | SSLStreamAdapterTestBase( |
| 281 | const std::string& client_cert_pem, |
| 282 | const std::string& client_private_key_pem, |
| 283 | bool dtls, |
| 284 | rtc::KeyParams client_key_type = rtc::KeyParams(rtc::KT_DEFAULT), |
| 285 | rtc::KeyParams server_key_type = rtc::KeyParams(rtc::KT_DEFAULT)) |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 286 | : client_cert_pem_(client_cert_pem), |
| 287 | client_private_key_pem_(client_private_key_pem), |
| 288 | client_key_type_(client_key_type), |
| 289 | server_key_type_(server_key_type), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 290 | client_stream_(nullptr), |
| 291 | server_stream_(nullptr), |
| 292 | client_identity_(nullptr), |
| 293 | server_identity_(nullptr), |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 294 | delay_(0), |
| 295 | mtu_(1460), |
| 296 | loss_(0), |
| 297 | lose_first_packet_(false), |
| 298 | damage_(false), |
| 299 | dtls_(dtls), |
| 300 | handshake_wait_(5000), |
| 301 | identities_set_(false) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 302 | // Set use of the test RNG to get predictable loss patterns. |
| 303 | rtc::SetRandomTestMode(true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 306 | ~SSLStreamAdapterTestBase() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 307 | // Put it back for the next test. |
| 308 | rtc::SetRandomTestMode(false); |
| 309 | } |
| 310 | |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 311 | void SetUp() override { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 312 | CreateStreams(); |
| 313 | |
| 314 | client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_)); |
| 315 | server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_)); |
| 316 | |
| 317 | // Set up the slots |
| 318 | client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent); |
| 319 | server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent); |
| 320 | |
| 321 | if (!client_cert_pem_.empty() && !client_private_key_pem_.empty()) { |
| 322 | client_identity_ = rtc::SSLIdentity::FromPEMStrings( |
| 323 | client_private_key_pem_, client_cert_pem_); |
| 324 | } else { |
| 325 | client_identity_ = rtc::SSLIdentity::Generate("client", client_key_type_); |
| 326 | } |
| 327 | server_identity_ = rtc::SSLIdentity::Generate("server", server_key_type_); |
| 328 | |
| 329 | client_ssl_->SetIdentity(client_identity_); |
| 330 | server_ssl_->SetIdentity(server_identity_); |
| 331 | } |
| 332 | |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 333 | void TearDown() override { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 334 | client_ssl_.reset(nullptr); |
| 335 | server_ssl_.reset(nullptr); |
| 336 | } |
| 337 | |
| 338 | virtual void CreateStreams() = 0; |
| 339 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 340 | // Recreate the client/server identities with the specified validity period. |
| 341 | // |not_before| and |not_after| are offsets from the current time in number |
| 342 | // of seconds. |
| 343 | void ResetIdentitiesWithValidity(int not_before, int not_after) { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 344 | CreateStreams(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 345 | |
| 346 | client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_)); |
| 347 | server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_)); |
| 348 | |
| 349 | client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent); |
| 350 | server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent); |
| 351 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 352 | time_t now = time(nullptr); |
| 353 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 354 | rtc::SSLIdentityParams client_params; |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 355 | client_params.key_params = rtc::KeyParams(rtc::KT_DEFAULT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 356 | client_params.common_name = "client"; |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 357 | client_params.not_before = now + not_before; |
| 358 | client_params.not_after = now + not_after; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 359 | client_identity_ = rtc::SSLIdentity::GenerateForTest(client_params); |
| 360 | |
| 361 | rtc::SSLIdentityParams server_params; |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 362 | server_params.key_params = rtc::KeyParams(rtc::KT_DEFAULT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 363 | server_params.common_name = "server"; |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 364 | server_params.not_before = now + not_before; |
| 365 | server_params.not_after = now + not_after; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 366 | server_identity_ = rtc::SSLIdentity::GenerateForTest(server_params); |
| 367 | |
| 368 | client_ssl_->SetIdentity(client_identity_); |
| 369 | server_ssl_->SetIdentity(server_identity_); |
| 370 | } |
| 371 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 372 | virtual void OnEvent(rtc::StreamInterface* stream, int sig, int err) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 373 | RTC_LOG(LS_VERBOSE) << "SSLStreamAdapterTestBase::OnEvent sig=" << sig; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 374 | |
| 375 | if (sig & rtc::SE_READ) { |
| 376 | ReadData(stream); |
| 377 | } |
| 378 | |
| 379 | if ((stream == client_ssl_.get()) && (sig & rtc::SE_WRITE)) { |
| 380 | WriteData(); |
| 381 | } |
| 382 | } |
| 383 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 384 | void SetPeerIdentitiesByDigest(bool correct, bool expect_success) { |
| 385 | unsigned char server_digest[20]; |
| 386 | size_t server_digest_len; |
| 387 | unsigned char client_digest[20]; |
| 388 | size_t client_digest_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 389 | bool rv; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 390 | rtc::SSLPeerCertificateDigestError err; |
| 391 | rtc::SSLPeerCertificateDigestError expected_err = |
| 392 | expect_success |
| 393 | ? rtc::SSLPeerCertificateDigestError::NONE |
| 394 | : rtc::SSLPeerCertificateDigestError::VERIFICATION_FAILED; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 395 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 396 | RTC_LOG(LS_INFO) << "Setting peer identities by digest"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 397 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 398 | rv = server_identity_->certificate().ComputeDigest( |
| 399 | rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 400 | ASSERT_TRUE(rv); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 401 | rv = client_identity_->certificate().ComputeDigest( |
| 402 | rtc::DIGEST_SHA_1, client_digest, 20, &client_digest_len); |
| 403 | ASSERT_TRUE(rv); |
| 404 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 405 | if (!correct) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 406 | RTC_LOG(LS_INFO) << "Setting bogus digest for server cert"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 407 | server_digest[0]++; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 408 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 409 | rv = client_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, server_digest, |
| 410 | server_digest_len, &err); |
| 411 | EXPECT_EQ(expected_err, err); |
| 412 | EXPECT_EQ(expect_success, rv); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 413 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 414 | if (!correct) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 415 | RTC_LOG(LS_INFO) << "Setting bogus digest for client cert"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 416 | client_digest[0]++; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 417 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 418 | rv = server_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, client_digest, |
| 419 | client_digest_len, &err); |
| 420 | EXPECT_EQ(expected_err, err); |
| 421 | EXPECT_EQ(expect_success, rv); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 422 | |
| 423 | identities_set_ = true; |
| 424 | } |
| 425 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 426 | void SetupProtocolVersions(rtc::SSLProtocolVersion server_version, |
| 427 | rtc::SSLProtocolVersion client_version) { |
| 428 | server_ssl_->SetMaxProtocolVersion(server_version); |
| 429 | client_ssl_->SetMaxProtocolVersion(client_version); |
| 430 | } |
| 431 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 432 | void TestHandshake(bool expect_success = true) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 433 | server_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS); |
| 434 | client_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 435 | |
| 436 | if (!dtls_) { |
| 437 | // Make sure we simulate a reliable network for TLS. |
| 438 | // This is just a check to make sure that people don't write wrong |
| 439 | // tests. |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 440 | RTC_CHECK_EQ(1460, mtu_); |
| 441 | RTC_CHECK(!loss_); |
| 442 | RTC_CHECK(!lose_first_packet_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (!identities_set_) |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 446 | SetPeerIdentitiesByDigest(true, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 447 | |
| 448 | // Start the handshake |
| 449 | int rv; |
| 450 | |
| 451 | server_ssl_->SetServerRole(); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 452 | rv = server_ssl_->StartSSL(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 453 | ASSERT_EQ(0, rv); |
| 454 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 455 | rv = client_ssl_->StartSSL(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 456 | ASSERT_EQ(0, rv); |
| 457 | |
| 458 | // Now run the handshake |
| 459 | if (expect_success) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 460 | EXPECT_TRUE_WAIT((client_ssl_->GetState() == rtc::SS_OPEN) && |
| 461 | (server_ssl_->GetState() == rtc::SS_OPEN), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 462 | handshake_wait_); |
| 463 | } else { |
| 464 | EXPECT_TRUE_WAIT(client_ssl_->GetState() == rtc::SS_CLOSED, |
| 465 | handshake_wait_); |
| 466 | } |
| 467 | } |
| 468 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 469 | // This tests that the handshake can complete before the identity is |
| 470 | // verified, and the identity will be verified after the fact. |
| 471 | void TestHandshakeWithDelayedIdentity(bool valid_identity) { |
| 472 | server_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS); |
| 473 | client_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS); |
| 474 | |
| 475 | if (!dtls_) { |
| 476 | // Make sure we simulate a reliable network for TLS. |
| 477 | // This is just a check to make sure that people don't write wrong |
| 478 | // tests. |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 479 | RTC_CHECK_EQ(1460, mtu_); |
| 480 | RTC_CHECK(!loss_); |
| 481 | RTC_CHECK(!lose_first_packet_); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // Start the handshake |
| 485 | int rv; |
| 486 | |
| 487 | server_ssl_->SetServerRole(); |
| 488 | rv = server_ssl_->StartSSL(); |
| 489 | ASSERT_EQ(0, rv); |
| 490 | |
| 491 | rv = client_ssl_->StartSSL(); |
| 492 | ASSERT_EQ(0, rv); |
| 493 | |
| 494 | // Now run the handshake. |
| 495 | EXPECT_TRUE_WAIT( |
| 496 | client_ssl_->IsTlsConnected() && server_ssl_->IsTlsConnected(), |
| 497 | handshake_wait_); |
| 498 | |
| 499 | // Until the identity has been verified, the state should still be |
| 500 | // SS_OPENING and writes should return SR_BLOCK. |
| 501 | EXPECT_EQ(rtc::SS_OPENING, client_ssl_->GetState()); |
| 502 | EXPECT_EQ(rtc::SS_OPENING, server_ssl_->GetState()); |
| 503 | unsigned char packet[1]; |
| 504 | size_t sent; |
| 505 | EXPECT_EQ(rtc::SR_BLOCK, client_ssl_->Write(&packet, 1, &sent, 0)); |
| 506 | EXPECT_EQ(rtc::SR_BLOCK, server_ssl_->Write(&packet, 1, &sent, 0)); |
| 507 | |
| 508 | // If we set an invalid identity at this point, SetPeerCertificateDigest |
| 509 | // should return false. |
| 510 | SetPeerIdentitiesByDigest(valid_identity, valid_identity); |
| 511 | // State should then transition to SS_OPEN or SS_CLOSED based on validation |
| 512 | // of the identity. |
| 513 | if (valid_identity) { |
| 514 | EXPECT_EQ(rtc::SS_OPEN, client_ssl_->GetState()); |
| 515 | EXPECT_EQ(rtc::SS_OPEN, server_ssl_->GetState()); |
| 516 | } else { |
| 517 | EXPECT_EQ(rtc::SS_CLOSED, client_ssl_->GetState()); |
| 518 | EXPECT_EQ(rtc::SS_CLOSED, server_ssl_->GetState()); |
| 519 | } |
| 520 | } |
| 521 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 522 | rtc::StreamResult DataWritten(SSLDummyStreamBase* from, |
| 523 | const void* data, |
| 524 | size_t data_len, |
| 525 | size_t* written, |
| 526 | int* error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 527 | // Randomly drop loss_ percent of packets |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 528 | if (rtc::CreateRandomId() % 100 < static_cast<uint32_t>(loss_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 529 | RTC_LOG(LS_VERBOSE) << "Randomly dropping packet, size=" << data_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 530 | *written = data_len; |
| 531 | return rtc::SR_SUCCESS; |
| 532 | } |
| 533 | if (dtls_ && (data_len > mtu_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 534 | RTC_LOG(LS_VERBOSE) << "Dropping packet > mtu, size=" << data_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 535 | *written = data_len; |
| 536 | return rtc::SR_SUCCESS; |
| 537 | } |
| 538 | |
| 539 | // Optionally damage application data (type 23). Note that we don't damage |
| 540 | // handshake packets and we damage the last byte to keep the header |
| 541 | // intact but break the MAC. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 542 | if (damage_ && (*static_cast<const unsigned char*>(data) == 23)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 543 | std::vector<char> buf(data_len); |
| 544 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 545 | RTC_LOG(LS_VERBOSE) << "Damaging packet"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 546 | |
| 547 | memcpy(&buf[0], data, data_len); |
| 548 | buf[data_len - 1]++; |
| 549 | |
| 550 | return from->WriteData(&buf[0], data_len, written, error); |
| 551 | } |
| 552 | |
| 553 | return from->WriteData(data, data_len, written, error); |
| 554 | } |
| 555 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 556 | void SetDelay(int delay) { delay_ = delay; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 557 | int GetDelay() { return delay_; } |
| 558 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 559 | void SetLoseFirstPacket(bool lose) { lose_first_packet_ = lose; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 560 | bool GetLoseFirstPacket() { return lose_first_packet_; } |
| 561 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 562 | void SetLoss(int percent) { loss_ = percent; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 563 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 564 | void SetDamage() { damage_ = true; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 565 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 566 | void SetMtu(size_t mtu) { mtu_ = mtu; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 567 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 568 | void SetHandshakeWait(int wait) { handshake_wait_ = wait; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 569 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 570 | void SetDtlsSrtpCryptoSuites(const std::vector<int>& ciphers, bool client) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 571 | if (client) |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 572 | client_ssl_->SetDtlsSrtpCryptoSuites(ciphers); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 573 | else |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 574 | server_ssl_->SetDtlsSrtpCryptoSuites(ciphers); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 577 | bool GetDtlsSrtpCryptoSuite(bool client, int* retval) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 578 | if (client) |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 579 | return client_ssl_->GetDtlsSrtpCryptoSuite(retval); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 580 | else |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 581 | return server_ssl_->GetDtlsSrtpCryptoSuite(retval); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 582 | } |
| 583 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 584 | std::unique_ptr<rtc::SSLCertificate> GetPeerCertificate(bool client) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 585 | std::unique_ptr<rtc::SSLCertChain> chain; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 586 | if (client) |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 587 | chain = client_ssl_->GetPeerSSLCertChain(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 588 | else |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 589 | chain = server_ssl_->GetPeerSSLCertChain(); |
| 590 | return (chain && chain->GetSize()) ? chain->Get(0).GetUniqueReference() |
| 591 | : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 594 | bool GetSslCipherSuite(bool client, int* retval) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 595 | if (client) |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 596 | return client_ssl_->GetSslCipherSuite(retval); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 597 | else |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 598 | return server_ssl_->GetSslCipherSuite(retval); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 599 | } |
| 600 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 601 | int GetSslVersion(bool client) { |
| 602 | if (client) |
| 603 | return client_ssl_->GetSslVersion(); |
| 604 | else |
| 605 | return server_ssl_->GetSslVersion(); |
| 606 | } |
| 607 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 608 | bool ExportKeyingMaterial(const char* label, |
| 609 | const unsigned char* context, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 610 | size_t context_len, |
| 611 | bool use_context, |
| 612 | bool client, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 613 | unsigned char* result, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 614 | size_t result_len) { |
| 615 | if (client) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 616 | return client_ssl_->ExportKeyingMaterial(label, context, context_len, |
| 617 | use_context, result, result_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 618 | else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 619 | return server_ssl_->ExportKeyingMaterial(label, context, context_len, |
| 620 | use_context, result, result_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // To be implemented by subclasses. |
| 624 | virtual void WriteData() = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 625 | virtual void ReadData(rtc::StreamInterface* stream) = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 626 | virtual void TestTransfer(int size) = 0; |
| 627 | |
| 628 | protected: |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 629 | std::string client_cert_pem_; |
| 630 | std::string client_private_key_pem_; |
| 631 | rtc::KeyParams client_key_type_; |
| 632 | rtc::KeyParams server_key_type_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 633 | SSLDummyStreamBase* client_stream_; // freed by client_ssl_ destructor |
| 634 | SSLDummyStreamBase* server_stream_; // freed by server_ssl_ destructor |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 635 | std::unique_ptr<rtc::SSLStreamAdapter> client_ssl_; |
| 636 | std::unique_ptr<rtc::SSLStreamAdapter> server_ssl_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 637 | rtc::SSLIdentity* client_identity_; // freed by client_ssl_ destructor |
| 638 | rtc::SSLIdentity* server_identity_; // freed by server_ssl_ destructor |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 639 | int delay_; |
| 640 | size_t mtu_; |
| 641 | int loss_; |
| 642 | bool lose_first_packet_; |
| 643 | bool damage_; |
| 644 | bool dtls_; |
| 645 | int handshake_wait_; |
| 646 | bool identities_set_; |
| 647 | }; |
| 648 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 649 | class SSLStreamAdapterTestTLS |
| 650 | : public SSLStreamAdapterTestBase, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 651 | public WithParamInterface<tuple<rtc::KeyParams, rtc::KeyParams>> { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 652 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 653 | SSLStreamAdapterTestTLS() |
| 654 | : SSLStreamAdapterTestBase("", |
| 655 | "", |
| 656 | false, |
| 657 | ::testing::get<0>(GetParam()), |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 658 | ::testing::get<1>(GetParam())), |
| 659 | client_buffer_(kFifoBufferSize), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 660 | server_buffer_(kFifoBufferSize) {} |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 661 | |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 662 | void CreateStreams() override { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 663 | client_stream_ = |
| 664 | new SSLDummyStreamTLS(this, "c2s", &client_buffer_, &server_buffer_); |
| 665 | server_stream_ = |
| 666 | new SSLDummyStreamTLS(this, "s2c", &server_buffer_, &client_buffer_); |
| 667 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 668 | |
| 669 | // Test data transfer for TLS |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 670 | void TestTransfer(int size) override { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 671 | RTC_LOG(LS_INFO) << "Starting transfer test with " << size << " bytes"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 672 | // Create some dummy data to send. |
| 673 | size_t received; |
| 674 | |
| 675 | send_stream_.ReserveSize(size); |
| 676 | for (int i = 0; i < size; ++i) { |
| 677 | char ch = static_cast<char>(i); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 678 | send_stream_.Write(&ch, 1, nullptr, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 679 | } |
| 680 | send_stream_.Rewind(); |
| 681 | |
| 682 | // Prepare the receive stream. |
| 683 | recv_stream_.ReserveSize(size); |
| 684 | |
| 685 | // Start sending |
| 686 | WriteData(); |
| 687 | |
| 688 | // Wait for the client to close |
| 689 | EXPECT_TRUE_WAIT(server_ssl_->GetState() == rtc::SS_CLOSED, 10000); |
| 690 | |
| 691 | // Now check the data |
| 692 | recv_stream_.GetSize(&received); |
| 693 | |
| 694 | EXPECT_EQ(static_cast<size_t>(size), received); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 695 | EXPECT_EQ(0, |
| 696 | memcmp(send_stream_.GetBuffer(), recv_stream_.GetBuffer(), size)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 697 | } |
| 698 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 699 | void WriteData() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 700 | size_t position, tosend, size; |
| 701 | rtc::StreamResult rv; |
| 702 | size_t sent; |
| 703 | char block[kBlockSize]; |
| 704 | |
| 705 | send_stream_.GetSize(&size); |
| 706 | if (!size) |
| 707 | return; |
| 708 | |
| 709 | for (;;) { |
| 710 | send_stream_.GetPosition(&position); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 711 | if (send_stream_.Read(block, sizeof(block), &tosend, nullptr) != |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 712 | rtc::SR_EOS) { |
| 713 | rv = client_ssl_->Write(block, tosend, &sent, 0); |
| 714 | |
| 715 | if (rv == rtc::SR_SUCCESS) { |
| 716 | send_stream_.SetPosition(position + sent); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 717 | RTC_LOG(LS_VERBOSE) << "Sent: " << position + sent; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 718 | } else if (rv == rtc::SR_BLOCK) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 719 | RTC_LOG(LS_VERBOSE) << "Blocked..."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 720 | send_stream_.SetPosition(position); |
| 721 | break; |
| 722 | } else { |
| 723 | ADD_FAILURE(); |
| 724 | break; |
| 725 | } |
| 726 | } else { |
| 727 | // Now close |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 728 | RTC_LOG(LS_INFO) << "Wrote " << position << " bytes. Closing"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 729 | client_ssl_->Close(); |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | }; |
| 734 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 735 | void ReadData(rtc::StreamInterface* stream) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 736 | char buffer[1600]; |
| 737 | size_t bread; |
| 738 | int err2; |
| 739 | rtc::StreamResult r; |
| 740 | |
| 741 | for (;;) { |
| 742 | r = stream->Read(buffer, sizeof(buffer), &bread, &err2); |
| 743 | |
| 744 | if (r == rtc::SR_ERROR || r == rtc::SR_EOS) { |
| 745 | // Unfortunately, errors are the way that the stream adapter |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 746 | // signals close in OpenSSL. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 747 | stream->Close(); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | if (r == rtc::SR_BLOCK) |
| 752 | break; |
| 753 | |
| 754 | ASSERT_EQ(rtc::SR_SUCCESS, r); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 755 | RTC_LOG(LS_VERBOSE) << "Read " << bread; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 756 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 757 | recv_stream_.Write(buffer, bread, nullptr, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | private: |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 762 | rtc::FifoBuffer client_buffer_; |
| 763 | rtc::FifoBuffer server_buffer_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 764 | rtc::MemoryStream send_stream_; |
| 765 | rtc::MemoryStream recv_stream_; |
| 766 | }; |
| 767 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 768 | class SSLStreamAdapterTestDTLS |
| 769 | : public SSLStreamAdapterTestBase, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 770 | public WithParamInterface<tuple<rtc::KeyParams, rtc::KeyParams>> { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 771 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 772 | SSLStreamAdapterTestDTLS() |
| 773 | : SSLStreamAdapterTestBase("", |
| 774 | "", |
| 775 | true, |
| 776 | ::testing::get<0>(GetParam()), |
| 777 | ::testing::get<1>(GetParam())), |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 778 | client_buffer_(kBufferCapacity, kDefaultBufferSize), |
| 779 | server_buffer_(kBufferCapacity, kDefaultBufferSize), |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 780 | packet_size_(1000), |
| 781 | count_(0), |
| 782 | sent_(0) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 783 | |
| 784 | SSLStreamAdapterTestDTLS(const std::string& cert_pem, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 785 | const std::string& private_key_pem) |
| 786 | : SSLStreamAdapterTestBase(cert_pem, private_key_pem, true), |
| 787 | client_buffer_(kBufferCapacity, kDefaultBufferSize), |
| 788 | server_buffer_(kBufferCapacity, kDefaultBufferSize), |
| 789 | packet_size_(1000), |
| 790 | count_(0), |
| 791 | sent_(0) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 792 | |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 793 | void CreateStreams() override { |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 794 | client_stream_ = |
| 795 | new SSLDummyStreamDTLS(this, "c2s", &client_buffer_, &server_buffer_); |
| 796 | server_stream_ = |
| 797 | new SSLDummyStreamDTLS(this, "s2c", &server_buffer_, &client_buffer_); |
| 798 | } |
| 799 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 800 | void WriteData() override { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 801 | unsigned char* packet = new unsigned char[1600]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 802 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 803 | while (sent_ < count_) { |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 804 | unsigned int rand_state = sent_; |
| 805 | packet[0] = sent_; |
| 806 | for (size_t i = 1; i < packet_size_; i++) { |
| 807 | // This is a simple LC PRNG. Keep in synch with identical code below. |
| 808 | rand_state = (rand_state * 251 + 19937) >> 7; |
| 809 | packet[i] = rand_state & 0xff; |
| 810 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 811 | |
| 812 | size_t sent; |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 813 | rtc::StreamResult rv = client_ssl_->Write(packet, packet_size_, &sent, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | if (rv == rtc::SR_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 815 | RTC_LOG(LS_VERBOSE) << "Sent: " << sent_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 816 | sent_++; |
| 817 | } else if (rv == rtc::SR_BLOCK) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 818 | RTC_LOG(LS_VERBOSE) << "Blocked..."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 819 | break; |
| 820 | } else { |
| 821 | ADD_FAILURE(); |
| 822 | break; |
| 823 | } |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 824 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 825 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 826 | delete[] packet; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 829 | void ReadData(rtc::StreamInterface* stream) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 830 | unsigned char buffer[2000]; |
| 831 | size_t bread; |
| 832 | int err2; |
| 833 | rtc::StreamResult r; |
| 834 | |
| 835 | for (;;) { |
| 836 | r = stream->Read(buffer, 2000, &bread, &err2); |
| 837 | |
| 838 | if (r == rtc::SR_ERROR) { |
| 839 | // Unfortunately, errors are the way that the stream adapter |
| 840 | // signals close right now |
| 841 | stream->Close(); |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | if (r == rtc::SR_BLOCK) |
| 846 | break; |
| 847 | |
| 848 | ASSERT_EQ(rtc::SR_SUCCESS, r); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 849 | RTC_LOG(LS_VERBOSE) << "Read " << bread; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 850 | |
| 851 | // Now parse the datagram |
| 852 | ASSERT_EQ(packet_size_, bread); |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 853 | unsigned char packet_num = buffer[0]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 854 | |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 855 | unsigned int rand_state = packet_num; |
| 856 | for (size_t i = 1; i < packet_size_; i++) { |
| 857 | // This is a simple LC PRNG. Keep in synch with identical code above. |
| 858 | rand_state = (rand_state * 251 + 19937) >> 7; |
| 859 | ASSERT_EQ(rand_state & 0xff, buffer[i]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 860 | } |
| 861 | received_.insert(packet_num); |
| 862 | } |
| 863 | } |
| 864 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 865 | void TestTransfer(int count) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 866 | count_ = count; |
| 867 | |
| 868 | WriteData(); |
| 869 | |
| 870 | EXPECT_TRUE_WAIT(sent_ == count_, 10000); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 871 | RTC_LOG(LS_INFO) << "sent_ == " << sent_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 872 | |
| 873 | if (damage_) { |
| 874 | WAIT(false, 2000); |
| 875 | EXPECT_EQ(0U, received_.size()); |
| 876 | } else if (loss_ == 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 877 | EXPECT_EQ_WAIT(static_cast<size_t>(sent_), received_.size(), 1000); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 878 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 879 | RTC_LOG(LS_INFO) << "Sent " << sent_ << " packets; received " |
| 880 | << received_.size(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 881 | } |
| 882 | }; |
| 883 | |
| 884 | private: |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 885 | BufferQueueStream client_buffer_; |
| 886 | BufferQueueStream server_buffer_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 887 | size_t packet_size_; |
| 888 | int count_; |
| 889 | int sent_; |
| 890 | std::set<int> received_; |
| 891 | }; |
| 892 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 893 | rtc::StreamResult SSLDummyStreamBase::Write(const void* data, |
| 894 | size_t data_len, |
| 895 | size_t* written, |
| 896 | int* error) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 897 | RTC_LOG(LS_VERBOSE) << "Writing to loopback " << data_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 898 | |
| 899 | if (first_packet_) { |
| 900 | first_packet_ = false; |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 901 | if (test_base_->GetLoseFirstPacket()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 902 | RTC_LOG(LS_INFO) << "Losing initial packet of length " << data_len; |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 903 | *written = data_len; // Fake successful writing also to writer. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 904 | return rtc::SR_SUCCESS; |
| 905 | } |
| 906 | } |
| 907 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 908 | return test_base_->DataWritten(this, data, data_len, written, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 909 | }; |
| 910 | |
| 911 | class SSLStreamAdapterTestDTLSFromPEMStrings : public SSLStreamAdapterTestDTLS { |
| 912 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 913 | SSLStreamAdapterTestDTLSFromPEMStrings() |
| 914 | : SSLStreamAdapterTestDTLS(kCERT_PEM, kRSA_PRIVATE_KEY_PEM) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 915 | }; |
| 916 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 917 | // Test fixture for certificate chaining. Server will push more than one |
| 918 | // certificate. |
| 919 | class SSLStreamAdapterTestDTLSCertChain : public SSLStreamAdapterTestDTLS { |
| 920 | public: |
| 921 | SSLStreamAdapterTestDTLSCertChain() : SSLStreamAdapterTestDTLS("", ""){}; |
| 922 | void SetUp() override { |
| 923 | CreateStreams(); |
| 924 | |
| 925 | client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_)); |
| 926 | server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_)); |
| 927 | |
| 928 | // Set up the slots |
| 929 | client_ssl_->SignalEvent.connect( |
| 930 | reinterpret_cast<SSLStreamAdapterTestBase*>(this), |
| 931 | &SSLStreamAdapterTestBase::OnEvent); |
| 932 | server_ssl_->SignalEvent.connect( |
| 933 | reinterpret_cast<SSLStreamAdapterTestBase*>(this), |
| 934 | &SSLStreamAdapterTestBase::OnEvent); |
| 935 | |
| 936 | if (!client_cert_pem_.empty() && !client_private_key_pem_.empty()) { |
| 937 | client_identity_ = rtc::SSLIdentity::FromPEMStrings( |
| 938 | client_private_key_pem_, client_cert_pem_); |
| 939 | } else { |
| 940 | client_identity_ = rtc::SSLIdentity::Generate("client", client_key_type_); |
| 941 | } |
| 942 | |
| 943 | client_ssl_->SetIdentity(client_identity_); |
| 944 | } |
| 945 | }; |
| 946 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 947 | // Basic tests: TLS |
| 948 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 949 | // Test that we can make a handshake work |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 950 | TEST_P(SSLStreamAdapterTestTLS, TestTLSConnect) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 951 | TestHandshake(); |
| 952 | }; |
| 953 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 954 | TEST_P(SSLStreamAdapterTestTLS, GetPeerCertChainWithOneCertificate) { |
| 955 | TestHandshake(); |
| 956 | std::unique_ptr<rtc::SSLCertChain> cert_chain = |
| 957 | client_ssl_->GetPeerSSLCertChain(); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 958 | ASSERT_NE(nullptr, cert_chain); |
| 959 | EXPECT_EQ(1u, cert_chain->GetSize()); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 960 | EXPECT_EQ(cert_chain->Get(0).ToPEMString(), |
| 961 | server_identity_->certificate().ToPEMString()); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | TEST_F(SSLStreamAdapterTestDTLSCertChain, TwoCertHandshake) { |
| 965 | server_identity_ = rtc::SSLIdentity::FromPEMChainStrings( |
| 966 | kRSA_PRIVATE_KEY_PEM, std::string(kCERT_PEM) + kCACert); |
| 967 | server_ssl_->SetIdentity(server_identity_); |
| 968 | TestHandshake(); |
| 969 | std::unique_ptr<rtc::SSLCertChain> peer_cert_chain = |
| 970 | client_ssl_->GetPeerSSLCertChain(); |
| 971 | ASSERT_NE(nullptr, peer_cert_chain); |
| 972 | ASSERT_EQ(2u, peer_cert_chain->GetSize()); |
| 973 | EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString()); |
| 974 | EXPECT_EQ(kCACert, peer_cert_chain->Get(1).ToPEMString()); |
| 975 | } |
| 976 | |
David Benjamin | ea84b6b | 2017-12-01 17:25:45 -0500 | [diff] [blame] | 977 | TEST_F(SSLStreamAdapterTestDTLSCertChain, TwoCertHandshakeWithCopy) { |
| 978 | std::unique_ptr<rtc::SSLIdentity> identity( |
| 979 | rtc::SSLIdentity::FromPEMChainStrings(kRSA_PRIVATE_KEY_PEM, |
| 980 | std::string(kCERT_PEM) + kCACert)); |
| 981 | server_identity_ = identity->GetReference(); |
| 982 | server_ssl_->SetIdentity(server_identity_); |
| 983 | TestHandshake(); |
| 984 | std::unique_ptr<rtc::SSLCertChain> peer_cert_chain = |
| 985 | client_ssl_->GetPeerSSLCertChain(); |
| 986 | ASSERT_NE(nullptr, peer_cert_chain); |
| 987 | ASSERT_EQ(2u, peer_cert_chain->GetSize()); |
| 988 | EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString()); |
| 989 | EXPECT_EQ(kCACert, peer_cert_chain->Get(1).ToPEMString()); |
| 990 | } |
| 991 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 992 | TEST_F(SSLStreamAdapterTestDTLSCertChain, ThreeCertHandshake) { |
| 993 | server_identity_ = rtc::SSLIdentity::FromPEMChainStrings( |
| 994 | kRSA_PRIVATE_KEY_PEM, std::string(kCERT_PEM) + kIntCert1 + kCACert); |
| 995 | server_ssl_->SetIdentity(server_identity_); |
| 996 | TestHandshake(); |
| 997 | std::unique_ptr<rtc::SSLCertChain> peer_cert_chain = |
| 998 | client_ssl_->GetPeerSSLCertChain(); |
| 999 | ASSERT_NE(nullptr, peer_cert_chain); |
| 1000 | ASSERT_EQ(3u, peer_cert_chain->GetSize()); |
| 1001 | EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString()); |
| 1002 | EXPECT_EQ(kIntCert1, peer_cert_chain->Get(1).ToPEMString()); |
| 1003 | EXPECT_EQ(kCACert, peer_cert_chain->Get(2).ToPEMString()); |
| 1004 | } |
| 1005 | |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 1006 | // Test that closing the connection on one side updates the other side. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1007 | TEST_P(SSLStreamAdapterTestTLS, TestTLSClose) { |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 1008 | TestHandshake(); |
| 1009 | client_ssl_->Close(); |
| 1010 | EXPECT_EQ_WAIT(rtc::SS_CLOSED, server_ssl_->GetState(), handshake_wait_); |
| 1011 | }; |
| 1012 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | // Test transfer -- trivial |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1014 | TEST_P(SSLStreamAdapterTestTLS, TestTLSTransfer) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1015 | TestHandshake(); |
| 1016 | TestTransfer(100000); |
| 1017 | }; |
| 1018 | |
| 1019 | // Test read-write after close. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1020 | TEST_P(SSLStreamAdapterTestTLS, ReadWriteAfterClose) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1021 | TestHandshake(); |
| 1022 | TestTransfer(100000); |
| 1023 | client_ssl_->Close(); |
| 1024 | |
| 1025 | rtc::StreamResult rv; |
| 1026 | char block[kBlockSize]; |
| 1027 | size_t dummy; |
| 1028 | |
| 1029 | // It's an error to write after closed. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1030 | rv = client_ssl_->Write(block, sizeof(block), &dummy, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1031 | ASSERT_EQ(rtc::SR_ERROR, rv); |
| 1032 | |
| 1033 | // But after closed read gives you EOS. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1034 | rv = client_ssl_->Read(block, sizeof(block), &dummy, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1035 | ASSERT_EQ(rtc::SR_EOS, rv); |
| 1036 | }; |
| 1037 | |
| 1038 | // Test a handshake with a bogus peer digest |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1039 | TEST_P(SSLStreamAdapterTestTLS, TestTLSBogusDigest) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1040 | SetPeerIdentitiesByDigest(false, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1041 | TestHandshake(false); |
| 1042 | }; |
| 1043 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1044 | TEST_P(SSLStreamAdapterTestTLS, TestTLSDelayedIdentity) { |
| 1045 | TestHandshakeWithDelayedIdentity(true); |
| 1046 | }; |
| 1047 | |
| 1048 | TEST_P(SSLStreamAdapterTestTLS, TestTLSDelayedIdentityWithBogusDigest) { |
| 1049 | TestHandshakeWithDelayedIdentity(false); |
| 1050 | }; |
| 1051 | |
| 1052 | // Test that the correct error is returned when SetPeerCertificateDigest is |
| 1053 | // called with an unknown algorithm. |
| 1054 | TEST_P(SSLStreamAdapterTestTLS, |
| 1055 | TestSetPeerCertificateDigestWithUnknownAlgorithm) { |
| 1056 | unsigned char server_digest[20]; |
| 1057 | size_t server_digest_len; |
| 1058 | bool rv; |
| 1059 | rtc::SSLPeerCertificateDigestError err; |
| 1060 | |
| 1061 | rv = server_identity_->certificate().ComputeDigest( |
| 1062 | rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len); |
| 1063 | ASSERT_TRUE(rv); |
| 1064 | |
| 1065 | rv = client_ssl_->SetPeerCertificateDigest("unknown algorithm", server_digest, |
| 1066 | server_digest_len, &err); |
| 1067 | EXPECT_EQ(rtc::SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM, err); |
| 1068 | EXPECT_FALSE(rv); |
| 1069 | } |
| 1070 | |
| 1071 | // Test that the correct error is returned when SetPeerCertificateDigest is |
| 1072 | // called with an invalid digest length. |
| 1073 | TEST_P(SSLStreamAdapterTestTLS, TestSetPeerCertificateDigestWithInvalidLength) { |
| 1074 | unsigned char server_digest[20]; |
| 1075 | size_t server_digest_len; |
| 1076 | bool rv; |
| 1077 | rtc::SSLPeerCertificateDigestError err; |
| 1078 | |
| 1079 | rv = server_identity_->certificate().ComputeDigest( |
| 1080 | rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len); |
| 1081 | ASSERT_TRUE(rv); |
| 1082 | |
| 1083 | rv = client_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, server_digest, |
| 1084 | server_digest_len - 1, &err); |
| 1085 | EXPECT_EQ(rtc::SSLPeerCertificateDigestError::INVALID_LENGTH, err); |
| 1086 | EXPECT_FALSE(rv); |
| 1087 | } |
| 1088 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1089 | // Test moving a bunch of data |
| 1090 | |
| 1091 | // Basic tests: DTLS |
| 1092 | // Test that we can make a handshake work |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1093 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnect) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1094 | TestHandshake(); |
| 1095 | }; |
| 1096 | |
| 1097 | // Test that we can make a handshake work if the first packet in |
| 1098 | // each direction is lost. This gives us predictable loss |
| 1099 | // rather than having to tune random |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1100 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1101 | SetLoseFirstPacket(true); |
| 1102 | TestHandshake(); |
| 1103 | }; |
| 1104 | |
| 1105 | // Test a handshake with loss and delay |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1106 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacketDelay2s) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1107 | SetLoseFirstPacket(true); |
| 1108 | SetDelay(2000); |
| 1109 | SetHandshakeWait(20000); |
| 1110 | TestHandshake(); |
| 1111 | }; |
| 1112 | |
| 1113 | // Test a handshake with small MTU |
pbos@webrtc.org | 127ca3f | 2014-10-09 07:52:03 +0000 | [diff] [blame] | 1114 | // Disabled due to https://code.google.com/p/webrtc/issues/detail?id=3910 |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1115 | TEST_P(SSLStreamAdapterTestDTLS, DISABLED_TestDTLSConnectWithSmallMtu) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1116 | SetMtu(700); |
| 1117 | SetHandshakeWait(20000); |
| 1118 | TestHandshake(); |
| 1119 | }; |
| 1120 | |
| 1121 | // Test transfer -- trivial |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 1122 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransfer) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1123 | TestHandshake(); |
| 1124 | TestTransfer(100); |
| 1125 | }; |
| 1126 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 1127 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransferWithLoss) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1128 | TestHandshake(); |
| 1129 | SetLoss(10); |
| 1130 | TestTransfer(100); |
| 1131 | }; |
| 1132 | |
jbauch | e488a0d | 2015-11-19 05:17:58 -0800 | [diff] [blame] | 1133 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransferWithDamage) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1134 | SetDamage(); // Must be called first because first packet |
| 1135 | // write happens at end of handshake. |
| 1136 | TestHandshake(); |
| 1137 | TestTransfer(100); |
| 1138 | }; |
| 1139 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1140 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSDelayedIdentity) { |
| 1141 | TestHandshakeWithDelayedIdentity(true); |
| 1142 | }; |
| 1143 | |
| 1144 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSDelayedIdentityWithBogusDigest) { |
| 1145 | TestHandshakeWithDelayedIdentity(false); |
| 1146 | }; |
| 1147 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1148 | // Test DTLS-SRTP with all high ciphers |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1149 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpHigh) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1150 | std::vector<int> high; |
| 1151 | high.push_back(rtc::SRTP_AES128_CM_SHA1_80); |
| 1152 | SetDtlsSrtpCryptoSuites(high, true); |
| 1153 | SetDtlsSrtpCryptoSuites(high, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1154 | TestHandshake(); |
| 1155 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1156 | int client_cipher; |
| 1157 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1158 | int server_cipher; |
| 1159 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1160 | |
| 1161 | ASSERT_EQ(client_cipher, server_cipher); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1162 | ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_80); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1163 | }; |
| 1164 | |
| 1165 | // Test DTLS-SRTP with all low ciphers |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1166 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpLow) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1167 | std::vector<int> low; |
| 1168 | low.push_back(rtc::SRTP_AES128_CM_SHA1_32); |
| 1169 | SetDtlsSrtpCryptoSuites(low, true); |
| 1170 | SetDtlsSrtpCryptoSuites(low, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1171 | TestHandshake(); |
| 1172 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1173 | int client_cipher; |
| 1174 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1175 | int server_cipher; |
| 1176 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1177 | |
| 1178 | ASSERT_EQ(client_cipher, server_cipher); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1179 | ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_32); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1180 | }; |
| 1181 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1182 | // Test DTLS-SRTP with a mismatch -- should not converge |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1183 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpHighLow) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1184 | std::vector<int> high; |
| 1185 | high.push_back(rtc::SRTP_AES128_CM_SHA1_80); |
| 1186 | std::vector<int> low; |
| 1187 | low.push_back(rtc::SRTP_AES128_CM_SHA1_32); |
| 1188 | SetDtlsSrtpCryptoSuites(high, true); |
| 1189 | SetDtlsSrtpCryptoSuites(low, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1190 | TestHandshake(); |
| 1191 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1192 | int client_cipher; |
| 1193 | ASSERT_FALSE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1194 | int server_cipher; |
| 1195 | ASSERT_FALSE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1196 | }; |
| 1197 | |
| 1198 | // Test DTLS-SRTP with each side being mixed -- should select high |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1199 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpMixed) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1200 | std::vector<int> mixed; |
| 1201 | mixed.push_back(rtc::SRTP_AES128_CM_SHA1_80); |
| 1202 | mixed.push_back(rtc::SRTP_AES128_CM_SHA1_32); |
| 1203 | SetDtlsSrtpCryptoSuites(mixed, true); |
| 1204 | SetDtlsSrtpCryptoSuites(mixed, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1205 | TestHandshake(); |
| 1206 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1207 | int client_cipher; |
| 1208 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1209 | int server_cipher; |
| 1210 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1211 | |
| 1212 | ASSERT_EQ(client_cipher, server_cipher); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1213 | ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_80); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1214 | }; |
| 1215 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1216 | // Test DTLS-SRTP with all GCM-128 ciphers. |
| 1217 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCM128) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1218 | std::vector<int> gcm128; |
| 1219 | gcm128.push_back(rtc::SRTP_AEAD_AES_128_GCM); |
| 1220 | SetDtlsSrtpCryptoSuites(gcm128, true); |
| 1221 | SetDtlsSrtpCryptoSuites(gcm128, false); |
| 1222 | TestHandshake(); |
| 1223 | |
| 1224 | int client_cipher; |
| 1225 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1226 | int server_cipher; |
| 1227 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
| 1228 | |
| 1229 | ASSERT_EQ(client_cipher, server_cipher); |
| 1230 | ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_128_GCM); |
| 1231 | }; |
| 1232 | |
| 1233 | // Test DTLS-SRTP with all GCM-256 ciphers. |
| 1234 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCM256) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1235 | std::vector<int> gcm256; |
| 1236 | gcm256.push_back(rtc::SRTP_AEAD_AES_256_GCM); |
| 1237 | SetDtlsSrtpCryptoSuites(gcm256, true); |
| 1238 | SetDtlsSrtpCryptoSuites(gcm256, false); |
| 1239 | TestHandshake(); |
| 1240 | |
| 1241 | int client_cipher; |
| 1242 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1243 | int server_cipher; |
| 1244 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
| 1245 | |
| 1246 | ASSERT_EQ(client_cipher, server_cipher); |
| 1247 | ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_256_GCM); |
| 1248 | }; |
| 1249 | |
| 1250 | // Test DTLS-SRTP with mixed GCM-128/-256 ciphers -- should not converge. |
| 1251 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCMMismatch) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1252 | std::vector<int> gcm128; |
| 1253 | gcm128.push_back(rtc::SRTP_AEAD_AES_128_GCM); |
| 1254 | std::vector<int> gcm256; |
| 1255 | gcm256.push_back(rtc::SRTP_AEAD_AES_256_GCM); |
| 1256 | SetDtlsSrtpCryptoSuites(gcm128, true); |
| 1257 | SetDtlsSrtpCryptoSuites(gcm256, false); |
| 1258 | TestHandshake(); |
| 1259 | |
| 1260 | int client_cipher; |
| 1261 | ASSERT_FALSE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1262 | int server_cipher; |
| 1263 | ASSERT_FALSE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
| 1264 | }; |
| 1265 | |
| 1266 | // Test DTLS-SRTP with both GCM-128/-256 ciphers -- should select GCM-256. |
| 1267 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCMMixed) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1268 | std::vector<int> gcmBoth; |
| 1269 | gcmBoth.push_back(rtc::SRTP_AEAD_AES_256_GCM); |
| 1270 | gcmBoth.push_back(rtc::SRTP_AEAD_AES_128_GCM); |
| 1271 | SetDtlsSrtpCryptoSuites(gcmBoth, true); |
| 1272 | SetDtlsSrtpCryptoSuites(gcmBoth, false); |
| 1273 | TestHandshake(); |
| 1274 | |
| 1275 | int client_cipher; |
| 1276 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher)); |
| 1277 | int server_cipher; |
| 1278 | ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher)); |
| 1279 | |
| 1280 | ASSERT_EQ(client_cipher, server_cipher); |
| 1281 | ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_256_GCM); |
| 1282 | }; |
| 1283 | |
| 1284 | // Test SRTP cipher suite lengths. |
| 1285 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpKeyAndSaltLengths) { |
| 1286 | int key_len; |
| 1287 | int salt_len; |
| 1288 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1289 | ASSERT_FALSE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_INVALID_CRYPTO_SUITE, |
| 1290 | &key_len, &salt_len)); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1291 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1292 | ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AES128_CM_SHA1_32, |
| 1293 | &key_len, &salt_len)); |
| 1294 | ASSERT_EQ(128 / 8, key_len); |
| 1295 | ASSERT_EQ(112 / 8, salt_len); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1296 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1297 | ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AES128_CM_SHA1_80, |
| 1298 | &key_len, &salt_len)); |
| 1299 | ASSERT_EQ(128 / 8, key_len); |
| 1300 | ASSERT_EQ(112 / 8, salt_len); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1301 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1302 | ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AEAD_AES_128_GCM, |
| 1303 | &key_len, &salt_len)); |
| 1304 | ASSERT_EQ(128 / 8, key_len); |
| 1305 | ASSERT_EQ(96 / 8, salt_len); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1306 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1307 | ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AEAD_AES_256_GCM, |
| 1308 | &key_len, &salt_len)); |
| 1309 | ASSERT_EQ(256 / 8, key_len); |
| 1310 | ASSERT_EQ(96 / 8, salt_len); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1311 | }; |
| 1312 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1313 | // Test an exporter |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1314 | TEST_P(SSLStreamAdapterTestDTLS, TestDTLSExporter) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1315 | TestHandshake(); |
| 1316 | unsigned char client_out[20]; |
| 1317 | unsigned char server_out[20]; |
| 1318 | |
| 1319 | bool result; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1320 | result = ExportKeyingMaterial(kExporterLabel, kExporterContext, |
| 1321 | kExporterContextLen, true, true, client_out, |
| 1322 | sizeof(client_out)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1323 | ASSERT_TRUE(result); |
| 1324 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1325 | result = ExportKeyingMaterial(kExporterLabel, kExporterContext, |
| 1326 | kExporterContextLen, true, false, server_out, |
| 1327 | sizeof(server_out)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1328 | ASSERT_TRUE(result); |
| 1329 | |
| 1330 | ASSERT_TRUE(!memcmp(client_out, server_out, sizeof(client_out))); |
| 1331 | } |
| 1332 | |
| 1333 | // Test not yet valid certificates are not rejected. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1334 | TEST_P(SSLStreamAdapterTestDTLS, TestCertNotYetValid) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1335 | long one_day = 60 * 60 * 24; |
| 1336 | // Make the certificates not valid until one day later. |
| 1337 | ResetIdentitiesWithValidity(one_day, one_day); |
| 1338 | TestHandshake(); |
| 1339 | } |
| 1340 | |
| 1341 | // Test expired certificates are not rejected. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1342 | TEST_P(SSLStreamAdapterTestDTLS, TestCertExpired) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1343 | long one_day = 60 * 60 * 24; |
| 1344 | // Make the certificates already expired. |
| 1345 | ResetIdentitiesWithValidity(-one_day, -one_day); |
| 1346 | TestHandshake(); |
| 1347 | } |
| 1348 | |
| 1349 | // Test data transfer using certs created from strings. |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 1350 | TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestTransfer) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1351 | TestHandshake(); |
| 1352 | TestTransfer(100); |
| 1353 | } |
| 1354 | |
| 1355 | // Test getting the remote certificate. |
torbjorng | 7593aad | 2015-11-19 12:20:51 -0800 | [diff] [blame] | 1356 | TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestDTLSGetPeerCertificate) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1357 | // Peer certificates haven't been received yet. |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 1358 | ASSERT_FALSE(GetPeerCertificate(true)); |
| 1359 | ASSERT_FALSE(GetPeerCertificate(false)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1360 | |
| 1361 | TestHandshake(); |
| 1362 | |
| 1363 | // The client should have a peer certificate after the handshake. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 1364 | std::unique_ptr<rtc::SSLCertificate> client_peer_cert = |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 1365 | GetPeerCertificate(true); |
| 1366 | ASSERT_TRUE(client_peer_cert); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1367 | |
| 1368 | // It's not kCERT_PEM. |
| 1369 | std::string client_peer_string = client_peer_cert->ToPEMString(); |
| 1370 | ASSERT_NE(kCERT_PEM, client_peer_string); |
| 1371 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1372 | // The server should have a peer certificate after the handshake. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 1373 | std::unique_ptr<rtc::SSLCertificate> server_peer_cert = |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 1374 | GetPeerCertificate(false); |
| 1375 | ASSERT_TRUE(server_peer_cert); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1376 | |
| 1377 | // It's kCERT_PEM |
| 1378 | ASSERT_EQ(kCERT_PEM, server_peer_cert->ToPEMString()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1379 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1380 | |
| 1381 | // Test getting the used DTLS ciphers. |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1382 | // DTLS 1.2 enabled for neither client nor server -> DTLS 1.0 will be used. |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1383 | TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuite) { |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1384 | SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_10, rtc::SSL_PROTOCOL_DTLS_10); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1385 | TestHandshake(); |
| 1386 | |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1387 | int client_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1388 | ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher)); |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1389 | int server_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1390 | ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher)); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1391 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1392 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true)); |
| 1393 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false)); |
| 1394 | |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1395 | ASSERT_EQ(client_cipher, server_cipher); |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1396 | ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher( |
| 1397 | server_cipher, ::testing::get<1>(GetParam()).type())); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | // Test getting the used DTLS 1.2 ciphers. |
| 1401 | // DTLS 1.2 enabled for client and server -> DTLS 1.2 will be used. |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1402 | TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Both) { |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1403 | SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_12, rtc::SSL_PROTOCOL_DTLS_12); |
| 1404 | TestHandshake(); |
| 1405 | |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1406 | int client_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1407 | ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher)); |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1408 | int server_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1409 | ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher)); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1410 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1411 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_12, GetSslVersion(true)); |
| 1412 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_12, GetSslVersion(false)); |
| 1413 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1414 | ASSERT_EQ(client_cipher, server_cipher); |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1415 | ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher( |
| 1416 | server_cipher, ::testing::get<1>(GetParam()).type())); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | // DTLS 1.2 enabled for client only -> DTLS 1.0 will be used. |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1420 | TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Client) { |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1421 | SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_10, rtc::SSL_PROTOCOL_DTLS_12); |
| 1422 | TestHandshake(); |
| 1423 | |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1424 | int client_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1425 | ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher)); |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1426 | int server_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1427 | ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher)); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1428 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1429 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true)); |
| 1430 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false)); |
| 1431 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1432 | ASSERT_EQ(client_cipher, server_cipher); |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1433 | ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher( |
| 1434 | server_cipher, ::testing::get<1>(GetParam()).type())); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | // DTLS 1.2 enabled for server only -> DTLS 1.0 will be used. |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1438 | TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Server) { |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1439 | SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_12, rtc::SSL_PROTOCOL_DTLS_10); |
| 1440 | TestHandshake(); |
| 1441 | |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1442 | int client_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1443 | ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher)); |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 1444 | int server_cipher; |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1445 | ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher)); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1446 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1447 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true)); |
| 1448 | ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false)); |
| 1449 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1450 | ASSERT_EQ(client_cipher, server_cipher); |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1451 | ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher( |
| 1452 | server_cipher, ::testing::get<1>(GetParam()).type())); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1453 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1454 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 1455 | // The RSA keysizes here might look strange, why not include the RFC's size |
| 1456 | // 2048?. The reason is test case slowness; testing two sizes to exercise |
| 1457 | // parametrization is sufficient. |
| 1458 | INSTANTIATE_TEST_CASE_P( |
| 1459 | SSLStreamAdapterTestsTLS, |
| 1460 | SSLStreamAdapterTestTLS, |
| 1461 | Combine(Values(rtc::KeyParams::RSA(1024, 65537), |
| 1462 | rtc::KeyParams::RSA(1152, 65537), |
| 1463 | rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)), |
| 1464 | Values(rtc::KeyParams::RSA(1024, 65537), |
| 1465 | rtc::KeyParams::RSA(1152, 65537), |
| 1466 | rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)))); |
| 1467 | INSTANTIATE_TEST_CASE_P( |
| 1468 | SSLStreamAdapterTestsDTLS, |
| 1469 | SSLStreamAdapterTestDTLS, |
| 1470 | Combine(Values(rtc::KeyParams::RSA(1024, 65537), |
| 1471 | rtc::KeyParams::RSA(1152, 65537), |
| 1472 | rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)), |
| 1473 | Values(rtc::KeyParams::RSA(1024, 65537), |
| 1474 | rtc::KeyParams::RSA(1152, 65537), |
| 1475 | rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)))); |