public static bool validateLicenseFile(string licFile, ref LicenseTerms lic) { LicenseDTO license = null; if (File.Exists(licFile)) { license = LicenseDTO.FromString(File.ReadAllText(licFile)); } else { throw new FileNotFoundException("Lic file not found"); } if (license != null) { lic = ValidateLicense(license); return(true); } else { //TODO : "Invalid License File Not Supplied!" return(false); } return(false); }
/// <summary> /// use a private key to generate a secure license file. the private key must match the public key accessible to /// the system validating the license. /// </summary> /// <param name="start">applicable start date for the license file.</param> /// <param name="end">applicable end date for the license file</param> /// <param name="productName">applicable product name</param> /// <param name="userName">user-name</param> /// <param name="privateKey">the private key (in XML form)</param> /// <returns>secure, public license, validated with the public part of the key</returns> public static License CreateLicense(DateTime start, DateTime end, String productName, String userName, String privateKey) { // create the licence terms: LicenseTerms terms = new LicenseTerms() { StartDate = start, EndDate = end, ProductName = productName, UserName = userName }; // create the crypto-service provider: DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); // setup the dsa from the private key: dsa.FromXmlString(privateKey); // get the byte-array of the licence terms: byte[] license = terms.GetLicenseData(); // get the signature: byte[] signature = dsa.SignData(license); // now create the license object: return(new License() { LicenseTerms = Convert.ToBase64String(license), Signature = Convert.ToBase64String(signature) }); }
private static LicenseTerms ValidateLicense(LicenseDTO license) { RSACryptoServiceProvider rsa = GetPublicKeyFromAssemblyAtClient(Assembly.GetExecutingAssembly()); byte[] dataToVerify = Convert.FromBase64String(license.LicenseTerms); byte[] signature = Convert.FromBase64String(license.Signature); // verify that the license-terms match the signature data if (rsa.VerifyData(dataToVerify, new SHA1CryptoServiceProvider(), signature)) { return(LicenseTerms.FromString(license.LicenseTerms)); } else { throw new Exception("license file is tempered."); } }
/// <summary> /// validates the license and if the app should run; if the license is valid the /// method will complete, if not it will throw a security exception. /// </summary> /// <param name="license"> /// the license object. /// </param> /// <exception cref="SecurityException">thrown if the license is invalid or expired</exception> /// <returns></returns> public static void ValidateLicense(License license, String publicKey) { // get the valid terms for the license: (this checks the digital signature on the license file) LicenseTerms terms = GetValidTerms(license, publicKey); // ensure a valid license-terms object was returned: if (terms != null) { // validate the date-range of the license terms: if (DateTime.Now.CompareTo(terms.EndDate) <= 0) { if (DateTime.Now.CompareTo(terms.StartDate) >= 0) { // date range is valid... check the product name against the current assembly if (Assembly.GetExecutingAssembly().FullName == terms.ProductName) { return; } else { // product name doesn't match. throw new SecurityException("Invalid Product Name: " + terms.ProductName); } } else { // license terms not valid yet. throw new SecurityException("License Terms Not Valid Until: " + terms.StartDate.ToShortDateString()); } } else { // license terms have expired. throw new SecurityException("License Terms Expired On: " + terms.EndDate.ToShortDateString()); } } else { // the license file was not valid. throw new SecurityException("Invalid License File!"); } }
/// <summary> /// validate license file and return the license terms. /// </summary> /// <param name="license"></param> /// <param name="publicKey"></param> /// <returns></returns> internal static LicenseTerms GetValidTerms(License license, String publicKey) { // create the crypto-service provider: DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); // setup the provider from the public key: dsa.FromXmlString(publicKey); // get the license terms data: byte[] terms = Convert.FromBase64String(license.LicenseTerms); // get the signature data: byte[] signature = Convert.FromBase64String(license.Signature); // verify that the license-terms match the signature data if (dsa.VerifyData(terms, signature)) { return(LicenseTerms.FromString(license.LicenseTerms)); } else { throw new SecurityException("Signature Not Verified!"); } }
protected override void OnStart(string[] args) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Thread.CurrentThread.Name = "MachineConnect_Service"; if (!Directory.Exists(appPath + "\\Logs\\")) { Directory.CreateDirectory(appPath + "\\Logs\\"); } //if (Utility.ValidateServiceTagName() == false) //{ // if (Utility.ValidateDomainName() == false) // { // if (Utility.ValidateMACAddress() == false) // { // Logger.WriteErrorLog("Licensing validation failed for MMT Computers. Please contact AMIT PVT LTD."); // return; // } // } //} if (!Directory.Exists(appPath + "\\TPMFiles\\")) { Directory.CreateDirectory(appPath + "\\TPMFiles\\"); } if (!Directory.Exists(appPath + "\\Licensing\\")) { Directory.CreateDirectory(appPath + "\\Licensing\\"); } try { licInfo.CNCData = new List <CNCData>(); var licFolderPath = Path.Combine(appPath, "Licensing"); var files = Directory.GetFiles(licFolderPath, "*.lic", SearchOption.TopDirectoryOnly); foreach (var file in files) { LicenseTerms licDto = null; Utility.validateLicenseFile(file, ref licDto); if (licDto == null || licDto.CNCData == null || licDto.CNCData.Count == 0) { Logger.WriteErrorLog("Invalid Lic file"); return; } else { licInfo.ComputerSerialNo = licDto.ComputerSerialNo; licInfo.Customer = licDto.Customer; licInfo.Email = licDto.Email; licInfo.ExpiresAt = licDto.ExpiresAt; licInfo.LicType = licDto.LicType; licInfo.Plant = licDto.Plant; licInfo.StartDate = licDto.StartDate; foreach (var cncdata in licDto.CNCData) { if (licInfo.CNCData.Exists(data => data.CNCdata1 == cncdata.CNCdata1 && data.IsOEM == cncdata.IsOEM) == false) { licInfo.CNCData.Add(cncdata); } } } } } catch (Exception ex) { if (ex is System.FormatException) { Logger.WriteErrorLog("Invald lic file....or file has been tempared.."); } else { Logger.WriteErrorLog(ex.Message); } return; } if (licInfo == null || licInfo.CNCData == null || licInfo.CNCData.Count == 0) { Logger.WriteErrorLog("Invalid Lic file"); return; } ServiceStop.stop_service = 0; List <MachineInfoDTO> machines = DatabaseAccess.GetTPMTrakMachine(); if (machines.Count == 0) { Logger.WriteDebugLog("No machine is enabled for DNCTransferEnabled. modify the machine setting and restart the service."); return; } try { foreach (MachineInfoDTO machine in machines) { CreateClient client = new CreateClient(machine); clients.Add(client); ThreadStart job = new ThreadStart(client.GetClient); Thread thread = new Thread(job); thread.Name = Utility.SafeFileName(machine.MachineId); thread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); thread.Start(); threads.Add(thread); Logger.WriteDebugLog(string.Format("Machine {0} started for DataCollection with IP = {1} , PORT = {2}.", machine.MachineId, machine.IpAddress, machine.PortNo)); } } catch (Exception e) { Logger.WriteErrorLog(e.Message); } //Pramod try { ThreadStart cleanupProcess = new ThreadStart(cleanupProcessService); Thread thread = new Thread(cleanupProcess); thread.Name = "cleanupProcess"; thread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); thread.Start(); threads.Add(thread); } catch (Exception ex) { Logger.WriteErrorLog(ex.ToString()); } }