Inheritance: System.Security.Cryptography.X509Certificates.X509Extension
        void initializeAsn1(Byte[] bytes)
        {
            switch (PropertyName)
            {
            case X509CertificatePropertyType.EnhancedKeyUsage:
                UnderlyingType = typeof(X509EnhancedKeyUsageExtension);
                AsnEncodedData asn = new AsnEncodedData(bytes);
                PropertyValue = new X509EnhancedKeyUsageExtension(asn, false);
                break;

            case X509CertificatePropertyType.RootProgramCertificatePolicies:
                UnderlyingType = typeof(X509CertificatePoliciesExtension);
                PropertyValue  = new X509CertificatePoliciesExtension(bytes, false);
                break;

            case X509CertificatePropertyType.OcspResponse:
                UnderlyingType = typeof(OCSPResponse);
                PropertyValue  = new OCSPResponse(bytes);
                break;

            case X509CertificatePropertyType.CrossCertificateDistributionPoints:
            case X509CertificatePropertyType.CTLNextUpdateLocation:
                UnderlyingType = typeof(X509AlternativeNameCollection);
                PropertyValue  = new X509AlternativeNameCollection();
                ((X509AlternativeNameCollection)PropertyValue).Decode(bytes);
                ((X509AlternativeNameCollection)PropertyValue).Close();
                break;
            }
        }
		public void ConstructorAsnEncodedData_BadAsn ()
		{
			AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[0]);
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, true);
			Assert.AreEqual (String.Empty, eku.Format (true), "Format(true)");
			Assert.AreEqual (String.Empty, eku.Format (false), "Format(false)");
			OidCollection oc = eku.EnhancedKeyUsages;
		}
        internal X509ExtensionCollection(MX.X509Certificate cert)
        {
            _list = new ArrayList(cert.Extensions.Count);
            if (cert.Extensions.Count == 0)
            {
                return;
            }

#if !MOONLIGHT
            object[] parameters = new object [2];
#endif
            foreach (MX.X509Extension ext in cert.Extensions)
            {
                bool   critical = ext.Critical;
                string oid      = ext.Oid;
                byte[] raw_data = null;
                // extension data is embedded in an octet stream (4)
                ASN1 value = ext.Value;
                if ((value.Tag == 0x04) && (value.Count > 0))
                {
                    raw_data = value [0].GetBytes();
                }

                X509Extension newt = null;
#if MOONLIGHT
                // non-extensible
                switch (oid)
                {
                case "2.5.29.14":
                    newt = new X509SubjectKeyIdentifierExtension(new AsnEncodedData(oid, raw_data), critical);
                    break;

                case "2.5.29.15":
                    newt = new X509KeyUsageExtension(new AsnEncodedData(oid, raw_data), critical);
                    break;

                case "2.5.29.19":
                    newt = new X509BasicConstraintsExtension(new AsnEncodedData(oid, raw_data), critical);
                    break;

                case "2.5.29.37":
                    newt = new X509EnhancedKeyUsageExtension(new AsnEncodedData(oid, raw_data), critical);
                    break;
                }
#else
                parameters [0] = new AsnEncodedData(oid, raw_data);
                parameters [1] = critical;
                newt           = (X509Extension)CryptoConfig.CreateFromName(oid, parameters);
#endif
                if (newt == null)
                {
                    // not registred in CryptoConfig, using default
                    newt = new X509Extension(oid, raw_data, critical);
                }
                _list.Add(newt);
            }
        }
		public void ConstructorEmpty ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ();
			Assert.IsFalse (eku.Critical, "Critical");
			Assert.IsNull (eku.RawData, "RawData");
			Assert.AreEqual (oid, eku.Oid.Value, "Oid.Value");
			Assert.AreEqual (fname, eku.Oid.FriendlyName, "Oid.FriendlyName");
			Assert.AreEqual (String.Empty, eku.Format (true), "Format(true)");
			Assert.AreEqual (String.Empty, eku.Format (false), "Format(false)");
		}
		public void ConstructorEmpty ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ();
			Assert.IsFalse (eku.Critical, "Critical");
			Assert.IsNull (eku.RawData, "RawData");
			Assert.AreEqual (oid, eku.Oid.Value, "Oid.Value");
			// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
			//Assert.AreEqual (fname, eku.Oid.FriendlyName, "Oid.FriendlyName");
			Assert.AreEqual (String.Empty, eku.Format (true), "Format(true)");
			Assert.AreEqual (String.Empty, eku.Format (false), "Format(false)");
		}
		public void ConstructorAsnEncodedData ()
		{
			AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, true);
			Assert.IsTrue (eku.Critical, "Critical");
			Assert.AreEqual (7, eku.RawData.Length, "RawData");	// original Oid ignored
			Assert.AreEqual (oid, eku.Oid.Value, "Oid.Value");
			Assert.AreEqual (fname, eku.Oid.FriendlyName, "Oid.FriendlyName");
			Assert.AreEqual (1, eku.EnhancedKeyUsages.Count, "EnhancedKeyUsages");
			Assert.AreEqual ("1.2.3.4", eku.EnhancedKeyUsages[0].Value, "EnhancedKeyUsages Oid");
			Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, eku.Format (true), "Format(true)");
			Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", eku.Format (false), "Format(false)");
		}
