Beispiel #1
0
        async partial void AuthenticateClicked(NSButton sender)
        {
            //_cancel = swAutoCancel.State == NSCellStateValue.On ? new CancellationTokenSource(TimeSpan.FromSeconds(10)) : new CancellationTokenSource();
            //lblStatus.StringValue = "";
            //var result = await Plugin.Fingerprint.CrossFingerprint.Current.AuthenticateAsync("Prove you have fingers!", _cancel.Token);

            //SetResult(result);

            var secObject = new SecAccessControl(SecAccessible.WhenPasscodeSetThisDeviceOnly, SecAccessControlCreateFlags.TouchIDCurrentSet);

            if (secObject == null)
            {
                string message = "Sec object broken";
                lblStatus.StringValue += message;
            }

            var securityRecord = new SecRecord(SecKind.Key)
            {
                Service       = "test",
                ValueData     = new NSString("Secret Data").Encode(NSStringEncoding.UTF8),
                AccessControl = secObject
            };

            DispatchQueue.MainQueue.DispatchAsync(() => {
                SecStatusCode status = SecKeyChain.Add(securityRecord);

                lblStatus.StringValue = status.ToString();
            });
        }
 public static void AndThrowExceptionOnFailure(this SecStatusCode status)
 {
     if (status != SecStatusCode.Success)
     {
         throw new ArgumentException(status.ToString());
     }
 }
        public override Task <bool> AddSecureDataAsync(string key, string value)
        {
            var secObject = new SecAccessControl(SecAccessible.WhenPasscodeSetThisDeviceOnly, SecAccessControlCreateFlags.TouchIDCurrentSet);

            if (secObject == null)
            {
                //todo:handle error
            }

            var securityRecord = new SecRecord(SecKind.Key)
            {
                Service       = key,
                ValueData     = new NSString(value).Encode(NSStringEncoding.UTF8),
                AccessControl = secObject
            };

            TaskCompletionSource <bool> response = new TaskCompletionSource <bool>();

            DispatchQueue.MainQueue.DispatchAsync(() =>
            {
                SecStatusCode status = SecKeyChain.Add(securityRecord);
                if (status == SecStatusCode.Success)
                {
                    response.TrySetResult(true);
                }
                else
                {
                    throw new Exception(status.ToString());
                }
            });
            return(response.Task);
        }
Beispiel #4
0
        public static string GetDescription(this SecStatusCode code)
        {
            string description = string.Empty;

            switch (code)
            {
            case SecStatusCode.Success:
                description = Text.SUCCESS;
                break;

            case SecStatusCode.DuplicateItem:
                description = Text.ERROR_ITEM_ALREADY_EXISTS;
                break;

            case SecStatusCode.ItemNotFound:
                description = Text.ERROR_ITEM_NOT_FOUND;
                break;

            case SecStatusCode.AuthFailed:
                description = Text.ERROR_ITEM_AUTHENTICATION_FAILED;
                break;

            default:
                description = code.ToString();
                break;
            }

            return(description);
        }
Beispiel #5
0
        // the API accept the handle for a single policy or an array of them
        void SetPolicies(IntPtr policy)
        {
            SecStatusCode result = SecTrustSetPolicies(handle, policy);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
        }
Beispiel #6
0
        void SetOCSPResponse(IntPtr ocsp)
        {
            SecStatusCode result = SecTrustSetOCSPResponse(handle, ocsp);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
        }
Beispiel #7
0
        void Initialize(IntPtr certHandle, SecPolicy policy)
        {
            SecStatusCode result = SecTrustCreateWithCertificates(certHandle, policy == null ? IntPtr.Zero : policy.Handle, out handle);

            if (result != SecStatusCode.Success)
            {
                throw new ArgumentException(result.ToString());
            }
        }
Beispiel #8
0
        void Initialize(IntPtr certHandle, SecPolicy?policy)
        {
            SecStatusCode result = SecTrustCreateWithCertificates(certHandle, policy.GetHandle(), out var handle);

            if (result != SecStatusCode.Success)
            {
                throw new ArgumentException(result.ToString());
            }
            InitializeHandle(handle);
        }
Beispiel #9
0
        public SecPolicy[] GetPolicies()
        {
            IntPtr        p      = IntPtr.Zero;
            SecStatusCode result = SecTrustCopyPolicies(handle, ref p);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
            return(NSArray.ArrayFromHandle <SecPolicy> (p));
        }
Beispiel #10
0
        public SecTrustResult GetTrustResult()
        {
            SecTrustResult trust_result;
            SecStatusCode  result = SecTrustGetTrustResult(handle, out trust_result);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
            return(trust_result);
        }
Beispiel #11
0
        public SecCertificate[] GetCustomAnchorCertificates()
        {
            IntPtr        p;
            SecStatusCode result = SecTrustCopyCustomAnchorCertificates(handle, out p);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
            return(NSArray.ArrayFromHandle <SecCertificate> (p));
        }
