def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Locate request payload and decode it into its constituent parts. Args: input_buffer (stream): A data buffer containing encoded object data, supporting a read method. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be decoded. Optional, defaults to KMIP 1.0. Raises: InvalidKmipEncoding: Raised if the attributes structure is missing from the encoded payload for KMIP 2.0+ encodings. """ super(LocateRequestPayload, self).read(input_buffer, kmip_version=kmip_version) local_buffer = utils.BytearrayStream(input_buffer.read(self.length)) if self.is_tag_next(enums.Tags.MAXIMUM_ITEMS, local_buffer): self._maximum_items = primitives.Integer( tag=enums.Tags.MAXIMUM_ITEMS) self._maximum_items.read(local_buffer, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.OFFSET_ITEMS, local_buffer): self._offset_items = primitives.Integer( tag=enums.Tags.OFFSET_ITEMS) self._offset_items.read(local_buffer, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.STORAGE_STATUS_MASK, local_buffer): self._storage_status_mask = primitives.Integer( tag=enums.Tags.STORAGE_STATUS_MASK) self._storage_status_mask.read(local_buffer, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.OBJECT_GROUP_MEMBER, local_buffer): self._object_group_member = primitives.Enumeration( enums.ObjectGroupMember, tag=enums.Tags.OBJECT_GROUP_MEMBER) self._object_group_member.read(local_buffer, kmip_version=kmip_version) if kmip_version < enums.KMIPVersion.KMIP_2_0: while self.is_tag_next(enums.Tags.ATTRIBUTE, local_buffer): attribute = objects.Attribute() attribute.read(local_buffer, kmip_version=kmip_version) self._attributes.append(attribute) else: if self.is_tag_next(enums.Tags.ATTRIBUTES, local_buffer): attributes = objects.Attributes() attributes.read(local_buffer, kmip_version=kmip_version) # TODO (ph) Add a new utility to avoid using TemplateAttributes temp_attr = objects.convert_attributes_to_template_attribute( attributes) self._attributes = temp_attr.attributes else: raise exceptions.InvalidKmipEncoding( "The Locate request payload encoding is missing the " "attributes structure.")
def test_not_equal_on_equal(self): """ Test that the inequality operator returns False when comparing two Integers with the same values. """ a = primitives.Integer(1) b = primitives.Integer(1) self.assertFalse(a != b) self.assertFalse(b != a)
def test_equal_on_equal_and_empty(self): """ Test that the equality operator returns True when comparing two Integers. """ a = primitives.Integer() b = primitives.Integer() self.assertTrue(a == b) self.assertTrue(b == a)
def test_not_equal_on_equal_and_empty(self): """ Test that the inequality operator returns False when comparing two Integers. """ a = primitives.Integer() b = primitives.Integer() self.assertFalse(a != b) self.assertFalse(b != a)
def test_not_equal_on_not_equal(self): """ Test that the inequality operator returns True when comparing two Integers with different values. """ a = primitives.Integer(1) b = primitives.Integer(2) self.assertTrue(a != b) self.assertTrue(b != a)
def test_equal_on_not_equal(self): """ Test that the equality operator returns False when comparing two Integers with different values. """ a = primitives.Integer(1) b = primitives.Integer(2) self.assertFalse(a == b) self.assertFalse(b == a)
def test_less_than(self): """ Test that the less than operator returns True/False when comparing two Integers with different values. """ a = primitives.Integer(1) b = primitives.Integer(2) self.assertTrue(a < b) self.assertFalse(b < a) self.assertFalse(a < a)
def test_greater_than(self): """ Test that the greater than operator returns True/False when comparing two Integers with different values. """ a = primitives.Integer(1) b = primitives.Integer(2) self.assertFalse(a > b) self.assertTrue(b > a) self.assertFalse(b > b)
def test_comparison(self): """ Test that the equality/inequality operators return True/False when comparing two SplitKey objects with the same data. """ a = secrets.SplitKey() b = secrets.SplitKey() self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a) a = secrets.SplitKey( split_key_parts=4, key_part_identifier=1, split_key_threshold=2, split_key_method=enums.SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8, prime_field_size=104729, key_block=objects.KeyBlock( key_format_type=misc.KeyFormatType(enums.KeyFormatType.RAW), key_value=objects.KeyValue(key_material=objects.KeyMaterial( value=(b'\x66\xC4\x6A\x77\x54\xF9\x4D\xE4' b'\x20\xC7\xB1\xA7\xFF\xF5\xEC\x56'))), cryptographic_algorithm=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM), cryptographic_length=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH))) b = secrets.SplitKey( split_key_parts=4, key_part_identifier=1, split_key_threshold=2, split_key_method=enums.SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8, prime_field_size=104729, key_block=objects.KeyBlock( key_format_type=misc.KeyFormatType(enums.KeyFormatType.RAW), key_value=objects.KeyValue(key_material=objects.KeyMaterial( value=(b'\x66\xC4\x6A\x77\x54\xF9\x4D\xE4' b'\x20\xC7\xB1\xA7\xFF\xF5\xEC\x56'))), cryptographic_algorithm=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM), cryptographic_length=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH))) self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a)
def test_greater_than_or_equal(self): """ Test that the greater than or equal operator returns True/False when comparing two Integers with different values. """ a = primitives.Integer(1) b = primitives.Integer(2) c = primitives.Integer(1) self.assertFalse(a >= b) self.assertTrue(b >= c) self.assertTrue(a >= c) self.assertTrue(a >= a)
def read(self, input_stream): """ Read the data encoding the Check response payload and decode it into its constituent parts. Args: input_stream (stream): A data stream containing encoded object data, supporting a read method; usually a BytearrayStream object. Raises: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(CheckResponsePayload, self).read(input_stream) local_stream = utils.BytearrayStream(input_stream.read(self.length)) if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream): self._unique_identifier = primitives.TextString( tag=enums.Tags.UNIQUE_IDENTIFIER) self._unique_identifier.read(local_stream) if self.is_tag_next(enums.Tags.USAGE_LIMITS_COUNT, local_stream): self._usage_limits_count = primitives.LongInteger( tag=enums.Tags.USAGE_LIMITS_COUNT) self._usage_limits_count.read(local_stream) if self.is_tag_next(enums.Tags.CRYPTOGRAPHIC_USAGE_MASK, local_stream): self._cryptographic_usage_mask = primitives.Integer( tag=enums.Tags.CRYPTOGRAPHIC_USAGE_MASK) self._cryptographic_usage_mask.read(local_stream) if self.is_tag_next(enums.Tags.LEASE_TIME, local_stream): self._lease_time = primitives.Interval(tag=enums.Tags.LEASE_TIME) self._lease_time.read(local_stream) self.is_oversized(local_stream)
def test_not_equal_on_not_equal_template_attribute(self): """ Test that the inequality operator returns True when comparing two Rekey response payloads with different template attributes. """ a = payloads.RekeyResponsePayload( template_attribute=objects.TemplateAttribute(attributes=[ objects.Attribute(attribute_name=objects.Attribute. AttributeName('Cryptographic Algorithm'), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM)) ])) b = payloads.RekeyResponsePayload( template_attribute=objects.TemplateAttribute(attributes=[ objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length'), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) ])) self.assertTrue(a != b) self.assertTrue(b != a)
def test_write(self): """ Test that a SplitKey object can be written to a buffer. """ # TODO (peter-hamilton) Update this test when the KeyBlock supports # generic key format type and key value/material values. key_block = objects.KeyBlock( key_format_type=misc.KeyFormatType(enums.KeyFormatType.RAW), key_value=objects.KeyValue(key_material=objects.KeyMaterial( value=(b'\x66\xC4\x6A\x77\x54\xF9\x4D\xE4' b'\x20\xC7\xB1\xA7\xFF\xF5\xEC\x56'))), cryptographic_algorithm=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM), cryptographic_length=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) split_key = secrets.SplitKey( split_key_parts=4, key_part_identifier=1, split_key_threshold=2, split_key_method=enums.SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8, prime_field_size=104729, key_block=key_block) stream = utils.BytearrayStream() split_key.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_write(self): """ Test that a Rekey response payload can be written to a data stream. """ payload = payloads.RekeyResponsePayload( unique_identifier='8efbbd67-2847-46b5-b7e7-4ab3b5e175de', template_attribute=objects.TemplateAttribute( attributes=[ objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Algorithm' ), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM ) ), objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length' ), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH ) ) ] ) ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_repr(self): """ Test that the representation of an Integer is formatted properly. """ integer = primitives.Integer() value = "value={0}".format(integer.value) self.assertEqual("Integer({0})".format(value), repr(integer))
def test_str(self): """ Test that str can be applied to a SplitKey object. """ key_block = objects.KeyBlock( key_format_type=misc.KeyFormatType(enums.KeyFormatType.RAW), key_value=objects.KeyValue(key_material=objects.KeyMaterial( value=(b'\x66\xC4\x6A\x77\x54\xF9\x4D\xE4' b'\x20\xC7\xB1\xA7\xFF\xF5\xEC\x56'))), cryptographic_algorithm=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM), cryptographic_length=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) split_key = secrets.SplitKey( split_key_parts=4, key_part_identifier=1, split_key_threshold=2, split_key_method=enums.SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8, prime_field_size=104729, key_block=key_block) args = [("split_key_parts", 4), ("key_part_identifier", 1), ("split_key_threshold", 2), ("split_key_method", enums.SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8), ("prime_field_size", 104729), ("key_block", str(key_block))] value = "{}".format(", ".join( ['"{}": {}'.format(arg[0], arg[1]) for arg in args])) self.assertEqual("{" + value + "}", str(split_key))
def test_repr(self): """ Test that repr can be applied to a SplitKey object. """ key_block = objects.KeyBlock( key_format_type=misc.KeyFormatType(enums.KeyFormatType.RAW), key_value=objects.KeyValue(key_material=objects.KeyMaterial( value=(b'\x66\xC4\x6A\x77\x54\xF9\x4D\xE4' b'\x20\xC7\xB1\xA7\xFF\xF5\xEC\x56'))), cryptographic_algorithm=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM), cryptographic_length=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) split_key = secrets.SplitKey( split_key_parts=4, key_part_identifier=1, split_key_threshold=2, split_key_method=enums.SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8, prime_field_size=104729, key_block=key_block) args = [ "split_key_parts=4", "key_part_identifier=1", "split_key_threshold=2", "split_key_method=SplitKeyMethod.POLYNOMIAL_SHARING_GF_2_8", "prime_field_size=104729", "key_block=Struct()" ] self.assertEqual("SplitKey({})".format(", ".join(args)), repr(split_key))
def test_str(self): """ Test that str can be applied to a Rekey response payload """ payload = payloads.RekeyResponsePayload( unique_identifier='49a1ca88-6bea-4fb2-b450-7e58802c3038', template_attribute=objects.TemplateAttribute( attributes=[ objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length' ), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH ) ) ] ) ) # TODO (peter-hamilton) Update this when TemplateAttributes have str expected = str({ 'unique_identifier': '49a1ca88-6bea-4fb2-b450-7e58802c3038', 'template_attribute': 'Struct()' }) observed = str(payload) self.assertEqual(expected, observed)
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Locate response payload and decode it into its constituent parts. Args: input_buffer (stream): A data buffer containing encoded object data, supporting a read method. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be decoded. Optional, defaults to KMIP 1.0. """ super(LocateResponsePayload, self).read(input_buffer, kmip_version=kmip_version) local_buffer = utils.BytearrayStream(input_buffer.read(self.length)) if self.is_tag_next(enums.Tags.LOCATED_ITEMS, local_buffer): self._located_items = primitives.Integer( tag=enums.Tags.LOCATED_ITEMS) self._located_items.read(local_buffer, kmip_version=kmip_version) self._unique_identifiers = [] while self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_buffer): unique_identifier = primitives.TextString( tag=enums.Tags.UNIQUE_IDENTIFIER) unique_identifier.read(local_buffer, kmip_version=kmip_version) self._unique_identifiers.append(unique_identifier) self.is_oversized(local_buffer)
def test_read_value(self): encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00') self.stream = utils.BytearrayStream(encoding) i = primitives.Integer() i.read_value(self.stream) self.assertEqual(1, i.value, self.bad_read.format(1, i.value))
def test_read(self): """ Test that a Rekey response payload can be read from a data stream. """ payload = payloads.RekeyResponsePayload() self.assertEqual(None, payload.unique_identifier) self.assertEqual(None, payload.template_attribute) payload.read(self.full_encoding) self.assertEqual('8efbbd67-2847-46b5-b7e7-4ab3b5e175de', payload.unique_identifier) self.assertEqual( objects.TemplateAttribute(attributes=[ objects.Attribute(attribute_name=objects.Attribute. AttributeName('Cryptographic Algorithm'), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length'), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) ]), payload.template_attribute)
def cryptographic_usage_mask(self, value): if value is None: self._cryptographic_usage_mask = None elif isinstance(value, six.integer_types): self._cryptographic_usage_mask = primitives.Integer( value=value, tag=enums.Tags.CRYPTOGRAPHIC_USAGE_MASK) else: raise TypeError("Cryptographic usage mask must be an integer.")
def attribute_index(self, value): if value is None: self._attribute_index = None elif isinstance(value, six.integer_types): self._attribute_index = primitives.Integer( value=value, tag=enums.Tags.ATTRIBUTE_INDEX) else: raise TypeError("The attribute index must be an integer.")
def offset_items(self, value): if value is None: self._offset_items = None elif isinstance(value, six.integer_types): self._offset_items = primitives.Integer( value=value, tag=enums.Tags.OFFSET_ITEMS) else: raise TypeError("Offset items must be an integer.")
def located_items(self, value): if value is None: self._located_items = None elif isinstance(value, six.integer_types): self._located_items = primitives.Integer( value=value, tag=enums.Tags.LOCATED_ITEMS) else: raise TypeError("Located items must be an integer.")
def maximum_items(self, value): if value is None: self._maximum_items = None elif isinstance(value, six.integer_types): self._maximum_items = primitives.Integer( value=value, tag=enums.Tags.MAXIMUM_ITEMS) else: raise TypeError("Maximum items must be an integer.")
def test_read_on_invalid_length(self): encoding = ( b'\x42\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00') self.stream = utils.BytearrayStream(encoding) i = primitives.Integer() self.assertRaises(exceptions.ReadValueError, i.read, self.stream)
def test_read_on_invalid_padding(self): encoding = ( b'\x42\x00\x00\x02\x00\x00\x00\x04\x00\x00\x00\x00\xff\xff\xff' b'\xff') self.stream = utils.BytearrayStream(encoding) i = primitives.Integer() self.assertRaises(errors.ReadValueError, i.read, self.stream)
def minor(self, value): if value is None: self._minor = None elif isinstance(value, six.integer_types): self._minor = primitives.Integer( value=value, tag=enums.Tags.PROTOCOL_VERSION_MINOR) else: raise TypeError( "Minor protocol version number must be an integer.")
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the ProtocolVersion struct and decode it into its constituent parts. Args: input_stream (stream): A data stream containing encoded object data, supporting a read method; usually a BytearrayStream object. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be decoded. Optional, defaults to KMIP 1.0. Raises: ValueError: Raised if either the major or minor protocol versions are missing from the encoding. """ super(ProtocolVersion, self).read( input_stream, kmip_version=kmip_version ) local_stream = utils.BytearrayStream(input_stream.read(self.length)) if self.is_tag_next(enums.Tags.PROTOCOL_VERSION_MAJOR, local_stream): self._major = primitives.Integer( tag=enums.Tags.PROTOCOL_VERSION_MAJOR ) self._major.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "Invalid encoding missing the major protocol version number." ) if self.is_tag_next(enums.Tags.PROTOCOL_VERSION_MINOR, local_stream): self._minor = primitives.Integer( tag=enums.Tags.PROTOCOL_VERSION_MINOR ) self._minor.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "Invalid encoding missing the minor protocol version number." ) self.is_oversized(local_stream)