public static Dictionary <string, object> Validate(License licenseKey, RSAParameters rsaParameters) { var keys = ExtractKeys(licenseKey.Keys); var result = new Dictionary <string, object>(); return(result); //using (var ms = new MemoryStream()) //using (var br = new BinaryReader(ms)) //{ // var buffer = keys.Attributes; // ms.Write(buffer, 0, buffer.Length); // ms.Position = 0; // while (ms.Position < buffer.Length) // { // var token = ms.ReadByte(); // var index = token & 0x1F; // object val; // var curr = (ValueType)(token >> TypeBitsToShift); // switch (curr) // { // case ValueType.False: // val = false; // break; // case ValueType.True: // val = true; // break; // case ValueType.Int8: // val = (int)br.ReadByte(); // break; // case ValueType.Int32: // val = br.ReadInt32(); // break; // case ValueType.Date: // val = FromDosDate(br.ReadUInt16()); // break; // case ValueType.String: // var valLength = (int)br.ReadByte(); // val = Encoding.UTF8.GetString(br.ReadBytes(valLength)); // break; // default: // throw new ArgumentOutOfRangeException(); // } // if (index >= Terms.Length) // continue; // new field, just skip // result[Terms[index]] = val; // } // var attributesLen = ms.Position; // ms.SetLength(attributesLen); // using (var binaryWriter = new BinaryWriter(ms)) // using (var rsa = RSA.Create()) // { // binaryWriter.Write(licenseKey.Id.ToByteArray()); // binaryWriter.Write(licenseKey.Name); // rsa.ImportParameters(rsaParameters); // using (var sha1 = SHA1.Create()) // { // ms.Position = 0; // var hash = sha1.ComputeHash(ms); // if (rsa.VerifyHash(hash, keys.Signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1) == false) // throw new InvalidDataException("Could not validate signature on license"); // } // } //return result; //} }
public static Dictionary <string, object> Validate(License licenseKey, RSAParameters rsaParameters) { var keys = ExtractKeys(licenseKey.Keys); var result = new Dictionary <string, object>(); using (var ms = new MemoryStream()) using (var br = new BinaryReader(ms)) { var buffer = keys.Attributes; ms.Write(buffer, 0, buffer.Length); ms.Position = 0; while (ms.Position < buffer.Length) { var licensePropertyExists = TryGetTermIndexAndType(br, out string licenseProperty, out ValueType type); object val = type switch { ValueType.False => false, ValueType.True => true, ValueType.Int8 => (int)br.ReadByte(), ValueType.Int32 => br.ReadInt32(), ValueType.Date => FromDosDate(br.ReadUInt16()), ValueType.String => Encoding.UTF8.GetString(br.ReadBytes((int)br.ReadByte())), _ => throw new ArgumentOutOfRangeException(), }; if (licensePropertyExists == false) { //new license property continue; } result[licenseProperty] = val; } var attributesLen = ms.Position; ms.SetLength(attributesLen); using (var binaryWriter = new BinaryWriter(ms)) using (var rsa = RSA.Create()) { binaryWriter.Write(licenseKey.Id.ToByteArray()); binaryWriter.Write(licenseKey.Name); rsa.ImportParameters(rsaParameters); using (var sha1 = SHA1.Create()) { ms.Position = 0; var hash = sha1.ComputeHash(ms); if (rsa.VerifyHash(hash, keys.Signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1) == false) { throw new InvalidDataException("Could not validate signature on license"); } } } return(result); } }
private static bool ValidateLicense(License oldLicense, RSAParameters rsaParameters, License newLicense) { try { LicenseValidator.Validate(oldLicense, rsaParameters); } catch (Exception e) { if (Logger.IsInfoEnabled) { Logger.Info($"Failed to validate license: `{oldLicense}`", e); } return(true); } if (oldLicense.Id != newLicense.Id) { if (Logger.IsInfoEnabled) { Logger.Info($"Can't update license because the new license ID is: {newLicense.Id} " + $"while old license ID is: {oldLicense.Id}"); } return(false); } return(true); }
public static Dictionary <string, object> Validate(License licenseKey, RSAParameters rsAParameters) { if (licenseKey.Keys.Count != 2) { throw new InvalidDataException("Wrong number of keys"); } var result = new Dictionary <string, object>(); using (var ms = new MemoryStream()) using (var br = new BinaryReader(ms)) { var buffer = GetBytesFromBase64String(licenseKey.Keys.First()); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; while (ms.Position < buffer.Length) { var token = ms.ReadByte(); var index = token & 0x1F; object val; var curr = (ValueType)(token >> TypeBitsToShift); switch (curr) { case ValueType.False: val = false; break; case ValueType.True: val = true; break; case ValueType.Int: val = (int)br.ReadByte(); break; case ValueType.Date: val = FromDosDate(br.ReadUInt16()); break; case ValueType.String: var valLength = (int)br.ReadByte(); val = Encoding.UTF8.GetString(br.ReadBytes(valLength)); break; default: throw new ArgumentOutOfRangeException(); } if (index >= Terms.Length) { continue; // new field, just skip } result[Terms[index]] = val; } var attributesLen = ms.Position; ms.SetLength(attributesLen); new BinaryWriter(ms).Write(licenseKey.Id.ToByteArray()); new BinaryWriter(ms).Write(licenseKey.Name); using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(rsAParameters); using (var sha1 = SHA1.Create()) { ms.Position = 0; var hash = sha1.ComputeHash(ms); var signature = licenseKey.Keys.Last(); if (rsa.VerifyHash(hash, GetBytesFromBase64String(signature), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1) == false) { throw new InvalidDataException("Could not validate signature on license"); } } } return(result); } }