// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package livekit.proto;
option csharp_namespace = "LiveKit.Proto";

// TODO(theomonnom): Should FrameCryptor be stateful on the client side and have their own handle?

enum EncryptionType {
    NONE = 0;
    GCM = 1;
    CUSTOM = 2;
}

message FrameCryptor {
    string participant_identity = 1;
    string track_sid = 2;
    int32 key_index = 3;
    bool enabled = 4;
}

message KeyProviderOptions {
    // Only specify if you want to use a shared_key
    optional bytes shared_key = 1; 
    int32 ratchet_window_size = 2;
    bytes ratchet_salt = 3;
    int32 failure_tolerance = 4; // -1 = no tolerence
}

message E2eeOptions {
  EncryptionType encryption_type = 1;
  KeyProviderOptions key_provider_options = 2;
}

enum EncryptionState {
    NEW = 0;
    OK = 1;
    ENCRYPTION_FAILED = 2;
    DECRYPTION_FAILED = 3;
    MISSING_KEY = 4;
    KEY_RATCHETED = 5;
    INTERNAL_ERROR = 6;
}

message E2eeManagerSetEnabledRequest {
    bool enabled = 1;
}
message E2eeManagerSetEnabledResponse {}

message E2eeManagerGetFrameCryptorsRequest {}
message E2eeManagerGetFrameCryptorsResponse {
    repeated FrameCryptor frame_cryptors = 1;
}

message FrameCryptorSetEnabledRequest {
    string participant_identity = 1;
    string track_sid = 2;
    bool enabled = 3;
}
message FrameCryptorSetEnabledResponse { }

message FrameCryptorSetKeyIndexRequest {
    string participant_identity = 1;
    string track_sid = 2;
    int32 key_index = 3;
}
message FrameCryptorSetKeyIndexResponse { }

message SetSharedKeyRequest {
    bytes shared_key = 1;
    int32 key_index = 2;
}
message SetSharedKeyResponse { }

message RatchetSharedKeyRequest {
    int32 key_index = 1;   
}
message RatchetSharedKeyResponse {
    optional bytes new_key = 1;
}

message GetSharedKeyRequest {
    int32 key_index = 1;
}
message GetSharedKeyResponse {
    optional bytes key = 1;
}

message SetKeyRequest {
    string participant_identity = 1;
    bytes key = 2;
    int32 key_index = 3;
}
message SetKeyResponse {}

message RatchetKeyRequest {
    string participant_identity = 1;
    int32 key_index = 2;
}
message RatchetKeyResponse {
    optional bytes new_key = 1;
}

message GetKeyRequest {
    string participant_identity = 1;
    int32 key_index = 2;
}
message GetKeyResponse {
    optional bytes key = 1;
}

message E2eeRequest {
    uint64 room_handle = 1;
    oneof message {
        E2eeManagerSetEnabledRequest manager_set_enabled = 2;
        E2eeManagerGetFrameCryptorsRequest manager_get_frame_cryptors = 3;
        FrameCryptorSetEnabledRequest cryptor_set_enabled = 4;
        FrameCryptorSetKeyIndexRequest cryptor_set_key_index = 5;
        SetSharedKeyRequest set_shared_key = 6;
        RatchetSharedKeyRequest ratchet_shared_key = 7;
        GetSharedKeyRequest get_shared_key = 8;
        SetKeyRequest set_key = 9;
        RatchetKeyRequest ratchet_key = 10;
        GetKeyRequest get_key = 11;
    }
}

message E2eeResponse {
    oneof message {
        E2eeManagerSetEnabledResponse manager_set_enabled = 1;
        E2eeManagerGetFrameCryptorsResponse manager_get_frame_cryptors = 2;
        FrameCryptorSetEnabledResponse cryptor_set_enabled = 3;
        FrameCryptorSetKeyIndexResponse cryptor_set_key_index = 4;
        SetSharedKeyResponse set_shared_key = 5;
        RatchetSharedKeyResponse ratchet_shared_key = 6;
        GetSharedKeyResponse get_shared_key = 7;
        SetKeyResponse set_key = 8;
        RatchetKeyResponse ratchet_key = 9;
        GetKeyResponse get_key = 10;
    }
}
