| 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 . import DEFAULT_KEY |
| 16 |
from yubihsm.defs import ALGORITHM, CAPABILITY, AUDIT, ERROR, COMMAND |
| 17 |
from yubihsm.objects import HmacKey |
| 18 |
from yubihsm.exceptions import YubiHsmDeviceError, YubiHsmInvalidResponseError |
| 19 |
from cryptography.hazmat.backends import default_backend |
| 20 |
from cryptography.hazmat.primitives import hashes |
| 21 |
from dataclasses import replace |
| 22 |
import os |
| 23 |
import time |
| 24 |
import struct |
| 25 |
import pytest |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(autouse=True, scope="module") |
| 29 |
def turn_on_logging(session, info): |
| 30 |
# Enable command audit logging for all commands on YubiHSM versions >= 2.3.1 |
| 31 |
# This ensures test independence across different YubiHSM versions |
| 32 |
if info.version >= (2, 3, 1): |
| 33 |
ignored_cmds = [ |
| 34 |
COMMAND.ERROR, |
| 35 |
COMMAND.DEVICE_INFO, |
| 36 |
COMMAND.GET_LOG_ENTRIES, |
| 37 |
COMMAND.ECHO, |
| 38 |
COMMAND.SESSION_MESSAGE, |
| 39 |
] |
| 40 |
|
| 41 |
if info.version < (2, 4, 0): |
| 42 |
ignored_cmds.extend( |
| 43 |
[ |
| 44 |
COMMAND.PUT_PUBLIC_WRAP_KEY, |
| 45 |
COMMAND.WRAP_KEY_RSA, |
| 46 |
COMMAND.UNWRAP_KEY_RSA, |
| 47 |
COMMAND.EXPORT_WRAPPED_RSA, |
| 48 |
COMMAND.IMPORT_WRAPPED_RSA, |
| 49 |
] |
| 50 |
) |
| 51 |
|
| 52 |
session.set_command_audit( |
| 53 |
{cmd: AUDIT.ON for cmd in COMMAND if cmd not in ignored_cmds} |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_get_log_entries(session): |
| 58 |
boot, auth, logs = session.get_log_entries() |
| 59 |
|
| 60 |
last_digest = logs[0].digest |
| 61 |
for i in range(1, len(logs)): |
| 62 |
digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) |
| 63 |
digest.update(logs[i].data) |
| 64 |
digest.update(last_digest) |
| 65 |
last_digest = digest.finalize()[:16] |
| 66 |
assert last_digest == logs[i].digest |
| 67 |
|
| 68 |
|
| 69 |
def test_full_log(session): |
| 70 |
hmackey = HmacKey.generate( |
| 71 |
session, |
| 72 |
0, |
| 73 |
"Test Full Log", |
| 74 |
1, |
| 75 |
CAPABILITY.SIGN_HMAC | CAPABILITY.VERIFY_HMAC, |
| 76 |
ALGORITHM.HMAC_SHA256, |
| 77 |
) |
| 78 |
|
| 79 |
for i in range(0, 30): |
| 80 |
data = os.urandom(64) |
| 81 |
resp = hmackey.sign_hmac(data) |
| 82 |
assert len(resp) == 32 |
| 83 |
assert hmackey.verify_hmac(resp, data) |
| 84 |
|
| 85 |
boot, auth, logs = session.get_log_entries() |
| 86 |
|
| 87 |
last_digest = logs[0].digest |
| 88 |
for i in range(1, len(logs)): |
| 89 |
digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) |
| 90 |
digest.update(logs[i].data) |
| 91 |
digest.update(last_digest) |
| 92 |
last_digest = digest.finalize()[:16] |
| 93 |
assert last_digest == logs[i].digest |
| 94 |
|
| 95 |
hmackey.delete() |
| 96 |
|
| 97 |
|
| 98 |
def test_wrong_chain(session): |
| 99 |
hmackey = HmacKey.generate( |
| 100 |
session, |
| 101 |
0, |
| 102 |
"Test Log hash chain", |
| 103 |
1, |
| 104 |
CAPABILITY.SIGN_HMAC | CAPABILITY.VERIFY_HMAC, |
| 105 |
ALGORITHM.HMAC_SHA256, |
| 106 |
) |
| 107 |
|
| 108 |
boot, auth, logs = session.get_log_entries() |
| 109 |
last_line = logs.pop() |
| 110 |
session.set_log_index(last_line.number) |
| 111 |
|
| 112 |
hmackey.sign_hmac(b"hello") |
| 113 |
hmackey.sign_hmac(b"hello") |
| 114 |
hmackey.sign_hmac(b"hello") |
| 115 |
|
| 116 |
with pytest.raises(ValueError): |
| 117 |
session.get_log_entries(logs.pop()) # Wrong number |
| 118 |
|
| 119 |
wrong_line = replace(last_line, digest=os.urandom(16)) |
| 120 |
with pytest.raises(YubiHsmInvalidResponseError): |
| 121 |
session.get_log_entries(wrong_line) |
| 122 |
|
| 123 |
hmackey.delete() |
| 124 |
|
| 125 |
|
| 126 |
def test_forced_log(hsm, session): |
| 127 |
boot, auth, logs = session.get_log_entries() |
| 128 |
last_line = logs.pop() |
| 129 |
session.set_log_index(last_line.number) |
| 130 |
session.set_force_audit(AUDIT.ON) |
| 131 |
assert session.get_force_audit() == AUDIT.ON |
| 132 |
|
| 133 |
hmackey = HmacKey.generate( |
| 134 |
session, |
| 135 |
0, |
| 136 |
"Test Force Log", |
| 137 |
1, |
| 138 |
CAPABILITY.SIGN_HMAC | CAPABILITY.VERIFY_HMAC, |
| 139 |
ALGORITHM.HMAC_SHA256, |
| 140 |
) |
| 141 |
|
| 142 |
error = 0 |
| 143 |
for i in range(0, 32): |
| 144 |
try: |
| 145 |
data = os.urandom(64) |
| 146 |
resp = hmackey.sign_hmac(data) |
| 147 |
assert len(resp) == 32 |
| 148 |
assert hmackey.verify_hmac(resp, data) |
| 149 |
except YubiHsmDeviceError as e: |
| 150 |
error = e.code |
| 151 |
assert error == ERROR.LOG_FULL |
| 152 |
device_info = hsm.get_device_info() |
| 153 |
assert device_info.log_used == device_info.log_size |
| 154 |
|
| 155 |
boot, auth, logs = session.get_log_entries(last_line) |
| 156 |
last_line = logs.pop() |
| 157 |
session.set_log_index(last_line.number) |
| 158 |
session.set_force_audit(AUDIT.OFF) |
| 159 |
assert session.get_force_audit() == AUDIT.OFF |
| 160 |
|
| 161 |
for i in range(0, 32): |
| 162 |
data = os.urandom(64) |
| 163 |
resp = hmackey.sign_hmac(data) |
| 164 |
assert len(resp) == 32 |
| 165 |
assert hmackey.verify_hmac(resp, data) |
| 166 |
|
| 167 |
hmackey.delete() |
| 168 |
|
| 169 |
|
| 170 |
def test_logs_after_reset(hsm, connect_hsm, session, info): |
| 171 |
session.reset_device() |
| 172 |
hsm.close() |
| 173 |
|
| 174 |
time.sleep(5) # Wait for device to reboot |
| 175 |
|
| 176 |
with connect_hsm() as hsm: |
| 177 |
with hsm.create_session_derived(1, DEFAULT_KEY) as session: |
| 178 |
boot, auth, logs = session.get_log_entries() |
| 179 |
|
| 180 |
# Check the version of the YubiHSM and verify the number of log entries. |
| 181 |
# YubiHSM versions >= 2.3.1 have command audit logging disabled per default, |
| 182 |
# so they will only have two initial logs: the boot and reset line. |
| 183 |
# Versions below 2.3.1 are expected to have four log entries: the boot line, |
| 184 |
# reset line, create_session and authenticate_session cmd. |
| 185 |
if info.version < (2, 3, 1): |
| 186 |
assert 4 == len(logs) |
| 187 |
else: |
| 188 |
assert 2 == len(logs) |
| 189 |
|
| 190 |
# Reset line |
| 191 |
assert logs.pop(0).data == b"\0\1" + b"\xff" * 14 |
| 192 |
|
| 193 |
# Boot line |
| 194 |
assert logs.pop(0).data == struct.pack("!HBHHHHBL", 2, 0, 0, 0xFFFF, 0, 0, 0, 0) |
| 195 |
|
| 196 |
if info.version < (2, 3, 1): |
| 197 |
assert logs.pop(0).command == COMMAND.CREATE_SESSION |
| 198 |
assert logs.pop(0).command == COMMAND.AUTHENTICATE_SESSION |