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