| 6 | // |
| 7 | #pragma once |
| 8 |
| 9 | #include <string_view> |
| 10 |
| 11 | namespace tde2e_api { |
| 12 |
| 13 | enum class ErrorCode : int { |
| 14 | UnknownError = 100, |
| 15 | Any = 101, |
| 16 | InvalidInput = 102, |
| 17 | InvalidKeyId = 103, |
| 18 | InvalidId = 104, |
| 19 | InvalidBlock = 200, |
| 20 | InvalidBlock_NoChanges = 201, |
| 21 | InvalidBlock_InvalidSignature = 202, |
| 22 | InvalidBlock_HashMismatch = 203, |
| 23 | InvalidBlock_HeightMismatch = 204, |
| 24 | InvalidBlock_InvalidStateProof_Group = 205, |
| 25 | InvalidBlock_InvalidStateProof_Secret = 206, |
| 26 | InvalidBlock_NoPermissions = 207, |
| 27 | InvalidBlock_InvalidGroupState = 208, |
| 28 | InvalidBlock_InvalidSharedSecret = 209, |
| 29 | InvalidCallGroupState_NotParticipant = 300, |
| 30 | InvalidCallGroupState_WrongUserId = 301, |
| 31 | Decrypt_UnknownEpoch = 400, |
| 32 | Encrypt_UnknownEpoch = 401, |
| 33 | InvalidBroadcast_InFuture = 500, |
| 34 | InvalidBroadcast_NotInCommit = 501, |
| 35 | InvalidBroadcast_NotInReveal = 502, |
| 36 | InvalidBroadcast_UnknownUserId = 503, |
| 37 | InvalidBroadcast_AlreadyApplied = 504, |
| 38 | InvalidBroadcast_InvalidReveal = 505, |
| 39 | InvalidBroadcast_InvalidBlockHash = 506, |
| 40 | InvalidCallChannelId = 600, |
| 41 | CallFailed = 601, |
| 42 | CallKeyAlreadyUsed = 602 |
| 43 | }; |
| 44 | inline std::string_view error_string(ErrorCode error_code) { |
| 45 | switch (error_code) { |
| 46 | case ErrorCode::Any: |
| 47 | return ""; |
| 48 | case ErrorCode::UnknownError: |
| 49 | return "UNKNOWN_ERROR"; |
| 50 | case ErrorCode::InvalidInput: |
| 51 | return "INVALID_INPUT"; |
| 52 | case ErrorCode::InvalidKeyId: |
| 53 | return "INVALID_KEY_ID"; |
| 54 | case ErrorCode::InvalidId: |
| 55 | return "INVALID_ID"; |
| 56 | case ErrorCode::InvalidBlock: |
| 57 | return "INVALID_BLOCK"; |
| 58 | case ErrorCode::InvalidBlock_NoChanges: |
| 59 | return "INVALID_BLOCK__NO_CHANGES"; |
| 60 | case ErrorCode::InvalidBlock_InvalidSignature: |
| 61 | return "INVALID_BLOCK__INVALID_SIGNATURE"; |
| 62 | case ErrorCode::InvalidBlock_HashMismatch: |
| 63 | return "INVALID_BLOCK__HASH_MISMATCH"; |
| 64 | case ErrorCode::InvalidBlock_HeightMismatch: |
| 65 | return "INVALID_BLOCK__HEIGHT_MISMATCH"; |
| 66 | case ErrorCode::InvalidBlock_InvalidStateProof_Group: |
| 67 | return "INVALID_BLOCK__INVALID_STATE_PROOF__GROUP"; |
| 68 | case ErrorCode::InvalidBlock_InvalidStateProof_Secret: |
| 69 | return "INVALID_BLOCK__INVALID_STATE_PROOF__SECRET"; |
| 70 | case ErrorCode::InvalidBlock_InvalidGroupState: |
| 71 | return "INVALID_BLOCK__INVALID_GROUP_STATE"; |
| 72 | case ErrorCode::InvalidBlock_InvalidSharedSecret: |
| 73 | return "INVALID_BLOCK__INVALID_SHARED_SECRET"; |
| 74 | case ErrorCode::InvalidBlock_NoPermissions: |
| 75 | return "INVALID_BLOCK__NO_PERMISSIONS"; |
| 76 | case ErrorCode::InvalidCallGroupState_NotParticipant: |
| 77 | return "INVALID_CALL_GROUP_STATE__NOT_PARTICIPANT"; |
| 78 | case ErrorCode::InvalidCallGroupState_WrongUserId: |
| 79 | return "INVALID_CALL_GROUP_STATE__WRONG_USER_ID"; |
| 80 | case ErrorCode::Decrypt_UnknownEpoch: |
| 81 | return "DECRYPT__UNKNOWN_EPOCH"; |
| 82 | case ErrorCode::Encrypt_UnknownEpoch: |
| 83 | return "ENCRYPT__UNKNOWN_EPOCH"; |
| 84 | case ErrorCode::InvalidBroadcast_InFuture: |
| 85 | return "INVALID_BROADCAST__IN_FUTURE"; |
| 86 | case ErrorCode::InvalidBroadcast_NotInCommit: |
| 87 | return "INVALID_BROADCAST__NOT_IN_COMMIT"; |
| 88 | case ErrorCode::InvalidBroadcast_NotInReveal: |
| 89 | return "INVALID_BROADCAST__NOT_IN_REVEAL"; |
| 90 | case ErrorCode::InvalidBroadcast_UnknownUserId: |
| 91 | return "INVALID_BROADCAST__UNKNOWN_USER_ID"; |
| 92 | case ErrorCode::InvalidBroadcast_AlreadyApplied: |
| 93 | return "INVALID_BROADCAST__ALREADY_APPLIED"; |
| 94 | case ErrorCode::InvalidBroadcast_InvalidReveal: |
| 95 | return "INVALID_BROADCAST__INVALID_REVEAL"; |
| 96 | case ErrorCode::InvalidBroadcast_InvalidBlockHash: |
| 97 | return "INVALID_BROADCAST__INVALID_BLOCK_HASH"; |
| 98 | case ErrorCode::CallFailed: |
| 99 | return "CALL_FAILED"; |
| 100 | case ErrorCode::CallKeyAlreadyUsed: |
| 101 | return "CALL_KEY_ALREADY_USED"; |
| 102 | case ErrorCode::InvalidCallChannelId: |
| 103 | return "INVALID_CALL_CHANNEL_ID"; |
| 104 | } |
| 105 | return "UNKNOWN_ERROR"; |
| 106 | } |
| 107 |
| 108 | } // namespace tde2e_api |
| 109 |
| 110 | #include <array> |
| 111 | #include <cstdint> |
| 112 | #include <optional> |
| 113 | #include <string> |
| 114 | #include <tuple> |
| 115 | #include <utility> |
| 116 | #include <variant> |
| 117 | #include <vector> |
| 118 |
| 119 | namespace td { |
| 120 | template <class T> |
| 121 | class Result; |
| 122 | class Status; |
| 123 | } // namespace td |
| 124 |
| 125 | namespace tde2e_api { |
| 126 | // |
| 127 | // Result and Error helper classes |
| 128 | // |
| 129 |
| 130 | struct Error { |
| 131 | ErrorCode code; |
| 132 | std::string message; |
| 133 | }; |
| 134 |
| 135 | template <typename T> |
| 136 | class Result { |
| 137 | public: |
| 138 | Result(const T &value) : data_(value) { |
| 139 | } |
| 140 | Result(T &&value) : data_(std::move(value)) { |
| 141 | } |
| 142 |
| 143 | Result(const Error &error) : data_(error) { |
| 144 | } |
| 145 | Result(Error &&error) : data_(std::move(error)) { |
| 146 | } |
| 147 |
| 148 | Result(td::Result<T> &&value); |
| 149 | Result(td::Status &&status); |
| 150 |
| 151 | // Check if the result is a success |
| 152 | bool is_ok() const { |
| 153 | return std::holds_alternative<T>(data_); |
| 154 | } |
| 155 |
| 156 | T &value() { |
| 157 | return std::get<T>(data_); |
| 158 | } |
| 159 | const T &value() const { |
| 160 | return std::get<T>(data_); |
| 161 | } |
| 162 |
| 163 | Error &error() { |
| 164 | return std::get<Error>(data_); |
| 165 | } |
| 166 | const Error &error() const { |
| 167 | return std::get<Error>(data_); |
| 168 | } |
| 169 |
| 170 | private: |
| 171 | std::variant<T, Error> data_; |
| 172 | }; |
| 173 |
| 174 | // |
| 175 | // Encryption |
| 176 | // |
| 177 |
| 178 | using Int256 = std::array<unsigned char, 32>; |
| 179 | using Int512 = std::array<unsigned char, 64>; |
| 180 | //TODO: strong typed ids |
| 181 | using PublicKey = std::string; |
| 182 | using HandshakeId = std::int64_t; |
| 183 | using LoginId = std::int64_t; |
| 184 | using UserId = std::int64_t; |
| 185 | using AnyKeyId = std::int64_t; |
| 186 | using PrivateKeyId = std::int64_t; |
| 187 | using PublicKeyId = std::int64_t; |
| 188 | using SymmetricKeyId = std::int64_t; |
| 189 | using Bytes = std::string; |
| 190 | using SecureBytes = std::string; |
| 191 | using Slice = std::string_view; |
| 192 | using SecureSlice = std::string_view; |
| 193 | struct Ok {}; |
| 194 |
| 195 | struct EncryptedMessageForMany { |
| 196 | std::vector<std::string> encrypted_headers; |
| 197 | std::string encrypted_message; |
| 198 | }; |
| 199 |
| 200 | Result<Ok> set_log_verbosity_level(int level); |
| 201 |
| 202 | // Keys management |
| 203 | // private keys will stay inside the library when it is possible |
| 204 | // all keys are stored only in memory and should be created or imported before usage |
| 205 | Result<PrivateKeyId> key_generate_private_key(); |
| 206 | Result<PrivateKeyId> key_generate_temporary_private_key(); |
| 207 | Result<SymmetricKeyId> key_derive_secret(PrivateKeyId key_id, Slice tag); |
| 208 | Result<SymmetricKeyId> key_from_bytes(SecureSlice secret); |
| 209 | Result<Bytes> key_to_encrypted_private_key(PrivateKeyId key_id, SymmetricKeyId secret_id); |
| 210 | Result<PrivateKeyId> key_from_encrypted_private_key(Slice encrypted_key, SymmetricKeyId secret_id); |
| 211 | Result<PublicKeyId> key_from_public_key(Slice public_key); |
| 212 | Result<SymmetricKeyId> key_from_ecdh(PrivateKeyId key_id, PublicKeyId other_public_key_id); |
| 213 | Result<PublicKey> key_to_public_key(PrivateKeyId key_id); |
| 214 | Result<SecureBytes> key_to_words(PrivateKeyId key_id); |
| 215 | Result<PrivateKeyId> key_from_words(SecureSlice words); |
| 216 | Result<Int512> key_sign(PrivateKeyId key, Slice data); |
| 217 | Result<Ok> key_destroy(AnyKeyId key_id); |
| 218 | Result<Ok> key_destroy_all(); |
| 219 |
| 220 | // Used to encrypt key between processes, secret_id must be generated with key_from_ecdh |
| 221 | Result<Bytes> key_to_encrypted_private_key_internal(PrivateKeyId key_id, SymmetricKeyId secret_id); |
| 222 | Result<PrivateKeyId> key_from_encrypted_private_key_internal(Slice encrypted_key, SymmetricKeyId secret_id); |
| 223 |
| 224 | Result<EncryptedMessageForMany> encrypt_message_for_many(const std::vector<SymmetricKeyId> &key_ids, |
| 225 | SecureSlice message); |
| 226 | // keeps encrypted_message empty in result |
| 227 | Result<EncryptedMessageForMany> re_encrypt_message_for_many(SymmetricKeyId decrypt_key_id, |
| 228 | const std::vector<SymmetricKeyId> &encrypt_key_ids, |
| 229 | Slice encrypted_header, Slice encrypted_message); |
| 230 | Result<SecureBytes> decrypt_message_for_many(SymmetricKeyId key_id, Slice encrypted_header, Slice encrypted_message); |
| 231 | Result<Bytes> encrypt_message_for_one(SymmetricKeyId key_id, SecureSlice message); |
| 232 | Result<SecureBytes> decrypt_message_for_one(SymmetricKeyId key_id, Slice encrypted_message); |
| 233 |
| 234 | // Utilities for secret key verification/transfer via qr (or any other alternative channel) |
| 235 | // Requires: |
| 236 | // - alternative channel to reliably transfer 'start' message from Bob to Alice (scanning QR is such channel) |
| 237 | // |
| 238 | // Alice: |
| 239 | // - knows shared secret right after the handshake creation |
| 240 | // - Bob's secret key is verified after finish is received |
| 241 | // |
| 242 | // Bob: |
| 243 | // - knows shared secret right after accept is received |
| 244 | // - Alice's secret key is verified after accept is received |
| 245 | // |
| 246 | // Use cases: |
| 247 | // - Transfer of the key from old device to the new device |
| 248 | // - Verification of other person's public key |
| 249 | // - Contact sharing |
| 250 | // |
| 251 | Result<HandshakeId> handshake_create_for_bob(UserId bob_user_id, PrivateKeyId bob_private_key_id); |
| 252 | Result<HandshakeId> handshake_create_for_alice(UserId alice_user_id, PrivateKeyId alice_private_key_id, |
| 253 | UserId bob_user_id, const PublicKey &bob_public_key, Slice start); |
| 254 |
| 255 | Result<Bytes> handshake_bob_send_start(HandshakeId bob_handshake_id); |
| 256 | Result<Bytes> handshake_alice_send_accept(HandshakeId alice_handshake_id); |
| 257 | Result<Bytes> handshake_bob_receive_accept_send_finish(HandshakeId bob_handshake_id, UserId alice_id, |
| 258 | const PublicKey &alice_public_key, Slice accept); |
| 259 | Result<Ok> handshake_alice_receive_finish(HandshakeId alice_handshake_id, Slice finish); |
| 260 | Result<SymmetricKeyId> handshake_get_shared_key_id(HandshakeId handshake_id); |
| 261 | Result<Ok> handshake_destroy(HandshakeId handshake_id); |
| 262 | Result<Ok> handshake_destroy_all(); |
| 263 |
| 264 | // Helper to get QR-code identifier |
| 265 | Result<Bytes> handshake_start_id(Slice start); |
| 266 |
| 267 | // There is wrapper for login |
| 268 | Result<LoginId> login_create_for_bob(); |
| 269 | Result<Bytes> login_bob_send_start(LoginId bob_login_id); |
| 270 | Result<Bytes> login_create_for_alice(UserId alice_user_id, PrivateKeyId alice_private_key_id, Slice start); |
| 271 | Result<PrivateKeyId> login_finish_for_bob(LoginId bob_login_id, UserId alice_user_id, const PublicKey &alice_public_key, |
| 272 | Slice data); |
| 273 | Result<Ok> login_destroy(LoginId login_id); |
| 274 | Result<Ok> login_destroy_all(); |
| 275 |
| 276 | // Personal info |
| 277 |
| 278 | // 1. Each entry stored and signed separately |
| 279 | // 2. Signature is never stored, but always is verified |
| 280 | // 3. It should be possible to save data without signature (but it can't override data with signature) |
| 281 | // 4. We should keep source of entry. Our, Server, Contact+Ts |
| 282 | // 5. We must keep is_contact flag. |
| 283 |
| 284 | template <class T> |
| 285 | struct Entry { |
| 286 | enum Source { Self, Server, Contact }; |
| 287 | Source source; |
| 288 | std::uint32_t timestamp; |
| 289 | T value; |
| 290 |
| 291 | Entry() : source(Self), timestamp(0), value() { |
| 292 | } |
| 293 | Entry(Source source, std::uint32_t timestamp, T &&value) |
| 294 | : source(source), timestamp(timestamp), value(std::move(value)) { |
| 295 | } |
| 296 | }; |
| 297 |
| 298 | template <class T> |
| 299 | struct SignedEntry { |
| 300 | Int512 signature; |
| 301 | std::uint32_t timestamp{0}; |
| 302 | T value; |
| 303 | }; |
| 304 |
| 305 | struct Name { |
| 306 | std::string first_name; |
| 307 | std::string last_name; |
| 308 | }; |
| 309 |
| 310 | struct PhoneNumber { |
| 311 | std::string phone_number; |
| 312 | }; |
| 313 |
| 314 | struct EmojiNonces { |
| 315 | std::optional<Int256> self_nonce; |
| 316 | std::optional<Int256> contact_nonce_hash; |
| 317 | std::optional<Int256> contact_nonce; |
| 318 | }; |
| 319 |
| 320 | struct ContactState { |
| 321 | enum State { Unknown, Contact, NotContact }; |
| 322 | State state{Unknown}; |
| 323 |
| 324 | ContactState() = default; |
| 325 |
| 326 | explicit ContactState(State state) : state(state) { |
| 327 | } |
| 328 | }; |
| 329 |
| 330 | struct Contact { |
| 331 | // Each contact has both public_key and user_id |
| 332 | // how it is stored internally is not clients library user's concern |
| 333 | std::uint32_t generation{0}; |
| 334 |
| 335 | // we always have public key. Essentially contact is defined by its public key |
| 336 | PublicKeyId public_key{}; |
| 337 |
| 338 | // Personal data, signed by contact. If it is not signed, it has no relations to this public key |
| 339 | std::optional<Entry<UserId>> o_user_id; |
| 340 | std::optional<Entry<Name>> o_name; |
| 341 | std::optional<Entry<PhoneNumber>> o_phone_number; |
| 342 |
| 343 | // Personal data save by user_itself |
| 344 | // It always exists and it always given but we keep timestamp just in case |
| 345 | Entry<EmojiNonces> emoji_nonces; |
| 346 | Entry<ContactState> contact_state; |
| 347 | }; |
| 348 |
| 349 | // library can't enforce persistency of changes, because it has no persistent state. |
| 350 | // so it is client's responsibility to ensure that each change will be eventually saved to the server |
| 351 | // NB: it is unclear how protect ourself from server ignoring our queries. |
| 352 |
| 353 | using StorageId = std::int64_t; |
| 354 | using UpdateId = std::int64_t; |
| 355 |
| 356 | struct StorageBlockchainState { |
| 357 | std::string next_suggested_block; |
| 358 | std::vector<std::string> required_proofs; |
| 359 | }; |
| 360 | struct StorageUpdates { |
| 361 | std::vector<std::pair<PublicKeyId, std::optional<Contact>>> updates; |
| 362 | }; |
| 363 |
| 364 | Result<StorageId> storage_create(PrivateKeyId key_id, Slice last_block); |
| 365 | Result<Ok> storage_destroy(StorageId storage_id); |
| 366 | Result<Ok> storage_destroy_all(); |
| 367 |
| 368 | template <class T> |
| 369 | Result<UpdateId> storage_update_contact(StorageId storage_id, PublicKeyId key, SignedEntry<T> signed_entry); |
| 370 | template <class T> |
| 371 | Result<SignedEntry<T>> storage_sign_entry(PrivateKeyId key, Entry<T> entry); |
| 372 |
| 373 | Result<std::optional<Contact>> storage_get_contact(StorageId storage_id, PublicKeyId key); |
| 374 | Result<std::optional<Contact>> storage_get_contact_optimistic(StorageId storage_id, PublicKeyId key); |
| 375 |
| 376 | Result<std::int64_t> storage_blockchain_height(StorageId storage_id); |
| 377 | Result<StorageUpdates> storage_blockchain_apply_block(StorageId storage_id, Slice block); |
| 378 | Result<Ok> storage_blockchain_add_proof(StorageId storage_id, Slice proof, const std::vector<std::string> &keys); |
| 379 |
| 380 | Result<StorageBlockchainState> storage_get_blockchain_state(StorageId); |
| 381 |
| 382 | using CallId = std::int64_t; |
| 383 | using CallChannelId = std::int32_t; |
| 384 | struct CallParticipant { |
| 385 | UserId user_id; |
| 386 | PublicKeyId public_key_id; |
| 387 | int permissions{}; |
| 388 | }; |
| 389 |
| 390 | struct CallState { |
| 391 | int height{}; |
| 392 | std::vector<CallParticipant> participants; |
| 393 | }; |
| 394 |
| 395 | Result<Bytes> call_create_zero_block(PrivateKeyId private_key_id, const CallState &initial_state); |
| 396 | Result<Bytes> call_create_self_add_block(PrivateKeyId private_key_id, Slice previous_block, |
| 397 | const CallParticipant &self); |
| 398 | Result<CallId> call_create(UserId user_id, PrivateKeyId private_key_id, Slice last_block); |
| 399 |
| 400 | Result<std::string> call_describe(CallId call); |
| 401 | Result<std::string> call_describe_block(Slice block); |
| 402 | Result<std::string> call_describe_message(Slice message); |
| 403 |
| 404 | Result<Bytes> call_create_change_state_block(CallId call_id, const CallState &new_state); |
| 405 | Result<Bytes> call_encrypt(CallId call_id, CallChannelId channel_id, SecureSlice message, |
| 406 | size_t unencrypted_prefix_size); |
| 407 | Result<SecureBytes> call_decrypt(CallId call_id, UserId user_id, CallChannelId channel_id, Slice message); |
| 408 |
| 409 | Result<int> call_get_height(CallId call_id); |
| 410 | Result<CallState> call_apply_block(CallId call_id, Slice block); |
| 411 |
| 412 | Result<CallState> call_get_state(CallId call_id); |
| 413 |
| 414 | struct CallVerificationState { |
| 415 | int height{}; |
| 416 | std::optional<Bytes> emoji_hash; |
| 417 | }; |
| 418 | Result<CallVerificationState> call_get_verification_state(CallId call_id); |
| 419 | Result<CallVerificationState> call_receive_inbound_message(CallId call_id, Slice message); |
| 420 |
| 421 | // should be called after: |
| 422 | // - creation |
| 423 | // - call_apply_block |
| 424 | // - call_receive_inbound_messages |
| 425 | Result<std::vector<Bytes>> call_pull_outbound_messages(CallId call_id); |
| 426 |
| 427 | struct CallVerificationWords { |
| 428 | int height{}; |
| 429 | std::vector<std::string> words; |
| 430 | }; |
| 431 |
| 432 | Result<CallVerificationWords> call_get_verification_words(CallId call_id); |
| 433 | Result<Ok> call_destroy(CallId call_id); |
| 434 | Result<Ok> call_destroy_all(); |
| 435 |
| 436 | } // namespace tde2e_api |
| 437 |