CredFree() private method

private CredFree ( IntPtr credential ) : void
credential System.IntPtr
return void
Esempio n. 1
0
        public IEnumerable <SecureData> EnumerateSecureData(string prefix)
        {
            string filter = prefix ?? string.Empty + "*";

            if (NativeMethods.CredEnumerate(filter, 0, out int count, out IntPtr credentialArrayPtr))
            {
                Trace.WriteLine($"{count} credentials enumerated from secret store.");

                try
                {
                    for (int i = 0; i < count; i += 1)
                    {
                        int    offset        = i * Marshal.SizeOf(typeof(IntPtr));
                        IntPtr credentialPtr = Marshal.ReadIntPtr(credentialArrayPtr, offset);

                        if (credentialPtr != IntPtr.Zero)
                        {
                            NativeMethods.Credential credStruct = Marshal.PtrToStructure <NativeMethods.Credential>(credentialPtr);
                            int passwordLength = credStruct.CredentialBlobSize;

                            byte[] data = new byte[credStruct.CredentialBlobSize];
                            Marshal.Copy(credStruct.CredentialBlob, data, 0, credStruct.CredentialBlobSize);

                            string name = credStruct.UserName ?? string.Empty;
                            string key  = credStruct.TargetName;

                            yield return(new SecureData(key, name, data));
                        }
                    }
                }
                finally
                {
                    NativeMethods.CredFree(credentialArrayPtr);
                }
            }
Esempio n. 2
0
        protected Credential ReadCredentials(string targetName)
        {
            Credential credentials = null;
            IntPtr     credPtr     = IntPtr.Zero;

            try
            {
                if (NativeMethods.CredRead(targetName, NativeMethods.CredentialType.Generic, 0, out credPtr))
                {
                    NativeMethods.Credential credStruct = (NativeMethods.Credential)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.Credential));
                    int passwordLength = (int)credStruct.CredentialBlobSize;

                    string password = passwordLength > 0
                                    ? Marshal.PtrToStringUni(credStruct.CredentialBlob, passwordLength / sizeof(char))
                                    : String.Empty;
                    string username = credStruct.UserName ?? String.Empty;

                    credentials = new Credential(username, password);

                    Git.Trace.WriteLine($"credentials for '{targetName}' read from store.");
                }
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }

            return(credentials);
        }
