blob: 6a3644d5e931cb734457bba71674632bb36a97ee [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_SRTPFILTER_H_
12#define PC_SRTPFILTER_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
Joachim Bauch5b32f232018-03-07 20:02:26 +010020#include "api/array_view.h"
Patrik Höglund7aee3d52017-11-15 13:15:17 +010021#include "api/cryptoparams.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080022#include "api/jsep.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070023#include "api/optional.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080024#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/basictypes.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070026#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#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.org28e20752013-07-10 00:45:36 +000031
32// Forward declaration to avoid pulling in libsrtp headers here
33struct srtp_event_data_t;
mattdr51f29192016-09-28 14:08:46 -070034struct srtp_ctx_t_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36namespace cricket {
37
Zhi Huangcf990f52017-09-22 12:12:30 -070038// A helper class used to negotiate SDES crypto params.
39// TODO(zhihuang): Find a better name for this class, like "SdesNegotiator".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040class 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 Huange818b6e2018-02-22 15:26:27 -080059 // 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.org28e20752013-07-10 00:45:36 +000066 // 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 Shieh1218d7a2015-12-05 09:59:56 -080083 bool ResetParams();
84
Zhi Huangcf990f52017-09-22 12:12:30 -070085 rtc::Optional<int> send_cipher_suite() { return send_cipher_suite_; }
86 rtc::Optional<int> recv_cipher_suite() { return recv_cipher_suite_; }
87
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
jbauchcb560652016-08-04 05:20:32 -0700111 static bool ParseKeyParams(const std::string& params,
112 uint8_t* key,
113 size_t len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 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 };
jbauchdfcab722017-03-06 00:14:10 -0800138 State state_ = ST_INIT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 std::vector<CryptoParams> offer_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 CryptoParams applied_send_params_;
141 CryptoParams applied_recv_params_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700142 rtc::Optional<int> send_cipher_suite_;
143 rtc::Optional<int> recv_cipher_suite_;
Joachim Bauch5b32f232018-03-07 20:02:26 +0100144 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
145 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146};
147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148} // namespace cricket
149
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200150#endif // PC_SRTPFILTER_H_