From 456765861fac4d47eb238b4c8114649c3253031c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rn=20=C3=85ne=20de=20Jong?= Date: Sat, 13 Aug 2022 19:44:41 +0000 Subject: [PATCH] Add XSD validation --- EduroamConfigure/EapConfig.cs | 25 +- EduroamConfigure/EduroamConfigure.csproj | 11 + .../Properties/Resources.Designer.cs | 85 ++++++ EduroamConfigure/Properties/Resources.resx | 124 ++++++++ .../Resources/EapConfigModified.xsd | 286 ++++++++++++++++++ 5 files changed, 528 insertions(+), 3 deletions(-) create mode 100644 EduroamConfigure/Properties/Resources.Designer.cs create mode 100644 EduroamConfigure/Properties/Resources.resx create mode 100644 EduroamConfigure/Resources/EapConfigModified.xsd diff --git a/EduroamConfigure/EapConfig.cs b/EduroamConfigure/EapConfig.cs index 9a51a82f..17498740 100644 --- a/EduroamConfigure/EapConfig.cs +++ b/EduroamConfigure/EapConfig.cs @@ -7,6 +7,7 @@ using System.Security.Cryptography.X509Certificates; using System.Xml; using System.Xml.Linq; +using System.Xml.Schema; namespace EduroamConfigure { @@ -15,6 +16,10 @@ namespace EduroamConfigure /// public class EapConfig { + #region Constants + private static readonly XmlSchemaSet EAP_CONFIG_SCHEMA; + #endregion + #region Properties public bool IsOauth { get; set; } // TODO: Setter used for scaffolding to PersistenStorage, need better solution @@ -47,6 +52,11 @@ private EapConfig( RawOriginalEapConfigXmlData = eapConfigXmlData; } + static EapConfig() { + var xsdReader = new StringReader(Properties.Resources.EapConfigModified); + EAP_CONFIG_SCHEMA = new XmlSchemaSet(); + EAP_CONFIG_SCHEMA.Add("", XmlReader.Create(xsdReader)); + } #endregion @@ -536,11 +546,12 @@ public static EapConfig FromXmlData(string eapConfigXmlData) static Func nameIs(string name) => // shorthand lambda element => element.Name.LocalName == name; - // load the XML file into a XElement object - XElement eapConfigXml; + XDocument eapConfigXml; try { - eapConfigXml = XElement.Parse(eapConfigXmlData); + var xmlReader = new StringReader(eapConfigXmlData); + eapConfigXml = XDocument.Load(XmlReader.Create(xmlReader)); + eapConfigXml.Validate(EAP_CONFIG_SCHEMA, ValidationEventHandler); } catch (XmlException) { @@ -710,6 +721,14 @@ static Func nameIs(string name) => // shorthand lambda ); } + static void ValidationEventHandler(object sender, ValidationEventArgs e) + { + XmlSeverityType type = XmlSeverityType.Warning; + if (Enum.TryParse("Error", out type)) + { + if (type == XmlSeverityType.Error) throw new XmlException(e.Message); + } + } /// /// Yields EapAuthMethodInstallers which will attempt to install eapConfig for you. /// Refer to frmSummary.InstallEapConfig to see how to use it (TODO: actually explain when finalized) diff --git a/EduroamConfigure/EduroamConfigure.csproj b/EduroamConfigure/EduroamConfigure.csproj index 8ecc4b2f..f659527a 100644 --- a/EduroamConfigure/EduroamConfigure.csproj +++ b/EduroamConfigure/EduroamConfigure.csproj @@ -61,6 +61,11 @@ + + True + True + Resources.resx + @@ -71,6 +76,9 @@ + + Designer + @@ -88,5 +96,8 @@ 5.0.0 + + + \ No newline at end of file diff --git a/EduroamConfigure/Properties/Resources.Designer.cs b/EduroamConfigure/Properties/Resources.Designer.cs new file mode 100644 index 00000000..5542e9bc --- /dev/null +++ b/EduroamConfigure/Properties/Resources.Designer.cs @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace EduroamConfigure.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("EduroamConfigure.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to <?xml version="1.0" encoding="UTF-8"?> + ///<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> + /// <xs:complexType name="VendorSpecificExtension"> + /// <xs:sequence> + /// <xs:any namespace="##other"/> + /// </xs:sequence> + /// <xs:attribute name="vendor" type="xs:int" use="required"/> + /// </xs:complexType> + /// + /// <xs:complexType name="TypeSpecificExtension"> + /// <xs:sequence> + /// <xs:any namespace="##other"/> + /// </xs:sequence> + /// [rest of string was truncated]";. + /// + internal static string EapConfigModified { + get { + return ResourceManager.GetString("EapConfigModified", resourceCulture); + } + } + } +} diff --git a/EduroamConfigure/Properties/Resources.resx b/EduroamConfigure/Properties/Resources.resx new file mode 100644 index 00000000..297d19b1 --- /dev/null +++ b/EduroamConfigure/Properties/Resources.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\EapConfigModified.xsd;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;us-ascii + + \ No newline at end of file diff --git a/EduroamConfigure/Resources/EapConfigModified.xsd b/EduroamConfigure/Resources/EapConfigModified.xsd new file mode 100644 index 00000000..e4b3db09 --- /dev/null +++ b/EduroamConfigure/Resources/EapConfigModified.xsd @@ -0,0 +1,286 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + PAP + + + + + MSCHAP + + + + + MSCHAPv2 + + + + + + + + + + + Temporal Key Integrity Protocol (if used, crypto settings + "WPA/TKIP", "WPA2/TKIP" and "WPA2/AES" and possible future + protos are acceptable). + + + + + + + CTR with CBC-MAC Protocol (if used, only crypto setting + "WPA2/AES" and possible future protos are acceptable). + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Not all EAP types and non-EAP authentication methods need or + + support all types of credentials in the list below. While the + + Schema allows to put all kinds of credential information inside + + every AuthenticationMethod, even where the information is not + + applicable, tags which are not applicable for an authentication + EAP or non-EAP type + SHOULD NOT be included in the corresponding instance of + AuthenticationMethod or InnerAuthenticationMethod when + producing the XML file, and + MUST be ignored by the entity consuming the XML file if + present in the XML file. + + + + + + + + + + + + + + + + + + + + + + + Not all EAP types and non-EAP authentication methods need or + + support all types of credentials in the list below. While the + + Schema allows to put all kinds of credential information inside + + every AuthenticationMethod, even where the information is not + + applicable, tags which are not applicable for an authentication + EAP or non-EAP type + SHOULD NOT be included in the corresponding instance of + AuthenticationMethod or InnerAuthenticationMethod when + producing the XML file, and + MUST be ignored by the entity consuming the XML file if + present in the XML file. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The conditions inside this element are considered AND conditions. + It does e.g. not make sense to have multiple SSIDs in one + + IEEE80211-Properties field because the condition would never + + match. To specify multiple ORed network properties, use multiple + IEEE80211-Properties instances. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +