Browse code

Introduce to_bytes().

Xavier G authored on 26/04/2020 19:41:55
Showing 1 changed files

  • yamltab index 4528ce4..8b3715d 100755
... ...
@@ -274,6 +274,11 @@ def simplify_keytab(keytab):
274 274
 		simplified['entries'].append(simple_entry)
275 275
 	return simplified
276 276
 
277
+def to_bytes(value):
278
+	if type(value) is bytes:
279
+		return value
280
+	return unhexlify(value)
281
+
277 282
 def prepare_serialization(obj):
278 283
 	"""
279 284
 	Prepare keytab for serialization.
... ...
@@ -343,7 +348,7 @@ def raw_entry_to_binary(entry, index):
343 348
 			raise KeytabComposingError('invalid %s data in entry #%d' % (key, index))
344 349
 	data = raw_principal_to_binary(entry['principal'], index)
345 350
 	try:
346
-		key_data = unhexlify(entry['key'])
351
+		key_data = to_bytes(entry['key'])
347 352
 	except:
348 353
 		raise KeytabComposingError('invalid key data in entry #%d' % index)
349 354
 	data += struct.pack('>IBHH', entry['timestamp'], entry['kvno'], entry['enctype_raw'], len(key_data))
... ...
@@ -357,7 +362,7 @@ def raw_record_to_binary(record, index):
357 362
 	data += raw_entry_to_binary(record['entry'], index)
358 363
 	if 'tail' in record:
359 364
 		try:
360
-			data += unhexlify(record['tail'])
365
+			data += to_bytes(record['tail'])
361 366
 		except:
362 367
 			raise KeytabComposingError('invalid tail data in record #%d' % index)
363 368
 	return struct.pack('>i', len(data)) + data
... ...
@@ -375,7 +380,7 @@ def raw_hole_to_binary(hole, index):
375 380
 		raise KeytabComposingError('invalid length in record #%d' % index)
376 381
 	if 'data' in record:
377 382
 		try:
378
-			hole_data = unhexlify(record['data'])
383
+			hole_data = to_bytes(record['data'])
379 384
 		except:
380 385
 			raise KeytabComposingError('invalid data in record #%d' % index)
381 386
 		if len(hole_data) != length:
... ...
@@ -432,7 +437,7 @@ def simple_kvno_to_full(inentry, index, entry, record):
432 437
 		entry['kvno'] = 0 if kvno_too_big else inentry['kvno']
433 438
 		if inentry.get('kvno_in_tail', False):
434 439
 			entry['tail_kvno'] = inentry['kvno']
435
-			record['tail'] = hexlify(struct.pack('>I', inentry['kvno']))
440
+			record['tail'] = struct.pack('>I', inentry['kvno'])
436 441
 		elif kvno_too_big:
437 442
 			message = 'Cannot store kvno > 255 without kvno_in_tail in entry #%d'
438 443
 			raise KeytabComposingError(message % index)
... ...
@@ -478,7 +483,7 @@ def simple_keytab_to_full(indata):
478 483
 		simple_enctype_to_full(inentry, index, entry)
479 484
 		if 'key' in inentry:
480 485
 			entry['key'] = inentry['key']
481
-			entry['key_length'] = len(unhexlify(inentry['key']))
486
+			entry['key_length'] = len(to_bytes(inentry['key']))
482 487
 		data['records'].append(record)
483 488
 	return data
484 489