def test_comparison(self): """ Test that the equality/inequality operators return True/False when comparing two NewAttribute objects with the same data. """ a = objects.NewAttribute() b = objects.NewAttribute() self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a) a = objects.NewAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM ) ) b = objects.NewAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM ) ) self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a)
def test_read_kmip_2_0(self): """ Test that a ModifyAttribute request payload can be read from a buffer with KMIP 2.0 fields. """ payload = payloads.ModifyAttributeRequestPayload() self.assertIsNone(payload.unique_identifier) self.assertIsNone(payload.attribute) self.assertIsNone(payload.current_attribute) self.assertIsNone(payload.new_attribute) payload.read(self.full_encoding_kmip_2_0, kmip_version=enums.KMIPVersion.KMIP_2_0) self.assertEqual("b4faee10-aa2a-4446-8ad4-0881f3422959", payload.unique_identifier) self.assertIsNone(payload.attribute) self.assertEqual( objects.CurrentAttribute(attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), payload.current_attribute) self.assertEqual( objects.NewAttribute(attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.RSA, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), payload.new_attribute)
def test_comparison(self): """ Test that the equality/inequality operators return True/False when comparing two SetAttribute request payloads with the same data. """ a = payloads.SetAttributeRequestPayload() b = payloads.SetAttributeRequestPayload() self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a) a = payloads.SetAttributeRequestPayload( unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959", new_attribute=objects.NewAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm. AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM))) b = payloads.SetAttributeRequestPayload( unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959", new_attribute=objects.NewAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm. AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM))) self.assertTrue(a == b) self.assertTrue(b == a) 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 Enumerations with different values. """ a = primitives.Enumeration(DummyEnumeration, DummyEnumeration.SMALL) b = primitives.Enumeration(DummyEnumeration, DummyEnumeration.LARGE) 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 Enumerations. """ a = primitives.Enumeration(DummyEnumeration) b = primitives.Enumeration(DummyEnumeration) self.assertFalse(a != b) self.assertFalse(b != a)
def test_not_equal_on_not_equal_enum(self): """ Test that the equality operator returns True when comparing two Enumerations with different enum types. """ a = primitives.Enumeration(DummyEnumeration, DummyEnumeration.SMALL) b = primitives.Enumeration(enums.Tags, enums.Tags.DEFAULT) self.assertTrue(a != b) self.assertTrue(b != a)
def test_equal_on_equal_and_empty(self): """ Test that the equality operator returns True when comparing two Enumerations. """ a = primitives.Enumeration(DummyEnumeration) b = primitives.Enumeration(DummyEnumeration) self.assertTrue(a == b) self.assertTrue(b == a)
def test_not_equal_on_equal(self): """ Test that the inequality operator returns False when comparing two Enumerations with the same values. """ a = primitives.Enumeration(DummyEnumeration, DummyEnumeration.SMALL) b = primitives.Enumeration(DummyEnumeration, DummyEnumeration.SMALL) self.assertFalse(a != b) self.assertFalse(b != a)
def read(self, input_stream): """ Read the data encoding the DeriveKey request 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(DeriveKeyRequestPayload, self).read(input_stream) local_stream = utils.BytearrayStream(input_stream.read(self.length)) if self.is_tag_next(enums.Tags.OBJECT_TYPE, local_stream): self._object_type = primitives.Enumeration( enums.ObjectType, tag=enums.Tags.OBJECT_TYPE) self._object_type.read(local_stream) else: raise ValueError("invalid payload missing object type") unique_identifiers = [] while self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream): unique_identifier = primitives.TextString( tag=enums.Tags.UNIQUE_IDENTIFIER) unique_identifier.read(local_stream) unique_identifiers.append(unique_identifier) if not unique_identifiers: raise ValueError("invalid payload missing unique identifiers") else: self._unique_identifiers = unique_identifiers if self.is_tag_next(enums.Tags.DERIVATION_METHOD, local_stream): self._derivation_method = primitives.Enumeration( enums.DerivationMethod, tag=enums.Tags.DERIVATION_METHOD) self._derivation_method.read(local_stream) else: raise ValueError("invalid payload missing derivation method") if self.is_tag_next(enums.Tags.DERIVATION_PARAMETERS, local_stream): self._derivation_parameters = attributes.DerivationParameters() self._derivation_parameters.read(local_stream) else: raise ValueError("invalid payload missing derivation parameters") if self.is_tag_next(enums.Tags.TEMPLATE_ATTRIBUTE, local_stream): self._template_attribute = objects.TemplateAttribute() self._template_attribute.read(local_stream) else: raise ValueError("invalid payload missing template attribute") self.is_oversized(local_stream)
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_str(self): """ Test that the string representation of an Enumeration is formatted properly. """ enum = primitives.Enumeration(DummyEnumeration, DummyEnumeration.SMALL) self.assertEqual(str(DummyEnumeration.SMALL), str(enum.value))
def read(self, input_stream): """ Read the data encoding the Cancel 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(CancelResponsePayload, self).read(input_stream) local_stream = utils.BytearrayStream(input_stream.read(self.length)) if self.is_tag_next( enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE, local_stream ): self._asynchronous_correlation_value = primitives.ByteString( tag=enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE ) self._asynchronous_correlation_value.read(local_stream) if self.is_tag_next(enums.Tags.CANCELLATION_RESULT, local_stream): self._cancellation_result = primitives.Enumeration( enums.CancellationResult, tag=enums.Tags.CANCELLATION_RESULT ) self._cancellation_result.read(local_stream) self.is_oversized(local_stream)
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_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 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_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_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 write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Write the data encoding the GetAttributeList response payload to a stream. Args: output_buffer (stream): A data stream in which to encode object data, supporting a write method; usually a BytearrayStream object. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be encoded. Optional, defaults to KMIP 1.0. Raises: InvalidField: Raised if the unique identifier or attribute name are not defined. """ local_buffer = utils.BytearrayStream() if self._unique_identifier: self._unique_identifier.write(local_buffer, kmip_version=kmip_version) else: raise exceptions.InvalidField( "The GetAttributeList response payload is missing the unique " "identifier field.") if self._attribute_names: if kmip_version < enums.KMIPVersion.KMIP_2_0: for attribute_name in self._attribute_names: attribute_name.write(local_buffer, kmip_version=kmip_version) else: # NOTE (ph) This approach simplifies backwards compatible # issues but limits easy support for Attribute # Reference structures going forward, specifically # limiting the use of VendorIdentification for # custom attributes. If custom attributes need to # be retrieved using the GetAttributeList operation # for KMIP 2.0 applications this code will need to # change. for attribute_name in self._attribute_names: t = enums.convert_attribute_name_to_tag( attribute_name.value) e = primitives.Enumeration( enums.Tags, value=t, tag=enums.Tags.ATTRIBUTE_REFERENCE) e.write(local_buffer, kmip_version=kmip_version) else: raise exceptions.InvalidField( "The GetAttributeList response payload is missing the " "attribute names field.") self.length = local_buffer.length() super(GetAttributeListResponsePayload, self).write(output_buffer, kmip_version=kmip_version) output_buffer.write(local_buffer.buffer)
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_init_unset(self): """ Test that an Enumeration can be instantiated with no input. """ enum = primitives.Enumeration(DummyEnumeration) self.assertEqual(DummyEnumeration, enum.enum) self.assertEqual(None, enum.value) self.assertEqual(enums.Tags.DEFAULT, enum.tag)
def test_read(self): """ Test that an Enumeration can be read from a byte stream. """ stream = utils.BytearrayStream(self.encoding) enum = primitives.Enumeration(DummyEnumeration) enum.read(stream) self.assertEqual(DummyEnumeration.SMALL, enum.value)
def object_type(self, value): if value is None: self._object_type = None elif isinstance(value, enums.ObjectType): self._object_type = primitives.Enumeration( enums.ObjectType, value=value, tag=enums.Tags.OBJECT_TYPE) else: raise TypeError("object type must be an ObjectType enumeration")
def test_comparison(self): """ Test that the equality/inequality operators return True/False when comparing two ModifyAttribute request payloads with the same data. """ a = payloads.ModifyAttributeRequestPayload() b = payloads.ModifyAttributeRequestPayload() self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a) a = payloads.ModifyAttributeRequestPayload( unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959", attribute=objects.Attribute( attribute_name=objects.Attribute.AttributeName("x-attribute1"), attribute_value=primitives.TextString( value="ModifiedValue1", tag=enums.Tags.ATTRIBUTE_VALUE)), current_attribute=objects.CurrentAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm. AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), new_attribute=objects.NewAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm. RSA, enums.Tags.CRYPTOGRAPHIC_ALGORITHM))) b = payloads.ModifyAttributeRequestPayload( unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959", attribute=objects.Attribute( attribute_name=objects.Attribute.AttributeName("x-attribute1"), attribute_value=primitives.TextString( value="ModifiedValue1", tag=enums.Tags.ATTRIBUTE_VALUE)), current_attribute=objects.CurrentAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm. AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), new_attribute=objects.NewAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm. RSA, enums.Tags.CRYPTOGRAPHIC_ALGORITHM))) self.assertTrue(a == b) self.assertTrue(b == a) self.assertFalse(a != b) self.assertFalse(b != a)
def test_init(self): """ Test that an Enumeration can be instantiated. """ enum = primitives.Enumeration(DummyEnumeration, DummyEnumeration.SMALL, enums.Tags.ACTIVATION_DATE) self.assertEqual(DummyEnumeration, enum.enum) self.assertEqual(DummyEnumeration.SMALL, enum.value) self.assertEqual(enums.Tags.ACTIVATION_DATE, enum.tag)
def test_read_on_invalid_length(self): """ Test that an InvalidPrimitiveLength exception is thrown when attempting to decode an Enumeration with an invalid length. """ stream = utils.BytearrayStream(self.encoding_bad_length) enum = primitives.Enumeration(enums.Tags) self.assertRaises(exceptions.InvalidPrimitiveLength, enum.read, stream)
def test_read_on_invalid_padding(self): """ Test that an InvalidPrimitiveLength exception is thrown when attempting to decode an Enumeration with invalid padding bytes. """ stream = utils.BytearrayStream(self.encoding_bad_padding) enum = primitives.Enumeration(enums.Types) self.assertRaises(exceptions.InvalidPaddingBytes, enum.read, stream)
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Create 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 object type or template attribute is missing from the encoded payload. """ super(CreateRequestPayload, self).read(input_buffer, kmip_version=kmip_version) local_buffer = utils.BytearrayStream(input_buffer.read(self.length)) if self.is_tag_next(enums.Tags.OBJECT_TYPE, local_buffer): self._object_type = primitives.Enumeration( enums.ObjectType, tag=enums.Tags.OBJECT_TYPE) self._object_type.read(local_buffer, kmip_version=kmip_version) else: raise exceptions.InvalidKmipEncoding( "The Create request payload encoding is missing the object " "type.") if kmip_version < enums.KMIPVersion.KMIP_2_0: if self.is_tag_next(enums.Tags.TEMPLATE_ATTRIBUTE, local_buffer): self._template_attribute = objects.TemplateAttribute() self._template_attribute.read(local_buffer, kmip_version=kmip_version) else: raise exceptions.InvalidKmipEncoding( "The Create request payload encoding is missing the " "template attribute.") else: # NOTE (ph) For now, leave attributes natively in TemplateAttribute # form and just convert to the KMIP 2.0 Attributes form as needed # for encoding/decoding purposes. Changing the payload to require # the new Attributes structure will trigger a bunch of second-order # effects across the client and server codebases that is beyond # the scope of updating the Create payloads to support KMIP 2.0. if self.is_tag_next(enums.Tags.ATTRIBUTES, local_buffer): attributes = objects.Attributes() attributes.read(local_buffer, kmip_version=kmip_version) value = objects.convert_attributes_to_template_attribute( attributes) self._template_attribute = value else: raise exceptions.InvalidKmipEncoding( "The Create request payload encoding is missing the " "attributes structure.") self.is_oversized(local_buffer)
def test_not_equal_on_equal(self): """ Test that the inequality operator returns False when comparing two Rekey response payloads with the same data. """ a = payloads.RekeyResponsePayload() b = payloads.RekeyResponsePayload() self.assertFalse(a != b) self.assertFalse(b != a) a = payloads.RekeyResponsePayload( unique_identifier='1346d253-69d6-474c-8cd5-ad475a3e0a81', 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)) ])) b = payloads.RekeyResponsePayload( unique_identifier='1346d253-69d6-474c-8cd5-ad475a3e0a81', 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)) ])) self.assertFalse(a != b) self.assertFalse(b != a)
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Get request 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. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be decoded. Optional, defaults to KMIP 1.0. """ super(GetRequestPayload, self).read(input_stream, kmip_version=kmip_version) 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, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.KEY_FORMAT_TYPE, local_stream): self._key_format_type = primitives.Enumeration( enum=enums.KeyFormatType, tag=enums.Tags.KEY_FORMAT_TYPE) self._key_format_type.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.KEY_COMPRESSION_TYPE, local_stream): self._key_compression_type = primitives.Enumeration( enum=enums.KeyCompressionType, tag=enums.Tags.KEY_COMPRESSION_TYPE) self._key_compression_type.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.KEY_WRAPPING_SPECIFICATION, local_stream): self._key_wrapping_specification = \ objects.KeyWrappingSpecification() self._key_wrapping_specification.read(local_stream, kmip_version=kmip_version) self.is_oversized(local_stream)