Beispiel #1
0
        static string[] CopyIssuers(int count, IntPtr sizesPtr, IntPtr dataPtr)
        {
            if (count == 0 || sizesPtr == IntPtr.Zero || dataPtr == IntPtr.Zero)
            {
                return(null);
            }
            var sizes = new int [count];

            Marshal.Copy(sizesPtr, sizes, 0, count);
            var data = new IntPtr [count];

            Marshal.Copy(dataPtr, data, 0, count);

            var issuers = new string [count];

            for (int i = 0; i < count; i++)
            {
                var buffer = new byte [sizes [i]];
                Marshal.Copy(data[i], buffer, 0, buffer.Length);
                using (var xname = MonoBtlsX509Name.CreateFromData(buffer, false))
                    issuers[i] = MonoBtlsUtils.FormatName(xname, true, ", ", true);
            }

            return(issuers);
        }
Beispiel #2
0
 public void Add(MonoBtlsX509Name name)
 {
     CheckThrow();
     mono_btls_x509_name_list_add(
         Handle.DangerousGetHandle(),
         name.Handle.DangerousGetHandle());
 }
Beispiel #3
0
 static int OnGetBySubject(IntPtr instance, IntPtr name_ptr, out IntPtr x509_ptr)
 {
     try {
         MonoBtlsX509LookupMono obj;
         MonoBtlsX509Name.BoringX509NameHandle name_handle = null;
         try {
             obj         = (MonoBtlsX509LookupMono)GCHandle.FromIntPtr(instance).Target;
             name_handle = new MonoBtlsX509Name.BoringX509NameHandle(name_ptr, false);
             MonoBtlsX509Name name_obj = new MonoBtlsX509Name(name_handle);
             var x509 = obj.OnGetBySubject(name_obj);
             if (x509 != null)
             {
                 x509_ptr = x509.Handle.StealHandle();
                 return(1);
             }
             else
             {
                 x509_ptr = IntPtr.Zero;
                 return(0);
             }
         } finally {
             if (name_handle != null)
             {
                 name_handle.Dispose();
             }
         }
     } catch (Exception ex) {
         Console.WriteLine("LOOKUP METHOD - GET BY SUBJECT EX: {0}", ex);
         x509_ptr = IntPtr.Zero;
         return(0);
     }
 }
Beispiel #4
0
        public MonoBtlsX509 LookupBySubject(MonoBtlsX509Name name)
        {
            var handle = mono_btls_x509_store_ctx_get_by_subject(
                Handle.DangerousGetHandle(), name.Handle.DangerousGetHandle());

            if (handle == IntPtr.Zero)
            {
                return(null);
            }
            return(new MonoBtlsX509(new MonoBtlsX509.BoringX509Handle(handle)));
        }
		protected override MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name)
		{
			Console.WriteLine ("COLLECTION LOOKUP: {0:x} - {1}", name.GetHash (), name.GetString ());
			Initialize ();

			var hash = name.GetHash ();
			for (int i = 0; i < certificates.Length; i++) {
				if (hashes [i] == hash)
					return certificates [i];
			}

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

            var hash = name.GetHash();

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

            return(null);
        }
        public static string FormatName(MonoBtlsX509Name name, X500DistinguishedNameFlags flag)
        {
            if ((flag != 0) && ((flag & AllFlags) == 0))
            {
                throw new ArgumentException("flag");
            }

            if (name.GetEntryCount() == 0)
            {
                return(String.Empty);
            }

            // Mono.Security reversed isn't the same as fx 2.0 (which is the reverse of 1.x)
            bool   reversed  = ((flag & X500DistinguishedNameFlags.Reversed) != 0);
            bool   quotes    = ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
            string separator = GetSeparator(flag);

            return(FormatName(name, reversed, separator, quotes));
        }
Beispiel #8
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);
        }
Beispiel #9
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);
        }
        public static string FormatName(MonoBtlsX509Name name, bool reversed, string separator, bool quotes)
        {
            var           count = name.GetEntryCount();
            StringBuilder sb    = new StringBuilder();

            if (reversed)
            {
                for (int i = count - 1; i >= 0; i--)
                {
                    AppendEntry(sb, name, i, separator, quotes);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    AppendEntry(sb, name, i, separator, quotes);
                }
            }

            return(sb.ToString());
        }
Beispiel #11
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);
            }
        }
		public MonoBtlsX509 LookupBySubject (MonoBtlsX509Name name)
		{
			var handle = mono_btls_x509_lookup_by_subject (
				Handle.DangerousGetHandle (),
				name.Handle.DangerousGetHandle ());
			if (handle == IntPtr.Zero)
				return null;
			return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
		}
