Exemplo n.º 1
0
    public void ThrowOnError_Failure()
    {
        SECURITY_STATUS status = SECURITY_STATUS.NTE_BAD_DATA;

        try
        {
            status.ThrowOnError();
            Assert.False(true, "Expected exception not thrown.");
        }
        catch (SecurityStatusException ex)
        {
            Assert.Equal("Bad Data (SECURITY_STATUS error: NTE_BAD_DATA (0x80090005))", ex.Message);
        }
    }
Exemplo n.º 2
0
    public unsafe void NCryptEnumKeys_IntPtr_Test()
    {
        using (var provider = NCryptOpenStorageProvider(KeyStorageProviders.MS_KEY_STORAGE_PROVIDER))
        {
            const string    scope = null;
            IntPtr          ipkeyName;
            IntPtr          enumState = IntPtr.Zero;
            SECURITY_STATUS status    = NCryptEnumKeys(provider, scope, out ipkeyName, ref enumState);
            while (status == SECURITY_STATUS.ERROR_SUCCESS)
            {
                var keyName = (NCryptKeyName *)ipkeyName.ToPointer();
                this.logger.WriteLine($"{keyName->Name} ({keyName->Algid})");

                if (keyName->Name.StartsWith("PclCrypto_"))
                {
                    using (var key = NCryptOpenKey(provider, *keyName))
                    {
                        NCryptDeleteKey(key).ThrowOnError();
                        key.SetHandleAsInvalid();
                    }
                }

                NCryptFreeBuffer(keyName).ThrowOnError();
                status = NCryptEnumKeys(provider, scope, out ipkeyName, ref enumState);
            }

            if (enumState != null)
            {
                NCryptFreeBuffer(enumState).ThrowOnError();
            }

            if (status != SECURITY_STATUS.NTE_NO_MORE_ITEMS)
            {
                status.ThrowOnError();
            }
        }
    }