This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
License.cs
298 lines (266 loc) · 10.9 KB
/
License.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
//
// Copyright (c) Vincent LE TOUX for Ping Castle. All rights reserved.
// https://www.pingcastle.com
//
// Licensed under the Non-Profit OSL. See LICENSE file in the project root for full license information.
//
using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
namespace PingCastleCloud
{
public interface IPingCastleLicenseInfo
{
string GetSerialNumber();
}
public class PingCastleLicenseProvider : LicenseProvider
{
#region Public Methods
/// <summary>
/// Gets a license for an instance or type of component.
/// </summary>
/// <param name="context">A <see cref="LicenseContext"/> that specifies where you can use the licensed object.</param>
/// <param name="type">A <see cref="System.Type"/> that represents the component requesting the license.</param>
/// <param name="instance">An object that is requesting the license.</param>
/// <param name="allowExceptions">true if a <see cref="LicenseException"/> should be thrown when the component cannot be granted a license; otherwise, false.</param>
/// <returns>A valid <see cref="License"/>.</returns>
public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
{
IPingCastleLicenseInfo licenseInfo = (IPingCastleLicenseInfo)instance;
return new PingCastleLicense(licenseInfo.GetSerialNumber());
}
#endregion
}
internal class PingCastleLicenseSettings : ConfigurationSection
{
private static PingCastleLicenseSettings settings;
public static PingCastleLicenseSettings Settings
{
get
{
if (settings == null)
settings = ConfigurationManager.GetSection("LicenseSettings") as PingCastleLicenseSettings;
return settings;
}
}
[ConfigurationProperty("license", IsRequired = false)]
public string License
{
get { return (string)this["license"]; }
set { this["license"] = value; }
}
}
public class PingCastleLicense : License, IDisposable
{
private bool _disposed = false;
private string _licKey = null;
public PingCastleLicense(string license)
: this(license, true)
{
}
public PingCastleLicense(string license, bool DoAKeyCheck)
{
if (String.IsNullOrEmpty(license))
throw new PingCastleCloudException("No PingCastle license has been provided");
_licKey = license;
Trace.WriteLine("License: " + _licKey);
if (!VerifyKey())
{
throw new PingCastleCloudException("The PingCastle license is not valid");
}
}
#region Properties
public DateTime EndTime { get; set; }
public string DomainLimitation { get; set; }
public string CustomerNotice { get; set; }
public string Edition { get; set; }
public int? DomainNumberLimit { get; set; }
/// <summary>
/// Gets the license key granted to this component.
/// </summary>
public override string LicenseKey
{
get { return _licKey; }
}
#endregion
private bool VerifyKey()
{
#if DEBUG
if (_licKey.Equals("debug", StringComparison.InvariantCultureIgnoreCase))
{
EndTime = DateTime.MaxValue;
DomainLimitation = null;
CustomerNotice = "debug version";
return true;
}
#endif
try
{
Trace.WriteLine("starting the license analysis");
Trace.WriteLine("License info uncompressed");
if (_licKey != null && _licKey.StartsWith("PC2"))
{
VerifyLicenseV2();
}
else
{
VerifyLicenseV1();
}
return true;
}
catch (Exception ex)
{
Trace.Write("License: exception " + ex.Message);
return false;
}
}
private void VerifyLicenseV2()
{
byte[] b = Convert.FromBase64String(_licKey.Substring(3));
using (MemoryStream ms = new MemoryStream(b))
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
{
using (var ms2 = new MemoryStream())
{
while (true)
{
int infoType = readint(gs);
int infoLength = readint(gs);
byte[] data = new byte[infoLength];
gs.Read(data, 0, data.Length);
Trace.WriteLine("data Type = " + infoType);
switch (infoType)
{
case 0:
Trace.WriteLine("Signature");
VerifySignature(data, ms2.ToArray());
if (Edition == "Pro" && DomainNumberLimit == null)
DomainNumberLimit = 1;
return;
case 1:
Trace.WriteLine("EndTime");
EndTime = DateTime.FromFileTimeUtc(BitConverter.ToInt64(data, 0));
break;
case 2:
Trace.WriteLine("DomainLimitation");
DomainLimitation = Encoding.Unicode.GetString(data);
break;
case 3:
Trace.WriteLine("CustomerNotice");
CustomerNotice = Encoding.Unicode.GetString(data);
break;
case 4:
Trace.WriteLine("Edition");
Edition = Encoding.Unicode.GetString(data);
break;
case 5:
DomainNumberLimit = BitConverter.ToInt32(data, 0);
break;
}
ms2.Write(BitConverter.GetBytes(infoType), 0, 4);
ms2.Write(BitConverter.GetBytes(data.Length), 0, 4);
ms2.Write(data, 0, data.Length);
}
}
}
}
}
private void VerifyLicenseV1()
{
byte[] b = Convert.FromBase64String(_licKey);
MemoryStream ms = new MemoryStream();
ms.Write(b, 0, b.Length);
ms.Position = 0;
byte[] date = new byte[readint(ms)];
byte[] limitation = new byte[readint(ms)];
byte[] notice = new byte[readint(ms)];
byte[] signature = new byte[readint(ms)];
Trace.WriteLine("reading date");
ms.Read(date, 0, date.Length);
Trace.WriteLine("reading limitation");
ms.Read(limitation, 0, limitation.Length);
Trace.WriteLine("reading notice");
ms.Read(notice, 0, notice.Length);
Trace.WriteLine("reading signature");
ms.Read(signature, 0, signature.Length);
Trace.WriteLine("reading done");
byte[] bytes = new byte[date.Length + limitation.Length + notice.Length];
Array.Copy(date, 0, bytes, 0, date.Length);
Array.Copy(limitation, 0, bytes, date.Length, limitation.Length);
Array.Copy(notice, 0, bytes, limitation.Length + date.Length, notice.Length);
VerifySignature(signature, bytes);
EndTime = DateTime.FromFileTimeUtc(BitConverter.ToInt64(date, 0));
Trace.WriteLine("Endtime=" + EndTime);
DomainLimitation = Encoding.Unicode.GetString(limitation);
Trace.WriteLine("DomainLimitation=" + DomainLimitation);
CustomerNotice = Encoding.Unicode.GetString(notice);
Trace.WriteLine("CustomerNotice=" + CustomerNotice);
Trace.WriteLine("license verified");
}
private void VerifySignature(byte[] signature, byte[] dataToVerify)
{
Trace.WriteLine("hashing license info");
using (SHA1 hashstring = SHA1.Create())
{
byte[] hash = hashstring.ComputeHash(dataToVerify);
Trace.WriteLine("hashing done");
Trace.WriteLine("loading rsa key");
using (RSACryptoServiceProvider RSA = LoadRSAKey())
{
Trace.WriteLine("loading rsa key");
Trace.WriteLine("verifying the signature");
if (!RSA.VerifyHash(hash, "1.3.14.3.2.26", signature))
{
throw new Exception("Invalid signature");
}
Trace.WriteLine("signature ok");
}
}
}
private RSACryptoServiceProvider LoadRSAKey()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters parameters = new RSAParameters();
parameters.Modulus = Convert.FromBase64String("wNtlwFv+zo0lrShHnSi5VLT6Sbfx3ZXhtefSJfYs3YjWyPHv3ihLjXlBjMlGI5ziXrjcriNNZ5zn2P2qvv3VdX02zsIuGuAYZi0c4WBhiqtKgTo7USxsAaGxpqiWTkW3NQylw27p3jqICO7cbLXsr3aEZJJUgqkNay/l4S3pYIs=");
parameters.Exponent = Convert.FromBase64String("AQAB");
RSA.ImportParameters(parameters);
return RSA;
}
int readint(Stream stream)
{
byte[] temp = new byte[4];
stream.Read(temp, 0, 4);
int size = BitConverter.ToInt32(temp, 0);
return size;
}
/// <summary>
/// Disposes this object.
/// </summary>
public sealed override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes this object.
/// </summary>
/// <param name="disposing">true if the object is disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (!_disposed)
{
//Custom disposing here.
}
_disposed = true;
}
}
}
}