Debian Codegraph @

tests/test_objects.py

raw
1 # Copyright 2016-2018 Yubico AB
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from yubihsm.objects import (
16 ObjectInfo,
17 YhsmObject,
18 Opaque,
19 AuthenticationKey,
20 AsymmetricKey,
21 WrapKey,
22 PublicWrapKey,
23 HmacKey,
24 Template,
25 OtpAeadKey,
26 )
27 from yubihsm.core import AuthSession
28 from yubihsm.defs import ORIGIN, ALGORITHM, OBJECT, CAPABILITY
29 from binascii import a2b_hex
30 from unittest.mock import MagicMock
31 from random import randint
32
33 import unittest
34
35
36 _DATA = a2b_hex(
37 "ffffffffffffffff00010028ffff0226000244454641554c5420415554484b4559204348414e47452054484953204153415000c0ffeec0ffee01ffffffffffffffff" # noqa E501
38 )
39
40
41 class TestObjectInfo(unittest.TestCase):
42 def test_objectinfo_parsing(self):
43 info = ObjectInfo.parse(_DATA)
44 self.assertEqual(info.capabilities, 0xFFFFFFFFFFFFFFFF)
45 self.assertEqual(info.id, 1)
46 self.assertEqual(info.size, 40)
47 self.assertEqual(info.domains, 0xFFFF)
48 self.assertEqual(info.object_type, OBJECT.AUTHENTICATION_KEY)
49 self.assertEqual(info.algorithm, ALGORITHM.AES128_YUBICO_AUTHENTICATION)
50 self.assertEqual(info.sequence, 0)
51 self.assertEqual(info.origin, ORIGIN.IMPORTED)
52 self.assertEqual(info.label, "DEFAULT AUTHKEY CHANGE THIS ASAP")
53 self.assertEqual(info.delegated_capabilities, 0xFFFFFFFFFFFFFFFF)
54
55 def test_non_utf8_label(self):
56 label = b"\xfe\xed\xfa\xce" * 10
57 data = bytearray(_DATA)
58 data[18:58] = label
59 info = ObjectInfo.parse(bytes(data))
60 self.assertEqual(info.label, label)
61 self.assertIsInstance(info.label, bytes)
62
63
64 class TestYhsmObject(unittest.TestCase):
65 def test_get_info(self):
66 AuthMock = MagicMock(AuthSession)
67 AuthMock.send_secure_cmd.return_value = b"\x00\x00\x7f\xff\xff\xff\xff\xff\x00\x05\x01\x00\x00)\x05\x16\x00\x01hmaclabel\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" # noqa E501
68
69 # Create instance from mocked data and set the object type
70 obj = YhsmObject(session=AuthMock, object_id=5)
71 obj.object_type = OBJECT.HMAC_KEY
72
73 # The expected ObjectInfo is below
74 info = ObjectInfo(
75 capabilities=CAPABILITY(140737488355327),
76 id=5,
77 size=256,
78 domains=41,
79 object_type=OBJECT.HMAC_KEY,
80 algorithm=ALGORITHM.HMAC_SHA512,
81 sequence=0,
82 origin=ORIGIN.GENERATED,
83 label="hmaclabel",
84 delegated_capabilities=0,
85 )
86
87 self.assertEqual(info, obj.get_info())
88
89 def test_delete(self):
90 AuthMock = MagicMock(AuthSession)
91 AuthMock.send_secure_cmd.return_value = b""
92 obj = YhsmObject(session=AuthMock, object_id=5)
93 obj.object_type = OBJECT.HMAC_KEY
94 obj.delete()
95
96 def test__create(self):
97 # create for every type
98 items = [
99 (OBJECT.OPAQUE, Opaque),
100 (OBJECT.AUTHENTICATION_KEY, AuthenticationKey),
101 (OBJECT.ASYMMETRIC_KEY, AsymmetricKey),
102 (OBJECT.WRAP_KEY, WrapKey),
103 (OBJECT.PUBLIC_WRAP_KEY, PublicWrapKey),
104 (OBJECT.HMAC_KEY, HmacKey),
105 (OBJECT.TEMPLATE, Template),
106 (OBJECT.OTP_AEAD_KEY, OtpAeadKey),
107 ]
108
109 AuthMock = MagicMock(AuthSession)
110
111 for obj_type, obj_class in items:
112 id_num = randint(1, 17)
113 obj = YhsmObject._create(obj_type, AuthMock, id_num)
114 self.assertIsInstance(obj, obj_class)
115 self.assertEqual(obj.id, id_num)
116 expected_repr = "{class_name}(id={id_num})".format(
117 class_name=obj_class.__name__, id_num=id_num
118 )
119 self.assertEqual(obj.__repr__(), expected_repr)