XmlDsigEnvelopedSignatureTransform Class
Represents the enveloped signature transform for an XML digital signature as defined by the W3C.
System.Security.Cryptography.Xml::Transform
System.Security.Cryptography.Xml::XmlDsigEnvelopedSignatureTransform
Assembly: System.Security (in System.Security.dll)
The XmlDsigEnvelopedSignatureTransform type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | XmlDsigEnvelopedSignatureTransform() | Initializes a new instance of the XmlDsigEnvelopedSignatureTransform class. |
![]() | XmlDsigEnvelopedSignatureTransform(Boolean) | Infrastructure. Initializes a new instance of the XmlDsigEnvelopedSignatureTransform class with comments, if specified. |
| Name | Description | |
|---|---|---|
![]() | Algorithm | Gets or sets the Uniform Resource Identifier (URI) that identifies the algorithm performed by the current transform. (Inherited from Transform.) |
![]() | Context | Gets or sets an XmlElement object that represents the document context under which the current Transform object is running. (Inherited from Transform.) |
![]() | InputTypes | Gets an array of types that are valid inputs to the LoadInput method of the current XmlDsigEnvelopedSignatureTransform object. (Overrides Transform::InputTypes.) |
![]() | OutputTypes | Gets an array of types that are possible outputs from the GetOutput methods of the current XmlDsigEnvelopedSignatureTransform object. (Overrides Transform::OutputTypes.) |
![]() | PropagatedNamespaces | Gets or sets a Hashtable object that contains the namespaces that are propagated into the signature. (Inherited from Transform.) |
![]() | Resolver | Sets the current XmlResolver object. (Inherited from Transform.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetDigestedOutput | When overridden in a derived class, returns the digest associated with a Transform object. (Inherited from Transform.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetInnerXml | Returns an XML representation of the parameters of an XmlDsigEnvelopedSignatureTransform object that are suitable to be included as subelements of an XMLDSIG <Transform> element. (Overrides Transform::GetInnerXml().) |
![]() | GetOutput() | Returns the output of the current XmlDsigEnvelopedSignatureTransform object. (Overrides Transform::GetOutput().) |
![]() | GetOutput(Type) | Returns the output of the current XmlDsigEnvelopedSignatureTransform object of type XmlNodeList. (Overrides Transform::GetOutput(Type).) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetXml | Returns the XML representation of the current Transform object. (Inherited from Transform.) |
![]() | LoadInnerXml | Parses the specified XmlNodeList as transform-specific content of a <Transform> element and configures the internal state of the current XmlDsigEnvelopedSignatureTransform object to match the <Transform> element. (Overrides Transform::LoadInnerXml(XmlNodeList).) |
![]() | LoadInput | Loads the specified input into the current XmlDsigEnvelopedSignatureTransform object. (Overrides Transform::LoadInput(Object).) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
The XmlDsigEnvelopedSignatureTransform class removes the <Signature> element from an XML document before the digest is computed. With this transform, you can sign and verify all elements of an XML document except the XML digital signature elements.
Although you can create and insert a signature into an XML document, verifying the modified document using the embedded signature will fail because the document now has additional elements. This transform removes the <Signature> element and allows you to verify the document using its original form.
Use the XmlDsigEnvelopedSignatureTransform class whenever you create an enveloped signature.
For more information about the enveloped signature transform, see Section 6.6.4 of the XMLDSIG specification, which is available from the W3C at www.w3.org/TR/xmldsig-core/.
Note |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
This section contains two code examples. The first example demonstrates how to sign an XML file using an envelope signature. The second example demonstrates how to use members of the XmlDsigEnvelopedSignatureTransform class.
Example #1
// // This example signs an XML file using an // envelope signature. It then verifies the // signed XML. // #using <System.Security.dll> #using <System.Xml.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::X509Certificates; using namespace System::Security::Cryptography::Xml; using namespace System::Text; using namespace System::Xml; // Sign an XML file and save the signature in a new file. void SignXmlFile( String^ FileName, String^ SignedFileName, RSA^ Key ) { // Create a new XML document. XmlDocument^ doc = gcnew XmlDocument; // Format the document to ignore white spaces. doc->PreserveWhitespace = false; // Load the passed XML file using its name. doc->Load( gcnew XmlTextReader( FileName ) ); // Create a SignedXml object. SignedXml^ signedXml = gcnew SignedXml( doc ); // Add the key to the SignedXml document. signedXml->SigningKey = Key; // Create a reference to be signed. Reference^ reference = gcnew Reference; reference->Uri = ""; // Add an enveloped transformation to the reference. XmlDsigEnvelopedSignatureTransform^ env = gcnew XmlDsigEnvelopedSignatureTransform; reference->AddTransform( env ); // Add the reference to the SignedXml object. signedXml->AddReference( reference ); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo^ keyInfo = gcnew KeyInfo; keyInfo->AddClause( gcnew RSAKeyValue( safe_cast<RSA^>(Key) ) ); signedXml->KeyInfo = keyInfo; // Compute the signature. signedXml->ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement^ xmlDigitalSignature = signedXml->GetXml(); // Append the element to the XML document. doc->DocumentElement->AppendChild( doc->ImportNode( xmlDigitalSignature, true ) ); if ( (doc->FirstChild)->GetType() == XmlDeclaration::typeid ) { doc->RemoveChild( doc->FirstChild ); } // Save the signed XML document to a file specified // using the passed string. XmlTextWriter^ xmltw = gcnew XmlTextWriter( SignedFileName,gcnew UTF8Encoding( false ) ); doc->WriteTo( xmltw ); xmltw->Close(); } // Verify the signature of an XML file and return the result. Boolean VerifyXmlFile( String^ Name ) { // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument; // Format using white spaces. xmlDocument->PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument->Load( Name ); // Create a new SignedXml object and pass it // the XML document class. SignedXml^ signedXml = gcnew SignedXml( xmlDocument ); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName( "Signature" ); // Load the signature node. signedXml->LoadXml( safe_cast<XmlElement^>(nodeList->Item( 0 )) ); // Check the signature and return the result. return signedXml->CheckSignature(); } // Create example data to sign. void CreateSomeXml( String^ FileName ) { // Create a new XmlDocument object. XmlDocument^ document = gcnew XmlDocument; // Create a new XmlNode object. XmlNode^ node = document->CreateNode( XmlNodeType::Element, "", "MyElement", "samples" ); // Add some text to the node. node->InnerText = "Example text to be signed."; // Append the node to the document. document->AppendChild( node ); // Save the XML document to the file name specified. XmlTextWriter^ xmltw = gcnew XmlTextWriter( FileName,gcnew UTF8Encoding( false ) ); document->WriteTo( xmltw ); xmltw->Close(); } int main() { try { // Generate a signing key. RSACryptoServiceProvider^ Key = gcnew RSACryptoServiceProvider; // Create an XML file to sign. CreateSomeXml( "Example.xml" ); Console::WriteLine( "New XML file created." ); // Sign the XML that was just created and save it in a // new file. SignXmlFile( "Example.xml", "SignedExample.xml", Key ); Console::WriteLine( "XML file signed." ); // Verify the signature of the signed XML. Console::WriteLine( "Verifying signature..." ); bool result = VerifyXmlFile( "SignedExample.xml" ); // Display the results of the signature verification to // the console. if ( result ) { Console::WriteLine( "The XML signature is valid." ); } else { Console::WriteLine( "The XML signature is not valid." ); } } catch ( CryptographicException^ e ) { Console::WriteLine( e->Message ); } }
Example #2
#using <System.dll> #using <System.Xml.dll> #using <System.Security.dll> using namespace System; using namespace System::IO; using namespace System::Xml; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::Security::Cryptography::X509Certificates; public ref class EnvelopedSignatureSample { private: static String^ Certificate = "..\\..\\my509.cer"; // Encrypt the text in the specified XmlDocument. public: static void ShowTransformProperties(XmlDocument^ xmlDoc) { XmlDsigEnvelopedSignatureTransform^ xmlTransform = gcnew XmlDsigEnvelopedSignatureTransform(); // Ensure the transform is using the proper algorithm. xmlTransform->Algorithm = SignedXml::XmlDsigEnvelopedSignatureTransformUrl; // Retrieve the XML representation of the current transform. XmlElement^ xmlInTransform = xmlTransform->GetXml(); Console::WriteLine("\nXml representation of the current transform: "); Console::WriteLine(xmlInTransform->OuterXml); // Retrieve the valid input types for the current transform. array<Type^>^ validInTypes = xmlTransform->InputTypes; // Verify the xmlTransform can accept the XMLDocument as an // input type. for (int i = 0; i < validInTypes->Length; i++) { if (validInTypes[i] == xmlDoc->GetType()) { // Load the document into the transfrom. xmlTransform->LoadInput(xmlDoc); bool IncludeComments = true; // This transform is created for demonstration purposes. XmlDsigEnvelopedSignatureTransform^ secondTransform = gcnew XmlDsigEnvelopedSignatureTransform(IncludeComments); String^ classDescription = secondTransform->ToString(); // This call does not perform as expected. // An enveloped signature has no inner XML elements secondTransform->LoadInnerXml(xmlDoc->SelectNodes("//.")); break; } } array<Type^>^ validOutTypes = xmlTransform->OutputTypes; for (int i = validOutTypes->Length-1; i >= 0; i--) { if (validOutTypes[i] == System::Xml::XmlDocument::typeid) { Type^ xmlDocumentType = System::Xml::XmlDocument::typeid; XmlDocument^ xmlDocumentOutput = (XmlDocument^) xmlTransform->GetOutput(xmlDocumentType); // Display to the console the Xml before and after // encryption. Console::WriteLine("Result of the GetOutput method call" + " from the current transform: " + xmlDocumentOutput->OuterXml); break; } else if (validOutTypes[i] == System::Xml::XmlNodeList::typeid) { Type^ xmlNodeListType = System::Xml::XmlNodeList::typeid; XmlNodeList^ xmlNodes = (XmlNodeList^) xmlTransform->GetOutput(xmlNodeListType); // Display to the console the Xml before and after // encryption. Console::WriteLine("Encoding the following message: " + xmlDoc->InnerText); Console::WriteLine("Nodes of the XmlNodeList retrieved " + "from GetOutput:"); for (int j = 0; j < xmlNodes->Count; j++) { Console::WriteLine("Node " + j + " has the following name: " + xmlNodes->Item(j)->Name + " and the following InnerXml: " + xmlNodes->Item(j)->InnerXml); } break; } else { Object^ outputObject = xmlTransform->GetOutput(); } } } // Create an XML document describing various products. public: static XmlDocument^ LoadProducts() { XmlDocument^ xmlDoc = gcnew XmlDocument(); String^ contosoProducts = "<PRODUCTS>" + "<PRODUCT><ID>123</ID><DESCRIPTION>Router" + "</DESCRIPTION></PRODUCT>" + "<PRODUCT><ID>456</ID><DESCRIPTION>Keyboard" + "</DESCRIPTION></PRODUCT>" + "<PRODUCT><ID>789</ID><DESCRIPTION>Monitor" + "</DESCRIPTION></PRODUCT>" + "</PRODUCTS>"; xmlDoc->LoadXml(contosoProducts); return xmlDoc; } // Create a signature and add it to the specified document. public: static void SignDocument(XmlDocument^ xmlDoc) { // Generate a signing key. RSACryptoServiceProvider^ key = gcnew RSACryptoServiceProvider(); // Create a SignedXml object. SignedXml^ signedDocument = gcnew SignedXml(xmlDoc); // Add the key to the SignedXml document. signedDocument->SigningKey = key; // Create a reference to be signed. Reference^ referenceToBeSigned = gcnew Reference(); referenceToBeSigned->Uri = ""; // Add an enveloped transformation to the reference. referenceToBeSigned->AddTransform( gcnew XmlDsigEnvelopedSignatureTransform()); // Add the reference to the SignedXml object. signedDocument->AddReference(referenceToBeSigned); if(File::Exists(Certificate)) { // Create a new KeyInfo object. KeyInfo^ info = gcnew KeyInfo(); // Load the X509 certificate. X509Certificate^ certFromFile = X509Certificate::CreateFromCertFile(Certificate); // Load the certificate into a KeyInfoX509Data object // and add it to the KeyInfo object. info->AddClause(gcnew KeyInfoX509Data(certFromFile)); // Add the KeyInfo object to the SignedXml object. signedDocument->KeyInfo = info; } else { Console::WriteLine("Unable to locate the following file: " + Certificate); } // Compute the signature. signedDocument->ComputeSignature(); // Add the signature branch to the original tree so it is enveloped. xmlDoc->DocumentElement->AppendChild(signedDocument->GetXml()); } // Resolve the specified base and relative Uri's . public: static Uri^ ResolveUris(Uri^ baseUri, String^ relativeUri) { XmlUrlResolver^ xmlResolver = gcnew XmlUrlResolver(); xmlResolver->Credentials = System::Net::CredentialCache::DefaultCredentials; XmlDsigEnvelopedSignatureTransform^ xmlTransform = gcnew XmlDsigEnvelopedSignatureTransform(); xmlTransform->Resolver = xmlResolver; Uri^ absoluteUri = xmlResolver->ResolveUri(baseUri, relativeUri); if (absoluteUri != nullptr) { Console::WriteLine( "\nResolved the base Uri and relative Uri to the following:"); Console::WriteLine(absoluteUri->ToString()); } else { Console::WriteLine( "Unable to resolve the base Uri and relative Uri"); } return absoluteUri; } }; [STAThread] int main() { // Encrypt an XML message XmlDocument^ productsXml = EnvelopedSignatureSample::LoadProducts(); EnvelopedSignatureSample::ShowTransformProperties(productsXml); EnvelopedSignatureSample::SignDocument(productsXml); EnvelopedSignatureSample::ShowTransformProperties(productsXml); // Use XmlDsigEnvelopedSignatureTransform to resolve a Uri. Uri^ baseUri = gcnew Uri("http://www.contoso.com"); String^ relativeUri = "xml"; Uri^ absoluteUri = EnvelopedSignatureSample::ResolveUris(baseUri, relativeUri); Console::WriteLine("This sample completed successfully; " + "press Enter to exit."); Console::ReadLine(); } // // This sample produces the following output: // // Xml representation of the current transform: // <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped- // signature" xmlns="http://www.w3.org/2000/09/xmldsig#" /> // Result of the GetOutput method call from the current transform: <PRODUCTS> // <PRODUCT><ID>123</ID><DESCRIPTION>Router</DESCRIPTION></PRODUCT><PRODUCT> // <ID>456</ID><DESCRIPTION>Keyboard</DESCRIPTION></PRODUCT><PRODUCT><ID>789 // </ID><DESCRIPTION>Monitor</DESCRIPTION></PRODUCT></PRODUCTS> // Unable to load the following file: ..\\my509.cer // // Xml representation of the current transform: // <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped- // signature" xmlns="http://www.w3.org/2000/09/xmldsig#" /> // Result of the GetOutput method call from the current transform: <PRODUCTS> // <PRODUCT><ID>123</ID><DESCRIPTION>Router</DESCRIPTION></PRODUCT><PRODUCT> // <ID>456</ID><DESCRIPTION>Keyboard</DESCRIPTION></PRODUCT><PRODUCT><ID>789 // </ID><DESCRIPTION>Monitor</DESCRIPTION></PRODUCT><Signature xmlns= // "http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod // Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> // <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> // <Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000 // /09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm= // "http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>KvPW6HUiIUMEDS0YSoT // gpo2JPbA=</DigestValue></Reference></SignedInfo><SignatureValue>c/njCGDru/a // WAmWG83I+mWO040xOzxvmNx0b0o8ZyPc9j5VwApdAt103OGBtB1H6EkOvt7Ekw+PVuUo8m5LzLP // yaTxUDMbb2kZZ5itSkGD4rmMUMUMuzrkAoquJZjxeOydBJ2CMehV2rE3RMPLIwRX176DZVy5JKU // 6Cb7PR2Rpw=</SignatureValue></Signature></PRODUCTS> // // Resolved the base Uri and relative Uri to the following: // http://www.contoso.com/xml // This sample completed successfully; press Enter to exit.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
