/// <summary>
        /// Clear a password from the keychain
        /// </summary>
        /// <param name='username'>
        /// Username of user to clear
        /// </param>
        public static void ClearPassword(string username)
        {
            var searchRecord = new SecRecord(SecKind.InternetPassword)
            {
                Service = ServiceName,
                Account = username
            };

            SecKeyChain.Remove(searchRecord);
        }
Esempio n. 2
0
		public string EncryptString (string serverName, string accountName, string password)
		{
			if (string.IsNullOrEmpty (password))
				return null;
			byte[] passwordBytes = Encoding.UTF8.GetBytes (password);
			var code = SecKeyChain.AddInternetPassword(serverName, accountName, passwordBytes);
			if (code == SecStatusCode.DuplicateItem) {
				var queryRec = new SecRecord (SecKind.InternetPassword) { Server = serverName, Account = accountName };
				queryRec = SecKeyChain.QueryAsRecord (queryRec, out code);
					
				if (code == SecStatusCode.Success ) {
					SecKeyChain.Remove (queryRec);
				}
				code = SecKeyChain.AddInternetPassword(serverName, accountName, passwordBytes);
			}
			return "keychain";
		}
Esempio n. 3
0
        public static void SetPat(string pat)
        {
            SecRecord searchRecord;
            var record = FetchKeychainRecord (out searchRecord);

            if (record == null)
            {
                record = new SecRecord(SecKind.InternetPassword)
                {
                    Server = _serviceName,
                    Label = _serviceName,
                    Account = _account,
                    ValueData = NSData.FromString(pat)
                };

                SecKeyChain.Add(record);
                return;
            }

            record.ValueData = NSData.FromString(pat);
            SecKeyChain.Update(searchRecord, record);
        }
Esempio n. 4
0
        public static SecRecord QueryAsRecord(SecRecord query, out SecStatusCode result)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                SetLimit (copy, 1);
                copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnAttributes);
                copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnData);
                IntPtr ptr;
                result = SecItem.SecItemCopyMatching (copy.Handle, out ptr);
                if (result == SecStatusCode.Success)
                    return new SecRecord (new NSMutableDictionary (new NSDictionary (ptr)));
                return null;
            }
        }
Esempio n. 5
0
 public static NSData[] QueryAsData(SecRecord query, int max)
 {
     SecStatusCode status;
     return QueryAsData (query, false, max, out status);
 }
Esempio n. 6
0
 public static NSData QueryAsData(SecRecord query)
 {
     SecStatusCode status;
     return QueryAsData (query, false, out status);
 }
Esempio n. 7
0
        public static NSData[] QueryAsData(SecRecord query, bool wantPersistentReference, int max, out SecStatusCode status)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                var n = SetLimit (copy, max);
                copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnData);

                IntPtr ptr;
                status = SecItem.SecItemCopyMatching (copy.Handle, out ptr);
                n = null;
                if (status == SecStatusCode.Success){
                    if (max == 1)
                        return new NSData [] { new NSData (ptr) };
                    else
                        return NSArray.ArrayFromHandle<NSData> (ptr);
                }
                return null;
            }
        }
Esempio n. 8
0
        public static SecRecord QueryAsRecord(SecRecord query)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            SecRecord result;

            var code = TryQueryAsRecord (query, out result);
            if (code != SecStatusCode.Success)
                throw new SecurityException (code);
            return result;
        }
Esempio n. 9
0
 public static SecStatusCode Add(SecRecord record)
 {
     if (record == null)
         throw new ArgumentNullException ("record");
     return SecItem.SecItemAdd (record.queryDict.Handle, IntPtr.Zero);
 }
Esempio n. 10
0
        public static SecStatusCode Update(SecRecord query, SecRecord newAttributes)
        {
            if (query == null)
                throw new ArgumentNullException ("record");
            if (newAttributes == null)
                throw new ArgumentNullException ("newAttributes");

            return SecItem.SecItemUpdate (query.queryDict.Handle, newAttributes.queryDict.Handle);
        }
Esempio n. 11
0
		public static NSData [] QueryAsData (SecRecord query, bool wantPersistentReference, int max, out SecStatusCode status)
		{
			if (query == null)
				throw new ArgumentNullException ("query");

			using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
				var n = SetLimit (copy, max);
				copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnData);

				IntPtr ptr;
				status = SecItem.SecItemCopyMatching (copy.Handle, out ptr);
				n = null;
				if (status == SecStatusCode.Success){
					if (max == 1)
						return new NSData [] { new NSData (ptr, false) };

					var array = new NSArray (ptr);
					var records = new NSData [array.Count];
					for (uint i = 0; i < records.Length; i++)
						records [i] = new NSData (array.ValueAt (i), false);
					return records;
				}
				return null;
			}
		}
Esempio n. 12
0
        static object QueryAsConcreteType(SecRecord query)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            object result;

            var code = TryQueryAsConcreteType (query, out result);
            if (code != SecStatusCode.Success)
                throw new SecurityException (code);
            return result;
        }
Esempio n. 13
0
        public static SecStatusCode TryQueryAsRecord(SecRecord query, out SecRecord result)
        {
            if (query == null){
                result = null;
                return SecStatusCode.Param;
            }

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                copy.SetObject (CFBoolean.TrueObject, SecItem.ReturnAttributes);

                IntPtr ptr;
                var ret = SecItem.CopyMatching (copy, out ptr);
                if (ret == SecStatusCode.Success)
                    result = new SecRecord (new NSMutableDictionary (new NSDictionary (ptr)));
                else
                    result = null;
                return ret;
            }
        }
