Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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))
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
 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.")
Ejemplo n.º 11
0
 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")
Ejemplo n.º 12
0
 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")
Ejemplo n.º 13
0
 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.")
Ejemplo n.º 14
0
 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.")
Ejemplo n.º 15
0
 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")
Ejemplo n.º 16
0
    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))
Ejemplo n.º 17
0
 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")
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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))
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
0
    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))
Ejemplo n.º 22
0
    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))
Ejemplo n.º 23
0
 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."
         )
Ejemplo n.º 24
0
    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"
            )
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
    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"
            )
Ejemplo n.º 27
0
    def test_validate_on_invalid_type(self):
        bs = primitives.ByteString()
        bs.value = 0

        self.assertRaises(TypeError, bs.validate)
Ejemplo n.º 28
0
    def test_validate_on_valid_unset(self):
        bs = primitives.ByteString()

        # Check no exception thrown.
        bs.validate()
Ejemplo n.º 29
0
    def test_validate_on_valid(self):
        bs = primitives.ByteString()
        bs.value = b'\x00'

        # Check no exception thrown.
        bs.validate()