[go: up one dir, main page]

File: xml.c

package info (click to toggle)
s390-tools 2.35.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,220 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (490 lines) | stat: -rw-r--r-- 12,129 bytes parent folder | download | duplicates (2)
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
/*
 * 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_XML_TTLV		"TTLV"
#define KMIP_XML_TAG		"tag"
#define KMIP_XML_NAME		"name"
#define KMIP_XML_TYPE		"type"
#define KMIP_XML_VALUE		"value"

/**
 * Decode a KMIP node from the data in the XML node using the XML encoding.
 *
 * @param xml               the XML node 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_xml(const xmlNode *xml, struct kmip_node *parent,
		    struct kmip_node **node, bool debug)
{
	char *tag_attr = NULL, *name_attr = NULL, *type_attr = NULL;
	enum kmip_tag tag, v1_attr_tag = 0;
	char *tag_name, *value_attr = NULL;
	struct kmip_node *n = NULL, *e;
	uint64_t uint64;
	xmlNode *child;
	int64_t int64;
	int rc = 0, i;

	if (xml == NULL || node == NULL)
		return -EINVAL;

	if (xml->type != XML_ELEMENT_NODE) {
		kmip_debug(debug, "Invalid XML node type: %d", xml->type);
		return -EINVAL;
	}

	n = calloc(1, sizeof(struct kmip_node));
	if (n == NULL) {
		kmip_debug(debug, "calloc failed");
		return -ENOMEM;
	}
	n->ref_count = 1;

	if (strcmp((char *)xml->name, KMIP_XML_TTLV) == 0) {
		tag_attr = (char *)xmlGetProp(xml, (xmlChar *)KMIP_XML_TAG);
		if (tag_attr == NULL) {
			kmip_debug(debug, "Missing '%s' attribute in XML node",
				   KMIP_XML_TAG);
			rc = -EBADMSG;
			goto out;
		}
		tag_name = tag_attr;
	} else {
		tag_name = (char *)xml->name;
	}
	n->tag = kmip_tag_by_name_or_hex(tag_name);
	if (n->tag == 0) {
		kmip_debug(debug, "Unknown 'tag' in XML object: '%s'",
			   tag_name);
		rc = -EBADMSG;
		goto out;
	}

	name_attr = (char *)xmlGetProp(xml, (xmlChar *)KMIP_XML_NAME);
	if (name_attr != NULL)
		n->name = strdup(name_attr);

	type_attr = (char *)xmlGetProp(xml, (xmlChar *)KMIP_XML_TYPE);
	if (type_attr == NULL) {
		n->type = KMIP_TYPE_STRUCTURE;
	} else {
		n->type = kmip_type_by_name_or_hex(type_attr);
		if (n->type == 0) {
			kmip_debug(debug, "Unknown 'type' in XML object: '%s'",
				   type_attr);
			rc = -EBADMSG;
			goto out;
		}
	}

	value_attr = (char *)xmlGetProp(xml, (xmlChar *)KMIP_XML_VALUE);
	if (n->type != KMIP_TYPE_STRUCTURE && value_attr == NULL) {
		kmip_debug(debug, "Missing '%s' attribute in XML node",
			   KMIP_XML_VALUE);
		rc = -EBADMSG;
		goto out;
	}

	/*
	 * 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", n->tag, n->type);

	switch (n->type) {
	case KMIP_TYPE_STRUCTURE:
		for (child = xml->children, i = 0; child != NULL;
						child = child->next, i++) {
			if (child->type != XML_ELEMENT_NODE)
				continue;

			rc = kmip_decode_xml(child, n, &e, debug);
			if (rc != 0) {
				kmip_debug(debug, "Failed to parse child "
					   "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;

	case KMIP_TYPE_INTEGER:
	case KMIP_TYPE_LONG_INTEGER:
	case KMIP_TYPE_DATE_TIME_EXTENDED:
		if (n->type == KMIP_TYPE_INTEGER &&
		    kmip_is_tag_mask(tag)) {
			rc = kmip_parse_mask(tag, value_attr, ' ', &int64);
			if (rc != 0) {
				kmip_debug(debug, "Failed to parse "
					   "mask string '%s'", value_attr);
				goto out;
			}
		} else {
			rc = kmip_parse_decimal_int(value_attr, &int64);
			if (rc != 0) {
				kmip_debug(debug, "Failed to parse "
					   "decimal string '%s'", value_attr);
				goto out;
			}
		}

		switch (n->type) {
		case KMIP_TYPE_INTEGER:
			n->integer_value = int64;
			break;
		case KMIP_TYPE_LONG_INTEGER:
			n->long_value = int64;
			break;
		case KMIP_TYPE_DATE_TIME_EXTENDED:
			n->date_time_ext_value = int64;
			break;
		default:
			break;
		}
		break;

	case KMIP_TYPE_INTERVAL:
		rc = kmip_parse_decimal_uint(value_attr, &uint64);
		if (rc != 0) {
			kmip_debug(debug, "Failed to parse "
				   "decimal string '%s'", value_attr);
			goto out;
		}
		n->interval_value = uint64;
		break;

	case KMIP_TYPE_BIG_INTEGER:
		rc = kmip_parse_bignum(value_attr, false,
				       &n->big_integer_value);
		if (rc != 0) {
			kmip_debug(debug,
				   "Failed to parse bignum string '%s'",
				   value_attr);
			goto out;
		}
		break;

	case KMIP_TYPE_ENUMERATION:
		rc = kmip_enum_value_by_tag_name_or_hex(tag, value_attr,
							&n->enumeration_value);
		if (rc != 0) {
			kmip_debug(debug,
				   "Failed to parse enumeration '%s'",
				   value_attr);
			goto out;
		}
		break;

	case KMIP_TYPE_BOOLEAN:
		n->boolean_value = (strcmp(value_attr, "true") == 0 ||
				    strcmp(value_attr, "1") == 0);
		break;

	case KMIP_TYPE_TEXT_STRING:
		n->text_value = strdup(value_attr);
		if (n->text_value == NULL) {
			rc = -ENOMEM;
			goto out;
		}
		n->length = strlen(n->text_value);
		break;

	case KMIP_TYPE_BYTE_STRING:
		rc = kmip_parse_hex(value_attr, false, &n->bytes_value,
				    &n->length);
		if (rc != 0) {
			kmip_debug(debug,
				   "Failed to parse hex string '%s'",
				   value_attr);
			goto out;
		}
		break;

	case KMIP_TYPE_DATE_TIME:
		rc = kmip_parse_timestamp(value_attr, &n->date_time_value);
		if (rc != 0) {
			kmip_debug(debug,
				   "Failed to parse time stamp '%s'",
				   value_attr);
			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 && n != NULL)
		kmip_node_free(n);
	if (tag_attr != NULL)
		xmlFree(tag_attr);
	if (name_attr != NULL)
		xmlFree(name_attr);
	if (type_attr != NULL)
		xmlFree(type_attr);
	if (value_attr != NULL)
		xmlFree(value_attr);

	return rc;
}

/**
 * Encode a KMIP node into an XML node using the XML encoding.
 *
 * @param node              the node to encode
 * @param xml               On return: the XML node
 * @param debug             if true, debug messages are printed
 *
 * @returns 0 in case of success, or a negative errno value
 */
