2
0
mirror of https://github.com/openkmip/pykmip synced 2025-12-26 21:23:44 +00:00

Updating Pie API key objects

This change makes some minor updates to the Pie key object hierarchy. It
fixes the key subclasses to inherit from Key directly. It adds in
support for the key format type attribute, which is required for
low-level key encoding. It also improves repr and str functionality by
using binascii.hexlify to represent the key values. The corresponding
test suites are updated accordingly to reflect these changes.
This commit is contained in:
Peter Hamilton
2015-07-21 16:03:53 -04:00
parent 57710db3dc
commit bc703f0757
4 changed files with 459 additions and 197 deletions

View File

@@ -13,16 +13,14 @@
# License for the specific language governing permissions and limitations
# under the License.
from testtools import TestCase
import binascii
import testtools
from kmip.core.enums import CryptographicAlgorithm
from kmip.core.enums import CryptographicUsageMask
from kmip.core.enums import ObjectType
from kmip.pie.objects import PublicKey
from kmip.core import enums
from kmip.pie import objects
class TestPublicKey(TestCase):
class TestPublicKey(testtools.TestCase):
"""
Test suite for PublicKey.
"""
@@ -69,12 +67,14 @@ class TestPublicKey(TestCase):
"""
Test that a PublicKey object can be instantiated.
"""
key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
key = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
self.assertEqual(key.cryptographic_algorithm,
CryptographicAlgorithm.RSA)
self.assertEqual(
key.cryptographic_algorithm, enums.CryptographicAlgorithm.RSA)
self.assertEqual(key.cryptographic_length, 1024)
self.assertEqual(key.value, self.bytes_1024)
self.assertEqual(key.key_format_type, enums.KeyFormatType.X_509)
self.assertEqual(key.cryptographic_usage_masks, list())
self.assertEqual(key.names, ['Public Key'])
@@ -82,29 +82,33 @@ class TestPublicKey(TestCase):
"""
Test that a PublicKey object can be instantiated with all arguments.
"""
key = PublicKey(
CryptographicAlgorithm.RSA,
key = objects.PublicKey(
enums.CryptographicAlgorithm.RSA,
1024,
self.bytes_1024,
masks=[CryptographicUsageMask.ENCRYPT,
CryptographicUsageMask.DECRYPT],
enums.KeyFormatType.X_509,
masks=[enums.CryptographicUsageMask.ENCRYPT,
enums.CryptographicUsageMask.DECRYPT],
name='Test Public Key')
self.assertEqual(key.cryptographic_algorithm,
CryptographicAlgorithm.RSA)
enums.CryptographicAlgorithm.RSA)
self.assertEqual(key.cryptographic_length, 1024)
self.assertEqual(key.value, self.bytes_1024)
self.assertEqual(key.key_format_type, enums.KeyFormatType.X_509)
self.assertEqual(key.cryptographic_usage_masks,
[CryptographicUsageMask.ENCRYPT,
CryptographicUsageMask.DECRYPT])
[enums.CryptographicUsageMask.ENCRYPT,
enums.CryptographicUsageMask.DECRYPT])
self.assertEqual(key.names, ['Test Public Key'])
def test_get_object_type(self):
"""
Test that the object type can be retrieved from the PublicKey.
"""
expected = ObjectType.PUBLIC_KEY
key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
expected = enums.ObjectType.PUBLIC_KEY
key = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
observed = key.object_type
self.assertEqual(expected, observed)
@@ -113,59 +117,85 @@ class TestPublicKey(TestCase):
Test that a TypeError is raised when an invalid algorithm value is
used to construct a PublicKey.
"""
args = ('invalid', 1024, self.bytes_1024)
self.assertRaises(TypeError, PublicKey, *args)
args = ('invalid', 1024, self.bytes_1024, enums.KeyFormatType.X_509)
self.assertRaises(TypeError, objects.PublicKey, *args)
def test_validate_on_invalid_length(self):
"""
Test that a TypeError is raised when an invalid length value is used
to construct a PublicKey.
"""
args = (CryptographicAlgorithm.RSA, 'invalid', self.bytes_1024)
self.assertRaises(TypeError, PublicKey, *args)
args = (enums.CryptographicAlgorithm.RSA, 'invalid', self.bytes_1024,
enums.KeyFormatType.X_509)
self.assertRaises(TypeError, objects.PublicKey, *args)
def test_validate_on_invalid_value(self):
"""
Test that a TypeError is raised when an invalid value is used to
construct a PublicKey.
"""
args = (CryptographicAlgorithm.RSA, 1024, 0)
self.assertRaises(TypeError, PublicKey, *args)
args = (enums.CryptographicAlgorithm.RSA, 1024, 0,
enums.KeyFormatType.X_509)
self.assertRaises(TypeError, objects.PublicKey, *args)
def test_validate_on_invalid_format_type(self):
"""
Test that a TypeError is raised when an invalid format type is used to
construct a PublicKey.
"""
args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
'invalid')
self.assertRaises(TypeError, objects.PublicKey, *args)
def test_validate_on_invalid_format_type_value(self):
"""
Test that a ValueError is raised when an invalid format type is used to
construct a PublicKey.
"""
args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.OPAQUE)
self.assertRaises(ValueError, objects.PublicKey, *args)
def test_validate_on_invalid_masks(self):
"""
Test that a TypeError is raised when an invalid masks value is used to
construct a PublicKey.
"""
args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
kwargs = {'masks': 'invalid'}
self.assertRaises(TypeError, PublicKey, *args, **kwargs)
self.assertRaises(TypeError, objects.PublicKey, *args, **kwargs)
def test_validate_on_invalid_mask(self):
"""
Test that a TypeError is raised when an invalid mask value is used to
construct a PublicKey.
"""
args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
kwargs = {'masks': ['invalid']}
self.assertRaises(TypeError, PublicKey, *args, **kwargs)
self.assertRaises(TypeError, objects.PublicKey, *args, **kwargs)
def test_validate_on_invalid_name(self):
"""
Test that a TypeError is raised when an invalid name value is used to
construct a PublicKey.
"""
args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
kwargs = {'name': 0}
self.assertRaises(TypeError, PublicKey, *args, **kwargs)
self.assertRaises(TypeError, objects.PublicKey, *args, **kwargs)
def test_repr(self):
"""
Test that repr can be applied to a PublicKey.
"""
key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
args = "algorithm={0}, length={1}, value={2}".format(
CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
key = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
args = "algorithm={0}, length={1}, value={2}, format_type={3}".format(
enums.CryptographicAlgorithm.RSA, 1024,
binascii.hexlify(self.bytes_1024), enums.KeyFormatType.X_509)
expected = "PublicKey({0})".format(args)
observed = repr(key)
self.assertEqual(expected, observed)
@@ -174,8 +204,10 @@ class TestPublicKey(TestCase):
"""
Test that str can be applied to a PublicKey.
"""
key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
expected = str(self.bytes_1024)
key = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
expected = str(binascii.hexlify(self.bytes_1024))
observed = str(key)
self.assertEqual(expected, observed)
@@ -184,8 +216,12 @@ class TestPublicKey(TestCase):
Test that the equality operator returns True when comparing two
PublicKey objects with the same data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
b = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
self.assertTrue(a == b)
self.assertTrue(b == a)
@@ -194,8 +230,12 @@ class TestPublicKey(TestCase):
Test that the equality operator returns False when comparing two
PublicKey objects with different data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
b = PublicKey(CryptographicAlgorithm.AES, 1024, self.bytes_1024)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
b = objects.PublicKey(
enums.CryptographicAlgorithm.AES, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
self.assertFalse(a == b)
self.assertFalse(b == a)
@@ -204,8 +244,12 @@ class TestPublicKey(TestCase):
Test that the equality operator returns False when comparing two
PublicKey objects with different data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
b = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_1024)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_1024,
enums.KeyFormatType.X_509)
self.assertFalse(a == b)
self.assertFalse(b == a)
@@ -214,8 +258,26 @@ class TestPublicKey(TestCase):
Test that the equality operator returns False when comparing two
PublicKey objects with different data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
b = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_2048)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_2048,
enums.KeyFormatType.X_509)
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_equal_on_not_equal_format_type(self):
"""
Test that the equality operator returns False when comparing two
PublicKey objects with different data.
"""
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.PKCS_1)
self.assertFalse(a == b)
self.assertFalse(b == a)
@@ -224,7 +286,9 @@ class TestPublicKey(TestCase):
Test that the equality operator returns False when comparing a
PublicKey object to a non-PublicKey object.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.X_509)
b = "invalid"
self.assertFalse(a == b)
self.assertFalse(b == a)
@@ -234,8 +298,12 @@ class TestPublicKey(TestCase):
Test that the inequality operator returns False when comparing
two PublicKey objects with the same internal data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048)
b = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
self.assertFalse(a != b)
self.assertFalse(b != a)
@@ -244,8 +312,12 @@ class TestPublicKey(TestCase):
Test that the equality operator returns True when comparing two
PublicKey objects with different data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048)
b = PublicKey(CryptographicAlgorithm.AES, 2048, self.bytes_2048)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
b = objects.PublicKey(
enums.CryptographicAlgorithm.AES, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
self.assertTrue(a != b)
self.assertTrue(b != a)
@@ -254,8 +326,12 @@ class TestPublicKey(TestCase):
Test that the equality operator returns True when comparing two
PublicKey objects with different data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048)
b = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024,
enums.KeyFormatType.PKCS_1)
self.assertTrue(a != b)
self.assertTrue(b != a)
@@ -264,8 +340,26 @@ class TestPublicKey(TestCase):
Test that the equality operator returns True when comparing two
PublicKey objects with different data.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048)
b = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_1024)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_1024,
enums.KeyFormatType.PKCS_1)
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_not_equal_on_not_equal_format_type(self):
"""
Test that the equality operator returns True when comparing two
PublicKey objects with different data.
"""
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
b = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.X_509)
self.assertTrue(a != b)
self.assertTrue(b != a)
@@ -274,7 +368,9 @@ class TestPublicKey(TestCase):
Test that the equality operator returns True when comparing a
PublicKey object to a non-PublicKey object.
"""
a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048)
a = objects.PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1)
b = "invalid"
self.assertTrue(a != b)
self.assertTrue(b != a)