X509CertificateImplBtls (X509CertificateImplBtls other)
		{
			disallowFallback = other.disallowFallback;
			x509 = other.x509 != null ? other.x509.Copy () : null;
			privateKey = other.privateKey != null ? other.privateKey.Copy () : null;
			if (other.intermediateCerts != null)
				intermediateCerts = other.intermediateCerts.Clone ();
		}
 protected override void Dispose(bool disposing)
 {
     if (x509 != null)
     {
         x509.Dispose();
         x509 = null;
     }
 }
Exemplo n.º 3
0
        public void AddCertificate(MonoBtlsX509 x509)
        {
            var ret = mono_btls_x509_store_add_cert(
                Handle.DangerousGetHandle(),
                x509.Handle.DangerousGetHandle());

            CheckError(ret);
        }
Exemplo n.º 4
0
		X509CertificateImplBtls (X509CertificateImplBtls other)
		{
			disallowFallback = other.disallowFallback;
			x509 = other.x509 != null ? other.x509.Copy () : null;
			nativePrivateKey = other.nativePrivateKey != null ? other.nativePrivateKey.Copy () : null;
			fallback = other.fallback != null ? (X509Certificate2Impl)other.fallback.Clone () : null;
			if (other.intermediateCerts != null)
				intermediateCerts = other.intermediateCerts.Clone ();
		}
        X509CertificateImplBtls(X509CertificateImplBtls other)
        {
            x509 = other.x509 != null?other.x509.Copy() : null;

            nativePrivateKey = other.nativePrivateKey != null?other.nativePrivateKey.Copy() : null;

            if (other.intermediateCerts != null)
            {
                intermediateCerts = other.intermediateCerts.Clone();
            }
        }
Exemplo n.º 6
0
        public static MonoBtlsX509 GetBtlsCertificate(X509Certificate certificate)
        {
            var impl = certificate.Impl as X509CertificateImplBtls;

            if (impl != null)
            {
                return(impl.X509.Copy());
            }

            return(MonoBtlsX509.LoadFromData(certificate.GetRawCertData(), MonoBtlsX509Format.DER));
        }
Exemplo n.º 7
0
        X509CertificateImplBtls(X509CertificateImplBtls other)
        {
            disallowFallback = other.disallowFallback;
            x509             = other.x509 != null?other.x509.Copy() : null;

            privateKey = other.privateKey != null?other.privateKey.Copy() : null;

            if (other.intermediateCerts != null)
            {
                intermediateCerts = other.intermediateCerts.Clone();
            }
        }
Exemplo n.º 8
0
 void Import(byte[] data)
 {
     // Does it look like PEM?
     if ((data.Length > 0) && (data [0] != 0x30))
     {
         x509 = MonoBtlsX509.LoadFromData(data, MonoBtlsX509Format.PEM);
     }
     else
     {
         x509 = MonoBtlsX509.LoadFromData(data, MonoBtlsX509Format.DER);
     }
 }
Exemplo n.º 9
0
        public MonoBtlsX509Revoked GetByCert(MonoBtlsX509 x509)
        {
            var revoked = mono_btls_x509_crl_get_by_cert(
                Handle.DangerousGetHandle(),
                x509.Handle.DangerousGetHandle());

            if (revoked == IntPtr.Zero)
            {
                return(null);
            }
            return(new MonoBtlsX509Revoked(new MonoBtlsX509Revoked.BoringX509RevokedHandle(revoked)));
        }
Exemplo n.º 10
0
        X509CertificateImplBtls(X509CertificateImplBtls other)
        {
            disallowFallback = other.disallowFallback;
            x509             = other.x509 != null?other.x509.Copy() : null;

            nativePrivateKey = other.nativePrivateKey != null?other.nativePrivateKey.Copy() : null;

            fallback = other.fallback != null ? (X509Certificate2Impl)other.fallback.Clone() : null;
            if (other.intermediateCerts != null)
            {
                intermediateCerts = other.intermediateCerts.Clone();
            }
        }
        public override bool Equals(X509CertificateImpl other, out bool result)
        {
            var otherBoringImpl = other as X509CertificateImplBtls;

            if (otherBoringImpl == null)
            {
                result = false;
                return(false);
            }

            result = MonoBtlsX509.Compare(X509, otherBoringImpl.X509) == 0;
            return(true);
        }
