-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignedTokenTests.cs
298 lines (254 loc) · 11.8 KB
/
SignedTokenTests.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
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
using System;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Xunit;
namespace SignedTokenSandbox
{
// Uses ES256 to sign the token, using the NIST P256/secp256 curve https://tools.ietf.org/html/rfc7518#section-3.4
// OpenSSL calls this prime256v1.
// https://wiki.openssl.org/index.php/Command_Line_Elliptic_Curve_Operations#EC_Private_Key_File_Formats
// openssl ecparam -out ec_key.pem -name prime256v1 -genkey
// openssl pkcs8 -topk8 -nocrypt -in ec_key.pem -out private.pem
// openssl ec -in ec_key.pem -pubout -out public.pem
// openssl asn1parse -in ec_key.pem -dump (to see raw bytes)
// Requires the random number generator to be secure when signing tokens, see https://en.wikipedia.org/wiki/EdDSA
// Google and Apple both use ES256 for signed JWTs
// https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/AppleMusicWebServicesReference/SetUpWebServices.html#//apple_ref/doc/uid/TP40017625-CH2-SW1
// https://cloud.google.com/iot/docs/how-tos/credentials/jwts
public class SignedTokenTests
{
// https://www.scottbrady91.com/C-Sharp/JWT-Signing-using-ECDSA-in-dotnet-Core
// https://stackoverflow.com/questions/24251336/import-a-public-key-from-somewhere-else-to-cngkey
[Fact]
public void SignedTokenRoundTripGeneratedKeys()
{
var (privateKey, publicKey) = GenerateKeys();
var payload = Encoding.UTF8.GetBytes("hello");
var signatureBytes = Sign(payload, publicKey, privateKey);
Assert.True(Verify(payload, signatureBytes, publicKey));
}
[Fact]
public void SignedTokenRoundTripExistingKeys()
{
var (privateKey, publicKey) = ReadPem("private.pem");
var payload = Encoding.UTF8.GetBytes("hello");
var signatureBytes = Sign(payload, publicKey, privateKey);
Assert.True(Verify(payload, signatureBytes, publicKey));
}
[Fact]
public void KeyPairsMatch()
{
var ecKeys = ReadPem("ec_key.pem");
var publicKey = ReadPem("public.pem").PublicKey;
var privateKeys = ReadPem("private.pem");
Assert.Equal(publicKey, privateKeys.PublicKey);
Assert.Equal(publicKey, ecKeys.PublicKey);
Assert.Equal(privateKeys.PrivateKey, ecKeys.PrivateKey);
}
// https://jwt.io/
[Fact]
public void TokenFormatMatches()
{
var jwtHandler = new JwtSecurityTokenHandler();
var (privateKey, publicKey) = ReadPem("private.pem");
var jwtToken = jwtHandler.CreateJwtSecurityToken(
issuer: "auth.example.com",
audience: "all-apis.example.com",
issuedAt: new DateTime(2018, 1, 1),
notBefore: new DateTime(2018, 1, 1),
expires: new DateTime(2028, 1, 1),
subject: new ClaimsIdentity(new[] { new Claim("sub", "2986689"), new Claim("is_admin_consumer", "true") }),
signingCredentials: new SigningCredentials(new ECDsaSecurityKey(LoadPrivateKey(privateKey)), SecurityAlgorithms.EcdsaSha256)
);
string signedToken = jwtHandler.WriteToken(jwtToken);
Assert.StartsWith("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyOTg2Njg5IiwiaXNfYWRtaW5fY29uc3VtZXIiOiJ0cnVlIiwibmJmIjoxNTE0NzkzNjAwLCJleHAiOjE4MzAzMjY0MDAsImlhdCI6MTUxNDc5MzYwMCwiaXNzIjoiYXV0aC5leGFtcGxlLmNvbSIsImF1ZCI6ImFsbC1hcGlzLmV4YW1wbGUuY29tIn0.", signedToken);
jwtHandler.ValidateToken(signedToken, new TokenValidationParameters
{
ValidIssuer = "auth.example.com",
ValidAudience = "all-apis.example.com",
IssuerSigningKey = new ECDsaSecurityKey(LoadPublicKey(publicKey))
}, out var parsedSecurityToken);
var parsedJwtToken = (JwtSecurityToken) parsedSecurityToken;
Assert.Equal("2986689", parsedJwtToken.Subject);
Assert.Equal("true", parsedJwtToken.Claims.First(x => x.Type == "is_admin_consumer").Value);
}
[Theory]
[InlineData("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyOTg2Njg5IiwiaXNfYWRtaW5fY29uc3VtZXIiOiJ0cnVlIiwibmJmIjoxNTE0NzkzNjAwLCJleHAiOjE4MzAzMjY0MDAsImlhdCI6MTUxNDc5MzYwMCwiaXNzIjoiYXV0aC5leGFtcGxlLmNvbSIsImF1ZCI6ImFsbC1hcGlzLmV4YW1wbGUuY29tIn0.P5k9R4aocz7FinBoVa0WkYH2jn7C9_hG2846GzAfBeaFcNuN65y5EateEZ1g3tpEMzyQ03YW2wvujt0ORzlvdA")]
public void ParsesPreviouslySignedToken(string token)
{
var jwtHandler = new JwtSecurityTokenHandler();
var publicKey = ReadPem("public.pem").PublicKey;
jwtHandler.ValidateToken(token, new TokenValidationParameters
{
ValidIssuer = "auth.example.com",
ValidAudience = "all-apis.example.com",
IssuerSigningKey = new ECDsaSecurityKey(LoadPublicKey(publicKey))
}, out var parsedToken);
var jwtToken = (JwtSecurityToken) parsedToken;
Assert.Equal("2986689", jwtToken.Subject);
}
[Theory]
[InlineData("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.e30.1xifFDkLeNVl735O28sR7HGbURRvRnnCDy8zvdLYuCWxFOTIryW2Q1gxw-FxCAQx_awERB-eF0CY87pG9rm-GQ", "SecurityTokenNoExpirationException")]
[InlineData("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyOTg2Njg5IiwiaXNfYWRtaW5fY29uc3VtZXIiOiJ0cnVlIiwibmJmIjoxNTE0NzkzNjAwLCJleHAiOjE1MTQ3OTM2MDAsImlhdCI6MTUxNDc5MzYwMCwiaXNzIjoiYXV0aC5mYWl0aGxpZmUuY29tIiwiYXVkIjoiYWxsLWFwaXMuZmFpdGhsaWZlLmNvbSJ9.H5MVAxhBrLYYYSIE7LIjREj60d-wHGWAL3HLr2yJt4sfFI3oC3VSCaubP7TsHnLr310Ix60-3cppW7ncl5hUJQ", "SecurityTokenExpiredException")]
[InlineData("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.1HogBH-GQS4ANFf4effJXmQSkJ5nr1bExZ7nlL7VZPoeHVoeJz4QtMFAAQFrNipRBuhYzny1bOG3zPzD-mUXPA", "SecurityTokenInvalidSignatureException")]
public void RejectsInvalidToken(string token, string exceptionName)
{
var jwtHandler = new JwtSecurityTokenHandler();
var publicKey = ReadPem("public.pem").PublicKey;
try
{
jwtHandler.ValidateToken(token, new TokenValidationParameters
{
ValidAudience = "all-apis.example.com",
ValidIssuer = "auth.example.com",
IssuerSigningKey = new ECDsaSecurityKey(LoadPublicKey(publicKey)),
}, out var parsedToken);
}
catch (Exception e)
{
Assert.Equal(exceptionName, e.GetType().Name);
}
}
[Theory]
[InlineData("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyOTg2Njg5IiwiaXNfYWRtaW5fY29uc3VtZXIiOiJ0cnVlIiwibmJmIjoxNTE0NzkzNjAwLCJleHAiOjE4MzAzMjY0MDAsImlhdCI6MTUxNDc5MzYwMCwiaXNzIjoiYXV0aC5mYWl0aGxpZmUuY29tIiwiYXVkIjoiYWxsLWFwaXMuZmFpdGhsaWZlLmNvbSJ9.AQujSTqoBaG-VtLWi6G5A-_iPAgfH3du4U1-XNU0m4Y")]
public void RejectsChangedSignatureAlgorithm(string token)
{
// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
var jwtHandler = new JwtSecurityTokenHandler();
var publicKey = ReadPem("public.pem").PublicKey;
jwtHandler.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(publicKey),
}, out var _);
Assert.Throws<SecurityTokenInvalidSignatureException>(() =>
jwtHandler.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new ECDsaSecurityKey(LoadPublicKey(publicKey)),
}, out var _));
}
[Theory]
[InlineData("eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOiIyOTg2Njg5In0.")]
public void RejectsNoSignature(string token)
{
// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
var jwtHandler = new JwtSecurityTokenHandler();
var publicKey = ReadPem("public.pem").PublicKey;
Assert.Throws<SecurityTokenInvalidSignatureException>(() =>
jwtHandler.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new ECDsaSecurityKey(LoadPublicKey(publicKey)),
}, out var _));
}
private (byte[] PrivateKey, byte[] PublicKey) GenerateKeys()
{
var generator = new ECKeyPairGenerator();
var curve = SecNamedCurves.GetByName("secp256r1");
generator.Init(new ECKeyGenerationParameters(new ECDomainParameters(curve.Curve, curve.G, curve.N), new SecureRandom(new RandomGenerator())));
var pair = generator.GenerateKeyPair();
var privateKey = (ECPrivateKeyParameters) pair.Private;
var publicKey = (ECPublicKeyParameters) pair.Public;
return (privateKey.D.ToByteArrayUnsigned(), publicKey.Q.GetEncoded());
}
private (byte[] PrivateKey, byte[] PublicKey) ReadPem(string pemFile)
{
using (var reader = File.OpenText(pemFile))
{
var pemReader = new PemReader(reader);
object o;
while ((o = pemReader.ReadObject()) != null)
{
if (o is AsymmetricCipherKeyPair pair)
{
var privateKeyFromPair = ((ECPrivateKeyParameters) pair.Private).D.ToByteArrayUnsigned();
var publicKeyFromPair = ((ECPublicKeyParameters) pair.Public).Q.GetEncoded();
return (PrivateKey: privateKeyFromPair, PublicKey: publicKeyFromPair);
}
if (o is ECPrivateKeyParameters privateKey)
{
var curve = SecNamedCurves.GetByName("secp256r1");
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N);
var publicKeyParameters = new ECPublicKeyParameters(domain.G.Multiply(new BigInteger(1, privateKey.D.ToByteArrayUnsigned())), domain);
return (PrivateKey: privateKey.D.ToByteArrayUnsigned(), PublicKey: publicKeyParameters.Q.GetEncoded());
}
if (o is ECPublicKeyParameters publicKey)
{
return (PrivateKey: null, PublicKey: publicKey.Q.GetEncoded());
}
}
throw new InvalidOperationException("Key pair was not found in PEM file");
}
}
private bool Verify(byte[] payload, byte[] signature, byte[] publicKey)
{
var key = new ECDsaSecurityKey(LoadPublicKey(publicKey));
var provider = new AsymmetricSignatureProvider(key, SecurityAlgorithms.EcdsaSha256Signature);
return provider.Verify(payload, signature);
}
private byte[] Sign(byte[] payload, byte[] publicKey, byte[] privateKey)
{
var key = new ECDsaSecurityKey(LoadPrivateKey(privateKey));
var provider = new AsymmetricSignatureProvider(key, SecurityAlgorithms.EcdsaSha256Signature);
return provider.Sign(payload);
}
private static ECDsa LoadPublicKey(byte[] key)
{
return ECDsa.Create(new ECParameters
{
Curve = ECCurve.NamedCurves.nistP256,
Q = new ECPoint
{
X = key.Skip(1).Take(32).ToArray(),
Y = key.Skip(33).ToArray(),
}
});
}
private static ECDsa LoadPrivateKey(byte[] key)
{
var privKeyInt = new BigInteger(1, key);
var parameters = SecNamedCurves.GetByName("secp256r1");
var ecPoint = parameters.G.Multiply(privKeyInt);
var privKeyX = ecPoint.Normalize().XCoord.ToBigInteger().ToByteArrayUnsigned();
var privKeyY = ecPoint.Normalize().YCoord.ToBigInteger().ToByteArrayUnsigned();
return ECDsa.Create(new ECParameters
{
Curve = ECCurve.NamedCurves.nistP256,
D = privKeyInt.ToByteArrayUnsigned(),
Q = new ECPoint
{
X = privKeyX,
Y = privKeyY,
}
});
}
private class RandomGenerator : IRandomGenerator
{
public void NextBytes(byte[] bytes)
{
using (var random = RandomNumberGenerator.Create())
random.GetBytes(bytes);
}
public void AddSeedMaterial(byte[] seed) => throw new NotImplementedException();
public void AddSeedMaterial(long seed) => throw new NotImplementedException();
public void NextBytes(byte[] bytes, int start, int len) => throw new NotImplementedException();
}
}
}