Пример #1
0
            public static void RunLocalLicenseKeyHelper()
            {
                InstallPerServerLicensePkCertificates();

                var mac = GetMacAddress();

                var myMac = new byte[mac.Length];

                for (int i = 0; i < mac.Length; i++)
                {
                    myMac[i] = (byte)mac[i];
                }

                byte[] mySignedMacPrivateKey = SignMacWithPrivateKey(myMac);

                byte[] myDecodedMacPrivate = CryptoHelper.VerifyAndRemoveSignature(mySignedMacPrivateKey);

                for (int i = 0; i < myDecodedMacPrivate.Length; i++)
                {
                    if (myDecodedMacPrivate[i] != myMac[i])
                    {
                        throw new Exception("kl License Key Invalid");
                    }
                }

                var myFile = File.OpenWrite("signedMac");

                myFile.Write(mySignedMacPrivateKey, 0, mySignedMacPrivateKey.Length);
                myFile.Close();

                //Remove Certificates with private keys
                var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

                try
                {
                    store.Open(OpenFlags.ReadWrite);
                    RemoveCertificates(store, "CN=klLicenseKeyGen");
                }
                finally
                {
                    store.Close();
                }

                store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
                try
                {
                    store.Open(OpenFlags.ReadWrite);
                    RemoveCertificates(store, "CN=klLicenseKeyGen");
                }
                finally
                {
                    store.Close();
                }

                store = new X509Store("TRUST", StoreLocation.LocalMachine);
                try
                {
                    store.Open(OpenFlags.ReadWrite);

                    RemoveCertificates(store, "CN=klLicenseKeyGenBase");
                }
                finally
                {
                    store.Close();
                }

                InstallPerServerLicensePublicCertificates();

                myFile = File.OpenRead("signedMac");

                long length = myFile.Length;

                var myRead = new byte[length];

                myFile.Read(myRead, 0, (int)length);

                myFile.Close();

                //Since we removed the private keys and instaleld the public ones, this step simulates the
                //validation step that allows the WCF security to be installed on the machine with the specified MAC address.

                var signedMessage = new SignedCms();

                signedMessage.Decode(myRead);

                signedMessage.CheckSignature(true);

                byte[] myDecodedMac = signedMessage.ContentInfo.Content;

                for (int i = 0; i < myDecodedMacPrivate.Length; i++)
                {
                    if (myDecodedMac[i] != myMac[i])
                    {
                        throw new Exception("kl License Key Invalid");
                    }
                }

                //Now we have the prerequisites to install the certificates on this machine.
                SecureString mySs = GetSecureStringFromConsole();

                InstallWcfSecurityCertificates(mySs);
            }