def setUp(self): super(TestCryptographicParameters, self).setUp() self.bad_enum_code = 8535937 self.factory = AttributeValueFactory() self.cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK }) self.cp_none = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {}) # Symmetric key object with Cryptographic Parameters # Byte stream edited to add Key Role Type parameter # Based on the KMIP Spec 1.1 Test Cases document # 11.1 page 255 on the pdf version self.key_req_with_crypt_params = BytearrayStream(( b'\x42\x00\x2B\x01\x00\x00\x00\x40' b'\x42\x00\x11\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x5F\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00' b'\x42\x00\x38\x05\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00' b'\x42\x00\x83\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' ))
def __init__(self, attribute_name=None, attribute_index=None, attribute_value=None): super(Attribute, self).__init__(tag=Tags.ATTRIBUTE) self.value_factory = AttributeValueFactory() self.attribute_name = attribute_name self.attribute_index = attribute_index self.attribute_value = attribute_value if attribute_value is not None: attribute_value.tag = Tags.ATTRIBUTE_VALUE
def setUp(self): super(TestCryptographicParameters, self).setUp() self.bad_enum_code = 8535937 self.factory = AttributeValueFactory() self.cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK}) self.cp_none = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {}) # Symmetric key object with Cryptographic Parameters # Byte stream edited to add Key Role Type parameter # Based on the KMIP Spec 1.1 Test Cases document # 11.1 page 255 on the pdf version self.key_req_with_crypt_params = BytearrayStream(( b'\x42\x00\x2B\x01\x00\x00\x00\x40' b'\x42\x00\x11\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x5F\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00' b'\x42\x00\x38\x05\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00' b'\x42\x00\x83\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' ))
class AttributeFactory(object): def __init__(self): self.value_factory = AttributeValueFactory() def _create_attribute(self, name, value, index): attribute_name = Attribute.AttributeName(name) if index is None: return Attribute(attribute_name=attribute_name, attribute_value=value) else: attribute_index = Attribute.AttributeIndex(index) return Attribute(attribute_name=attribute_name, attribute_index=attribute_index, attribute_value=value) def create_attribute(self, name, value, index=None): value = self.value_factory.create_attribute_value(name, value) if isinstance(name, Enum): name = name.value elif isinstance(name, str): # Name is already a string, pass pass else: msg = utils.build_er_error(Attribute, 'name', '{0} or {1}'.format('Enum', 'str'), type(name)) raise TypeError(msg) return self._create_attribute(name, value, index)
class AttributeFactory(object): def __init__(self): self.value_factory = AttributeValueFactory() def _create_attribute(self, name, value, index): attribute_name = Attribute.AttributeName(name) if index is None: return Attribute(attribute_name=attribute_name, attribute_value=value) else: attribute_index = Attribute.AttributeIndex(index) return Attribute(attribute_name=attribute_name, attribute_index=attribute_index, attribute_value=value) def create_attribute(self, name, value, index=None): value = self.value_factory.create_attribute_value(name, value) if isinstance(name, Enum): name = name.value elif isinstance(name, str): # Name is already a string, pass pass else: msg = utils.build_er_error(Attribute, 'name', '{} or {}'.format('Enum', 'str'), type(name)) raise TypeError(msg) return self._create_attribute(name, value, index)
class TestAttributeValueFactory(TestCase): def setUp(self): super(TestAttributeValueFactory, self).setUp() self.factory = AttributeValueFactory() def tearDown(self): super(TestAttributeValueFactory, self).tearDown() # TODO (peter-hamilton) Consider even further modularity def _test_operation_policy_name(self, opn, value): if value is None: value = '' msg = "expected {0}, received {1}".format(OperationPolicyName, opn) self.assertIsInstance(opn, OperationPolicyName, msg) msg = "expected {0}, received {1}".format(value, opn.value) self.assertEqual(value, opn.value, msg) def _test_create_attribute_value_operation_policy_name(self, value): opn = self.factory.create_attribute_value( AttributeType.OPERATION_POLICY_NAME, value) self._test_operation_policy_name(opn, value) def _test_create_operation_policy_name(self, value): opn = self.factory._create_operation_policy_name(value) self._test_operation_policy_name(opn, value) def test_create_attribute_value_operation_policy_name(self): self._test_create_attribute_value_operation_policy_name('test') def test_create_attribute_value_operation_policy_name_on_none(self): self._test_create_attribute_value_operation_policy_name(None) def test_create_operation_policy_name(self): self._test_create_operation_policy_name('test') def test_create_operation_policy_name_on_none(self): self._test_create_operation_policy_name(None)
class Attribute(Struct): class AttributeName(TextString): def __init__(self, value=None): super(Attribute.AttributeName, self).__init__( value, Tags.ATTRIBUTE_NAME) def __eq__(self, other): if isinstance(other, Attribute.AttributeName): if self.value != other.value: return False else: return True else: NotImplemented def __ne__(self, other): if isinstance(other, Attribute.AttributeName): return not (self == other) else: return NotImplemented class AttributeIndex(Integer): def __init__(self, value=None): super(Attribute.AttributeIndex, self).__init__( value, Tags.ATTRIBUTE_INDEX) def __init__(self, attribute_name=None, attribute_index=None, attribute_value=None): super(Attribute, self).__init__(tag=Tags.ATTRIBUTE) self.value_factory = AttributeValueFactory() self.attribute_name = attribute_name self.attribute_index = attribute_index self.attribute_value = attribute_value if attribute_value is not None: attribute_value.tag = Tags.ATTRIBUTE_VALUE def read(self, istream): super(Attribute, self).read(istream) tstream = BytearrayStream(istream.read(self.length)) # Read the name of the attribute self.attribute_name = Attribute.AttributeName() self.attribute_name.read(tstream) # Read the attribute index if it is next if self.is_tag_next(Tags.ATTRIBUTE_INDEX, tstream): self.attribute_index = Attribute.AttributeIndex() self.attribute_index.read(tstream) # Lookup the attribute class that belongs to the attribute name name = self.attribute_name.value enum_name = name.replace('.', '_').replace(' ', '_').upper() enum_type = None try: enum_type = AttributeType[enum_name] except KeyError: # Likely custom attribute, pass raw name string as attribute type enum_type = name value = self.value_factory.create_attribute_value(enum_type, None) self.attribute_value = value self.attribute_value.tag = Tags.ATTRIBUTE_VALUE self.attribute_value.read(tstream) self.is_oversized(tstream) def write(self, ostream): tstream = BytearrayStream() self.attribute_name.write(tstream) if self.attribute_index is not None: self.attribute_index.write(tstream) self.attribute_value.write(tstream) # Write the length and value of the attribute self.length = tstream.length() super(Attribute, self).write(ostream) ostream.write(tstream.buffer) def __repr__(self): attribute_name = "attribute_name={0}".format(repr(self.attribute_name)) attribute_index = "attribute_index={0}".format( repr(self.attribute_index) ) attribute_value = "attribute_value={0}".format( repr(self.attribute_value) ) return "Attribute({0}, {1}, {2})".format( attribute_name, attribute_index, attribute_value ) def __str__(self): return str({ 'attribute_name': str(self.attribute_name), 'attribute_index': str(self.attribute_index), 'attribute_value': str(self.attribute_value) }) def __eq__(self, other): if isinstance(other, Attribute): if self.attribute_name != other.attribute_name: return False elif self.attribute_index != other.attribute_index: return False elif self.attribute_value != other.attribute_value: return False else: return True else: return NotImplemented def __ne__(self, other): if isinstance(other, Attribute): return not self.__eq__(other) else: return NotImplemented
class TestCryptographicParameters(TestCase): """ A test suite for the CryptographicParameters class """ def setUp(self): super(TestCryptographicParameters, self).setUp() self.bad_enum_code = 8535937 self.factory = AttributeValueFactory() self.cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK}) self.cp_none = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {}) # Symmetric key object with Cryptographic Parameters # Byte stream edited to add Key Role Type parameter # Based on the KMIP Spec 1.1 Test Cases document # 11.1 page 255 on the pdf version self.key_req_with_crypt_params = BytearrayStream(( b'\x42\x00\x2B\x01\x00\x00\x00\x40' b'\x42\x00\x11\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x5F\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00' b'\x42\x00\x38\x05\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00' b'\x42\x00\x83\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' )) def teardown(self): super(TestDigestValue, self).tearDown() def test_write_crypto_params(self): ostream = BytearrayStream() self.cp.write(ostream) self.assertEqual(self.key_req_with_crypt_params, ostream) def test_read_crypto_params(self): CryptographicParameters.read(self.cp, self.key_req_with_crypt_params) self.assertEqual(Tags.BLOCK_CIPHER_MODE.value, self.cp.block_cipher_mode.tag.value) self.assertEqual(BlockCipherMode.CBC.value, self.cp.block_cipher_mode.value.value) self.assertEqual(Tags.PADDING_METHOD.value, self.cp.padding_method.tag.value) self.assertEqual(PaddingMethod.PKCS5.value, self.cp.padding_method.value.value) self.assertEqual(Tags.KEY_ROLE_TYPE.value, self.cp.key_role_type.tag.value) self.assertEqual(KeyRoleType.BDK.value, self.cp.key_role_type.value.value) self.assertEqual(Tags.HASHING_ALGORITHM.value, self.cp.hashing_algorithm.tag.value) self.assertEqual(HashingAlgorithmEnum.SHA_1.value, self.cp.hashing_algorithm.value.value) def test_bad_cipher_mode(self): self.cp.block_cipher_mode = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK}) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_padding_method(self): self.cp.padding_method = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK}) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_hash_algorithm(self): self.cp.hashing_algorithm = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK}) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_key_role_type(self): self.cp.key_role_type = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK}) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_object(self): name_value = 'puppies' name_type = NameType.UNINTERPRETED_TEXT_STRING bad_obj = Name.create(name_value, name_type) self.assertNotEqual(NotImplemented, bad_obj)
class Attribute(Struct): class AttributeName(TextString): def __init__(self, value=None): super(self.__class__, self).__init__(value, Tags.ATTRIBUTE_NAME) class AttributeIndex(Integer): def __init__(self, value=None): super(self.__class__, self).__init__(value, Tags.ATTRIBUTE_INDEX) def __init__(self, attribute_name=None, attribute_index=None, attribute_value=None): super(self.__class__, self).__init__(tag=Tags.ATTRIBUTE) self.value_factory = AttributeValueFactory() self.attribute_name = attribute_name self.attribute_index = attribute_index self.attribute_value = attribute_value if attribute_value is not None: attribute_value.tag = Tags.ATTRIBUTE_VALUE def read(self, istream): super(self.__class__, self).read(istream) tstream = BytearrayStream(istream.read(self.length)) # Read the name of the attribute self.attribute_name = Attribute.AttributeName() self.attribute_name.read(tstream) # Read the attribute index if it is next if self.is_tag_next(Tags.ATTRIBUTE_INDEX, tstream): self.attribute_index = Attribute.AttributeIndex() self.attribute_index.read(tstream) # Lookup the attribute class that belongs to the attribute name name = self.attribute_name.value enum_name = name.replace('.', '_').replace(' ', '_').upper() enum_type = None try: enum_type = AttributeType[enum_name] except KeyError: # Likely custom attribute, pass raw name string as attribute type enum_type = name value = self.value_factory.create_attribute_value(enum_type, None) self.attribute_value = value self.attribute_value.tag = Tags.ATTRIBUTE_VALUE self.attribute_value.read(tstream) self.is_oversized(tstream) def write(self, ostream): tstream = BytearrayStream() self.attribute_name.write(tstream) if self.attribute_index is not None: self.attribute_index.write(tstream) self.attribute_value.write(tstream) # Write the length and value of the attribute self.length = tstream.length() super(self.__class__, self).write(ostream) ostream.write(tstream.buffer) def __eq__(self, other): if isinstance(other, Attribute): if self.attribute_name != other.attribute_name: return False elif self.attribute_index != other.attribute_index: return False elif self.attribute_value != other.attribute_value: return False else: return True else: return NotImplemented def __ne__(self, other): return not self.__eq__(other)
def __init__(self): self.value_factory = AttributeValueFactory()
class TestCryptographicParameters(TestCase): """ A test suite for the CryptographicParameters class """ def setUp(self): super(TestCryptographicParameters, self).setUp() self.bad_enum_code = 8535937 self.factory = AttributeValueFactory() self.cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.cp_none = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {}) # Symmetric key object with Cryptographic Parameters # Byte stream edited to add: # Key Role Type parameter # Digital Signature Algorithm parameter # Cryptographic Algorithm parameter # Based on the KMIP Spec 1.1 Test Cases document # 11.1 page 255 on the pdf version self.key_req_with_crypt_params = BytearrayStream(( b'\x42\x00\x2B\x01\x00\x00\x00\x60' b'\x42\x00\x11\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x5F\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00' b'\x42\x00\x38\x05\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00' b'\x42\x00\x83\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\xAE\x05\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x00' b'\x42\x00\x28\x05\x00\x00\x00\x04\x00\x00\x00\x0B\x00\x00\x00\x00' )) def teardown(self): super(TestCryptographicParameters, self).tearDown() def test_write_crypto_params(self): ostream = BytearrayStream() self.cp.write(ostream) self.assertEqual(self.key_req_with_crypt_params, ostream) def test_read_crypto_params(self): CryptographicParameters.read(self.cp, self.key_req_with_crypt_params) self.assertEqual(Tags.BLOCK_CIPHER_MODE.value, self.cp.block_cipher_mode.tag.value) self.assertEqual(BlockCipherMode.CBC.value, self.cp.block_cipher_mode.value.value) self.assertEqual(Tags.PADDING_METHOD.value, self.cp.padding_method.tag.value) self.assertEqual(PaddingMethod.PKCS5.value, self.cp.padding_method.value.value) self.assertEqual(Tags.KEY_ROLE_TYPE.value, self.cp.key_role_type.tag.value) self.assertEqual(KeyRoleType.BDK.value, self.cp.key_role_type.value.value) self.assertEqual(Tags.HASHING_ALGORITHM.value, self.cp.hashing_algorithm.tag.value) self.assertEqual(HashingAlgorithmEnum.SHA_1.value, self.cp.hashing_algorithm.value.value) self.assertEqual(Tags.DIGITAL_SIGNATURE_ALGORITHM.value, self.cp.digital_signature_algorithm.tag.value) self.assertEqual( DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION.value, self.cp.digital_signature_algorithm.value.value) self.assertEqual(Tags.CRYPTOGRAPHIC_ALGORITHM.value, self.cp.cryptographic_algorithm.tag.value) self.assertEqual(CryptographicAlgorithm.HMAC_SHA512.value, self.cp.cryptographic_algorithm.value.value) def test_bad_cipher_mode(self): self.cp.block_cipher_mode = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_padding_method(self): self.cp.padding_method = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_hash_algorithm(self): self.cp.hashing_algorithm = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_key_role_type(self): self.cp.key_role_type = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_digital_signature_algorithm(self): self.cp.digital_signature_algorithm = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_cryptographic_algorithm(self): self.cp.cryptographic_algorithm = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK, 'digital_signature_algorithm': DigitalSignatureAlgorithm.SHA256_WITH_RSA_ENCRYPTION, 'cryptographic_algorithm': CryptographicAlgorithm.HMAC_SHA512 }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate)
def setUp(self): super(TestAttributeValueFactory, self).setUp() self.factory = AttributeValueFactory()
class TestAttributeValueFactory(TestCase): def setUp(self): super(TestAttributeValueFactory, self).setUp() self.factory = AttributeValueFactory() def tearDown(self): super(TestAttributeValueFactory, self).tearDown() # TODO (peter-hamilton) Consider even further modularity def _test_operation_policy_name(self, opn, value): if value is None: value = '' msg = "expected {0}, received {1}".format(OperationPolicyName, opn) self.assertIsInstance(opn, OperationPolicyName, msg) msg = "expected {0}, received {1}".format(value, opn.value) self.assertEqual(value, opn.value, msg) def _test_create_attribute_value_operation_policy_name(self, value): opn = self.factory.create_attribute_value( AttributeType.OPERATION_POLICY_NAME, value) self._test_operation_policy_name(opn, value) def _test_create_operation_policy_name(self, value): opn = self.factory._create_operation_policy_name(value) self._test_operation_policy_name(opn, value) def test_create_attribute_value_operation_policy_name(self): self._test_create_attribute_value_operation_policy_name('test') def test_create_attribute_value_operation_policy_name_on_none(self): self._test_create_attribute_value_operation_policy_name(None) def test_create_operation_policy_name(self): self._test_create_operation_policy_name('test') def test_create_operation_policy_name_on_none(self): self._test_create_operation_policy_name(None) def _test_cryptograpic_parameters(self, obj, block_cipher_mode, padding_method, key_role_type, hashing_algorithm): msg = "expected {0}, received {1}" self.assertIsInstance(obj, CryptographicParameters, msg.format( CryptographicParameters, obj.__class__)) self.assertEqual(block_cipher_mode, obj.block_cipher_mode, msg.format( block_cipher_mode, obj.block_cipher_mode)) self.assertEqual(padding_method, obj.padding_method, msg.format( padding_method, obj.padding_method)) self.assertEqual(key_role_type, obj.key_role_type, msg.format( key_role_type, obj.hashing_algorithm)) self.assertEqual(hashing_algorithm, obj.hashing_algorithm, msg.format( hashing_algorithm, obj.hashing_algorithm)) def test_create_cryptograpic_parameters_none(self): cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {}) self._test_cryptograpic_parameters(cp, None, None, None, None) def test_create_cryptograpic_parameters_block_cipher_mode(self): cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'block_cipher_mode': BlockCipherMode.NIST_KEY_WRAP}) self._test_cryptograpic_parameters( cp, CryptographicParameters.BlockCipherMode( BlockCipherMode.NIST_KEY_WRAP), None, None, None) def test_create_cryptograpic_parameters_padding_method(self): cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'padding_method': PaddingMethod.ANSI_X9_23}) # noqa - E128 continuation line under-indented for visual indent self._test_cryptograpic_parameters(cp, None, CryptographicParameters.PaddingMethod(PaddingMethod.ANSI_X9_23), None, None) # noqa def test_create_cryptograpic_parameters_key_role_type(self): cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'key_role_type': KeyRoleType.KEK}) # noqa - E128 continuation line under-indented for visual indent self._test_cryptograpic_parameters(cp, None, None, CryptographicParameters.KeyRoleType(KeyRoleType.KEK), None) # noqa def test_create_cryptograpic_parameters_hashing_algorithm(self): cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {'hashing_algorithm': HashingAlgorithm.SHA_512}) # noqa - E128 continuation line under-indented for visual indent self._test_cryptograpic_parameters(cp, None, None, None, attributes.HashingAlgorithm(HashingAlgorithm.SHA_512)) # noqa def _test_date_value(self, date, value, tag): msg = "expected {0}, received {1}" self.assertIsInstance(date, DateTime, msg.format( DateTime, date.__class__)) self.assertEqual(date.value, value, msg.format(value, date.value)) self.assertEqual(date.tag, tag, msg.format(tag, date.tag)) def test_create_initial_date(self): date = self.factory.create_attribute_value( AttributeType.INITIAL_DATE, 0) self._test_date_value(date, 0, Tags.INITIAL_DATE) def test_create_activation_date(self): date = self.factory.create_attribute_value( AttributeType.ACTIVATION_DATE, 0) self._test_date_value(date, 0, Tags.ACTIVATION_DATE) def test_create_process_start_date(self): date = self.factory.create_attribute_value( AttributeType.PROCESS_START_DATE, 0) self._test_date_value(date, 0, Tags.PROCESS_START_DATE) def test_create_protect_stop_date(self): date = self.factory.create_attribute_value( AttributeType.PROTECT_STOP_DATE, 0) self._test_date_value(date, 0, Tags.PROTECT_STOP_DATE) def test_create_deactivation_date(self): date = self.factory.create_attribute_value( AttributeType.DEACTIVATION_DATE, 0) self._test_date_value(date, 0, Tags.DEACTIVATION_DATE) def test_create_destroy_date(self): date = self.factory.create_attribute_value( AttributeType.DESTROY_DATE, 0) self._test_date_value(date, 0, Tags.DESTROY_DATE) def test_create_compromise_occurance_date(self): date = self.factory.create_attribute_value( AttributeType.COMPROMISE_OCCURRENCE_DATE, 0) self._test_date_value(date, 0, Tags.COMPROMISE_OCCURRENCE_DATE) def test_create_compromise_date(self): date = self.factory.create_attribute_value( AttributeType.COMPROMISE_DATE, 0) self._test_date_value(date, 0, Tags.COMPROMISE_DATE) def test_create_archive_date(self): date = self.factory.create_attribute_value( AttributeType.ARCHIVE_DATE, 0) self._test_date_value(date, 0, Tags.ARCHIVE_DATE)
class TestCryptographicParameters(TestCase): """ A test suite for the CryptographicParameters class """ def setUp(self): super(TestCryptographicParameters, self).setUp() self.bad_enum_code = 8535937 self.factory = AttributeValueFactory() self.cp = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK }) self.cp_none = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, {}) # Symmetric key object with Cryptographic Parameters # Byte stream edited to add Key Role Type parameter # Based on the KMIP Spec 1.1 Test Cases document # 11.1 page 255 on the pdf version self.key_req_with_crypt_params = BytearrayStream(( b'\x42\x00\x2B\x01\x00\x00\x00\x40' b'\x42\x00\x11\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x5F\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00' b'\x42\x00\x38\x05\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00' b'\x42\x00\x83\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' )) def teardown(self): super(TestDigestValue, self).tearDown() def test_write_crypto_params(self): ostream = BytearrayStream() self.cp.write(ostream) self.assertEqual(self.key_req_with_crypt_params, ostream) def test_read_crypto_params(self): CryptographicParameters.read(self.cp, self.key_req_with_crypt_params) self.assertEqual(Tags.BLOCK_CIPHER_MODE.value, self.cp.block_cipher_mode.tag.value) self.assertEqual(BlockCipherMode.CBC.value, self.cp.block_cipher_mode.value.value) self.assertEqual(Tags.PADDING_METHOD.value, self.cp.padding_method.tag.value) self.assertEqual(PaddingMethod.PKCS5.value, self.cp.padding_method.value.value) self.assertEqual(Tags.KEY_ROLE_TYPE.value, self.cp.key_role_type.tag.value) self.assertEqual(KeyRoleType.BDK.value, self.cp.key_role_type.value.value) self.assertEqual(Tags.HASHING_ALGORITHM.value, self.cp.hashing_algorithm.tag.value) self.assertEqual(HashingAlgorithmEnum.SHA_1.value, self.cp.hashing_algorithm.value.value) def test_bad_cipher_mode(self): self.cp.block_cipher_mode = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_padding_method(self): self.cp.padding_method = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_hash_algorithm(self): self.cp.hashing_algorithm = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_key_role_type(self): self.cp.key_role_type = self.bad_enum_code cp_valid = self.factory.create_attribute_value( AttributeType.CRYPTOGRAPHIC_PARAMETERS, { 'block_cipher_mode': BlockCipherMode.CBC, 'padding_method': PaddingMethod.PKCS5, 'hashing_algorithm': HashingAlgorithmEnum.SHA_1, 'key_role_type': KeyRoleType.BDK }) self.assertFalse(self.cp == cp_valid) self.assertRaises(TypeError, self.cp.validate) def test_bad_object(self): name_value = 'puppies' name_type = NameType.UNINTERPRETED_TEXT_STRING bad_obj = Name.create(name_value, name_type) self.assertNotEqual(NotImplemented, bad_obj)