blob: 5b6c99dcb5f509f65ce7f12e2afbb4d352537c8a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2009 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_SRTP_FILTER_H_
12#define PC_SRTP_FILTER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <list>
15#include <map>
kwiberg31022942016-03-11 14:18:21 -080016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <string>
18#include <vector>
19
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020020#include "absl/types/optional.h"
Joachim Bauch5b32f232018-03-07 20:02:26 +010021#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/crypto_params.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080023#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "pc/session_description.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070025#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/constructor_magic.h"
27#include "rtc_base/critical_section.h"
28#include "rtc_base/ssl_stream_adapter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/thread_checker.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31// Forward declaration to avoid pulling in libsrtp headers here
32struct srtp_event_data_t;
mattdr51f29192016-09-28 14:08:46 -070033struct srtp_ctx_t_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35namespace cricket {
36
Zhi Huangcf990f52017-09-22 12:12:30 -070037// A helper class used to negotiate SDES crypto params.
38// TODO(zhihuang): Find a better name for this class, like "SdesNegotiator".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039class SrtpFilter {
40 public:
Yves Gerey665174f2018-06-19 15:03:05 +020041 enum Mode { PROTECT, UNPROTECT };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042 enum Error {
43 ERROR_NONE,
44 ERROR_FAIL,
45 ERROR_AUTH,
46 ERROR_REPLAY,
47 };
48
49 SrtpFilter();
50 ~SrtpFilter();
51
52 // Whether the filter is active (i.e. crypto has been properly negotiated).
53 bool IsActive() const;
54
Zhi Huange818b6e2018-02-22 15:26:27 -080055 // Handle the offer/answer negotiation of the crypto parameters internally.
56 // TODO(zhihuang): Make SetOffer/ProvisionalAnswer/Answer private as helper
57 // methods once start using Process.
58 bool Process(const std::vector<CryptoParams>& cryptos,
59 webrtc::SdpType type,
60 ContentSource source);
61
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 // Indicates which crypto algorithms and keys were contained in the offer.
63 // offer_params should contain a list of available parameters to use, or none,
64 // if crypto is not desired. This must be called before SetAnswer.
65 bool SetOffer(const std::vector<CryptoParams>& offer_params,
66 ContentSource source);
67 // Same as SetAnwer. But multiple calls are allowed to SetProvisionalAnswer
68 // after a call to SetOffer.
69 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params,
70 ContentSource source);
71 // Indicates which crypto algorithms and keys were contained in the answer.
72 // answer_params should contain the negotiated parameters, which may be none,
73 // if crypto was not desired or could not be negotiated (and not required).
74 // This must be called after SetOffer. If crypto negotiation completes
75 // successfully, this will advance the filter to the active state.
76 bool SetAnswer(const std::vector<CryptoParams>& answer_params,
77 ContentSource source);
78
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -080079 bool ResetParams();
80
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070081 static bool ParseKeyParams(const std::string& params,
82 uint8_t* key,
83 size_t len);
84
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020085 absl::optional<int> send_cipher_suite() { return send_cipher_suite_; }
86 absl::optional<int> recv_cipher_suite() { return recv_cipher_suite_; }
Zhi Huangcf990f52017-09-22 12:12:30 -070087
Joachim Bauch5b32f232018-03-07 20:02:26 +010088 rtc::ArrayView<const uint8_t> send_key() { return send_key_; }
89 rtc::ArrayView<const uint8_t> recv_key() { return recv_key_; }
Zhi Huangcf990f52017-09-22 12:12:30 -070090
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 protected:
92 bool ExpectOffer(ContentSource source);
zhihuange683c682017-08-31 16:00:07 -070093
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 bool StoreParams(const std::vector<CryptoParams>& params,
95 ContentSource source);
zhihuange683c682017-08-31 16:00:07 -070096
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 bool ExpectAnswer(ContentSource source);
zhihuange683c682017-08-31 16:00:07 -070098
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params,
zhihuange683c682017-08-31 16:00:07 -0700100 ContentSource source,
101 bool final);
102
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 bool NegotiateParams(const std::vector<CryptoParams>& answer_params,
104 CryptoParams* selected_params);
zhihuange683c682017-08-31 16:00:07 -0700105
Zhi Huangcf990f52017-09-22 12:12:30 -0700106 private:
107 bool ApplySendParams(const CryptoParams& send_params);
108
109 bool ApplyRecvParams(const CryptoParams& recv_params);
110
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 enum State {
Yves Gerey665174f2018-06-19 15:03:05 +0200112 ST_INIT, // SRTP filter unused.
113 ST_SENTOFFER, // Offer with SRTP parameters sent.
114 ST_RECEIVEDOFFER, // Offer with SRTP parameters received.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto.
116 // Received provisional answer without crypto.
117 ST_RECEIVEDPRANSWER_NO_CRYPTO,
Yves Gerey665174f2018-06-19 15:03:05 +0200118 ST_ACTIVE, // Offer and answer set.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 // SRTP filter is active but new parameters are offered.
120 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT.
121 ST_SENTUPDATEDOFFER,
122 // SRTP filter is active but new parameters are received.
123 // When the answer is set, the state transitions back to ST_ACTIVE.
124 ST_RECEIVEDUPDATEDOFFER,
125 // SRTP filter is active but the sent answer is only provisional.
126 // When the final answer is set, the state transitions to ST_ACTIVE or
127 // ST_INIT.
128 ST_SENTPRANSWER,
129 // SRTP filter is active but the received answer is only provisional.
130 // When the final answer is set, the state transitions to ST_ACTIVE or
131 // ST_INIT.
132 ST_RECEIVEDPRANSWER
133 };
jbauchdfcab722017-03-06 00:14:10 -0800134 State state_ = ST_INIT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 std::vector<CryptoParams> offer_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 CryptoParams applied_send_params_;
137 CryptoParams applied_recv_params_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200138 absl::optional<int> send_cipher_suite_;
139 absl::optional<int> recv_cipher_suite_;
Joachim Bauch5b32f232018-03-07 20:02:26 +0100140 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
141 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142};
143
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144} // namespace cricket
145
Steve Anton10542f22019-01-11 09:11:00 -0800146#endif // PC_SRTP_FILTER_H_