Debian Codegraph @

src/encryption/VerificationManager.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "VerificationManager.h"
6
7 #include <chrono>
8
9 #include "Cache.h"
10 #include "ChatPage.h"
11 #include "DeviceVerificationFlow.h"
12 #include "Logging.h"
13 #include "timeline/RoomlistModel.h"
14 #include "timeline/TimelineModel.h"
15 #include "timeline/TimelineViewManager.h"
16
17 VerificationManager::VerificationManager(TimelineViewManager *o)
18 : QObject(o)
19 , rooms_(o->rooms())
20 {
21 instance_ = this;
22 }
23
24 static bool
25 isValidTime(std::optional<uint64_t> t)
26 {
27 if (!t)
28 return false;
29
30 using namespace std::chrono_literals;
31
32 std::chrono::time_point<std::chrono::system_clock> time{std::chrono::milliseconds(*t)};
33 auto diff = std::chrono::system_clock::now() - time;
34
35 return diff < 10min && diff > -5min;
36 }
37
38 void
39 VerificationManager::receivedRoomDeviceVerificationRequest(
40 const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &message,
41 TimelineModel *model)
42 {
43 if (this->isInitialSync_)
44 return;
45
46 if (!isValidTime(message.origin_server_ts))
47 return;
48
49 auto event_id = QString::fromStdString(message.event_id);
50 if (!this->dvList.contains(event_id)) {
51 if (auto flow = DeviceVerificationFlow::NewInRoomVerification(
52 this, model, message.content, QString::fromStdString(message.sender), event_id)) {
53 dvList[event_id] = flow;
54 emit newDeviceVerificationRequest(flow.data());
55 }
56 }
57 }
58
59 void
60 VerificationManager::receivedDeviceVerificationRequest(
61 const mtx::events::msg::KeyVerificationRequest &msg,
62 std::string sender)
63 {
64 if (this->isInitialSync_)
65 return;
66
67 if (!isValidTime(msg.timestamp))
68 return;
69
70 if (!msg.transaction_id)
71 return;
72
73 auto txnid = QString::fromStdString(msg.transaction_id.value());
74 if (!this->dvList.contains(txnid)) {
75 if (auto flow = DeviceVerificationFlow::NewToDeviceVerification(
76 this, msg, QString::fromStdString(sender), txnid)) {
77 dvList[txnid] = flow;
78 emit newDeviceVerificationRequest(flow.data());
79 }
80 }
81 }
82
83 void
84 VerificationManager::receivedDeviceVerificationStart(
85 const mtx::events::msg::KeyVerificationStart &msg,
86 std::string sender)
87 {
88 if (this->isInitialSync_)
89 return;
90
91 // can't do this for start messages sent as to_device...
92 // if (!isValidTime(msg.timestamp))
93 // return;
94
95 if (!msg.transaction_id)
96 return;
97
98 auto txnid = QString::fromStdString(msg.transaction_id.value());
99 if (!this->dvList.contains(txnid)) {
100 if (auto flow = DeviceVerificationFlow::NewToDeviceVerification(
101 this, msg, QString::fromStdString(sender), txnid)) {
102 dvList[txnid] = flow;
103 emit newDeviceVerificationRequest(flow.data());
104 }
105 }
106 }
107
108 void
109 VerificationManager::verifyUser(QString userid)
110 {
111 auto joined_rooms = cache::joinedRooms();
112 auto room_infos = cache::getRoomInfo(joined_rooms);
113
114 for (const std::string &room_id : joined_rooms) {
115 if ((room_infos[QString::fromStdString(room_id)].member_count == 2) &&
116 cache::isRoomEncrypted(room_id)) {
117 auto room_members = cache::roomMembers(room_id);
118 if (std::find(room_members.begin(), room_members.end(), (userid).toStdString()) !=
119 room_members.end()) {
120 if (auto model = rooms_->getRoomById(QString::fromStdString(room_id))) {
121 auto flow =
122 DeviceVerificationFlow::InitiateUserVerification(this, model.data(), userid);
123 std::unique_ptr<QObject> context{new QObject(flow.get())};
124 QObject *pcontext = context.get();
125 connect(
126 model.data(),
127 &TimelineModel::updateFlowEventId,
128 pcontext,
129 [this, flow, context = std::move(context)](std::string eventId) mutable {
130 if (context->parent() == flow.get()) {
131 dvList[QString::fromStdString(eventId)] = flow;
132 context.reset();
133 }
134 });
135 emit newDeviceVerificationRequest(flow.data());
136 return;
137 }
138 }
139 }
140 }
141
142 emit ChatPage::instance()->showNotification(
143 tr("No encrypted private chat found with this user. Create an "
144 "encrypted private chat with this user and try again."));
145 }
146
147 void
148 VerificationManager::removeVerificationFlow(DeviceVerificationFlow *flow)
149 {
150 nhlog::crypto()->debug("Removing verification flow {}", (void *)flow);
151 for (auto it = dvList.keyValueBegin(); it != dvList.keyValueEnd(); ++it) {
152 if (it->second == flow) {
153 dvList.remove((*it).first);
154 return;
155 }
156 }
157 }
158
159 void
160 VerificationManager::verifyDevice(QString userid, QString deviceid)
161 {
162 auto flow = DeviceVerificationFlow::InitiateDeviceVerification(this, userid, {deviceid});
163 this->dvList[flow->transactionId()] = flow;
164 emit newDeviceVerificationRequest(flow.data());
165 }
166
167 void
168 VerificationManager::verifyOneOfDevices(QString userid, std::vector<QString> deviceids)
169 {
170 auto flow =
171 DeviceVerificationFlow::InitiateDeviceVerification(this, userid, std::move(deviceids));
172 this->dvList[flow->transactionId()] = flow;
173 emit newDeviceVerificationRequest(flow.data());
174 }
175
176 #include "moc_VerificationManager.cpp"