-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathCredentialPublicKey.cs
259 lines (231 loc) · 10.1 KB
/
CredentialPublicKey.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using NSec.Cryptography;
using PeterO.Cbor;
namespace Fido2NetLib.Objects
{
public class CredentialPublicKey
{
public CredentialPublicKey(Stream stream) : this(CBORObject.Read(stream))
{
}
public CredentialPublicKey(byte[] cpk) : this(CBORObject.DecodeFromBytes(cpk))
{
}
public CredentialPublicKey(CBORObject cpk)
{
_cpk = cpk;
_type = (COSE.KeyType)cpk[CBORObject.FromObject(COSE.KeyCommonParameter.KeyType)].AsInt32();
_alg = (COSE.Algorithm)cpk[CBORObject.FromObject(COSE.KeyCommonParameter.Alg)].AsInt32();
}
public CredentialPublicKey(X509Certificate2 cert, int alg)
{
_cpk = CBORObject.NewMap();
var keyAlg = cert.GetKeyAlgorithm();
_type = CoseKeyTypeFromOid[keyAlg];
_alg = (COSE.Algorithm)alg;
_cpk.Add(COSE.KeyCommonParameter.KeyType, _type);
_cpk.Add(COSE.KeyCommonParameter.Alg, alg);
if (COSE.KeyType.RSA == _type)
{
var keyParams = cert.GetRSAPublicKey().ExportParameters(false);
_cpk.Add(COSE.KeyTypeParameter.N, keyParams.Modulus);
_cpk.Add(COSE.KeyTypeParameter.E, keyParams.Exponent);
}
if (COSE.KeyType.EC2 == _type)
{
var ecDsaPubKey = cert.GetECDsaPublicKey();
var keyParams = ecDsaPubKey.ExportParameters(false);
if (keyParams.Curve.Oid.FriendlyName.Equals("secP256k1"))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P256K);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (keyParams.Curve.Oid.FriendlyName.Equals(ECCurve.NamedCurves.nistP256.Oid.FriendlyName))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P256);
if (keyParams.Curve.Oid.FriendlyName.Equals(ECCurve.NamedCurves.nistP384.Oid.FriendlyName))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P384);
if (keyParams.Curve.Oid.FriendlyName.Equals(ECCurve.NamedCurves.nistP521.Oid.FriendlyName))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P521);
}
else
{
if (keyParams.Curve.Oid.Value.Equals(ECCurve.NamedCurves.nistP256.Oid.Value))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P256);
if (keyParams.Curve.Oid.Value.Equals(ECCurve.NamedCurves.nistP384.Oid.Value))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P384);
if (keyParams.Curve.Oid.Value.Equals(ECCurve.NamedCurves.nistP521.Oid.Value))
_cpk.Add(COSE.KeyTypeParameter.Crv, COSE.EllipticCurve.P521);
}
_cpk.Add(COSE.KeyTypeParameter.X, keyParams.Q.X);
_cpk.Add(COSE.KeyTypeParameter.Y, keyParams.Q.Y);
}
}
public bool Verify(byte[] data, byte[] sig)
{
switch (_type)
{
case COSE.KeyType.EC2:
using(ECDsa ecdsa = CreateECDsa())
{
var ecsig = CryptoUtils.SigFromEcDsaSig(sig, ecdsa.KeySize);
return ecdsa.VerifyData(data, ecsig, CryptoUtils.HashAlgFromCOSEAlg((int)_alg));
}
case COSE.KeyType.RSA:
using (RSA rsa = CreateRsa())
{
return rsa.VerifyData(data, sig, CryptoUtils.HashAlgFromCOSEAlg((int)_alg), Padding);
}
case COSE.KeyType.OKP:
return SignatureAlgorithm.Ed25519.Verify(EdDSAPublicKey, data, sig);
}
throw new InvalidOperationException($"Missing or unknown kty {_type}");
}
internal RSA CreateRsa()
{
if (_type == COSE.KeyType.RSA)
{
var rsa = RSA.Create();
rsa.ImportParameters(
new RSAParameters()
{
Modulus = _cpk[CBORObject.FromObject(COSE.KeyTypeParameter.N)].GetByteString(),
Exponent = _cpk[CBORObject.FromObject(COSE.KeyTypeParameter.E)].GetByteString()
}
);
return rsa;
}
return null;
}
internal ECDsa CreateECDsa()
{
if (_type == COSE.KeyType.EC2)
{
var point = new ECPoint
{
X = _cpk[CBORObject.FromObject(COSE.KeyTypeParameter.X)].GetByteString(),
Y = _cpk[CBORObject.FromObject(COSE.KeyTypeParameter.Y)].GetByteString(),
};
ECCurve curve;
var crv = (COSE.EllipticCurve)_cpk[CBORObject.FromObject(COSE.KeyTypeParameter.Crv)].AsInt32();
switch (_alg)
{
case COSE.Algorithm.ES256:
switch (crv)
{
case COSE.EllipticCurve.P256:
case COSE.EllipticCurve.P256K:
curve = ECCurve.NamedCurves.nistP256;
break;
default:
throw new InvalidOperationException($"Missing or unknown crv {crv}");
}
break;
case COSE.Algorithm.ES384:
switch (crv) // https://www.iana.org/assignments/cose/cose.xhtml#elliptic-curves
{
case COSE.EllipticCurve.P384:
curve = ECCurve.NamedCurves.nistP384;
break;
default:
throw new InvalidOperationException($"Missing or unknown crv {crv}");
}
break;
case COSE.Algorithm.ES512:
switch (crv) // https://www.iana.org/assignments/cose/cose.xhtml#elliptic-curves
{
case COSE.EllipticCurve.P521:
curve = ECCurve.NamedCurves.nistP521;
break;
default:
throw new InvalidOperationException($"Missing or unknown crv {crv}");
}
break;
default:
throw new InvalidOperationException($"Missing or unknown alg {_alg}");
}
return ECDsa.Create(new ECParameters
{
Q = point,
Curve = curve
});
}
return null;
}
internal RSASignaturePadding Padding
{
get
{
if (_type == COSE.KeyType.RSA)
{
switch (_alg) // https://www.iana.org/assignments/cose/cose.xhtml#algorithms
{
case COSE.Algorithm.PS256:
case COSE.Algorithm.PS384:
case COSE.Algorithm.PS512:
return RSASignaturePadding.Pss;
case COSE.Algorithm.RS1:
case COSE.Algorithm.RS256:
case COSE.Algorithm.RS384:
case COSE.Algorithm.RS512:
return RSASignaturePadding.Pkcs1;
default:
throw new InvalidOperationException($"Missing or unknown alg {_alg}");
}
}
return null;
}
}
internal NSec.Cryptography.PublicKey EdDSAPublicKey
{
get
{
if (_type == COSE.KeyType.OKP)
{
switch (_alg) // https://www.iana.org/assignments/cose/cose.xhtml#algorithms
{
case COSE.Algorithm.EdDSA:
var crv = (COSE.EllipticCurve)_cpk[CBORObject.FromObject(COSE.KeyTypeParameter.Crv)].AsInt32();
switch (crv) // https://www.iana.org/assignments/cose/cose.xhtml#elliptic-curves
{
case COSE.EllipticCurve.Ed25519:
return NSec.Cryptography.PublicKey.Import(SignatureAlgorithm.Ed25519, _cpk[CBORObject.FromObject(COSE.KeyTypeParameter.X)].GetByteString(), KeyBlobFormat.RawPublicKey);
default:
throw new InvalidOperationException($"Missing or unknown crv {crv}");
}
default:
throw new InvalidOperationException($"Missing or unknown alg {_alg}");
}
}
return null;
}
}
internal readonly COSE.KeyType _type;
internal readonly COSE.Algorithm _alg;
internal readonly CBORObject _cpk;
internal static readonly Dictionary<string, COSE.KeyType> CoseKeyTypeFromOid = new Dictionary<string, COSE.KeyType>
{
{ "1.2.840.10045.2.1", COSE.KeyType.EC2 },
{ "1.2.840.113549.1.1.1", COSE.KeyType.RSA}
};
public override string ToString()
{
return _cpk.ToString();
}
public byte[] GetBytes()
{
return _cpk.EncodeToBytes();
}
public bool IsSameAlg(COSE.Algorithm alg)
{
return _alg.Equals(alg);
}
public CBORObject GetCBORObject()
{
return _cpk;
}
}
}