public bool ValidateUpdate(MFUpdate_Emu update, IntPtr pValidation, int validationLen)
 {
     // todo: add signature check
     return(true);
 }
        public bool ValidatePacket(MFUpdate_Emu update, IntPtr packet, int packetLen, IntPtr validationData, int validationLen)
        {
            bool valid = false;

            switch ((MFUpdateType)update.Header.UpdateType)
            {
            case MFUpdateType.AssemblyUpdate:
                HashAlgorithm alg = null;

                switch (validationLen)
                {
                case 512 / 8:
                    alg = new SHA512Cng();
                    break;

                case 256 / 8:
                    alg = new SHA256Cng();
                    break;

                case 384 / 8:
                    alg = new SHA384Cng();
                    break;

                case 160 / 8:
                    alg = new SHA1Cng();
                    break;
                }

                if (alg != null)
                {
                    byte[] data = new byte[packetLen];
                    Marshal.Copy(packet, data, 0, packetLen);

                    byte[] hash = alg.ComputeHash(data);

                    if (hash.Length == validationLen)
                    {
                        valid = true;
                        byte[] vHash = new byte[validationLen];
                        Marshal.Copy(validationData, vHash, 0, validationLen);

                        for (int i = 0; i < validationLen; i++)
                        {
                            if (vHash[i] != hash[i])
                            {
                                valid = false;
                                break;
                            }
                        }
                    }
                }
                break;

            case MFUpdateType.FirmwareUpdate:
                valid = true;
                break;

            case MFUpdateType.KeyUpdate:
                // TODO:
                break;
            }
            return(valid);
        }
 public bool Authenticate(MFUpdate_Emu update, IntPtr pAuth, int authLen)
 {
     return(true);
 }
 public bool AuthCommand(MFUpdate_Emu update, uint cmd, IntPtr pArgs, int argsLen, IntPtr pResponse, ref int responseLen)
 {
     return(true);
 }
        public bool InstallUpdate(MFUpdate_Emu update, IntPtr pValidation, int validationLen)
        {
            bool            fValid = false;
            MFUpdate_Header header = new MFUpdate_Header();

            GCHandle h   = GCHandle.Alloc(header, GCHandleType.Pinned);
            IntPtr   ptr = h.AddrOfPinnedObject();

            Hal.UpdateStorage.Read(update.StorageHandle, 0, ptr, Marshal.SizeOf(header));
            header = (MFUpdate_Header)Marshal.PtrToStructure(ptr, typeof(MFUpdate_Header));
            h.Free();

            switch ((MFUpdateType)header.UpdateType)
            {
            case MFUpdateType.AssemblyUpdate:
                // signature check or strong name check
                fValid = true;
                if (fValid)
                {
                    byte[] asmBytes = new byte[header.UpdateSize];

                    GCHandle hb      = GCHandle.Alloc(asmBytes, GCHandleType.Pinned);
                    IntPtr   gcBytes = h.AddrOfPinnedObject();

                    if (0 < Hal.UpdateStorage.Read(update.StorageHandle, Marshal.SizeOf(header), gcBytes, (int)header.UpdateSize))
                    {
                        Marshal.Copy(gcBytes, asmBytes, 0, asmBytes.Length);

                        string fTmp = Path.ChangeExtension(Path.GetTempFileName(), ".pe");
                        try
                        {
                            using (FileStream fs = File.OpenWrite(fTmp))
                            {
                                fs.Write(asmBytes, 0, asmBytes.Length);
                            }

                            Emulator.LoadAssembly(fTmp);
                            Emulator.Run();

                            File.Delete(fTmp);
                        }
                        catch
                        {
                            return(false);
                        }
                        finally
                        {
                            if (File.Exists(fTmp))
                            {
                                File.Delete(fTmp);
                            }
                        }
                    }

                    hb.Free();
                }
                break;

            case MFUpdateType.KeyUpdate:
                // unwrap and store
                break;
            }

            return(fValid);
        }
 public bool SetProperty(MFUpdate_Emu update, string propertyName, IntPtr propValue, int propSize)
 {
     return(false);
 }