-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathsign_arkg.py
More file actions
148 lines (126 loc) · 4.87 KB
/
sign_arkg.py
File metadata and controls
148 lines (126 loc) · 4.87 KB
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
# Copyright (c) 2024 Yubico AB
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""
Connects to the first FIDO device found which supports the PRF extension,
creates a new credential for it with the extension enabled, and uses it to
derive two separate secrets.
"""
import sys
from exampleutils import get_client
from fido2 import cbor
from fido2.cose import ESP256_SPLIT_ARKG_PLACEHOLDER, CoseKey
from fido2.ctap2.extensions import PreviewSignExtension
from fido2.server import Fido2Server
from fido2.utils import sha256, websafe_decode, websafe_encode
uv = "discouraged"
# Locate a suitable FIDO authenticator
client, info = get_client(
lambda info: PreviewSignExtension.NAME in info.extensions,
extensions=[PreviewSignExtension()],
)
server = Fido2Server({"id": "example.com", "name": "Example RP"}, attestation="none")
user = {"id": b"user_id", "name": "A. User"}
# Prepare parameters for makeCredential
create_options, state = server.register_begin(
user,
resident_key_requirement="discouraged",
user_verification=uv,
authenticator_attachment="cross-platform",
)
# Create a credential
result = client.make_credential(
{
**create_options["publicKey"],
"extensions": {
PreviewSignExtension.NAME: {
"generateKey": {"algorithms": [ESP256_SPLIT_ARKG_PLACEHOLDER]}
}
},
}
)
# Complete registration
auth_data = server.register_complete(state, result)
credentials = [auth_data.credential_data]
print("New credential created, with the sign extension.")
# PRF result:
sign_result = result.client_extension_results.previewSign
print("CREATE sign result", sign_result)
sign_key = sign_result.generated_key
if not sign_key:
print(
"Failed to create credential with sign extension",
result.client_extension_results,
)
sys.exit(1)
# Extension output contains master public key
pk = CoseKey.parse(
cbor.decode(websafe_decode(sign_key["publicKey"]))
) # COSE key in bytes
print("public key", pk)
# Master public key contains blinding and KEM keys
# ARKG derive_public_key uses these
print("Blinding public key", pk.pk_bl)
print("KEM public key", pk.pk_kem)
# Arbitrary bytestring used for ctx, ikm
ctx = b"my-ctx-here"
ikm = b"my-ikm-here"
# Derived public key to verify with, and kh to send to Authenticator
pk2, args = pk.derive_public_key(ikm, ctx)
print("Derived public key", pk2)
print("COSE_Sign_Args for derived key", args)
# Prepare a message to sign
message = b"New message"
ph_data = sha256(message)
# Prepare parameters for getAssertion
request_options, state = server.authenticate_begin(credentials, user_verification=uv)
# Authenticate the credential
result = client.get_assertion(
{
**request_options["publicKey"],
# Add extension outputs. We have only 1 credential in allowCredentials
"extensions": {
PreviewSignExtension.NAME: {
"signByCredential": {
websafe_encode(credentials[0].credential_id): {
"keyHandle": sign_key.key_handle,
"tbs": ph_data,
"additionalArgs": cbor.encode(args),
},
},
}
},
}
)
# Only one cred in allowCredentials, only one response.
result = result.get_response(0)
sign_result = result.client_extension_results[PreviewSignExtension.NAME]
print("GET sign result", sign_result)
# Response contains a signature over message
signature = sign_result.get("signature")
print("Test verify signature", signature)
pk2.verify(message, websafe_decode(signature))
print("Signature verified with derived public key!")