diff --git a/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.cs b/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.cs index 93d7a641b2d..f998b1381b3 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.cs @@ -40,7 +40,7 @@ static void Main(string[] args) Console.WriteLine("Verifying signature..."); //Verify the XML signature in the XML file. - bool result = VerifyDetachedSignature(XmlFileName); + bool result = VerifyDetachedSignature(XmlFileName, DSAKey); // Display the results of the signature verification to // the console. @@ -99,13 +99,16 @@ public static void SignDetachedResource(string URIString, string XmlSigFileName, // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyDetachedSignature(string XmlSigFileName) + public static Boolean VerifyDetachedSignature(string XmlSigFileName, DSA DSAKey) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(XmlSigFileName); + using (XmlReader reader = XmlReader.Create(XmlSigFileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXMl object. SignedXml signedXml = new SignedXml(); @@ -118,7 +121,7 @@ public static Boolean VerifyDetachedSignature(string XmlSigFileName) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(DSAKey); } // } diff --git a/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.cs b/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.cs index 5d6995a22ee..ba623229f5d 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.cs @@ -31,7 +31,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", DSAKey); // Display the results of the signature verification to \ // the console. @@ -61,7 +61,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, DSA DSAKe doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -113,7 +116,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, DSA DSAKe // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, DSA DSAKey) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -122,7 +125,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -136,7 +142,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(DSAKey); } // diff --git a/snippets/csharp/System.Security.Cryptography.Xml/DataObject/Overview/source1.cs b/snippets/csharp/System.Security.Cryptography.Xml/DataObject/Overview/source1.cs index 24f3ff8ea50..4567f179f92 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/DataObject/Overview/source1.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/DataObject/Overview/source1.cs @@ -12,18 +12,21 @@ public static void Main(String[] args) Console.WriteLine("Verifying " + args[0] + "..."); - // Create a SignedXml. - SignedXml signedXml = new SignedXml(); + RSA trustedKey = RSA.Create(); + // ... load trustedKey from an out-of-band source ... - // Load the XML. XmlDocument xmlDocument = new XmlDocument(); xmlDocument.PreserveWhitespace = true; - xmlDocument.Load(new XmlTextReader(args[0])); + using (XmlReader reader = XmlReader.Create(args[0])) + { + xmlDocument.Load(reader); + } + SignedXml signedXml = new SignedXml(xmlDocument); XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature"); signedXml.LoadXml((XmlElement)nodeList[0]); - if (signedXml.CheckSignature()) { + if (signedXml.CheckSignature(trustedKey)) { Console.WriteLine("Signature check OK"); } else { Console.WriteLine("Signature check FAILED"); diff --git a/snippets/csharp/System.Security.Cryptography.Xml/DataReference/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/DataReference/Overview/sample.cs index f679cf29b54..901e82d41aa 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/DataReference/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/DataReference/Overview/sample.cs @@ -15,7 +15,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.cs index 575e8ee24b1..56d0bb4cb90 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.cs index a921bc77b5b..e2689ac87ef 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.cs index abbe06a87d4..a3d831a964d 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.cs index ff447321ed1..50adb9b8390 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/Project.csproj b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/Project.csproj new file mode 100644 index 00000000000..6d43e9dc6e9 --- /dev/null +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/Project.csproj @@ -0,0 +1,14 @@ + + + + Library + net10.0-windows + true + false + + + + + + + \ No newline at end of file diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.cs index b74e7169ad4..e22214485e0 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.cs index 65e228c42cc..4a09ca96199 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.cs index 38f9a1ce91e..9ab8eab3f6d 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.cs index 6b81abb4395..c57220bb12d 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.cs @@ -17,7 +17,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.cs index 5fb7faf559a..5d93382d180 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.cs index c980e40596f..62caa70da1a 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.cs @@ -16,7 +16,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.cs index 64d405bd2ba..dc07597bb61 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.cs @@ -12,7 +12,7 @@ public static void Main() { } // - public static void CheckSignatureWithEncryptedGrant(string fileName, IRelDecryptor decryptor) + public static void CheckSignatureWithEncryptedGrant(string fileName, IRelDecryptor decryptor, AsymmetricAlgorithm trustedKey) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -22,7 +22,10 @@ public static void CheckSignatureWithEncryptedGrant(string fileName, IRelDecrypt xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(fileName); + using (XmlReader reader = XmlReader.Create(fileName)) + { + xmlDocument.Load(reader); + } nsManager.AddNamespace("dsig", SignedXml.XmlDsigNamespaceUrl); // Find the "Signature" node and create a new XmlNodeList object. @@ -51,7 +54,7 @@ public static void CheckSignatureWithEncryptedGrant(string fileName, IRelDecrypt } // Check the signature and display the result. - bool result = signedXml.CheckSignature(); + bool result = signedXml.CheckSignature(trustedKey); if (result) Console.WriteLine("SUCCESS: CheckSignatureWithEncryptedGrant - issuer index #" + diff --git a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.cs b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.cs index 6a8da594df5..3d682e588d8 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.cs @@ -40,7 +40,7 @@ static void Main(string[] args) Console.WriteLine("Verifying signature..."); //Verify the XML signature in the XML file. - bool result = VerifyDetachedSignature(XmlFileName); + bool result = VerifyDetachedSignature(XmlFileName, Key); // Display the results of the signature verification to // the console. @@ -99,13 +99,16 @@ public static void SignDetachedResource(string URIString, string XmlSigFileName, // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyDetachedSignature(string XmlSigFileName) + public static Boolean VerifyDetachedSignature(string XmlSigFileName, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(XmlSigFileName); + using (XmlReader reader = XmlReader.Create(XmlSigFileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXMl object. SignedXml signedXml = new SignedXml(); @@ -118,7 +121,7 @@ public static Boolean VerifyDetachedSignature(string XmlSigFileName) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // } diff --git a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.cs b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.cs index c4e9ec91dd9..ecda34ff49f 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.cs @@ -32,7 +32,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to \ // the console. @@ -62,7 +62,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -110,7 +113,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -119,7 +122,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -133,7 +139,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // diff --git a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.cs index 90b42dd15e5..8058db83a1e 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.cs @@ -74,7 +74,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, string Su doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -147,7 +150,10 @@ public static Boolean VerifyXmlFile(String FileName, String CertificateSubject) XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(FileName); + using (XmlReader reader = XmlReader.Create(FileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.cs b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.cs index e3b7f1491b8..e4e06f20430 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.cs @@ -50,7 +50,11 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + // Load the passed XML file into the document. + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); diff --git a/snippets/csharp/System.Security.Cryptography.Xml/KeyReference/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/KeyReference/Overview/sample.cs index 9e0e685dadf..e5950d1fa95 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/KeyReference/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/KeyReference/Overview/sample.cs @@ -15,7 +15,10 @@ static void Main(string[] args) try { xmlDoc.PreserveWhitespace = true; - xmlDoc.Load("test.xml"); + using (XmlReader reader = XmlReader.Create("test.xml")) + { + xmlDoc.Load(reader); + } } catch (Exception e) { diff --git a/snippets/csharp/System.Security.Cryptography.Xml/Reference/.ctor/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/Reference/.ctor/sample.cs index 7ae5f3e01ce..66ddb5942d3 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/Reference/.ctor/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/Reference/.ctor/sample.cs @@ -29,7 +29,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to // the console. @@ -72,7 +72,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -124,11 +127,13 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) xmltw.Close(); } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); + if (Key == null) + throw new ArgumentNullException("Key"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -137,7 +142,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -151,7 +159,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } } // \ No newline at end of file diff --git a/snippets/csharp/System.Security.Cryptography.Xml/Signature/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/Signature/Overview/sample.cs index f2fee5bd8c6..204ae313813 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/Signature/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/Signature/Overview/sample.cs @@ -29,7 +29,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to \ // the console. @@ -105,11 +105,13 @@ public static void SignDetachedResource(string URIString, string XmlSigFileName, } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); + if (Key == null) + throw new ArgumentNullException("Key"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -118,7 +120,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -132,7 +137,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } } // \ No newline at end of file diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/Project.csproj b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/Project.csproj new file mode 100644 index 00000000000..6d43e9dc6e9 --- /dev/null +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/Project.csproj @@ -0,0 +1,14 @@ + + + + Library + net10.0-windows + true + false + + + + + + + \ No newline at end of file diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.cs index f680d84492d..4fb8446dbee 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.cs @@ -101,7 +101,10 @@ public static Boolean VerifyDetachedSignature(string XmlSigFileName, RSA Key) XmlDocument xmlDocument = new XmlDocument(); // Load the passedXML file into the document. - xmlDocument.Load(XmlSigFileName); + using (XmlReader reader = XmlReader.Create(XmlSigFileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object. SignedXml signedXml = new SignedXml(); diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.cs index 91dcd58c307..4573fdef109 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.cs @@ -74,7 +74,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, string Su doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -145,7 +148,10 @@ public static Boolean VerifyXmlFile(String FileName, String CertificateSubject) XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(FileName); + using (XmlReader reader = XmlReader.Create(FileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.cs index 389d70a570d..ed37af989ce 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.cs @@ -94,7 +94,10 @@ public static Boolean VerifyDetachedSignature(string XmlSigFileName, KeyedHashAl XmlDocument xmlDocument = new XmlDocument(); // Load the passedXML file into the document. - xmlDocument.Load(XmlSigFileName); + using (XmlReader reader = XmlReader.Create(XmlSigFileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.cs index 72dae0ee257..c47ff39ca37 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.cs @@ -62,7 +62,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, KeyedHash doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -110,7 +113,10 @@ public static Boolean VerifyXmlFile(String Name, KeyedHashAlgorithm Key) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXMl object and pass it // the XMl document class. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.cs index 22fcfc4b640..6e582ce1778 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.cs @@ -5,6 +5,7 @@ // signed XML. // using System; +using System.Linq; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Text; @@ -31,7 +32,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", RSAKey); // Display the results of the signature verification to \ // the console. @@ -61,7 +62,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA RSAKe doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -109,7 +113,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA RSAKe // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA TrustedKey) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -118,7 +122,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -131,8 +138,29 @@ public static Boolean VerifyXmlFile(String Name) // Load the signature node. signedXml.LoadXml((XmlElement)nodeList[0]); - // Check the signature and return the result. - return signedXml.CheckSignature(); + // Verify the signature and retrieve the key that produced it. + AsymmetricAlgorithm signingKey; + bool verified = signedXml.CheckSignatureReturningKey(out signingKey); + + // The returned key is owned by the caller and can hold native + // handles, so dispose it once the comparison is complete. + using (signingKey) + { + if (!verified) + return false; + + // A valid signature only proves possession of some key. + // The caller must confirm the returned key matches a key it trusts. + if (signingKey is RSA rsa) + { + RSAParameters expected = TrustedKey.ExportParameters(false); + RSAParameters actual = rsa.ExportParameters(false); + return expected.Modulus.SequenceEqual(actual.Modulus) + && expected.Exponent.SequenceEqual(actual.Exponent); + } + + return false; + } } // diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/Project.csproj b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/Project.csproj new file mode 100644 index 00000000000..6d43e9dc6e9 --- /dev/null +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/Project.csproj @@ -0,0 +1,14 @@ + + + + Library + net10.0-windows + true + false + + + + + + + \ No newline at end of file diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.cs index 265919c64aa..44016429e76 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.cs @@ -60,7 +60,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) XmlDocument doc = new XmlDocument(); // Load the passed XML file using its name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -109,7 +112,10 @@ public static Boolean VerifyXmlFile(String Name, RSA Key) XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/sample.cs index 47a0664fbe5..4582e04d589 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/sample.cs @@ -31,7 +31,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to // the console. @@ -76,7 +76,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -127,11 +130,13 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, xmltw.Close(); } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); + if (Key == null) + throw new ArgumentNullException("Key"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -140,7 +145,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -154,7 +162,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } } // \ No newline at end of file diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.cs index 09d142d0e56..a2d77740f82 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.cs @@ -32,7 +32,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to \ // the console. @@ -61,7 +61,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -114,7 +117,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) xmltw.Close(); } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -123,7 +126,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -137,7 +143,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // Create example data to sign. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.cs index db6ec649fcd..139faae4401 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.cs @@ -32,7 +32,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to // the console. @@ -61,7 +61,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -114,7 +117,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) xmltw.Close(); } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -123,7 +126,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -137,7 +143,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // Create example data to sign. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/Project.csproj b/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/Project.csproj new file mode 100644 index 00000000000..b62ae7deede --- /dev/null +++ b/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/Project.csproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + diff --git a/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.cs index a8ea02121a0..ddba2e6f3ed 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.cs @@ -74,7 +74,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, string Su doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -103,7 +106,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, string Su KeyInfoX509Data kdata = new KeyInfoX509Data(cert); - X509IssuerSerial xserial; + X509IssuerSerial xserial = default; xserial.IssuerName = cert.IssuerName.ToString(); xserial.SerialNumber = cert.SerialNumber; @@ -157,7 +160,10 @@ public static Boolean VerifyXmlFile(String FileName, String CertificateSubject) XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(FileName); + using (XmlReader reader = XmlReader.Create(FileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.cs b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.cs index 8b6fea65c06..4b16b1d88c1 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.cs @@ -40,7 +40,7 @@ static void Main(string[] args) Console.WriteLine("Verifying signature..."); //Verify the XML signature in the XML file. - bool result = VerifyDetachedSignature(XmlFileName); + bool result = VerifyDetachedSignature(XmlFileName, Key); // Display the results of the signature verification to // the console. @@ -99,13 +99,16 @@ public static void SignDetachedResource(string URIString, string XmlSigFileName, // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyDetachedSignature(string XmlSigFileName) + public static Boolean VerifyDetachedSignature(string XmlSigFileName, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Load the passed XML file into the document. - xmlDocument.Load(XmlSigFileName); + using (XmlReader reader = XmlReader.Create(XmlSigFileName)) + { + xmlDocument.Load(reader); + } // Create a new SignedXMl object. SignedXml signedXml = new SignedXml(); @@ -118,7 +121,7 @@ public static Boolean VerifyDetachedSignature(string XmlSigFileName) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // } diff --git a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.cs b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.cs index 99d57ff1691..6c7fe332b8c 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.cs @@ -30,7 +30,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to \ // the console. @@ -60,7 +60,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -108,7 +111,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) // // // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -117,7 +120,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -131,7 +137,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // diff --git a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.cs index 82433826768..27fb0e29a88 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.cs @@ -32,7 +32,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to \ // the console. @@ -65,7 +65,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -111,7 +114,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, xmltw.Close(); } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -120,7 +123,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -134,7 +140,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // Create the XML that represents the transform. diff --git a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.cs b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.cs index 4e6bd00b09f..50653240b09 100644 --- a/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.cs +++ b/snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.cs @@ -42,7 +42,7 @@ public static void Main(String[] args) // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); - bool result = VerifyXmlFile("SignedExample.xml"); + bool result = VerifyXmlFile("SignedExample.xml", Key); // Display the results of the signature verification to \ // the console. @@ -75,7 +75,10 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, doc.PreserveWhitespace = false; // Load the passed XML file using it's name. - doc.Load(new XmlTextReader(FileName)); + using (XmlReader reader = XmlReader.Create(FileName)) + { + doc.Load(reader); + } // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); @@ -125,7 +128,7 @@ public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, xmltw.Close(); } // Verify the signature of an XML file and return the result. - public static Boolean VerifyXmlFile(String Name) + public static Boolean VerifyXmlFile(String Name, RSA Key) { // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); @@ -134,7 +137,10 @@ public static Boolean VerifyXmlFile(String Name) xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. - xmlDocument.Load(Name); + using (XmlReader reader = XmlReader.Create(Name)) + { + xmlDocument.Load(reader); + } // Create a new SignedXml object and pass it // the XML document class. @@ -148,7 +154,7 @@ public static Boolean VerifyXmlFile(String Name) signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. - return signedXml.CheckSignature(); + return signedXml.CheckSignature(Key); } // Create the XML that represents the transform. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.vb index cc6d1577fe3..f6e242e4c04 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.vb @@ -37,7 +37,7 @@ Class XMLDSIGDetached Console.WriteLine("Verifying signature...") 'Verify the XML signature in the XML file. - Dim result As Boolean = VerifyDetachedSignature(XmlFileName) + Dim result As Boolean = VerifyDetachedSignature(XmlFileName, DSAKey) ' Display the results of the signature verification to ' the console. @@ -94,12 +94,14 @@ Class XMLDSIGDetached ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyDetachedSignature(XmlSigFileName As String) As [Boolean] + Public Shared Function VerifyDetachedSignature(XmlSigFileName As String, DSAKey As DSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(XmlSigFileName) + Using reader As XmlReader = XmlReader.Create(XmlSigFileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXMl object. Dim signedXml As New SignedXml() @@ -112,7 +114,7 @@ Class XMLDSIGDetached signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(DSAKey) End Function ' diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.vb index 8c9caa77dfc..90233879627 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.vb @@ -30,7 +30,7 @@ Public Class SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", DSAKey) ' Display the results of the signature verification to \ ' the console. @@ -55,7 +55,9 @@ Public Class SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -109,7 +111,7 @@ Public Class SignVerifyEnvelope ' ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyXmlFile(Name As [String]) As [Boolean] + Public Shared Function VerifyXmlFile(Name As [String], DSAKey As DSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -117,7 +119,9 @@ Public Class SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -131,7 +135,7 @@ Public Class SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(DSAKey) End Function ' diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/source1.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/source1.vb index 769bbf3df49..22cb987e9a1 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/source1.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DataObject/Overview/source1.vb @@ -1,33 +1,31 @@ ' +Imports System Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml -Imports System.IO Imports System.Xml - _ - Public Class Verify - - Public Shared Sub Main(args() As [String]) - - Console.WriteLine(("Verifying " + args(0) + "...")) - - ' Create a SignedXml. - Dim signedXml As New SignedXml() - - ' Load the XML. - Dim xmlDocument As New XmlDocument() - xmlDocument.PreserveWhitespace = True - xmlDocument.Load(New XmlTextReader(args(0))) - - Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature") - signedXml.LoadXml(CType(nodeList(0), XmlElement)) - - If signedXml.CheckSignature() Then - Console.WriteLine("Signature check OK") - Else - Console.WriteLine("Signature check FAILED") - End If - End Sub + Public Shared Sub Main(ByVal args As String()) + Console.WriteLine("Verifying " & args(0) & "...") + + Dim trustedKey As RSA = RSA.Create() + ' ... load trustedKey from an out-of-band source ... + + Dim xmlDocument As New XmlDocument() + xmlDocument.PreserveWhitespace = True + Using reader As XmlReader = XmlReader.Create(args(0)) + xmlDocument.Load(reader) + End Using + + Dim signedXml As New SignedXml(xmlDocument) + Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature") + signedXml.LoadXml(CType(nodeList(0), XmlElement)) + + If signedXml.CheckSignature(trustedKey) Then + Console.WriteLine("Signature check OK") + Else + Console.WriteLine("Signature check FAILED") + End If + End Sub End Class -' \ No newline at end of file +' diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/sample.vb index c71e58c26cc..ccb9701129f 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/DataReference/Overview/sample.vb @@ -14,7 +14,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.vb index bf93d63833c..befe01b6742 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.vb index c8463c679cf..808427ae5e9 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.vb index 3a4f395a47c..c372af3051e 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.vb index 97f599523b6..96174b7fa04 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/Project.vbproj new file mode 100644 index 00000000000..b8426703b93 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/Project.vbproj @@ -0,0 +1,14 @@ + + + + Library + net10.0-windows + true + false + + + + + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.vb index cf51f3b73cf..c697fea10e2 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.vb @@ -14,7 +14,9 @@ Module Program ' Load an XML file into the XmlDocument object. xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using ' Create a new TripleDES key. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.vb index cc2e679276b..d3aa1ac4993 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) Return diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.vb index 89c92314c63..3a8a99bc4c6 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) Return diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.vb index f1cb89111bd..49580a6a17f 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.vb @@ -16,7 +16,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) Return diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.vb index a4895989b63..eb20a28ae2a 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) Return diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.vb index ede0bf7cdd5..c99f4ca7998 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.vb @@ -15,7 +15,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.vb index 291d139584e..cf5c8d9a8cc 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.vb @@ -13,7 +13,7 @@ Class XmlLicenseTransformExample ' - Public Shared Sub CheckSignatureWithEncryptedGrant(ByVal fileName As String, ByVal decryptor As IRelDecryptor) + Public Shared Sub CheckSignatureWithEncryptedGrant(ByVal fileName As String, ByVal decryptor As IRelDecryptor, ByVal trustedKey As AsymmetricAlgorithm) ' Create a new XML document. Dim xmlDocument As New XmlDocument() Dim nsManager As New XmlNamespaceManager(xmlDocument.NameTable) @@ -22,7 +22,9 @@ Class XmlLicenseTransformExample xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(fileName) + Using reader As XmlReader = XmlReader.Create(fileName) + xmlDocument.Load(reader) + End Using nsManager.AddNamespace("dsig", SignedXml.XmlDsigNamespaceUrl) ' Find the "Signature" node and create a new XmlNodeList object. @@ -54,7 +56,7 @@ Class XmlLicenseTransformExample End If ' Check the signature and display the result. - Dim result As Boolean = signedXml.CheckSignature() + Dim result As Boolean = signedXml.CheckSignature(trustedKey) If result Then Console.WriteLine("SUCCESS: CheckSignatureWithEncryptedGrant - issuer index #" + i.ToString()) diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.vb index 2a3b5029b1d..9cce433fa85 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.vb @@ -37,7 +37,7 @@ Class XMLDSIGDetached Console.WriteLine("Verifying signature...") 'Verify the XML signature in the XML file. - Dim result As Boolean = VerifyDetachedSignature(XmlFileName) + Dim result As Boolean = VerifyDetachedSignature(XmlFileName, Key) ' Display the results of the signature verification to ' the console. @@ -92,12 +92,14 @@ Class XMLDSIGDetached ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyDetachedSignature(XmlSigFileName As String) As [Boolean] + Public Shared Function VerifyDetachedSignature(XmlSigFileName As String, Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(XmlSigFileName) + Using reader As XmlReader = XmlReader.Create(XmlSigFileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXMl object. Dim signedXml As New SignedXml() @@ -110,7 +112,7 @@ Class XMLDSIGDetached signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function ' End Class diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.vb index 550438b84a5..96a40b85937 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.vb @@ -30,7 +30,7 @@ Public Class SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to \ ' the console. @@ -55,7 +55,9 @@ Public Class SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -104,7 +106,7 @@ Public Class SignVerifyEnvelope ' ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyXmlFile(Name As [String]) As [Boolean] + Public Shared Function VerifyXmlFile(Name As [String], Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -112,7 +114,9 @@ Public Class SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -126,7 +130,7 @@ Public Class SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function ' diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.vb index 34d2b288cd2..051474d237d 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.vb @@ -70,7 +70,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -146,7 +148,9 @@ Module SignVerifyEnvelope Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(FileName) + Using reader As XmlReader = XmlReader.Create(FileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.vb index ad403dd3258..7fae4b56afb 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.vb @@ -47,7 +47,9 @@ Public Class SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/sample.vb index 6a046b72cda..718a8b8f4df 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/KeyReference/Overview/sample.vb @@ -14,7 +14,9 @@ Module Program ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True - xmlDoc.Load("test.xml") + Using reader As XmlReader = XmlReader.Create("test.xml") + xmlDoc.Load(reader) + End Using Catch e As Exception Console.WriteLine(e.Message) End Try diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/sample.vb index 5ae07f731d4..3b280eca685 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/Reference/.ctor/sample.vb @@ -27,7 +27,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to ' the console. @@ -67,7 +67,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -121,11 +123,14 @@ Module SignVerifyEnvelope End Sub ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If + If Key Is Nothing Then + Throw New ArgumentNullException("Key") + End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -133,7 +138,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -147,7 +154,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function End Module diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/sample.vb index f901920a96f..2c2c823a477 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/Signature/Overview/sample.vb @@ -27,7 +27,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to \ ' the console. @@ -102,11 +102,14 @@ Module SignVerifyEnvelope ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If + If Key Is Nothing Then + Throw New ArgumentNullException("Key") + End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -114,7 +117,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -128,7 +133,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function End Module diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/Project.vbproj new file mode 100644 index 00000000000..b8426703b93 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/Project.vbproj @@ -0,0 +1,14 @@ + + + + Library + net10.0-windows + true + false + + + + + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.vb index 0948b07b866..ac2c7abd76b 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.vb @@ -96,7 +96,9 @@ Class XMLDSIGDetached Dim xmlDocument As New XmlDocument() ' Load the passedXML file into the document. - xmlDocument.Load(XmlSigFileName) + Using reader As XmlReader = XmlReader.Create(XmlSigFileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object. Dim signedXml As New SignedXml() diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.vb index 917028ee727..5ce32ad4f29 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.vb @@ -70,7 +70,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -144,7 +146,9 @@ Module SignVerifyEnvelope Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(FileName) + Using reader As XmlReader = XmlReader.Create(FileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.vb index 69112ec7e38..f58fb0ddff8 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.vb @@ -87,7 +87,9 @@ Class XMLDSIGDetached Dim xmlDocument As New XmlDocument() ' Load the passedXML file into the document. - xmlDocument.Load(XmlSigFileName) + Using reader As XmlReader = XmlReader.Create(XmlSigFileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.vb index b8d599ed0ff..e78e570fa1c 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.vb @@ -57,7 +57,9 @@ Public Class SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -105,7 +107,9 @@ Public Class SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXMl object and pass it ' the XMl document class. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.vb index 90057710cda..7cea299a8f6 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.vb @@ -4,6 +4,7 @@ ' envelope signature. It then verifies the ' signed XML. ' +Imports System.Linq Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Imports System.Text @@ -30,7 +31,7 @@ Public Class SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", RSAKey) ' Display the results of the signature verification to \ ' the console. @@ -55,7 +56,9 @@ Public Class SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -105,7 +108,7 @@ Public Class SignVerifyEnvelope ' ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyXmlFile(Name As [String]) As [Boolean] + Public Shared Function VerifyXmlFile(Name As [String], TrustedKey As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -113,7 +116,9 @@ Public Class SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -126,8 +131,28 @@ Public Class SignVerifyEnvelope ' Load the signature node. signedXml.LoadXml(CType(nodeList(0), XmlElement)) - ' Check the signature and return the result. - Return signedXml.CheckSignature() + ' Verify the signature and retrieve the key that produced it. + Dim signingKey As AsymmetricAlgorithm = Nothing + Dim verified As Boolean = signedXml.CheckSignatureReturningKey(signingKey) + + ' The returned key is owned by the caller and can hold native + ' handles, so dispose it once the comparison is complete. + Using signingKey + If Not verified Then + Return False + End If + + ' A valid signature only proves possession of some key. + ' The caller must confirm the returned key matches a key it trusts. + If TypeOf signingKey Is RSA Then + Dim rsa As RSA = CType(signingKey, RSA) + Dim expected As RSAParameters = TrustedKey.ExportParameters(False) + Dim actual As RSAParameters = rsa.ExportParameters(False) + Return expected.Modulus.SequenceEqual(actual.Modulus) AndAlso expected.Exponent.SequenceEqual(actual.Exponent) + End If + + Return False + End Using End Function ' diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/Project.vbproj new file mode 100644 index 00000000000..b8426703b93 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/Project.vbproj @@ -0,0 +1,14 @@ + + + + Library + net10.0-windows + true + false + + + + + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.vb index 9937ec960d5..85499abbcd8 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.vb @@ -54,7 +54,9 @@ Public Class SignVerifyEnvelope Dim doc As New XmlDocument() ' Load the passed XML file using its name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -102,7 +104,9 @@ Public Class SignVerifyEnvelope Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/sample.vb index 986a166206c..9e421838880 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/Overview/sample.vb @@ -30,7 +30,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to \ ' the console. @@ -72,7 +72,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -125,11 +127,14 @@ Module SignVerifyEnvelope End Sub ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If + If Key Is Nothing Then + Throw New ArgumentNullException("Key") + End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -137,7 +142,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -151,7 +158,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function End Module diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.vb index 36f55c48fd7..2b60f1246a6 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.vb @@ -31,7 +31,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to ' the console. @@ -57,7 +57,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -113,7 +115,7 @@ Module SignVerifyEnvelope End Sub ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -121,7 +123,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -135,7 +139,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.vb index 5d5ca881553..da4f1dc5e80 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.vb @@ -31,7 +31,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to ' the console. @@ -56,7 +56,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -112,7 +114,7 @@ Module SignVerifyEnvelope End Sub ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -120,7 +122,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -134,7 +138,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.vb index 89a3e367b0e..89321b8fde7 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.vb @@ -70,7 +70,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -156,7 +158,9 @@ Module SignVerifyEnvelope Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(FileName) + Using reader As XmlReader = XmlReader.Create(FileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.vb index 2ab397eaf48..21cbd730ffc 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.vb @@ -35,7 +35,7 @@ Class XMLDSIGDetached Console.WriteLine("Verifying signature...") 'Verify the XML signature in the XML file. - Dim result As Boolean = VerifyDetachedSignature(XmlFileName) + Dim result As Boolean = VerifyDetachedSignature(XmlFileName, Key) ' Display the results of the signature verification to ' the console. @@ -89,12 +89,14 @@ Class XMLDSIGDetached ' ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyDetachedSignature(XmlSigFileName As String) As [Boolean] + Public Shared Function VerifyDetachedSignature(XmlSigFileName As String, Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Load the passed XML file into the document. - xmlDocument.Load(XmlSigFileName) + Using reader As XmlReader = XmlReader.Create(XmlSigFileName) + xmlDocument.Load(reader) + End Using ' Create a new SignedXMl object. Dim signedXml As New SignedXml() @@ -107,7 +109,7 @@ Class XMLDSIGDetached signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function ' End Class diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.vb index d4bda6a9e78..23a6fead846 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.vb @@ -26,7 +26,7 @@ Public Class SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to \ ' the console. @@ -51,7 +51,9 @@ Public Class SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -101,7 +103,7 @@ Public Class SignVerifyEnvelope ' ' ' Verify the signature of an XML file and return the result. - Public Shared Function VerifyXmlFile(Name As [String]) As [Boolean] + Public Shared Function VerifyXmlFile(Name As [String], Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -109,7 +111,9 @@ Public Class SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -123,7 +127,7 @@ Public Class SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function ' diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.vb index d63c482deb5..fa5f1c32d69 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.vb @@ -31,7 +31,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to \ ' the console. @@ -58,7 +58,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -105,7 +107,7 @@ Module SignVerifyEnvelope End Sub ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -113,7 +115,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -127,7 +131,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/Project.vbproj b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/Project.vbproj new file mode 100644 index 00000000000..5ca874772d9 --- /dev/null +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/Project.vbproj @@ -0,0 +1,9 @@ + + + + Library + net10.0-windows + true + + + \ No newline at end of file diff --git a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.vb b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.vb index 3951be72531..1e76712163d 100644 --- a/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.vb +++ b/snippets/visualbasic/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.vb @@ -33,7 +33,7 @@ Module SignVerifyEnvelope ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") - Dim result As Boolean = VerifyXmlFile("SignedExample.xml") + Dim result As Boolean = VerifyXmlFile("SignedExample.xml", Key) ' Display the results of the signature verification to \ ' the console. @@ -60,7 +60,9 @@ Module SignVerifyEnvelope doc.PreserveWhitespace = False ' Load the passed XML file using it's name. - doc.Load(New XmlTextReader(FileName)) + Using reader As XmlReader = XmlReader.Create(FileName) + doc.Load(reader) + End Using ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) @@ -111,7 +113,7 @@ Module SignVerifyEnvelope End Sub ' Verify the signature of an XML file and return the result. - Function VerifyXmlFile(ByVal Name As String) As [Boolean] + Function VerifyXmlFile(ByVal Name As String, Key As RSA) As [Boolean] ' Create a new XML document. Dim xmlDocument As New XmlDocument() @@ -119,7 +121,9 @@ Module SignVerifyEnvelope xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. - xmlDocument.Load(Name) + Using reader As XmlReader = XmlReader.Create(Name) + xmlDocument.Load(reader) + End Using ' Create a new SignedXml object and pass it ' the XML document class. @@ -133,7 +137,7 @@ Module SignVerifyEnvelope signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. - Return signedXml.CheckSignature() + Return signedXml.CheckSignature(Key) End Function