| 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 COMMAND, CAPABILITY, ERROR |
| 16 |
from yubihsm.objects import AuthenticationKey |
| 17 |
from yubihsm.exceptions import YubiHsmAuthenticationError, YubiHsmDeviceError |
| 18 |
from yubihsm.utils import password_to_key |
| 19 |
from cryptography.hazmat.backends import default_backend |
| 20 |
from cryptography.hazmat.primitives.asymmetric import ec |
| 21 |
from binascii import a2b_hex |
| 22 |
import pytest |
| 23 |
import os |
| 24 |
|
| 25 |
|
| 26 |
class TestAuthenticationKey: |
| 27 |
def test_put_unicode_authkey(self, hsm, session): |
| 28 |
# UTF-8 encoded unicode password |
| 29 |
password = b"\xf0\x9f\x98\x81\xf0\x9f\x98\x83\xf0\x9f\x98\x84".decode() |
| 30 |
|
| 31 |
authkey = AuthenticationKey.put_derived( |
| 32 |
session, |
| 33 |
0, |
| 34 |
"Test PUT authkey", |
| 35 |
1, |
| 36 |
CAPABILITY.NONE, |
| 37 |
CAPABILITY.NONE, |
| 38 |
password, |
| 39 |
) |
| 40 |
|
| 41 |
with hsm.create_session_derived(authkey.id, password) as session: |
| 42 |
message = os.urandom(256) |
| 43 |
resp = session.send_secure_cmd(COMMAND.ECHO, message) |
| 44 |
|
| 45 |
assert resp == message |
| 46 |
|
| 47 |
authkey.delete() |
| 48 |
|
| 49 |
|
| 50 |
class TestChangeAuthenticationKey: |
| 51 |
@pytest.fixture(autouse=True) |
| 52 |
def prerequisites(self, info): |
| 53 |
if info.version < (2, 1, 0): |
| 54 |
pytest.skip("Change authentication key requires 2.1.0") |
| 55 |
|
| 56 |
def test_change_password(self, hsm, session): |
| 57 |
# Create an auth key with the capability to change |
| 58 |
authkey = AuthenticationKey.put_derived( |
| 59 |
session, |
| 60 |
0, |
| 61 |
"Test CHANGE authkey", |
| 62 |
1, |
| 63 |
CAPABILITY.CHANGE_AUTHENTICATION_KEY, |
| 64 |
CAPABILITY.NONE, |
| 65 |
"first_password", |
| 66 |
) |
| 67 |
|
| 68 |
# Can't change the password of another key |
| 69 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 70 |
authkey.change_password("second_password") |
| 71 |
assert context.value.code == ERROR.INVALID_ID |
| 72 |
|
| 73 |
# Try again, using the new auth key |
| 74 |
with hsm.create_session_derived(authkey.id, "first_password") as session: |
| 75 |
authkey.with_session(session).change_password("second_password") |
| 76 |
|
| 77 |
with pytest.raises(YubiHsmAuthenticationError): |
| 78 |
hsm.create_session_derived(authkey.id, "first_password") |
| 79 |
|
| 80 |
hsm.create_session_derived(authkey.id, "second_password").close() |
| 81 |
|
| 82 |
authkey.delete() |
| 83 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 84 |
hsm.create_session_derived(authkey.id, "second_password") |
| 85 |
assert context.value.code == ERROR.OBJECT_NOT_FOUND |
| 86 |
|
| 87 |
def test_change_raw_keys(self, session, hsm): |
| 88 |
key_enc = a2b_hex("090b47dbed595654901dee1cc655e420") |
| 89 |
key_mac = a2b_hex("592fd483f759e29909a04c4505d2ce0a") |
| 90 |
|
| 91 |
# Create an auth key with the capability to change |
| 92 |
authkey = AuthenticationKey.put( |
| 93 |
session, |
| 94 |
0, |
| 95 |
"Test CHANGE authkey", |
| 96 |
1, |
| 97 |
CAPABILITY.CHANGE_AUTHENTICATION_KEY, |
| 98 |
CAPABILITY.NONE, |
| 99 |
key_enc, |
| 100 |
key_mac, |
| 101 |
) |
| 102 |
|
| 103 |
with hsm.create_session_derived(authkey.id, "password") as session: |
| 104 |
key_enc, key_mac = password_to_key("second_password") |
| 105 |
authkey.with_session(session).change_key(key_enc, key_mac) |
| 106 |
|
| 107 |
with hsm.create_session_derived(authkey.id, "second_password"): |
| 108 |
pass |
| 109 |
|
| 110 |
authkey.delete() |
| 111 |
|
| 112 |
|
| 113 |
class TestSessions: |
| 114 |
def test_parallel_sessions(self, session, hsm): |
| 115 |
authkey1 = AuthenticationKey.put_derived( |
| 116 |
session, |
| 117 |
0, |
| 118 |
"Test authkey 1", |
| 119 |
1, |
| 120 |
CAPABILITY.NONE, |
| 121 |
CAPABILITY.NONE, |
| 122 |
"one", |
| 123 |
) |
| 124 |
|
| 125 |
authkey2 = AuthenticationKey.put_derived( |
| 126 |
session, |
| 127 |
0, |
| 128 |
"Test authkey 2", |
| 129 |
2, |
| 130 |
CAPABILITY.NONE, |
| 131 |
CAPABILITY.NONE, |
| 132 |
"two", |
| 133 |
) |
| 134 |
|
| 135 |
authkey3 = AuthenticationKey.put_derived( |
| 136 |
session, |
| 137 |
0, |
| 138 |
"Test authkey 3", |
| 139 |
1, |
| 140 |
CAPABILITY.NONE, |
| 141 |
CAPABILITY.NONE, |
| 142 |
"three", |
| 143 |
) |
| 144 |
|
| 145 |
session1 = hsm.create_session_derived(authkey1.id, "one") |
| 146 |
session2 = hsm.create_session_derived(authkey2.id, "two") |
| 147 |
session3 = hsm.create_session_derived(authkey3.id, "three") |
| 148 |
|
| 149 |
session2.close() |
| 150 |
session1.send_secure_cmd(COMMAND.ECHO, b"hello") |
| 151 |
session3.send_secure_cmd(COMMAND.ECHO, b"hi") |
| 152 |
|
| 153 |
session1.send_secure_cmd(COMMAND.ECHO, b"hello") |
| 154 |
session3.send_secure_cmd(COMMAND.ECHO, b"greetings") |
| 155 |
session1.close() |
| 156 |
|
| 157 |
session3.send_secure_cmd(COMMAND.ECHO, b"good bye") |
| 158 |
session3.close() |
| 159 |
|
| 160 |
authkey1.delete() |
| 161 |
authkey2.delete() |
| 162 |
authkey3.delete() |
| 163 |
|
| 164 |
|
| 165 |
class TestAymmetricAuthenticationKey: |
| 166 |
@pytest.fixture(autouse=True) |
| 167 |
def prerequisites(self, info): |
| 168 |
if info.version < (2, 3, 0): |
| 169 |
pytest.skip("Asymmetric authentication requires 2.3.0") |
| 170 |
|
| 171 |
def test_put_public_key(self, hsm, session): |
| 172 |
private_key = ec.generate_private_key(ec.SECP256R1(), backend=default_backend()) |
| 173 |
|
| 174 |
authkey = AuthenticationKey.put_public_key( |
| 175 |
session, |
| 176 |
0, |
| 177 |
"Test PUT asym authkey", |
| 178 |
1, |
| 179 |
CAPABILITY.NONE, |
| 180 |
CAPABILITY.NONE, |
| 181 |
private_key.public_key(), |
| 182 |
) |
| 183 |
|
| 184 |
try: |
| 185 |
with hsm.create_session_asymmetric( |
| 186 |
authkey.id, private_key |
| 187 |
) as asymmetric_session: |
| 188 |
message = os.urandom(256) |
| 189 |
resp = asymmetric_session.send_secure_cmd(COMMAND.ECHO, message) |
| 190 |
assert message == resp |
| 191 |
finally: |
| 192 |
authkey.delete() |
| 193 |
|
| 194 |
def test_change_public_key(self, hsm, session): |
| 195 |
first_private_key = ec.generate_private_key( |
| 196 |
ec.SECP256R1(), backend=default_backend() |
| 197 |
) |
| 198 |
second_private_key = ec.generate_private_key( |
| 199 |
ec.SECP256R1(), backend=default_backend() |
| 200 |
) |
| 201 |
|
| 202 |
authkey = AuthenticationKey.put_public_key( |
| 203 |
session, |
| 204 |
0, |
| 205 |
"Test PUT asym authkey", |
| 206 |
1, |
| 207 |
CAPABILITY.CHANGE_AUTHENTICATION_KEY, |
| 208 |
CAPABILITY.NONE, |
| 209 |
first_private_key.public_key(), |
| 210 |
) |
| 211 |
|
| 212 |
with hsm.create_session_asymmetric( |
| 213 |
authkey.id, first_private_key |
| 214 |
) as asymmetric_session: |
| 215 |
authkey.with_session(asymmetric_session).change_public_key( |
| 216 |
second_private_key.public_key() |
| 217 |
) |
| 218 |
|
| 219 |
try: |
| 220 |
with hsm.create_session_asymmetric( |
| 221 |
authkey.id, second_private_key |
| 222 |
) as asymmetric_session: |
| 223 |
message = os.urandom(256) |
| 224 |
resp = asymmetric_session.send_secure_cmd(COMMAND.ECHO, message) |
| 225 |
assert message == resp |
| 226 |
finally: |
| 227 |
authkey.delete() |
| 228 |
|
| 229 |
def test_cached_device_public_key(self, hsm, session): |
| 230 |
private_key = ec.generate_private_key(ec.SECP256R1(), backend=default_backend()) |
| 231 |
|
| 232 |
authkey = AuthenticationKey.put_public_key( |
| 233 |
session, |
| 234 |
0, |
| 235 |
"Test PUT asym authkey", |
| 236 |
1, |
| 237 |
CAPABILITY.NONE, |
| 238 |
CAPABILITY.NONE, |
| 239 |
private_key.public_key(), |
| 240 |
) |
| 241 |
|
| 242 |
right_public_key = hsm.get_device_public_key() |
| 243 |
wrong_public_key = ec.generate_private_key( |
| 244 |
ec.SECP256R1(), backend=default_backend() |
| 245 |
).public_key() |
| 246 |
|
| 247 |
with pytest.raises(YubiHsmAuthenticationError): |
| 248 |
hsm.create_session_asymmetric(authkey.id, private_key, wrong_public_key) |
| 249 |
|
| 250 |
try: |
| 251 |
hsm.create_session_asymmetric(authkey.id, private_key, right_public_key) |
| 252 |
finally: |
| 253 |
authkey.delete() |