-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCborDecoderTests.cs
More file actions
423 lines (352 loc) · 14 KB
/
Copy pathCborDecoderTests.cs
File metadata and controls
423 lines (352 loc) · 14 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using NpgsqlRestClient.Fido2;
namespace NpgsqlRestTests.Fido2;
/// <summary>
/// Unit tests for the CborDecoder class.
/// Tests CBOR decoding functionality for WebAuthn attestation objects.
/// </summary>
public class CborDecoderTests
{
#region DecodeAttestationObject Tests
[Fact]
public void DecodeAttestationObject_WithValidNoneFormat_ReturnsAttestationObject()
{
// Arrange - Minimal attestation object with "none" format
// CBOR map with: fmt="none", authData=empty bytes, attStmt=empty map
var cborData = CreateMinimalAttestationObjectCbor();
// Act
var result = CborDecoder.DecodeAttestationObject(cborData);
// Assert
result.Should().NotBeNull();
result!.Fmt.Should().Be("none");
result.AuthData.Should().NotBeNull();
}
[Fact]
public void DecodeAttestationObject_WithNullInput_ReturnsNull()
{
// Act
var result = CborDecoder.DecodeAttestationObject(null!);
// Assert
result.Should().BeNull();
}
[Fact]
public void DecodeAttestationObject_WithEmptyInput_ReturnsNull()
{
// Act
var result = CborDecoder.DecodeAttestationObject([]);
// Assert
result.Should().BeNull();
}
[Fact]
public void DecodeAttestationObject_WithInvalidCbor_ReturnsNull()
{
// Arrange - Invalid CBOR data (not a map)
byte[] invalidData = [0x00, 0x01, 0x02];
// Act
var result = CborDecoder.DecodeAttestationObject(invalidData);
// Assert
result.Should().BeNull();
}
[Fact]
public void DecodeAttestationObject_WithPackedFormat_ParsesCorrectly()
{
// Arrange - Attestation object with "packed" format
var cborData = CreateAttestationObjectCborWithFormat("packed");
// Act
var result = CborDecoder.DecodeAttestationObject(cborData);
// Assert
result.Should().NotBeNull();
result!.Fmt.Should().Be("packed");
}
[Fact]
public void DecodeAttestationObject_WithAuthData_ParsesCorrectly()
{
// Arrange - Attestation object with actual authData
var authData = new byte[37]; // Minimum valid authData size
System.Security.Cryptography.RandomNumberGenerator.Fill(authData);
var cborData = CreateAttestationObjectWithAuthData(authData);
// Act
var result = CborDecoder.DecodeAttestationObject(cborData);
// Assert
result.Should().NotBeNull();
result!.AuthData.Should().BeEquivalentTo(authData);
}
#endregion
#region CredentialPublicKey Tests
[Fact]
public void CredentialPublicKey_Decode_WithES256Key_ReturnsValidKey()
{
// Arrange - COSE key for ES256 (ECDSA with P-256)
var cborData = CreateES256CoseKeyCbor();
// Act
var result = CredentialPublicKey.Decode(cborData);
// Assert
result.Should().NotBeNull();
result.Algorithm.Should().Be(COSEAlgorithmIdentifier.ES256);
result.AlgorithmInt.Should().Be(CoseAlgorithm.ES256);
}
[Fact]
public void CredentialPublicKey_Decode_WithRS256Key_ReturnsValidKey()
{
// Arrange - COSE key for RS256 (RSA with SHA-256)
var cborData = CreateRS256CoseKeyCbor();
// Act
var result = CredentialPublicKey.Decode(cborData);
// Assert
result.Should().NotBeNull();
result.Algorithm.Should().Be(COSEAlgorithmIdentifier.RS256);
result.AlgorithmInt.Should().Be(CoseAlgorithm.RS256);
}
[Fact]
public void CredentialPublicKey_Decode_WithInvalidCbor_ThrowsException()
{
// Arrange - Invalid CBOR data
byte[] invalidData = [0xFF, 0xFE, 0xFD];
// Act & Assert
var action = () => CredentialPublicKey.Decode(invalidData);
action.Should().Throw<Exception>();
}
[Fact]
public void CredentialPublicKey_IsSupportedAlgorithm_ES256_ReturnsTrue()
{
CredentialPublicKey.IsSupportedAlgorithm(CoseAlgorithm.ES256).Should().BeTrue();
}
[Fact]
public void CredentialPublicKey_IsSupportedAlgorithm_RS256_ReturnsTrue()
{
CredentialPublicKey.IsSupportedAlgorithm(CoseAlgorithm.RS256).Should().BeTrue();
}
[Fact]
public void CredentialPublicKey_IsSupportedAlgorithm_PS256_ReturnsTrue()
{
CredentialPublicKey.IsSupportedAlgorithm(-37).Should().BeTrue(); // PS256
}
[Fact]
public void CredentialPublicKey_IsSupportedAlgorithm_ES384_ReturnsTrue()
{
CredentialPublicKey.IsSupportedAlgorithm(-35).Should().BeTrue(); // ES384
}
[Fact]
public void CredentialPublicKey_IsSupportedAlgorithm_ES512_ReturnsTrue()
{
CredentialPublicKey.IsSupportedAlgorithm(-36).Should().BeTrue(); // ES512
}
[Fact]
public void CredentialPublicKey_IsSupportedAlgorithm_UnsupportedAlgorithm_ReturnsFalse()
{
CredentialPublicKey.IsSupportedAlgorithm(9999).Should().BeFalse();
}
[Fact]
public void CredentialPublicKey_Verify_WithValidES256Signature_ReturnsTrue()
{
// Arrange - Create a real ECDSA key pair
using var ecdsa = System.Security.Cryptography.ECDsa.Create(System.Security.Cryptography.ECCurve.NamedCurves.nistP256);
var publicKeyParams = ecdsa.ExportParameters(false);
var coseKey = CreateES256CoseKeyFromParams(publicKeyParams);
var data = new byte[64];
System.Security.Cryptography.RandomNumberGenerator.Fill(data);
// Sign the data with DER format (what WebAuthn uses)
var signature = ecdsa.SignData(data, System.Security.Cryptography.HashAlgorithmName.SHA256, System.Security.Cryptography.DSASignatureFormat.Rfc3279DerSequence);
var credentialPublicKey = CredentialPublicKey.Decode(coseKey);
// Act
var result = credentialPublicKey.Verify(data, signature);
// Assert
result.Should().BeTrue();
}
[Fact]
public void CredentialPublicKey_Verify_WithValidRS256Signature_ReturnsTrue()
{
// Arrange - Create a real RSA key pair
using var rsa = System.Security.Cryptography.RSA.Create(2048);
var publicKeyParams = rsa.ExportParameters(false);
var coseKey = CreateRS256CoseKeyFromParams(publicKeyParams);
var data = new byte[64];
System.Security.Cryptography.RandomNumberGenerator.Fill(data);
// Sign with PKCS1 padding
var signature = rsa.SignData(data, System.Security.Cryptography.HashAlgorithmName.SHA256, System.Security.Cryptography.RSASignaturePadding.Pkcs1);
var credentialPublicKey = CredentialPublicKey.Decode(coseKey);
// Act
var result = credentialPublicKey.Verify(data, signature);
// Assert
result.Should().BeTrue();
}
[Fact]
public void CredentialPublicKey_Verify_WithInvalidSignature_ReturnsFalse()
{
// Arrange
using var ecdsa = System.Security.Cryptography.ECDsa.Create(System.Security.Cryptography.ECCurve.NamedCurves.nistP256);
var publicKeyParams = ecdsa.ExportParameters(false);
var coseKey = CreateES256CoseKeyFromParams(publicKeyParams);
var data = new byte[64];
System.Security.Cryptography.RandomNumberGenerator.Fill(data);
// Create an invalid signature
var invalidSignature = new byte[70];
System.Security.Cryptography.RandomNumberGenerator.Fill(invalidSignature);
var credentialPublicKey = CredentialPublicKey.Decode(coseKey);
// Act
var result = credentialPublicKey.Verify(data, invalidSignature);
// Assert
result.Should().BeFalse();
}
#endregion
#region Helper Methods
/// <summary>
/// Creates a minimal CBOR-encoded attestation object with "none" format.
/// </summary>
private static byte[] CreateMinimalAttestationObjectCbor()
{
// CBOR map with 3 items:
// - "fmt" -> "none"
// - "authData" -> empty byte string
// - "attStmt" -> empty map
return
[
0xA3, // Map with 3 items
0x63, 0x66, 0x6D, 0x74, // Text string "fmt"
0x64, 0x6E, 0x6F, 0x6E, 0x65, // Text string "none"
0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, // Text string "authData"
0x40, // Empty byte string
0x67, 0x61, 0x74, 0x74, 0x53, 0x74, 0x6D, 0x74, // Text string "attStmt"
0xA0 // Empty map
];
}
/// <summary>
/// Creates a CBOR-encoded attestation object with a specific format.
/// </summary>
private static byte[] CreateAttestationObjectCborWithFormat(string format)
{
var formatBytes = System.Text.Encoding.UTF8.GetBytes(format);
var result = new List<byte>
{
0xA3, // Map with 3 items
0x63, 0x66, 0x6D, 0x74, // Text string "fmt"
(byte)(0x60 | formatBytes.Length) // Text string header for format
};
result.AddRange(formatBytes);
result.AddRange([
0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, // Text string "authData"
0x40, // Empty byte string
0x67, 0x61, 0x74, 0x74, 0x53, 0x74, 0x6D, 0x74, // Text string "attStmt"
0xA0 // Empty map
]);
return [.. result];
}
/// <summary>
/// Creates a CBOR-encoded attestation object with authData.
/// </summary>
private static byte[] CreateAttestationObjectWithAuthData(byte[] authData)
{
var result = new List<byte>
{
0xA3, // Map with 3 items
0x63, 0x66, 0x6D, 0x74, // Text string "fmt"
0x64, 0x6E, 0x6F, 0x6E, 0x65, // Text string "none"
0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61 // Text string "authData"
};
// Add auth data byte string
if (authData.Length < 24)
{
result.Add((byte)(0x40 | authData.Length));
}
else if (authData.Length < 256)
{
result.Add(0x58);
result.Add((byte)authData.Length);
}
else
{
result.Add(0x59);
result.Add((byte)(authData.Length >> 8));
result.Add((byte)(authData.Length & 0xFF));
}
result.AddRange(authData);
result.AddRange([
0x67, 0x61, 0x74, 0x74, 0x53, 0x74, 0x6D, 0x74, // Text string "attStmt"
0xA0 // Empty map
]);
return [.. result];
}
/// <summary>
/// Creates a CBOR-encoded COSE key for ES256 (ECDSA with P-256) using a real key pair.
/// </summary>
private static byte[] CreateES256CoseKeyCbor()
{
// Generate a real EC key pair to get valid coordinates
using var ecdsa = System.Security.Cryptography.ECDsa.Create(System.Security.Cryptography.ECCurve.NamedCurves.nistP256);
var ecParams = ecdsa.ExportParameters(false);
return CreateES256CoseKeyFromParams(ecParams);
}
/// <summary>
/// Creates a CBOR-encoded COSE key for RS256 (RSA with SHA-256).
/// </summary>
private static byte[] CreateRS256CoseKeyCbor()
{
// COSE Key for RS256:
// { 1: 3 (kty=RSA), 3: -257 (alg=RS256), -1: n (modulus), -2: e (exponent) }
var n = new byte[256]; // Dummy modulus (2048 bits)
var e = new byte[] { 0x01, 0x00, 0x01 }; // Common exponent 65537
Array.Fill(n, (byte)0xAB);
var result = new List<byte>
{
0xA4, // Map with 4 items
0x01, 0x03, // 1: 3 (kty = RSA)
0x03, 0x39, 0x01, 0x00, // 3: -257 (alg = RS256, encoded as -1-256 = negative, 2 byte length)
0x20, 0x59, 0x01, 0x00 // -1: byte string (256 bytes) for n
};
result.AddRange(n);
result.AddRange([0x21, 0x43]); // -2: byte string (3 bytes) for e
result.AddRange(e);
return [.. result];
}
/// <summary>
/// Creates a CBOR-encoded COSE key for ES256 from ECParameters.
/// </summary>
private static byte[] CreateES256CoseKeyFromParams(System.Security.Cryptography.ECParameters ecParams)
{
var result = new List<byte>
{
0xA5, // Map with 5 items
0x01, 0x02, // 1: 2 (kty = EC2)
0x03, 0x26, // 3: -7 (alg = ES256)
0x20, 0x01, // -1: 1 (crv = P-256)
0x21, 0x58, 0x20 // -2: byte string (32 bytes) for X
};
result.AddRange(ecParams.Q.X!);
result.AddRange([0x22, 0x58, 0x20]); // -3: byte string (32 bytes) for Y
result.AddRange(ecParams.Q.Y!);
return [.. result];
}
/// <summary>
/// Creates a CBOR-encoded COSE key for RS256 from RSAParameters.
/// </summary>
private static byte[] CreateRS256CoseKeyFromParams(System.Security.Cryptography.RSAParameters rsaParams)
{
var n = rsaParams.Modulus!;
var e = rsaParams.Exponent!;
var result = new List<byte>
{
0xA4, // Map with 4 items
0x01, 0x03, // 1: 3 (kty = RSA)
0x03, 0x39, 0x01, 0x00, // 3: -257 (alg = RS256)
};
// Add n (modulus)
result.Add(0x20); // -1 label
if (n.Length < 256)
{
result.Add(0x58);
result.Add((byte)n.Length);
}
else
{
result.Add(0x59);
result.Add((byte)(n.Length >> 8));
result.Add((byte)(n.Length & 0xFF));
}
result.AddRange(n);
// Add e (exponent)
result.Add(0x21); // -2 label
result.Add((byte)(0x40 | e.Length));
result.AddRange(e);
return [.. result];
}
#endregion
}