Exemplo n.º 12
0
        public void SetCertificate(MonoBtlsX509 x509)
        {
            CheckThrow();

            var ret = mono_btls_ssl_use_certificate(
                Handle.DangerousGetHandle(),
                x509.Handle.DangerousGetHandle());

            if (ret <= 0)
            {
                throw ThrowError();
            }
        }
Exemplo n.º 13
0
        public void AddIntermediateCertificate(MonoBtlsX509 x509)
        {
            CheckThrow();

            var ret = mono_btls_ssl_add_chain_certificate(
                Handle.DangerousGetHandle(),
                x509.Handle.DangerousGetHandle());

            if (ret <= 0)
            {
                throw ThrowError();
            }
        }
Exemplo n.º 14
0
		static void ConvertToNewFormat (string root, MonoBtlsX509 x509)
		{
			long hash = x509.GetSubjectNameHash ();

			string newName;
			int index = 0;
			do {
				newName = Path.Combine (root, string.Format ("{0:x8}.{1}", hash, index++));
			} while (File.Exists (newName));
			Console.WriteLine ("  new name: {0}", newName);

			using (var stream = new FileStream (newName, FileMode.Create))
			using (var bio = MonoBtlsBio.CreateMonoStream (stream))
                                x509.ExportAsPEM (bio, true);
		}
 public override void Reset()
 {
     if (x509 != null)
     {
         x509.Dispose();
         x509 = null;
     }
     if (nativePrivateKey != null)
     {
         nativePrivateKey.Dispose();
         nativePrivateKey = null;
     }
     publicKey         = null;
     intermediateCerts = null;
 }
Exemplo n.º 16
0
        static void ConvertToNewFormat(string root, MonoBtlsX509 x509)
        {
            long hash = x509.GetSubjectNameHash();

            string newName;
            int    index = 0;

            do
            {
                newName = Path.Combine(root, string.Format("{0:x8}.{1}", hash, index++));
            } while (File.Exists(newName));
            Console.WriteLine("  new name: {0}", newName);

            using (var stream = new FileStream(newName, FileMode.Create))
                using (var bio = MonoBtlsBio.CreateMonoStream(stream))
                    x509.ExportAsPEM(bio, true);
        }
Exemplo n.º 17
0
        void Initialize()
        {
            if (certificates != null)
            {
                return;
            }

            hashes       = new long [collection.Count];
            certificates = new MonoBtlsX509 [collection.Count];
            for (int i = 0; i < collection.Count; i++)
            {
                // Create new 'X509 *' instance since we need to modify it to add the
                // trust settings.
                var data = collection [i].GetRawCertData();
                certificates [i] = MonoBtlsX509.LoadFromData(data, MonoBtlsX509Format.DER);
                certificates [i].AddExplicitTrust(trust);
                hashes [i] = certificates [i].GetSubjectNameHash();
            }
        }
Exemplo n.º 18
0
        protected override MonoBtlsX509 OnGetBySubject(MonoBtlsX509Name name)
        {
            Initialize();

            var          hash  = name.GetHash();
            MonoBtlsX509 found = null;

            for (int i = 0; i < certificates.Length; i++)
            {
                if (hashes [i] != hash)
                {
                    continue;
                }
                found = certificates [i];
                AddCertificate(found);
            }

            return(found);
        }
