// OCSPRef.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.Xml; namespace Microsoft.Xades { /// /// This class identifies one OCSP response /// public class OCSPRef { #region Private variables private OCSPIdentifier ocspIdentifier; private DigestAlgAndValueType digestAlgAndValue; #endregion #region Public properties /// /// Identification of one OCSP response /// public OCSPIdentifier OCSPIdentifier { get { return this.ocspIdentifier; } set { this.ocspIdentifier = value; } } /// /// The digest computed on the DER encoded OCSP response, since it may be /// needed to differentiate between two OCSP responses by the same server /// with their "ProducedAt" fields within the same second /// public DigestAlgAndValueType CertDigest { get { return this.digestAlgAndValue; } set { this.digestAlgAndValue = value; } } #endregion #region Constructors /// /// Default constructor /// public OCSPRef() { this.ocspIdentifier = new OCSPIdentifier(); this.digestAlgAndValue = new DigestAlgAndValueType("DigestAlgAndValue"); } #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.ocspIdentifier != null && this.ocspIdentifier.HasChanged()) { retVal = true; } if (this.digestAlgAndValue != null && this.digestAlgAndValue.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("xsd", XadesSignedXml.XadesNamespaceUri); xmlNodeList = xmlElement.SelectNodes("xsd:OCSPIdentifier", xmlNamespaceManager); if (xmlNodeList.Count == 0) { throw new CryptographicException("OCSPIdentifier missing"); } this.ocspIdentifier = new OCSPIdentifier(); this.ocspIdentifier.LoadXml((XmlElement)xmlNodeList.Item(0)); xmlNodeList = xmlElement.SelectNodes("xsd:DigestAlgAndValue", xmlNamespaceManager); if (xmlNodeList.Count == 0) { this.digestAlgAndValue = null; } else { this.digestAlgAndValue = new DigestAlgAndValueType("DigestAlgAndValue"); this.digestAlgAndValue.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", "OCSPRef", XadesSignedXml.XadesNamespaceUri); if (this.ocspIdentifier != null && this.ocspIdentifier.HasChanged()) { retVal.AppendChild(creationXmlDocument.ImportNode(this.ocspIdentifier.GetXml(), true)); } else { throw new CryptographicException("OCSPIdentifier element missing in OCSPRef"); } if (this.digestAlgAndValue != null && this.digestAlgAndValue.HasChanged()) { retVal.AppendChild(creationXmlDocument.ImportNode(this.digestAlgAndValue.GetXml(), true)); } return retVal; } #endregion } }