Beispiel #13
0
 protected abstract MonoBtlsX509 OnGetBySubject(MonoBtlsX509Name name);
		protected override MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name)
		{
			return AndroidPlatform.CertStoreLookup (name);
		}
        static bool AppendEntry(StringBuilder sb, MonoBtlsX509Name name, int index, string separator, bool quotes)
        {
            var type = name.GetEntryType(index);

            if (type < 0)
            {
                return(false);
            }
            else if (type == 0)
            {
                var oidValue = name.GetEntryOidData(index);
                if (Compare(oidValue, emailOid))
                {
                    type = MonoBtlsX509NameEntryType.Email;
                }
            }
            int tag;
            var text = name.GetEntryValue(index, out tag);

            if (text == null)
            {
                return(false);
            }
            var oid = name.GetEntryOid(index);

            if (oid == null)
            {
                return(false);
            }

            if (sb.Length > 0)
            {
                sb.Append(separator);
            }

            switch (type)
            {
            case MonoBtlsX509NameEntryType.CountryName:
                sb.Append("C=");
                break;

            case MonoBtlsX509NameEntryType.OrganizationName:
                sb.Append("O=");
                break;

            case MonoBtlsX509NameEntryType.OrganizationalUnitName:
                sb.Append("OU=");
                break;

            case MonoBtlsX509NameEntryType.CommonName:
                sb.Append("CN=");
                break;

            case MonoBtlsX509NameEntryType.LocalityName:
                sb.Append("L=");
                break;

            case MonoBtlsX509NameEntryType.StateOrProvinceName:
                sb.Append("S=");                        // NOTE: RFC2253 uses ST=
                break;

            case MonoBtlsX509NameEntryType.StreetAddress:
                sb.Append("STREET=");
                break;

            case MonoBtlsX509NameEntryType.DomainComponent:
                sb.Append("DC=");
                break;

            case MonoBtlsX509NameEntryType.UserId:
                sb.Append("UID=");
                break;

            case MonoBtlsX509NameEntryType.Email:
                sb.Append("E=");                        // NOTE: Not part of RFC2253
                break;

            case MonoBtlsX509NameEntryType.DnQualifier:
                sb.Append("dnQualifier=");
                break;

            case MonoBtlsX509NameEntryType.Title:
                sb.Append("T=");
                break;

            case MonoBtlsX509NameEntryType.Surname:
                sb.Append("SN=");
                break;

            case MonoBtlsX509NameEntryType.GivenName:
                sb.Append("G=");
                break;

            case MonoBtlsX509NameEntryType.Initial:
                sb.Append("I=");
                break;

            case MonoBtlsX509NameEntryType.SerialNumber:
                sb.Append("SERIALNUMBER=");
                break;

            default:
                // unknown OID
                sb.Append("OID.");                      // NOTE: Not present as RFC2253
                sb.Append(oid);
                sb.Append("=");
                break;
            }

            // 16bits or 8bits string ? TODO not complete (+special chars!)
            char[] specials = { ',', '+', '"', '\\', '<', '>', ';' };
            if (quotes && tag != 0x1E)
            {
                if ((text.IndexOfAny(specials, 0, text.Length) > 0) ||
                    text.StartsWith(" ") || (text.EndsWith(" ")))
                {
                    text = "\"" + text + "\"";
                }
            }

            sb.Append(text);
            return(true);
        }
