private void BtnSave_Click(object sender, EventArgs e) { _manager.WriteAllOpenAssets(); _fileProvider.Save(); if (_fileProvider is ApkAssetsFileProvider) { ApkSigner s = new ApkSigner(BSConst.DebugCertificatePEM); s.Sign(_fileProvider); } CloseStuff(); }
public void Sign(IAssetsFileProvider fileProvider) { MemoryStream msManifestFile = new MemoryStream(); MemoryStream msSigFile = new MemoryStream(); byte[] keyBlock; MemoryStream msSignatureFileBody = new MemoryStream(); try { //create the MF file header using (StreamWriter swManifest = GetSW(msManifestFile)) { swManifest.WriteLine("Manifest-Version: 1.0"); swManifest.WriteLine("Created-By: emulamer"); swManifest.WriteLine(); } //so that we can do it in one pass, write the MF and SF line items at the same time to their respective streams foreach (var infFile in fileProvider.FindFiles("*").Where(x => !x.StartsWith("META-INF"))) { WriteEntryHashes(fileProvider, infFile, msManifestFile, msSignatureFileBody); } //compute the hash on the entirety of the manifest file for the SF file msManifestFile.Seek(0, SeekOrigin.Begin); var manifestFileHash = _sha.ComputeHash(msManifestFile); //write the SF to memory then copy it out to the actual file- contents will be needed later to use for signing, don't want to hit the zip stream twice byte[] sigFileBytes = null; using (StreamWriter swSignatureFile = GetSW(msSigFile)) { swSignatureFile.WriteLine("Signature-Version: 1.0"); swSignatureFile.WriteLine($"SHA1-Digest-Manifest: {Convert.ToBase64String(manifestFileHash)}"); swSignatureFile.WriteLine("Created-By: emulamer"); swSignatureFile.WriteLine(); } msSignatureFileBody.Seek(0, SeekOrigin.Begin); msSignatureFileBody.CopyTo(msSigFile); msSigFile.Seek(0, SeekOrigin.Begin); sigFileBytes = msSigFile.ToArray(); //get the key block (all the hassle distilled into one line), then write it out to the RSA file keyBlock = SignIt(sigFileBytes); //delete all the META-INF stuff that exists already fileProvider.DeleteFiles("META-INF*"); //write the 3 files msManifestFile.Seek(0, SeekOrigin.Begin); fileProvider.Write("META-INF/MANIFEST.MF", msManifestFile.ToArray(), true, true); fileProvider.Write("META-INF/BS.SF", sigFileBytes, true, true); fileProvider.Write("META-INF/BS.RSA", keyBlock, true, true); fileProvider.Save(); } finally { if (msManifestFile != null) { msManifestFile.Dispose(); } if (msSignatureFileBody != null) { msSignatureFileBody.Dispose(); } if (msManifestFile != null) { msManifestFile.Dispose(); } if (msSigFile != null) { msSigFile.Dispose(); } } }