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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/*
* libkmipclient - KMIP client library
*
* Copyright IBM Corp. 2021
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <errno.h>
#include <endian.h>
#include <string.h>
#include "kmip.h"
#include "names.h"
#include "utils.h"
#define KMIP_JSON_TAG "tag"
#define KMIP_JSON_NAME "name"
#define KMIP_JSON_TYPE "type"
#define KMIP_JSON_VALUE "value"
/**
* Decode a KMIP node from the data in a JSON object using the JSON encoding.
*
* @param obj the JSON object to decode
* @param parent the parent node or NULL if no parent exists.
* @param node On return: the decoded node. The newly allocated
* node has a reference count of 1.
* @param debug if true, debug messages are printed
*
* @returns 0 in case of success, or a negative errno value
*/
int kmip_decode_json(const json_object *obj, struct kmip_node *parent,
struct kmip_node **node, bool debug)
{
json_object *tag_obj, *type_obj, *value_obj, *name_obj;
enum kmip_tag tag, v1_attr_tag = 0;
enum json_type value_type;
struct kmip_node *n, *e;
const char *str;
int rc, num, i;
int64_t int64;
if (obj == NULL || node == NULL)
return -EINVAL;
if (!json_object_is_type(obj, json_type_object)) {
kmip_debug(debug, "Object is not a JSON object");
return -EINVAL;
}
n = calloc(1, sizeof(struct kmip_node));
if (n == NULL) {
kmip_debug(debug, "calloc failed");
return -ENOMEM;
}
n->ref_count = 1;
tag_obj = json_object_object_get(obj, KMIP_JSON_TAG);
if (tag_obj == NULL ||
!json_object_is_type(tag_obj, json_type_string)) {
kmip_debug(debug, "Missing or invalid '%s' in JSON object",
KMIP_JSON_TAG);
rc = -EBADMSG;
goto out;
}
str = json_object_get_string(tag_obj);
n->tag = kmip_tag_by_name_or_hex(str);
if (n->tag == 0) {
kmip_debug(debug, "Unknown 'tag' in JSON object: '%s'", str);
rc = -EBADMSG;
goto out;
}
name_obj = json_object_object_get(obj, KMIP_JSON_NAME);
if (name_obj != NULL) {
if (!json_object_is_type(name_obj, json_type_string)) {
kmip_debug(debug, "Invalid '%s' in JSON object",
KMIP_JSON_NAME);
rc = -EBADMSG;
goto out;
}
n->name = strdup(json_object_get_string(tag_obj));
}
type_obj = json_object_object_get(obj, KMIP_JSON_TYPE);
if (type_obj == NULL) {
n->type = KMIP_TYPE_STRUCTURE;
} else {
if (!json_object_is_type(type_obj, json_type_string)) {
kmip_debug(debug,
"Missing or invalid '%s' in JSON object",
KMIP_JSON_TYPE);
rc = -EBADMSG;
goto out;
}
str = json_object_get_string(type_obj);
n->type = kmip_type_by_name_or_hex(str);
if (n->type == 0) {
kmip_debug(debug, "Unknown 'type' in JSON object: '%s'",
str);
rc = -EBADMSG;
goto out;
}
}
value_obj = json_object_object_get(obj, KMIP_JSON_VALUE);
if (value_obj == NULL) {
kmip_debug(debug, "Missing '%s' in JSON object",
KMIP_JSON_VALUE);
rc = -EBADMSG;
goto out;
}
value_type = json_object_get_type(value_obj);
/*
* KMIP v1.x attribute values may be Enumerations or Integer Masks.
* To correctly decode them, we need to know the tag. This is contained
* in a Attribute Name node, which is an element of our parent node.
*/
if (n->tag == KMIP_TAG_ATTRIBUTE_VALUE)
v1_attr_tag = kmip_find_v1_attribute_name_tag(parent);
tag = (v1_attr_tag != 0 ? v1_attr_tag : n->tag);
kmip_debug(debug, "tag: 0x%x type: 0x%x value_type: %d,", n->tag,
n->type, value_type);
switch (n->type) {
case KMIP_TYPE_STRUCTURE:
switch (value_type) {
case json_type_null:
break;
case json_type_array:
num = json_object_array_length(value_obj);
for (i = 0; i < num; i++) {
rc = kmip_decode_json(
json_object_array_get_idx(value_obj, i),
n, &e, debug);
if (rc != 0) {
kmip_debug(debug, "Failed to parse "
"array element %d", i);
goto out;
}
rc = kmip_node_add_structure_element(n, e);
kmip_node_free(e);
if (rc != 0) {
kmip_debug(debug,
"kmip_node_structure_add_element "
"failed: rc: %d", rc);
goto out;
}
}
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_INTEGER:
case KMIP_TYPE_LONG_INTEGER:
switch (value_type) {
case json_type_int:
case json_type_double:
int64 = json_object_get_int64(value_obj);
break;
case json_type_string:
str = json_object_get_string(value_obj);
if (n->type == KMIP_TYPE_INTEGER &&
kmip_is_tag_mask(tag)) {
rc = kmip_parse_mask(tag, str, '|', &int64);
if (rc != 0) {
kmip_debug(debug, "Failed to parse "
"mask string '%s'", str);
goto out;
}
} else {
rc = kmip_parse_hex_int(str, &int64);
if (rc != 0) {
kmip_debug(debug, "Failed to parse "
"hex string '%s'", str);
goto out;
}
}
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
if (n->type == KMIP_TYPE_INTEGER)
n->integer_value = int64;
else
n->long_value = int64;
break;
case KMIP_TYPE_INTERVAL:
switch (value_type) {
case json_type_int:
case json_type_double:
n->interval_value = json_object_get_int64(value_obj);
break;
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_parse_hex_int(str, &int64);
if (rc != 0) {
kmip_debug(debug, "Failed to parse "
"hex string '%s'", str);
goto out;
}
n->interval_value = int64;
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_BIG_INTEGER:
switch (value_type) {
case json_type_int:
case json_type_double:
int64 = htobe64(json_object_get_int64(value_obj));
rc = kmip_decode_bignum((const unsigned char *)&int64,
sizeof(int64),
&n->big_integer_value);
if (rc != 0) {
kmip_debug(debug, "kmip_decode_bignum failed");
goto out;
}
break;
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_parse_bignum(str, true,
&n->big_integer_value);
if (rc != 0) {
kmip_debug(debug,
"Failed to parse bignum string '%s'",
str);
goto out;
}
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_ENUMERATION:
switch (value_type) {
case json_type_int:
case json_type_double:
n->enumeration_value = json_object_get_int64(value_obj);
break;
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_enum_value_by_tag_name_or_hex(tag, str,
&n->enumeration_value);
if (rc != 0) {
kmip_debug(debug,
"Failed to parse enumeration '%s'",
str);
goto out;
}
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_BOOLEAN:
switch (value_type) {
case json_type_boolean:
n->boolean_value = json_object_get_boolean(value_obj);
break;
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_parse_hex_int(str, &int64);
if (rc != 0) {
kmip_debug(debug,
"Failed to parse hex string '%s'",
str);
goto out;
}
n->boolean_value = (int64 != 0);
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_TEXT_STRING:
switch (value_type) {
case json_type_string:
n->text_value = strdup(
json_object_get_string(value_obj));
if (n->text_value == NULL) {
rc = -ENOMEM;
goto out;
}
n->length = strlen(n->text_value);
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_BYTE_STRING:
switch (value_type) {
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_parse_hex(str, false, &n->bytes_value,
&n->length);
if (rc != 0) {
kmip_debug(debug,
"Failed to parse hex string '%s'",
str);
goto out;
}
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_DATE_TIME:
switch (value_type) {
case json_type_int:
case json_type_double:
n->date_time_value = json_object_get_int64(value_obj);
break;
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_parse_timestamp(str, &n->date_time_value);
if (rc != 0) {
kmip_debug(debug,
"Failed to parse time stamp '%s'",
str);
goto out;
}
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
case KMIP_TYPE_DATE_TIME_EXTENDED:
switch (value_type) {
case json_type_int:
case json_type_double:
n->date_time_ext_value =
json_object_get_int64(value_obj);
break;
case json_type_string:
str = json_object_get_string(value_obj);
rc = kmip_parse_hex_int(str, &int64);
if (rc != 0) {
kmip_debug(debug,
"Failed to parse hex string '%s'",
str);
goto out;
}
n->date_time_ext_value = int64;
break;
default:
kmip_debug(debug, "Invalid JSON type %d for node type "
"0x%x", value_type, n->type);
rc = -EBADMSG;
goto out;
}
break;
default:
kmip_debug(debug, "unknown type: 0x%x", n->type);
rc = -EBADMSG;
goto out;
}
*node = n;
rc = 0;
out:
if (rc != 0)
kmip_node_free(n);
return rc;
}
/**
* Encode a KMIP node into a JSON object using the JSON encoding.
*
* @param node the node to encode
* @param obj On return: the JSON object
* @param debug if true, debug messages are printed
*
* @returns 0 in case of success, or a negative errno value
*/
int kmip_encode_json(const struct kmip_node *node, json_object **obj,
bool debug)
{
json_object *ret_obj = NULL, *memb_obj, *elem_obj;
enum kmip_tag tag, v1_attr_tag = 0;
struct kmip_node *element;
char outstr[200] = { 0 };
const char *str;
int64_t int64;
struct tm *tm;
char *tmp;
char *s;
int rc;
if (node == NULL || obj == NULL)
return -EINVAL;
kmip_debug(debug, "tag: 0x%x type: 0x%x", node->tag, node->type);
ret_obj = json_object_new_object();
if (ret_obj == NULL) {
kmip_debug(debug, "Failed to allocate a JSON object");
return -ENOMEM;
}
memb_obj = json_object_new_string(
kmip_tag_name_or_hex_by_tag(node->tag,
outstr));
if (memb_obj == NULL) {
kmip_debug(debug, "Failed to build JSON object for tag");
rc = -ENOMEM;
goto out;
}
rc = json_object_object_add(ret_obj, KMIP_JSON_TAG, memb_obj);
if (rc != 0) {
kmip_debug(debug, "Failed to add JSON object for tag");
rc = -EIO;
goto out;
}
if (node->name != NULL) {
memb_obj = json_object_new_string(node->name);
if (memb_obj == NULL) {
kmip_debug(debug,
"Failed to build JSON object for name");
rc = -ENOMEM;
goto out;
}
rc = json_object_object_add(ret_obj, KMIP_JSON_NAME, memb_obj);
if (rc != 0) {
kmip_debug(debug, "Failed to add JSON object for name");
rc = -EIO;
goto out;
}
}
if (node->type != KMIP_TYPE_STRUCTURE) {
str = kmip_type_name_by_type(node->type);
if (str == NULL) {
kmip_debug(debug, "unknown type 0x%x", node->type);
rc = -EINVAL;
goto out;
}
memb_obj = json_object_new_string(str);
if (memb_obj == NULL) {
kmip_debug(debug,
"Failed to build JSON object for type");
rc = -ENOMEM;
goto out;
}
rc = json_object_object_add(ret_obj, KMIP_JSON_TYPE, memb_obj);
if (rc != 0) {
kmip_debug(debug, "Failed to add JSON object for type");
rc = -EIO;
goto out;
}
}
/*
* KMIP v1.x attribute values may be Enumerations or Integer Masks.
* To correctly encode them, we need to know the tag. This is contained
* in a Attribute Name node, which is an element of our parent node.
*/
if (node->tag == KMIP_TAG_ATTRIBUTE_VALUE)
v1_attr_tag = kmip_find_v1_attribute_name_tag(node->parent);
tag = (v1_attr_tag != 0 ? v1_attr_tag : node->tag);
switch (node->type) {
case KMIP_TYPE_STRUCTURE:
memb_obj = json_object_new_array();
if (memb_obj == NULL) {
kmip_debug(debug,
"Failed to build JSON object for value array");
rc = -ENOMEM;
goto out;
}
element = node->structure_value;
while (element != NULL) {
rc = kmip_encode_json(element, &elem_obj, debug);
if (rc != 0) {
kmip_debug(debug, "kmip_encode_json failed");
goto out;
}
rc = json_object_array_add(memb_obj, elem_obj);
if (rc != 0) {
kmip_debug(debug,
"json_object_array_add failed");
rc = EIO;
goto out;
}
element = element->next;
}
break;
case KMIP_TYPE_INTEGER:
if (kmip_is_tag_mask(tag) && node->integer_value != 0) {
rc = kmip_format_mask(tag, node->integer_value,
'|', &tmp);
if (rc != 0) {
kmip_debug(debug, "kmip_format_mask failed");
goto out;
}
memb_obj = json_object_new_string(tmp);
free(tmp);
} else {
memb_obj = json_object_new_int(node->integer_value);
}
break;
case KMIP_TYPE_INTERVAL:
memb_obj = json_object_new_int(node->interval_value);
break;
case KMIP_TYPE_LONG_INTEGER:
case KMIP_TYPE_DATE_TIME_EXTENDED:
if (node->type == KMIP_TYPE_LONG_INTEGER)
int64 = node->long_value;
else
int64 = node->date_time_ext_value;
/* any values >= 2^52 must be represented as hex strings */
if (int64 < 4503599627370496 &&
int64 > -4503599627370496) {
memb_obj = json_object_new_int64(int64);
} else {
rc = kmip_format_hex((const unsigned char *)&int64,
sizeof(int64), true, &tmp);
if (rc != 0) {
kmip_debug(debug, "kmip_format_hex failed");
goto out;
}
memb_obj = json_object_new_string(tmp);
free(tmp);
}
break;
case KMIP_TYPE_BIG_INTEGER:
rc = kmip_format_bignum(node->big_integer_value, true, &s);
if (rc != 0) {
kmip_debug(debug, "kmip_format_bignum failed");
goto out;
}
memb_obj = json_object_new_string(s);
free(s);
break;
case KMIP_TYPE_ENUMERATION:
str = kmip_enum_name_by_tag_value(tag, node->enumeration_value);
if (str != NULL)
memb_obj = json_object_new_string(str);
else
memb_obj = json_object_new_int(node->enumeration_value);
break;
case KMIP_TYPE_BOOLEAN:
memb_obj = json_object_new_boolean(node->boolean_value);
break;
case KMIP_TYPE_TEXT_STRING:
memb_obj = json_object_new_string(node->text_value);
break;
case KMIP_TYPE_BYTE_STRING:
rc = kmip_format_hex(node->bytes_value, node->length,
false, &s);
if (rc != 0) {
kmip_debug(debug, "kmip_format_hex_long failed");
goto out;
}
memb_obj = json_object_new_string(s);
free(s);
break;
case KMIP_TYPE_DATE_TIME:
tm = gmtime((time_t *)&node->date_time_value);
strftime(outstr, sizeof(outstr), KMIP_ISO8601_TIMESTAMP_UTC,
tm);
memb_obj = json_object_new_string(outstr);
break;
default:
kmip_debug(debug, "unknown type: 0x%x", node->type);
rc = -EINVAL;
goto out;
}
if (memb_obj == NULL) {
rc = -ENOMEM;
goto out;
}
rc = json_object_object_add(ret_obj, KMIP_JSON_VALUE, memb_obj);
if (rc != 0) {
kmip_debug(debug, "Failed to add JSON object for value");
rc = -EIO;
goto out;
}
rc = 0;
*obj = ret_obj;
out:
if (rc != 0)
json_object_put(ret_obj);
return rc;
}
|