Beispiel #16
0
		internal static MonoBtlsX509 CertStoreLookup (MonoBtlsX509Name name)
		{
			var hash = name.GetHash ();
			var hashOld = name.GetHashOld ();
			var result = certStoreLookup (hash, false);
			if (result == null)
				result = certStoreLookup (hashOld, false);
			if (result == null)
				result = certStoreLookup (hash, true);
			if (result == null)
				result = certStoreLookup (hashOld, true);

			if (result == null)
				return null;

			return MonoBtlsX509.LoadFromData (result, MonoBtlsX509Format.DER);
		}
		static bool AppendEntry (StringBuilder sb, MonoBtlsX509Name name, int index, string separator, bool quotes)
		{
			var type = name.GetEntryType (index);
			if (type < 0)
				return false;
			else if (type == 0) {
				var oidValue = name.GetEntryOidData (index);
				if (Compare (oidValue, emailOid))
					type = MonoBtlsX509NameEntryType.Email;
			}
			int tag;
			var text = name.GetEntryValue (index, out tag);
			if (text == null)
				return false;
			var oid = name.GetEntryOid (index);
			if (oid == null)
				return false;

			if (sb.Length > 0)
				sb.Append (separator);

			switch (type) {
			case MonoBtlsX509NameEntryType.CountryName:
				sb.Append ("C=");
				break;
			case MonoBtlsX509NameEntryType.OrganizationName:
				sb.Append ("O=");
				break;
			case MonoBtlsX509NameEntryType.OrganizationalUnitName:
				sb.Append ("OU=");
				break;
			case MonoBtlsX509NameEntryType.CommonName:
				sb.Append ("CN=");
				break;
			case MonoBtlsX509NameEntryType.LocalityName:
				sb.Append ("L=");
				break;
			case MonoBtlsX509NameEntryType.StateOrProvinceName:
				sb.Append ("S=");       // NOTE: RFC2253 uses ST=
				break;
			case MonoBtlsX509NameEntryType.StreetAddress:
				sb.Append ("STREET=");
				break;
			case MonoBtlsX509NameEntryType.DomainComponent:
				sb.Append ("DC=");
				break;
			case MonoBtlsX509NameEntryType.UserId:
				sb.Append ("UID=");
				break;
			case MonoBtlsX509NameEntryType.Email:
				sb.Append ("E=");       // NOTE: Not part of RFC2253
				break;
			case MonoBtlsX509NameEntryType.DnQualifier:
				sb.Append ("dnQualifier=");
				break;
			case MonoBtlsX509NameEntryType.Title:
				sb.Append ("T=");
				break;
			case MonoBtlsX509NameEntryType.Surname:
				sb.Append ("SN=");
				break;
			case MonoBtlsX509NameEntryType.GivenName:
				sb.Append ("G=");
				break;
			case MonoBtlsX509NameEntryType.Initial:
				sb.Append ("I=");
				break;
			default:
				// unknown OID
				sb.Append ("OID.");     // NOTE: Not present as RFC2253
				sb.Append (oid);
				sb.Append ("=");
				break;
			}

			// 16bits or 8bits string ? TODO not complete (+special chars!)
			char[] specials = { ',', '+', '"', '\\', '<', '>', ';' };
			if (quotes && tag != 0x1E) {
				if ((text.IndexOfAny (specials, 0, text.Length) > 0) ||
				    text.StartsWith (" ") || (text.EndsWith (" ")))
					text = "\"" + text + "\"";
			}

			sb.Append (text);
			return true;
		}
		public void Add (MonoBtlsX509Name name)
		{
			CheckThrow ();
			mono_btls_x509_name_list_add (
				Handle.DangerousGetHandle (),
				name.Handle.DangerousGetHandle ());
		}
		public static string FormatName (MonoBtlsX509Name name, bool reversed, string separator, bool quotes)
		{
			var count = name.GetEntryCount ();
			StringBuilder sb = new StringBuilder ();

			if (reversed) {
				for (int i = count - 1; i >= 0; i--) {
					AppendEntry (sb, name, i, separator, quotes);
				}
			} else {
				for (int i = 0; i < count; i++) {
					AppendEntry (sb, name, i, separator, quotes);
				}
			}

			return sb.ToString ();
		}
		public static string FormatName (MonoBtlsX509Name name, X500DistinguishedNameFlags flag)
		{
			if ((flag != 0) && ((flag & AllFlags) == 0))
				throw new ArgumentException ("flag");

			if (name.GetEntryCount () == 0)
				return String.Empty;

			// Mono.Security reversed isn't the same as fx 2.0 (which is the reverse of 1.x)
			bool reversed = ((flag & X500DistinguishedNameFlags.Reversed) != 0);
			bool quotes = ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
			string separator = GetSeparator (flag);

			return FormatName (name, reversed, separator, quotes);
		}
		static int OnGetBySubject (IntPtr instance, IntPtr name_ptr, out IntPtr x509_ptr)
		{
			try {
				MonoBtlsX509LookupMono obj;
				MonoBtlsX509Name.BoringX509NameHandle name_handle = null;
				try {
					obj = (MonoBtlsX509LookupMono)GCHandle.FromIntPtr (instance).Target;
					name_handle = new MonoBtlsX509Name.BoringX509NameHandle (name_ptr, false);
					MonoBtlsX509Name name_obj = new MonoBtlsX509Name (name_handle);
					var x509 = obj.OnGetBySubject (name_obj);
					if (x509 != null) {
						x509_ptr = x509.Handle.StealHandle ();
						return 1;
					} else {
						x509_ptr = IntPtr.Zero;
						return 0;
					}
				} finally {
					if (name_handle != null)
						name_handle.Dispose ();
				}
			} catch (Exception ex) {
				Console.WriteLine ("LOOKUP METHOD - GET BY SUBJECT EX: {0}", ex);
				x509_ptr = IntPtr.Zero;
				return 0;
			}
		}
		protected abstract MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name);
Beispiel #23
0
 protected override MonoBtlsX509 OnGetBySubject(MonoBtlsX509Name name)
 {
     return(AndroidPlatform.CertStoreLookup(name));
 }