Debian Codegraph @

tests/device/test_delete.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, ERROR
16 from yubihsm.objects import (
17 AuthenticationKey,
18 HmacKey,
19 Opaque,
20 AsymmetricKey,
21 OtpAeadKey,
22 WrapKey,
23 SymmetricKey,
24 )
25 from yubihsm.exceptions import YubiHsmDeviceError
26 from cryptography.hazmat.primitives.asymmetric import ec
27 from cryptography.hazmat.backends import default_backend
28 import os
29 import pytest
30
31
32 def _set_up_key(hsm, session, capability):
33 password = session.get_pseudo_random(32).hex()
34 key = AuthenticationKey.put_derived(
35 session, 0, "Test Delete authkey", 1, capability, CAPABILITY.NONE, password
36 )
37 session = hsm.create_session_derived(key.id, password)
38 return key, session
39
40
41 def _test_delete(hsm, session, obj, capability):
42 pos_key, pos_sess = _set_up_key(hsm, session, capability)
43 neg_key, neg_sess = _set_up_key(hsm, session, CAPABILITY.NONE)
44
45 with pytest.raises(YubiHsmDeviceError) as context:
46 obj.with_session(neg_sess).delete()
47 assert context.value.code == ERROR.INSUFFICIENT_PERMISSIONS
48
49 obj.with_session(pos_sess).delete()
50
51 pos_sess.close()
52 neg_sess.close()
53 neg_key.delete()
54 pos_key.delete()
55
56
57 def test_opaque(hsm, session):
58 obj = Opaque.put(
59 session,
60 0,
61 "Test opaque data",
62 1,
63 CAPABILITY.NONE,
64 ALGORITHM.OPAQUE_DATA,
65 b"data",
66 )
67 _test_delete(hsm, session, obj, CAPABILITY.DELETE_OPAQUE)
68
69
70 def test_authentication_key(hsm, session):
71 obj = AuthenticationKey.put_derived(
72 session,
73 0,
74 "Test delete authkey",
75 1,
76 CAPABILITY.GET_LOG_ENTRIES,
77 CAPABILITY.NONE,
78 session.get_pseudo_random(32).hex(),
79 )
80 _test_delete(hsm, session, obj, CAPABILITY.DELETE_AUTHENTICATION_KEY)
81
82
83 def test_asymmetric_key(hsm, session):
84 obj = AsymmetricKey.put(
85 session,
86 0,
87 "Test delete asym",
88 0xFFFF,
89 CAPABILITY.SIGN_ECDSA,
90 ec.generate_private_key(ec.SECP384R1(), backend=default_backend()),
91 )
92 _test_delete(hsm, session, obj, CAPABILITY.DELETE_ASYMMETRIC_KEY)
93
94
95 def test_wrap_key(hsm, session):
96 obj = WrapKey.put(
97 session,
98 0,
99 "Test delete",
100 1,
101 CAPABILITY.IMPORT_WRAPPED,
102 ALGORITHM.AES192_CCM_WRAP,
103 CAPABILITY.NONE,
104 os.urandom(24),
105 )
106 _test_delete(hsm, session, obj, CAPABILITY.DELETE_WRAP_KEY)
107
108
109 def test_hmac_key(hsm, session):
110 obj = HmacKey.put(
111 session, 0, "Test delete HMAC", 1, CAPABILITY.SIGN_HMAC, bytes(16)
112 )
113 _test_delete(hsm, session, obj, CAPABILITY.DELETE_HMAC_KEY)
114
115
116 def test_otp_aead_key(hsm, session):
117 obj = OtpAeadKey.put(
118 session,
119 0,
120 "Test delete OTP AEAD",
121 1,
122 CAPABILITY.DECRYPT_OTP,
123 ALGORITHM.AES256_YUBICO_OTP,
124 0x00000001,
125 os.urandom(32),
126 )
127 _test_delete(hsm, session, obj, CAPABILITY.DELETE_OTP_AEAD_KEY)
128
129
130 def test_symmetric_key(hsm, session, info):
131 if info.version < (2, 3, 0):
132 pytest.skip("Symmetric keys require YubiHSM 2.3.0")
133
134 obj = SymmetricKey.put(
135 session,
136 0,
137 "Test delete symmetric",
138 0xFFFF,
139 CAPABILITY.DECRYPT_ECB,
140 ALGORITHM.AES128,
141 os.urandom(16),
142 )
143 _test_delete(hsm, session, obj, CAPABILITY.DELETE_SYMMETRIC_KEY)