Ejemplo n.º 7
0
		internal X509ExtensionCollection (MX.X509Certificate cert)
		{
			_list = new ArrayList (cert.Extensions.Count);
			if (cert.Extensions.Count == 0)
				return;

#if !MOONLIGHT
			object[] parameters = new object [2];
#endif
			foreach (MX.X509Extension ext in cert.Extensions) {
				bool critical = ext.Critical;
				string oid = ext.Oid;
				byte[] raw_data = null;
				// extension data is embedded in an octet stream (4)
				ASN1 value = ext.Value;
				if ((value.Tag == 0x04) && (value.Count > 0))
					raw_data = value [0].GetBytes ();

				X509Extension newt = null;
#if MOONLIGHT || FULL_AOT_RUNTIME
				// non-extensible
				switch (oid) {
				case "2.5.29.14":
					newt = new X509SubjectKeyIdentifierExtension (new AsnEncodedData (oid, raw_data), critical);
					break;
				case "2.5.29.15":
					newt = new X509KeyUsageExtension (new AsnEncodedData (oid, raw_data), critical);
					break;
				case "2.5.29.19":
					newt = new X509BasicConstraintsExtension (new AsnEncodedData (oid, raw_data), critical);
					break;
				case "2.5.29.37":
					newt = new X509EnhancedKeyUsageExtension (new AsnEncodedData (oid, raw_data), critical);
					break;
				}
#else
				parameters [0] = new AsnEncodedData (oid, raw_data ?? Empty);
				parameters [1] = critical;
				newt = (X509Extension) CryptoConfig.CreateFromName (oid, parameters);
#endif
				if (newt == null) {
					// not registred in CryptoConfig, using default
					newt = new X509Extension (oid, raw_data ?? Empty, critical);
				}
				_list.Add (newt);
			}
		}
Ejemplo n.º 8
0
        internal string EnhancedKeyUsageExtension(bool multiLine)
        {
            string result;

            try
            {
                System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension x509EnhancedKeyUsageExtension = new System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension(this, false);
                result = x509EnhancedKeyUsageExtension.ToString(multiLine);
            }
            catch
            {
                result = string.Empty;
            }
            return(result);
        }
		public void WrongExtension_X509EnhancedKeyUsageExtension ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ();
			X509KeyUsageExtension ku = new X509KeyUsageExtension ();
			ku.CopyFrom (eku);
		}
 public X509EnhancedKeyUsageExtensionWrapper(X509EnhancedKeyUsageExtension extension)
     : base(extension)
 {
     x509 = extension;
     FillOids();
 }
		public void ConstructorAsnEncodedData_BadAsnTag ()
		{
			AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x05, 0x00 });
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, true);
			OidCollection oc = eku.EnhancedKeyUsages;
		}
		public void ConstructorOidCollection_Null ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ((OidCollection)null, true);
		}
