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