File: test_prf.py

package info (click to toggle)
python-fido2 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,456 kB
  • sloc: python: 11,423; javascript: 181; sh: 21; makefile: 9
file content (226 lines) | stat: -rw-r--r-- 6,974 bytes parent folder | download
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
import os

import pytest

from fido2.client import DefaultClientDataCollector, Fido2Client
from fido2.ctap2.extensions import HmacSecretExtension
from fido2.server import Fido2Server
from fido2.utils import websafe_encode

from . import TEST_PIN, CliInteraction


@pytest.fixture(autouse=True, scope="module")
def preconditions(dev_manager):
    if "hmac-secret" not in dev_manager.info.extensions:
        pytest.skip("hmac-secret not supported by authenticator")


def test_prf(client, pin_protocol):
    rp = {"id": "example.com", "name": "Example RP"}
    server = Fido2Server(rp)
    user = {"id": b"user_id", "name": "A. User"}
    uv = "required"

    create_options, state = server.register_begin(user, user_verification=uv)

    # Create a credential
    result = client.make_credential(
        {
            **create_options["publicKey"],
            "extensions": {"prf": {}},
        }
    )
    assert result.client_extension_results.prf.enabled is True
    assert result.client_extension_results["prf"]["enabled"] is True

    auth_data = server.register_complete(state, result)
    credentials = [auth_data.credential_data]

    # Complete registration
    auth_data = server.register_complete(state, result)
    credential = auth_data.credential_data

    # Generate a salt for PRF:
    salt = websafe_encode(os.urandom(32))

    # Prepare parameters for getAssertion
    credentials = [credential]
    request_options, state = server.authenticate_begin(
        credentials, user_verification=uv
    )

    # Authenticate the credential
    result = client.get_assertion(
        {
            **request_options["publicKey"],
            "extensions": {"prf": {"eval": {"first": salt}}},
        }
    )

    # Only one cred in allowCredentials, only one response.
    response = result.get_response(0)

    output1 = response.client_extension_results.prf.results.first
    assert response.client_extension_results["prf"]["results"][
        "first"
    ] == websafe_encode(output1)

    # Authenticate again, using two salts to generate two secrets.

    # This time we will use evalByCredential, which can be used if there are multiple
    # credentials which use different salts. Here it is not needed, but provided for
    # completeness of the example.

    # Generate a second salt for PRF:
    salt2 = websafe_encode(os.urandom(32))
    # The first salt is reused, which should result in the same secret.

    result = client.get_assertion(
        {
            **request_options["publicKey"],
            "extensions": {
                "prf": {
                    "evalByCredential": {
                        websafe_encode(credential.credential_id): {
                            "first": salt,
                            "second": salt2,
                        }
                    }
                }
            },
        }
    )

    response = result.get_response(0)

    output = response.client_extension_results.prf.results
    assert output.first == output1
    assert output.second != output1
    assert response.client_extension_results["prf"]["results"][
        "second"
    ] == websafe_encode(output.second)


def test_hmac_secret(device, pin_protocol, printer):
    rp = {"id": "example.com", "name": "Example RP"}
    server = Fido2Server(rp)
    user = {"id": b"user_id", "name": "A. User"}
    uv = "required"

    create_options, state = server.register_begin(user, user_verification=uv)

    client = Fido2Client(
        device,
        client_data_collector=DefaultClientDataCollector("https://example.com"),
        user_interaction=CliInteraction(printer, TEST_PIN),
        extensions=[HmacSecretExtension(allow_hmac_secret=True)],
    )

    # Create a credential
    result = client.make_credential(
        {
            **create_options["publicKey"],
            "extensions": {"hmacCreateSecret": True},
        }
    )
    assert result.client_extension_results.hmac_create_secret is True
    assert result.client_extension_results["hmacCreateSecret"] is True

    # Complete registration
    auth_data = server.register_complete(state, result)
    credentials = [auth_data.credential_data]

    # Generate a salt for HmacSecret:
    salt = os.urandom(32)

    # Prepare parameters for getAssertion
    request_options, state = server.authenticate_begin(
        credentials, user_verification=uv
    )

    result = client.get_assertion(
        {
            **request_options["publicKey"],
            "extensions": {"hmacGetSecret": {"salt1": salt}},
        }
    )
    result = result.get_response(0)

    output1 = result.client_extension_results.hmac_get_secret.output1
    assert result.client_extension_results["hmacGetSecret"][
        "output1"
    ] == websafe_encode(output1)

    salt2 = os.urandom(32)

    result = client.get_assertion(
        {
            **request_options["publicKey"],
            "extensions": {"hmacGetSecret": {"salt1": salt, "salt2": salt2}},
        }
    )
    result = result.get_response(0)

    output = result.client_extension_results.hmac_get_secret
    assert output.output1 == output1
    assert output.output2 != output1


def test_prf_mc(client, pin_protocol, info):
    if "hmac-secret-mc" not in info.extensions:
        pytest.skip("hmac-secret-mc not supported by authenticator")

    rp = {"id": "example.com", "name": "Example RP"}
    server = Fido2Server(rp)
    user = {"id": b"user_id", "name": "A. User"}
    uv = "required"

    create_options, state = server.register_begin(user, user_verification=uv)

    # Generate salts for PRF:
    salt1 = websafe_encode(os.urandom(32))
    salt2 = websafe_encode(os.urandom(32))

    # Create a credential, with salts
    result = client.make_credential(
        {
            **create_options["publicKey"],
            "extensions": {"prf": {"eval": {"first": salt1, "second": salt2}}},
        }
    )
    auth_data = server.register_complete(state, result)
    credential = auth_data.credential_data

    assert result.client_extension_results.prf.enabled is True
    assert result.client_extension_results["prf"]["enabled"] is True

    output = result.client_extension_results.prf.results
    assert output.first
    assert output.second

    # Prepare parameters for getAssertion
    credentials = [credential]
    request_options, state = server.authenticate_begin(
        credentials, user_verification=uv
    )

    # Authenticate the credential
    result = client.get_assertion(
        {
            **request_options["publicKey"],
            "extensions": {
                "prf": {
                    "evalByCredential": {
                        websafe_encode(credential.credential_id): {
                            "first": salt1,
                            "second": salt2,
                        }
                    }
                }
            },
        }
    )

    response = result.get_response(0)
    assert output == response.client_extension_results.prf.results