/////////////////////////////////////////////////////////// // Global.cs // Implementation of Class GlobalEx // CSLA type: Editable Root // Created on: 09-Jun-2005 // Object design: John // Coded: John 09-Jun-2005 /////////////////////////////////////////////////////////// //case 3378 //#define FIPS_MODE using System; using System.Data; using CSLA.Data; using CSLA; using System.Threading; using CSLA.Security; using System.ComponentModel; using System.Text; using System.IO; using System.Xml; using System.Security; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using GZTW.Data; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; //JSON KEY using Newtonsoft.Json; using Org.BouncyCastle.Security; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.OpenSsl; namespace GZTW.AyaNova.BLL { /// /// Global settings ex /// [Serializable, EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal class GlobalEx : BusinessBase {// Create a logger for use in this class //private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region Attributes private byte[] mbGlobalData; //Keydata private bool _Valid; private string _RegisteredTo; private int _ScheduleableUsers; //case 3187 private string _Source; //case 2094, now a plugin //private bool _FeatureWebInterface; private object _Expires; //case 2094 now a plugin ////Case 762 //private bool _FeatureMBIInterface; //Case 999 private bool _RequestedTrial; private bool _BootKeyIsRequestedTrial; //Case 508 private string _DigestValue; private string _LicenseValidationStatus; private DateTime _Generated; private DateTime _InstallableUntil; private bool _Installable; private double _SchemaVersion; private bool _TrialKey; //Housekeeping info private int mDBSchema = 0; //trial expiry date and evaluation date advance reference date AGlobalExData _AGlobalExData = null; //case 1053 private DataTable dtPlugins; //case 1172 private bool _Lite; //case 2094 private bool _SubscriptionLicense; private bool _Lockout; private DateTime _LockoutDate; #if(FIPS_MODE) //case 3378 FIPS private bool _FIPS_READY = false; #endif #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private GlobalEx() { InValidate(); //_AGlobalExData=new AGlobalExData(); } #endregion #region Business properties [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal AGlobalExData TData { //trial version first boot data and expiry date for trial get { return _AGlobalExData; } set { //if(!User.IsAdmin) return; _AGlobalExData = value; MarkDirty(); } } /// /// Get Database schema version /// /// /// [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] public int DBSchemaVersion { get { return mDBSchema; } } /// /// Get/Set global data /// [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal string GlobalData { set { if (!User.IsAdmin && !AyaBizUtils.Lite) return; UTF8Encoding utf8 = new UTF8Encoding(); mbGlobalData = utf8.GetBytes(value); Validate(true); BrokenRules.Assert("GlobalDataValid", "Global data not valid", "GlobalData", !_Valid); MarkDirty(); } } /// /// View global data in human readable format /// [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal string ViewGlobalData { get { if (this._Valid == false) return "Invalid data"; //_Expires can be null!!!! DateTime dt_Expires = DateTime.MinValue; if (_Expires != null) { dt_Expires = (DateTime)_Expires; } System.Text.StringBuilder sb = new StringBuilder(); sb.Append("Key issued: " + this._Generated.ToString() + "\r\n"); if (_SubscriptionLicense && !_TrialKey) { sb.Append("Support and updates until: " + dt_Expires.ToLongDateString() + "\r\n"); } else { if (_Expires != null) { //check if downloaded trial if (dt_Expires.Year == 1968) { sb.Append("Trial key Expires: " + _AGlobalExData.EvalExpire.ToLongDateString() + "\r\n"); } else { sb.Append("Key Expires: " + dt_Expires.ToLongDateString() + "\r\n"); } } } //Lockout key? if (_Lockout) { if (_LockoutDate < DateTime.Now) sb.AppendLine("\r\n** TEMPORARY KEY EXPIRED: " + _LockoutDate.ToLongDateString() + " **\r\n"); else sb.AppendLine("Temporary key, will expire: " + _LockoutDate.ToLongDateString() + "\r\n"); } sb.Append("Registered to: " + _RegisteredTo + "\r\n"); //case 3187 sb.Append("Source: " + _Source + "\r\n"); sb.Append("Version: " + _SchemaVersion + ".X\r\n"); if (!_SubscriptionLicense) sb.Append("Scheduleable resources: " + this._ScheduleableUsers.ToString() + "\r\n"); else { sb.AppendLine(string.Format("Scheduleable resources: Up to {0}", this._ScheduleableUsers)); //case 3551 //switch (this._ScheduleableUsers) //{ // case 1: // sb.AppendLine("1"); // break; // case 5: // sb.AppendLine("Up to 5"); // break; // case 10: // sb.AppendLine("Up to 10"); // break; // case 20: // sb.AppendLine("Up to 20"); // break; // case 50: // sb.AppendLine("Up to 50"); // break; // case 999: // sb.AppendLine("Up to 999"); // break; //} } //case 1172 sb.Append("Lite: " + _Lite.ToString() + "\r\n"); //case 999 sb.Append("Requested trial: " + this._RequestedTrial.ToString() + "\r\n"); //case 2094 These are now plugins: //Case 762 //sb.Append("Option - Mobile browser interface: " + this._FeatureMBIInterface.ToString() + "\r\n"); //sb.Append("Option - Web browser interface: " + this._FeatureWebInterface.ToString() + "\r\n"); if (dtPlugins.Rows.Count > 0) { sb.Append("\r\n"); sb.Append("Plugins:\r\n"); foreach (DataRow dr in dtPlugins.Rows) { sb.Append("\t"); sb.Append(dr["Plugin"].ToString()); sb.Append(" "); //case 2094 if (_SubscriptionLicense) { //check if downloaded trial if (dt_Expires.Year == 1968) { sb.Append("Trial subscription expires: "); sb.Append(_AGlobalExData.EvalExpire.ToLongDateString()); } else { sb.Append("Support and updates until: "); //case 3113 DateTime dtx = (DateTime)dr["SubscriptionExpires"]; // DateTime dtx = DateTime.Parse(dr["SubscriptionExpires"].ToString()); sb.Append(dtx.ToLongDateString()); } } else { sb.Append(dr["Version"].ToString()); } sb.Append("\r\n"); } } return sb.ToString(); } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal string RegisteredTo { get { return _RegisteredTo; } } //[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] //internal string Status //{ // get // { // return _LicenseValidationStatus; // } //} [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal int ScheduleableUsers { get { return _ScheduleableUsers; } } //case 2094 - now a plugin in license code //[EditorBrowsable(EditorBrowsableState.Never),Browsable(false)] //internal bool FeatureWebInterface //{ // get // { // return _FeatureWebInterface; // } //} [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool TrialExpired { get { //Is the license for an older version of AyaNova? //SET VALID LICENSE VERSION HERE //case 2094 //if (_SchemaVersion < 7)//case 999 // return true; //TRIAL KEY if (_TrialKey) { if (System.DateTime.Now > _AGlobalExData.EvalExpire) return true; } //if it's not a trial key then it can never be true right? return false; ////NON TRIAL KEY PATH ////null expiry never expires //if(_Expires==null) return false; //if(System.DateTime.Now>(System.DateTime)_Expires) // return true; //return false; } } //case 2094 //If the license contains a lockout date and it's passed then this returns true //used for web requested trial and licensed trial to differentiate //subscription expiry date [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool LockedOut { get { if (_Lockout && System.DateTime.Now > _LockoutDate) return true; return false; } } /// /// case 2094 /// Returns true only if it's a subscription license and it has expired /// In all other cases returns true (such as it's an expired trial) /// [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool SubscriptionExpired { get { //If it's a trial then it's not an active subscription so it can't have expired if (_TrialKey) return false; //null expiry always expires, it's a bug or something weird if (_Expires == null) return true; if (System.DateTime.Now > (System.DateTime)_Expires) return true; return false; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool WillExpire { get { if (_TrialKey || _Lockout) return true; //null expiry never expires if (_Expires == null) return false; return true; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal DateTime ExpiryDate { get { if (_TrialKey) return _AGlobalExData.EvalExpire; if (_Expires == null) return System.DateTime.MaxValue; else return (DateTime)_Expires; } } //case 2094 now a plugin in license code ////Case 762 //[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] //internal bool FeatureMBIInterface //{ // get // { // return _FeatureMBIInterface; // } //} //Case 999 [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool RequestedTrial { get { return _RequestedTrial; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool Valid { get { return _Valid; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal DateTime Generated { get { return _Generated; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal DateTime InstallableUntil { get { return _InstallableUntil; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool Installable { get { return _Installable; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal double SchemaVersion { get { return _SchemaVersion; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool TrialKey { get { return _TrialKey; } } //Case 508 [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal string DigestValue { get { return _DigestValue; } } /// /// Get plugins /// /// /// [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal DataTable Plugins { get { if (dtPlugins == null) { dtPlugins = new DataTable(); dtPlugins.Columns.Add("Plugin"); dtPlugins.Columns.Add("Version"); //case 2094 dtPlugins.Columns.Add("SubscriptionExpires", typeof(DateTime)); } return dtPlugins; } } //case 1172 [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool Lite { get { return _Lite; } } //case 2094 [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool SubscriptionLicense { get { return _SubscriptionLicense; } } //case 2094 [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal bool Lockout { get { return _Lockout; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal DateTime LockoutDate { get { return _LockoutDate; } } #endregion #region System.Object overrides public override string ToString() { return "GlobalEx"; } /// /// public override bool Equals(Object obj) { return true; } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] public override int GetHashCode() { return ("GlobalEx").GetHashCode(); } #endregion #region Static methods [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] public static GlobalEx GetItem() { GlobalEx a = (GlobalEx)DataPortal.Fetch(new Criteria()); return a; //return (GlobalEx)DataPortal.Fetch(new Criteria()); } #endregion #region DAL Data access /// /// protected override void DataPortal_Fetch(object Criteria) { this._LicenseValidationStatus = ""; //Fetching license key //sets all settings to safe mode before fetch and validate InValidate(); //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 2"; Criteria crit = (Criteria)Criteria; #if(FIPS_MODE) //case 3378 bool bMustUpdate = false;//if it has to decrypt the key and ex data then it should save it back immediately here #endif //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 3"; using (SafeDataReader dataReader = DBUtil.GetReaderFromSQLString("SELECT * FROM aGlobalEx")) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 4"; if (dataReader.Read()) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 5"; try { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 6"; //schema version mDBSchema = dataReader.GetInt32("aDBSchema"); //Global data int nTempsize = dataReader.GetInt32("aGlobalDataSize"); //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 7"; if (nTempsize > 0) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 8"; byte[] temp = new Byte[nTempsize]; dataReader.GetBytes("aGlobalData", 0, temp, 0, nTempsize); #if(FIPS_MODE) //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 9"; //case 3378 string sKey = new UTF8Encoding().GetString(temp); if (sKey.Contains("LicenseKey")) { //already stored decrypted //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 9-PathA"; _FIPS_READY = true;//flag for the globalEx block below which is not as easily identifiable as decrypted mbGlobalData = temp; } else { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 9-PathB"; //trigger a save after all is decrypted bMustUpdate = true; /*shaM.ComputeHash(enc.GetBytes("Who is John Galt?"))*/ byte[] bkey = new byte[32] { 0xAE, 0x6E, 0x53, 0x24, 0xC6, 0x53, 0x1D, 0x88, 0x90, 0x12, 0x80, 0xCC, 0x19, 0x88, 0x71, 0x38, 0xA3, 0xBF, 0x63, 0x64, 0xCB, 0x0C, 0xED, 0x50, 0x47, 0xCA, 0x96, 0x8C, 0x27, 0xCD, 0xD2, 0x64 }; /*shaM.ComputeHash(enc.GetBytes("License IV"))*/ byte[] biv = new byte[32] { 0xB7, 0xF8, 0x10, 0xED, 0x51, 0x28, 0x44, 0x2C, 0x77, 0x3D, 0xBB, 0x6B, 0x0D, 0x89, 0x3A, 0x1E, 0xA1, 0xA3, 0x4E, 0xAA, 0x0A, 0x74, 0xD7, 0x35, 0xE0, 0x52, 0xC2, 0xFF, 0x38, 0x72, 0xB3, 0xF2 }; CipherWrapper c = new CipherWrapper(bkey); mbGlobalData = c.DecryptMessage(temp, biv); } #else //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 9-PathC"; /*shaM.ComputeHash(enc.GetBytes("Who is John Galt?"))*/ byte[] bkey = new byte[32] { 0xAE, 0x6E, 0x53, 0x24, 0xC6, 0x53, 0x1D, 0x88, 0x90, 0x12, 0x80, 0xCC, 0x19, 0x88, 0x71, 0x38, 0xA3, 0xBF, 0x63, 0x64, 0xCB, 0x0C, 0xED, 0x50, 0x47, 0xCA, 0x96, 0x8C, 0x27, 0xCD, 0xD2, 0x64 }; /*shaM.ComputeHash(enc.GetBytes("License IV"))*/ byte[] biv = new byte[32] { 0xB7, 0xF8, 0x10, 0xED, 0x51, 0x28, 0x44, 0x2C, 0x77, 0x3D, 0xBB, 0x6B, 0x0D, 0x89, 0x3A, 0x1E, 0xA1, 0xA3, 0x4E, 0xAA, 0x0A, 0x74, 0xD7, 0x35, 0xE0, 0x52, 0xC2, 0xFF, 0x38, 0x72, 0xB3, 0xF2 }; CipherWrapper c = new CipherWrapper(bkey); mbGlobalData = c.DecryptMessage(temp, biv); #endif } else throw new InvalidOperationException(); //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 10"; //Global data EX nTempsize = dataReader.GetInt32("aGlobalDataExSize"); if (nTempsize > 0) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 11"; //Get the data... byte[] temp = new Byte[nTempsize]; dataReader.GetBytes("aGlobalDataEx", 0, temp, 0, nTempsize); //Into a memory stream... MemoryStream mstream = null; //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 12"; #if(FIPS_MODE) if (!_FIPS_READY) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 12-A"; //Decrypt the data... UTF8Encoding enc = new UTF8Encoding(); //SHA256 shaM = new SHA256Managed(); //enc.GetBytes("The dog barks at midnight.") byte[] bkey = new byte[32] { 0xC2, 0x18, 0x47, 0x78, 0xF8, 0x4B, 0x1A, 0x56, 0xE9, 0x9A, 0x87, 0x82, 0x82, 0x8E, 0x5F, 0x6B, 0x7F, 0x94, 0x5E, 0xB1, 0x1E, 0xA0, 0x57, 0x1A, 0x23, 0xFE, 0x13, 0x91, 0xCD, 0xE8, 0xC5, 0x90 }; //enc.GetBytes("EX IV") byte[] biv = new byte[32] { 0x9F, 0xDF, 0x65, 0x02, 0xA8, 0x48, 0x79, 0x2D, 0xC2, 0xD9, 0xCB, 0xCB, 0x9F, 0x0C, 0x8B, 0x55, 0x3F, 0x2E, 0x92, 0x3C, 0x48, 0xAD, 0xDC, 0xF4, 0x53, 0x03, 0x82, 0x29, 0x80, 0x28, 0x45, 0x1D }; CipherWrapper c = new CipherWrapper(bkey); mstream = new MemoryStream(c.DecryptMessage(temp, biv)); } else { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 12-B"; //already fips ready (decrypted in db) so just process it mstream = new MemoryStream(temp); } #else //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 12-C"; //Decrypt the data... UTF8Encoding enc = new UTF8Encoding(); //SHA256 shaM = new SHA256Managed(); //enc.GetBytes("The dog barks at midnight.") byte[] bkey = new byte[32] { 0xC2, 0x18, 0x47, 0x78, 0xF8, 0x4B, 0x1A, 0x56, 0xE9, 0x9A, 0x87, 0x82, 0x82, 0x8E, 0x5F, 0x6B, 0x7F, 0x94, 0x5E, 0xB1, 0x1E, 0xA0, 0x57, 0x1A, 0x23, 0xFE, 0x13, 0x91, 0xCD, 0xE8, 0xC5, 0x90 }; //enc.GetBytes("EX IV") byte[] biv = new byte[32] { 0x9F, 0xDF, 0x65, 0x02, 0xA8, 0x48, 0x79, 0x2D, 0xC2, 0xD9, 0xCB, 0xCB, 0x9F, 0x0C, 0x8B, 0x55, 0x3F, 0x2E, 0x92, 0x3C, 0x48, 0xAD, 0xDC, 0xF4, 0x53, 0x03, 0x82, 0x29, 0x80, 0x28, 0x45, 0x1D }; CipherWrapper c = new CipherWrapper(bkey); mstream = new MemoryStream(c.DecryptMessage(temp, biv)); #endif //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 13"; //De-serialize it into the AGlobalExData object... BinaryFormatter bformatter = new BinaryFormatter(); bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; try { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 13-A"; _AGlobalExData = (AGlobalExData)bformatter.Deserialize(mstream); } catch { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 13-B"; _AGlobalExData = new AGlobalExData(); _AGlobalExData.EvalExpire = System.DateTime.Now; _AGlobalExData.EvalAdvanceReference = System.DateTime.Now; } } else throw new InvalidOperationException(); //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 14"; } catch (Exception ex) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 15"; //this will be caught if the fields requested above don't exist //or there is *any* problem dealing with them //which generally will be that the database is a pre-release one they //are trying to use or they have screwed with the data somehow #if(DEBUG) //string s=ex.Message; #else throw new System.ApplicationException( "\r\n************************************************\r\n" + "Database is missing startup information.\r\n " + "It is either damaged, tamped with or not a valid AyaNova database.\r\n" + "(Note that a pre-release database can not be used with AyaNova)\r\n" + "\r\n************************************************\r\n", ex ); #endif } finally { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 16"; dataReader.Close(); } } } //END new database code MarkOld(); try { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 17";//this is the last logged line before 18 and then finally 18a Validate(false);//this must be throwing an exception //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 17-A";//this doesn't log if (_Valid == false) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 17-B"; throw new InvalidOperationException(); } } catch(Exception ex) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 18"; _Valid = false; #if(FIPS_MODE) //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 18-A"; _LicenseValidationStatus += "\r\nValidate Exception:\r\n" + ex.Message; _LicenseValidationStatus += "\r\nValidate Exception stack:\r\n" + ex.StackTrace; throw new InvalidOperationException( "**** A VALID AYANOVA LICENSE WAS NOT FOUND ***\r\n\r\n " + "Diagnostic info:\r\n\r\n"+_LicenseValidationStatus); #else //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 18-B"; //_LicenseValidationStatus throw new InvalidOperationException( "**** A VALID AYANOVA LICENSE WAS NOT FOUND ***\r\n\r\n " + "The license key has been tampered with or damaged.\r\n\r\n" + "** License violations are logged for the protection of the licensee and licensor **\r\n\r\n" + "Restore the database from a backup or contact AyaNova technical\r\n" + "support for assistance."); #endif } #if(FIPS_MODE) //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 19"; //case 3378 if (bMustUpdate) { //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 20"; DataPortal_Update(); } #endif //_LicenseValidationStatus += "\r\nDataPortal_Fetch: 21-FIN"; } /// /// Called by DataPortal to delete/add/update data into the database /// protected override void DataPortal_Update() { if (!_Valid) throw new InvalidOperationException("DataPortal_Update, not validated can't update, see log for details "); #if(FIPS_MODE) //license key byte[] bencrypted = mbGlobalData; //globalEx (trial) data //Serialize to a memory stream: MemoryStream ms = new MemoryStream(); BinaryFormatter b = new BinaryFormatter(); b.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; b.Serialize(ms, _AGlobalExData); byte[] beencrypted = ms.GetBuffer(); #else //this is merely for obfuscation while the license is resident in the database //it's assumed that this assembly will always be obfuscated and all strings //encrypted. Even if the license was plain text it still //contains a signature that disallows editing, this is just //to prevent casual users from bothering to try messing with it in the first place //by seeing it when opening the db in a db admin tool /*shaM.ComputeHash(enc.GetBytes("Who is John Galt?"))*/ byte[] bkey = new byte[32] { 0xAE, 0x6E, 0x53, 0x24, 0xC6, 0x53, 0x1D, 0x88, 0x90, 0x12, 0x80, 0xCC, 0x19, 0x88, 0x71, 0x38, 0xA3, 0xBF, 0x63, 0x64, 0xCB, 0x0C, 0xED, 0x50, 0x47, 0xCA, 0x96, 0x8C, 0x27, 0xCD, 0xD2, 0x64 }; /*shaM.ComputeHash(enc.GetBytes("License IV"))*/ byte[] biv = new byte[32] { 0xB7, 0xF8, 0x10, 0xED, 0x51, 0x28, 0x44, 0x2C, 0x77, 0x3D, 0xBB, 0x6B, 0x0D, 0x89, 0x3A, 0x1E, 0xA1, 0xA3, 0x4E, 0xAA, 0x0A, 0x74, 0xD7, 0x35, 0xE0, 0x52, 0xC2, 0xFF, 0x38, 0x72, 0xB3, 0xF2 }; CipherWrapper c = new CipherWrapper(bkey); byte[] bencrypted = c.EncryptMessage(mbGlobalData, biv); //Serialize to a memory stream: MemoryStream ms = new MemoryStream(); BinaryFormatter b = new BinaryFormatter(); b.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; b.Serialize(ms, _AGlobalExData); //Encrypt //enc.GetBytes("The dog barks at midnight.") byte[] bekey = new byte[32] { 0xC2, 0x18, 0x47, 0x78, 0xF8, 0x4B, 0x1A, 0x56, 0xE9, 0x9A, 0x87, 0x82, 0x82, 0x8E, 0x5F, 0x6B, 0x7F, 0x94, 0x5E, 0xB1, 0x1E, 0xA0, 0x57, 0x1A, 0x23, 0xFE, 0x13, 0x91, 0xCD, 0xE8, 0xC5, 0x90 }; //enc.GetBytes("EX IV") byte[] beiv = new byte[32] { 0x9F, 0xDF, 0x65, 0x02, 0xA8, 0x48, 0x79, 0x2D, 0xC2, 0xD9, 0xCB, 0xCB, 0x9F, 0x0C, 0x8B, 0x55, 0x3F, 0x2E, 0x92, 0x3C, 0x48, 0xAD, 0xDC, 0xF4, 0x53, 0x03, 0x82, 0x29, 0x80, 0x28, 0x45, 0x1D }; CipherWrapper ce = new CipherWrapper(bekey); byte[] beencrypted = ce.EncryptMessage(ms.GetBuffer(), beiv); #endif DBCommandWrapper cm = DBUtil.GetCommandFromSQL( "UPDATE aGlobalEx " + "SET aGlobalData=@GlobalData, " + "aGlobalDataSize=@GlobalDataSize, " + "aGlobalDataEx=@GlobalDataEx, " + "aGlobalDataExSize=@GlobalDataExSize"); //globaldata cm.AddInParameter("@GlobalData", DbType.Object, bencrypted); cm.AddInParameter("@GlobalDataSize", DbType.Int32, bencrypted.Length); //globaldata ex cm.AddInParameter("@GlobalDataEx", DbType.Object, beencrypted); cm.AddInParameter("@GlobalDataExSize", DbType.Int32, beencrypted.Length); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cm, transaction); // Commit the transaction transaction.Commit(); MarkOld();//db is now synched with object } catch { // Rollback transaction transaction.Rollback(); throw; } connection.Close(); } } #endregion #region Override IsValid / IsDirty etc etc //Override base class version if there are child objects [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] public override bool IsValid { get { return base.IsValid; } } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] public override bool IsDirty { get { return base.IsDirty; } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { //public Guid ID; public Criteria() { } } #endregion #region validation //clear settings set back to invalid state //in preparation for validation private void InValidate() { _Valid = false; this._TrialKey = true; this._RegisteredTo = ""; //Hex encoded string "UNKNOWN" this._Source = "554E4B4E4F574E"; this._ScheduleableUsers = 0; this._SchemaVersion = 0; this._Expires = System.DateTime.Now; //case 999 _RequestedTrial = false; //case 2094 now a plugin //_FeatureWebInterface=false; //case 2094 this._SubscriptionLicense = false;//default for old license processing _Lockout = false; _LockoutDate = DateTime.MaxValue; Plugins.Rows.Clear(); _Installable = false; this._InstallableUntil = System.DateTime.Today.AddDays(-10); this._Generated = System.DateTime.Today.AddDays(-10); } private void Validate(bool CheckInstallableUntilDate) { //_LicenseValidationStatus += "\r\nValidate: 1"; InValidate(); UTF8Encoding utf8 = new UTF8Encoding(); string Key = utf8.GetString(mbGlobalData); // throw new System.NotSupportedException("TEST"); //case 1172 string sKeyType = "AyaNovaLiteLicenseKey"; _Lite = true; if (Key.Contains("AyaNovaLicenseKey")) { sKeyType = "AyaNovaLicenseKey"; _Lite = false; } bool containsXML = Key.Contains(""); bool containsJSON = Key.Contains("[KEY"); //_LicenseValidationStatus += "\r\nValidate: 2"; if (!containsXML && !containsJSON) { _Valid = false; _LicenseValidationStatus = "Error: could not find license key in text provided"; return; } //_LicenseValidationStatus += "\r\nValidate: 3"; if (containsXML) { #region PARSE XML KEY //Ensure we have a key and strip out any unnecessary text int nStart = Key.IndexOf(""); int nEnd = Key.IndexOf(""); if (nEnd == -1 || nStart == -1 || nEnd < nStart) { _Valid = false; // log.Error("Internal error # 1000");//Error: could not find license XML in text."; return; } Key = Key.Substring(nStart, (nEnd + (("").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(GlobalEx).Assembly.GetManifestResourceStream( "GZTW.AyaNova.BLL.p.xml"); // Read-in the XML content. StreamReader reader = new StreamReader(s); xmlkey = reader.ReadToEnd(); reader.Close(); } catch { _Valid = false; //log.Error("Internal error # 1001"); //log.Error("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 { _Valid = false; //log.Error("Internal error # 1002"); //_LicenseValidationStatus="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 { _Valid = false; //log.Error("Internal error # 1003"); //_LicenseValidationStatus="Error: no signature found." + ex.Message + "\r\n" + ex.InnerException; return; } // Verify the signature. if (!sxml.CheckSignature(csp)) { _Valid = false; //log.Error("Internal error # 1004"); //_LicenseValidationStatus="Error: Signature not valid, key has been tampered with."; return; } _Valid = true; try { _SchemaVersion = XmlConvert.ToDouble(xmldoc.SelectSingleNode("/" + sKeyType + "/SchemaVersion").InnerText); _RegisteredTo = xmldoc.SelectSingleNode("/" + sKeyType + "/RegisteredTo").InnerText; _ScheduleableUsers = XmlConvert.ToInt32(xmldoc.SelectSingleNode("/" + sKeyType + "/TotalScheduleableUsers").InnerText); _Generated = XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/" + sKeyType + "/Created").InnerText, XmlDateTimeSerializationMode.Local); _InstallableUntil = XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/" + sKeyType + "/InstallableUntil").InnerText, XmlDateTimeSerializationMode.Local); //Case 508 _DigestValue = xmldoc.GetElementsByTagName("DigestValue")[0].InnerText; if (xmldoc.SelectSingleNode("/" + sKeyType + "/Expires") != null) _Expires = XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/" + sKeyType + "/Expires").InnerText, XmlDateTimeSerializationMode.Local); else _Expires = null; //case 2094, now just standard plugin //_FeatureWebInterface = XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/" + sKeyType + "/FeatureWebBrowserInterface").InnerText); //case 3187 if (xmldoc.SelectSingleNode("/" + sKeyType + "/Source") != null) _Source = xmldoc.SelectSingleNode("/" + sKeyType + "/Source").InnerText; else _Source = "554E4B4E4F574E";//Hex encoded string "UNKNOWN" if (CheckInstallableUntilDate && System.DateTime.Now > _InstallableUntil) { _Installable = false; _Valid = false; //log.Error("Internal error # 1005"); //_LicenseValidationStatus="Error: Installation key has expired."; //return; } else _Installable = true; if (_RegisteredTo != "Unregistered trial") _TrialKey = false; //case 2094 MBI now plugin license like pti ////Case 726 //if (xmldoc.SelectSingleNode("/" + sKeyType + "/FeatureMBIInterface") != null) // _FeatureMBIInterface = XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/" + sKeyType + "/FeatureMBIInterface").InnerText); //else // _FeatureMBIInterface = false; //Case 999 if (xmldoc.SelectSingleNode("/" + sKeyType + "/RequestedTrial") != null) _RequestedTrial = XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/" + sKeyType + "/RequestedTrial").InnerText); else _RequestedTrial = false; //Case 2094 if (xmldoc.SelectSingleNode("/" + sKeyType + "/Sub") != null) _SubscriptionLicense = XmlConvert.ToBoolean(xmldoc.SelectSingleNode("/" + sKeyType + "/Sub").InnerText); else _SubscriptionLicense = false; //case 2094 if (xmldoc.SelectSingleNode("/" + sKeyType + "/LockDate") != null) { _Lockout = true; _LockoutDate = XmlConvert.ToDateTime(xmldoc.SelectSingleNode("/" + sKeyType + "/LockDate").InnerText, XmlDateTimeSerializationMode.Local); } else { _Lockout = false; _LockoutDate = DateTime.MaxValue; } //case 1053 plugins XmlNodeList plugs = xmldoc.SelectNodes("//Plugin"); if (plugs != null && plugs.Count > 0) { foreach (XmlNode n in plugs) { DataRow dr = Plugins.NewRow(); dr["Plugin"] = n.SelectSingleNode("Item").InnerText; //case 2094 SubscriptionExpires key if (_SubscriptionLicense) { if (n.SelectSingleNode("SubscriptionExpires") != null) dr["SubscriptionExpires"] = XmlConvert.ToDateTime(n.SelectSingleNode("SubscriptionExpires").InnerText, XmlDateTimeSerializationMode.Local); } else { dr["Version"] = n.SelectSingleNode("Version").InnerText; } Plugins.Rows.Add(dr); } } if (!CheckInstallableUntilDate)//case 999, this is only false if just reading from db at start _BootKeyIsRequestedTrial = _RequestedTrial; else//we're here because a new key has just been entered { if (_RequestedTrial && _BootKeyIsRequestedTrial)//was boot key a requested trial and new key a requested trial? { //Nope, can't install a requested trial on top of a requested trial //otherwise people will keep requesting and take advantage _Installable = false; _Valid = false; } } } catch { _Valid = false; //log.Error("Internal error # 1006"); //_LicenseValidationStatus="Error: Signature not valid - exception Processing fields:\r\n." + ex.Message + "\r\n" + ex.InnerException; return; } #endregion read XML format key } else { //_LicenseValidationStatus += "\r\nValidate: 4"; #region PARSE JSON KEY string licenseFileData = Key; //extract between [KEY and KEY] if (!licenseFileData.Contains("[KEY") || !licenseFileData.Contains("KEY]") || !licenseFileData.Contains("[SIGNATURE") || !licenseFileData.Contains("SIGNATURE]")) { _Valid = false; //_LicenseValidationStatus = "KEY IS NOT VALID! Missing one or more required delimiters"; return; } //_LicenseValidationStatus += "\r\nValidate: 5"; string keyNoWS = System.Text.RegularExpressions.Regex.Replace(AyaBizUtils.ExtractString(licenseFileData, "[KEY", "KEY]").Trim(), "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1"); string keySig = AyaBizUtils.ExtractString(licenseFileData, "[SIGNATURE", "SIGNATURE]").Trim(); #region Check Signature //***** NOTE: this is our real 2016 public key var publicPem = @"-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz7wrvLDcKVMZ31HFGBnL WL08IodYIV5VJkKy1Z0n2snprhSiu3izxTyz+SLpftvKHJpky027ii7l/pL9Bo3J cjU5rKrxXavnE7TuYPjXn16dNLd0K/ERSU+pXLmUaVN0nUWuGuUMoGJMEXoulS6p JiG11yu3BM9fL2Nbj0C6a+UwzEHFmns3J/daZOb4gAzMUdJfh9OJ0+wRGzR8ZxyC 99Na2gDmqYglUkSMjwLTL/HbgwF4OwmoQYJBcET0Wa6Gfb17SaF8XRBV5ZtpCsbS tkthGeoXZkFriB9c1eFQLKpBYQo2DW3H1MPG2nAlQZLbkJj5cSh7/t1bRF08m6P+ EQIDAQAB -----END PUBLIC KEY-----"; PemReader pr = new PemReader(new StringReader(publicPem)); var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pr.ReadObject(); var signer = SignerUtilities.GetSigner("SHA256WITHRSA"); signer.Init(false, KeyParameter); var expectedSig = Convert.FromBase64String(keySig); var msgBytes = Encoding.UTF8.GetBytes(keyNoWS); signer.BlockUpdate(msgBytes, 0, msgBytes.Length); if (!signer.VerifySignature(expectedSig)) { _Valid = false; _LicenseValidationStatus = "Error: Signature not valid, key has been tampered with."; return; } #endregion check signature //_LicenseValidationStatus += "\r\nValidate: 6"; //valid key (has valid signature) _Valid = true; try { #region Get Values Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.Parse(keyNoWS); _SchemaVersion = (double)token.SelectToken(sKeyType + ".SchemaVersion"); //for json keys use the Id unique index number as the digestvalue for revocation purposes //(this is a unix timestamp of moment key generated) _DigestValue = (string)token.SelectToken(sKeyType + ".Id"); _RegisteredTo = (string)token.SelectToken(sKeyType + ".RegisteredTo"); //case 3187 if (token.SelectToken(sKeyType + ".Source") != null) { _Source = (string)token.SelectToken(sKeyType + ".Source"); } else { _Source = "554E4B4E4F574E";//Hex encoded string "UNKNOWN" } _ScheduleableUsers = (int)token.SelectToken(sKeyType + ".TotalScheduleableUsers"); _Generated = (DateTime)token.SelectToken(sKeyType + ".Created"); _InstallableUntil = (DateTime)token.SelectToken(sKeyType + ".InstallableUntil"); if (token.SelectToken(sKeyType + ".Expires") != null) { _Expires = (DateTime)token.SelectToken(sKeyType + ".Expires"); } else { _Expires = null; } if (CheckInstallableUntilDate && System.DateTime.Now > _InstallableUntil) { _Installable = false; _Valid = false; } else _Installable = true; if (_RegisteredTo != "Unregistered trial") _TrialKey = false; if (token.SelectToken(sKeyType + ".RequestedTrial") != null) { _RequestedTrial = (bool)token.SelectToken(sKeyType + ".RequestedTrial"); } else { _RequestedTrial = false; } if (token.SelectToken(sKeyType + ".Sub") != null) { _SubscriptionLicense = (bool)token.SelectToken(sKeyType + ".Sub"); } else { _SubscriptionLicense = false; //default expiry to yesterday on v7 keys forcing new date to be selected in UI _Expires = DateTime.Now.AddDays(-1); } if (token.SelectToken(sKeyType + ".LockDate") != null) { _Lockout = true; _LockoutDate = (DateTime)token.SelectToken(sKeyType + ".LockDate"); } else { _LockoutDate = DateTime.MaxValue; _Lockout = false; } //PLUGINS //case 3222 Newtonsoft.Json.Linq.JArray p = (Newtonsoft.Json.Linq.JArray)token.SelectToken(sKeyType + ".Plugins.Plugin"); for (int x = 0; x < p.Count; x++) { DataRow dr = dtPlugins.NewRow(); dr["Plugin"] = (string)p[x].SelectToken("Item"); dr["SubscriptionExpires"] = (DateTime)p[x].SelectToken("SubscriptionExpires"); dtPlugins.Rows.Add(dr); } if (!CheckInstallableUntilDate)//case 999, this is only false if just reading from db at start _BootKeyIsRequestedTrial = _RequestedTrial; else//we're here because a new key has just been entered { if (_RequestedTrial && _BootKeyIsRequestedTrial)//was boot key a requested trial and new key a requested trial? { //Nope, can't install a requested trial on top of a requested trial //otherwise people will keep requesting and take advantage _Installable = false; _Valid = false; } } //_LicenseValidationStatus += "\r\nValidate: 7"; #endregion get values } catch (Exception ex) { //_LicenseValidationStatus += "\r\nValidate: 8"; _Valid = false; //_LicenseValidationStatus += "Error: Signature not valid - exception processing fields:\r\n." + ex.Message; if (ex.InnerException != null) //_LicenseValidationStatus += "\r\n" + ex.InnerException.Message; return; } #endregion parse json key } } #endregion #region crypt //case 3378 //this class is not actually doing anything useful and blocking FIPS //deprecated internal class CipherWrapper { RijndaelManaged _cipher = null; public CipherWrapper() { _cipher = InitCipher(); } public CipherWrapper(byte[] key) { _cipher = InitCipher(key); } public byte[] Key { get { return _cipher.Key; } set { _cipher.Key = value; } } // public byte[] EncryptMessage(byte[] plainText, out byte[] iv) // { // _cipher.GenerateIV(); // iv = _cipher.IV; // ICryptoTransform transform = _cipher.CreateEncryptor(); // byte[] cipherText = transform.TransformFinalBlock(plainText, 0, plainText.Length); // return cipherText; // } public byte[] EncryptMessage(byte[] plainText, byte[] iv) { _cipher.IV = iv; ICryptoTransform transform = _cipher.CreateEncryptor(); byte[] cipherText = transform.TransformFinalBlock(plainText, 0, plainText.Length); return cipherText; } public byte[] DecryptMessage(byte[] cipherText, byte[] iv) { _cipher.IV = iv; ICryptoTransform transform = _cipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(cipherText, 0, cipherText.Length); return plainText; } private RijndaelManaged InitCipher() { RijndaelManaged cipher = CreateCipher(); cipher.GenerateKey(); return cipher; } private RijndaelManaged InitCipher(byte[] key) { RijndaelManaged cipher = CreateCipher(); cipher.Key = key; return cipher; } private RijndaelManaged CreateCipher() { RijndaelManaged cipher = new RijndaelManaged(); cipher.KeySize = 256; cipher.BlockSize = 256; cipher.Mode = CipherMode.CBC; cipher.Padding = PaddingMode.PKCS7; return cipher; } } #endregion crypt #region trial data ex [Serializable, EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] internal class AGlobalExData { // private System.DateTime mEvalExpire; public System.DateTime EvalExpire { get { return mEvalExpire; } set { mEvalExpire = value; } } private System.DateTime mEvalAdvanceReference; public System.DateTime EvalAdvanceReference { get { return mEvalAdvanceReference; } set { mEvalAdvanceReference = value; } } } #endregion trial data ex }//end Global }//end namespace GZTW.AyaNova.BLL