123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Collections;
- using Org.BouncyCastle.Utilities;
- using Org.BouncyCastle.Utilities.Collections;
- namespace Org.BouncyCastle.Asn1.X509
- {
- public class X509Extensions
- : Asn1Encodable
- {
- /**
- * Subject Directory Attributes
- */
- public static readonly DerObjectIdentifier SubjectDirectoryAttributes = new DerObjectIdentifier("2.5.29.9");
- /**
- * Subject Key Identifier
- */
- public static readonly DerObjectIdentifier SubjectKeyIdentifier = new DerObjectIdentifier("2.5.29.14");
- /**
- * Key Usage
- */
- public static readonly DerObjectIdentifier KeyUsage = new DerObjectIdentifier("2.5.29.15");
- /**
- * Private Key Usage Period
- */
- public static readonly DerObjectIdentifier PrivateKeyUsagePeriod = new DerObjectIdentifier("2.5.29.16");
- /**
- * Subject Alternative Name
- */
- public static readonly DerObjectIdentifier SubjectAlternativeName = new DerObjectIdentifier("2.5.29.17");
- /**
- * Issuer Alternative Name
- */
- public static readonly DerObjectIdentifier IssuerAlternativeName = new DerObjectIdentifier("2.5.29.18");
- /**
- * Basic Constraints
- */
- public static readonly DerObjectIdentifier BasicConstraints = new DerObjectIdentifier("2.5.29.19");
- /**
- * CRL Number
- */
- public static readonly DerObjectIdentifier CrlNumber = new DerObjectIdentifier("2.5.29.20");
- /**
- * Reason code
- */
- public static readonly DerObjectIdentifier ReasonCode = new DerObjectIdentifier("2.5.29.21");
- /**
- * Hold Instruction Code
- */
- public static readonly DerObjectIdentifier InstructionCode = new DerObjectIdentifier("2.5.29.23");
- /**
- * Invalidity Date
- */
- public static readonly DerObjectIdentifier InvalidityDate = new DerObjectIdentifier("2.5.29.24");
- /**
- * Delta CRL indicator
- */
- public static readonly DerObjectIdentifier DeltaCrlIndicator = new DerObjectIdentifier("2.5.29.27");
- /**
- * Issuing Distribution Point
- */
- public static readonly DerObjectIdentifier IssuingDistributionPoint = new DerObjectIdentifier("2.5.29.28");
- /**
- * Certificate Issuer
- */
- public static readonly DerObjectIdentifier CertificateIssuer = new DerObjectIdentifier("2.5.29.29");
- /**
- * Name Constraints
- */
- public static readonly DerObjectIdentifier NameConstraints = new DerObjectIdentifier("2.5.29.30");
- /**
- * CRL Distribution Points
- */
- public static readonly DerObjectIdentifier CrlDistributionPoints = new DerObjectIdentifier("2.5.29.31");
- /**
- * Certificate Policies
- */
- public static readonly DerObjectIdentifier CertificatePolicies = new DerObjectIdentifier("2.5.29.32");
- /**
- * Policy Mappings
- */
- public static readonly DerObjectIdentifier PolicyMappings = new DerObjectIdentifier("2.5.29.33");
- /**
- * Authority Key Identifier
- */
- public static readonly DerObjectIdentifier AuthorityKeyIdentifier = new DerObjectIdentifier("2.5.29.35");
- /**
- * Policy Constraints
- */
- public static readonly DerObjectIdentifier PolicyConstraints = new DerObjectIdentifier("2.5.29.36");
- /**
- * Extended Key Usage
- */
- public static readonly DerObjectIdentifier ExtendedKeyUsage = new DerObjectIdentifier("2.5.29.37");
- /**
- * Freshest CRL
- */
- public static readonly DerObjectIdentifier FreshestCrl = new DerObjectIdentifier("2.5.29.46");
- /**
- * Inhibit Any Policy
- */
- public static readonly DerObjectIdentifier InhibitAnyPolicy = new DerObjectIdentifier("2.5.29.54");
- /**
- * Authority Info Access
- */
- public static readonly DerObjectIdentifier AuthorityInfoAccess = new DerObjectIdentifier("1.3.6.1.5.5.7.1.1");
- /**
- * Subject Info Access
- */
- public static readonly DerObjectIdentifier SubjectInfoAccess = new DerObjectIdentifier("1.3.6.1.5.5.7.1.11");
- /**
- * Logo Type
- */
- public static readonly DerObjectIdentifier LogoType = new DerObjectIdentifier("1.3.6.1.5.5.7.1.12");
- /**
- * BiometricInfo
- */
- public static readonly DerObjectIdentifier BiometricInfo = new DerObjectIdentifier("1.3.6.1.5.5.7.1.2");
- /**
- * QCStatements
- */
- public static readonly DerObjectIdentifier QCStatements = new DerObjectIdentifier("1.3.6.1.5.5.7.1.3");
- /**
- * Audit identity extension in attribute certificates.
- */
- public static readonly DerObjectIdentifier AuditIdentity = new DerObjectIdentifier("1.3.6.1.5.5.7.1.4");
- /**
- * NoRevAvail extension in attribute certificates.
- */
- public static readonly DerObjectIdentifier NoRevAvail = new DerObjectIdentifier("2.5.29.56");
- /**
- * TargetInformation extension in attribute certificates.
- */
- public static readonly DerObjectIdentifier TargetInformation = new DerObjectIdentifier("2.5.29.55");
- private readonly IDictionary extensions = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
- private readonly IList ordering;
- public static X509Extensions GetInstance(
- Asn1TaggedObject obj,
- bool explicitly)
- {
- return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
- }
- public static X509Extensions GetInstance(
- object obj)
- {
- if (obj == null || obj is X509Extensions)
- {
- return (X509Extensions) obj;
- }
- if (obj is Asn1Sequence)
- {
- return new X509Extensions((Asn1Sequence) obj);
- }
- if (obj is Asn1TaggedObject)
- {
- return GetInstance(((Asn1TaggedObject) obj).GetObject());
- }
- throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
- }
- /**
- * Constructor from Asn1Sequence.
- *
- * the extensions are a list of constructed sequences, either with (Oid, OctetString) or (Oid, Boolean, OctetString)
- */
- private X509Extensions(
- Asn1Sequence seq)
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- foreach (Asn1Encodable ae in seq)
- {
- Asn1Sequence s = Asn1Sequence.GetInstance(ae.ToAsn1Object());
- if (s.Count < 2 || s.Count > 3)
- throw new ArgumentException("Bad sequence size: " + s.Count);
- DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(s[0].ToAsn1Object());
- bool isCritical = s.Count == 3
- && DerBoolean.GetInstance(s[1].ToAsn1Object()).IsTrue;
- Asn1OctetString octets = Asn1OctetString.GetInstance(s[s.Count - 1].ToAsn1Object());
- extensions.Add(oid, new X509Extension(isCritical, octets));
- ordering.Add(oid);
- }
- }
- /**
- * constructor from a table of extensions.
- * <p>
- * it's is assumed the table contains Oid/string pairs.</p>
- */
- public X509Extensions(
- IDictionary extensions)
- : this(null, extensions)
- {
- }
- /**
- * Constructor from a table of extensions with ordering.
- * <p>
- * It's is assumed the table contains Oid/string pairs.</p>
- */
- public X509Extensions(
- IList ordering,
- IDictionary extensions)
- {
- if (ordering == null)
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.Keys);
- }
- else
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList(ordering);
- }
- foreach (DerObjectIdentifier oid in this.ordering)
- {
- this.extensions.Add(oid, (X509Extension)extensions[oid]);
- }
- }
- /**
- * Constructor from two vectors
- *
- * @param objectIDs an ArrayList of the object identifiers.
- * @param values an ArrayList of the extension values.
- */
- public X509Extensions(
- IList oids,
- IList values)
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList(oids);
- int count = 0;
- foreach (DerObjectIdentifier oid in this.ordering)
- {
- this.extensions.Add(oid, (X509Extension)values[count++]);
- }
- }
- #if !SILVERLIGHT && !NETFX_CORE && !UNITY_WP8// || !PORTABLE
- //#if !(SILVERLIGHT || PORTABLE)
- /**
- * constructor from a table of extensions.
- * <p>
- * it's is assumed the table contains Oid/string pairs.</p>
- */
- [Obsolete]
- public X509Extensions(
- Hashtable extensions)
- : this(null, extensions)
- {
- }
- /**
- * Constructor from a table of extensions with ordering.
- * <p>
- * It's is assumed the table contains Oid/string pairs.</p>
- */
- [Obsolete]
- public X509Extensions(
- ArrayList ordering,
- Hashtable extensions)
- {
- if (ordering == null)
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.Keys);
- }
- else
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList(ordering);
- }
- foreach (DerObjectIdentifier oid in this.ordering)
- {
- this.extensions.Add(oid, (X509Extension) extensions[oid]);
- }
- }
- /**
- * Constructor from two vectors
- *
- * @param objectIDs an ArrayList of the object identifiers.
- * @param values an ArrayList of the extension values.
- */
- [Obsolete]
- public X509Extensions(
- ArrayList oids,
- ArrayList values)
- {
- this.ordering = Org.BouncyCastle.Utilities.Platform.CreateArrayList(oids);
- int count = 0;
- foreach (DerObjectIdentifier oid in this.ordering)
- {
- this.extensions.Add(oid, (X509Extension) values[count++]);
- }
- }
- #endif
- [Obsolete("Use ExtensionOids IEnumerable property")]
- public IEnumerator Oids()
- {
- return ExtensionOids.GetEnumerator();
- }
- /**
- * return an Enumeration of the extension field's object ids.
- */
- public IEnumerable ExtensionOids
- {
- get { return new EnumerableProxy(ordering); }
- }
- /**
- * return the extension represented by the object identifier
- * passed in.
- *
- * @return the extension if it's present, null otherwise.
- */
- public X509Extension GetExtension(
- DerObjectIdentifier oid)
- {
- return (X509Extension) extensions[oid];
- }
- /**
- * <pre>
- * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
- *
- * Extension ::= SEQUENCE {
- * extnId EXTENSION.&id ({ExtensionSet}),
- * critical BOOLEAN DEFAULT FALSE,
- * extnValue OCTET STRING }
- * </pre>
- */
- public override Asn1Object ToAsn1Object()
- {
- Asn1EncodableVector vec = new Asn1EncodableVector();
- foreach (DerObjectIdentifier oid in ordering)
- {
- X509Extension ext = (X509Extension) extensions[oid];
- Asn1EncodableVector v = new Asn1EncodableVector(oid);
- if (ext.IsCritical)
- {
- v.Add(DerBoolean.True);
- }
- v.Add(ext.Value);
- vec.Add(new DerSequence(v));
- }
- return new DerSequence(vec);
- }
- public bool Equivalent(
- X509Extensions other)
- {
- if (extensions.Count != other.extensions.Count)
- return false;
- foreach (DerObjectIdentifier oid in extensions.Keys)
- {
- if (!extensions[oid].Equals(other.extensions[oid]))
- return false;
- }
- return true;
- }
- public DerObjectIdentifier[] GetExtensionOids()
- {
- return ToOidArray(ordering);
- }
- public DerObjectIdentifier[] GetNonCriticalExtensionOids()
- {
- return GetExtensionOids(false);
- }
- public DerObjectIdentifier[] GetCriticalExtensionOids()
- {
- return GetExtensionOids(true);
- }
- private DerObjectIdentifier[] GetExtensionOids(bool isCritical)
- {
- IList oids = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- foreach (DerObjectIdentifier oid in this.ordering)
- {
- X509Extension ext = (X509Extension)extensions[oid];
- if (ext.IsCritical == isCritical)
- {
- oids.Add(oid);
- }
- }
- return ToOidArray(oids);
- }
- private static DerObjectIdentifier[] ToOidArray(IList oids)
- {
- DerObjectIdentifier[] oidArray = new DerObjectIdentifier[oids.Count];
- oids.CopyTo(oidArray, 0);
- return oidArray;
- }
- }
- }
- #endif
|