Exemple #1
0
        public static Uri GetSignatureUrl(string filename)
        {
            try
            {
                int    encodingType;
                int    contentType;
                int    formatType;
                IntPtr certStore = IntPtr.Zero;
                IntPtr cryptMsg  = IntPtr.Zero;
                IntPtr context   = IntPtr.Zero;

                if (!WinCrypt.CryptQueryObject(WinCrypt.CERT_QUERY_OBJECT_FILE, Marshal.StringToHGlobalUni(filename), WinCrypt.CERT_QUERY_CONTENT_FLAG_ALL, WinCrypt.CERT_QUERY_FORMAT_FLAG_ALL, 0, out encodingType, out contentType, out formatType, ref certStore, ref cryptMsg, ref context))
                {
                    return(null);
                }

                // Get size of the encoded message.
                int cbData = 0;
                if (!WinCrypt.CryptMsgGetParam(cryptMsg, WinCrypt.CMSG_ENCODED_MESSAGE, 0, IntPtr.Zero, ref cbData))
                {
                    return(null);
                }
                var vData = new byte[cbData];

                // Get the encoded message.
                if (!WinCrypt.CryptMsgGetParam(cryptMsg, WinCrypt.CMSG_ENCODED_MESSAGE, 0, vData, ref cbData))
                {
                    return(null);
                }

                var signedCms = new SignedCms();
                signedCms.Decode(vData);

                foreach (var signerInfo in signedCms.SignerInfos)
                {
                    foreach (CryptographicAttributeObject signedAttribute in signerInfo.SignedAttributes)
                    {
                        if (signedAttribute.Oid.Value == "1.3.6.1.4.1.311.2.1.12")
                        {
                            foreach (AsnEncodedData x in signedAttribute.Values)
                            {
                                string z = x.Format(true);
                                int    i = z.IndexOf("68 74 74 70 73 3a 2f 2f"); // "https://"
                                if (i >= 0)
                                {
                                    return(new Uri(System.Text.UTF8Encoding.UTF8.GetString(FromHex(z.Substring(i)))));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception) { }
            return(null);
        }
        static void Main(string[] args)
        {
            // If this application is signed, get the URL of the signature, this will be used to lock this application to a server.
            Uri signedUrl = WinCrypt.GetSignatureUrl(System.Reflection.Assembly.GetEntryAssembly().Location);

            if (signedUrl != null)
            {
                NameValueCollection urlArguments = HttpUtility.ParseQueryString(signedUrl.Query);
                if (urlArguments["serverid"] != null)
                {
                    LockToServerId = urlArguments["serverid"];
                    LockToHostname = signedUrl.Host;
                }
            }

            if (Environment.OSVersion.Version.Major >= 6)
            {
                SetProcessDPIAware();
            }

            string update = null;

            foreach (string arg in args)
            {
                if ((arg.Length > 3) && (string.Compare(arg.Substring(0, 3), "-l:", true) == 0))
                {
                    try { Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(arg.Substring(3)); } catch (ArgumentException) { }
                }
                if ((arg.Length == 5) && (string.Compare(arg.Substring(0, 5), "-info", true) == 0))
                {
                    string dialogText = string.Format(Properties.Resources.Version, System.Reflection.Assembly.GetExecutingAssembly().ImageRuntimeVersion);
                    if (LockToHostname != null)
                    {
                        dialogText += "\r\n" + string.Format(Properties.Resources.LockedToHost, LockToHostname);
                    }
                    if (LockToServerId != null)
                    {
                        dialogText += "\r\n" + string.Format(Properties.Resources.LockedToServerId, LockToServerId);
                    }
                    MessageBox.Show(dialogText, Properties.Resources.MeshCentralAssistant);
                    return;
                }
                if (arg.Length > 8 && arg.Substring(0, 8).ToLower() == "-update:")
                {
                    update = arg.Substring(8);
                }
            }

            if (update != null)
            {
                // Perform self update, no mutex
                MainForm main = new MainForm(args);
            }
            else
            {
                // Named Mutexes are available computer-wide. Use a unique name.
                using (var mutex = new Mutex(false, "MeshCentralAssistantSingletonMutex"))
                {
                    // TimeSpan.Zero to test the mutex's signal state and
                    // return immediately without blocking
                    bool isAnotherInstanceOpen = !mutex.WaitOne(TimeSpan.Zero);
                    if (isAnotherInstanceOpen)
                    {
                        return;
                    }

                    // Setup settings & visual style
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Properties.Settings.Default.Upgrade();

                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ExceptionSink);
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventSink);
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);

                    foreach (string arg in args)
                    {
                        if (arg.Length > 3 && string.Compare(arg.Substring(0, 3), "-l:", true) == 0)
                        {
                            try { Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(arg.Substring(3)); } catch (ArgumentException) { }
                        }
                    }

                    MainForm main;
                    System.Globalization.CultureInfo currentCulture;
                    do
                    {
                        currentCulture = Thread.CurrentThread.CurrentUICulture;
                        main           = new MainForm(args);
                        if (main.forceExit == false)
                        {
                            Application.Run(main);
                        }
                    }while (currentCulture.Equals(Thread.CurrentThread.CurrentUICulture) == false);

                    mutex.ReleaseMutex();
                }
            }
        }