Beispiel #12
0
        public SecTrustResult Evaluate()
        {
            SecTrustResult trust;
            SecStatusCode  result = SecTrustEvaluate(GetCheckedHandle(), out trust);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
            return(trust);
        }
Beispiel #13
0
        void Initialize(IntPtr certHandle, SecPolicy policy)
        {
            if (policy == null)
            {
                throw new ArgumentNullException("policy");
            }
            IntPtr        handle;
            SecStatusCode result = SecTrustCreateWithCertificates(certHandle, policy.Handle, out handle);

            if (result != SecStatusCode.Success)
            {
                throw new ArgumentException(result.ToString());
            }
            Handle = handle;
        }
Beispiel #14
0
        public SecTrustResult Evaluate()
        {
            if (handle == IntPtr.Zero)
            {
                throw new ObjectDisposedException("SecTrust");
            }

            SecTrustResult trust;
            SecStatusCode  result = SecTrustEvaluate(handle, out trust);

            if (result != SecStatusCode.Success)
            {
                throw new InvalidOperationException(result.ToString());
            }
            return(trust);
        }
Beispiel #15
0
        public static SecIdentity Import(byte[] data, string password)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (string.IsNullOrEmpty(password))              // SecPKCS12Import() doesn't allow empty passwords.
            {
                throw new ArgumentException("password");
            }
            using (var pwstring = new NSString(password))
                using (var options = NSDictionary.FromObjectAndKey(pwstring, SecImportExport.Passphrase)) {
                    NSDictionary[] array;
                    SecStatusCode  result = SecImportExport.ImportPkcs12(data, options, out array);
                    if (result != SecStatusCode.Success)
                    {
                        throw new InvalidOperationException(result.ToString());
                    }

                    return(new SecIdentity(array [0].LowlevelObjectForKey(SecImportExport.Identity.Handle)));
                }
        }
Beispiel #16
0
        public static SecIdentity Import(byte[] data, string password, ImportOptions options = null)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (string.IsNullOrEmpty(password))              // SecPKCS12Import() doesn't allow empty passwords.
            {
                throw new ArgumentException("password");
            }
            using (var pwstring = CFString.Create(password))
                using (var optionDict = CreateImportOptions(pwstring, options)) {
                    CFDictionary [] array;
                    SecStatusCode   result = SecImportExport.ImportPkcs12(data, optionDict, out array);
                    if (result != SecStatusCode.Success)
                    {
                        throw new InvalidOperationException(result.ToString());
                    }

                    return(new SecIdentity(array [0].GetValue(ImportItemIdentity.Handle)));
                }
        }
Beispiel #17
0
        public Task WriteAsync(string key, string contents)
        {
            if (string.IsNullOrEmpty(key) || contents == null)
            {
                throw new ArgumentException(Resources.ObjectStoreParametersEmpty);
            }

            SecStatusCode status = SecStatusCode.IO;

            try
            {
                SecRecord record = ExistingSecRecordForKey(key);

                SecRecord newRecord = NewSecRecordForKey(key);

                if (record == null)
                {
                    newRecord.ValueData = NSData.FromString(contents);

                    status = SecKeyChain.Add(newRecord);
                }
                else
                {
                    SecRecord update = new SecRecord()
                    {
                        ValueData = NSData.FromString(contents)
                    };

                    status = SecKeyChain.Update(newRecord, update);
                }
            }
            catch (Exception e)
            {
                throw new IOException(string.Format(ClientResources.FileAccessErrorMessage, ClientResources.FileAccessActionWrite, key), e);
            }

            if (status != SecStatusCode.Success)
            {
                throw new IOException(string.Format(ClientResources.FileAccessErrorMessage, ClientResources.FileAccessActionWrite, key, status.ToString()));
            }

            return(Task.CompletedTask);
        }
Beispiel #18
0
        public Task DeleteAsync(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException(Resources.ObjectStoreParametersEmpty);
            }

            SecStatusCode status = SecKeyChain.Remove(NewSecRecordForKey(key));

            if (status != SecStatusCode.Success && status != SecStatusCode.ItemNotFound)
            {
                throw new IOException(string.Format(ClientResources.FileAccessErrorMessage, ClientResources.FileAccessActionDelete, key, status.ToString()));
            }

            return(Task.CompletedTask);
        }
Beispiel #19
0
 static string ToMessage(SecStatusCode code)
 {
     switch (code){
     case SecStatusCode.Success:
     case SecStatusCode.Unimplemented:
     case SecStatusCode.Param:
     case SecStatusCode.Allocate:
     case SecStatusCode.NotAvailable:
     case SecStatusCode.DuplicateItem:
     case SecStatusCode.ItemNotFound:
     case SecStatusCode.InteractionNotAllowed:
     case SecStatusCode.Decode:
         return code.ToString ();
     }
     return String.Format ("Unknown error: 0x{0:x}", code);
 }