blob: 1cfc1228e9e9ede5077f50732c85ed2252959889 [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
Patrik Höglund7aee3d52017-11-15 13:15:17 +010020#include "api/cryptoparams.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070021#include "api/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "p2p/base/sessiondescription.h"
23#include "rtc_base/basictypes.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070024#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#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.org28e20752013-07-10 00:45:36 +000029
30// Forward declaration to avoid pulling in libsrtp headers here
31struct srtp_event_data_t;
mattdr51f29192016-09-28 14:08:46 -070032struct srtp_ctx_t_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
34namespace cricket {
35
Zhi Huangcf990f52017-09-22 12:12:30 -070036// A helper class used to negotiate SDES crypto params.
37// TODO(zhihuang): Find a better name for this class, like "SdesNegotiator".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038class 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 Shieh1218d7a2015-12-05 09:59:56 -080074 bool ResetParams();
75
Zhi Huangcf990f52017-09-22 12:12:30 -070076 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.org28e20752013-07-10 00:45:36 +000082 protected:
83 bool ExpectOffer(ContentSource source);
zhihuange683c682017-08-31 16:00:07 -070084
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 bool StoreParams(const std::vector<CryptoParams>& params,
86 ContentSource source);
zhihuange683c682017-08-31 16:00:07 -070087
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 bool ExpectAnswer(ContentSource source);
zhihuange683c682017-08-31 16:00:07 -070089
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params,
zhihuange683c682017-08-31 16:00:07 -070091 ContentSource source,
92 bool final);
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 bool NegotiateParams(const std::vector<CryptoParams>& answer_params,
95 CryptoParams* selected_params);
zhihuange683c682017-08-31 16:00:07 -070096
Zhi Huangcf990f52017-09-22 12:12:30 -070097 private:
98 bool ApplySendParams(const CryptoParams& send_params);
99
100 bool ApplyRecvParams(const CryptoParams& recv_params);
101
jbauchcb560652016-08-04 05:20:32 -0700102 static bool ParseKeyParams(const std::string& params,
103 uint8_t* key,
104 size_t len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 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 };
jbauchdfcab722017-03-06 00:14:10 -0800129 State state_ = ST_INIT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 std::vector<CryptoParams> offer_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 CryptoParams applied_send_params_;
132 CryptoParams applied_recv_params_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700133 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.org28e20752013-07-10 00:45:36 +0000137};
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139} // namespace cricket
140
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200141#endif // PC_SRTPFILTER_H_