Export (0) Print
Expand All

KeyInfoX509Data Class

Represents an <X509Data> subelement of an XMLDSIG or XML Encryption <KeyInfo> element.

System.Object
  System.Security.Cryptography.Xml.KeyInfoClause
    System.Security.Cryptography.Xml.KeyInfoX509Data

Namespace:  System.Security.Cryptography.Xml
Assembly:  System.Security (in System.Security.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class KeyInfoX509Data : KeyInfoClause

The KeyInfoX509Data type exposes the following members.

  NameDescription
Public methodKeyInfoX509Data()Initializes a new instance of the KeyInfoX509Data class.
Public methodKeyInfoX509Data(Byte[])Initializes a new instance of the KeyInfoX509Data class from the specified ASN.1 DER encoding of an X.509v3 certificate.
Public methodKeyInfoX509Data(X509Certificate)Initializes a new instance of the KeyInfoX509Data class from the specified X.509v3 certificate.
Public methodKeyInfoX509Data(X509Certificate, X509IncludeOption)Initializes a new instance of the KeyInfoX509Data class from the specified X.509v3 certificate.
Top

  NameDescription
Public propertyCertificatesGets a list of the X.509v3 certificates contained in the KeyInfoX509Data object.
Public propertyCRLGets or sets the Certificate Revocation List (CRL) contained within the KeyInfoX509Data object.
Public propertyIssuerSerialsGets a list of X509IssuerSerial structures that represent an issuer name and serial number pair.
Public propertySubjectKeyIdsGets a list of the subject key identifiers (SKIs) contained in the KeyInfoX509Data object.
Public propertySubjectNamesGets a list of the subject names of the entities contained in the KeyInfoX509Data object.
Top

  NameDescription
Public methodAddCertificateAdds the specified X.509v3 certificate to the KeyInfoX509Data.
Public methodAddIssuerSerialAdds the specified issuer name and serial number pair to the KeyInfoX509Data object.
Public methodAddSubjectKeyId(Byte[])Adds the specified subject key identifier (SKI) byte array to the KeyInfoX509Data object.
Public methodAddSubjectKeyId(String)Adds the specified subject key identifier (SKI) string to the KeyInfoX509Data object.
Public methodAddSubjectNameAdds the subject name of the entity that was issued an X.509v3 certificate to the KeyInfoX509Data object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetXmlReturns an XML representation of the KeyInfoX509Data object. (Overrides KeyInfoClause.GetXml().)
Public methodLoadXmlParses the input XmlElement object and configures the internal state of the KeyInfoX509Data object to match. (Overrides KeyInfoClause.LoadXml(XmlElement).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The KeyInfoX509Data class represents the <X509Data> element that contains X.509v3 certificate information related to the validation or encryption key. For example, an <X509Data> element might contain an X.509 certificate whose subject public key is the validation key, or a chain of X.509 certificates that terminate in a certificate for the validating key, or other identifiers for related X.509 certificates.

Use the KeyInfoX509Data class whenever you need to include X.509 certificate data in an encrypted or signed XML document.

For more information about the <X509Data> element, see Section 4.4.4 of the XMLDSIG specification, which is available from the World Wide Web Consortium (W3C) at www.w3.org/TR/xmldsig-core/, or see the XML Encryption specification, which is available from the W3C at www.w3.org/TR/xmlenc-core/.

NoteNote

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 a detached signature. The second example demonstrates how to sign an XML file using an envelope signature.

Example #1

// 
// This example signs a file specified by a URI  
// using a detached signature. It then verifies   
// the signed XML. 
// 

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;



class XMLDSIGDetached
{
	
    [STAThread]
    static void Main(string[] args)
    {
        // The URI to sign. 
        string resourceToSign = "http://www.microsoft.com";
		
        // The name of the file to which to save the XML signature. 
        string XmlFileName = "xmldsig.xml";

        // The name of the X509 certificate 
        string Certificate = "microsoft.cer";

        try
        {

            // Generate a signing key. This key should match the certificate.
            RSACryptoServiceProvider Key = new RSACryptoServiceProvider();

            Console.WriteLine("Signing: {0}", resourceToSign);

            // Sign the detached resourceand save the signature in an XML file.
            SignDetachedResource(resourceToSign, XmlFileName, Key, Certificate);

            Console.WriteLine("XML signature was succesfully computed and saved to {0}.", XmlFileName);

        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
		
    }

    // Sign an XML file and save the signature in a new file. 
    public static void SignDetachedResource(string URIString, string XmlSigFileName, RSA Key, string Certificate)
    {
        // Create a SignedXml object.
        SignedXml signedXml = new SignedXml();

        // Assign the key to the SignedXml object.
        signedXml.SigningKey = Key;

        // Create a reference to be signed.
        Reference reference = new Reference();

        // Add the passed URI to the reference object.
        reference.Uri = URIString;
		
        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        // Create a new KeyInfo object.
        KeyInfo keyInfo = new KeyInfo();

        // Load the X509 certificate.
        X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);

        // Load the certificate into a KeyInfoX509Data object 
        // and add it to the KeyInfo object.
        keyInfo.AddClause(new KeyInfoX509Data(MSCert));

        // Add the KeyInfo object to the SignedXml object.
        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();

        // Save the signed XML document to a file specified 
        // using the passed string.
        XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false));
        xmlDigitalSignature.WriteTo(xmltw);
        xmltw.Close();

    }
}

Example #2

// 
// This example signs an XML file using an 
// envelope signature. It then verifies the  
// signed XML. 
// 
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;

public class SignVerifyEnvelope
{

    public static void Main(String[] args)
    {

        string Certificate =  "microsoft.cer";

        try
        {
            // Generate a signing key.
            RSACryptoServiceProvider Key = new 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, Certificate);
            Console.WriteLine("XML file signed."); 
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    // Sign an XML file and save the signature in a new file. 
    public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, string Certificate)
    {
        // Create a new XML document.
        XmlDocument doc = new XmlDocument();

        // Format the document to ignore white spaces.
        doc.PreserveWhitespace = false;

        // Load the passed XML file using it's name.
        doc.Load(new XmlTextReader(FileName));

        // Create a SignedXml object.
        SignedXml signedXml = new SignedXml(doc);

        // Add the key to the SignedXml document. 
        signedXml.SigningKey = Key;

        // Create a reference to be signed.
        Reference reference = new Reference();
        reference.Uri = "";

        // Add an enveloped transformation to the reference.
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);

        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        // Create a new KeyInfo object.
        KeyInfo keyInfo = new KeyInfo();

        // Load the X509 certificate.
        X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);

        // Load the certificate into a KeyInfoX509Data object 
        // and add it to the KeyInfo object.
        keyInfo.AddClause(new KeyInfoX509Data(MSCert));

        // Add the KeyInfo object to the SignedXml object.
        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 is XmlDeclaration)  
        {
            doc.RemoveChild(doc.FirstChild);
        }

        // Save the signed XML document to a file specified 
        // using the passed string.
        XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
        doc.WriteTo(xmltw);
        xmltw.Close();
    }


    // Create example data to sign. 
    public static void CreateSomeXml(string FileName)
    {
        // Create a new XmlDocument object.
        XmlDocument document = new 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 = new XmlTextWriter(FileName, new UTF8Encoding(false));
        document.WriteTo(xmltw);
        xmltw.Close();
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft