-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathFidoU2f.cs
180 lines (162 loc) · 8.12 KB
/
FidoU2f.cs
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
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using fido2_net_lib.Test;
using Fido2NetLib;
using Fido2NetLib.Cbor;
using Fido2NetLib.Objects;
using Xunit;
namespace Test.Attestation
{
public class FidoU2f : Fido2Tests.Attestation
{
public FidoU2f()
{
_aaguid = Guid.Empty;
_attestationObject.Add("fmt", "fido-u2f");
X509Certificate2 attestnCert;
using (var ecdsaAtt = ECDsa.Create(ECCurve.NamedCurves.nistP256))
{
var attRequest = new CertificateRequest("CN=U2FTesting, OU=Authenticator Attestation, O=FIDO2-NET-LIB, C=US", ecdsaAtt, HashAlgorithmName.SHA256);
attRequest.CertificateExtensions.Add(notCAExt);
using (attestnCert = attRequest.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(2)))
{
var X5c = new CborArray {
attestnCert.RawData
};
var ecparams = ecdsaAtt.ExportParameters(true);
_credentialPublicKey = Fido2Tests.MakeCredentialPublicKey(COSE.KeyType.EC2, COSE.Algorithm.ES256, COSE.EllipticCurve.P256, ecparams.Q.X, ecparams.Q.Y);
var x = (byte[])_credentialPublicKey.GetCborObject()[COSE.KeyTypeParameter.X];
var y = (byte[])_credentialPublicKey.GetCborObject()[COSE.KeyTypeParameter.Y];
byte[] publicKeyU2F = DataHelper.Concat(new byte[1] { 0x4 }, x, y);
byte[] verificationData = DataHelper.Concat(
new byte[1] { 0x00 },
_rpIdHash,
_clientDataHash,
_credentialID,
publicKeyU2F
);
byte[] signature = Fido2Tests.SignData(COSE.KeyType.EC2, COSE.Algorithm.ES256, verificationData, ecdsaAtt, null, null);
_attestationObject.Add("attStmt", new CborMap {
{ "x5c", X5c },
{ "sig", signature }
});
}
}
}
[Fact]
public async void TestU2f()
{
var res = await MakeAttestationResponse();
Assert.Equal(string.Empty, res.ErrorMessage);
Assert.Equal("ok", res.Status);
Assert.Equal(_aaguid, res.Result.Aaguid);
Assert.Equal(_signCount, res.Result.Counter);
Assert.Equal("fido-u2f", res.Result.CredType);
Assert.Equal(_credentialID, res.Result.CredentialId);
Assert.Null(res.Result.ErrorMessage);
Assert.Equal(_credentialPublicKey.GetBytes(), res.Result.PublicKey);
Assert.Null(res.Result.Status);
Assert.Equal("Test User", res.Result.User.DisplayName);
Assert.Equal(System.Text.Encoding.UTF8.GetBytes("testuser"), res.Result.User.Id);
Assert.Equal("testuser", res.Result.User.Name);
}
[Fact]
public void TestU2fWithAaguid()
{
_aaguid = new Guid("F1D0F1D0-F1D0-F1D0-F1D0-F1D0F1D0F1D0");
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Aaguid was not empty parsing fido-u2f atttestation statement", ex.Result.Message);
}
[Fact]
public void TestU2fMissingX5c()
{
((CborMap)_attestationObject["attStmt"]).Set("x5c", CborNull.Instance);
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Malformed x5c in fido-u2f attestation", ex.Result.Message);
}
[Fact]
public void TestU2fX5cNotArray()
{
((CborMap)_attestationObject["attStmt"]).Set("x5c", new CborTextString("boomerang"));
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Malformed x5c in fido-u2f attestation", ex.Result.Message);
}
[Fact]
public void TestU2fX5cCountNotOne()
{
((CborMap)_attestationObject["attStmt"]).Set("x5c", new CborArray { new byte[0], new byte[0] });
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Malformed x5c in fido-u2f attestation", ex.Result.Message);
}
[Fact]
public void TestU2fX5cValueNotByteString()
{
((CborMap)_attestationObject["attStmt"]).Set("x5c", new CborTextString("x"));
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Malformed x5c in fido-u2f attestation", ex.Result.Message);
}
[Fact]
public void TestU2fX5cValueZeroLengthByteString()
{
((CborMap)_attestationObject["attStmt"]).Set("x5c", new CborArray { new byte[0] });
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Malformed x5c in fido-u2f attestation", ex.Result.Message);
}
[Fact]
public void TestU2fAttCertNotP256()
{
using (var ecdsaAtt = ECDsa.Create(ECCurve.NamedCurves.nistP384))
{
var attRequest = new CertificateRequest("CN=U2FTesting, OU=Authenticator Attestation, O=FIDO2-NET-LIB, C=US", ecdsaAtt, HashAlgorithmName.SHA256);
attRequest.CertificateExtensions.Add(
new X509BasicConstraintsExtension(false, false, 0, false));
using (var attestnCert = attRequest.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(2)))
{
var attnStmt = (CborMap)_attestationObject["attStmt"];
attnStmt.Set("x5c", new CborArray { attestnCert.RawData });
}
}
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Attestation certificate public key is not an Elliptic Curve (EC) public key over the P-256 curve", ex.Result.Message);
}
[Fact]
public void TestU2fSigNull()
{
((CborMap)_attestationObject["attStmt"]).Set("sig", CborNull.Instance);
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Invalid fido-u2f attestation signature", ex.Result.Message);
}
[Fact]
public void TestU2fSigNotByteString()
{
((CborMap)_attestationObject["attStmt"]).Set("sig", new CborTextString("walrus"));
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Invalid fido-u2f attestation signature", ex.Result.Message);
}
[Fact]
public void TestU2fSigByteStringZeroLen()
{
((CborMap)_attestationObject["attStmt"]).Set("sig", new CborByteString(new byte[0]));
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Invalid fido-u2f attestation signature", ex.Result.Message);
}
[Fact]
public void TestU2fSigNotASN1()
{
((CborMap)_attestationObject["attStmt"]).Set("sig", new CborByteString(new byte[] { 0xf1, 0xd0 }));
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Failed to decode fido-u2f attestation signature from ASN.1 encoded form", ex.Result.Message);
}
[Fact]
public void TestU2fBadSig()
{
var attnStmt = (CborMap)_attestationObject["attStmt"];
var sig = (byte[])attnStmt["sig"];
sig[sig.Length - 1] ^= 0xff;
attnStmt.Set("sig", new CborByteString(sig));
var ex = Assert.ThrowsAsync<Fido2VerificationException>(() => MakeAttestationResponse());
Assert.Equal("Invalid fido-u2f attestation signature", ex.Result.Message);
}
}
}