def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Decrypt 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. Raises: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(DecryptRequestPayload, 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.CRYPTOGRAPHIC_PARAMETERS, local_stream): self._cryptographic_parameters = \ attributes.CryptographicParameters() self._cryptographic_parameters.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream, kmip_version=kmip_version) else: raise ValueError("invalid payload missing the data attribute") if self.is_tag_next(enums.Tags.IV_COUNTER_NONCE, local_stream): self._iv_counter_nonce = primitives.ByteString( tag=enums.Tags.IV_COUNTER_NONCE) self._iv_counter_nonce.read(local_stream, kmip_version=kmip_version) if kmip_version >= enums.KMIPVersion.KMIP_1_4: if self.is_tag_next( enums.Tags.AUTHENTICATED_ENCRYPTION_ADDITIONAL_DATA, local_stream): self._auth_additional_data = primitives.ByteString( tag=enums.Tags.AUTHENTICATED_ENCRYPTION_ADDITIONAL_DATA) self._auth_additional_data.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.AUTHENTICATED_ENCRYPTION_TAG, local_stream): self._auth_tag = primitives.ByteString( tag=enums.Tags.AUTHENTICATED_ENCRYPTION_TAG) self._auth_tag.read(local_stream, kmip_version=kmip_version) self.is_oversized(local_stream)
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the SignatureVerify 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. Raises: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(SignatureVerifyRequestPayload, 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.CRYPTOGRAPHIC_PARAMETERS, local_stream): self._cryptographic_parameters = \ attributes.CryptographicParameters() self._cryptographic_parameters.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.DIGESTED_DATA, local_stream): self._digested_data = primitives.ByteString( tag=enums.Tags.DIGESTED_DATA) self._digested_data.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.SIGNATURE_DATA, local_stream): self._signature_data = primitives.ByteString( tag=enums.Tags.SIGNATURE_DATA) self._signature_data.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.CORRELATION_VALUE, local_stream): self._correlation_value = primitives.ByteString( tag=enums.Tags.CORRELATION_VALUE) self._correlation_value.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.INIT_INDICATOR, local_stream): self._init_indicator = primitives.Boolean( tag=enums.Tags.INIT_INDICATOR) self._init_indicator.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.FINAL_INDICATOR, local_stream): self._final_indicator = primitives.Boolean( tag=enums.Tags.FINAL_INDICATOR) self._final_indicator.read(local_stream, kmip_version=kmip_version) self.is_oversized(local_stream)
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the SignatureVerify 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. 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 the data attribute is missing from the encoded payload. """ super(SignatureVerifyResponsePayload, 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) else: raise ValueError( "Parsed payload encoding is missing the unique identifier " "field.") if self.is_tag_next(enums.Tags.VALIDITY_INDICATOR, local_stream): self._validity_indicator = primitives.Enumeration( enums.ValidityIndicator, tag=enums.Tags.VALIDITY_INDICATOR) self._validity_indicator.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "Parsed payload encoding is missing the validity indicator " "field.") if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.CORRELATION_VALUE, local_stream): self._correlation_value = primitives.ByteString( tag=enums.Tags.CORRELATION_VALUE) self._correlation_value.read(local_stream, kmip_version=kmip_version) self.is_oversized(local_stream)
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Poll 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. Raises: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(PollRequestPayload, self).read(input_stream, kmip_version=kmip_version) 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, kmip_version=kmip_version) self.is_oversized(local_stream)
def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0): super(ResponseHeader, self).read( istream, kmip_version=kmip_version ) tstream = BytearrayStream(istream.read(self.length)) self.protocol_version = contents.ProtocolVersion() self.protocol_version.read(tstream, kmip_version=kmip_version) kmip_version = contents.protocol_version_to_kmip_version( self.protocol_version ) self.time_stamp = contents.TimeStamp() self.time_stamp.read(tstream, kmip_version=kmip_version) if kmip_version >= enums.KMIPVersion.KMIP_2_0: if self.is_tag_next(enums.Tags.SERVER_HASHED_PASSWORD, tstream): server_hashed_password = primitives.ByteString( tag=enums.Tags.SERVER_HASHED_PASSWORD ) server_hashed_password.read(tstream, kmip_version=kmip_version) self._server_hashed_password = server_hashed_password self.batch_count = contents.BatchCount() self.batch_count.read(tstream, kmip_version=kmip_version) self.is_oversized(tstream) self.validate()
def read(self, input_stream): """ Read the data encoding the Cancel 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(CancelRequestPayload, 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) self.is_oversized(local_stream)
def read(self, input_stream): """ Read the data encoding the Decrypt 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 unique_identifier or data attributes are missing from the encoded payload. """ super(DecryptResponsePayload, 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) else: raise ValueError( "invalid payload missing the unique identifier attribute") if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream) else: raise ValueError("invalid payload missing the data attribute") self.is_oversized(local_stream)
def test_init_unset(self): bs = primitives.ByteString() self.assertIsInstance( bs.value, bytes, self.bad_type.format('value', type(None), type(bs.value))) self.assertEqual(bytes(), bs.value, self.bad_value.format('value', None, bs.value))
def test_read_on_invalid_padding(self): encoding = ( b'\x42\x00\x00\x08\x00\x00\x00\x03\x01\x02\x03\xff\xff\xff\xff' b'\xff') self.stream = utils.BytearrayStream(encoding) bs = primitives.ByteString() self.assertRaises(errors.ReadValueError, bs.read, self.stream)
def asynchronous_correlation_value(self, value): if value is None: self._asynchronous_correlation_value = None elif isinstance(value, six.binary_type): self._asynchronous_correlation_value = primitives.ByteString( value=value, tag=enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE) else: raise TypeError("Asynchronous correlation value must be bytes.")
def iv_counter_nonce(self, value): if value is None: self._iv_counter_nonce = None elif isinstance(value, six.binary_type): self._iv_counter_nonce = primitives.ByteString( value=value, tag=enums.Tags.IV_COUNTER_NONCE) else: raise TypeError("IV/counter/nonce must be bytes")
def data(self, value): if value is None: self._data = None elif isinstance(value, six.binary_type): self._data = primitives.ByteString(value=value, tag=enums.Tags.DATA) else: raise TypeError("data must be bytes")
def correlation_value(self, value): if value is None: self._correlation_value = None elif isinstance(value, six.binary_type): self._correlation_value = primitives.ByteString( value=value, tag=enums.Tags.CORRELATION_VALUE) else: raise TypeError("Correlation value must be bytes.")
def signature_data(self, value): if value is None: self._signature_data = None elif isinstance(value, six.binary_type): self._signature_data = primitives.ByteString( value=value, tag=enums.Tags.SIGNATURE_DATA) else: raise TypeError("Signature data must be bytes.")
def auth_tag(self, value): if value is None: self._auth_tag = None elif isinstance(value, six.binary_type): self._auth_tag = primitives.ByteString( value=value, tag=enums.Tags.AUTHENTICATED_ENCRYPTION_TAG) else: raise TypeError("authenticated encryption tag must be bytes")
def test_init(self): value = b'\x01\x02\x03' bs = primitives.ByteString(value) self.assertIsInstance( bs.value, bytes, self.bad_type.format('value', bytes, type(bs.value))) self.assertEqual(value, bs.value, self.bad_value.format('value', value, bs.value))
def auth_additional_data(self, value): if value is None: self._auth_additional_data = None elif isinstance(value, six.binary_type): self._auth_additional_data = primitives.ByteString( value=value, tag=enums.Tags.AUTHENTICATED_ENCRYPTION_ADDITIONAL_DATA) else: raise TypeError("authenticated additional data must be bytes")
def read(self, input_stream): """ Read the data encoding the Encrypt 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(EncryptRequestPayload, 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.CRYPTOGRAPHIC_PARAMETERS, local_stream ): self._cryptographic_parameters = \ attributes.CryptographicParameters() self._cryptographic_parameters.read(local_stream) if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream) else: raise ValueError("invalid payload missing the data attribute") if self.is_tag_next(enums.Tags.IV_COUNTER_NONCE, local_stream): self._iv_counter_nonce = primitives.ByteString( tag=enums.Tags.IV_COUNTER_NONCE ) self._iv_counter_nonce.read(local_stream) self.is_oversized(local_stream)
def test_read_value_zero(self): encoding = b'\x00\x00\x00\x00\x00\x00\x00\x00' self.stream = utils.BytearrayStream(encoding) bs = primitives.ByteString() bs.length = 0x01 bs.read_value(self.stream) expected = b'\x00' self.assertEqual(expected, bs.value, self.bad_read.format('value', expected, bs.value))
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Encrypt 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. 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 the unique_identifier or data attributes are missing from the encoded payload. """ super(EncryptResponsePayload, 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) else: raise ValueError( "invalid payload missing the unique identifier attribute") if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream, kmip_version=kmip_version) else: raise ValueError("invalid payload missing the data attribute") if self.is_tag_next(enums.Tags.IV_COUNTER_NONCE, local_stream): self._iv_counter_nonce = primitives.ByteString( tag=enums.Tags.IV_COUNTER_NONCE) self._iv_counter_nonce.read(local_stream, kmip_version=kmip_version) self.is_oversized(local_stream)
def test_read_value_no_padding(self): encoding = b'\x01\x02\x03\x04\x05\x06\x07\x08' self.stream = utils.BytearrayStream(encoding) bs = primitives.ByteString() bs.length = 0x08 bs.read_value(self.stream) expected = b'\x01\x02\x03\x04\x05\x06\x07\x08' self.assertEqual(expected, bs.value, self.bad_read.format('value', expected, bs.value))
def test_read(self): encoding = ( b'\x42\x00\x00\x08\x00\x00\x00\x03\x01\x02\x03\x00\x00\x00\x00' b'\x00') self.stream = utils.BytearrayStream(encoding) bs = primitives.ByteString() bs.read(self.stream) expected = b'\x01\x02\x03' self.assertEqual(expected, bs.value, self.bad_read.format('value', expected, bs.value))
def server_hashed_password(self, value): if value is None: self._server_hashed_password = None elif isinstance(value, six.binary_type): self._server_hashed_password = primitives.ByteString( value=value, tag=enums.Tags.SERVER_HASHED_PASSWORD ) else: raise TypeError( "The server hashed password must be a binary string." )
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Sign request payload and decode it into its parts Args: input_stream (stream): A data stream 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: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(SignRequestPayload, 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.CRYPTOGRAPHIC_PARAMETERS, local_stream ): self._cryptographic_parameters = \ attributes.CryptographicParameters() self._cryptographic_parameters.read( local_stream, kmip_version=kmip_version ) if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "invalid payload missing the data attribute" )
def test_write_value_zero(self): encoding = b'\x00\x00\x00\x00\x00\x00\x00\x00' self.stream = utils.BytearrayStream() value = b'\x00' bs = primitives.ByteString(value) bs.write_value(self.stream) result = self.stream.read() len_exp = len(encoding) len_rcv = len(result) self.assertEqual(len_exp, len_rcv, self.bad_length.format(len_exp, len_rcv)) self.assertEqual(encoding, result, self.bad_encoding)
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the Sign response payload and decode it. 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 the unique_identifier or signature attributes are missing from the encoded payload. """ super(SignResponsePayload, 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 ) else: raise ValueError( "invalid payload missing the unique identifier attribute" ) if self.is_tag_next(enums.Tags.SIGNATURE_DATA, local_stream): self._signature_data = primitives.ByteString( tag=enums.Tags.SIGNATURE_DATA ) self._signature_data.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "invalid payload missing the signature data attribute" )
def test_validate_on_invalid_type(self): bs = primitives.ByteString() bs.value = 0 self.assertRaises(TypeError, bs.validate)
def test_validate_on_valid_unset(self): bs = primitives.ByteString() # Check no exception thrown. bs.validate()
def test_validate_on_valid(self): bs = primitives.ByteString() bs.value = b'\x00' # Check no exception thrown. bs.validate()