henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2009 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef PC_SRTPFILTER_H_ |
| 12 | #define PC_SRTPFILTER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | #include <map> |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 16 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 20 | #include "api/array_view.h" |
Patrik Höglund | 7aee3d5 | 2017-11-15 13:15:17 +0100 | [diff] [blame] | 21 | #include "api/cryptoparams.h" |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 22 | #include "api/jsep.h" |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 23 | #include "api/optional.h" |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 24 | #include "pc/sessiondescription.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/basictypes.h" |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 26 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/constructormagic.h" |
| 28 | #include "rtc_base/criticalsection.h" |
| 29 | #include "rtc_base/sslstreamadapter.h" |
| 30 | #include "rtc_base/thread_checker.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 31 | |
| 32 | // Forward declaration to avoid pulling in libsrtp headers here |
| 33 | struct srtp_event_data_t; |
mattdr | 51f2919 | 2016-09-28 14:08:46 -0700 | [diff] [blame] | 34 | struct srtp_ctx_t_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
| 36 | namespace cricket { |
| 37 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 38 | // A helper class used to negotiate SDES crypto params. |
| 39 | // TODO(zhihuang): Find a better name for this class, like "SdesNegotiator". |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | class SrtpFilter { |
| 41 | public: |
| 42 | enum Mode { |
| 43 | PROTECT, |
| 44 | UNPROTECT |
| 45 | }; |
| 46 | enum Error { |
| 47 | ERROR_NONE, |
| 48 | ERROR_FAIL, |
| 49 | ERROR_AUTH, |
| 50 | ERROR_REPLAY, |
| 51 | }; |
| 52 | |
| 53 | SrtpFilter(); |
| 54 | ~SrtpFilter(); |
| 55 | |
| 56 | // Whether the filter is active (i.e. crypto has been properly negotiated). |
| 57 | bool IsActive() const; |
| 58 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 59 | // Handle the offer/answer negotiation of the crypto parameters internally. |
| 60 | // TODO(zhihuang): Make SetOffer/ProvisionalAnswer/Answer private as helper |
| 61 | // methods once start using Process. |
| 62 | bool Process(const std::vector<CryptoParams>& cryptos, |
| 63 | webrtc::SdpType type, |
| 64 | ContentSource source); |
| 65 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | // Indicates which crypto algorithms and keys were contained in the offer. |
| 67 | // offer_params should contain a list of available parameters to use, or none, |
| 68 | // if crypto is not desired. This must be called before SetAnswer. |
| 69 | bool SetOffer(const std::vector<CryptoParams>& offer_params, |
| 70 | ContentSource source); |
| 71 | // Same as SetAnwer. But multiple calls are allowed to SetProvisionalAnswer |
| 72 | // after a call to SetOffer. |
| 73 | bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, |
| 74 | ContentSource source); |
| 75 | // Indicates which crypto algorithms and keys were contained in the answer. |
| 76 | // answer_params should contain the negotiated parameters, which may be none, |
| 77 | // if crypto was not desired or could not be negotiated (and not required). |
| 78 | // This must be called after SetOffer. If crypto negotiation completes |
| 79 | // successfully, this will advance the filter to the active state. |
| 80 | bool SetAnswer(const std::vector<CryptoParams>& answer_params, |
| 81 | ContentSource source); |
| 82 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 83 | bool ResetParams(); |
| 84 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 85 | rtc::Optional<int> send_cipher_suite() { return send_cipher_suite_; } |
| 86 | rtc::Optional<int> recv_cipher_suite() { return recv_cipher_suite_; } |
| 87 | |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 88 | rtc::ArrayView<const uint8_t> send_key() { return send_key_; } |
| 89 | rtc::ArrayView<const uint8_t> recv_key() { return recv_key_; } |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 90 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | protected: |
| 92 | bool ExpectOffer(ContentSource source); |
zhihuang | e683c68 | 2017-08-31 16:00:07 -0700 | [diff] [blame] | 93 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | bool StoreParams(const std::vector<CryptoParams>& params, |
| 95 | ContentSource source); |
zhihuang | e683c68 | 2017-08-31 16:00:07 -0700 | [diff] [blame] | 96 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | bool ExpectAnswer(ContentSource source); |
zhihuang | e683c68 | 2017-08-31 16:00:07 -0700 | [diff] [blame] | 98 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, |
zhihuang | e683c68 | 2017-08-31 16:00:07 -0700 | [diff] [blame] | 100 | ContentSource source, |
| 101 | bool final); |
| 102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | bool NegotiateParams(const std::vector<CryptoParams>& answer_params, |
| 104 | CryptoParams* selected_params); |
zhihuang | e683c68 | 2017-08-31 16:00:07 -0700 | [diff] [blame] | 105 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 106 | private: |
| 107 | bool ApplySendParams(const CryptoParams& send_params); |
| 108 | |
| 109 | bool ApplyRecvParams(const CryptoParams& recv_params); |
| 110 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 111 | static bool ParseKeyParams(const std::string& params, |
| 112 | uint8_t* key, |
| 113 | size_t len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | enum State { |
| 116 | ST_INIT, // SRTP filter unused. |
| 117 | ST_SENTOFFER, // Offer with SRTP parameters sent. |
| 118 | ST_RECEIVEDOFFER, // Offer with SRTP parameters received. |
| 119 | ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. |
| 120 | // Received provisional answer without crypto. |
| 121 | ST_RECEIVEDPRANSWER_NO_CRYPTO, |
| 122 | ST_ACTIVE, // Offer and answer set. |
| 123 | // SRTP filter is active but new parameters are offered. |
| 124 | // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. |
| 125 | ST_SENTUPDATEDOFFER, |
| 126 | // SRTP filter is active but new parameters are received. |
| 127 | // When the answer is set, the state transitions back to ST_ACTIVE. |
| 128 | ST_RECEIVEDUPDATEDOFFER, |
| 129 | // SRTP filter is active but the sent answer is only provisional. |
| 130 | // When the final answer is set, the state transitions to ST_ACTIVE or |
| 131 | // ST_INIT. |
| 132 | ST_SENTPRANSWER, |
| 133 | // SRTP filter is active but the received answer is only provisional. |
| 134 | // When the final answer is set, the state transitions to ST_ACTIVE or |
| 135 | // ST_INIT. |
| 136 | ST_RECEIVEDPRANSWER |
| 137 | }; |
jbauch | dfcab72 | 2017-03-06 00:14:10 -0800 | [diff] [blame] | 138 | State state_ = ST_INIT; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | std::vector<CryptoParams> offer_params_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | CryptoParams applied_send_params_; |
| 141 | CryptoParams applied_recv_params_; |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 142 | rtc::Optional<int> send_cipher_suite_; |
| 143 | rtc::Optional<int> recv_cipher_suite_; |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 144 | rtc::ZeroOnFreeBuffer<uint8_t> send_key_; |
| 145 | rtc::ZeroOnFreeBuffer<uint8_t> recv_key_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | } // namespace cricket |
| 149 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 150 | #endif // PC_SRTPFILTER_H_ |