Esempio n. 14
0
        public static SecStatusCode TryQueryAsData(SecRecord query, bool wantPersistentReference, out NSData result)
        {
            if (query == null){
                result = null;
                return SecStatusCode.Param;
            }

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                copy.SetObject (CFBoolean.TrueObject, SecItem.ReturnData);

                IntPtr ptr;
                var ret = SecItem.CopyMatching (copy, out ptr);
                if (ret == SecStatusCode.Success)
                    result = new NSData (ptr);
                else
                    result = null;
                return ret;
            }
        }
Esempio n. 15
0
        public static SecStatusCode TryQueryAsConcreteType(SecRecord query, out object result)
        {
            if (query == null){
                result = null;
                return SecStatusCode.Param;
            }

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                copy.SetObject (CFBoolean.TrueObject, SecItem.ReturnRef);

                IntPtr ptr;
                var ret = SecItem.CopyMatching (copy, out ptr);
                if (ret == SecStatusCode.Success){
                    int cfType = CFType.GetTypeID (ptr);

                    if (cfType == SecCertificate.GetTypeID ())
                        result = new SecCertificate (ptr, true);
                    else if (cfType == SecKey.GetTypeID ())
                        result = new SecKey (ptr, true);
                    else if (cfType == SecIdentity.GetTypeID ())
                        result = new SecIdentity (ptr, true);
                    else
                        throw new Exception (String.Format ("Unexpected type: 0x{0:x}", ret));
                    result = null;
                } else
                    result = null;
                return ret;
            }
        }
Esempio n. 16
0
        public static SecRecord[] QueryAsRecord(SecRecord query, int max, out SecStatusCode result)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnAttributes);
                var n = SetLimit (copy, max);

                IntPtr ptr;
                result = SecItem.SecItemCopyMatching (copy.Handle, out ptr);
                n = null;
                if (result == SecStatusCode.Success){
                    var dicts = NSArray.ArrayFromHandle<NSDictionary> (ptr);
                    var records = new SecRecord [dicts.Length];
                    for (int i = 0; i < dicts.Length; i++)
                        records [i] = new SecRecord (new NSMutableDictionary (dicts [i]));
                    return records;
                }
                return null;
            }
        }
Esempio n. 17
0
 public static SecStatusCode Remove(SecRecord record)
 {
     if (record == null)
         throw new ArgumentNullException ("record");
     return SecItem.SecItemDelete (record.queryDict.Handle);
 }
        /// <summary>
        /// Sets a password in the OSX keychain
        /// </summary>
        /// <param name='username'>
        /// Username
        /// </param>
        /// <param name='password'>
        /// Password
        /// </param>
        public static void SetPassword(string username, string password)
        {
            SecRecord searchRecord;
            var record = FetchRecord(username, out searchRecord);

            if (record == null)
            {
                record = new SecRecord(SecKind.InternetPassword)
                {
                    Service = ServiceName,
                    Label = ServiceName,
                    Account = username,
                    ValueData = NSData.FromString(password)
                };

                SecKeyChain.Add(record);
                return;
            }

            record.ValueData = NSData.FromString(password);
            SecKeyChain.Update(searchRecord, record);
        }
Esempio n. 19
0
        public static object QueryAsConcreteType(SecRecord query, out SecStatusCode result)
        {
            if (query == null){
                result = SecStatusCode.Param;
                return null;
            }

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnRef);
                SetLimit (copy, 1);

                IntPtr ptr;
                result = SecItem.SecItemCopyMatching (copy.Handle, out ptr);
                if (result == SecStatusCode.Success){
                    int cfType = CFType.GetTypeID (ptr);

                    if (cfType == SecCertificate.GetTypeID ())
                        return new SecCertificate (ptr, true);
                    else if (cfType == SecKey.GetTypeID ())
                        return new SecKey (ptr, true);
                    else if (cfType == SecIdentity.GetTypeID ())
                        return new SecIdentity (ptr, true);
                    else
                        throw new Exception (String.Format ("Unexpected type: 0x{0:x}", cfType));
                }
                return null;
            }
        }
Esempio n. 20
0
        private static SecRecord FetchKeychainRecord(out SecRecord searchRecord)
        {
            searchRecord = new SecRecord(SecKind.InternetPassword)
            {
                Service = _serviceName,
                Account = _account
            };

            SecStatusCode code;
            var data = SecKeyChain.QueryAsRecord(searchRecord, out code);

            if (code == SecStatusCode.Success)
                return data;
            else
                return null;
        }
Esempio n. 21
0
        public static NSData QueryAsData(SecRecord query, bool wantPersistentReference, out SecStatusCode status)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            using (var copy = NSMutableDictionary.FromDictionary (query.queryDict)){
                SetLimit (copy, 1);
                copy.LowlevelSetObject (CFBoolean.True.Handle, SecItem.ReturnData);

                IntPtr ptr;
                status = SecItem.SecItemCopyMatching (copy.Handle, out ptr);
                if (status == SecStatusCode.Success)
                    return new NSData (ptr);
                return null;
            }
        }
Esempio n. 22
0
        public static NSData QueryAsData(SecRecord query, bool wantPersistentReference)
        {
            if (query == null)
                throw new ArgumentNullException ("query");

            NSData result;

            var code = TryQueryAsData (query, wantPersistentReference, out result);
            if (code != SecStatusCode.Success)
                throw new SecurityException (code);
            return result;
        }