Beispiel #1
0
        private async Task <bool> DeployMFUpdateAsync(StorageFile zipFile, CancellationToken cancellationToken, IProgress <ProgressReport> progress = null)
        {
            const int c_PacketSize = 1024;

            if (zipFile.IsAvailable)
            {
                byte[] packet = new byte[c_PacketSize];
                try
                {
                    int handle = -1;
                    int idx    = 0;

                    var fileInfo = await zipFile.GetBasicPropertiesAsync();

                    int numPkts = ((int)fileInfo.Size + c_PacketSize - 1) / c_PacketSize;

                    byte[] hashData = UTF8Encoding.UTF8.GetBytes(zipFile.Name + fileInfo.DateModified.ToString());

                    uint updateId = CRC.ComputeCRC(hashData, 0, hashData.Length, 0);
                    uint imageCRC = 0;

                    byte[] sig = null;

                    Debug.WriteLine(updateId);

                    handle = await DebugEngine.StartUpdateAsync("NetMF", 4, 4, updateId, 0, 0, (uint)fileInfo.Size, (uint)c_PacketSize, 0);

                    if (handle > -1)
                    {
                        uint         authType;
                        IAsyncResult iar = null;

                        // perform request
                        var resp = await DebugEngine.UpdateAuthCommandAsync(handle, 1, null);

                        // check result
                        if (!resp.Item2 || resp.Item1.Length < 4)
                        {
                            return(false);
                        }


                        using (MemoryStream ms = new MemoryStream(resp.Item1))
                            using (BinaryReader br = new BinaryReader(ms))
                            {
                                authType = br.ReadUInt32();
                            }


                        byte[] pubKey = null;

                        // FIXME
                        //if (m_serverCert != null)
                        //{

                        //    RSACryptoServiceProvider rsa = m_serverCert.PrivateKey as RSACryptoServiceProvider;

                        //    if (rsa != null)
                        //    {
                        //        pubKey = rsa.ExportCspBlob(false);
                        //    }
                        //}

                        if (!await DebugEngine.UpdateAuthenticateAsync(handle, pubKey))
                        {
                            return(false);
                        }

                        // FIXME
                        //if (authType == 1 && m_serverCert != null)
                        //{
                        //    iar = await DebugEngine.UpgradeConnectionToSsl_Begin(m_serverCert, m_requireClientCert);

                        //    if (0 == WaitHandle.WaitAny(new WaitHandle[] { iar.AsyncWaitHandle, EventCancel }, 10000))
                        //    {
                        //        try
                        //        {
                        //            if (!m_eng.UpgradeConnectionToSSL_End(iar))
                        //            {
                        //                m_eng.Dispose();
                        //                m_eng = null;
                        //                return false;
                        //            }
                        //        }
                        //        catch
                        //        {
                        //            m_eng.Dispose();
                        //            m_eng = null;
                        //            return false;
                        //        }
                        //    }
                        //    else
                        //    {
                        //        return false;
                        //    }
                        //}

                        // FIXME
                        //RSAPKCS1SignatureFormatter alg = null;
                        object alg = null;
                        HashAlgorithmProvider hash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
                        byte[] hashValue           = null;

                        try
                        {
                            if (m_serverCert != null)
                            {
                                //alg = new RSAPKCS1SignatureFormatter(m_serverCert.PrivateKey);
                                //alg.SetHashAlgorithm("SHA1");
                                hash      = HashAlgorithmProvider.OpenAlgorithm("SHA1");
                                hashValue = new byte[hash.HashLength / 8];
                            }
                        }
                        catch
                        {
                        }

                        IBuffer buffer = await FileIO.ReadBufferAsync(zipFile);

                        using (DataReader dataReader = DataReader.FromBuffer(buffer))
                        {
                            dataReader.ReadBytes(packet);

                            uint crc = CRC.ComputeCRC(packet, 0, packet.Length, 0);

                            if (!await DebugEngine.AddPacketAsync(handle, (uint)idx++, packet, CRC.ComputeCRC(packet, 0, packet.Length, 0)))
                            {
                                return(false);
                            }

                            imageCRC = CRC.ComputeCRC(packet, 0, packet.Length, imageCRC);

                            progress?.Report(new ProgressReport(idx, numPkts, string.Format("Deploying {0}...", idx)));
                        }

                        if (hash != null)
                        {
                            buffer = await FileIO.ReadBufferAsync(zipFile);

                            // hash it
                            IBuffer hashed = hash.HashData(buffer);
                            CryptographicBuffer.CopyToByteArray(hashed, out sig);
                        }

                        if (alg != null)
                        {
                            //sig = alg.CreateSignature(hash);
                            //CryptographicBuffer.CopyToByteArray(sig)
                        }
                        else
                        {
                            sig = new byte[4];
                            using (MemoryStream ms = new MemoryStream(sig))
                                using (BinaryWriter br = new BinaryWriter(ms))
                                {
                                    br.Write(imageCRC);
                                }
                        }

                        if (await DebugEngine.InstallUpdateAsync(handle, sig))
                        {
                            return(true);
                        }
                    }
                }
                catch
                {
                }
            }

            return(false);
        }