1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# Copyright 2016-2018 Yubico AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from yubihsm.defs import ALGORITHM, CAPABILITY, ERROR
from yubihsm.objects import (
AuthenticationKey,
HmacKey,
Opaque,
AsymmetricKey,
OtpAeadKey,
WrapKey,
SymmetricKey,
)
from yubihsm.exceptions import YubiHsmDeviceError
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
import os
import pytest
def _set_up_key(hsm, session, capability):
password = session.get_pseudo_random(32).hex()
key = AuthenticationKey.put_derived(
session, 0, "Test Delete authkey", 1, capability, CAPABILITY.NONE, password
)
session = hsm.create_session_derived(key.id, password)
return key, session
def _test_delete(hsm, session, obj, capability):
pos_key, pos_sess = _set_up_key(hsm, session, capability)
neg_key, neg_sess = _set_up_key(hsm, session, CAPABILITY.NONE)
with pytest.raises(YubiHsmDeviceError) as context:
obj.with_session(neg_sess).delete()
assert context.value.code == ERROR.INSUFFICIENT_PERMISSIONS
obj.with_session(pos_sess).delete()
pos_sess.close()
neg_sess.close()
neg_key.delete()
pos_key.delete()
def test_opaque(hsm, session):
obj = Opaque.put(
session,
0,
"Test opaque data",
1,
CAPABILITY.NONE,
ALGORITHM.OPAQUE_DATA,
b"data",
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_OPAQUE)
def test_authentication_key(hsm, session):
obj = AuthenticationKey.put_derived(
session,
0,
"Test delete authkey",
1,
CAPABILITY.GET_LOG_ENTRIES,
CAPABILITY.NONE,
session.get_pseudo_random(32).hex(),
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_AUTHENTICATION_KEY)
def test_asymmetric_key(hsm, session):
obj = AsymmetricKey.put(
session,
0,
"Test delete asym",
0xFFFF,
CAPABILITY.SIGN_ECDSA,
ec.generate_private_key(ec.SECP384R1(), backend=default_backend()),
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_ASYMMETRIC_KEY)
def test_wrap_key(hsm, session):
obj = WrapKey.put(
session,
0,
"Test delete",
1,
CAPABILITY.IMPORT_WRAPPED,
ALGORITHM.AES192_CCM_WRAP,
CAPABILITY.NONE,
os.urandom(24),
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_WRAP_KEY)
def test_hmac_key(hsm, session):
obj = HmacKey.put(
session, 0, "Test delete HMAC", 1, CAPABILITY.SIGN_HMAC, bytes(16)
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_HMAC_KEY)
def test_otp_aead_key(hsm, session):
obj = OtpAeadKey.put(
session,
0,
"Test delete OTP AEAD",
1,
CAPABILITY.DECRYPT_OTP,
ALGORITHM.AES256_YUBICO_OTP,
0x00000001,
os.urandom(32),
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_OTP_AEAD_KEY)
def test_symmetric_key(hsm, session, info):
if info.version < (2, 3, 0):
pytest.skip("Symmetric keys require YubiHSM 2.3.0")
obj = SymmetricKey.put(
session,
0,
"Test delete symmetric",
0xFFFF,
CAPABILITY.DECRYPT_ECB,
ALGORITHM.AES128,
os.urandom(16),
)
_test_delete(hsm, session, obj, CAPABILITY.DELETE_SYMMETRIC_KEY)
|