File: test_auth.py

package info (click to toggle)
python-yubihsm 3.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: python: 4,882; makefile: 4
file content (253 lines) | stat: -rw-r--r-- 8,232 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 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 COMMAND, CAPABILITY, ERROR
from yubihsm.objects import AuthenticationKey
from yubihsm.exceptions import YubiHsmAuthenticationError, YubiHsmDeviceError
from yubihsm.utils import password_to_key
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec
from binascii import a2b_hex
import pytest
import os


class TestAuthenticationKey:
    def test_put_unicode_authkey(self, hsm, session):
        # UTF-8 encoded unicode password
        password = b"\xf0\x9f\x98\x81\xf0\x9f\x98\x83\xf0\x9f\x98\x84".decode()

        authkey = AuthenticationKey.put_derived(
            session,
            0,
            "Test PUT authkey",
            1,
            CAPABILITY.NONE,
            CAPABILITY.NONE,
            password,
        )

        with hsm.create_session_derived(authkey.id, password) as session:
            message = os.urandom(256)
            resp = session.send_secure_cmd(COMMAND.ECHO, message)

        assert resp == message

        authkey.delete()


class TestChangeAuthenticationKey:
    @pytest.fixture(autouse=True)
    def prerequisites(self, info):
        if info.version < (2, 1, 0):
            pytest.skip("Change authentication key requires 2.1.0")

    def test_change_password(self, hsm, session):
        # Create an auth key with the capability to change
        authkey = AuthenticationKey.put_derived(
            session,
            0,
            "Test CHANGE authkey",
            1,
            CAPABILITY.CHANGE_AUTHENTICATION_KEY,
            CAPABILITY.NONE,
            "first_password",
        )

        # Can't change the password of another key
        with pytest.raises(YubiHsmDeviceError) as context:
            authkey.change_password("second_password")
        assert context.value.code == ERROR.INVALID_ID

        # Try again, using the new auth key
        with hsm.create_session_derived(authkey.id, "first_password") as session:
            authkey.with_session(session).change_password("second_password")

        with pytest.raises(YubiHsmAuthenticationError):
            hsm.create_session_derived(authkey.id, "first_password")

        hsm.create_session_derived(authkey.id, "second_password").close()

        authkey.delete()
        with pytest.raises(YubiHsmDeviceError) as context:
            hsm.create_session_derived(authkey.id, "second_password")
        assert context.value.code == ERROR.OBJECT_NOT_FOUND

    def test_change_raw_keys(self, session, hsm):
        key_enc = a2b_hex("090b47dbed595654901dee1cc655e420")
        key_mac = a2b_hex("592fd483f759e29909a04c4505d2ce0a")

        # Create an auth key with the capability to change
        authkey = AuthenticationKey.put(
            session,
            0,
            "Test CHANGE authkey",
            1,
            CAPABILITY.CHANGE_AUTHENTICATION_KEY,
            CAPABILITY.NONE,
            key_enc,
            key_mac,
        )

        with hsm.create_session_derived(authkey.id, "password") as session:
            key_enc, key_mac = password_to_key("second_password")
            authkey.with_session(session).change_key(key_enc, key_mac)

        with hsm.create_session_derived(authkey.id, "second_password"):
            pass

        authkey.delete()


class TestSessions:
    def test_parallel_sessions(self, session, hsm):
        authkey1 = AuthenticationKey.put_derived(
            session,
            0,
            "Test authkey 1",
            1,
            CAPABILITY.NONE,
            CAPABILITY.NONE,
            "one",
        )

        authkey2 = AuthenticationKey.put_derived(
            session,
            0,
            "Test authkey 2",
            2,
            CAPABILITY.NONE,
            CAPABILITY.NONE,
            "two",
        )

        authkey3 = AuthenticationKey.put_derived(
            session,
            0,
            "Test authkey 3",
            1,
            CAPABILITY.NONE,
            CAPABILITY.NONE,
            "three",
        )

        session1 = hsm.create_session_derived(authkey1.id, "one")
        session2 = hsm.create_session_derived(authkey2.id, "two")
        session3 = hsm.create_session_derived(authkey3.id, "three")

        session2.close()
        session1.send_secure_cmd(COMMAND.ECHO, b"hello")
        session3.send_secure_cmd(COMMAND.ECHO, b"hi")

        session1.send_secure_cmd(COMMAND.ECHO, b"hello")
        session3.send_secure_cmd(COMMAND.ECHO, b"greetings")
        session1.close()

        session3.send_secure_cmd(COMMAND.ECHO, b"good bye")
        session3.close()

        authkey1.delete()
        authkey2.delete()
        authkey3.delete()


class TestAymmetricAuthenticationKey:
    @pytest.fixture(autouse=True)
    def prerequisites(self, info):
        if info.version < (2, 3, 0):
            pytest.skip("Asymmetric authentication requires 2.3.0")

    def test_put_public_key(self, hsm, session):
        private_key = ec.generate_private_key(ec.SECP256R1(), backend=default_backend())

        authkey = AuthenticationKey.put_public_key(
            session,
            0,
            "Test PUT asym authkey",
            1,
            CAPABILITY.NONE,
            CAPABILITY.NONE,
            private_key.public_key(),
        )

        try:
            with hsm.create_session_asymmetric(
                authkey.id, private_key
            ) as asymmetric_session:
                message = os.urandom(256)
                resp = asymmetric_session.send_secure_cmd(COMMAND.ECHO, message)
                assert message == resp
        finally:
            authkey.delete()

    def test_change_public_key(self, hsm, session):
        first_private_key = ec.generate_private_key(
            ec.SECP256R1(), backend=default_backend()
        )
        second_private_key = ec.generate_private_key(
            ec.SECP256R1(), backend=default_backend()
        )

        authkey = AuthenticationKey.put_public_key(
            session,
            0,
            "Test PUT asym authkey",
            1,
            CAPABILITY.CHANGE_AUTHENTICATION_KEY,
            CAPABILITY.NONE,
            first_private_key.public_key(),
        )

        with hsm.create_session_asymmetric(
            authkey.id, first_private_key
        ) as asymmetric_session:
            authkey.with_session(asymmetric_session).change_public_key(
                second_private_key.public_key()
            )

        try:
            with hsm.create_session_asymmetric(
                authkey.id, second_private_key
            ) as asymmetric_session:
                message = os.urandom(256)
                resp = asymmetric_session.send_secure_cmd(COMMAND.ECHO, message)
                assert message == resp
        finally:
            authkey.delete()

    def test_cached_device_public_key(self, hsm, session):
        private_key = ec.generate_private_key(ec.SECP256R1(), backend=default_backend())

        authkey = AuthenticationKey.put_public_key(
            session,
            0,
            "Test PUT asym authkey",
            1,
            CAPABILITY.NONE,
            CAPABILITY.NONE,
            private_key.public_key(),
        )

        right_public_key = hsm.get_device_public_key()
        wrong_public_key = ec.generate_private_key(
            ec.SECP256R1(), backend=default_backend()
        ).public_key()

        with pytest.raises(YubiHsmAuthenticationError):
            hsm.create_session_asymmetric(authkey.id, private_key, wrong_public_key)

        try:
            hsm.create_session_asymmetric(authkey.id, private_key, right_public_key)
        finally:
            authkey.delete()