def test_create(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) attributes = self._get_attrs() template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.create(obj_type, template_attribute) self.assertNotEqual(None, res, 'result is None') self.assertEqual(ResultStatus.SUCCESS, res.result_status.value, 'result status did not return success')
def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the QueryResponsePayload object and decode it into its constituent parts. Args: istream (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(QueryResponsePayload, self).read(istream, kmip_version=kmip_version) tstream = BytearrayStream(istream.read(self.length)) while (self.is_tag_next(enums.Tags.OPERATION, tstream)): operation = Operation() operation.read(tstream, kmip_version=kmip_version) self.operations.append(operation) while (self.is_tag_next(enums.Tags.OBJECT_TYPE, tstream)): object_type = ObjectType() object_type.read(tstream, kmip_version=kmip_version) self.object_types.append(object_type) if self.is_tag_next(enums.Tags.VENDOR_IDENTIFICATION, tstream): self.vendor_identification = VendorIdentification() self.vendor_identification.read(tstream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.SERVER_INFORMATION, tstream): self.server_information = ServerInformation() self.server_information.read(tstream, kmip_version=kmip_version) while (self.is_tag_next(enums.Tags.APPLICATION_NAMESPACE, tstream)): application_namespace = ApplicationNamespace() application_namespace.read(tstream, kmip_version=kmip_version) self.application_namespaces.append(application_namespace) while (self.is_tag_next(enums.Tags.EXTENSION_INFORMATION, tstream)): extension_information = ExtensionInformation() extension_information.read(tstream, kmip_version=kmip_version) self.extension_information.append(extension_information) self.is_oversized(tstream) self.validate()
def test_register(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) key = self._get_symmetric_key() attributes = [] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertNotEqual(None, res, 'result is None') self.assertEqual(ResultStatus.SUCCESS, res.result_status.value, 'result status did not return success')
def test_create_no_usage_mask(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) attributes = [self._get_attrs()[0]] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.create(obj_type, template_attribute) self.assertNotEqual(None, res, 'result is None') self.assertEqual( ResultStatus.OPERATION_FAILED, res.result_status.value, 'result status did not return failed')
def read(self, istream): """ Read the data encoding the QueryResponsePayload object and decode it into its constituent parts. Args: istream (Stream): A data stream containing encoded object data, supporting a read method; usually a BytearrayStream object. """ super(QueryResponsePayload, self).read(istream) tstream = BytearrayStream(istream.read(self.length)) while(self.is_tag_next(Tags.OPERATION, tstream)): operation = Operation() operation.read(tstream) self.operations.append(operation) while(self.is_tag_next(Tags.OBJECT_TYPE, tstream)): object_type = ObjectType() object_type.read(tstream) self.object_types.append(object_type) if self.is_tag_next(Tags.VENDOR_IDENTIFICATION, tstream): self.vendor_identification = VendorIdentification() self.vendor_identification.read(tstream) if self.is_tag_next(Tags.SERVER_INFORMATION, tstream): self.server_information = ServerInformation() self.server_information.read(tstream) while(self.is_tag_next(Tags.APPLICATION_NAMESPACE, tstream)): application_namespace = ApplicationNamespace() application_namespace.read(tstream) self.application_namespaces.append(application_namespace) while(self.is_tag_next(Tags.EXTENSION_INFORMATION, tstream)): extension_information = ExtensionInformation() extension_information.read(tstream) self.extension_information.append(extension_information) self.is_oversized(tstream) self.validate()
def test_create_no_length(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) attributes = self._get_attrs()[0:2] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.create(obj_type, template_attribute) self.assertNotEqual(None, res, 'result is None') attrs = res.template_attribute.attributes self.assertEqual(ResultStatus.SUCCESS, res.result_status.value, 'result status did not return success') self.assertTrue(self._check_attr_exists(attributes[2], attrs), 'length attribute not returned')
def test_register_length_in_template_and_key_block(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) key = self._get_symmetric_key() attributes = [self._get_length_attr()] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertEqual(ResultStatus.OPERATION_FAILED, res.result_status.value, 'result status did not return failed') self.assertEqual(ResultReason.INDEX_OUT_OF_BOUNDS, res.result_reason.value, 'result reason did not match')
def test_register_attrs_in_template(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) key = self._get_symmetric_key() key.key_block.cryptographic_algorithm = None key.key_block.cryptographic_length = None key.key_block.key_value.attributes = [] attributes = self._get_attrs() template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertNotEqual(None, res, 'result is None') self.assertEqual(ResultStatus.SUCCESS, res.result_status.value, 'result status did not return success')
def test_register_no_usage_mask(self): obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) key = self._get_symmetric_key() key.key_block.key_value.attributes = [] attributes = [] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertEqual(ResultStatus.OPERATION_FAILED, res.result_status.value, 'result status did not return failed') self.assertEqual(ResultReason.ITEM_NOT_FOUND, res.result_reason.value, 'result reason did not match')
def test_register_invalid_length(self): unsupported_lens = (-1, 0, 2048, 5, 18) for len in unsupported_lens: obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) key = self._get_symmetric_key() key.key_block.cryptographic_length = CryptographicLength(len) attributes = [] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertEqual(ResultStatus.OPERATION_FAILED, res.result_status.value, 'result status did not return failed') self.assertEqual(ResultReason.INVALID_FIELD, res.result_reason.value, 'result reason did not match')
def get(self, uuid=None, key_format_type=None, key_compression_type=None, key_wrapping_specification=None, credential=None): self.logger.debug('get() called') ret_value = RS.OPERATION_FAILED if uuid is None or not hasattr(uuid, 'value'): self.logger.debug('no uuid provided') reason = ResultReason(ResultReasonEnum.ITEM_NOT_FOUND) message = ResultMessage('') return GetResult(ResultStatus(ret_value), reason, message) if key_format_type is None: self.logger.debug('key format type is None, setting to raw') key_format_type = KeyFormatType(KeyFormatTypeEnum.RAW) if key_format_type.value != KeyFormatTypeEnum.RAW: self.logger.debug('key format type is not raw') reason = ResultReason(ResultReasonEnum. KEY_FORMAT_TYPE_NOT_SUPPORTED) message = ResultMessage('') return GetResult(ResultStatus(ret_value), reason, message) if key_compression_type is not None: self.logger.debug('key compression type is not None') reason = ResultReason(ResultReasonEnum. KEY_COMPRESSION_TYPE_NOT_SUPPORTED) message = ResultMessage('') return GetResult(ResultStatus(ret_value), reason, message) if key_wrapping_specification is not None: self.logger.debug('key wrapping specification is not None') reason = ResultReason(ResultReasonEnum.FEATURE_NOT_SUPPORTED) message = ResultMessage('key wrapping is not currently supported') return GetResult(ResultStatus(ret_value), reason, message) self.logger.debug('retrieving object from repo') managed_object, _ = self.repo.get(uuid.value) if managed_object is None: self.logger.debug('object not found in repo') reason = ResultReason(ResultReasonEnum.ITEM_NOT_FOUND) message = ResultMessage('') return GetResult(ResultStatus(ret_value), reason, message) # currently only symmetric keys are supported, fix this in future object_type = ObjectType(OT.SYMMETRIC_KEY) ret_value = RS.SUCCESS return GetResult(ResultStatus(ret_value), object_type=object_type, uuid=uuid, secret=managed_object)
def test_register_object_type_mismatch(self): unsupported_types = (ObjectTypeEnum.CERTIFICATE, ObjectTypeEnum.PUBLIC_KEY, ObjectTypeEnum.PRIVATE_KEY, ObjectTypeEnum.SPLIT_KEY, ObjectTypeEnum.TEMPLATE, ObjectTypeEnum.SECRET_DATA, ObjectTypeEnum.OPAQUE_DATA) for unsupported_type in unsupported_types: obj_type = ObjectType(unsupported_type) key = self._get_symmetric_key() attributes = [] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertNotEqual(None, res, 'result is None') self.assertEqual(ResultStatus.OPERATION_FAILED, res.result_status.value, 'result status did not return failed') self.assertEqual(ResultReason.INVALID_FIELD, res.result_reason.value, 'result reason did not match')
def test_register_invalid_alg(self): unsupported_algs = (CryptoAlgorithmEnum.RSA, CryptoAlgorithmEnum.DSA, CryptoAlgorithmEnum.ECDSA, CryptoAlgorithmEnum.HMAC_SHA1, CryptoAlgorithmEnum.HMAC_SHA224, CryptoAlgorithmEnum.HMAC_SHA256, CryptoAlgorithmEnum.HMAC_SHA384, CryptoAlgorithmEnum.HMAC_SHA512, CryptoAlgorithmEnum.HMAC_MD5, CryptoAlgorithmEnum.DH, CryptoAlgorithmEnum.ECDH, CryptoAlgorithmEnum.ECMQV, CryptoAlgorithmEnum.BLOWFISH, CryptoAlgorithmEnum.CAMELLIA, CryptoAlgorithmEnum.CAST5, CryptoAlgorithmEnum.IDEA, CryptoAlgorithmEnum.MARS, CryptoAlgorithmEnum.RC2, CryptoAlgorithmEnum.RC4, CryptoAlgorithmEnum.RC5, CryptoAlgorithmEnum.SKIPJACK, CryptoAlgorithmEnum.TWOFISH) for alg in unsupported_algs: obj_type = ObjectType(ObjectTypeEnum.SYMMETRIC_KEY) key = self._get_symmetric_key() key.key_block.cryptographic_algorithm = CryptographicAlgorithm(alg) attributes = [] template_attribute = TemplateAttribute(attributes=attributes) res = self.kmip.register(obj_type, template_attribute, key) self.assertEqual(ResultStatus.OPERATION_FAILED, res.result_status.value, 'result status did not return failed') self.assertEqual(ResultReason.INVALID_FIELD, res.result_reason.value, 'result reason did not match')
def setUp(self): super(TestQueryResponsePayload, self).setUp() self.operations = list() self.object_types = list() self.application_namespaces = list() self.extension_information = list() self.vendor_identification = VendorIdentification( "IBM test server, not-TKLM 2.0.1.1 KMIP 2.0.0.1") self.server_information = ServerInformation() self.operations.append(Operation(OperationEnum.CREATE)) self.operations.append(Operation(OperationEnum.CREATE_KEY_PAIR)) self.operations.append(Operation(OperationEnum.REGISTER)) self.operations.append(Operation(OperationEnum.REKEY)) self.operations.append(Operation(OperationEnum.CERTIFY)) self.operations.append(Operation(OperationEnum.RECERTIFY)) self.operations.append(Operation(OperationEnum.LOCATE)) self.operations.append(Operation(OperationEnum.CHECK)) self.operations.append(Operation(OperationEnum.GET)) self.operations.append(Operation(OperationEnum.GET_ATTRIBUTES)) self.operations.append(Operation(OperationEnum.GET_ATTRIBUTE_LIST)) self.operations.append(Operation(OperationEnum.ADD_ATTRIBUTE)) self.operations.append(Operation(OperationEnum.MODIFY_ATTRIBUTE)) self.operations.append(Operation(OperationEnum.DELETE_ATTRIBUTE)) self.operations.append(Operation(OperationEnum.OBTAIN_LEASE)) self.operations.append(Operation(OperationEnum.GET_USAGE_ALLOCATION)) self.operations.append(Operation(OperationEnum.ACTIVATE)) self.operations.append(Operation(OperationEnum.REVOKE)) self.operations.append(Operation(OperationEnum.DESTROY)) self.operations.append(Operation(OperationEnum.ARCHIVE)) self.operations.append(Operation(OperationEnum.RECOVER)) self.operations.append(Operation(OperationEnum.QUERY)) self.operations.append(Operation(OperationEnum.CANCEL)) self.operations.append(Operation(OperationEnum.POLL)) self.operations.append(Operation(OperationEnum.REKEY_KEY_PAIR)) self.operations.append(Operation(OperationEnum.DISCOVER_VERSIONS)) self.object_types.append(ObjectType(ObjectTypeEnum.CERTIFICATE)) self.object_types.append(ObjectType(ObjectTypeEnum.SYMMETRIC_KEY)) self.object_types.append(ObjectType(ObjectTypeEnum.PUBLIC_KEY)) self.object_types.append(ObjectType(ObjectTypeEnum.PRIVATE_KEY)) self.object_types.append(ObjectType(ObjectTypeEnum.TEMPLATE)) self.object_types.append(ObjectType(ObjectTypeEnum.SECRET_DATA)) self.extension_information.append( ExtensionInformation( extension_name=ExtensionName("ACME LOCATION"))) self.extension_information.append( ExtensionInformation( extension_name=ExtensionName("ACME ZIP CODE"))) self.encoding_a = utils.BytearrayStream( (b'\x42\x00\x7C\x01\x00\x00\x00\x00')) self.encoding_b = utils.BytearrayStream(( b'\x42\x00\x7C\x01\x00\x00\x02\x40\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x01\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x02\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x03\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x04\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x06\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x07\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x08\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x09\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x0A\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x0B\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x0C\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x0D\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x0E\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x0F\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x10\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x11\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x12\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x13\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x14\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x15\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x16\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x18\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x19\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x1A\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x1D\x00\x00\x00\x00\x42\x00\x5C\x05\x00\x00\x00\x04' b'\x00\x00\x00\x1E\x00\x00\x00\x00\x42\x00\x57\x05\x00\x00\x00\x04' b'\x00\x00\x00\x01\x00\x00\x00\x00\x42\x00\x57\x05\x00\x00\x00\x04' b'\x00\x00\x00\x02\x00\x00\x00\x00\x42\x00\x57\x05\x00\x00\x00\x04' b'\x00\x00\x00\x03\x00\x00\x00\x00\x42\x00\x57\x05\x00\x00\x00\x04' b'\x00\x00\x00\x04\x00\x00\x00\x00\x42\x00\x57\x05\x00\x00\x00\x04' b'\x00\x00\x00\x06\x00\x00\x00\x00\x42\x00\x57\x05\x00\x00\x00\x04' b'\x00\x00\x00\x07\x00\x00\x00\x00\x42\x00\x9D\x07\x00\x00\x00\x2E' b'\x49\x42\x4D\x20\x74\x65\x73\x74\x20\x73\x65\x72\x76\x65\x72\x2C' b'\x20\x6E\x6F\x74\x2D\x54\x4B\x4C\x4D\x20\x32\x2E\x30\x2E\x31\x2E' b'\x31\x20\x4B\x4D\x49\x50\x20\x32\x2E\x30\x2E\x30\x2E\x31\x00\x00' b'\x42\x00\x88\x01\x00\x00\x00\x00')) self.encoding_c = utils.BytearrayStream(( b'\x42\x00\x7C\x01\x00\x00\x00\x40\x42\x00\xA4\x01\x00\x00\x00\x18' b'\x42\x00\xA5\x07\x00\x00\x00\x0D\x41\x43\x4D\x45\x20\x4C\x4F\x43' b'\x41\x54\x49\x4F\x4E\x00\x00\x00\x42\x00\xA4\x01\x00\x00\x00\x18' b'\x42\x00\xA5\x07\x00\x00\x00\x0D\x41\x43\x4D\x45\x20\x5A\x49\x50' b'\x20\x43\x4F\x44\x45\x00\x00\x00'))