using System; using System.IO; using System.Management; using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json; using Xylem.Common.CommonCore.Consts; namespace Xylem.Common.Cryptology.Security { /// /// Cryptology information container /// public static class CryptInformation { /// /// Separator for PC name from hardware information /// public const String SeparatorPcFromHwId = @"\"; /// /// Get domain from local machine /// /// domain /// /// - Initial. /// public static String GetSysDomain() { // Collect key information from HW and user var userInformation = GetDomainAndUserLoginName(); var userInformationFields = userInformation.Split(Convert.ToChar("\\")); return userInformationFields[0]; } /// /// Get user login name from local machine /// /// user login name /// /// - Initial. /// /// /// - Convert to upper to get rid of the case sensitive user login name. /// public static String GetSysUserLoginName() { // Collect key information from HW and user var userInformation = GetDomainAndUserLoginName(); var userInformationFields = userInformation.Split(Convert.ToChar("\\")); return userInformationFields[1].ToUpper(); } /// /// User login name including the domain e.g. "SPXMLU\bauer_marc" /// /// /// - Initial. /// /// /// - Private function as user name is case sensitive. /// private static String GetDomainAndUserLoginName() { return System.Security.Principal.WindowsIdentity.GetCurrent().Name; } /// /// Getting local PC name which is useful for inter-program communication for operators to /// select the right PC for update operation. /// /// /// - Initial. /// public static String GetSysPcName() { return Environment.MachineName; } /// /// Getting the PC name out of the system information string which is useful for inter-program /// communication for operators to select the right PC for update operation. /// /// /// - Initial. /// public static String GetPcNameFromHwId(String hardwareId) { if (!hardwareId.Contains(SeparatorPcFromHwId)) return Constants.StrUnknown; var hwIdFields = hardwareId.Split(Convert.ToChar(SeparatorPcFromHwId)); return hwIdFields[0]; } /// /// User password hash replaced by random code if user is not registered or valid date is expired /// /// /// - Initial. /// /// /// - Replace "-" with blank in random code. /// public static String GetSysUserPasswordHash(UserInformation sysUser) { var randomCode = BitConverter.ToString(Cryptology.BuildRandomValue(32)).Replace("-", ""); // Take the validation date for to decide for new random code. if (sysUser == null || string.IsNullOrEmpty(sysUser.PasswordHash) || DateTimeOffset.Compare(sysUser.ValidDate, DateTimeOffset.Now) < 1) return randomCode; // if the registration valid date is not outdated, the sys user password shall remain as is return sysUser.PasswordHash; } /// /// Check user input for "full name". /// /// full user name for check /// true if user name matches the: /// - Start with capital letter, /// - one or more lowercase letters, /// - white space and at least /// - one trailing word, /// - double names separated with - or ' are allowed as well as special characters /// like umlaut in German or Spain... /// /// /// - Initial. /// /// /// - Check name for null or empty. /// public static Boolean CheckUserFullNameFormat(String name) { return !string.IsNullOrEmpty(name) && Regex.Match(name, @"\b\p{Lu}[-'\w]+(\s[-'\w]+)+$").Success; } /// /// Check if the decoding is possible by comparison of the reg user with the sys user. /// All information needs to be checked including the proper assigned FullName and the pwd hash. /// /// /// true if all information is set for decoding /// /// - Initial. /// /// /// - GetSysUserPasswordHash. /// public static Boolean IsDecodingPossible(UserInformation regUser) { if (regUser == null || string.IsNullOrEmpty(regUser.FullName)) return false; return regUser.Domain == GetSysDomain() && regUser.LogInName == GetSysUserLoginName() && regUser.HardwareId == GetSysHardwareId() && regUser.PasswordHash == GetSysUserPasswordHash(regUser); } /// /// Read the sys user from Win10 system. /// /// /// /// - Initial. /// /// /// - Take the sys user as input. /// public static void GetSysUser(UserInformation sysUser) { sysUser.Domain = GetSysDomain(); sysUser.LogInName = GetSysUserLoginName(); sysUser.HardwareId = GetSysHardwareId(); sysUser.PasswordHash = GetSysUserPasswordHash(sysUser); } /// /// Write the user registration from the registration file on the local HDD. /// /// /// /// true if file exists and user has at least the FullName being set /// /// - Initial. /// public static Boolean WriteUserRegistration(String pathFile, UserInformation dbFullRegUser) { // Register user to local HDD if the DB user is not identical if (string.IsNullOrEmpty(pathFile)) return false; try { // Store user registration from ..[User]/AppData/Roaming/Genesis. var serializedData = JsonConvert.SerializeObject(dbFullRegUser); var asciiStream = Encoding.UTF8.GetBytes(serializedData); File.WriteAllBytes(pathFile, asciiStream); return true; } catch (Exception) { return false; } } /// /// Read the user registration from the registration file on the local HDD. /// /// /// /// true if file exists and user has at least the FullName being set /// /// - Initial. /// /// /// - Convert to upper to get rid of the case sensitive user login name. /// public static Boolean ReadUserRegistration(String pathFile, out UserInformation regUser) { regUser = null; try { if (!File.Exists(pathFile)) return false; regUser = new UserInformation(); // load user registration from ..[User]/AppData/Roaming/Genesis var asciiStream = File.ReadAllBytes(pathFile); var serializedData = Encoding.UTF8.GetString(asciiStream, 0, asciiStream.Length); regUser = JsonConvert.DeserializeObject(serializedData); regUser.LogInName = regUser.LogInName.ToUpper(); return true; } catch (Exception) { return false; } } /// /// Bios version of user PC /// /// /// - Initial. /// public static String GetBiosVersion() { var bios = new ManagementObjectSearcher("SELECT Version FROM Win32_BIOS"); var biosCollection = bios.Get(); var strBios = new StringBuilder(); foreach (var obj in biosCollection) { strBios.Append(obj["Version"]); } return strBios.ToString(); } /// /// Hardware identification of user PC based on UUID and processor ID /// /// /// - Initial. /// /// /// - Add PC-Name to the HW Id to inform the FwUpdateBuilder operator about the PC. /// public static String GetSysHardwareId() { var strHwId = new StringBuilder(); var pcName = GetSysPcName(); strHwId.Append(pcName); strHwId.Append(SeparatorPcFromHwId); var computerSystem = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct"); var computerSystemCollection = computerSystem.Get(); foreach (var obj in computerSystemCollection) { strHwId.Append(obj["UUID"]); } strHwId.Append("-"); var processorInfo = new ManagementObjectSearcher("SELECT ProcessorID From Win32_processor"); var processorInfoCollection = processorInfo.Get(); foreach (var obj in processorInfoCollection) { strHwId.Append(obj["ProcessorID"]); } return strHwId.ToString(); } } }