Exemplo n.º 19
0
        protected override MonoBtlsX509 OnGetBySubject(MonoBtlsX509Name name)
        {
            Console.WriteLine("COLLECTION LOOKUP: {0:x} - {1}", name.GetHash(), name.GetString());
            Initialize();

            var          hash  = name.GetHash();
            MonoBtlsX509 found = null;

            for (int i = 0; i < certificates.Length; i++)
            {
                if (hashes [i] != hash)
                {
                    continue;
                }
                found = certificates [i];
                AddCertificate(found);
            }

            return(found);
        }
        void ImportPkcs12(byte[] data, SafePasswordHandle password)
        {
            using (var pkcs12 = new MonoBtlsPkcs12()) {
                if (password == null || password.IsInvalid)
                {
                    try {
                        // Support both unencrypted PKCS#12..
                        pkcs12.Import(data, null);
                    } catch {
                        // ..and PKCS#12 encrypted with an empty password
                        using (var empty = new SafePasswordHandle(string.Empty))
                            pkcs12.Import(data, empty);
                    }
                }
                else
                {
                    pkcs12.Import(data, password);
                }

                x509 = pkcs12.GetCertificate(0);
                if (pkcs12.HasPrivateKey)
                {
                    nativePrivateKey = pkcs12.GetPrivateKey();
                }
                if (pkcs12.Count > 1)
                {
                    intermediateCerts = new X509CertificateImplCollection();
                    for (int i = 0; i < pkcs12.Count; i++)
                    {
                        using (var ic = pkcs12.GetCertificate(i)) {
                            if (MonoBtlsX509.Compare(ic, x509) == 0)
                            {
                                continue;
                            }
                            var impl = new X509CertificateImplBtls(ic);
                            intermediateCerts.Add(impl, true);
                        }
                    }
                }
            }
        }
Exemplo n.º 21
0
 public override void Reset()
 {
     if (x509 != null)
     {
         x509.Dispose();
         x509 = null;
     }
     if (nativePrivateKey != null)
     {
         nativePrivateKey = null;
     }
     subjectName       = null;
     issuerName        = null;
     archived          = false;
     publicKey         = null;
     intermediateCerts = null;
     if (fallback != null)
     {
         fallback.Reset();
     }
 }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            if (!MonoBtlsProvider.IsSupported())
            {
                Console.Error.WriteLine("BTLS is not supported in this runtime!");
                Environment.Exit(255);
            }

            var configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            configPath = Path.Combine(configPath, ".mono");

            var oldStorePath = Path.Combine(configPath, "certs", "Trust");
            var newStorePath = MonoBtlsX509StoreManager.GetStorePath(MonoBtlsX509StoreType.UserTrustedRoots);

            if (!Directory.Exists(oldStorePath))
            {
                Console.WriteLine("Old trust store {0} does not exist.");
                Environment.Exit(255);
            }

            if (Directory.Exists(newStorePath))
            {
                Directory.Delete(newStorePath, true);
            }
            Directory.CreateDirectory(newStorePath);

            var oldfiles = Directory.GetFiles(oldStorePath, "*.cer");

            Console.WriteLine("Found {0} files in the old store.", oldfiles.Length);

            foreach (var file in oldfiles)
            {
                Console.WriteLine("Converting {0}.", file);
                var data = File.ReadAllBytes(file);
                using (var x509 = MonoBtlsX509.LoadFromData(data, MonoBtlsX509Format.DER)) {
                    ConvertToNewFormat(newStorePath, x509);
                }
            }
        }
Exemplo n.º 23
0
        void SetPrivateCertificate(X509CertificateImplBtls privateCert)
        {
            Debug("SetPrivateCertificate: {0}", privateCert);
            ssl.SetCertificate(privateCert.X509);
            ssl.SetPrivateKey(privateCert.NativePrivateKey);
            var intermediate = privateCert.IntermediateCertificates;

            if (intermediate == null)
            {
                /* Intermediate certificates are lost in the translation from X509Certificate(2) to X509CertificateImplBtls, so we need to restore them somehow. */
                var chain = new System.Security.Cryptography.X509Certificates.X509Chain(false);
                /* Let's try to recover as many as we can. */
                chain.ChainPolicy.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck;
                chain.Build(new System.Security.Cryptography.X509Certificates.X509Certificate2(privateCert.X509.GetRawData(MonoBtlsX509Format.DER), ""));
                var elems = chain.ChainElements;
                for (int j = 1; j < elems.Count; j++)
                {
                    var cert = elems[j].Certificate;
                    /* If self-signed, it's a root and should not be sent. */
                    if (cert.SubjectName.RawData.SequenceEqual(cert.IssuerName.RawData))
                    {
                        break;
                    }
                    ssl.AddIntermediateCertificate(MonoBtlsX509.LoadFromData(cert.RawData, MonoBtlsX509Format.DER));
                }
            }
            else
            {
                for (int i = 0; i < intermediate.Count; i++)
                {
                    var impl = (X509CertificateImplBtls)intermediate [i];
                    Debug("SetPrivateCertificate - add intermediate: {0}", impl);
                    ssl.AddIntermediateCertificate(impl.X509);
                }
            }
        }
