mirror of
https://github.com/openkmip/pykmip
synced 2026-01-03 09:03:16 +00:00
Secret Data (2.2.7) can now be registered Opaque Object (2.2.8) can now be registered This required the adding of a NONE type to the OpaqueDataType enum with the value of 0. This is not indicated by the standard but we needed some value to satisfy response decoding. Also fixed get demo This has been tested vs a HP Atalla ESKM HSM
153 lines
5.8 KiB
Python
153 lines
5.8 KiB
Python
# Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory
|
|
# All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
from kmip.core.factories.keys import KeyFactory
|
|
|
|
from kmip.core.attributes import CryptographicAlgorithm
|
|
from kmip.core.attributes import CryptographicLength
|
|
|
|
from kmip.core.enums import ObjectType
|
|
from kmip.core.errors import ErrorStrings
|
|
from kmip.core.misc import KeyFormatType
|
|
|
|
from kmip.core.objects import Attribute
|
|
from kmip.core.objects import KeyBlock
|
|
from kmip.core.objects import KeyMaterial
|
|
from kmip.core.objects import KeyWrappingData
|
|
from kmip.core.objects import KeyValue
|
|
|
|
from kmip.core.secrets import OpaqueObject
|
|
from kmip.core.secrets import PrivateKey
|
|
from kmip.core.secrets import PublicKey
|
|
from kmip.core.secrets import SecretData
|
|
from kmip.core.secrets import SymmetricKey
|
|
from kmip.core.secrets import Template
|
|
|
|
from kmip.core import utils
|
|
|
|
|
|
class SecretFactory(object):
|
|
|
|
def __init__(self):
|
|
self.key_factory = KeyFactory()
|
|
|
|
self.base_error = ErrorStrings.BAD_EXP_RECV
|
|
self.template_input = self.base_error.format('Template', '{0}', '{1}',
|
|
'{2}')
|
|
|
|
def create(self, secret_type, value=None):
|
|
if secret_type is ObjectType.CERTIFICATE:
|
|
return self._create_certificate(value)
|
|
elif secret_type is ObjectType.SYMMETRIC_KEY:
|
|
return self._create_symmetric_key(value)
|
|
elif secret_type is ObjectType.PUBLIC_KEY:
|
|
return self._create_public_key()
|
|
elif secret_type is ObjectType.PRIVATE_KEY:
|
|
return self._create_private_key()
|
|
elif secret_type is ObjectType.SPLIT_KEY:
|
|
return self._create_split_key(value)
|
|
elif secret_type is ObjectType.TEMPLATE:
|
|
return self._create_template(value)
|
|
elif secret_type is ObjectType.SECRET_DATA:
|
|
return self._create_secret_data(value)
|
|
elif secret_type is ObjectType.OPAQUE_DATA:
|
|
return self._create_opaque_data(value)
|
|
|
|
def _create_certificate(self, value):
|
|
raise NotImplementedError()
|
|
|
|
def _create_symmetric_key(self, value):
|
|
if value is None:
|
|
return SymmetricKey()
|
|
else:
|
|
key_block = self._build_key_block(value)
|
|
return SymmetricKey(key_block)
|
|
|
|
def _create_public_key(self):
|
|
return PublicKey()
|
|
|
|
def _create_private_key(self):
|
|
return PrivateKey()
|
|
|
|
def _create_split_key(self, value):
|
|
raise NotImplementedError()
|
|
|
|
def _create_template(self, value):
|
|
if value is None:
|
|
return Template()
|
|
else:
|
|
if not isinstance(value, list):
|
|
msg = utils.build_er_error(Template,
|
|
'constructor argument type', list,
|
|
type(value))
|
|
raise TypeError(msg)
|
|
else:
|
|
for val in value:
|
|
if not isinstance(val, Attribute):
|
|
msg = utils.build_er_error(Template,
|
|
'constructor argument type',
|
|
Attribute, type(val))
|
|
raise TypeError(msg)
|
|
return Template(value)
|
|
|
|
def _create_secret_data(self, value):
|
|
if value:
|
|
kind = SecretData.SecretDataType(value.get("secret_data_type"))
|
|
key_block = self._build_key_block(value)
|
|
return SecretData(kind, key_block)
|
|
return SecretData()
|
|
|
|
def _create_opaque_data(self, value):
|
|
if value:
|
|
kind = OpaqueObject.OpaqueDataType(value.get("opaque_data_type"))
|
|
data = OpaqueObject.OpaqueDataValue(value.get("opaque_data_value"))
|
|
return OpaqueObject(kind, data)
|
|
return OpaqueObject()
|
|
|
|
def _build_key_block(self, value):
|
|
key_type = value.get('key_format_type')
|
|
key_compression_type = value.get('key_compression_type')
|
|
key_value = value.get('key_value')
|
|
cryptographic_algorithm = value.get('cryptographic_algorithm')
|
|
cryptographic_length = value.get('cryptographic_length')
|
|
key_wrapping_data = value.get('key_wrapping_data')
|
|
|
|
key_format_type = KeyFormatType(key_type)
|
|
|
|
key_comp_type = None
|
|
if key_compression_type is not None:
|
|
key_comp_type = KeyBlock.KeyCompressionType(
|
|
key_compression_type)
|
|
|
|
key_material = KeyMaterial(key_value)
|
|
key_value = KeyValue(key_material)
|
|
crypto_algorithm = CryptographicAlgorithm(cryptographic_algorithm)
|
|
crypto_length = CryptographicLength(cryptographic_length)
|
|
|
|
key_wrap_data = None
|
|
if key_wrapping_data is not None:
|
|
# TODO (peter-hamilton) This currently isn't used in the tests
|
|
# TODO (peter-hamilton) but needs to be updated to properly
|
|
# TODO (peter-hamilton) create a KeyWrappingData object.
|
|
key_wrap_data = KeyWrappingData(key_wrapping_data)
|
|
|
|
key_block = KeyBlock(key_format_type,
|
|
key_comp_type,
|
|
key_value,
|
|
crypto_algorithm,
|
|
crypto_length,
|
|
key_wrap_data)
|
|
return key_block
|