| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "api/crypto/frame_crypto_transformer.h" |
| 26 | #include "api/scoped_refptr.h" |
| 27 | #include "livekit/peer_connection.h" |
| 28 | #include "livekit/peer_connection_factory.h" |
| 29 | #include "livekit/rtp_receiver.h" |
| 30 | #include "livekit/rtp_sender.h" |
| 31 | #include "livekit/webrtc.h" |
| 32 | #include "rtc_base/synchronization/mutex.h" |
| 33 | #include "rust/cxx.h" |
| 34 | |
| 35 | namespace livekit_ffi { |
| 36 | |
| 37 | struct KeyProviderOptions; |
| 38 | struct EncryptedPacket; |
| 39 | enum class Algorithm : ::std::int32_t; |
| 40 | class RtcFrameCryptorObserverWrapper; |
| 41 | class NativeFrameCryptorObserver; |
| 42 | class PacketTrailerHandler; |
| 43 | |
| 44 | /// Shared secret key for frame encryption. |
| 45 | class KeyProvider { |
| 46 | public: |
| 47 | KeyProvider(KeyProviderOptions options); |
| 48 | ~KeyProvider() {} |
| 49 | |
| 50 | bool set_shared_key(int32_t index, rust::Vec<::std::uint8_t> key) const { |
| 51 | std::vector<uint8_t> key_vec; |
| 52 | std::copy(key.begin(), key.end(), std::back_inserter(key_vec)); |
| 53 | return impl_->SetSharedKey(index, key_vec); |
| 54 | } |
| 55 | |
| 56 | rust::Vec<::std::uint8_t> ratchet_shared_key(int32_t key_index) const { |
| 57 | rust::Vec<uint8_t> vec; |
| 58 | auto data = impl_->RatchetSharedKey(key_index); |
| 59 | if (data.empty()) { |
| 60 | throw std::runtime_error("ratchet_shared_key failed"); |
| 61 | } |
| 62 | |
| 63 | std::move(data.begin(), data.end(), std::back_inserter(vec)); |
| 64 | return vec; |
| 65 | } |
| 66 | |
| 67 | rust::Vec<::std::uint8_t> get_shared_key(int32_t key_index) const { |
| 68 | rust::Vec<uint8_t> vec; |
| 69 | auto data = impl_->ExportSharedKey(key_index); |
| 70 | if (data.empty()) { |
| 71 | throw std::runtime_error("get_shared_key failed"); |
| 72 | } |
| 73 | |
| 74 | std::move(data.begin(), data.end(), std::back_inserter(vec)); |
| 75 | return vec; |
| 76 | } |
| 77 | |
| 78 | /// Set the key at the given index. |
| 79 | bool set_key(const ::rust::String participant_id, |
| 80 | int32_t index, |
| 81 | rust::Vec<::std::uint8_t> key) const { |
| 82 | std::vector<uint8_t> key_vec; |
| 83 | std::copy(key.begin(), key.end(), std::back_inserter(key_vec)); |
| 84 | return impl_->SetKey( |
| 85 | std::string(participant_id.data(), participant_id.size()), index, |
| 86 | key_vec); |
| 87 | } |
| 88 | |
| 89 | rust::Vec<::std::uint8_t> ratchet_key(const ::rust::String participant_id, |
| 90 | int32_t key_index) const { |
| 91 | rust::Vec<uint8_t> vec; |
| 92 | auto data = impl_->RatchetKey( |
| 93 | std::string(participant_id.data(), participant_id.size()), key_index); |
| 94 | if (data.empty()) { |
| 95 | throw std::runtime_error("ratchet_key failed"); |
| 96 | } |
| 97 | |
| 98 | std::move(data.begin(), data.end(), std::back_inserter(vec)); |
| 99 | return vec; |
| 100 | } |
| 101 | |
| 102 | rust::Vec<::std::uint8_t> get_key(const ::rust::String participant_id, |
| 103 | int32_t key_index) const { |
| 104 | rust::Vec<uint8_t> vec; |
| 105 | auto data = impl_->ExportKey( |
| 106 | std::string(participant_id.data(), participant_id.size()), key_index); |
| 107 | if (data.empty()) { |
| 108 | throw std::runtime_error("get_key failed"); |
| 109 | } |
| 110 | |
| 111 | std::move(data.begin(), data.end(), std::back_inserter(vec)); |
| 112 | return vec; |
| 113 | } |
| 114 | |
| 115 | void set_sif_trailer(rust::Vec<::std::uint8_t> trailer) const { |
| 116 | std::vector<uint8_t> trailer_vec; |
| 117 | std::copy(trailer.begin(), trailer.end(), std::back_inserter(trailer_vec)); |
| 118 | impl_->SetSifTrailer(trailer_vec); |
| 119 | } |
| 120 | |
| 121 | webrtc::scoped_refptr<webrtc::KeyProvider> rtc_key_provider() { return impl_; } |
| 122 | |
| 123 | private: |
| 124 | webrtc::scoped_refptr<webrtc::DefaultKeyProviderImpl> impl_; |
| 125 | }; |
| 126 | |
| 127 | class FrameCryptor { |
| 128 | public: |
| 129 | FrameCryptor(std::shared_ptr<RtcRuntime> rtc_runtime, |
| 130 | const std::string participant_id, |
| 131 | webrtc::FrameCryptorTransformer::Algorithm algorithm, |
| 132 | webrtc::scoped_refptr<webrtc::KeyProvider> key_provider, |
| 133 | webrtc::scoped_refptr<webrtc::RtpSenderInterface> sender); |
| 134 | |
| 135 | FrameCryptor(std::shared_ptr<RtcRuntime> rtc_runtime, |
| 136 | const std::string participant_id, |
| 137 | webrtc::FrameCryptorTransformer::Algorithm algorithm, |
| 138 | webrtc::scoped_refptr<webrtc::KeyProvider> key_provider, |
| 139 | webrtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver); |
| 140 | ~FrameCryptor(); |
| 141 | |
| 142 | /// Enable/Disable frame crypto for the sender or receiver. |
| 143 | void set_enabled(bool enabled) const; |
| 144 | |
| 145 | /// Get the enabled state for the sender or receiver. |
| 146 | bool enabled() const; |
| 147 | |
| 148 | /// Set the key index for the sender or receiver. |
| 149 | /// If the key index is not set, the key index will be set to 0. |
| 150 | void set_key_index(int32_t index) const; |
| 151 | |
| 152 | /// Get the key index for the sender or receiver. |
| 153 | int32_t key_index() const; |
| 154 | |
| 155 | rust::String participant_id() const { return participant_id_; } |
| 156 | |
| 157 | void register_observer( |
| 158 | rust::Box<RtcFrameCryptorObserverWrapper> observer) const; |
| 159 | |
| 160 | void unregister_observer() const; |
| 161 | |
| 162 | /// Attach a packet trailer transformer for chained processing. |
| 163 | void set_packet_trailer_handler( |
| 164 | std::shared_ptr<PacketTrailerHandler> handler) const; |
| 165 | |
| 166 | private: |
| 167 | std::shared_ptr<RtcRuntime> rtc_runtime_; |
| 168 | const rust::String participant_id_; |
| 169 | mutable webrtc::Mutex mutex_; |
| 170 | webrtc::scoped_refptr<webrtc::FrameCryptorTransformer> e2ee_transformer_; |
| 171 | webrtc::scoped_refptr<webrtc::KeyProvider> key_provider_; |
| 172 | webrtc::scoped_refptr<webrtc::RtpSenderInterface> sender_; |
| 173 | webrtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver_; |
| 174 | mutable webrtc::scoped_refptr<NativeFrameCryptorObserver> observer_; |
| 175 | mutable webrtc::scoped_refptr<webrtc::FrameTransformerInterface> |
| 176 | chained_transformer_; |
| 177 | }; |
| 178 | |
| 179 | class NativeFrameCryptorObserver |
| 180 | : public webrtc::FrameCryptorTransformerObserver { |
| 181 | public: |
| 182 | NativeFrameCryptorObserver(rust::Box<RtcFrameCryptorObserverWrapper> observer, |
| 183 | const FrameCryptor* fc); |
| 184 | ~NativeFrameCryptorObserver(); |
| 185 | |
| 186 | void OnFrameCryptionStateChanged(const std::string participant_id, |
| 187 | webrtc::FrameCryptionState error) override; |
| 188 | |
| 189 | private: |
| 190 | rust::Box<RtcFrameCryptorObserverWrapper> observer_; |
| 191 | const FrameCryptor* fc_; |
| 192 | }; |
| 193 | |
| 194 | class DataPacketCryptor { |
| 195 | public: |
| 196 | DataPacketCryptor(webrtc::FrameCryptorTransformer::Algorithm algorithm, |
| 197 | webrtc::scoped_refptr<webrtc::KeyProvider> key_provider); |
| 198 | |
| 199 | EncryptedPacket encrypt_data_packet( |
| 200 | const ::rust::String participant_id, |
| 201 | uint32_t key_index, |
| 202 | rust::Vec<::std::uint8_t> data) const; |
| 203 | |
| 204 | rust::Vec<::std::uint8_t> decrypt_data_packet( |
| 205 | const ::rust::String participant_id, |
| 206 | const EncryptedPacket& encrypted_packet) const; |
| 207 | |
| 208 | private: |
| 209 | webrtc::scoped_refptr<webrtc::DataPacketCryptor> data_packet_cryptor_; |
| 210 | }; |
| 211 | |
| 212 | std::shared_ptr<FrameCryptor> new_frame_cryptor_for_rtp_sender( |
| 213 | std::shared_ptr<PeerConnectionFactory> peer_factory, |
| 214 | const ::rust::String participant_id, |
| 215 | Algorithm algorithm, |
| 216 | std::shared_ptr<KeyProvider> key_provider, |
| 217 | std::shared_ptr<RtpSender> sender); |
| 218 | |
| 219 | std::shared_ptr<FrameCryptor> new_frame_cryptor_for_rtp_receiver( |
| 220 | std::shared_ptr<PeerConnectionFactory> peer_factory, |
| 221 | const ::rust::String participant_id, |
| 222 | Algorithm algorithm, |
| 223 | std::shared_ptr<KeyProvider> key_provider, |
| 224 | std::shared_ptr<RtpReceiver> receiver); |
| 225 | |
| 226 | std::shared_ptr<KeyProvider> new_key_provider(KeyProviderOptions options); |
| 227 | |
| 228 | std::shared_ptr<DataPacketCryptor> new_data_packet_cryptor( |
| 229 | Algorithm algorithm, |
| 230 | std::shared_ptr<KeyProvider> key_provider); |
| 231 | |
| 232 | } // namespace livekit_ffi |