Debian Codegraph @

tests/device/test_basic.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.defs import ALGORITHM, CAPABILITY, OBJECT, COMMAND, ORIGIN, FIPS_STATUS
16 from yubihsm.objects import (
17 YhsmObject,
18 AsymmetricKey,
19 HmacKey,
20 WrapKey,
21 AuthenticationKey,
22 )
23 from cryptography.hazmat.primitives.asymmetric import ec
24 from yubihsm.exceptions import YubiHsmInvalidRequestError, YubiHsmDeviceError
25 from time import sleep
26 import uuid
27 import os
28 import pytest
29
30
31 class TestListObjects:
32 def print_list_objects(self, session):
33 objlist = session.list_objects()
34
35 for i in range(len(objlist)):
36 print(
37 "id: ",
38 "0x%0.4X" % objlist[i].id,
39 ",type: ",
40 objlist[i].object_type.name,
41 "\t,sequence: ",
42 objlist[i].sequence,
43 )
44
45 objinfo = objlist[1].get_info()
46 print(
47 "id: ",
48 "0x%0.4X" % objinfo.id,
49 ",type: ",
50 objinfo.object_type.name,
51 "\t,sequence: ",
52 objinfo.sequence,
53 ",domains: 0x%0.4X" % objinfo.domains,
54 ",capabilities: 0x%0.8X" % objinfo.capabilities,
55 ",algorithm: ",
56 objinfo.algorithm,
57 )
58
59 def key_in_list(self, session, keytype, algorithm=None):
60 dom = None
61 cap = CAPABILITY.NONE
62 key_label = "%s%s" % (str(uuid.uuid4()), b"\xf0\x9f\x98\x83".decode())
63
64 key: YhsmObject
65 if keytype == OBJECT.ASYMMETRIC_KEY:
66 dom = 0xFFFF
67 key = AsymmetricKey.generate(session, 0, key_label, dom, cap, algorithm)
68 elif keytype == OBJECT.WRAP_KEY:
69 dom = 0x01
70 key = WrapKey.generate(session, 0, key_label, dom, cap, algorithm, cap)
71 elif keytype == OBJECT.HMAC_KEY:
72 dom = 0x01
73 key = HmacKey.generate(session, 0, key_label, dom, cap, algorithm)
74 elif keytype == OBJECT.AUTHENTICATION_KEY:
75 dom = 0x01
76 key = AuthenticationKey.put_derived(
77 session,
78 0,
79 key_label,
80 dom,
81 cap,
82 cap,
83 "password",
84 )
85
86 objlist = session.list_objects(object_id=key.id, object_type=key.object_type)
87 assert objlist[0].id == key.id
88 assert objlist[0].object_type == key.object_type
89
90 objinfo = objlist[0].get_info()
91 assert objinfo.id == key.id
92 assert objinfo.object_type == key.object_type
93 assert objinfo.domains == dom
94 assert objinfo.capabilities == cap
95 if algorithm:
96 assert objinfo.algorithm == algorithm
97
98 if key.object_type == OBJECT.AUTHENTICATION_KEY:
99 assert objinfo.origin == ORIGIN.IMPORTED
100 else:
101 assert objinfo.origin == ORIGIN.GENERATED
102
103 assert objinfo.label == key_label
104
105 key.delete()
106
107 def test_keys_in_list(self, session):
108 self.key_in_list(session, OBJECT.ASYMMETRIC_KEY, ALGORITHM.EC_P256)
109 self.key_in_list(session, OBJECT.WRAP_KEY, ALGORITHM.AES128_CCM_WRAP)
110 self.key_in_list(session, OBJECT.HMAC_KEY, ALGORITHM.HMAC_SHA1)
111 self.key_in_list(session, OBJECT.AUTHENTICATION_KEY)
112
113 def test_list_all_params(self, session):
114 # TODO: this test should check for presence of some things..
115 session.list_objects(
116 object_id=1,
117 object_type=OBJECT.HMAC_KEY,
118 domains=1,
119 capabilities=CAPABILITY.ALL,
120 algorithm=ALGORITHM.HMAC_SHA1,
121 label="foo",
122 )
123
124
125 class TestVarious:
126 def test_device_info(self, hsm):
127 device_info = hsm.get_device_info()
128 assert len(device_info.version) == 3
129 assert device_info.serial > 0
130 assert device_info.log_used > 0
131 assert device_info.log_size >= device_info.log_used
132 assert len(device_info.supported_algorithms) >= 47
133 if device_info.version > (2, 4, 0):
134 assert isinstance(device_info.part_number, str)
135
136 def test_get_pseudo_random(self, session):
137 data = session.get_pseudo_random(10)
138 assert len(data) == 10
139 data2 = session.get_pseudo_random(10)
140 assert len(data2) == 10
141 assert data != data2
142
143 def test_send_too_big(self, hsm, session):
144 max_msg_size = hsm._msg_buf_size - 1
145 buf = os.urandom(max_msg_size - 3 + 1) # Message 1 byte too large
146 with pytest.raises(YubiHsmInvalidRequestError):
147 hsm.send_cmd(COMMAND.ECHO, buf)
148
149
150 class TestDevicePublicKey:
151 @pytest.fixture(autouse=True)
152 def prerequisites(self, info):
153 if info.version < (2, 3, 0):
154 pytest.skip("Device public keys requires 2.3.0")
155
156 def test_get_device_public_key(self, hsm):
157 public_key = hsm.get_device_public_key()
158 assert isinstance(public_key, ec.EllipticCurvePublicKey)
159
160
161 class TestEcho:
162 def plain_echo(self, hsm, echo_len):
163 echo_buf = os.urandom(echo_len)
164
165 resp = hsm.send_cmd(COMMAND.ECHO, echo_buf)
166
167 assert len(resp) == echo_len
168 assert resp == echo_buf
169
170 def secure_echo(self, session, echo_len):
171 echo_buf = os.urandom(echo_len)
172
173 resp = session.send_secure_cmd(COMMAND.ECHO, echo_buf)
174 assert resp == echo_buf
175
176 def test_plain_echo(self, hsm):
177 self.plain_echo(hsm, 1024)
178
179 def test_secure_echo(self, session):
180 self.secure_echo(session, 1024)
181
182 def test_plain_echo_many(self, hsm):
183 for i in range(1, 256):
184 self.plain_echo(hsm, i)
185
186 def test_echo_max_size(self, hsm, session):
187 self.plain_echo(hsm, 2021)
188 self.secure_echo(session, 2021)
189
190
191 class TestFipsOptions:
192 @pytest.fixture(scope="class", autouse=True)
193 @classmethod
194 def session2(cls, session, connect_hsm):
195 try:
196 session.get_fips_status()
197 session.reset_device()
198 sleep(5.0)
199 hsm = connect_hsm()
200 new_session = hsm.create_session_derived(1, "password")
201 yield new_session
202 new_session.reset_device()
203 sleep(5.0)
204 except YubiHsmDeviceError:
205 pytest.skip("Non-FIPS YubiHSM")
206
207 def test_set_in_fips_mode(self, session2, info):
208 assert session2.get_fips_status() == FIPS_STATUS.OFF
209 session2.set_fips_mode(True)
210 if info.version < (2, 4, 0):
211 assert session2.get_fips_status() == FIPS_STATUS.ON
212 else:
213 assert session2.get_fips_status() == FIPS_STATUS.PENDING
214
215 def test_fips_mode_disables_algorithms(self, session2, info):
216 session2.set_fips_mode(True)
217 enabled = session2.get_enabled_algorithms()
218 if info.version < (2, 4, 0):
219 assert not any(
220 enabled[alg]
221 for alg in (
222 ALGORITHM.RSA_PKCS1_SHA1,
223 ALGORITHM.RSA_PSS_SHA1,
224 ALGORITHM.EC_ECDSA_SHA1,
225 ALGORITHM.EC_ED25519,
226 )
227 )
228 else:
229 assert not any(
230 enabled[alg]
231 for alg in (
232 ALGORITHM.RSA_PKCS1_SHA1,
233 ALGORITHM.RSA_PSS_SHA1,
234 ALGORITHM.EC_K256,
235 ALGORITHM.EC_ECDSA_SHA1,
236 ALGORITHM.RSA_PKCS1_DECRYPT,
237 )
238 )
239
240 def test_enabling_algorithms_in_fips_mode(self, session2, info):
241 session2.set_fips_mode(True)
242 if info.version < (2, 4, 0):
243 # For YubiHSM FW < 2.4.0, enabling dissallowed algorithms
244 # disables FIPS mode.
245 session2.set_enabled_algorithms(
246 {
247 ALGORITHM.RSA_PKCS1_SHA1: True,
248 }
249 )
250 assert session2.get_fips_status() == FIPS_STATUS.OFF
251 else:
252 with pytest.raises(YubiHsmDeviceError):
253 session2.set_enabled_algorithms({ALGORITHM.RSA_PKCS1_SHA1: True})