Files
ayanova7/keys/Generator/Backup/KeyRead.cs
2018-06-29 19:47:36 +00:00

297 lines
6.1 KiB
C#

using System;
using System.IO;
using System.Xml;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
namespace GroundZero.KeyCodes
{
/// <summary>
/// AyaNova .net key reader and validator
/// </summary>
public class KeyReader
{
private bool _IsValid;
private string _RegisteredTo;
private int _ScheduleableUsers;
private bool _FeatureWebInterface;
private object _Expires;
private bool _FeatureQuickBooksInterface;
private string _status;
private DateTime _Generated;
private DateTime _InstallableUntil;
private bool _Installable;
private double _SchemaVersion;
public string RegisteredTo
{
get
{
return _RegisteredTo;
}
}
public string Status
{
get
{
return _status;
}
}
public int ScheduleableUsers
{
get
{
return _ScheduleableUsers;
}
}
public bool FeatureWebInterface
{
get
{
return _FeatureWebInterface;
}
}
public bool FeatureQuickBooksInterface
{
get
{
return _FeatureQuickBooksInterface;
}
}
public bool IsValid
{
get
{
return _IsValid;
}
}
public DateTime Generated
{
get
{
return _Generated;
}
}
public object Expires
{
get
{
return _Expires;
}
}
public DateTime InstallableUntil
{
get
{
return _InstallableUntil;
}
}
public bool Installable
{
get
{
return _Installable;
}
}
public double SchemaVersion
{
get
{
return _SchemaVersion;
}
}
public KeyReader(string Key)
{
_IsValid=false;
_FeatureWebInterface=false;
_FeatureQuickBooksInterface=false;
_Installable=false;
//Ensure we have a key and strip out any unnecessary text
int nStart=Key.IndexOf("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>");
int nEnd=Key.IndexOf("</AyaNovaLicenseKey>");
if(nEnd==-1 || nStart==-1 || nEnd<nStart)
{
_IsValid=false;
_status="Error: could not find license XML in text.";
return;
}
Key=Key.Substring(nStart,(nEnd+("</AyaNovaLicenseKey>".Length))-nStart);
//validate key signature then get settings within key
// Get the XML content from the embedded XML public key.
Stream s = null;
string xmlkey = string.Empty;
try
{
// System.Reflection.Assembly ass=System.Reflection.Assembly.GetExecutingAssembly();
// string [] items=ass.GetManifestResourceNames();
// foreach(string ss in items)
// {
// string xy=ss;
// string ab=xy;
//
// }
s = typeof(KeyReader).Assembly.GetManifestResourceStream(
"Generator.pubkey.xml");
// Read-in the XML content.
StreamReader reader = new StreamReader(s);
xmlkey = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
_IsValid=false;
_status="Error: could not import public key. " + ex.Message + "\r\n" + ex.InnerException;
return;
}
// Create an RSA crypto service provider from the embedded
// XML document resource (the public key).
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.FromXmlString(xmlkey);
// Load the signed XML license file.
XmlDocument xmldoc = new XmlDocument();
try
{
xmldoc.LoadXml(Key);
}
catch(Exception ex)
{
_IsValid=false;
_status="Error: exception in LoadXml:\r\n." + ex.Message + "\r\n" + ex.InnerException;
return;
}
// Create the signed XML object.
SignedXml sxml = new SignedXml(xmldoc);
try
{
// Get the XML Signature node and load it into the signed XML object.
XmlNode dsig = xmldoc.GetElementsByTagName("Signature",
SignedXml.XmlDsigNamespaceUrl)[0];
sxml.LoadXml((XmlElement)dsig);
}
catch(Exception ex)
{
_IsValid=false;
_status="Error: no signature found." + ex.Message + "\r\n" + ex.InnerException;
return;
}
// Verify the signature.
if (!sxml.CheckSignature(csp))
{
_IsValid=false;
_status="Error: Signature not valid, key has been tampered with.";
return;
}
_IsValid=true;
try
{
_SchemaVersion=XmlConvert.ToDouble(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/SchemaVersion").InnerText);
_RegisteredTo=xmldoc.SelectSingleNode("/AyaNovaLicenseKey/RegisteredTo").InnerText;
_ScheduleableUsers=XmlConvert.ToInt32(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/TotalScheduleableUsers").InnerText);
_Generated=XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/Created").InnerText);
_InstallableUntil=XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/InstallableUntil").InnerText);
if(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/Expires")!=null)
_Expires=XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/Expires").InnerText);
else
_Expires=null;
_FeatureQuickBooksInterface=XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/FeatureQuickBooksInterface").InnerText);
_FeatureWebInterface=XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/FeatureWebBrowserInterface").InnerText);
if(System.DateTime.Now > _InstallableUntil)
{
_Installable=false;
_status="Error: Installation key has expired.";
return;
}
_Installable=true;
//FUTURE: Here is an example of new items in a hypothetical future schema version...
// if(_SchemaVersion>1.1)
// {
// _FeatureNew=XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/AyaNovaLicenseKey/FeatureNew").InnerText);
// }
//
// if(_SchemaVersion>1.2)
// {
// }
// ETC
}
catch(Exception ex)
{
_IsValid=false;
_status="Error: Signature not valid - exception processing fields:\r\n." + ex.Message + "\r\n" + ex.InnerException;
return;
}
}
}
}