Exemplo n.º 24
0
        protected override MonoBtlsX509 OnGetBySubject(MonoBtlsX509Name name)
        {
            byte[] raw_data  = name.GetRawData(false);
            var    x509_name = new X500DistinguishedName(raw_data);

            using (var certstore = new X509Store(StoreName.Root, Location))
            {
                try
                {
                    certstore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                }
                catch (CryptographicException)
                {
                    return(null);
                }
                var matches = certstore.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, x509_name.Name, false);
                if (matches.Count >= 1)
                {
                    // FIXME: Which one to use if more than 1 match?
                    return(MonoBtlsX509.LoadFromData(matches[0].RawData, MonoBtlsX509Format.DER));
                }
                return(null);
            }
        }
Exemplo n.º 25
0
 protected void AddCertificate(MonoBtlsX509 certificate)
 {
     lookup.AddCertificate(certificate);
 }
Exemplo n.º 26
0
 internal X509CertificateImplBtls(MonoBtlsX509 x509, bool disallowFallback = false)
 {
     this.disallowFallback = disallowFallback;
     this.x509             = x509.Copy();
 }
Exemplo n.º 27
0
		internal static long GetSubjectNameHash (MonoBtlsX509 x509)
		{
			using (var subject = x509.GetSubjectName ())
				return subject.GetHash ();
		}
 internal X509CertificateImplBtls(byte[] data, MonoBtlsX509Format format)
 {
     x509 = MonoBtlsX509.LoadFromData(data, format);
 }
