-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginAuthenticator.cs
More file actions
300 lines (268 loc) · 16.1 KB
/
Copy pathPluginAuthenticator.cs
File metadata and controls
300 lines (268 loc) · 16.1 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System.Buffers.Binary;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using DarksFIDO2.Core.Passkeys;
namespace DarksFIDO2.Provider;
[ComVisible(true)]
[Guid("D26BCF6F-B54C-43FF-9F06-D5BF148625F7")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPluginAuthenticator
{
[PreserveSig] int MakeCredential(IntPtr request, IntPtr response);
[PreserveSig] int GetAssertion(IntPtr request, IntPtr response);
[PreserveSig] int CancelOperation(IntPtr request);
[PreserveSig] int GetLockStatus(IntPtr status);
}
[ComVisible(true)]
[Guid("00000001-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IClassFactory
{
[PreserveSig] int CreateInstance(IntPtr outer, ref Guid interfaceId, out IntPtr instance);
[PreserveSig] int LockServer([MarshalAs(UnmanagedType.Bool)] bool value);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public sealed class PluginClassFactory : IClassFactory
{
public int CreateInstance(IntPtr outer, ref Guid interfaceId, out IntPtr instance)
{
instance = IntPtr.Zero;
if (outer != IntPtr.Zero) return unchecked((int)0x80040110);
var authenticator = new PluginAuthenticator();
IntPtr unknown = Marshal.GetIUnknownForObject(authenticator);
try { return Marshal.QueryInterface(unknown, in interfaceId, out instance); }
finally { Marshal.Release(unknown); }
}
public int LockServer(bool value) => 0;
}
[ComVisible(true)]
[Guid("AA84D912-9B38-4E8F-B37E-7C95687A2D41")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IPluginAuthenticator))]
public sealed class PluginAuthenticator : IPluginAuthenticator
{
private static readonly Guid Aaguid = ProviderIds.Aaguid;
private static readonly SemaphoreSlim OperationGate = new(1, 1);
private static readonly ConcurrentDictionary<Guid, CancellationTokenSource> Operations = new();
private readonly SoftwarePasskeyStore _store = new();
private readonly PasskeyProviderContext _profileContext = new();
public int MakeCredential(IntPtr requestPointer, IntPtr responsePointer) =>
Run(requestPointer, responsePointer, makeCredential: true);
public int GetAssertion(IntPtr requestPointer, IntPtr responsePointer) =>
Run(requestPointer, responsePointer, makeCredential: false);
public int CancelOperation(IntPtr requestPointer)
{
if (requestPointer == IntPtr.Zero) return NativeMethods.EInvalidArg;
NativeMethods.PluginCancelRequest request = Marshal.PtrToStructure<NativeMethods.PluginCancelRequest>(requestPointer);
if (Operations.TryGetValue(request.TransactionId, out CancellationTokenSource? cancellation)) cancellation.Cancel();
return 0;
}
public int GetLockStatus(IntPtr status)
{
if (status == IntPtr.Zero) return NativeMethods.EInvalidArg;
Marshal.WriteInt32(status, _profileContext.GetActiveProfileId() is null ? 0 : 1);
return 0;
}
private int Run(IntPtr requestPointer, IntPtr responsePointer, bool makeCredential)
{
if (requestPointer == IntPtr.Zero || responsePointer == IntPtr.Zero) return NativeMethods.EInvalidArg;
Marshal.StructureToPtr(new NativeMethods.PluginOperationResponse(), responsePointer, false);
string operationName = makeCredential ? "MakeCredential" : "GetAssertion";
Guid transactionId = Guid.Empty;
if (!OperationGate.Wait(TimeSpan.FromSeconds(5)))
{
ProviderLog.Write(transactionId, operationName, "Rejected because another provider request remained busy for five seconds.");
return unchecked((int)0x800700AA);
}
byte[] encoded = [];
byte[] requestSignature = [];
try
{
NativeMethods.PluginOperationRequest request = Marshal.PtrToStructure<NativeMethods.PluginOperationRequest>(requestPointer);
transactionId = request.TransactionId;
ProviderLog.Write(transactionId, operationName, "Request received.");
if (request.TransactionId == Guid.Empty || request.RequestType != 1 || request.EncodedRequestLength == 0 ||
request.EncodedRequest == IntPtr.Zero || request.RequestSignatureLength == 0 || request.RequestSignature == IntPtr.Zero)
return NativeMethods.EInvalidArg;
encoded = Copy(request.EncodedRequest, request.EncodedRequestLength, CtapCbor.MaximumDocumentBytes);
requestSignature = Copy(request.RequestSignature, request.RequestSignatureLength, 16 * 1024);
if (!NativeMethods.VerifyPlatformOperation(encoded, requestSignature))
{
ProviderLog.Write(transactionId, operationName, "Windows operation signature verification failed.");
return unchecked((int)0x80096010);
}
using var cancellation = new CancellationTokenSource();
if (!Operations.TryAdd(request.TransactionId, cancellation)) return unchecked((int)0x800700AA);
try
{
byte[] response = [];
try
{
response = makeCredential
? MakeCredentialCore(request, encoded, cancellation.Token)
: GetAssertionCore(request, encoded, cancellation.Token);
if (response.Length is < 1 or > CtapCbor.MaximumDocumentBytes) throw new FormatException("Invalid CTAP response size.");
IntPtr buffer = Marshal.AllocCoTaskMem(response.Length);
try
{
Marshal.Copy(response, 0, buffer, response.Length);
Marshal.StructureToPtr(new NativeMethods.PluginOperationResponse
{
EncodedResponseLength = (uint)response.Length,
EncodedResponse = buffer
}, responsePointer, false);
}
catch { Marshal.FreeCoTaskMem(buffer); throw; }
ProviderLog.Write(transactionId, operationName, $"Completed successfully ({response.Length} response bytes).");
return 0;
}
finally { if (response.Length > 0) CryptographicOperations.ZeroMemory(response); }
}
finally { Operations.TryRemove(request.TransactionId, out _); }
}
catch (OperationCanceledException) { ProviderLog.Write(transactionId, operationName, "Cancelled during user verification."); return NativeMethods.NteUserCancelled; }
catch (KeyNotFoundException ex) { ProviderLog.Write(transactionId, operationName, ex.Message); return NativeMethods.NteNotFound; }
catch (FormatException ex) { ProviderLog.Write(transactionId, operationName, "Invalid request: " + ex.Message); return NativeMethods.EInvalidArg; }
catch (CryptographicException ex) { ProviderLog.Write(transactionId, operationName, "Cryptographic failure: " + ex.Message); return NativeMethods.EFail; }
catch (Exception ex) { ProviderLog.Write(transactionId, operationName, "Unexpected failure: " + ex.GetType().Name); return NativeMethods.EFail; }
finally
{
if (encoded.Length > 0) CryptographicOperations.ZeroMemory(encoded);
if (requestSignature.Length > 0) CryptographicOperations.ZeroMemory(requestSignature);
OperationGate.Release();
}
}
private byte[] MakeCredentialCore(NativeMethods.PluginOperationRequest operation, byte[] encoded, CancellationToken cancellation)
{
Dictionary<object, object?> request = CtapCbor.Map(CtapCbor.Decode(encoded));
byte[] clientDataHash = CtapCbor.Bytes(Get(request, 1));
Dictionary<object, object?> rp = CtapCbor.Map(Get(request, 2));
Dictionary<object, object?> user = CtapCbor.Map(Get(request, 3));
string rpId = CtapCbor.Text(Get(rp, "id"));
string rpName = TryText(rp, "name") ?? rpId;
byte[] userId = CtapCbor.Bytes(Get(user, "id"));
string userName = CtapCbor.Text(Get(user, "name"));
string displayName = TryText(user, "displayName") ?? userName;
ValidateIdentity(rpId, rpName, userId, userName, displayName);
ProviderLog.Write(operation.TransactionId, "MakeCredential", $"Validated request for RP {rpId}.");
if (clientDataHash.Length != 32 || !SupportsEs256(Get(request, 4))) throw new FormatException("Unsupported passkey request.");
Guid profileId = _profileContext.GetActiveProfileId()
?? throw new KeyNotFoundException("No Darks FIDO2 profile is currently unlocked. Unlock the intended profile before creating a passkey.");
foreach (byte[] excluded in ReadCredentialList(request, 5))
if (_store.Find(profileId, rpId, [excluded]) is not null) throw new CryptographicException("Credential excluded by the relying party.");
cancellation.ThrowIfCancellationRequested();
if (!NativeMethods.VerifyUser(operation.Window, operation.TransactionId, userName, encoded)) throw new OperationCanceledException();
cancellation.ThrowIfCancellationRequested();
SoftwarePasskeyCredential credential = _store.Create(rpId, rpName, userId, userName, displayName, profileId);
int metadataHr = NativeMethods.AddCredentialMetadata(credential);
if (metadataHr < 0)
{
_store.Remove(profileId, credential.CredentialId);
ProviderLog.Write(operation.TransactionId, "MakeCredential", $"Windows rejected credential metadata with HRESULT 0x{metadataHr:X8}; rolled back the key.");
Marshal.ThrowExceptionForHR(metadataHr);
}
ProviderLog.Write(operation.TransactionId, "MakeCredential", $"Created {credential.RpId} credential {Convert.ToHexString(credential.CredentialId)[..12]} (profile {profileId:D}, {(credential.HardwareBacked ? "TPM" : "software KSP")}).");
byte[] cose = _store.PublicCoseKey(credential);
byte[] authenticatorData = BuildRegistrationAuthenticatorData(rpId, credential.CredentialId, cose);
return CtapCbor.Encode(new Dictionary<object, object?>
{
[1L] = "none",
[2L] = authenticatorData,
[3L] = new Dictionary<object, object?>()
});
}
private byte[] GetAssertionCore(NativeMethods.PluginOperationRequest operation, byte[] encoded, CancellationToken cancellation)
{
Dictionary<object, object?> request = CtapCbor.Map(CtapCbor.Decode(encoded));
string rpId = CtapCbor.Text(Get(request, 1));
byte[] clientDataHash = CtapCbor.Bytes(Get(request, 2));
ValidateRpId(rpId);
if (clientDataHash.Length != 32) throw new FormatException("Invalid client data hash.");
Guid profileId = _profileContext.GetActiveProfileId()
?? throw new KeyNotFoundException("No Darks FIDO2 profile is currently unlocked. Unlock the profile that owns this passkey.");
SoftwarePasskeyCredential credential = _store.Find(profileId, rpId, ReadCredentialList(request, 3))
?? throw new KeyNotFoundException($"No {rpId} passkey belongs to the currently unlocked Darks FIDO2 profile.");
ProviderLog.Write(operation.TransactionId, "GetAssertion", $"Matched {credential.RpId} credential {Convert.ToHexString(credential.CredentialId)[..12]}.");
cancellation.ThrowIfCancellationRequested();
if (!NativeMethods.VerifyUser(operation.Window, operation.TransactionId, credential.UserName, encoded)) throw new OperationCanceledException();
cancellation.ThrowIfCancellationRequested();
uint counter = _store.IncrementCounter(credential);
byte[] authenticatorData = BuildAssertionAuthenticatorData(rpId, counter);
byte[] signed = new byte[authenticatorData.Length + clientDataHash.Length];
authenticatorData.CopyTo(signed, 0); clientDataHash.CopyTo(signed, authenticatorData.Length);
byte[] signature = _store.Sign(credential, signed);
CryptographicOperations.ZeroMemory(signed);
return CtapCbor.Encode(new Dictionary<object, object?>
{
[1L] = new Dictionary<object, object?> { ["id"] = credential.CredentialId, ["type"] = "public-key" },
[2L] = authenticatorData,
[3L] = signature,
[4L] = new Dictionary<object, object?> { ["id"] = credential.UserId, ["name"] = credential.UserName, ["displayName"] = credential.UserDisplayName },
[5L] = 1L
});
}
private static byte[] BuildRegistrationAuthenticatorData(string rpId, byte[] credentialId, byte[] cose)
{
byte[] output = new byte[32 + 1 + 4 + 16 + 2 + credentialId.Length + cose.Length];
SHA256.HashData(Encoding.UTF8.GetBytes(rpId)).CopyTo(output, 0);
output[32] = 0x45; // user present, user verified, attested credential data
Convert.FromHexString(Aaguid.ToString("N")).CopyTo(output, 37);
BinaryPrimitives.WriteUInt16BigEndian(output.AsSpan(53, 2), checked((ushort)credentialId.Length));
credentialId.CopyTo(output, 55);
cose.CopyTo(output, 55 + credentialId.Length);
return output;
}
private static byte[] BuildAssertionAuthenticatorData(string rpId, uint counter)
{
byte[] output = new byte[37];
SHA256.HashData(Encoding.UTF8.GetBytes(rpId)).CopyTo(output, 0);
output[32] = 0x05; // user present and user verified
BinaryPrimitives.WriteUInt32BigEndian(output.AsSpan(33, 4), counter);
return output;
}
private static object? Get(Dictionary<object, object?> map, object key) =>
map.TryGetValue(key is int i ? (long)i : key, out object? value) ? value : throw new FormatException($"Missing CTAP field {key}.");
private static string? TryText(Dictionary<object, object?> map, string key) =>
map.TryGetValue(key, out object? value) && value is string text ? text : null;
private static bool SupportsEs256(object? value) =>
CtapCbor.Array(value).Any(item => CtapCbor.Map(item).TryGetValue("alg", out object? alg) && alg is long number && number == -7);
private static IReadOnlyList<byte[]> ReadCredentialList(Dictionary<object, object?> request, int key)
{
if (!request.TryGetValue((long)key, out object? value)) return [];
List<object?> entries = CtapCbor.Array(value);
if (entries.Count > 128) throw new FormatException("The credential descriptor list is too large.");
var result = new List<byte[]>(entries.Count);
foreach (object? entry in entries)
{
Dictionary<object, object?> descriptor = CtapCbor.Map(entry);
if (descriptor.TryGetValue("type", out object? type) && (!string.Equals(type as string, "public-key", StringComparison.Ordinal)))
throw new FormatException("Unsupported credential descriptor type.");
byte[] bytes = CtapCbor.Bytes(Get(descriptor, "id"));
if (bytes.Length is < 1 or > 1_024) throw new FormatException("Invalid credential identifier length.");
result.Add(bytes);
}
return result;
}
private static byte[] Copy(IntPtr pointer, uint length, int maximumLength)
{
if (length == 0) return [];
if (pointer == IntPtr.Zero || length > maximumLength) throw new FormatException("Invalid native buffer.");
byte[] bytes = new byte[length]; Marshal.Copy(pointer, bytes, 0, bytes.Length); return bytes;
}
private static void ValidateIdentity(string rpId, string rpName, byte[] userId, string userName, string displayName)
{
ValidateRpId(rpId);
if (userId.Length is < 1 or > 64 || !ValidText(rpName, 0, 256) || !ValidText(userName, 1, 256) || !ValidText(displayName, 0, 256))
throw new FormatException("Invalid passkey identity fields.");
}
private static void ValidateRpId(string rpId)
{
if (!ValidText(rpId, 1, 253) || rpId.Contains("://", StringComparison.Ordinal) || rpId.Contains('/') || rpId.Any(char.IsWhiteSpace))
throw new FormatException("Invalid relying-party identifier.");
}
private static bool ValidText(string? value, int minimum, int maximum) =>
value is not null && value.Length >= minimum && value.Length <= maximum && value.IndexOf('\0') < 0;
}