Ejemplo n.º 13
0
		internal string EnhancedKeyUsageExtension (bool multiLine)
		{
			try {
				X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension  (this, false);
				return eku.ToString (multiLine);
			}
			catch {
				return String.Empty;
			}
		}
		public void CopyFrom_Null ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ();
			eku.CopyFrom (null);
		}
		public void WrongAsnEncodedData ()
		{
			AsnEncodedData aed = new AsnEncodedData (new byte[0]);
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (new OidCollection (), true);
			eku.CopyFrom (aed); // note: not the same behaviour than using the constructor!
		}
		public void WrongExtension_X509Extension_KeyUsages ()
		{
			X509Extension ex = new X509Extension ("1.2.3", new byte[0], true);
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ();
			eku.CopyFrom (ex);
			Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "EnhancedKeyUsages");
		}
		public void WrongExtension_X509Extension ()
		{
			X509Extension ex = new X509Extension ("1.2.3", new byte[0], true);
			OidCollection oc = new OidCollection ();
			oc.Add (new Oid ("1.2.3.4"));
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (oc, false);
			Assert.AreEqual (1, eku.EnhancedKeyUsages.Count, "EnhancedKeyUsages");
			Assert.IsFalse (eku.Critical, "Critical");
			eku.CopyFrom (ex);
			Assert.IsTrue (eku.Critical, "Critical");
			Assert.AreEqual (String.Empty, BitConverter.ToString (eku.RawData), "RawData");
			Assert.AreEqual ("1.2.3", eku.Oid.Value, "Oid.Value");
			Assert.IsNull (eku.Oid.FriendlyName, "Oid.FriendlyName");
		}
		public void ConstructorOidCollection ()
		{
			OidCollection oc = new OidCollection ();
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (oc, true);
			Assert.AreEqual ("30-00", BitConverter.ToString (eku.RawData), "RawData");
			Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "Count 0");
			// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
			//Assert.AreEqual ("Information Not Available", eku.Format (true), "Format(true)");
			//Assert.AreEqual ("Information Not Available", eku.Format (false), "Format(false)");
			oc.Add (new Oid ("1.2.3.4"));
			Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "Count still 0");
			int n = eku.EnhancedKeyUsages.Add (new Oid ("1.2.3"));
			Assert.AreEqual (0, n, "Add");
			Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "Count again 0");	// readonly!
			Assert.AreEqual (1, oc.Count, "Count 1 - oc");
			Assert.AreEqual ("1.2.3.4", oc [0].Value, "Value - oc");

			oc.Add (new Oid ("1.3.6.1.5.5.7.3.1"));
			eku = new X509EnhancedKeyUsageExtension (oc, true);
			Assert.AreEqual (2, eku.EnhancedKeyUsages.Count, "Count 2");
			Assert.AreEqual ("1.2.3.4", eku.EnhancedKeyUsages[0].Value, "Value - 1");
			Assert.AreEqual ("1.3.6.1.5.5.7.3.1", eku.EnhancedKeyUsages[1].Value, "Value - 2");
			// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
			//Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine + "Server Authentication (1.3.6.1.5.5.7.3.1)" + Environment.NewLine,
			//	eku.Format (true), "Format(true)");
			//Assert.AreEqual ("Unknown Key Usage (1.2.3.4), Server Authentication (1.3.6.1.5.5.7.3.1)", eku.Format (false), "Format(false)");
		}
            private static bool CheckCertificateUsage(System.Security.Cryptography.X509Certificates.X509Certificate2 cert)
            {
                bool result;

                try
                {
                    if (cert.Version < 3)
                    {
                        result = true;
                    }
                    else
                    {
                        System.Security.Cryptography.X509Certificates.X509KeyUsageExtension         x509KeyUsageExtension         = (System.Security.Cryptography.X509Certificates.X509KeyUsageExtension)cert.Extensions["2.5.29.15"];
                        System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension x509EnhancedKeyUsageExtension = (System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension)cert.Extensions["2.5.29.37"];
                        if (x509KeyUsageExtension != null && x509EnhancedKeyUsageExtension != null)
                        {
                            if ((x509KeyUsageExtension.KeyUsages & ServicePointManager.ChainValidationHelper.s_flags) == System.Security.Cryptography.X509Certificates.X509KeyUsageFlags.None)
                            {
                                result = false;
                            }
                            else
                            {
                                result = (x509EnhancedKeyUsageExtension.EnhancedKeyUsages["1.3.6.1.5.5.7.3.1"] != null || x509EnhancedKeyUsageExtension.EnhancedKeyUsages["2.16.840.1.113730.4.1"] != null);
                            }
                        }
                        else if (x509KeyUsageExtension != null)
                        {
                            result = ((x509KeyUsageExtension.KeyUsages & ServicePointManager.ChainValidationHelper.s_flags) != System.Security.Cryptography.X509Certificates.X509KeyUsageFlags.None);
                        }
                        else if (x509EnhancedKeyUsageExtension != null)
                        {
                            result = (x509EnhancedKeyUsageExtension.EnhancedKeyUsages["1.3.6.1.5.5.7.3.1"] != null || x509EnhancedKeyUsageExtension.EnhancedKeyUsages["2.16.840.1.113730.4.1"] != null);
                        }
                        else
                        {
                            System.Security.Cryptography.X509Certificates.X509Extension x509Extension = cert.Extensions["2.16.840.1.113730.1.1"];
                            if (x509Extension != null)
                            {
                                string text = x509Extension.NetscapeCertType(false);
                                result = (text.IndexOf("SSL Server Authentication") != -1);
                            }
                            else
                            {
                                result = true;
                            }
                        }
                    }
                }
                catch (Exception arg)
                {
                    Console.Error.WriteLine("ERROR processing certificate: {0}", arg);
                    Console.Error.WriteLine("Please, report this problem to the Mono team");
                    result = false;
                }
                return(result);
            }
