From af8499a7997b3e76d988aa39424b8b2fd3ddb305 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Mon, 15 May 2017 15:30:29 +0200 Subject: [PATCH] more sanity checking --- HashingCmd/Program.cs | 31 +++++++++++++++++++------- VectoCommon/VectoHashing/VectoHash.cs | 32 +++++++++++++++++++++------ 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/HashingCmd/Program.cs b/HashingCmd/Program.cs index a9a57e2101..ceb3df8f15 100644 --- a/HashingCmd/Program.cs +++ b/HashingCmd/Program.cs @@ -16,22 +16,25 @@ namespace HashingCmd public delegate void HashingAction(string filename, VectoHash h); private const string Usage = @" -hashingcmd.exe -v <file.xml> +hashingcmd.exe (-h | [-v] [[-s] -x] [-c] [-r]) <file.xml> <file2.xml> <file3.xml> "; private const string Help = @" hashingcmd.exe --h: help +-h: print help -v: verify hashed file --s: create hashed file file +-s: create hashed file +-x: validate generated XML against VECTO XML schema -c: compute hash and write to stdout -r: read hash from file and write to stdout "; static Dictionary<string, HashingAction> actions = new Dictionary<string, HashingAction>(); + static bool _validateXML = false; + static int Main(string[] args) { try { @@ -45,7 +48,16 @@ hashingcmd.exe actions["-r"] = ReadHashAction; actions["-s"] = CreateHashedFileAction; - var fileList = args.Except(actions.Keys); + if (args.Contains("-x")) { + _validateXML = true; + } + + var fileList = args.Except(actions.Keys.Concat(new[] { "-x" })).ToArray(); + if (fileList.Length == 0 || !args.Intersect(actions.Keys.ToArray()).Any()) { + ShowVersionInformation(); + Console.Write(Usage); + return 0; + } foreach (var file in fileList) { WriteLine("processing " + Path.GetFileName(file)); if (!File.Exists(Path.GetFullPath(file))) { @@ -60,6 +72,9 @@ hashingcmd.exe } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.Error.WriteLine(e.Message); + if (e.InnerException != null) { + Console.Error.WriteLine(e.InnerException.Message); + } Console.ResetColor(); } } @@ -115,7 +130,7 @@ hashingcmd.exe } for (var i = 0; i < component.Count; i++) { var readHash = h.ReadHash(component.Entry, i); - WriteLine(" " + component.Entry.XMLElementName() + "\t ... >" + readHash + "<"); + WriteLine(" " + component.Entry.XMLElementName() + "\t ... " + readHash + ""); } } } @@ -135,14 +150,14 @@ hashingcmd.exe } for (var i = 0; i < component.Count; i++) { var computedHash = h.ComputeHash(component.Entry, i); - WriteLine(" " + component.Entry.XMLElementName() + "\t ... >" + computedHash + "<"); + WriteLine(" " + component.Entry.XMLElementName() + "\t ... " + computedHash + ""); } } var jobHash = h.ComputeHash(); - WriteLine(" job file\t ... >" + jobHash + "<"); + WriteLine(" job file\t ... " + jobHash + ""); } else { var hash = h.ComputeHash(); - WriteLine(" computed hash: >" + hash + "<"); + WriteLine(" computed hash: " + hash + ""); } } diff --git a/VectoCommon/VectoHashing/VectoHash.cs b/VectoCommon/VectoHashing/VectoHash.cs index 4ba0be427e..ef31b0c150 100644 --- a/VectoCommon/VectoHashing/VectoHash.cs +++ b/VectoCommon/VectoHashing/VectoHash.cs @@ -19,14 +19,22 @@ namespace TUGraz.VectoHashing public static VectoHash Load(string filename) { var doc = new XmlDocument(); - doc.Load(new XmlTextReader(filename)); + try { + doc.Load(new XmlTextReader(filename)); + } catch (Exception e) { + throw new Exception("failed to read XML document", e); + } return new VectoHash(doc); } public static VectoHash Load(Stream stream) { var doc = new XmlDocument(); - doc.Load(new XmlTextReader(stream)); + try { + doc.Load(new XmlTextReader(stream)); + } catch (Exception e) { + throw new Exception("failed to read XML document", e); + } return new VectoHash(doc); } @@ -77,12 +85,15 @@ namespace TUGraz.VectoHashing if (components.Count > 1) { throw new Exception("input must not contain multiple components!"); } - var query = string.Format("//*[local-name()='{0}']/*[local-name()='Data']", components[0]); + if (components.Count == 0) { + throw new Exception("input does not contain a known component!"); + } + var query = string.Format("//*[local-name()='{0}']/*[local-name()='Data']", components[0].XMLElementName()); var node = Document.SelectSingleNode(query); if (node == null) { - throw new Exception(string.Format("'Data' element for component '{0}' not found!", components[0])); + throw new Exception(string.Format("'Data' element for component '{0}' not found!", components[0].XMLElementName())); } - query = string.Format("//*[local-name()='{0}']/*[local-name()='Signature']", components[0]); + query = string.Format("//*[local-name()='{0}']/*[local-name()='Signature']", components[0].XMLElementName()); var sigNodes = Document.SelectNodes(query); if (sigNodes != null && sigNodes.Count > 0) { throw new Exception("input data already contains a signature element"); @@ -160,8 +171,15 @@ namespace TUGraz.VectoHashing private static string GetHashValue(XmlDocument hashed, string elementToHash) { - var node = hashed.SelectSingleNode("//*[@URI='#" + elementToHash + "']/*[local-name() = 'DigestValue']"); - return node == null ? null : node.InnerText; + //var node = hashed.SelectSingleNode("//*[@URI='#" + elementToHash + "']/*[local-name() = 'DigestValue']"); + var nodes = hashed.SelectNodes("//*[@URI='#" + elementToHash + "']/*[local-name() = 'DigestValue']"); + if (nodes == null || nodes.Count == 0) { + return null; + } + if (nodes.Count > 1) { + throw new Exception("Multiple DigestValue elements found!"); + } + return nodes[0].InnerText; } } } \ No newline at end of file -- GitLab