private async Task <bool> DeployMFUpdateAsync(StorageFile zipFile, CancellationToken cancellationToken, IProgress <ProgressReport> progress = null) { if (zipFile.IsAvailable) { byte[] packet = new byte[DebugEngine.WireProtocolPacketSize]; try { int handle = -1; int idx = 0; var fileInfo = await zipFile.GetBasicPropertiesAsync(); uint numPkts = (uint)(fileInfo.Size + DebugEngine.WireProtocolPacketSize - 1) / DebugEngine.WireProtocolPacketSize; 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 = DebugEngine.StartUpdate("NetMF", 4, 4, updateId, 0, 0, (uint)fileInfo.Size, (uint)DebugEngine.WireProtocolPacketSize, 0); if (handle > -1) { uint authType; IAsyncResult iar = null; // perform request var resp = DebugEngine.UpdateAuthCommand(handle, 1, null); // check result if (!resp.Success || resp.Response.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 (!DebugEngine.UpdateAuthenticate(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 (!DebugEngine.AddPacket(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 (DebugEngine.InstallUpdate(handle, sig)) { return(true); } } } catch { } } return(false); }