Exemplo n.º 29
0
		public void AddIntermediateCertificate (MonoBtlsX509 x509)
		{
			CheckThrow ();

			var ret = mono_btls_ssl_add_chain_certificate (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
			if (ret <= 0)
				throw ThrowError ();
		}
Exemplo n.º 30
0
 public void AddCertificate(MonoBtlsX509 x509)
 {
     mono_btls_pkcs12_add_cert(
         Handle.DangerousGetHandle(),
         x509.Handle.DangerousGetHandle());
 }
 internal X509CertificateImplBtls(MonoBtlsX509 x509)
 {
     this.x509 = x509.Copy();
 }
Exemplo n.º 32
0
 internal void AddCertificate(MonoBtlsX509 certificate)
 {
     store.AddCertificate(certificate);
 }
Exemplo n.º 33
0
		protected override void Dispose (bool disposing)
		{
			if (x509 != null) {
				x509.Dispose ();
				x509 = null;
			}
		}
Exemplo n.º 34
0
		internal X509CertificateImplBtls (byte[] data, MonoBtlsX509Format format, bool disallowFallback = false)
		{
			this.disallowFallback = disallowFallback;
			x509 = MonoBtlsX509.LoadFromData (data, format);
		}
Exemplo n.º 35
0
		void Import (byte[] data)
		{
			// Does it look like PEM?
			if ((data.Length > 0) && (data [0] != 0x30))
				x509 = MonoBtlsX509.LoadFromData (data, MonoBtlsX509Format.PEM);
			else
				x509 = MonoBtlsX509.LoadFromData (data, MonoBtlsX509Format.DER);
		}
Exemplo n.º 36
0
		internal X509CertificateImplBtls (MonoBtlsX509 x509, bool disallowFallback = false)
		{
			this.disallowFallback = disallowFallback;
			this.x509 = x509.Copy ();
		}
Exemplo n.º 37
0
		public MonoBtlsX509Revoked GetByCert (MonoBtlsX509 x509)
		{
			var revoked = mono_btls_x509_crl_get_by_cert (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
			if (revoked == IntPtr.Zero)
				return null;
			return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
		}
Exemplo n.º 38
0
		public static X509Certificate CreateCertificate (MonoBtlsX509 x509)
		{
			using (var impl = new X509CertificateImplBtls (x509, true))
				return new X509Certificate (impl);
		}
Exemplo n.º 39
0
		public void AddCertificate (MonoBtlsX509 x509)
		{
			var ret = mono_btls_x509_store_add_cert (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
			CheckError (ret);
		}
Exemplo n.º 40
0
 public static int Compare(MonoBtlsX509 a, MonoBtlsX509 b)
 {
     return(mono_btls_x509_cmp(
                a.Handle.DangerousGetHandle(),
                b.Handle.DangerousGetHandle()));
 }
Exemplo n.º 41
0
		internal static void ExportAsPEM (MonoBtlsX509 x509, Stream stream, bool includeHumanReadableForm)
		{
			using (var bio = MonoBtlsBio.CreateMonoStream (stream)) {
				x509.ExportAsPEM (bio, includeHumanReadableForm);
			}
		}
Exemplo n.º 42
0
		public static int Compare (MonoBtlsX509 a, MonoBtlsX509 b)
		{
			return mono_btls_x509_cmp (
				a.Handle.DangerousGetHandle (),
				b.Handle.DangerousGetHandle ());
		}
Exemplo n.º 43
0
		public void SetCertificate (MonoBtlsX509 x509)
		{
			CheckThrow ();

			var ret = mono_btls_ssl_use_certificate (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
			if (ret <= 0)
				throw ThrowError ();
		}
Exemplo n.º 44
0
 public static X509Certificate CreateCertificate(MonoBtlsX509 x509)
 {
     using (var impl = new X509CertificateImplBtls(x509))
         return(new X509Certificate(impl));
 }
Exemplo n.º 45
0
		public void AddCertificate (MonoBtlsX509 x509)
		{
			mono_btls_pkcs12_add_cert (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
		}
Exemplo n.º 46
0
 internal X509CertificateImplBtls(byte[] data, MonoBtlsX509Format format, bool disallowFallback = false)
 {
     this.disallowFallback = disallowFallback;
     x509 = MonoBtlsX509.LoadFromData(data, format);
 }
Exemplo n.º 47
0
		public override void Reset ()
		{
			if (x509 != null) {
				x509.Dispose ();
				x509 = null;
			}
			if (nativePrivateKey != null) {
				nativePrivateKey = null;
			}
			subjectName = null;
			issuerName = null;
			archived = false;
			publicKey = null;
			intermediateCerts = null;
			if (fallback != null)
				fallback.Reset ();
		}
Exemplo n.º 48
0
		void ImportPkcs12 (byte[] data, string password)
		{
			using (var pkcs12 = new MonoBtlsPkcs12 ()) {
				if (string.IsNullOrEmpty (password)) {
					try {
						// Support both unencrypted PKCS#12..
						pkcs12.Import (data, null);
					} catch {
						// ..and PKCS#12 encrypted with an empty password
						pkcs12.Import (data, string.Empty);
					}
				} else {
					pkcs12.Import (data, password);
				}

				x509 = pkcs12.GetCertificate (0);
				if (pkcs12.HasPrivateKey)
					nativePrivateKey = pkcs12.GetPrivateKey ();
				if (pkcs12.Count > 1) {
					intermediateCerts = new X509CertificateImplCollection ();
					for (int i = 0; i < pkcs12.Count; i++) {
						using (var ic = pkcs12.GetCertificate (i)) {
							if (MonoBtlsX509.Compare (ic, x509) == 0)
								continue;
							var impl = new X509CertificateImplBtls (ic, true);
							intermediateCerts.Add (impl, true);
						}
					}
				}
			}
		}