-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUID_message.c
More file actions
446 lines (384 loc) · 13.9 KB
/
UID_message.c
File metadata and controls
446 lines (384 loc) · 13.9 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright (c) 2016-2018. Uniquid Inc. or its affiliates. All Rights Reserved.
*
* License is in the "LICENSE" file accompanying this file.
* See the License for the specific language governing permissions and limitations under the License.
*/
/*
* @file UID_message.c
*
* @date 25/oct/2016
* @author M. Palumbi
*/
/**
* @file UID_message.h
*
* Functions related to the RPC messages
*
*/
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <inttypes.h>
#include "yajl/yajl_gen.h"
#include "yajl/yajl_tree.h"
#include "UID_message.h"
#include "UID_bchainBTC.h"
#include "UID_dispatch.h"
#include "UID_time.h"
#include "UID_utils.h"
/**
* Create the context for the user to talk with the
* provider entity named <destMachine>
*
* look the contracts cache to find contract informations
*
* @param[in] destMachine string holding the name of the provider
* @param[out] ctx pointer to a struct to be filled with the context
* (contract informations and may be encryption context)
* @return 0 == no error
*/
int UID_createChannel(char *destMachine, UID_ClientChannelCtx *channel_ctx)
{
UID_ClientProfile *contract;
if (NULL == (contract = UID_matchProvider(destMachine))) return UID_MSG_NOT_FOUND;
memcpy(&(channel_ctx->contract), contract, sizeof(channel_ctx->contract));
return UID_MSG_OK;
}
/*
* hash the serialization. Can be used for both request and response.
*
* serialization:
* sprintf(serializeData,"%d%s%" PRId64 "", method, params, id) for the request
* sprintf(serializeData,"%d%s%" PRId64 "", error, result, id) for the response
*
* es:
* char serializeData[] = "31param-string1551443586389"
*/
static void hashSerializeddata(int method, char *params, int64_t sID, uint8_t hash[32])
{
char s_method[5] = {0};
size_t l_method = 0;
char s_sID[21] = {0};
size_t l_sID = 0;
size_t l_params = strlen(params);
SHA256_CTX ctx;
l_method = snprintf(s_method, sizeof(s_method), "%d", method);
l_sID = snprintf(s_sID, sizeof(s_sID), "%" PRId64, sID);
// hash the serialized data
UID_hashMessage_init(l_method+l_params+l_sID, &ctx);
UID_hashMessage_update(s_method, l_method, &ctx);
UID_hashMessage_update(params, l_params, &ctx);
UID_hashMessage_update(s_sID, l_sID, &ctx);
UID_hashMessage_final(hash, &ctx);
}
/**
* Format the RPC request message
*
* @param[in] path BIP32 path of the sender address
* @param[in] method the requested RPC method
* @param[in] params the RPC params string
* @param[out] msg pointer to a buffer to be filled with the formatted message
* @param[in,out] size size of the msg buffer
* @param[out] sID pointer to an int64_t variable to be filled with the generated session ID
*
* @return 0 == no error
*/
int UID_formatReqMsg(UID_Bip32Path *path, int method, char *params, uint8_t *msg, size_t *size, int64_t *sID)
{
yajl_gen g;
const uint8_t *json;
size_t sz;
int ret;
*sID = UID_getTime();
g = yajl_gen_alloc(NULL);
if (NULL == g) return UID_MSG_GEN_ALLOC_FAIL;
/*
yajl_gen_map_open(g);
yajl_gen_string(g, (uint8_t *)"sender", 6);
yajl_gen_string(g, (uint8_t *)channel_ctx->myid, strlen(channel_ctx->myid));
yajl_gen_string(g, (uint8_t *)"body", 4);
yajl_gen_map_open(g);
yajl_gen_string(g, (uint8_t *)"method", 6);
yajl_gen_integer(g, method);
yajl_gen_string(g, (uint8_t *)"params", 6);
yajl_gen_string(g, (uint8_t *)params, strlen(params));
yajl_gen_string(g, (uint8_t *)"id", 2);
yajl_gen_integer(g, *id);
yajl_gen_map_close(g);
yajl_gen_map_close(g);
yajl_gen_get_buf(g, &json, &sz); //get the msg
//if(sz > size) return
strncpy((char *)msg, (char *)json, *size);
*/
ret = UID_MSG_GEN_FAIL;
if (yajl_gen_status_ok != yajl_gen_string(g, (uint8_t *)params, strlen(params))) goto clean;
if (yajl_gen_status_ok != yajl_gen_get_buf(g, &json, &sz)) goto clean; //format params string
// hash the data
BTC_Signature signature = {0};
uint8_t hash[32] = {0};
hashSerializeddata(method, params, *sID, hash);
ret = UID_signMessageHash(hash, path, signature, sizeof(signature));
if (UID_SIGN_OK != ret) goto clean;
sz = snprintf((char *)msg, *size, "{\"body\":{\"method\":%d,\"params\":%s,\"id\":%" PRId64 "},\"signature\":\"%s\"}", method, json, *sID, signature);
ret = UID_MSG_SMALL_BUFFER;
if (sz >= *size) goto clean;
*size = sz + 1; // add the end of string
ret = UID_MSG_OK;
clean:
yajl_gen_clear(g);
yajl_gen_free(g);
return ret;
}
/**
* Gets as input the first message in a user<>provider communication
* and builds the context for the provider, may be performing
* additional message exchanges needed by
* encription/authentication
*
* Returns the context and the first real RPC message
*
* @param[in] in_msg input message
* @param[in] in_size size in bytes of the input message
* @param[out] channel_ctx pointer to a struct to be filled with the context
* (contract informations and may be encryption context)
* @param[out] first_msg pointer to a buffer to be filled with the message
* @param[in,out] out_size pointer to a size_t variable containing the size of
* the buffer pointed by first_msg and returning the actual
* number of bytes copied in the buffer on return
* @return 0 == no error
*/
int UID_accept_channel(uint8_t *in_msg, size_t in_size, UID_ServerChannelCtx *channel_ctx, uint8_t *first_msg, size_t *out_size)
{
yajl_val node, v;
char *s;
UID_SecurityProfile *contract;
int ret;
const char * _id[] = { "body", "id", (const char *) 0 };
const char * _method[] = { "body", "method", (const char *) 0 };
const char * _params[] = { "body", "params", (const char *) 0 };
const char * _signature[] = { "signature", (const char *) 0 };
// parse message
node = yajl_tree_parse((char *)in_msg, NULL, 0);
if (node == NULL) return UID_MSG_JPARSE_ERROR;
// get the sID
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _id, yajl_t_number);
if (v == NULL) goto clean_return;
int64_t sID = YAJL_GET_INTEGER(v);
int64_t td = UID_getTime() - sID;
ret = UID_MSG_ID_MISMATCH;
if (td > UID_MSG_TWINDOW ) goto clean_return;
if (td < -UID_MSG_TWINDOW ) goto clean_return;
// get the method
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _method, yajl_t_number);
if (v == NULL) goto clean_return;
int method = YAJL_GET_INTEGER(v);
//get the params
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _params, yajl_t_string);
if (v == NULL) goto clean_return;
s = YAJL_GET_STRING(v);
//get the signature
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _signature, yajl_t_string);
if (v == NULL) goto clean_return;
char *b64signature = YAJL_GET_STRING(v);
// recover the address
uint8_t hash[32] = {0};
BTC_Address address = {0};
hashSerializeddata(method, s, sID, hash);
ret = UID_addressFromSignedHash(hash, b64signature, address);
if(UID_SIGN_OK != ret) goto clean_return;
ret = UID_MSG_NO_CONTRACT; // We dont have a contract. we dont send any answer...
contract = UID_matchContract(address);
if (NULL == contract) goto clean_return;
memcpy(&(channel_ctx->contract), contract, sizeof(channel_ctx->contract));
ret = UID_MSG_SMALL_BUFFER;
if (in_size > *out_size) goto clean_return;
memcpy(first_msg, in_msg, in_size);
*out_size = in_size;
ret = UID_MSG_OK;
clean_return:
yajl_tree_free(node);
return ret;
}
/**
* Parses an RPC request message and returns its components
*
* in this implementation msg is a JSON, thus must be a NULL terminated string
*
* @param[in] msg message
* @param[in] size size in byte of the message
* @param[out] method pointer to an int variable to be filled with the method
* @param[out] params pointer to a buffer filled with the RPC parameter string
* @param[in] psize size of the params buffer
* @param[out] sID pointer to an int64_t variable to be filled with the session ID
*
* @return 0 == no error
*/
int UID_parseReqMsg(uint8_t *msg, size_t size, int *method, char *params, size_t psize, int64_t *sID)
{
(void)size;
yajl_val node, v;
int ret;
char *s;
const char * _id[] = { "body", "id", (const char *) 0 };
const char * _method[] = { "body", "method", (const char *) 0 };
const char * _params[] = { "body", "params", (const char *) 0 };
// parse message
node = yajl_tree_parse((char *)msg, NULL, 0);
if (node == NULL) return UID_MSG_JPARSE_ERROR;
// get the sID
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _id, yajl_t_number);
if (v == NULL) goto clean_return;
*sID = YAJL_GET_INTEGER(v);
// get the method
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _method, yajl_t_number);
if (v == NULL) goto clean_return;
*method = YAJL_GET_INTEGER(v);
//get the params
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _params, yajl_t_string);
if (v == NULL) goto clean_return;
s = YAJL_GET_STRING(v);
ret = UID_MSG_SMALL_BUFFER;
if (strlen(s) >= psize) goto clean_return;
strncpy(params, s, psize);
ret = UID_MSG_OK;
clean_return:
if (NULL != node) yajl_tree_free(node);
return ret;
}
/**
* Format the RPC response message
*
* @param[in] path BIP32 path of the sender address
* @param[in] result the RPC result string
* @param[in] error error
* @param[in] sID session ID
* @param[out] msg pointer to a buffer to be filled with the formatted message
* @param[in,out] size size of the msg buffer
*
* @return 0 == no error
*/
int UID_formatRespMsg(UID_Bip32Path *path, char *result, int error, int64_t sID, uint8_t *msg, size_t *size)
{
int ret;
yajl_gen g = NULL;
// build the response
ret = UID_MSG_GEN_ALLOC_FAIL;
g = yajl_gen_alloc(NULL);
if (NULL == g) goto clean_return;
size_t sz;
const uint8_t *json;
ret = UID_MSG_GEN_FAIL;
if (yajl_gen_status_ok != yajl_gen_string(g, (uint8_t *)result, strlen(result))) goto clean_return;
if (yajl_gen_status_ok != yajl_gen_get_buf(g, &json, &sz)) goto clean_return; //get the buffer
// hash the data
BTC_Signature signature = {0};
uint8_t hash[32] = {0};
hashSerializeddata(error, result, sID, hash);
ret = UID_signMessageHash(hash, path, signature, sizeof(signature));
if (UID_SIGN_OK != ret) goto clean_return;
sz = snprintf((char *)msg, *size, "{\"body\":{\"result\":%s,\"error\":%d,\"id\":%" PRId64 "},\"signature\":\"%s\"}", json, error, sID, signature);
ret = UID_MSG_SMALL_BUFFER;
if (sz >= *size) goto clean_return;
*size = sz + 1; // add the end of string
ret = UID_MSG_OK;
clean_return:
if (NULL != g) yajl_gen_free(g);
return ret;
}
// {"sender":"my3CohS9f57yCqNy4yAPbBRqLaAAJ9oqXV","body":{"method":33,"params":"{\"pippa\":\"lapeppa\"}","id":1477550301}}
/**
* Parses an RPC response message and returns its components
*
* @param[in] msg message
* @param[in] size size in byte of the message
* @param[out] sender pointer to a buffer to be filled with the sender address
* @param[in] ssize size of the sender buffer
* @param[out] error pointer to an int variable to be filled with the error
* error represent the status of the RPC transport and not
* of the RPC execution
* @param[out] result pointer to a buffer filled with the RPC result string
* @param[in] rsize size of the result buffer
* @param[out] sID pointer to an int64_t variable to be filled with the session ID
*
* @return 0 == no error
*/
int UID_parseRespMsg(uint8_t *msg, size_t size, char *sender, size_t ssize, int *error, char *result, size_t rsize, int64_t *sID)
{
(void)size;
yajl_val node, v;
int ret;
char *s;
const char * _id[] = { "body", "id", (const char *) 0 };
const char * _error[] = { "body", "error", (const char *) 0 };
const char * _result[] = { "body", "result", (const char *) 0 };
const char * _signature[] = { "signature", (const char *) 0 };
// parse message
node = yajl_tree_parse((char *)msg, NULL, 0);
if (node == NULL) return UID_MSG_JPARSE_ERROR;
// get the sID
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _id, yajl_t_number);
if (v == NULL) goto clean_return;
*sID = YAJL_GET_INTEGER(v);
// get the error
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _error, yajl_t_number);
if (v == NULL) goto clean_return;
*error = YAJL_GET_INTEGER(v);
//get the result
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _result, yajl_t_string);
if (v == NULL) goto clean_return;
s = YAJL_GET_STRING(v);
ret = UID_MSG_SMALL_BUFFER;
if (strlen(s) >= rsize) goto clean_return;
strncpy(result, s, rsize);
//get the signature
ret = UID_MSG_JPARSE_ERROR;
v = yajl_tree_get(node, _signature, yajl_t_string);
if (v == NULL) goto clean_return;
char *b64signature = YAJL_GET_STRING(v);
//recover the sender
ret = UID_MSG_SMALL_BUFFER;
if (sizeof(BTC_Address) > ssize) goto clean_return;
uint8_t hash[32] = {0};
hashSerializeddata(*error, result, *sID, hash);
ret = UID_addressFromSignedHash(hash, b64signature, sender);
if(UID_SIGN_OK != ret) goto clean_return;
ret = UID_MSG_OK;
clean_return:
if (NULL != node) yajl_tree_free(node);
return ret;
}
/**
* Perform all the closing activities for the user
*
* @param[in] ctx pointer to the context
*
* @return 0 == no error
*/
int UID_closeChannel(UID_ClientChannelCtx *ctx)
{
(void)ctx;
return UID_MSG_OK;
}
/**
* Perform all the closing activities for the provider
*
* @param[in] ctx pointer to the context
*
* @return 0 == no error
*/
int UID_closeServerChannel(UID_ServerChannelCtx *ctx)
{
(void)ctx;
return UID_MSG_OK;
}