mirror of
https://github.com/openkmip/pykmip
synced 2025-12-16 08:13:56 +00:00
Adding SymmetricKey to the Pie object hierarchy
This change adds SymmetricKey to the Pie object hierarchy along with a test suite and updates to the other classes and test suites composing the Pie interface.
This commit is contained in:
@@ -16,10 +16,14 @@
|
||||
from abc import ABCMeta
|
||||
from abc import abstractmethod
|
||||
|
||||
from six import add_metaclass
|
||||
import six
|
||||
|
||||
from kmip.core.enums import CryptographicAlgorithm
|
||||
from kmip.core.enums import CryptographicUsageMask
|
||||
from kmip.core.enums import ObjectType
|
||||
|
||||
|
||||
@add_metaclass(ABCMeta)
|
||||
@six.add_metaclass(ABCMeta)
|
||||
class ManagedObject:
|
||||
"""
|
||||
The abstract base class of the simplified KMIP object hierarchy.
|
||||
@@ -83,6 +87,13 @@ class ManagedObject:
|
||||
"""
|
||||
raise AttributeError("object type cannot be set")
|
||||
|
||||
@abstractmethod
|
||||
def validate(self):
|
||||
"""
|
||||
Verify that the contents of the ManagedObject are valid.
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def __repr__(self):
|
||||
pass
|
||||
@@ -123,7 +134,7 @@ class CryptographicObject(ManagedObject):
|
||||
|
||||
super(CryptographicObject, self).__init__()
|
||||
|
||||
self.crpytographic_usage_masks = list()
|
||||
self.cryptographic_usage_masks = list()
|
||||
|
||||
# All remaining attributes are not considered part of the public API
|
||||
# and are subject to change.
|
||||
@@ -176,3 +187,129 @@ class Key(CryptographicObject):
|
||||
# The following attributes are placeholders for attributes that are
|
||||
# unsupported by kmip.core
|
||||
self._usage_limits = None
|
||||
|
||||
|
||||
class SymmetricKey(Key):
|
||||
"""
|
||||
The SymmetricKey class of the simplified KMIP object hierarchy.
|
||||
|
||||
A SymmetricKey is a core KMIP object that is the subject of key
|
||||
management operations. For more information, see Section 2.2 of the KMIP
|
||||
1.1 specification.
|
||||
|
||||
Attributes:
|
||||
cryptographic_algorithm: The type of algorithm for the SymmetricKey.
|
||||
cryptographic_length: The length in bits of the SymmetricKey value.
|
||||
value: The bytes of the SymmetricKey.
|
||||
cryptographic_usage_masks: The list of usage mask flags for
|
||||
SymmetricKey application.
|
||||
names: The string names of the SymmetricKey.
|
||||
"""
|
||||
|
||||
def __init__(self, algorithm, length, value, masks=None,
|
||||
name='Symmetric Key'):
|
||||
"""
|
||||
Create a SymmetricKey.
|
||||
|
||||
Args:
|
||||
algorithm(CryptographicAlgorithm): An enumeration identifying the
|
||||
type of algorithm for the key.
|
||||
length(int): The length in bits of the key.
|
||||
value(bytes): The bytes representing the key.
|
||||
masks(list): A list of CryptographicUsageMask enumerations defining
|
||||
how the key will be used.
|
||||
name(string): The string name of the key.
|
||||
"""
|
||||
super(SymmetricKey, self).__init__()
|
||||
|
||||
self._object_type = ObjectType.SYMMETRIC_KEY
|
||||
|
||||
self.value = value
|
||||
self.cryptographic_algorithm = algorithm
|
||||
self.cryptographic_length = length
|
||||
self.names = [name]
|
||||
|
||||
if masks:
|
||||
self.cryptographic_usage_masks = masks
|
||||
else:
|
||||
self.cryptographic_usage_masks = list()
|
||||
|
||||
# All remaining attributes are not considered part of the public API
|
||||
# and are subject to change.
|
||||
|
||||
# The following attributes are placeholders for attributes that are
|
||||
# unsupported by kmip.core
|
||||
self._process_start_date = None
|
||||
self._protect_stop_date = None
|
||||
|
||||
self.validate()
|
||||
|
||||
def validate(self):
|
||||
"""
|
||||
Verify that the contents of the SymmetricKey object are valid.
|
||||
|
||||
Raises:
|
||||
TypeError: if the types of any SymmetricKey attributes are invalid
|
||||
ValueError: if the key length and key value length do not match
|
||||
"""
|
||||
if not isinstance(self.value, bytes):
|
||||
raise TypeError("key value must be bytes")
|
||||
elif not isinstance(self.cryptographic_algorithm,
|
||||
CryptographicAlgorithm):
|
||||
raise TypeError("key algorithm must be a CryptographicAlgorithm "
|
||||
"enumeration")
|
||||
elif not isinstance(self.cryptographic_length, six.integer_types):
|
||||
raise TypeError("key length must be an integer")
|
||||
elif not isinstance(self.cryptographic_usage_masks, list):
|
||||
raise TypeError("key usage masks must be a list")
|
||||
|
||||
mask_count = len(self.cryptographic_usage_masks)
|
||||
for i in range(mask_count):
|
||||
mask = self.cryptographic_usage_masks[i]
|
||||
if not isinstance(mask, CryptographicUsageMask):
|
||||
position = "({0} in list)".format(i)
|
||||
raise TypeError(
|
||||
"key mask {0} must be a CryptographicUsageMask "
|
||||
"enumeration".format(position))
|
||||
|
||||
name_count = len(self.names)
|
||||
for i in range(name_count):
|
||||
name = self.names[i]
|
||||
if not isinstance(name, six.string_types):
|
||||
position = "({0} in list)".format(i)
|
||||
raise TypeError("key name {0} must be a string".format(
|
||||
position))
|
||||
|
||||
if (len(self.value) * 8) != self.cryptographic_length:
|
||||
msg = "key length ({0}) not equal to key value length ({1})"
|
||||
msg = msg.format(self.cryptographic_length, len(self.value) * 8)
|
||||
raise ValueError(msg)
|
||||
|
||||
def __repr__(self):
|
||||
algorithm = "algorithm={0}".format(self.cryptographic_algorithm)
|
||||
length = "length={0}".format(self.cryptographic_length)
|
||||
value = "value={0}".format(self.value)
|
||||
|
||||
return "SymmetricKey({0}, {1}, {2})".format(algorithm, length, value)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.value)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, SymmetricKey):
|
||||
if self.value != other.value:
|
||||
return False
|
||||
elif self.cryptographic_algorithm != other.cryptographic_algorithm:
|
||||
return False
|
||||
elif self.cryptographic_length != other.cryptographic_length:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, SymmetricKey):
|
||||
return not (self == other)
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
Reference in New Issue
Block a user