-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriendreq.cc
412 lines (335 loc) · 13 KB
/
friendreq.cc
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2008-2011, Adrian Thurston <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsnp.h"
#include "packet.h"
#include "string.h"
#include "error.h"
#include "keyagent.h"
#include <mysql/mysql.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <openssl/sha.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
bool checkFriendClaimExists( MYSQL *mysql, User &user, Identity &identity )
{
/* Check to see if there is already a friend claim. */
DbQuery claim( mysql,
"SELECT id "
"FROM friend_claim "
"WHERE user_id = %L AND identity_id = %L",
user.id(), identity.id() );
if ( claim.rows() != 0 )
return true;
return false;
}
bool checkFriendRequestExists( MYSQL *mysql, User &user, Identity &identity )
{
/* Check to see if there is already a friend claim. */
DbQuery claim( mysql,
"SELECT id "
"FROM friend_request "
"WHERE user_id = %L AND identity_id = %L",
user.id(), identity.id() );
if ( claim.rows() != 0 )
return true;
return false;
}
void RelidSet::generate()
{
priv0.allocate( RELID_SIZE );
priv1.allocate( RELID_SIZE );
priv2.allocate( RELID_SIZE );
priv3.allocate( RELID_SIZE );
priv4.allocate( RELID_SIZE );
RAND_bytes( priv0.binary(), priv0.length );
RAND_bytes( priv1.binary(), priv1.length );
RAND_bytes( priv2.binary(), priv2.length );
RAND_bytes( priv3.binary(), priv3.length );
RAND_bytes( priv4.binary(), priv4.length );
}
long long Server::storeGetRelid( User &user, Identity &identity, long long friendClaimId,
int keyPriv, const String &relid )
{
String relidStr = binToBase64( relid );
DbQuery( mysql,
"INSERT INTO get_relid ( "
" user_id, identity_id, friend_claim_id, key_priv, get_relid "
") "
"VALUES( %L, %L, %L, %l, %e )",
user.id(), identity.id(), friendClaimId,
keyPriv, relidStr() );
return lastInsertId( mysql );
}
long long Server::storePutRelid( User &user, Identity &identity, long long friendClaimId,
int keyPriv, const String &relid )
{
String relidStr = binToBase64( relid );
DbQuery( mysql,
"INSERT INTO put_relid ( "
" user_id, identity_id, friend_claim_id, key_priv, put_relid "
") "
"VALUES( %L, %L, %L, %l, %e )",
user.id(), identity.id(), friendClaimId,
keyPriv, relidStr() );
return lastInsertId( mysql );
}
void Server::relidRequest( const String &_user, const String &_iduri )
{
/* a) verifies challenge response
* b) fetches $URI/id.asc (using SSL)
* c) randomly generates a one-way relationship id ($FR-RELID)
* d) randomly generates a one-way request id ($FR-REQID)
* e) encrypts $FR-RELID to friender and signs it
* f) makes message available at $FR-URI/friend-request/$FR-REQID.asc
* g) redirects the user's browser to $URI/return-relid?uri=$FR-URI&reqid=$FR-REQID
*/
User user( mysql, _user );
Identity identity( mysql, _iduri );
if ( user.id() < 0 )
throw InvalidUser( _user );
/* Check to make sure this isn't ourselves. */
String ourId = user.iduri;
if ( strcasecmp( ourId(), identity.iduri ) == 0 )
throw CannotFriendSelf( identity.iduri );
/* FIXME: these should be presented to the user only after the bounce back
* that authenticates the submitter, otherwise anyone can test friendship
* between arbitrary users. */
/* Check for the existence of a friend claim. */
if ( checkFriendClaimExists( mysql, user, identity ) )
throw FriendClaimExists( user.iduri, identity.iduri );
/* Check for the existence of a friend request. */
if ( checkFriendRequestExists( mysql, user, identity ) )
throw FriendRequestExists( user.iduri, identity.iduri );
/* Generate the request id. */
String requestedReqidBin( REQID_SIZE );
RAND_bytes( requestedReqidBin.binary(), requestedReqidBin.length );
String requestedReqid = binToBase64( requestedReqidBin );
/* Generate the relids. */
RelidSet requestedRelidSet;
requestedRelidSet.generate();
/* Encrypt and sign the relationship id. */
String requestedRelidSetPacket = consRelidSet( requestedRelidSet );
fetchPublicKey( identity );
String encPacket = keyAgent.signEncrypt( identity, user, requestedRelidSetPacket );
/* Store the request. */
DbQuery( mysql,
"INSERT INTO relid_request "
"( user_id, identity_id, requested_reqid, requested_relid_set, message ) "
"VALUES( %L, %L, %e, %d, %d )",
user.id(), identity.id(), requestedReqid(),
requestedRelidSetPacket(), requestedRelidSetPacket.length,
encPacket.binary(), encPacket.length );
/* Return the request id for the requester to use. */
bioWrap.returnOkLocal( requestedReqid );
}
void Server::fetchRequestedRelid( const String &requestedReqid )
{
DbQuery request( mysql,
"SELECT message "
"FROM relid_request "
"WHERE requested_reqid = %e",
requestedReqid() );
/* Check for a result. */
if ( request.rows() == 0 )
throw RequestIdInvalid();
MYSQL_ROW row = request.fetchRow();
u_long *lengths = request.fetchLengths();
String msg = Pointer( row[0], lengths[0] );
bioWrap.returnOkServer( msg );
}
void Server::relidResponse( const String &token,
const String &requestedReqid, const String &iduri )
{
/* a) verifies browser is logged in as owner
* b) fetches $FR-URI/id.asc (using SSL)
* c) fetches $FR-URI/friend-request/$FR-REQID.asc
* d) decrypts and verifies $FR-RELID
* e) randomly generates $RELID
* f) randomly generates $REQID
* g) encrypts "$FR-RELID $RELID" to friendee and signs it
* h) makes message available at $URI/request-return/$REQID.asc
* i) redirects the friender to $FR-URI/friend-final?uri=$URI&reqid=$REQID
*/
User user( mysql, User::ByLoginToken(), token );
if ( user.id() < 0 )
throw InvalidUser( token );
Identity identity( mysql, iduri );
String host = Pointer( identity.host() );
String recvEncPacket = fetchRequestedRelidNet( host, requestedReqid );
/* Decrypt and verify the requested_relid. */
fetchPublicKey( identity );
String decrypted = keyAgent.decryptVerify( identity, user, recvEncPacket );
/* Parse the requested relid. */
PacketRelidSet epp( decrypted );
DbQuery( mysql,
"INSERT INTO friend_claim "
"( user_id, identity_id, state ) "
"VALUES ( %L, %L, %l ) ",
user.id(), identity.id(), FC_SENT
);
long long friendClaimId = lastInsertId( mysql );
/* Generate the response request id. */
String responseReqidBin( REQID_SIZE );
RAND_bytes( responseReqidBin.binary(), responseReqidBin.length );
String responseReqid = binToBase64( responseReqidBin );
RelidSet responseRelidSet;
responseRelidSet.generate();
storePutRelid( user, identity, friendClaimId, KEY_PRIV0, epp.priv0 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV1, epp.priv1 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV2, epp.priv2 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV3, epp.priv3 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV4, epp.priv4 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV0, responseRelidSet.priv0 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV1, responseRelidSet.priv1 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV2, responseRelidSet.priv2 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV3, responseRelidSet.priv3 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV4, responseRelidSet.priv4 );
/* Make IDs to identify the sent request. One will be handed to the
* responder. The other to the user agent. */
String peerNotifyReqidBin(REQID_SIZE);
RAND_bytes( peerNotifyReqidBin.binary(), peerNotifyReqidBin.length );
String peerNotifyReqid = binToBase64( peerNotifyReqidBin );
String userNotifyReqidBin(REQID_SIZE);
RAND_bytes( userNotifyReqidBin.binary(), userNotifyReqidBin.length );
String userNotifyReqid = binToBase64( userNotifyReqidBin );
/* Construct the packet we will store and provide to the peer when asked. */
String responseRelidSetPacket = consRelidSet( responseRelidSet );
String relidSetPairPacket = consRelidSetPair( decrypted, responseRelidSetPacket );
String relidResponse = consRelidResponse( peerNotifyReqidBin, relidSetPairPacket );
/* Encrypt and sign using the same credentials. */
String encPacket = keyAgent.signEncrypt( identity, user, relidResponse );
/* Record the response, which will be fetched. */
DbQuery( mysql,
"INSERT INTO relid_response "
"( identity_id, requested_reqid, response_reqid, message ) "
"VALUES ( %L, %e, %e, %d )",
identity.id(), requestedReqid(), responseReqid(),
encPacket.binary(), encPacket.length );
/* The GET relids (ones we generated) always come first. */
String storedRelidSetPair = consRelidSetPair( responseRelidSetPacket, decrypted );
/* Recored the sent request. */
DbQuery( mysql,
"INSERT INTO sent_friend_request "
"( "
" user_id, identity_id, "
" peer_notify_reqid, user_notify_reqid, "
" relid_set_pair "
") "
"VALUES ( %L, %L, %e, %e, %d );",
user.id(), identity.id(),
peerNotifyReqid(), userNotifyReqid(),
storedRelidSetPair(), storedRelidSetPair.length
);
String hash = identity.hash();
String userSite = user.site();
notifAgent.notifFriendRequestSent(
userSite, user.iduri, identity.iduri(),
hash(), userNotifyReqid() );
/* Return the request id for the requester to use. */
bioWrap.returnOkLocal( responseReqid );
}
void Server::fetchResponseRelid( const String &responseReqid )
{
/* Execute the query. */
DbQuery response( mysql,
"SELECT message "
"FROM relid_response "
"WHERE response_reqid = %e",
responseReqid() );
/* Check for a result. */
if ( response.rows() == 0 )
throw RequestIdInvalid();
MYSQL_ROW row = response.fetchRow();
u_long *lengths = response.fetchLengths();
String msg = Pointer( row[0], lengths[0] );
bioWrap.returnOkServer( msg );
}
void Server::friendFinal( const String &_user,
const String &reqid, const String &_iduri )
{
/* a) fetches $URI/request-return/$REQID.asc
* b) decrypts and verifies message, must contain correct $FR-RELID
* c) stores request for friendee to accept/deny
*/
/* FIXME: some checking is missing from this. */
User user( mysql, _user );
if ( user.id() < 0 )
throw InvalidUser( _user );
Identity identity( mysql, _iduri );
String host = Pointer( identity.host() );
String encPacket = fetchResponseRelidNet( host, reqid );
fetchPublicKey( identity );
String decrypted = keyAgent.decryptVerify( identity, user, encPacket );
/* Parse the relid response. */
PacketRelidResponse err( decrypted );
/* Parse the pair. */
PacketRelidSetPair epp( err.relidSetPair );
/* Parse the requested half. */
PacketRelidSet epp1( epp.requested );
/* Parse the returned half. */
PacketRelidSet epp2( epp.returned );
/* FIXME: more verification. */
DbQuery( mysql,
"INSERT INTO friend_claim "
"( user_id, identity_id, state ) "
"VALUES ( %L, %L, %l ) ",
user.id(), identity.id(), FC_RECEIVED
);
long long friendClaimId = lastInsertId( mysql );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV0, epp1.priv0 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV1, epp1.priv1 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV2, epp1.priv2 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV3, epp1.priv3 );
storeGetRelid( user, identity, friendClaimId, KEY_PRIV4, epp1.priv4 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV0, epp2.priv0 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV1, epp2.priv1 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV2, epp2.priv2 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV3, epp2.priv3 );
storePutRelid( user, identity, friendClaimId, KEY_PRIV4, epp2.priv4 );
/* Make a user request id. */
String acceptReqidBin(REQID_SIZE);
RAND_bytes( acceptReqidBin.binary(), acceptReqidBin.length );
String acceptReqid = binToBase64( acceptReqidBin );
String peerNotifyReqid = binToBase64( err.peerNotifyReqid );
DbQuery( mysql,
"INSERT INTO friend_request "
"( "
" user_id, identity_id, "
" accept_reqid, peer_notify_reqid, "
" relid_set_pair "
") "
"VALUES ( %L, %L, %e, %e, %d ) ",
user.id(), identity.id(),
acceptReqid(), peerNotifyReqid(),
decrypted(), decrypted.length
);
String hash = identity.hash();
String userSite = user.site();
notifAgent.notifFriendRequestReceived(
userSite, user.iduri, identity.iduri(), hash(), acceptReqid() );
/* Return the request id for the requester to use. */
bioWrap.returnOkLocal();
}