diff options
Diffstat (limited to 'src/lib/json/json_helper.c')
-rw-r--r-- | src/lib/json/json_helper.c | 428 |
1 files changed, 428 insertions, 0 deletions
diff --git a/src/lib/json/json_helper.c b/src/lib/json/json_helper.c index b6965e080..5c2f8ae05 100644 --- a/src/lib/json/json_helper.c +++ b/src/lib/json/json_helper.c | |||
@@ -26,6 +26,7 @@ | |||
26 | */ | 26 | */ |
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_json_lib.h" | 28 | #include "gnunet_json_lib.h" |
29 | #include "gnunet_common.h" | ||
29 | 30 | ||
30 | 31 | ||
31 | struct GNUNET_JSON_Specification | 32 | struct GNUNET_JSON_Specification |
@@ -42,6 +43,29 @@ GNUNET_JSON_spec_end () | |||
42 | 43 | ||
43 | 44 | ||
44 | /** | 45 | /** |
46 | * Convert string value to numeric cipher value. | ||
47 | * | ||
48 | * @param cipher_s input string | ||
49 | * @return numeric cipher value | ||
50 | */ | ||
51 | static enum GNUNET_CRYPTO_BlindSignatureAlgorithm | ||
52 | string_to_cipher (const char *cipher_s) | ||
53 | { | ||
54 | if ((0 == strcasecmp (cipher_s, | ||
55 | "RSA")) || | ||
56 | (0 == strcasecmp (cipher_s, | ||
57 | "RSA+age_restricted"))) | ||
58 | return GNUNET_CRYPTO_BSA_RSA; | ||
59 | if ((0 == strcasecmp (cipher_s, | ||
60 | "CS")) || | ||
61 | (0 == strcasecmp (cipher_s, | ||
62 | "CS+age_restricted"))) | ||
63 | return GNUNET_CRYPTO_BSA_CS; | ||
64 | return GNUNET_CRYPTO_BSA_INVALID; | ||
65 | } | ||
66 | |||
67 | |||
68 | /** | ||
45 | * Parse given JSON object to fixed size data | 69 | * Parse given JSON object to fixed size data |
46 | * | 70 | * |
47 | * @param cls closure, NULL | 71 | * @param cls closure, NULL |
@@ -1180,4 +1204,408 @@ GNUNET_JSON_spec_boolean (const char *name, | |||
1180 | } | 1204 | } |
1181 | 1205 | ||
1182 | 1206 | ||
1207 | /** | ||
1208 | * Parse given JSON object to a blinded message. | ||
1209 | * | ||
1210 | * @param cls closure, NULL | ||
1211 | * @param root the json object representing data | ||
1212 | * @param[out] spec where to write the data | ||
1213 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
1214 | */ | ||
1215 | static enum GNUNET_GenericReturnValue | ||
1216 | parse_blinded_message (void *cls, | ||
1217 | json_t *root, | ||
1218 | struct GNUNET_JSON_Specification *spec) | ||
1219 | { | ||
1220 | struct GNUNET_CRYPTO_BlindedMessage **target = spec->ptr; | ||
1221 | struct GNUNET_CRYPTO_BlindedMessage *blinded_message; | ||
1222 | const char *cipher; | ||
1223 | struct GNUNET_JSON_Specification dspec[] = { | ||
1224 | GNUNET_JSON_spec_string ("cipher", | ||
1225 | &cipher), | ||
1226 | GNUNET_JSON_spec_end () | ||
1227 | }; | ||
1228 | const char *emsg; | ||
1229 | unsigned int eline; | ||
1230 | |||
1231 | (void) cls; | ||
1232 | if (GNUNET_OK != | ||
1233 | GNUNET_JSON_parse (root, | ||
1234 | dspec, | ||
1235 | &emsg, | ||
1236 | &eline)) | ||
1237 | { | ||
1238 | GNUNET_break_op (0); | ||
1239 | return GNUNET_SYSERR; | ||
1240 | } | ||
1241 | blinded_message = GNUNET_new (struct GNUNET_CRYPTO_BlindedMessage); | ||
1242 | blinded_message->rc = 1; | ||
1243 | blinded_message->cipher = string_to_cipher (cipher); | ||
1244 | switch (blinded_message->cipher) | ||
1245 | { | ||
1246 | case GNUNET_CRYPTO_BSA_INVALID: | ||
1247 | break; | ||
1248 | case GNUNET_CRYPTO_BSA_RSA: | ||
1249 | { | ||
1250 | struct GNUNET_JSON_Specification ispec[] = { | ||
1251 | GNUNET_JSON_spec_varsize ( | ||
1252 | /* TODO: Change this field name to something | ||
1253 | more generic / pass in as argument. */ | ||
1254 | "rsa_blinded_planchet", | ||
1255 | &blinded_message->details.rsa_blinded_message.blinded_msg, | ||
1256 | &blinded_message->details.rsa_blinded_message.blinded_msg_size), | ||
1257 | GNUNET_JSON_spec_end () | ||
1258 | }; | ||
1259 | |||
1260 | if (GNUNET_OK != | ||
1261 | GNUNET_JSON_parse (root, | ||
1262 | ispec, | ||
1263 | &emsg, | ||
1264 | &eline)) | ||
1265 | { | ||
1266 | GNUNET_break_op (0); | ||
1267 | GNUNET_free (blinded_message); | ||
1268 | return GNUNET_SYSERR; | ||
1269 | } | ||
1270 | *target = blinded_message; | ||
1271 | return GNUNET_OK; | ||
1272 | } | ||
1273 | case GNUNET_CRYPTO_BSA_CS: | ||
1274 | { | ||
1275 | struct GNUNET_JSON_Specification ispec[] = { | ||
1276 | GNUNET_JSON_spec_fixed_auto ( | ||
1277 | "cs_nonce", | ||
1278 | &blinded_message->details.cs_blinded_message.nonce), | ||
1279 | GNUNET_JSON_spec_fixed_auto ( | ||
1280 | "cs_blinded_c0", | ||
1281 | &blinded_message->details.cs_blinded_message.c[0]), | ||
1282 | GNUNET_JSON_spec_fixed_auto ( | ||
1283 | "cs_blinded_c1", | ||
1284 | &blinded_message->details.cs_blinded_message.c[1]), | ||
1285 | GNUNET_JSON_spec_end () | ||
1286 | }; | ||
1287 | |||
1288 | if (GNUNET_OK != | ||
1289 | GNUNET_JSON_parse (root, | ||
1290 | ispec, | ||
1291 | &emsg, | ||
1292 | &eline)) | ||
1293 | { | ||
1294 | GNUNET_break_op (0); | ||
1295 | GNUNET_free (blinded_message); | ||
1296 | return GNUNET_SYSERR; | ||
1297 | } | ||
1298 | *target = blinded_message; | ||
1299 | return GNUNET_OK; | ||
1300 | } | ||
1301 | } | ||
1302 | GNUNET_break_op (0); | ||
1303 | GNUNET_free (blinded_message); | ||
1304 | return GNUNET_SYSERR; | ||
1305 | } | ||
1306 | |||
1307 | /** | ||
1308 | * Cleanup data left from parsing blinded message. | ||
1309 | * | ||
1310 | * @param cls closure, NULL | ||
1311 | * @param[out] spec where to free the data | ||
1312 | */ | ||
1313 | static void | ||
1314 | clean_blinded_message (void *cls, | ||
1315 | struct GNUNET_JSON_Specification *spec) | ||
1316 | { | ||
1317 | struct GNUNET_CRYPTO_BlindedMessage **blinded_message = spec->ptr; | ||
1318 | |||
1319 | (void) cls; | ||
1320 | if (NULL != blinded_message) | ||
1321 | { | ||
1322 | GNUNET_CRYPTO_blinded_message_decref (*blinded_message); | ||
1323 | *blinded_message = NULL; | ||
1324 | } | ||
1325 | } | ||
1326 | |||
1327 | |||
1328 | struct GNUNET_JSON_Specification | ||
1329 | GNUNET_JSON_spec_blinded_message (const char *name, | ||
1330 | struct GNUNET_CRYPTO_BlindedMessage **msg) | ||
1331 | { | ||
1332 | struct GNUNET_JSON_Specification ret = { | ||
1333 | .parser = &parse_blinded_message, | ||
1334 | .cleaner = &clean_blinded_message, | ||
1335 | .cls = NULL, | ||
1336 | .field = name, | ||
1337 | .ptr = msg, | ||
1338 | .ptr_size = 0, | ||
1339 | .size_ptr = NULL | ||
1340 | }; | ||
1341 | |||
1342 | *msg = NULL; | ||
1343 | return ret; | ||
1344 | } | ||
1345 | |||
1346 | |||
1347 | /** | ||
1348 | * Parse given JSON object to a blinded signature. | ||
1349 | * | ||
1350 | * @param cls closure, NULL | ||
1351 | * @param root the json object representing data | ||
1352 | * @param[out] spec where to write the data | ||
1353 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
1354 | */ | ||
1355 | static enum GNUNET_GenericReturnValue | ||
1356 | parse_blinded_sig (void *cls, | ||
1357 | json_t *root, | ||
1358 | struct GNUNET_JSON_Specification *spec) | ||
1359 | { | ||
1360 | struct GNUNET_CRYPTO_BlindedSignature **target = spec->ptr; | ||
1361 | struct GNUNET_CRYPTO_BlindedSignature *blinded_sig; | ||
1362 | const char *cipher; | ||
1363 | struct GNUNET_JSON_Specification dspec[] = { | ||
1364 | GNUNET_JSON_spec_string ("cipher", | ||
1365 | &cipher), | ||
1366 | GNUNET_JSON_spec_end () | ||
1367 | }; | ||
1368 | const char *emsg; | ||
1369 | unsigned int eline; | ||
1370 | |||
1371 | (void) cls; | ||
1372 | if (GNUNET_OK != | ||
1373 | GNUNET_JSON_parse (root, | ||
1374 | dspec, | ||
1375 | &emsg, | ||
1376 | &eline)) | ||
1377 | { | ||
1378 | GNUNET_break_op (0); | ||
1379 | return GNUNET_SYSERR; | ||
1380 | } | ||
1381 | blinded_sig = GNUNET_new (struct GNUNET_CRYPTO_BlindedSignature); | ||
1382 | blinded_sig->cipher = string_to_cipher (cipher); | ||
1383 | blinded_sig->rc = 1; | ||
1384 | switch (blinded_sig->cipher) | ||
1385 | { | ||
1386 | case GNUNET_CRYPTO_BSA_INVALID: | ||
1387 | break; | ||
1388 | case GNUNET_CRYPTO_BSA_RSA: | ||
1389 | { | ||
1390 | struct GNUNET_JSON_Specification ispec[] = { | ||
1391 | GNUNET_JSON_spec_rsa_signature ( | ||
1392 | "blinded_rsa_signature", | ||
1393 | &blinded_sig->details.blinded_rsa_signature), | ||
1394 | GNUNET_JSON_spec_end () | ||
1395 | }; | ||
1396 | |||
1397 | if (GNUNET_OK != | ||
1398 | GNUNET_JSON_parse (root, | ||
1399 | ispec, | ||
1400 | &emsg, | ||
1401 | &eline)) | ||
1402 | { | ||
1403 | GNUNET_break_op (0); | ||
1404 | GNUNET_free (blinded_sig); | ||
1405 | return GNUNET_SYSERR; | ||
1406 | } | ||
1407 | *target = blinded_sig; | ||
1408 | return GNUNET_OK; | ||
1409 | } | ||
1410 | case GNUNET_CRYPTO_BSA_CS: | ||
1411 | { | ||
1412 | struct GNUNET_JSON_Specification ispec[] = { | ||
1413 | GNUNET_JSON_spec_uint32 ("b", | ||
1414 | &blinded_sig->details.blinded_cs_answer.b), | ||
1415 | GNUNET_JSON_spec_fixed_auto ("s", | ||
1416 | &blinded_sig->details.blinded_cs_answer. | ||
1417 | s_scalar), | ||
1418 | GNUNET_JSON_spec_end () | ||
1419 | }; | ||
1420 | |||
1421 | if (GNUNET_OK != | ||
1422 | GNUNET_JSON_parse (root, | ||
1423 | ispec, | ||
1424 | &emsg, | ||
1425 | &eline)) | ||
1426 | { | ||
1427 | GNUNET_break_op (0); | ||
1428 | GNUNET_free (blinded_sig); | ||
1429 | return GNUNET_SYSERR; | ||
1430 | } | ||
1431 | *target = blinded_sig; | ||
1432 | return GNUNET_OK; | ||
1433 | } | ||
1434 | } | ||
1435 | GNUNET_break_op (0); | ||
1436 | GNUNET_free (blinded_sig); | ||
1437 | return GNUNET_SYSERR; | ||
1438 | } | ||
1439 | |||
1440 | |||
1441 | /** | ||
1442 | * Cleanup data left from parsing blinded sig. | ||
1443 | * | ||
1444 | * @param cls closure, NULL | ||
1445 | * @param[out] spec where to free the data | ||
1446 | */ | ||
1447 | static void | ||
1448 | clean_blinded_sig (void *cls, | ||
1449 | struct GNUNET_JSON_Specification *spec) | ||
1450 | { | ||
1451 | struct GNUNET_CRYPTO_BlindedSignature **b_sig = spec->ptr; | ||
1452 | |||
1453 | (void) cls; | ||
1454 | |||
1455 | if (NULL != *b_sig) | ||
1456 | { | ||
1457 | GNUNET_CRYPTO_blinded_sig_decref (*b_sig); | ||
1458 | *b_sig = NULL; | ||
1459 | } | ||
1460 | } | ||
1461 | |||
1462 | |||
1463 | struct GNUNET_JSON_Specification | ||
1464 | GNUNET_JSON_spec_blinded_signature (const char *field, | ||
1465 | struct GNUNET_CRYPTO_BlindedSignature **b_sig) | ||
1466 | { | ||
1467 | struct GNUNET_JSON_Specification ret = { | ||
1468 | .parser = &parse_blinded_sig, | ||
1469 | .cleaner = &clean_blinded_sig, | ||
1470 | .field = field, | ||
1471 | .ptr = b_sig | ||
1472 | }; | ||
1473 | |||
1474 | *b_sig = NULL; | ||
1475 | return ret; | ||
1476 | } | ||
1477 | |||
1478 | /** | ||
1479 | * Parse given JSON object to unblinded signature. | ||
1480 | * | ||
1481 | * @param cls closure, NULL | ||
1482 | * @param root the json object representing data | ||
1483 | * @param[out] spec where to write the data | ||
1484 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
1485 | */ | ||
1486 | static enum GNUNET_GenericReturnValue | ||
1487 | parse_unblinded_sig (void *cls, | ||
1488 | json_t *root, | ||
1489 | struct GNUNET_JSON_Specification *spec) | ||
1490 | { | ||
1491 | struct GNUNET_CRYPTO_UnblindedSignature **target = spec->ptr; | ||
1492 | struct GNUNET_CRYPTO_UnblindedSignature *unblinded_sig; | ||
1493 | const char *cipher; | ||
1494 | struct GNUNET_JSON_Specification dspec[] = { | ||
1495 | GNUNET_JSON_spec_string ("cipher", | ||
1496 | &cipher), | ||
1497 | GNUNET_JSON_spec_end () | ||
1498 | }; | ||
1499 | const char *emsg; | ||
1500 | unsigned int eline; | ||
1501 | |||
1502 | (void) cls; | ||
1503 | if (GNUNET_OK != | ||
1504 | GNUNET_JSON_parse (root, | ||
1505 | dspec, | ||
1506 | &emsg, | ||
1507 | &eline)) | ||
1508 | { | ||
1509 | GNUNET_break_op (0); | ||
1510 | return GNUNET_SYSERR; | ||
1511 | } | ||
1512 | unblinded_sig = GNUNET_new (struct GNUNET_CRYPTO_UnblindedSignature); | ||
1513 | unblinded_sig->cipher = string_to_cipher (cipher); | ||
1514 | unblinded_sig->rc = 1; | ||
1515 | switch (unblinded_sig->cipher) | ||
1516 | { | ||
1517 | case GNUNET_CRYPTO_BSA_INVALID: | ||
1518 | break; | ||
1519 | case GNUNET_CRYPTO_BSA_RSA: | ||
1520 | { | ||
1521 | struct GNUNET_JSON_Specification ispec[] = { | ||
1522 | GNUNET_JSON_spec_rsa_signature ( | ||
1523 | "rsa_signature", | ||
1524 | &unblinded_sig->details.rsa_signature), | ||
1525 | GNUNET_JSON_spec_end () | ||
1526 | }; | ||
1527 | |||
1528 | if (GNUNET_OK != | ||
1529 | GNUNET_JSON_parse (root, | ||
1530 | ispec, | ||
1531 | &emsg, | ||
1532 | &eline)) | ||
1533 | { | ||
1534 | GNUNET_break_op (0); | ||
1535 | GNUNET_free (unblinded_sig); | ||
1536 | return GNUNET_SYSERR; | ||
1537 | } | ||
1538 | *target = unblinded_sig; | ||
1539 | return GNUNET_OK; | ||
1540 | } | ||
1541 | case GNUNET_CRYPTO_BSA_CS: | ||
1542 | { | ||
1543 | struct GNUNET_JSON_Specification ispec[] = { | ||
1544 | GNUNET_JSON_spec_fixed_auto ("cs_signature_r", | ||
1545 | &unblinded_sig->details.cs_signature. | ||
1546 | r_point), | ||
1547 | GNUNET_JSON_spec_fixed_auto ("cs_signature_s", | ||
1548 | &unblinded_sig->details.cs_signature. | ||
1549 | s_scalar), | ||
1550 | GNUNET_JSON_spec_end () | ||
1551 | }; | ||
1552 | |||
1553 | if (GNUNET_OK != | ||
1554 | GNUNET_JSON_parse (root, | ||
1555 | ispec, | ||
1556 | &emsg, | ||
1557 | &eline)) | ||
1558 | { | ||
1559 | GNUNET_break_op (0); | ||
1560 | GNUNET_free (unblinded_sig); | ||
1561 | return GNUNET_SYSERR; | ||
1562 | } | ||
1563 | *target = unblinded_sig; | ||
1564 | return GNUNET_OK; | ||
1565 | } | ||
1566 | } | ||
1567 | GNUNET_break_op (0); | ||
1568 | GNUNET_free (unblinded_sig); | ||
1569 | return GNUNET_SYSERR; | ||
1570 | } | ||
1571 | |||
1572 | |||
1573 | /** | ||
1574 | * Cleanup data left from parsing unblinded signature. | ||
1575 | * | ||
1576 | * @param cls closure, NULL | ||
1577 | * @param[out] spec where to free the data | ||
1578 | */ | ||
1579 | static void | ||
1580 | clean_unblinded_sig (void *cls, | ||
1581 | struct GNUNET_JSON_Specification *spec) | ||
1582 | { | ||
1583 | struct GNUNET_CRYPTO_UnblindedSignature **ub_sig = spec->ptr; | ||
1584 | |||
1585 | (void) cls; | ||
1586 | if (NULL != *ub_sig) | ||
1587 | { | ||
1588 | GNUNET_CRYPTO_unblinded_sig_decref (*ub_sig); | ||
1589 | *ub_sig = NULL; | ||
1590 | } | ||
1591 | } | ||
1592 | |||
1593 | |||
1594 | struct GNUNET_JSON_Specification | ||
1595 | GNUNET_JSON_spec_unblinded_signature (const char *field, | ||
1596 | struct GNUNET_CRYPTO_UnblindedSignature **ub_sig) | ||
1597 | { | ||
1598 | struct GNUNET_JSON_Specification ret = { | ||
1599 | .parser = &parse_unblinded_sig, | ||
1600 | .cleaner = &clean_unblinded_sig, | ||
1601 | .field = field, | ||
1602 | .ptr = ub_sig | ||
1603 | }; | ||
1604 | |||
1605 | *ub_sig = NULL; | ||
1606 | return ret; | ||
1607 | } | ||
1608 | |||
1609 | |||
1610 | |||
1183 | /* end of json_helper.c */ | 1611 | /* end of json_helper.c */ |