// Cert.cs // // XAdES Starter Kit for Microsoft .NET 3.5 (and above) // 2010 Microsoft France // Published under the CECILL-B Free Software license agreement. // (http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt) // // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. // THE ENTIRE RISK OF USE OR RESULTS IN CONNECTION WITH THE USE OF THIS CODE // AND INFORMATION REMAINS WITH THE USER. using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Xml; namespace Microsoft.Xades { /// /// This class contains certificate identification information /// public class Cert { #region Private variables private DigestAlgAndValueType certDigest; private IssuerSerial issuerSerial; #endregion #region Public properties /// /// The element CertDigest contains the digest of one of the /// certificates referenced in the sequence /// public DigestAlgAndValueType CertDigest { get { return this.certDigest; } set { this.certDigest = value; } } /// /// The element IssuerSerial contains the identifier of one of the /// certificates referenced in the sequence. Should the /// X509IssuerSerial element appear in the signature to denote the same /// certificate, its value MUST be consistent with the corresponding /// IssuerSerial element. /// public IssuerSerial IssuerSerial { get { return this.issuerSerial; } set { this.issuerSerial = value; } } #endregion #region Constructors /// /// Default constructor /// public Cert() { this.certDigest = new DigestAlgAndValueType("CertDigest"); this.issuerSerial = new IssuerSerial(); } #endregion #region Public methods /// /// Check to see if something has changed in this instance and needs to be serialized /// /// Flag indicating if a member needs serialization public bool HasChanged() { bool retVal = false; if (this.certDigest != null && this.certDigest.HasChanged()) { retVal = true; } if (this.issuerSerial != null && this.issuerSerial.HasChanged()) { retVal = true; } return retVal; } /// /// Load state from an XML element /// /// XML element containing new state public void LoadXml(System.Xml.XmlElement xmlElement) { XmlNamespaceManager xmlNamespaceManager; XmlNodeList xmlNodeList; if (xmlElement == null) { throw new ArgumentNullException("xmlElement"); } xmlNamespaceManager = new XmlNamespaceManager(xmlElement.OwnerDocument.NameTable); xmlNamespaceManager.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); xmlNamespaceManager.AddNamespace("xsd", XadesSignedXml.XadesNamespaceUri); xmlNodeList = xmlElement.SelectNodes("xsd:CertDigest", xmlNamespaceManager); if (xmlNodeList.Count == 0) { throw new CryptographicException("CertDigest missing"); } this.certDigest = new DigestAlgAndValueType("CertDigest"); this.certDigest.LoadXml((XmlElement)xmlNodeList.Item(0)); xmlNodeList = xmlElement.SelectNodes("xsd:IssuerSerial", xmlNamespaceManager); if (xmlNodeList.Count == 0) { throw new CryptographicException("IssuerSerial missing"); } this.issuerSerial = new IssuerSerial(); this.issuerSerial.LoadXml((XmlElement)xmlNodeList.Item(0)); } /// /// Returns the XML representation of the this object /// /// XML element containing the state of this object public XmlElement GetXml() { XmlDocument creationXmlDocument; XmlElement retVal; creationXmlDocument = new XmlDocument(); retVal = creationXmlDocument.CreateElement("xades", "Cert", XadesSignedXml.XadesNamespaceUri); if (this.certDigest != null && this.certDigest.HasChanged()) { retVal.AppendChild(creationXmlDocument.ImportNode(this.certDigest.GetXml(), true)); } if (this.issuerSerial != null && this.issuerSerial.HasChanged()) { retVal.AppendChild(creationXmlDocument.ImportNode(this.issuerSerial.GetXml(), true)); } return retVal; } #endregion } }