﻿// Decompiled with JetBrains decompiler
// Type: System.Security.Cryptography.X509Certificates.X509Extension
// Assembly: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 5ABD58FD-DF31-44FD-A492-63F2B47CC9AF
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll

using System;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace System.Security.Cryptography.X509Certificates
{
  /// <summary>
  /// Represents an X509 extension.
  /// </summary>
  public class X509Extension : AsnEncodedData
  {
    private bool m_critical;

    /// <summary>
    /// Gets a Boolean value indicating whether the extension is critical.
    /// </summary>
    /// 
    /// <returns>
    /// true if the extension is critical; otherwise, false.
    /// </returns>
    public bool Critical
    {
      [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get
      {
        return this.m_critical;
      }
      [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] set
      {
        this.m_critical = value;
      }
    }

    internal X509Extension(string oid)
      : base(new Oid(oid, System.Security.Cryptography.OidGroup.ExtensionOrAttribute, false))
    {
    }

    internal X509Extension(IntPtr pExtension)
    {
      CAPIBase.CERT_EXTENSION certExtension = (CAPIBase.CERT_EXTENSION) Marshal.PtrToStructure(pExtension, typeof (CAPIBase.CERT_EXTENSION));
      this.m_critical = certExtension.fCritical;
      this.m_oid = new Oid(certExtension.pszObjId, System.Security.Cryptography.OidGroup.ExtensionOrAttribute, false);
      byte[] destination = new byte[(IntPtr) certExtension.Value.cbData];
      if (certExtension.Value.pbData != IntPtr.Zero)
        Marshal.Copy(certExtension.Value.pbData, destination, 0, destination.Length);
      this.m_rawData = destination;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Extension"/> class.
    /// </summary>
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    protected X509Extension()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Extension"/> class.
    /// </summary>
    /// <param name="oid">A string representing the object identifier.</param><param name="rawData">The encoded data used to create the extension.</param><param name="critical">true if the extension is critical; otherwise false.</param>
    public X509Extension(string oid, byte[] rawData, bool critical)
      : this(new Oid(oid, System.Security.Cryptography.OidGroup.ExtensionOrAttribute, true), rawData, critical)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Extension"/> class.
    /// </summary>
    /// <param name="encodedExtension">The encoded data to be used to create the extension.</param><param name="critical">true if the extension is critical; otherwise false.</param>
    public X509Extension(AsnEncodedData encodedExtension, bool critical)
      : this(encodedExtension.Oid, encodedExtension.RawData, critical)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Extension"/> class.
    /// </summary>
    /// <param name="oid">The object identifier used to identify the extension.</param><param name="rawData">The encoded data used to create the extension.</param><param name="critical">true if the extension is critical; otherwise false.</param><exception cref="T:System.ArgumentNullException"><paramref name="oid"/> is null.</exception><exception cref="T:System.ArgumentException"><paramref name="oid"/> is an empty string ("").</exception>
    public X509Extension(Oid oid, byte[] rawData, bool critical)
      : base(oid, rawData)
    {
      if (this.Oid == null || this.Oid.Value == null)
        throw new ArgumentNullException("oid");
      if (this.Oid.Value.Length == 0)
        throw new ArgumentException(SR.GetString("Arg_EmptyOrNullString"), "oid.Value");
      this.m_critical = critical;
    }

    /// <summary>
    /// Copies the extension properties of the specified <see cref="T:System.Security.Cryptography.AsnEncodedData"/> object.
    /// </summary>
    /// <param name="asnEncodedData">The <see cref="T:System.Security.Cryptography.AsnEncodedData"/> to be copied.</param><exception cref="T:System.ArgumentNullException"><paramref name="asnEncodedData"/> is null. </exception><exception cref="T:System.ArgumentException"><paramref name="asnEncodedData"/> does not have a valid X.509 extension.</exception>
    public override void CopyFrom(AsnEncodedData asnEncodedData)
    {
      if (asnEncodedData == null)
        throw new ArgumentNullException("asnEncodedData");
      X509Extension x509Extension = asnEncodedData as X509Extension;
      if (x509Extension == null)
        throw new ArgumentException(SR.GetString("Cryptography_X509_ExtensionMismatch"));
      base.CopyFrom(asnEncodedData);
      this.m_critical = x509Extension.Critical;
    }
  }
}
