public bool CheckScript(PKGRootData Package, out string ErrorText) { try { ErrorText = ""; AbortProcess = false; UpdateText("Checking data"); if (Package.PackageID.Trim() == "") { ErrorText = "Invalid element (PackageID=\"\")"; return(false); } if (Package.Title.Trim() == "") { ErrorText = "Invalid element (Title=\"\")"; return(false); } foreach (PKGFile f in Package.Files) { UpdateText("Checking " + f.SrcFile); Debug.WriteLine("Checking " + f.SrcFile); if (File.Exists(f.SrcFile) == false) { ErrorText = "File " + f.SrcFile + " does not exist."; return(false); } if (f.FileName.Trim() == "") { ErrorText = "Invalid element (Filename=\"\")"; return(false); } if (f.FolderName.Trim() == "") { ErrorText = "Invalid element (FolderName=\"\")"; return(false); } if (AbortProcess == true) { ErrorText = "Aborted"; return(false); } } UpdateText(""); return(true); } catch (Exception ee) { Debug.WriteLine(ee.ToString()); ErrorText = "SEH Error"; return(false); } }
bool TestSignatureByInternalSigning(PKGRootData Package, string Filename, ZipArchive ZipArch, List <byte[]> CertificatesData) { ZipArchiveEntry entry = ZipArch.GetEntry(Filename); if (entry == null) { return(false); } bool SignValid = false; if (Package.Signatures.ContainsKey(Filename.ToLower()) == false) { return(false); } foreach (byte[] signdata in Package.Signatures[Filename.ToLower()]) { //Debug.WriteLine("Check " + Filename + " with Signature " + Filename + ".sign" + (i == 1 ? "" : i.ToString())); UpdateText("Checking " + Filename); foreach (byte[] cer in CertificatesData) { using (Stream data = GetZipStreamData(entry)) { SignValid = Certificates.Verify(data, signdata, cer); } if (SignValid == true) { break; } } if (SignValid == true) { break; } } return(SignValid); }
public bool CompilePackage(PKGRootData Package, PKGCompilerArgs Args, out string ErrorText) { try { ErrorText = ""; AbortProcess = false; UpdateText("Preparing data"); PKGRootData ZipDef = new PKGRootData(); ZipDef.Description = Package.Description; ZipDef.HeaderID = Package.HeaderID; ZipDef.Outputfile = ""; ZipDef.PackageID = Package.PackageID; ZipDef.Script = Package.Script; ZipDef.Title = Package.Title; ZipDef.VersionID = Package.VersionID; ZipDef.Folders = new List <PKGFolder>(); //not needed here ZipDef.Files = new List <PKGFile>(); ZipDef.Signatures = new Dictionary <string, List <byte[]> >(); using (Stream filestream = File.Open(Package.Outputfile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { using (ZipArchive ZipFile = new ZipArchive(filestream, ZipArchiveMode.Create, true, Encoding.UTF8)) { ZipArchiveEntry entry; byte[] signature; foreach (PKGFile file in Package.Files) { if (AbortProcess == true) { filestream.Close(); ErrorText = "Aborted"; return(false); } UpdateText("Compressing " + file.SrcFile); PKGFile inzipfile = new PKGFile(); inzipfile.ID = file.ID; inzipfile.SrcFile = Guid.NewGuid().ToString(); inzipfile.FileName = file.FileName; inzipfile.FolderName = file.FolderName; inzipfile.KeepInMeta = file.KeepInMeta; inzipfile.InstallThisFile = file.InstallThisFile; Stream FileStream = File.Open(file.SrcFile, FileMode.Open, FileAccess.Read, FileShare.Read); entry = ZipFile.CreateEntry(inzipfile.SrcFile, CompressionLevel.Optimal); using (Stream ZipStream = entry.Open()) { FileStream.CopyTo(ZipStream); } FileStream.Seek(0, SeekOrigin.Begin); if (AbortProcess == true) { filestream.Close(); ErrorText = "Aborted"; return(false); } UpdateText("Signing " + file.SrcFile); signature = null; if (Args.UseExtSign == false) { signature = Certificates.Sign(FileStream, Args.SignCert, Args.SignLocation); } else { SmartCards sm = new SmartCards(); signature = sm.SignData(Args.SignExtCert, FileStream, Args.PIN); } FileStream.Close(); if (signature == null) { ErrorText = "Signing failed"; filestream.Close(); return(false); } UpdateText("Storing Signature " + file.SrcFile); ZipDef.Files.Add(inzipfile); ZipDef.Signatures.Add(inzipfile.SrcFile.ToLower(), new List <byte[]> { signature }); } UpdateText("Finalizing"); byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ZipDef, Formatting.None)); signature = null; if (Args.UseExtSign == false) { signature = Certificates.Sign(data, Args.SignCert, Args.SignLocation); } else { SmartCards sm = new SmartCards(); signature = sm.SignData(Args.SignExtCert, data, Args.PIN); } entry = ZipFile.CreateEntry("Script.json", CompressionLevel.Optimal); using (Stream ZipStream = entry.Open()) { ZipStream.Write(data, 0, data.Length); } entry = ZipFile.CreateEntry("Script.json.sign", CompressionLevel.Optimal); using (Stream ZipStream = entry.Open()) { ZipStream.Write(signature, 0, signature.Length); } return(true); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); ErrorText = "SEH Error"; return(false); } }
public bool CreateMetaDataPackage(string Filename, string OutputFilename, out string ErrorText) { ErrorText = ""; using (FileStream ZipFile = File.Open(Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (ZipArchive ZipArch = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8)) { ZipArchiveEntry entry; entry = ZipArch.GetEntry("Script.json"); if (entry == null) { ZipFile.Close(); ErrorText = "Missing Script.json"; return(false); } PKGRootData Package = new PKGRootData(); byte[] packagedata = GetZipData(entry); string ScriptSource = Encoding.UTF8.GetString(packagedata); try { Package = JsonConvert.DeserializeObject <PKGRootData>(ScriptSource); if (Package.HeaderID != "FoxPackageScriptV1") { ErrorText = "Script.json is not the format wanted (Header)!"; ZipFile.Close(); return(false); } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); ErrorText = "Script.json is not the format wanted (Format)!"; ZipFile.Close(); return(false); } using (FileStream ZipFileOut = File.Open(OutputFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) { using (ZipArchive ZipArchOut = new ZipArchive(ZipFileOut, ZipArchiveMode.Create, true, Encoding.UTF8)) { entry = ZipArch.GetEntry("Script.json"); if (entry == null) { ZipFile.Close(); ErrorText = "Missing Script.json"; return(false); } ZipArchiveEntry newentry = ZipArchOut.CreateEntry("Script.json"); using (Stream newentrystream = newentry.Open()) { using (Stream currententry = entry.Open()) { currententry.CopyTo(newentrystream); } } for (int i = 1; i < 9; i++) { ZipArchiveEntry entry2 = ZipArch.GetEntry("Script.json.sign" + (i == 1 ? "" : i.ToString())); if (entry2 == null) { continue; } newentry = ZipArchOut.CreateEntry("Script.json.sign" + (i == 1 ? "" : i.ToString())); using (Stream newentrystream = newentry.Open()) { using (Stream currententry = entry2.Open()) { currententry.CopyTo(newentrystream); } } } foreach (PKGFile PFile in Package.Files) { if (PFile.KeepInMeta == false) { continue; } entry = ZipArch.GetEntry(PFile.SrcFile); if (entry == null) { ZipFile.Close(); ErrorText = "Missing " + PFile.SrcFile; return(false); } ZipArchiveEntry newentry2 = ZipArchOut.CreateEntry(PFile.SrcFile); using (Stream newentrystream2 = newentry2.Open()) { using (Stream currententry2 = entry.Open()) { currententry2.CopyTo(newentrystream2); } } } } } } } return(true); }
public bool PackageInfo(string Filename, List <byte[]> CerCertificates, out string ErrorText) { ErrorText = ""; bool ScriptSignFailed = false; UpdateText("Initializing"); if (CerCertificates == null) { CerCertificates = new List <byte[]>(); } CerCertificates.Add(Resources.Vulpes_Main); try { if (File.Exists(Filename) == false) { ErrorText = "File does not exist"; return(false); } using (FileStream ZipFile = File.Open(Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (ZipArchive ZipArch = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8)) { ZipArchiveEntry entry; entry = ZipArch.GetEntry("Script.json"); if (entry == null) { ZipFile.Close(); ErrorText = "Missing Script.json"; return(false); } if (TestSignatureBySignFile("Script.json", ZipArch, CerCertificates) == false) { ScriptSignFailed = true; } byte[] packagedata; packagedata = GetZipData(entry); PKGRootData Package; string script = Encoding.UTF8.GetString(packagedata); try { Package = JsonConvert.DeserializeObject <PKGRootData>(script); if (Package.HeaderID != "FoxPackageScriptV1") { ErrorText = "Script.json is not the format wanted (Header)!"; ZipFile.Close(); return(false); } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); ErrorText = "Script.json is not the format wanted (Format)!"; ZipFile.Close(); return(false); } if (CheckScript(Package, ZipArch, false, false, out ErrorText) == false) { return(false); } ErrorText = "Package Details:\n"; ErrorText += "Name: " + Package.Title + "\n"; ErrorText += "ID: " + Package.PackageID + "\n"; ErrorText += "Description: " + Package.Description + "\n"; ErrorText += "Version ID: " + Package.VersionID.ToString() + "\n"; if (ScriptSignFailed == true) { ErrorText += "!QUICK SIGNATURE CHECK FAILED!\n"; } ZipFile.Close(); PackageInfoData = Package; } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); ErrorText = "Internal error, likely a corrupted ZIP file"; return(false); } return(true); }
bool CheckScript(PKGRootData Package, ZipArchive ZipArch, bool WithFiles, bool MetaOnly, out string ErrorText) { try { ErrorText = ""; AbortProcess = false; UpdateText("Checking data"); if (Package.PackageID.Trim() == "") { ErrorText = "Invalid element (PackageID=\"\")"; return(false); } if (Package.Title.Trim() == "") { ErrorText = "Invalid element (Title=\"\")"; return(false); } if (WithFiles == true) { foreach (PKGFile f in Package.Files) { UpdateText("Checking " + f.SrcFile); //Debug.WriteLine("Checking " + f.SrcFile); if (MetaOnly == true) { if (f.KeepInMeta == false) { continue; } } ZipArchiveEntry entry = ZipArch.GetEntry(f.SrcFile); if (entry == null) { ErrorText = "Cannot find file " + f.SrcFile; return(false); } if (f.FileName.Trim() == "") { ErrorText = "Invalid element (Filename=\"\")"; return(false); } if (f.FolderName.Trim() == "") { ErrorText = "Invalid element (FolderName=\"\")"; return(false); } if (AbortProcess == true) { ErrorText = "Aborted"; return(false); } } } return(true); } catch (Exception ee) { Debug.WriteLine(ee.ToString()); ErrorText = "SEH Error"; return(false); } }