-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsWebAuthnClient.cpp
More file actions
248 lines (218 loc) · 8.68 KB
/
Copy pathWindowsWebAuthnClient.cpp
File metadata and controls
248 lines (218 loc) · 8.68 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
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
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <webauthn.h>
#include <cstdio>
#include "WindowsWebAuthnClient.h"
namespace
{
constexpr DWORD TIMEOUT_MS = 60000;
std::wstring ToWide(const std::string& s)
{
if (s.empty())
{
return {};
}
int n = MultiByteToWideChar(CP_UTF8, 0, s.data(), static_cast<int>(s.size()), nullptr, 0);
std::wstring w(n, L'\0');
MultiByteToWideChar(CP_UTF8, 0, s.data(), static_cast<int>(s.size()), w.data(), n);
return w;
}
std::string ToUtf8(PCWSTR w)
{
if (w == nullptr)
{
return {};
}
int n = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr);
if (n <= 1)
{
return {};
}
std::string s(n - 1, '\0');
WideCharToMultiByte(CP_UTF8, 0, w, -1, s.data(), n, nullptr, nullptr);
return s;
}
std::string ErrorText(HRESULT hr)
{
char code[16];
snprintf(code, sizeof(code), "0x%08lx", static_cast<unsigned long>(hr));
std::string name = ToUtf8(WebAuthNGetErrorName(hr)) + ", " + code;
// webauthn.dll maps most user-facing failures onto a few NTE_* codes
switch (hr)
{
case NTE_USER_CANCELLED: return "Cancelled by the user or timed out. (" + name + ")";
case NTE_NOT_FOUND: return "No matching credential on this authenticator. (" + name + ")";
case NTE_EXISTS: return "Credential already registered on this authenticator. (" + name + ")";
case NTE_NOT_SUPPORTED: return "Not supported by the authenticator. (" + name + ")";
case NTE_TOKEN_KEYSET_STORAGE_FULL: return "Authenticator storage is full. (" + name + ")";
default: return name;
}
}
FidoError ApiError(const char* call, HRESULT hr)
{
std::string text = ErrorText(hr);
return { std::string(call) + ": " + text, text, {} };
}
std::string TransportName(DWORD transport)
{
std::string s;
auto add = [&s](const char* name) { s += s.empty() ? name : std::string(", ") + name; };
if (transport & WEBAUTHN_CTAP_TRANSPORT_USB)
{
add("USB");
}
if (transport & WEBAUTHN_CTAP_TRANSPORT_NFC)
{
add("NFC");
}
if (transport & WEBAUTHN_CTAP_TRANSPORT_BLE)
{
add("BLE");
}
if (transport & WEBAUTHN_CTAP_TRANSPORT_TEST)
{
add("TEST");
}
if (transport & WEBAUTHN_CTAP_TRANSPORT_INTERNAL)
{
add("INTERNAL (platform)");
}
if (transport & WEBAUTHN_CTAP_TRANSPORT_HYBRID)
{
add("HYBRID (phone)");
}
return s.empty() ? "unknown" : s;
}
std::vector<uint8_t> Bytes(const BYTE* p, DWORD len)
{
return p ? std::vector<uint8_t>(p, p + len) : std::vector<uint8_t>{};
}
// webauthn.dll centers its dialog on this window (libfido2's winhello backend does the same)
HWND OwnerWindow()
{
return GetForegroundWindow();
}
DWORD Attachment(bool roamingOnly)
{
return roamingOnly ? WEBAUTHN_AUTHENTICATOR_ATTACHMENT_CROSS_PLATFORM : WEBAUTHN_AUTHENTICATOR_ATTACHMENT_ANY;
}
DWORD UserVerification(bool requireUv)
{
return requireUv ? WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED : WEBAUTHN_USER_VERIFICATION_REQUIREMENT_DISCOURAGED;
}
}
WindowsWebAuthnClient::WindowsWebAuthnClient(bool roamingOnly)
: m_roamingOnly(roamingOnly)
{
}
uint32_t WindowsWebAuthnClient::ApiVersion()
{
return WebAuthNGetApiVersionNumber();
}
std::string WindowsWebAuthnClient::Description() const
{
return "windows://hello -> webauthn.dll directly (API v" + std::to_string(ApiVersion()) + ")" +
(m_roamingOnly ? ", security keys only" : "");
}
bool WindowsWebAuthnClient::MakeCredential(const RegistrationOptions& options, const std::string& clientDataJson,
RegistrationResponse& response, FidoError& error)
{
std::wstring wRpId = ToWide(options.rpId);
std::wstring wRpName = ToWide(options.rpName);
std::wstring wUserName = ToWide(options.userName);
std::vector<BYTE> uid(options.userId.begin(), options.userId.end());
std::vector<BYTE> cd(clientDataJson.begin(), clientDataJson.end());
WEBAUTHN_RP_ENTITY_INFORMATION rp{};
rp.dwVersion = WEBAUTHN_RP_ENTITY_INFORMATION_CURRENT_VERSION;
rp.pwszId = wRpId.c_str();
rp.pwszName = wRpName.c_str();
WEBAUTHN_USER_ENTITY_INFORMATION user{};
user.dwVersion = WEBAUTHN_USER_ENTITY_INFORMATION_CURRENT_VERSION;
user.cbId = static_cast<DWORD>(uid.size());
user.pbId = uid.data();
user.pwszName = wUserName.c_str();
user.pwszDisplayName = wUserName.c_str();
WEBAUTHN_COSE_CREDENTIAL_PARAMETER alg{};
alg.dwVersion = WEBAUTHN_COSE_CREDENTIAL_PARAMETER_CURRENT_VERSION;
alg.pwszCredentialType = WEBAUTHN_CREDENTIAL_TYPE_PUBLIC_KEY;
alg.lAlg = WEBAUTHN_COSE_ALGORITHM_ECDSA_P256_WITH_SHA256;
WEBAUTHN_COSE_CREDENTIAL_PARAMETERS algs{};
algs.cCredentialParameters = 1;
algs.pCredentialParameters = &alg;
WEBAUTHN_CLIENT_DATA clientData{};
clientData.dwVersion = WEBAUTHN_CLIENT_DATA_CURRENT_VERSION;
clientData.cbClientDataJSON = static_cast<DWORD>(cd.size());
clientData.pbClientDataJSON = cd.data();
clientData.pwszHashAlgId = WEBAUTHN_HASH_ALGORITHM_SHA_256;
// Version 1 already has everything we need and works on every webauthn.dll (Windows 10 1903+)
WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS opt{};
opt.dwVersion = WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_1;
opt.dwTimeoutMilliseconds = TIMEOUT_MS;
opt.dwAuthenticatorAttachment = Attachment(m_roamingOnly);
opt.bRequireResidentKey = options.residentKey;
opt.dwUserVerificationRequirement = UserVerification(options.userVerification);
opt.dwAttestationConveyancePreference = WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT;
PWEBAUTHN_CREDENTIAL_ATTESTATION att = nullptr;
HRESULT hr = WebAuthNAuthenticatorMakeCredential(OwnerWindow(), &rp, &user, &algs, &clientData, &opt, &att);
if (FAILED(hr))
{
error = ApiError("WebAuthNAuthenticatorMakeCredential", hr);
return false;
}
response.clientDataJson = clientDataJson;
response.format = ToUtf8(att->pwszFormatType);
response.authData = Bytes(att->pbAuthenticatorData, att->cbAuthenticatorData);
response.attStmt = Bytes(att->pbAttestation, att->cbAttestation);
if (att->dwVersion >= WEBAUTHN_CREDENTIAL_ATTESTATION_VERSION_3 && att->dwUsedTransport != 0)
{
response.transport = TransportName(att->dwUsedTransport);
}
WebAuthNFreeCredentialAttestation(att);
return true;
}
bool WindowsWebAuthnClient::GetAssertion(const AuthenticationOptions& options, const std::string& clientDataJson,
std::vector<AssertionResponse>& responses, FidoError& error)
{
std::wstring wRpId = ToWide(options.rpId);
std::vector<BYTE> cd(clientDataJson.begin(), clientDataJson.end());
WEBAUTHN_CLIENT_DATA clientData{};
clientData.dwVersion = WEBAUTHN_CLIENT_DATA_CURRENT_VERSION;
clientData.cbClientDataJSON = static_cast<DWORD>(cd.size());
clientData.pbClientDataJSON = cd.data();
clientData.pwszHashAlgId = WEBAUTHN_HASH_ALGORITHM_SHA_256;
std::vector<WEBAUTHN_CREDENTIAL> creds;
for (const auto& id : options.allowCredentials)
{
WEBAUTHN_CREDENTIAL c{};
c.dwVersion = WEBAUTHN_CREDENTIAL_CURRENT_VERSION;
c.cbId = static_cast<DWORD>(id.size());
c.pbId = const_cast<PBYTE>(id.data()); // not modified by webauthn.dll
c.pwszCredentialType = WEBAUTHN_CREDENTIAL_TYPE_PUBLIC_KEY;
creds.push_back(c);
}
WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS opt{};
opt.dwVersion = WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_1;
opt.dwTimeoutMilliseconds = TIMEOUT_MS;
opt.CredentialList.cCredentials = static_cast<DWORD>(creds.size());
opt.CredentialList.pCredentials = creds.empty() ? nullptr : creds.data();
opt.dwAuthenticatorAttachment = Attachment(m_roamingOnly);
opt.dwUserVerificationRequirement = UserVerification(options.userVerification);
PWEBAUTHN_ASSERTION assertion = nullptr;
HRESULT hr = WebAuthNAuthenticatorGetAssertion(OwnerWindow(), wRpId.c_str(), &clientData, &opt, &assertion);
if (FAILED(hr))
{
error = ApiError("WebAuthNAuthenticatorGetAssertion", hr);
return false;
}
// Windows lets the user pick one account, so there is always exactly one assertion
AssertionResponse a;
a.credentialId = Bytes(assertion->Credential.pbId, assertion->Credential.cbId);
a.clientDataJson = clientDataJson;
a.authData = Bytes(assertion->pbAuthenticatorData, assertion->cbAuthenticatorData);
a.signature = Bytes(assertion->pbSignature, assertion->cbSignature);
a.userHandle = Bytes(assertion->pbUserId, assertion->cbUserId);
responses.push_back(std::move(a));
WebAuthNFreeAssertion(assertion);
return true;
}