1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# 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 Certificate
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):
"""
Create a secret object of the specified type with the given value.
Args:
secret_type (ObjectType): An ObjectType enumeration specifying the
type of secret to create.
value (dict): A dictionary containing secret data. Optional,
defaults to None.
Returns:
secret: The newly constructed secret object.
Raises:
TypeError: If the provided secret type is unrecognized.
Example:
>>> factory.create(ObjectType.SYMMETRIC_KEY)
SymmetricKey(...)
"""
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(value)
elif secret_type is ObjectType.PRIVATE_KEY:
return self._create_private_key(value)
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)
else:
raise TypeError("Unrecognized secret type: {0}".format(
secret_type))
def _create_certificate(self, value):
if value:
return Certificate(
certificate_type=value.get('certificate_type'),
certificate_value=value.get('certificate_value')
)
else:
return Certificate()
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, value):
if value is None:
return PublicKey()
else:
key_block = self._build_key_block(value)
return PublicKey(key_block)
def _create_private_key(self, value):
if value is None:
return PrivateKey()
else:
key_block = self._build_key_block(value)
return PrivateKey(key_block)
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 = None
if cryptographic_algorithm is not None:
crypto_algorithm = CryptographicAlgorithm(
cryptographic_algorithm
)
crypto_length = None
if cryptographic_length is not None:
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
|