コード例 #1
0
        private static bool Verify(byte[] signedaddon, out SignedAddonHeader header, out byte[] assembly)
        {
            using (var stream = new MemoryStream(signedaddon))
            {
                using (var reader = new BinaryReader(stream))
                {
                    using (var rsaProvider = new RSACryptoServiceProvider(new CspParameters {
                        ProviderType = 1
                    }))
                    {
                        using (var sha1 = new SHA1CryptoServiceProvider())
                        {
                            rsaProvider.ImportCspBlob(Convert.FromBase64String(PublicKey));

                            var headerBuffer   = reader.ReadBytes(Marshal.SizeOf(typeof(SignedAddonHeader)));
                            var assemblyBuffer = reader.ReadBytes(signedaddon.Length - headerBuffer.Length);
                            header = DeserializeStructure <SignedAddonHeader>(headerBuffer);

                            bool result;

                            switch (header.Data.SignatureVersion)
                            {
                            case "2":
                                const int signatureSize = 320;
                                var       verifyBuffer  = new byte[signedaddon.Length - signatureSize];
                                Array.Copy(signedaddon, signatureSize, verifyBuffer, 0, verifyBuffer.Length);

                                result = rsaProvider.VerifyData(verifyBuffer, sha1, header.Signature);
                                break;

                            default:
                                Log.Instance.DoLog("You are using an older version of the addon, support for older addons will be removed soon.");
                                result = rsaProvider.VerifyData(assemblyBuffer, sha1, header.Signature);
                                break;
                            }

                            if (result)
                            {
                                var key = CustomRsa.DecodeBlock(header.CryptoData.Key, new BigInteger(Exponent), new BigInteger(Modulus));
                                assembly = RijndaelHelper.Decrypt(assemblyBuffer, key, header.CryptoData.Salt, header.CryptoData.Iterations);

                                return(true);
                            }
                        }
                    }
                }
            }

            assembly = null;
            return(false);
        }
コード例 #2
0
        internal static byte[] VerifyAndDecrypt(byte[] signedaddon)
        {
            SignedAddonHeader header;

            byte[] encryptedAssembly = null;

            using (var stream = new MemoryStream(signedaddon))
            {
                using (var reader = new BinaryReader(stream))
                {
                    var headerBuffer   = reader.ReadBytes(Marshal.SizeOf(typeof(SignedAddonHeader)));
                    var assemblyBuffer = reader.ReadBytes(signedaddon.Length - headerBuffer.Length);

                    var gcHandle = GCHandle.Alloc(headerBuffer, GCHandleType.Pinned);
                    header = (SignedAddonHeader)Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof(SignedAddonHeader));
                    gcHandle.Free();

                    var cspParams = new CspParameters {
                        ProviderType = 1
                    };

                    using (var rsaProvider = new RSACryptoServiceProvider(cspParams))
                    {
                        rsaProvider.ImportCspBlob(Convert.FromBase64String(PublicKey));

                        using (var sha1 = new SHA1CryptoServiceProvider())
                        {
                            if (rsaProvider.VerifyData(assemblyBuffer, sha1, header.Signature))
                            {
                                encryptedAssembly = assemblyBuffer;
                            }
                        }
                    }
                }
            }

            if (encryptedAssembly != null)
            {
                var key = CustomRsa.DecodeBlock(header.CryptoData.Key, new BigInteger(Exponent), new BigInteger(Modulus));
                return(RijndaelHelper.Decrypt(encryptedAssembly, key, header.CryptoData.Salt, header.CryptoData.Iterations));
            }

            return(null);
        }