Ejemplo n.º 20
0
		[Category ("NotDotNet")] // FriendlyName should not only be English.
		public void Build_X509EnhancedKeyUsageExtension ()
		{
			AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
			Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (true), "Format(true)");
			Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (false), "Format(false)");
			aed.Oid = new Oid ("2.5.29.37");
			// and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
			// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
			Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
			Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)");
			// compare with the output of the "appropriate" class
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, false);
			// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
			Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, eku.Format (true), "eku.Format(true)");
			Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", eku.Format (false), "eku.Format(false)");
		}
		public void ConstructorEmpty_EnhancedKeyUsages ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ();
			Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "EnhancedKeyUsages");
		}
		public void CopyFrom_Self ()
		{
			OidCollection oc = new OidCollection ();
			oc.Add (new Oid ("1.2.3.4"));
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (oc, true);
			Assert.IsTrue (eku.Critical, "Critical");
			byte[] raw = eku.RawData;
			Assert.AreEqual ("30-05-06-03-2A-03-04", BitConverter.ToString (raw), "RawData");

			AsnEncodedData aed = new AsnEncodedData (raw);
			X509EnhancedKeyUsageExtension copy = new X509EnhancedKeyUsageExtension (aed, false);
			Assert.IsFalse (copy.Critical, "Critical");
			Assert.AreEqual (7, copy.RawData.Length, "RawData");	// original Oid ignored
			Assert.AreEqual (oid, copy.Oid.Value, "Oid.Value");
			// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
			//Assert.AreEqual (fname, copy.Oid.FriendlyName, "Oid.FriendlyName");
			Assert.AreEqual (1, copy.EnhancedKeyUsages.Count, "EnhancedKeyUsages");
			Assert.AreEqual ("1.2.3.4", copy.EnhancedKeyUsages[0].Value, "EnhancedKeyUsages Oid");
		}
		public void ConstructorAsnEncodedData_Null ()
		{
			X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension ((AsnEncodedData)null, true);
		}