-
Notifications
You must be signed in to change notification settings - Fork 4
/
sessionbuilder.cpp
313 lines (247 loc) · 14 KB
/
sessionbuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "sessionbuilder.h"
#include "untrustedidentityexception.h"
#include "invalidkeyidexception.h"
#include "invalidkeyexception.h"
#include "invalidmessageexception.h"
#include "stalekeyexchangeexception.h"
#include "ratchet/bobaxolotlparameters.h"
#include "ratchet/ratchetingsession.h"
#include "util/medium.h"
#include "util/keyhelper.h"
#include <QtMath>
#include <QDebug>
SessionBuilder::SessionBuilder()
{
}
SessionBuilder::SessionBuilder(QSharedPointer<SessionStore> sessionStore, QSharedPointer<PreKeyStore> preKeyStore, QSharedPointer<SignedPreKeyStore> signedPreKeyStore, QSharedPointer<IdentityKeyStore> identityKeyStore, const AxolotlAddress &remoteAddress)
{
init(sessionStore, preKeyStore, signedPreKeyStore, identityKeyStore, remoteAddress);
}
SessionBuilder::SessionBuilder(QSharedPointer<AxolotlStore> store, const AxolotlAddress &remoteAddress)
{
init(qSharedPointerCast<SessionStore>(store),
qSharedPointerCast<PreKeyStore>(store),
qSharedPointerCast<SignedPreKeyStore>(store),
qSharedPointerCast<IdentityKeyStore>(store),
remoteAddress);
}
void SessionBuilder::init(QSharedPointer<SessionStore> sessionStore, QSharedPointer<PreKeyStore> preKeyStore, QSharedPointer<SignedPreKeyStore> signedPreKeyStore, QSharedPointer<IdentityKeyStore> identityKeyStore, const AxolotlAddress &remoteAddress)
{
this->sessionStore = sessionStore;
this->preKeyStore = preKeyStore;
this->signedPreKeyStore = signedPreKeyStore;
this->identityKeyStore = identityKeyStore;
this->remoteAddress = remoteAddress;
}
ulong SessionBuilder::process(SessionRecord *sessionRecord, QSharedPointer<PreKeyWhisperMessage> message)
{
int messageVersion = message->getMessageVersion();
IdentityKey theirIdentityKey = message->getIdentityKey();
ulong unsignedPreKeyId;
if (!identityKeyStore->isTrustedIdentity(remoteAddress.getName(), theirIdentityKey)) {
throw UntrustedIdentityException(QString("Untrusted identity: %1").arg(remoteAddress.getName()));
}
qDebug() << messageVersion;
switch (messageVersion) {
case 2: unsignedPreKeyId = processV2(sessionRecord, message); break;
case 3: unsignedPreKeyId = processV3(sessionRecord, message); break;
default: throw InvalidMessageException("Unknown version: " + messageVersion);
}
identityKeyStore->saveIdentity(remoteAddress.getName(), theirIdentityKey);
return unsignedPreKeyId;
}
ulong SessionBuilder::processV3(SessionRecord *sessionRecord, QSharedPointer<PreKeyWhisperMessage> message)
{
if (sessionRecord->hasSessionState(message->getMessageVersion(), message->getBaseKey().serialize())) {
return -1;
}
ECKeyPair ourSignedPreKey = signedPreKeyStore->loadSignedPreKey(message->getSignedPreKeyId()).getKeyPair();
BobAxolotlParameters parameters;
parameters.setTheirBaseKey(message->getBaseKey());
parameters.setTheirIdentityKey(message->getIdentityKey());
parameters.setOurIdentityKey(identityKeyStore->getIdentityKeyPair());
parameters.setOurSignedPreKey(ourSignedPreKey);
parameters.setOurRatchetKey(ourSignedPreKey);
if (message->getPreKeyId() >= 0) {
parameters.setOurOneTimePreKey(preKeyStore->loadPreKey(message->getPreKeyId()).getKeyPair());
} else {
//parameters.setOurOneTimePreKey(NULL);
}
if (!sessionRecord->isFresh()) sessionRecord->archiveCurrentState();
RatchetingSession::initializeSession(sessionRecord->getSessionState(), message->getMessageVersion(), parameters);
sessionRecord->getSessionState()->setLocalRegistrationId(identityKeyStore->getLocalRegistrationId());
sessionRecord->getSessionState()->setRemoteRegistrationId(message->getRegistrationId());
sessionRecord->getSessionState()->setAliceBaseKey(message->getBaseKey().serialize());
if (message->getPreKeyId() != Medium::MAX_VALUE) {
return message->getPreKeyId();
} else {
return -1;
}
}
ulong SessionBuilder::processV2(SessionRecord *sessionRecord, QSharedPointer<PreKeyWhisperMessage> message)
{
if (message->getPreKeyId() < 0) {
throw InvalidKeyIdException("V2 message requires one time prekey id!");
}
if (!preKeyStore->containsPreKey(message->getPreKeyId()) &&
sessionStore->containsSession(remoteAddress))
{
return -1;
}
ECKeyPair ourPreKey = preKeyStore->loadPreKey(message->getPreKeyId()).getKeyPair();
BobAxolotlParameters parameters;
parameters.setOurIdentityKey(identityKeyStore->getIdentityKeyPair());
parameters.setOurSignedPreKey(ourPreKey);
parameters.setOurRatchetKey(ourPreKey);
//parameters.setOurOneTimePreKey(NULL);
parameters.setTheirIdentityKey(message->getIdentityKey());
parameters.setTheirBaseKey(message->getBaseKey());
if (!sessionRecord->isFresh()) sessionRecord->archiveCurrentState();
RatchetingSession::initializeSession(sessionRecord->getSessionState(), message->getMessageVersion(), parameters);
sessionRecord->getSessionState()->setLocalRegistrationId(identityKeyStore->getLocalRegistrationId());
sessionRecord->getSessionState()->setRemoteRegistrationId(message->getRegistrationId());
sessionRecord->getSessionState()->setAliceBaseKey(message->getBaseKey().serialize());
if (message->getPreKeyId() != Medium::MAX_VALUE) {
return message->getPreKeyId();
} else {
return -1;
}
}
void SessionBuilder::process(const PreKeyBundle &preKey)
{
if (!identityKeyStore->isTrustedIdentity(remoteAddress.getName(), preKey.getIdentityKey())) {
throw UntrustedIdentityException(QString("Untrusted identity: %1").arg(remoteAddress.getName()));
}
if (!preKey.getSignedPreKey().serialize().isEmpty() &&
!Curve::verifySignature(preKey.getSignedPreKey(),
preKey.getIdentityKey().getPublicKey().serialize(),
preKey.getSignedPreKeySignature()))
{
qWarning() << preKey.getIdentityKey().getPublicKey().serialize().toHex();
qWarning() << preKey.getSignedPreKey().serialize().toHex();
qWarning() << preKey.getSignedPreKeySignature().toHex();
throw InvalidKeyException("Invalid signature on device key!");
}
if (preKey.getSignedPreKey().serialize().isEmpty() && preKey.getPreKey().serialize().isEmpty()) {
throw InvalidKeyException("Both signed and unsigned prekeys are absent!");
}
bool supportsV3 = !preKey.getSignedPreKey().serialize().isEmpty();
SessionRecord *sessionRecord = sessionStore->loadSession(remoteAddress);
ECKeyPair ourBaseKey = Curve::generateKeyPair();
DjbECPublicKey theirSignedPreKey = supportsV3 ? preKey.getSignedPreKey() : preKey.getPreKey();
DjbECPublicKey theirOneTimePreKey = preKey.getPreKey();
int theirOneTimePreKeyId = theirOneTimePreKey.serialize().isEmpty() ? -1 : preKey.getPreKeyId();
AliceAxolotlParameters parameters;
parameters.setOurBaseKey(ourBaseKey);
parameters.setOurIdentityKey(identityKeyStore->getIdentityKeyPair());
parameters.setTheirIdentityKey(preKey.getIdentityKey());
parameters.setTheirSignedPreKey(theirSignedPreKey);
parameters.setTheirRatchetKey(theirSignedPreKey);
if (supportsV3) {
parameters.setTheirOneTimePreKey(theirOneTimePreKey);
}
if (!sessionRecord->isFresh()) sessionRecord->archiveCurrentState();
RatchetingSession::initializeSession(sessionRecord->getSessionState(),
supportsV3 ? 3 : 2,
parameters);
sessionRecord->getSessionState()->setUnacknowledgedPreKeyMessage(theirOneTimePreKeyId, preKey.getSignedPreKeyId(), ourBaseKey.getPublicKey());
sessionRecord->getSessionState()->setLocalRegistrationId(identityKeyStore->getLocalRegistrationId());
sessionRecord->getSessionState()->setRemoteRegistrationId(preKey.getRegistrationId());
sessionRecord->getSessionState()->setAliceBaseKey(ourBaseKey.getPublicKey().serialize());
sessionStore->storeSession(remoteAddress, sessionRecord);
identityKeyStore->saveIdentity(remoteAddress.getName(), preKey.getIdentityKey());
}
KeyExchangeMessage SessionBuilder::process(QSharedPointer<KeyExchangeMessage> message)
{
if (!identityKeyStore->isTrustedIdentity(remoteAddress.getName(), message->getIdentityKey())) {
throw UntrustedIdentityException(QString("Untrusted identity: %1").arg(remoteAddress.getName()));
}
KeyExchangeMessage responseMessage;
if (message->isInitiate()) responseMessage = processInitiate(message);
else processResponse(message);
return responseMessage;
}
KeyExchangeMessage SessionBuilder::process()
{
int sequence = KeyHelper::getRandomFFFF();
int flags = KeyExchangeMessage::INITIATE_FLAG;
ECKeyPair baseKey = Curve::generateKeyPair();
ECKeyPair ratchetKey = Curve::generateKeyPair();
IdentityKeyPair identityKey = identityKeyStore->getIdentityKeyPair();
QByteArray baseKeySignature = Curve::calculateSignature(identityKey.getPrivateKey(), baseKey.getPublicKey().serialize());
SessionRecord *sessionRecord = sessionStore->loadSession(remoteAddress);
sessionRecord->getSessionState()->setPendingKeyExchange(sequence, baseKey, ratchetKey, identityKey);
sessionStore->storeSession(remoteAddress, sessionRecord);
return KeyExchangeMessage(2, sequence, flags, baseKey.getPublicKey(), baseKeySignature,
ratchetKey.getPublicKey(), identityKey.getPublicKey());
}
KeyExchangeMessage SessionBuilder::processInitiate(QSharedPointer<KeyExchangeMessage> message)
{
int flags = KeyExchangeMessage::RESPONSE_FLAG;
SessionRecord *sessionRecord = sessionStore->loadSession(remoteAddress);
if (message->getVersion() >= 3 &&
!Curve::verifySignature(message->getIdentityKey().getPublicKey(),
message->getBaseKey().serialize(),
message->getBaseKeySignature()))
{
throw InvalidKeyException("Bad signature!");
}
SymmetricAxolotlParameters parameters;
if (!sessionRecord->getSessionState()->hasPendingKeyExchange()) {
parameters.setOurIdentityKey(identityKeyStore->getIdentityKeyPair());
parameters.setOurBaseKey(Curve::generateKeyPair());
parameters.setOurRatchetKey(Curve::generateKeyPair());
} else {
parameters.setOurIdentityKey(sessionRecord->getSessionState()->getPendingKeyExchangeIdentityKey());
parameters.setOurBaseKey(sessionRecord->getSessionState()->getPendingKeyExchangeBaseKey());
parameters.setOurRatchetKey(sessionRecord->getSessionState()->getPendingKeyExchangeRatchetKey());
flags |= KeyExchangeMessage::SIMULTAENOUS_INITIATE_FLAG;
}
parameters.setTheirBaseKey(message->getBaseKey());
parameters.setTheirRatchetKey(message->getRatchetKey());
parameters.setTheirIdentityKey(message->getIdentityKey());
if (!sessionRecord->isFresh()) sessionRecord->archiveCurrentState();
RatchetingSession::initializeSession(sessionRecord->getSessionState(),
qMin(message->getMaxVersion(), CiphertextMessage::CURRENT_VERSION),
parameters);
sessionStore->storeSession(remoteAddress, sessionRecord);
identityKeyStore->saveIdentity(remoteAddress.getName(), message->getIdentityKey());
QByteArray baseKeySignature = Curve::calculateSignature(parameters.getOurIdentityKey().getPrivateKey(),
parameters.getOurBaseKey().getPublicKey().serialize());
return KeyExchangeMessage(sessionRecord->getSessionState()->getSessionVersion(),
message->getSequence(), flags,
parameters.getOurBaseKey().getPublicKey(),
baseKeySignature, parameters.getOurRatchetKey().getPublicKey(),
parameters.getOurIdentityKey().getPublicKey());
}
void SessionBuilder::processResponse(QSharedPointer<KeyExchangeMessage> message)
{
SessionRecord *sessionRecord = sessionStore->loadSession(remoteAddress);
SessionState *sessionState = sessionRecord->getSessionState();
bool hasPendingKeyExchange = sessionState->hasPendingKeyExchange();
bool isSimultaneousInitiateResponse = message->isResponseForSimultaneousInitiate();
if (!hasPendingKeyExchange || sessionState->getPendingKeyExchangeSequence() != message->getSequence()) {
if (!isSimultaneousInitiateResponse) throw StaleKeyExchangeException("");
else return;
}
SymmetricAxolotlParameters parameters;
parameters.setOurBaseKey(sessionRecord->getSessionState()->getPendingKeyExchangeBaseKey());
parameters.setOurRatchetKey(sessionRecord->getSessionState()->getPendingKeyExchangeRatchetKey());
parameters.setOurIdentityKey(sessionRecord->getSessionState()->getPendingKeyExchangeIdentityKey());
parameters.setTheirBaseKey(message->getBaseKey());
parameters.setTheirRatchetKey(message->getRatchetKey());
parameters.setTheirIdentityKey(message->getIdentityKey());
if (!sessionRecord->isFresh()) sessionRecord->archiveCurrentState();
RatchetingSession::initializeSession(sessionRecord->getSessionState(),
qMin(message->getMaxVersion(), CiphertextMessage::CURRENT_VERSION),
parameters);
if (sessionRecord->getSessionState()->getSessionVersion() >= 3 &&
!Curve::verifySignature(message->getIdentityKey().getPublicKey(),
message->getBaseKey().serialize(),
message->getBaseKeySignature()))
{
throw InvalidKeyException("Base key signature doesn't match!");
}
sessionStore->storeSession(remoteAddress, sessionRecord);
identityKeyStore->saveIdentity(remoteAddress.getName(), message->getIdentityKey());
}