libkazv
Loading...
Searching...
No Matches
verification-process.hpp
Go to the documentation of this file.
1/*
2 * This file is part of libkazv.
3 * SPDX-FileCopyrightText: 2026 tusooa <tusooa@kazv.moe>
4 * SPDX-License-Identifier: AGPL-3.0-or-later
5 */
6
7#pragma once
8#include <libkazv-config.hpp>
9#include "sas-desc.hpp"
10#include <crypto-util.hpp>
11#include <types.hpp>
12#include <immer/array.hpp>
13
14namespace Kazv
15{
16 namespace VerificationCancelCodes
17 {
18 inline const std::string unexpectedMessage = "m.unexpected_message";
19 inline const std::string userCancel = "m.user";
20 inline const std::string timeout = "m.timeout";
21 inline const std::string unknownMethod = "m.unknown_method";
22 inline const std::string keyMismatch = "m.key_mismatch";
23 inline const std::string invalidMessage = "m.invalid_message";
24 inline const std::string acceptedElsewhere = "m.accepted";
25 inline const std::string mismatchedCommitment = "m.mismatched_commitment";
26 inline const std::string mismatchedSas = "m.mismatched_sas";
27 }
28
30 {
34 immer::array<int> emojiIndices;
36 immer::array<int> decimalCode;
37 friend bool operator==(const SasDisplayCodes &a, const SasDisplayCodes &b) = default;
38 };
39
40 namespace VerificationProcessStates
41 {
42 struct WeRequested {};
43 struct TheyRequested {};
44 struct WeReady {};
45 struct TheyReady {};
46 struct WeStartedSas {};
47 struct TheyStartedSas {};
48 struct WeAcceptedSas {};
49 struct TheyAcceptedSas {};
50 // from ReceivedSasKey to VerifiedBoth, we need to also check confirmedMatch
51 struct ReceivedSasKey {};
52 struct ReceivedSasMac {};
53 struct VerifiedThem {};
54 struct VerifiedBoth {};
55 struct Cancelled
56 {
57 std::string reasonCode;
58 std::string reasonString;
59 };
60 }
61
62 using VerificationProcessState = std::variant<
63 std::monostate,
77 >;
78
80 {
81 enum Party {
84 };
85 struct RequestTag {};
86 inline static const immer::flex_vector<std::string> supportedMethods = {"m.sas.v1"};
87 inline static const immer::flex_vector<std::string> supportedHashes = {"sha256"};
88 inline static const immer::flex_vector<std::string> supportedKeyAgreementProtocols = {"curve25519-hkdf-sha256"};
89 inline static const immer::flex_vector<std::string> supportedMessageAuthenticationCodes = {"hkdf-hmac-sha256.v2"};
90 inline static const immer::flex_vector<std::string> defaultShortAuthenticationString = {"emoji", "decimal"};
91
92 VerificationProcess(std::string ourUserId, std::string ourDeviceId, std::string theirUserId, std::string theirDeviceId, std::string ourDeviceKey);
93
94 std::string ourUserId;
95 std::string ourDeviceId;
96 std::string theirUserId;
97 std::string theirDeviceId;
99 immer::flex_vector<std::pair<Party, Event>> events;
100 bool confirmedMatch{false};
101 std::string ourDeviceKey;
102 std::string theirDeviceKey;
103
107
109 bool finished() const;
110
111 // Modification functions
115 [[nodiscard]] EventList processIncoming(Event e);
116
120 void addOutgoing(Event e);
121
125 void setTheirDeviceKey(std::string key);
126
130 [[nodiscard]] EventList userReady();
131
135 [[nodiscard]] EventList userCancel();
136
140 [[nodiscard]] EventList userConfirmMatch();
141
145 [[nodiscard]] EventList userDenyMatch();
146
150 [[nodiscard]] EventList makeRequest(Timestamp now);
151
155 [[nodiscard]] std::string txnId() const;
156
160 [[nodiscard]] Timestamp requestTimestamp() const;
161
162 private:
163 struct ValidateResult
164 {
166 bool valid;
170 EventList msgs;
171
172 static ValidateResult ok();
173 static ValidateResult error(VerificationProcessState state, EventList msgs);
174 };
180 [[nodiscard]] ValidateResult validateEvent(Event incomingEvent) const;
181
185 [[nodiscard]] EventList toNextState(Event e);
186
190 [[nodiscard]] Event makeEvent(std::string type, json content) const;
191
195 [[nodiscard]] Event makeCancelEvent(std::string code) const;
196
203 [[nodiscard]] Event makeReadyEvent() const;
204
213 [[nodiscard]] Event makeStartEvent(std::string method) const;
214
222 [[nodiscard]] Event makeSasAcceptEvent();
223
231 [[nodiscard]] Event makeSasKeyEvent();
232
240 [[nodiscard]] Event makeSasMacEvent();
241
248 [[nodiscard]] std::pair<Party, Event> getStartEvent() const;
249
251 [[nodiscard]] Party getStartingParty() const;
252
254 [[nodiscard]] json addCommitmentToAcceptContent(json content, Event startEvent);
255
257 [[nodiscard]] bool verifySasCommitment();
258
261 [[nodiscard]] SasDisplayCodes makeDisplayCodes();
262
264 [[nodiscard]] std::string getTheirKey() const;
265
267 [[nodiscard]] std::pair<VerificationProcessState, EventList> verifyKeyMac(Event macEvent);
268
270 void addOutgoingEvents(EventList el);
271 };
272}
Definition event.hpp:21
Definition sas-desc.hpp:16
const std::string mismatchedSas
Definition verification-process.hpp:26
const std::string userCancel
Definition verification-process.hpp:19
const std::string mismatchedCommitment
Definition verification-process.hpp:25
const std::string invalidMessage
Definition verification-process.hpp:23
const std::string timeout
Definition verification-process.hpp:20
const std::string keyMismatch
Definition verification-process.hpp:22
const std::string unknownMethod
Definition verification-process.hpp:21
const std::string acceptedElsewhere
Definition verification-process.hpp:24
const std::string unexpectedMessage
Definition verification-process.hpp:18
Definition location.hpp:10
std::variant< std::monostate, VerificationProcessStates::WeRequested, VerificationProcessStates::TheyRequested, VerificationProcessStates::WeReady, VerificationProcessStates::TheyReady, VerificationProcessStates::WeStartedSas, VerificationProcessStates::TheyStartedSas, VerificationProcessStates::WeAcceptedSas, VerificationProcessStates::TheyAcceptedSas, VerificationProcessStates::ReceivedSasKey, VerificationProcessStates::ReceivedSasMac, VerificationProcessStates::VerifiedThem, VerificationProcessStates::VerifiedBoth, VerificationProcessStates::Cancelled > VerificationProcessState
Definition verification-process.hpp:77
nlohmann::json json
Definition jsonwrap.hpp:20
std::int_fast64_t Timestamp
Definition event.hpp:18
immer::flex_vector< Event > EventList
Definition types.hpp:107
Definition verification-process.hpp:30
immer::array< int > emojiIndices
The indices of emojis to display They can be converted to emojis using the table at https://spec....
Definition verification-process.hpp:34
friend bool operator==(const SasDisplayCodes &a, const SasDisplayCodes &b)=default
immer::array< int > decimalCode
The numbers to display.
Definition verification-process.hpp:36
Definition verification-process.hpp:56
std::string reasonCode
Definition verification-process.hpp:57
std::string reasonString
Definition verification-process.hpp:58
Definition verification-process.hpp:51
Definition verification-process.hpp:52
Definition verification-process.hpp:49
Definition verification-process.hpp:45
Definition verification-process.hpp:43
Definition verification-process.hpp:47
Definition verification-process.hpp:54
Definition verification-process.hpp:53
Definition verification-process.hpp:48
Definition verification-process.hpp:44
Definition verification-process.hpp:42
Definition verification-process.hpp:46
Definition verification-process.hpp:85
Definition verification-process.hpp:80
void addOutgoing(Event e)
Add an outgoing event to the process.
Definition verification-process.cpp:287
void setTheirDeviceKey(std::string key)
Set the device key of the other party.
Definition verification-process.cpp:81
std::string theirDeviceKey
Definition verification-process.hpp:102
static const immer::flex_vector< std::string > supportedKeyAgreementProtocols
Definition verification-process.hpp:88
EventList userCancel()
Signal that the user wants to cancel the verification.
Definition verification-process.cpp:310
EventList userReady()
Signal that the user is ready for an incoming verification.
Definition verification-process.cpp:299
EventList makeRequest(Timestamp now)
Make an outgoing verification request to the other party.
Definition verification-process.cpp:360
Party
Definition verification-process.hpp:81
@ Them
Definition verification-process.hpp:83
@ Us
Definition verification-process.hpp:82
static const immer::flex_vector< std::string > supportedHashes
Definition verification-process.hpp:87
static const immer::flex_vector< std::string > supportedMessageAuthenticationCodes
Definition verification-process.hpp:89
VerificationProcessState state
Definition verification-process.hpp:105
bool confirmedMatch
Definition verification-process.hpp:100
Timestamp requestTimestamp() const
Get the request timestamp for this process.
Definition verification-process.cpp:387
EventList processIncoming(Event e)
Process an incoming event.
Definition verification-process.cpp:177
std::string theirDeviceId
Definition verification-process.hpp:97
std::string theirUserId
Definition verification-process.hpp:96
std::string ourDeviceKey
Definition verification-process.hpp:101
static const immer::flex_vector< std::string > supportedMethods
Definition verification-process.hpp:86
static const immer::flex_vector< std::string > defaultShortAuthenticationString
Definition verification-process.hpp:90
SasDesc sas
Definition verification-process.hpp:104
SasDisplayCodes codes
Definition verification-process.hpp:106
std::string txnId() const
Get the transaction id for this process.
Definition verification-process.cpp:378
EventList userConfirmMatch()
Signal that the user has confirmed that the codes match.
Definition verification-process.cpp:321
std::string ourUserId
Definition verification-process.hpp:94
immer::flex_vector< std::pair< Party, Event > > events
A list of events that have been transmitted between the parties.
Definition verification-process.hpp:99
std::string ourDeviceId
Definition verification-process.hpp:95
EventList userDenyMatch()
Signal that the user has noticed that the codes do not match.
Definition verification-process.cpp:346