int kmip_encode_xml(const struct kmip_node *node, xmlNode **xml, bool debug)
{
	enum kmip_tag tag, v1_attr_tag = 0;
	xmlNode *ret_xml = NULL, *elem_xml;
	struct kmip_node *element;
	const char *tag_name;
	char tmp_str[50];
	const char *str;
	struct tm *tm;
	xmlAttr *attr;
	char *tmp;
	int rc;

	if (node == NULL || xml == NULL)
		return -EINVAL;

	kmip_debug(debug, "tag: 0x%x type: 0x%x", node->tag, node->type);

	tag_name = kmip_tag_name_by_tag(node->tag);
	if (tag_name != NULL)
		ret_xml = xmlNewNode(NULL, (xmlChar *)tag_name);
	else
		ret_xml = xmlNewNode(NULL, (xmlChar *)KMIP_XML_TTLV);
	if (ret_xml == NULL) {
		kmip_debug(debug, "Failed to allocate a XML node");
		return -ENOMEM;
	}

	if (tag_name == NULL) {
		sprintf(tmp_str, "0x%06x", node->tag);
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_TAG,
				  (xmlChar *)tmp_str);
		if (attr == NULL) {
			kmip_debug(debug,
				   "Failed to add '%s' attribute to XML node",
				   KMIP_XML_TAG);
			rc = -ENOMEM;
			goto out;
		}

		if (node->name != NULL) {
			attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_NAME,
					  (xmlChar *)node->name);
			if (attr == NULL) {
				kmip_debug(debug, "Failed to add '%s' "
					   "attribute to XML node",
					   KMIP_XML_NAME);
				rc = -ENOMEM;
				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;
		}

		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_TYPE,
				  (xmlChar *)str);
		if (attr == NULL) {
			kmip_debug(debug,
				   "Failed to add '%s' attribute to XML node",
				   KMIP_XML_TYPE);
			rc = -ENOMEM;
			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:
		element = node->structure_value;
		while (element != NULL) {
			rc = kmip_encode_xml(element, &elem_xml, debug);
			if (rc != 0) {
				kmip_debug(debug, "kmip_encode_xml failed");
				goto out;
			}
			if (xmlAddChild(ret_xml, elem_xml) == NULL) {
				kmip_debug(debug, "xmlAddChild failed");
				rc = -EIO;
				goto out;
			}
			element = element->next;
		}
		attr = NULL;
		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;
			}
			attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
					  (xmlChar *)tmp);
			free(tmp);
		} else {
			sprintf(tmp_str, "%d", node->integer_value);
			attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
					  (xmlChar *)tmp_str);
		}
		break;

	case KMIP_TYPE_INTERVAL:
		sprintf(tmp_str, "%u", node->interval_value);
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)tmp_str);
		break;

	case KMIP_TYPE_LONG_INTEGER:
		sprintf(tmp_str, "%ld", node->long_value);
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)tmp_str);
		break;

	case KMIP_TYPE_BIG_INTEGER:
		rc = kmip_format_bignum(node->big_integer_value, false, &tmp);
		if (rc != 0) {
			kmip_debug(debug, "kmip_format_bignum failed");
			goto out;
		}
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)tmp);
		free(tmp);
		break;

	case KMIP_TYPE_ENUMERATION:
		str = kmip_enum_name_by_tag_value(tag, node->enumeration_value);
		if (str != NULL) {
			attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
					  (xmlChar *)str);
		} else {
			sprintf(tmp_str, "0x%08x", node->enumeration_value);
			attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
					  (xmlChar *)tmp_str);
		}
		break;

	case KMIP_TYPE_BOOLEAN:
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)(node->boolean_value ?
							  "true" : "false"));
		break;

	case KMIP_TYPE_TEXT_STRING:
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)node->text_value);
		break;

	case KMIP_TYPE_BYTE_STRING:
		rc = kmip_format_hex(node->bytes_value, node->length,
				     false, &tmp);
		if (rc != 0) {
			kmip_debug(debug, "kmip_format_hex_long failed");
			goto out;
		}
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)tmp);
		free(tmp);
		break;

	case KMIP_TYPE_DATE_TIME:
		tm = gmtime((time_t *)&node->date_time_value);
		strftime(tmp_str, sizeof(tmp_str), KMIP_ISO8601_TIMESTAMP_UTC,
			 tm);
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)tmp_str);
		break;

	case KMIP_TYPE_DATE_TIME_EXTENDED:
		sprintf(tmp_str, "%ld", node->date_time_ext_value);
		attr = xmlSetProp(ret_xml, (xmlChar *)KMIP_XML_VALUE,
				  (xmlChar *)tmp_str);
		break;

	default:
		kmip_debug(debug, "unknown type: 0x%x", node->type);
		rc = -EINVAL;
		goto out;
	}

	if (attr == NULL && node->type != KMIP_TYPE_STRUCTURE) {
		kmip_debug(debug, "Failed to add '%s' "
			   "attribute to XML node",
			   KMIP_XML_VALUE);
		rc = -ENOMEM;
		goto out;
	}

	rc = 0;
	*xml = ret_xml;

out:
	if (rc != 0)
		xmlFreeNode(ret_xml);

	return rc;
}