Esempio n. 3
0
        protected void PurgeCredentials(string @namespace)
        {
            string filter = @namespace + "*";
            int    count;
            IntPtr credentialArrayPtr;

            if (NativeMethods.CredEnumerate(filter, 0, out count, out credentialArrayPtr))
            {
                for (int i = 0; i < count; i += 1)
                {
                    int    offset        = i * Marshal.SizeOf(typeof(IntPtr));
                    IntPtr credentialPtr = Marshal.ReadIntPtr(credentialArrayPtr, offset);

                    if (credentialPtr != IntPtr.Zero)
                    {
                        NativeMethods.Credential credential = Marshal.PtrToStructure <NativeMethods.Credential>(credentialPtr);

                        if (!NativeMethods.CredDelete(credential.TargetName, credential.Type, 0))
                        {
                            int error = Marshal.GetLastWin32Error();
                            Debug.Fail("Failed with error code " + error.ToString("X"));
                        }
                    }
                }

                NativeMethods.CredFree(credentialArrayPtr);
            }
            else
            {
                int error = Marshal.GetLastWin32Error();
                Debug.Fail("Failed with error code " + error.ToString("X"));
            }
        }
        protected Token ReadToken(string targetName)
        {
            Token  token   = null;
            IntPtr credPtr = IntPtr.Zero;

            try
            {
                if (NativeMethods.CredRead(targetName, NativeMethods.CredentialType.Generic, 0, out credPtr))
                {
                    NativeMethods.Credential credStruct = (NativeMethods.Credential)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.Credential));
                    if (credStruct.CredentialBlob != null && credStruct.CredentialBlobSize > 0)
                    {
                        int    size  = (int)credStruct.CredentialBlobSize;
                        byte[] bytes = new byte[size];
                        Marshal.Copy(credStruct.CredentialBlob, bytes, 0, size);

                        TokenType type;
                        if (Token.GetTypeFromFriendlyName(credStruct.UserName, out type))
                        {
                            Token.Deserialize(bytes, type, out token);
                        }

                        Git.Trace.WriteLine($"token for '{targetName}' read from store.");
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);

                Git.Trace.WriteLine($"failed to read credentials: {exception.GetType().Name}.");

                return(null);
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }

            return(token);
        }
        protected Credential ReadCredentials(string targetName)
        {
            Trace.WriteLine("BaseSecureStore::ReadCredentials");

            Credential credentials = null;
            IntPtr     credPtr     = IntPtr.Zero;

            try
            {
                if (!NativeMethods.CredRead(targetName, NativeMethods.CredentialType.Generic, 0, out credPtr))
                {
                    return(null);
                }

                NativeMethods.Credential credStruct = (NativeMethods.Credential)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.Credential));

                // https://msdn.microsoft.com/en-us/library/gg309393.aspx
                int          size = (int)credStruct.CredentialBlobSize;
                SecureString pwd  = null;
                if (size != 0)
                {
                    byte[] bpassword = new byte[size];
                    Marshal.Copy(credStruct.CredentialBlob, bpassword, 0, size);
                    char[] chars = Encoding.Unicode.GetChars(bpassword);
                    pwd = ConvertToSecureString(chars);
                    Array.Clear(chars, 0, chars.Length);
                    Array.Clear(bpassword, 0, bpassword.Length);
                }

                credentials = new Credential(credStruct.UserName, pwd);
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }

            return(credentials);
        }
        protected Token ReadToken(string targetName)
        {
            Trace.WriteLine("BaseSecureStore::ReadToken");

            Token  token   = null;
            IntPtr credPtr = IntPtr.Zero;

            try
            {
                if (!NativeMethods.CredRead(targetName, NativeMethods.CredentialType.Generic, 0, out credPtr))
                {
                    return(null);
                }

                NativeMethods.Credential credStruct = (NativeMethods.Credential)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.Credential));
                if (credStruct.CredentialBlob == null || credStruct.CredentialBlobSize <= 0)
                {
                    return(null);
                }

                int    size  = (int)credStruct.CredentialBlobSize;
                byte[] bytes = new byte[size];
                Marshal.Copy(credStruct.CredentialBlob, bytes, 0, size);

                TokenType type;
                if (Token.GetTypeFromFriendlyName(credStruct.UserName, out type))
                {
                    Token.Deserialize(bytes, type, out token);
                }
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }

            return(token);
        }
        protected IEnumerable <Secret> EnumerateCredentials(string @namespace)
        {
            string filter = @namespace ?? string.Empty + "*";

            if (NativeMethods.CredEnumerate(filter, 0, out int count, out IntPtr credentialArrayPtr))
            {
                Trace.WriteLine($"{count} credentials enumerated from secret store.");

                try
                {
                    for (int i = 0; i < count; i += 1)
                    {
                        int    offset        = i * Marshal.SizeOf(typeof(IntPtr));
                        IntPtr credentialPtr = Marshal.ReadIntPtr(credentialArrayPtr, offset);

                        if (credentialPtr != IntPtr.Zero)
                        {
                            NativeMethods.Credential credStruct = Marshal.PtrToStructure <NativeMethods.Credential>(credentialPtr);
                            int passwordLength = (int)credStruct.CredentialBlobSize;

                            string password = passwordLength > 0
                                            ? Marshal.PtrToStringUni(credStruct.CredentialBlob, passwordLength / sizeof(char))
                                            : string.Empty;
                            string username = credStruct.UserName ?? string.Empty;

                            var credentials = new Credential(username, password);

                            yield return(credentials);
                        }
                    }
                }
                finally
                {
                    NativeMethods.CredFree(credentialArrayPtr);
                }
            }